30
30
MIN_RANGE = 0.1
31
31
MAX_RANGE = 1
32
32
33
+ # Rate of display change
34
+ TRANSITION_RATE = 1.0 / 32.0
35
+
33
36
34
37
# perform linear interpolation to map a range of values to discrete
35
38
def map_range (
@@ -45,9 +48,14 @@ def map_range(
45
48
46
49
47
50
# 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 )
51
+ # clamps the brightness value it outside the ranges specified
52
+ def calculate_brightness (prev_brightness_val ):
53
+ current_lsv = gu .light ()
54
+ current_brightness_val = map_range (current_lsv )
55
+
56
+ # uses the previous value to smooth out display changes reducing flickering
57
+ brightness_diff = current_brightness_val - prev_brightness_val
58
+ brightness_val = prev_brightness_val + (brightness_diff * TRANSITION_RATE )
51
59
if brightness_val > 1 :
52
60
brightness_val = 1
53
61
elif brightness_val < 0.1 :
@@ -59,7 +67,7 @@ def calculate_brightness(current_lsv):
59
67
# draws percentage icon
60
68
def draw_percentage (x , y ):
61
69
graphics .rectangle (x + 1 , y + 1 , 2 , 2 )
62
- graphics .line (x , y + 6 , x + 6 , y )
70
+ graphics .line (x + 1 , y + 5 , x + 6 , y )
63
71
graphics .rectangle (x + 4 , y + 4 , 2 , 2 )
64
72
65
73
@@ -69,17 +77,17 @@ def clear():
69
77
graphics .clear ()
70
78
71
79
72
- mode = "auto"
80
+ mode = "auto" # set auto brightness on
73
81
last = time .ticks_ms ()
82
+ brightness_value = MIN_RANGE # set the initial brightness level to the minimum
74
83
while True :
75
84
current = time .ticks_ms ()
76
85
77
- # get light sensor value from the sensor
78
- ls_value = gu .light ()
79
- brightness_value = calculate_brightness (ls_value )
86
+ # set the display brightness
87
+ brightness_value = calculate_brightness (brightness_value )
80
88
gu .set_brightness (brightness_value )
81
- # calculate brightness percentage
82
- bp = (brightness_value / MAX_RANGE ) * 100
89
+
90
+ bp = (brightness_value / MAX_RANGE ) * 100 # gets brightness value in percentage relative to the MAX_LS_VALUE set
83
91
84
92
# deactivate auto brightness by pressing A
85
93
if gu .is_pressed (GalacticUnicorn .SWITCH_A ):
@@ -110,10 +118,11 @@ def clear():
110
118
# draw the text
111
119
graphics .set_pen (CURRENT_COLOUR )
112
120
graphics .text ("BRT: " , 0 , 1 , scale = 1 )
121
+
113
122
# measure the rest of the text before drawing to right align it
114
- text_width = graphics .measure_text (f"{ bp :.0f} /° " , scale = 1 )
123
+ text_width = graphics .measure_text (f"{ bp :.0f} " , scale = 1 )
115
124
graphics .text (f"{ bp :.0f} " , WIDTH - text_width , 1 , scale = 1 )
116
- draw_percentage ((WIDTH - text_width ) + 10 , 1 )
125
+ draw_percentage ((WIDTH - 8 ) , 1 )
117
126
118
127
# draw a bar for the background
119
128
graphics .set_pen (GREY )
0 commit comments