Skip to content

Commit 0b0474e

Browse files
committed
Added light sensor example to the su
1 parent 90a2076 commit 0b0474e

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

micropython/examples/stellar_unicorn/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [Feature Test With Audio](#feature-test-with-audio)
1010
- [Fire Effect](#fire-effect)
1111
- [Lava Lamp](#lava-lamp)
12+
- [Light Sensor](#light-sensor)
1213
- [Nostalgia Prompt](#nostalgia-prompt)
1314
- [Rainbow](#rainbow)
1415
- [Scrolling Text](#scrolling-text)
@@ -91,6 +92,14 @@ A pretty, procedural fire effect. Switch between landscape fire and vertical fir
9192

9293
A 70s-tastic, procedural rainbow lava lamp. You can adjust the brightness with LUX + and -.
9394

95+
### Light Sensor
96+
97+
[light_sensor.py](light_sensor.py)
98+
99+
Reads data from the on board light sensor and displays the brightness level of the environment. The display is by default set to auto brightness i.e reacts to the brightness of the environment.
100+
- Button A turns auto brightness off
101+
- Button B turns auto brightness on
102+
94103
### Nostalgia Prompt
95104

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

0 commit comments

Comments
 (0)