Reduced flickering
This commit is contained in:
parent
462724210c
commit
7e65c15cfb
|
@ -30,6 +30,8 @@ MAX_LS_VALUE = 295 # 4095 to use the full range
|
||||||
MIN_RANGE = 0.1
|
MIN_RANGE = 0.1
|
||||||
MAX_RANGE = 1
|
MAX_RANGE = 1
|
||||||
|
|
||||||
|
# Rate of display change i.e the lower the value the slower the transition
|
||||||
|
TRANSITION_RATE = 1.0 / 32.0
|
||||||
|
|
||||||
# perform linear interpolation to map a range of values to discrete
|
# perform linear interpolation to map a range of values to discrete
|
||||||
def map_range(
|
def map_range(
|
||||||
|
@ -45,9 +47,14 @@ def map_range(
|
||||||
|
|
||||||
|
|
||||||
# gets the light sensor value from onboard sensor and interpolates it
|
# gets the light sensor value from onboard sensor and interpolates it
|
||||||
# clamps the brightness values
|
# clamps the brightness value it outside the ranges specified
|
||||||
def calculate_brightness(current_lsv):
|
def calculate_brightness(prev_brightness_val):
|
||||||
brightness_val = map_range(current_lsv)
|
current_lsv = cu.light()
|
||||||
|
current_brightness_val = map_range(current_lsv)
|
||||||
|
|
||||||
|
# uses the previous value to smooth out display changes reducing flickering
|
||||||
|
brightness_diff = current_brightness_val - prev_brightness_val
|
||||||
|
brightness_val = prev_brightness_val + (brightness_diff * TRANSITION_RATE)
|
||||||
if brightness_val > 1:
|
if brightness_val > 1:
|
||||||
brightness_val = 1
|
brightness_val = 1
|
||||||
elif brightness_val < 0.1:
|
elif brightness_val < 0.1:
|
||||||
|
@ -64,7 +71,7 @@ def clear():
|
||||||
|
|
||||||
def draw_percentage(x, y):
|
def draw_percentage(x, y):
|
||||||
graphics.rectangle(x + 1, y + 1, 2, 2)
|
graphics.rectangle(x + 1, y + 1, 2, 2)
|
||||||
graphics.line(x, y + 6, x + 6, y)
|
graphics.line(x + 1, y + 5, x + 6, y)
|
||||||
graphics.rectangle(x + 4, y + 4, 2, 2)
|
graphics.rectangle(x + 4, y + 4, 2, 2)
|
||||||
|
|
||||||
|
|
||||||
|
@ -93,15 +100,17 @@ def draw_sun(x, y, r):
|
||||||
|
|
||||||
mode = "auto"
|
mode = "auto"
|
||||||
last = time.ticks_ms()
|
last = time.ticks_ms()
|
||||||
|
|
||||||
|
brightness_value = MIN_RANGE # set the initial brightness level to the minimum
|
||||||
while True:
|
while True:
|
||||||
current = time.ticks_ms()
|
current = time.ticks_ms()
|
||||||
|
|
||||||
# get light sensor value from the sensor
|
# set the display brightness
|
||||||
ls_value = cu.light()
|
brightness_value = calculate_brightness(brightness_value)
|
||||||
brightness_value = calculate_brightness(ls_value)
|
|
||||||
cu.set_brightness(brightness_value)
|
cu.set_brightness(brightness_value)
|
||||||
# calculate brightness percentage
|
|
||||||
bp = (brightness_value / MAX_RANGE) * 100
|
bp = (brightness_value / MAX_RANGE) * 100 # gets brightness value in percentage relative to the MAX_LS_VALUE set
|
||||||
|
|
||||||
|
|
||||||
# deactivate auto brightness by pressing A
|
# deactivate auto brightness by pressing A
|
||||||
if cu.is_pressed(CosmicUnicorn.SWITCH_A):
|
if cu.is_pressed(CosmicUnicorn.SWITCH_A):
|
||||||
|
@ -133,20 +142,20 @@ while True:
|
||||||
graphics.set_pen(CURRENT_COLOUR)
|
graphics.set_pen(CURRENT_COLOUR)
|
||||||
graphics.text("BRT ", 0, 1, scale=1)
|
graphics.text("BRT ", 0, 1, scale=1)
|
||||||
draw_percentage(15, 1)
|
draw_percentage(15, 1)
|
||||||
graphics.text(f"{bp:.0f}", 9, 24, scale=1)
|
graphics.text(f"{bp:.0f}", 7, 23, scale=1)
|
||||||
draw_percentage(20, 24)
|
draw_percentage((WIDTH - 10), 23)
|
||||||
|
|
||||||
# draw sun icon
|
# draw sun icon
|
||||||
draw_sun(0, 12, 2)
|
draw_sun(0, 10, 2)
|
||||||
|
|
||||||
# draw a bar for the background
|
# draw a bar for the background
|
||||||
bar_width = WIDTH - 12
|
bar_width = WIDTH - 12
|
||||||
graphics.set_pen(GREY)
|
graphics.set_pen(GREY)
|
||||||
graphics.rectangle(13, 12, bar_width, 10)
|
graphics.rectangle(13, 10, bar_width, 11)
|
||||||
|
|
||||||
# draw a bar for the current brightness percentage
|
# draw a bar for the current brightness percentage
|
||||||
graphics.set_pen(CURRENT_COLOUR)
|
graphics.set_pen(CURRENT_COLOUR)
|
||||||
graphics.rectangle(13, 12, int((bp / 100) * bar_width), 10)
|
graphics.rectangle(13, 10, int((bp / 100) * bar_width), 11)
|
||||||
|
|
||||||
last = current
|
last = current
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,7 @@ MAX_LS_VALUE = 295 # 4095 to use the full range
|
||||||
MIN_RANGE = 0.1
|
MIN_RANGE = 0.1
|
||||||
MAX_RANGE = 1
|
MAX_RANGE = 1
|
||||||
|
|
||||||
# Rate of display change
|
# Rate of display change i.e the lower the value the slower the transition
|
||||||
TRANSITION_RATE = 1.0 / 32.0
|
TRANSITION_RATE = 1.0 / 32.0
|
||||||
|
|
||||||
|
|
||||||
|
@ -79,7 +79,7 @@ def clear():
|
||||||
|
|
||||||
mode = "auto" # set auto brightness on
|
mode = "auto" # set auto brightness on
|
||||||
last = time.ticks_ms()
|
last = time.ticks_ms()
|
||||||
brightness_value = MIN_RANGE # set the initial brightness level to the minimum
|
brightness_value = MIN_RANGE # set the initial brightness level to the specified minimum
|
||||||
while True:
|
while True:
|
||||||
current = time.ticks_ms()
|
current = time.ticks_ms()
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,9 @@ MAX_LS_VALUE = 295 # 4095 to use the full range
|
||||||
MIN_RANGE = 0.1
|
MIN_RANGE = 0.1
|
||||||
MAX_RANGE = 1
|
MAX_RANGE = 1
|
||||||
|
|
||||||
|
# Rate of display change i.e the lower the value the slower the transition
|
||||||
|
TRANSITION_RATE = 1.0 / 32.0
|
||||||
|
|
||||||
|
|
||||||
# perform linear interpolation to map a range of values to discrete
|
# perform linear interpolation to map a range of values to discrete
|
||||||
def map_range(
|
def map_range(
|
||||||
|
@ -45,9 +48,14 @@ def map_range(
|
||||||
|
|
||||||
|
|
||||||
# gets the light sensor value from onboard sensor and interpolates it
|
# gets the light sensor value from onboard sensor and interpolates it
|
||||||
# clamps the brightness values
|
# clamps the brightness value it outside the ranges specified
|
||||||
def calculate_brightness(current_lsv):
|
def calculate_brightness(prev_brightness_val):
|
||||||
brightness_val = map_range(current_lsv)
|
current_lsv = su.light()
|
||||||
|
current_brightness_val = map_range(current_lsv)
|
||||||
|
|
||||||
|
# uses the previous value to smooth out display changes reducing flickering
|
||||||
|
brightness_diff = current_brightness_val - prev_brightness_val
|
||||||
|
brightness_val = prev_brightness_val + (brightness_diff * TRANSITION_RATE)
|
||||||
if brightness_val > 1:
|
if brightness_val > 1:
|
||||||
brightness_val = 1
|
brightness_val = 1
|
||||||
elif brightness_val < 0.1:
|
elif brightness_val < 0.1:
|
||||||
|
@ -59,7 +67,7 @@ def calculate_brightness(current_lsv):
|
||||||
# draws percentage icon
|
# draws percentage icon
|
||||||
def draw_percentage(x, y):
|
def draw_percentage(x, y):
|
||||||
graphics.rectangle(x + 1, y + 1, 2, 2)
|
graphics.rectangle(x + 1, y + 1, 2, 2)
|
||||||
graphics.line(x, y + 6, x + 6, y)
|
graphics.line(x + 1 , y + 5, x + 6, y)
|
||||||
graphics.rectangle(x + 4, y + 4, 2, 2)
|
graphics.rectangle(x + 4, y + 4, 2, 2)
|
||||||
|
|
||||||
|
|
||||||
|
@ -71,13 +79,16 @@ def clear():
|
||||||
|
|
||||||
mode = "auto"
|
mode = "auto"
|
||||||
last = time.ticks_ms()
|
last = time.ticks_ms()
|
||||||
|
brightness_value = MIN_RANGE # set the initial brightness level to the specified minimum
|
||||||
while True:
|
while True:
|
||||||
current = time.ticks_ms()
|
current = time.ticks_ms()
|
||||||
|
|
||||||
# get light sensor value from the sensor
|
# set the display brightness
|
||||||
ls_value = su.light()
|
brightness_value = calculate_brightness(brightness_value)
|
||||||
brightness_value = calculate_brightness(ls_value)
|
|
||||||
su.set_brightness(brightness_value)
|
su.set_brightness(brightness_value)
|
||||||
|
|
||||||
|
bp = (brightness_value / MAX_RANGE) * 100 # gets brightness value in percentage relative to the MAX_LS_VALUE set
|
||||||
|
|
||||||
# calculate brightness percentage
|
# calculate brightness percentage
|
||||||
bp = (brightness_value / MAX_RANGE) * 100
|
bp = (brightness_value / MAX_RANGE) * 100
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue