Skip to content

Commit b8cdff8

Browse files
committed
Added light sensor example with auto brightness feature
1 parent 51574f8 commit b8cdff8

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import time
2+
from galactic import GalacticUnicorn
3+
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY
4+
import math
5+
6+
'''
7+
Auto brightness feature for the galactic unicorn
8+
Uses the onboard light sensor to detect the light
9+
The brightness percentage is displayed with brightness auto adjusted
10+
'''
11+
# set up unicorn and drawing variables
12+
gu = GalacticUnicorn()
13+
graphics = PicoGraphics(DISPLAY)
14+
15+
WIDTH, HEIGHT = graphics.get_bounds()
16+
BLACK = graphics.create_pen(0, 0, 0)
17+
WHITE = graphics.create_pen(255, 255, 255)
18+
GREY = graphics.create_pen(20, 20, 20)
19+
HUE_START = 0
20+
HUE_END = 100
21+
graphics.set_font("bitmap8")
22+
23+
# Text display sleep time in ms
24+
TEXT_SLEEP = 500
25+
26+
27+
# the onboard light sensor has a wide range from 0 t0 4095
28+
# It is therefore needed to set a lower max and a higher minimum
29+
MIN_LS_VALUE = 10
30+
MAX_LS_VALUE = 295 # 4095 to use the full range
31+
MIN_RANGE = 0.1
32+
MAX_RANGE = 1
33+
34+
35+
# perform linear interpolation to map a range of values to discrete
36+
def map_range(x, min_input = MIN_LS_VALUE,
37+
max_input = MAX_LS_VALUE,
38+
min_output = MIN_RANGE,
39+
max_output = MAX_RANGE):
40+
return (x - min_input) * (max_output - min_output) / (max_input - min_input) + min_output
41+
42+
43+
# gets the light sensor value from onboard sensor and interpolates it
44+
# clamps the brightness values
45+
def calculate_brightness(current_lsv):
46+
brightness_value = map_range(current_lsv)
47+
if brightness_value > 1:
48+
brightness_value = 1
49+
elif brightness_value < 0.1:
50+
brightness_value = 0.1
51+
52+
return brightness_value
53+
54+
55+
# sets up a handy function we can call to clear the screen
56+
def clear():
57+
graphics.set_pen(BLACK)
58+
graphics.clear()
59+
60+
mode = "auto"
61+
last = time.ticks_ms()
62+
while True:
63+
current = time.ticks_ms()
64+
65+
66+
67+
# get light sensor value from the sensor
68+
ls_value = gu.light()
69+
brightness_value = calculate_brightness(ls_value)
70+
gu.set_brightness(brightness_value)
71+
# calculate brightness percentage
72+
bp = (brightness_value / MAX_RANGE) * 100
73+
74+
# deactivate auto brightness by pressing A
75+
if gu.is_pressed(GalacticUnicorn.SWITCH_A):
76+
print("Auto brightness off")
77+
mode = "off"
78+
79+
# reactivate auto brightness by pressing A
80+
if gu.is_pressed(GalacticUnicorn.SWITCH_B):
81+
print("Auto brightness on")
82+
mode = "auto"
83+
84+
# set brightness to default value if off
85+
if mode == "off":
86+
gu.set_brightness(0.5)
87+
88+
# set text update rate after a certain time to reduce flickering
89+
if current - last >= TEXT_SLEEP:
90+
clear()
91+
92+
# calculate colour from the brightness value
93+
hue = max(0, HUE_START + ((bp - 0) * (HUE_END - HUE_START) / (100 - 0)))
94+
95+
# create pens with this colour (and with the high / low colours)
96+
CURRENT_COLOUR = graphics.create_pen_hsv(hue / 360, 1.0, 0.8)
97+
HIGH_COLOUR = graphics.create_pen_hsv(HUE_END / 360, 1.0, 0.8)
98+
LOW_COLOUR = graphics.create_pen_hsv(HUE_START / 360, 1.0, 0.8)
99+
100+
# draw the text
101+
graphics.set_pen(CURRENT_COLOUR)
102+
graphics.text("BRT: ", 0, 1, scale=1)
103+
# measure the rest of the text before drawing so that we can right align it
104+
text_width = graphics.measure_text(f"{bp:.0f}/°", scale=1)
105+
graphics.text(f"{bp:.0f}%", WIDTH - text_width, 1, scale=1)
106+
107+
# draw a bar for the background
108+
graphics.set_pen(GREY)
109+
graphics.rectangle(0, 9, WIDTH, 10)
110+
111+
# draw a bar for the current brightness percentage
112+
graphics.set_pen(CURRENT_COLOUR)
113+
graphics.rectangle(0, 9, int((bp / 100) * WIDTH), 10)
114+
last = current
115+
116+
# time to update the display
117+
gu.update(graphics)
118+
# time.sleep(1)
119+
120+

0 commit comments

Comments
 (0)