Skip to content

Commit 4627242

Browse files
committed
Modified the calculate_brightness function to reduce flickering on gu
1 parent 0b0474e commit 4627242

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

micropython/examples/galactic_unicorn/light_sensor.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
MIN_RANGE = 0.1
3131
MAX_RANGE = 1
3232

33+
# Rate of display change
34+
TRANSITION_RATE = 1.0 / 32.0
35+
3336

3437
# perform linear interpolation to map a range of values to discrete
3538
def map_range(
@@ -45,9 +48,14 @@ def map_range(
4548

4649

4750
# 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)
5159
if brightness_val > 1:
5260
brightness_val = 1
5361
elif brightness_val < 0.1:
@@ -59,7 +67,7 @@ def calculate_brightness(current_lsv):
5967
# draws percentage icon
6068
def draw_percentage(x, y):
6169
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)
6371
graphics.rectangle(x + 4, y + 4, 2, 2)
6472

6573

@@ -69,17 +77,17 @@ def clear():
6977
graphics.clear()
7078

7179

72-
mode = "auto"
80+
mode = "auto" # set auto brightness on
7381
last = time.ticks_ms()
82+
brightness_value = MIN_RANGE # set the initial brightness level to the minimum
7483
while True:
7584
current = time.ticks_ms()
7685

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)
8088
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
8391

8492
# deactivate auto brightness by pressing A
8593
if gu.is_pressed(GalacticUnicorn.SWITCH_A):
@@ -110,10 +118,11 @@ def clear():
110118
# draw the text
111119
graphics.set_pen(CURRENT_COLOUR)
112120
graphics.text("BRT: ", 0, 1, scale=1)
121+
113122
# 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)
115124
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)
117126

118127
# draw a bar for the background
119128
graphics.set_pen(GREY)

0 commit comments

Comments
 (0)