Skip to content

Commit 7e65c15

Browse files
committed
Reduced flickering
1 parent 4627242 commit 7e65c15

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

micropython/examples/cosmic_unicorn/light_sensor.py

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

33+
# Rate of display change i.e the lower the value the slower the transition
34+
TRANSITION_RATE = 1.0 / 32.0
3335

3436
# perform linear interpolation to map a range of values to discrete
3537
def map_range(
@@ -45,9 +47,14 @@ def map_range(
4547

4648

4749
# 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)
5158
if brightness_val > 1:
5259
brightness_val = 1
5360
elif brightness_val < 0.1:
@@ -64,7 +71,7 @@ def clear():
6471

6572
def draw_percentage(x, y):
6673
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)
6875
graphics.rectangle(x + 4, y + 4, 2, 2)
6976

7077

@@ -93,15 +100,17 @@ def draw_sun(x, y, r):
93100

94101
mode = "auto"
95102
last = time.ticks_ms()
103+
104+
brightness_value = MIN_RANGE # set the initial brightness level to the minimum
96105
while True:
97106
current = time.ticks_ms()
98107

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)
102110
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+
105114

106115
# deactivate auto brightness by pressing A
107116
if cu.is_pressed(CosmicUnicorn.SWITCH_A):
@@ -133,20 +142,20 @@ def draw_sun(x, y, r):
133142
graphics.set_pen(CURRENT_COLOUR)
134143
graphics.text("BRT ", 0, 1, scale=1)
135144
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)
138147

139148
# draw sun icon
140-
draw_sun(0, 12, 2)
149+
draw_sun(0, 10, 2)
141150

142151
# draw a bar for the background
143152
bar_width = WIDTH - 12
144153
graphics.set_pen(GREY)
145-
graphics.rectangle(13, 12, bar_width, 10)
154+
graphics.rectangle(13, 10, bar_width, 11)
146155

147156
# draw a bar for the current brightness percentage
148157
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)
150159

151160
last = current
152161

micropython/examples/galactic_unicorn/light_sensor.py

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

33-
# Rate of display change
33+
# Rate of display change i.e the lower the value the slower the transition
3434
TRANSITION_RATE = 1.0 / 32.0
3535

3636

@@ -79,7 +79,7 @@ def clear():
7979

8080
mode = "auto" # set auto brightness on
8181
last = time.ticks_ms()
82-
brightness_value = MIN_RANGE # set the initial brightness level to the minimum
82+
brightness_value = MIN_RANGE # set the initial brightness level to the specified minimum
8383
while True:
8484
current = time.ticks_ms()
8585

micropython/examples/stellar_unicorn/light_sensor.py

Lines changed: 18 additions & 7 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 i.e the lower the value the slower the transition
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 = su.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

@@ -71,13 +79,16 @@ def clear():
7179

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

77-
# get light sensor value from the sensor
78-
ls_value = su.light()
79-
brightness_value = calculate_brightness(ls_value)
86+
# set the display brightness
87+
brightness_value = calculate_brightness(brightness_value)
8088
su.set_brightness(brightness_value)
89+
90+
bp = (brightness_value / MAX_RANGE) * 100 # gets brightness value in percentage relative to the MAX_LS_VALUE set
91+
8192
# calculate brightness percentage
8293
bp = (brightness_value / MAX_RANGE) * 100
8394

0 commit comments

Comments
 (0)