Skip to content

Commit e14903d

Browse files
committed
Added light sensor example on cu
1 parent f06d103 commit e14903d

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed

micropython/examples/cosmic_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)
@@ -85,6 +86,14 @@ A pretty, procedural fire effect. Switch between landscape fire and vertical fir
8586

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

89+
### Light Sensor
90+
91+
[light_sensor.py](light_sensor.py)
92+
93+
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.
94+
- Button A turns auto brightness off
95+
- Button B turns auto brightness on
96+
8897
### Nostalgia Prompt
8998

9099
[nostalgia_prompt.py](nostalgia_prompt.py)
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import time
2+
from cosmic import CosmicUnicorn
3+
from picographics import PicoGraphics, DISPLAY_COSMIC_UNICORN as DISPLAY
4+
5+
"""
6+
Auto brightness feature for the Cosmic 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+
cu = CosmicUnicorn()
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+
# sets up a handy function we can call to clear the screen
60+
def clear():
61+
graphics.set_pen(BLACK)
62+
graphics.clear()
63+
64+
65+
def draw_percentage(x, y):
66+
graphics.rectangle(x + 1, y + 1, 2, 2)
67+
graphics.line(x, y + 6, x + 6, y)
68+
graphics.rectangle(x + 4, y + 4, 2, 2)
69+
70+
71+
# draws a sun icon
72+
def draw_sun(x, y, r):
73+
circle_x = x + 3 + r
74+
circle_y = y + 3 + r
75+
graphics.circle(circle_x, circle_y, r)
76+
graphics.line(circle_x, y, circle_x, y + 2)
77+
graphics.line(x, circle_y, x + 2, circle_y)
78+
graphics.line(circle_x, (y + 5 + 2 * r), circle_x, (y + 5 + 2 * r) + 2)
79+
graphics.line((x + 5 + 2 * r), circle_y, (x + 5 + 2 * r) + 2, circle_y)
80+
graphics.line(
81+
circle_x + 1 + r, circle_y - 1 - r, circle_x + 3 + r, circle_y - 3 - r
82+
)
83+
graphics.line(
84+
circle_x + 1 + r, circle_y + 1 + r, circle_x + 3 + r, circle_y + 3 + r
85+
)
86+
graphics.line(
87+
circle_x - 1 - r, circle_y - 1 - r, circle_x - 3 - r, circle_y - 3 - r
88+
)
89+
graphics.line(
90+
circle_x - 1 - r, circle_y + 1 + r, circle_x - 3 - r, circle_y + 3 + r
91+
)
92+
93+
94+
mode = "auto"
95+
last = time.ticks_ms()
96+
while True:
97+
current = time.ticks_ms()
98+
99+
# get light sensor value from the sensor
100+
ls_value = cu.light()
101+
brightness_value = calculate_brightness(ls_value)
102+
cu.set_brightness(brightness_value)
103+
# calculate brightness percentage
104+
bp = (brightness_value / MAX_RANGE) * 100
105+
106+
# deactivate auto brightness by pressing A
107+
if cu.is_pressed(CosmicUnicorn.SWITCH_A):
108+
print("Auto brightness off")
109+
mode = "off"
110+
111+
# reactivate auto brightness by pressing A
112+
if cu.is_pressed(CosmicUnicorn.SWITCH_B):
113+
print("Auto brightness on")
114+
mode = "auto"
115+
116+
# set brightness to default value if off
117+
if mode == "off":
118+
cu.set_brightness(0.5)
119+
120+
# set text update rate after a certain time to reduce flickering
121+
if current - last >= TEXT_SLEEP:
122+
clear()
123+
124+
# calculate colour from the brightness value
125+
hue = max(0, HUE_START + ((bp - 0) * (HUE_END - HUE_START) / (100 - 0)))
126+
127+
# create pens with this colour (and with the high / low colours)
128+
CURRENT_COLOUR = graphics.create_pen_hsv(hue / 360, 1.0, 0.8)
129+
HIGH_COLOUR = graphics.create_pen_hsv(HUE_END / 360, 1.0, 0.8)
130+
LOW_COLOUR = graphics.create_pen_hsv(HUE_START / 360, 1.0, 0.8)
131+
132+
# draw the text
133+
graphics.set_pen(CURRENT_COLOUR)
134+
graphics.text("BRT ", 0, 1, scale=1)
135+
draw_percentage(15, 1)
136+
graphics.text(f"{bp:.0f}", 9, 24, scale=1)
137+
draw_percentage(20, 24)
138+
139+
# draw sun icon
140+
draw_sun(0, 12, 2)
141+
142+
# draw a bar for the background
143+
bar_width = WIDTH - 12
144+
graphics.set_pen(GREY)
145+
graphics.rectangle(13, 12, bar_width, 10)
146+
147+
# draw a bar for the current brightness percentage
148+
graphics.set_pen(CURRENT_COLOUR)
149+
graphics.rectangle(13, 12, int((bp / 100) * bar_width), 10)
150+
151+
last = current
152+
153+
# time to update the display
154+
cu.update(graphics)

micropython/examples/galactic_unicorn/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- [Feature Test With Audio](#feature-test-with-audio)
1111
- [Fire Effect](#fire-effect)
1212
- [Lava Lamp](#lava-lamp)
13+
- [Light Sensor](#light-sensor)
1314
- [Nostalgia Prompt](#nostalgia-prompt)
1415
- [Rainbow](#rainbow)
1516
- [Scrolling Text](#scrolling-text)

0 commit comments

Comments
 (0)