30
30
MIN_RANGE = 0.1
31
31
MAX_RANGE = 1
32
32
33
+ # Rate of display change i.e the lower the value the slower the transition
34
+ TRANSITION_RATE = 1.0 / 32.0
33
35
34
36
# perform linear interpolation to map a range of values to discrete
35
37
def map_range (
@@ -45,9 +47,14 @@ def map_range(
45
47
46
48
47
49
# gets the light sensor value from onboard sensor and interpolates it
48
- # clamps the brightness values
49
- def calculate_brightness (current_lsv ):
50
- brightness_val = map_range (current_lsv )
50
+ # clamps the brightness value it outside the ranges specified
51
+ def calculate_brightness (prev_brightness_val ):
52
+ current_lsv = cu .light ()
53
+ current_brightness_val = map_range (current_lsv )
54
+
55
+ # uses the previous value to smooth out display changes reducing flickering
56
+ brightness_diff = current_brightness_val - prev_brightness_val
57
+ brightness_val = prev_brightness_val + (brightness_diff * TRANSITION_RATE )
51
58
if brightness_val > 1 :
52
59
brightness_val = 1
53
60
elif brightness_val < 0.1 :
@@ -64,7 +71,7 @@ def clear():
64
71
65
72
def draw_percentage (x , y ):
66
73
graphics .rectangle (x + 1 , y + 1 , 2 , 2 )
67
- graphics .line (x , y + 6 , x + 6 , y )
74
+ graphics .line (x + 1 , y + 5 , x + 6 , y )
68
75
graphics .rectangle (x + 4 , y + 4 , 2 , 2 )
69
76
70
77
@@ -93,15 +100,17 @@ def draw_sun(x, y, r):
93
100
94
101
mode = "auto"
95
102
last = time .ticks_ms ()
103
+
104
+ brightness_value = MIN_RANGE # set the initial brightness level to the minimum
96
105
while True :
97
106
current = time .ticks_ms ()
98
107
99
- # get light sensor value from the sensor
100
- ls_value = cu .light ()
101
- brightness_value = calculate_brightness (ls_value )
108
+ # set the display brightness
109
+ brightness_value = calculate_brightness (brightness_value )
102
110
cu .set_brightness (brightness_value )
103
- # calculate brightness percentage
104
- bp = (brightness_value / MAX_RANGE ) * 100
111
+
112
+ bp = (brightness_value / MAX_RANGE ) * 100 # gets brightness value in percentage relative to the MAX_LS_VALUE set
113
+
105
114
106
115
# deactivate auto brightness by pressing A
107
116
if cu .is_pressed (CosmicUnicorn .SWITCH_A ):
@@ -133,20 +142,20 @@ def draw_sun(x, y, r):
133
142
graphics .set_pen (CURRENT_COLOUR )
134
143
graphics .text ("BRT " , 0 , 1 , scale = 1 )
135
144
draw_percentage (15 , 1 )
136
- graphics .text (f"{ bp :.0f} " , 9 , 24 , scale = 1 )
137
- draw_percentage (20 , 24 )
145
+ graphics .text (f"{ bp :.0f} " , 7 , 23 , scale = 1 )
146
+ draw_percentage (( WIDTH - 10 ), 23 )
138
147
139
148
# draw sun icon
140
- draw_sun (0 , 12 , 2 )
149
+ draw_sun (0 , 10 , 2 )
141
150
142
151
# draw a bar for the background
143
152
bar_width = WIDTH - 12
144
153
graphics .set_pen (GREY )
145
- graphics .rectangle (13 , 12 , bar_width , 10 )
154
+ graphics .rectangle (13 , 10 , bar_width , 11 )
146
155
147
156
# draw a bar for the current brightness percentage
148
157
graphics .set_pen (CURRENT_COLOUR )
149
- graphics .rectangle (13 , 12 , int ((bp / 100 ) * bar_width ), 10 )
158
+ graphics .rectangle (13 , 10 , int ((bp / 100 ) * bar_width ), 11 )
150
159
151
160
last = current
152
161
0 commit comments