Skip to content

Commit 2cd80ab

Browse files
authored
Merge pull request #71 from pimoroni/examples/backlight-control
Examples/backlight control
2 parents a62c02f + 11678b4 commit 2cd80ab

File tree

2 files changed

+218
-0
lines changed

2 files changed

+218
-0
lines changed

examples/auto_backlight.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# ICON [[(-7.43, 6.46), (-4.6, 6.46), (-3.19, 2.39), (3.27, 2.39), (4.69, 6.46), (7.43, 6.46), (1.42, -9.47), (-1.42, -9.47), (-7.43, 6.46)], [(-2.39, 0.09), (-0.09, -6.55), (0.09, -6.55), (2.39, 0.09), (-2.39, 0.09)], [(-0.0, 19.38), (-5.93, 13.54), (-14.16, 13.54), (-14.16, 5.31), (-20.0, -0.62), (-14.16, -6.55), (-14.16, -14.78), (-5.93, -14.78), (-0.0, -20.62), (5.93, -14.78), (14.16, -14.78), (14.16, -6.55), (20.0, -0.62), (14.16, 5.31), (14.16, 13.54), (5.93, 13.54), (-0.0, 19.38)], [(-0.0, 14.42), (4.42, 10.0), (10.62, 10.0), (10.62, 3.81), (15.04, -0.62), (10.62, -5.04), (10.62, -11.24), (4.42, -11.24), (-0.0, -15.66), (-4.42, -11.24), (-10.62, -11.24), (-10.62, -5.04), (-15.04, -0.62), (-10.62, 3.81), (-10.62, 10.0), (-4.42, 10.0), (-0.0, 14.42)]]
2+
# NAME Auto Backlight Demo
3+
# DESC Using the Multi-Sensor Stick
4+
from presto import Presto
5+
from picovector import ANTIALIAS_BEST, PicoVector, Polygon, Transform
6+
from machine import I2C
7+
from breakout_ltr559 import BreakoutLTR559
8+
9+
# Setup for the Presto display
10+
presto = Presto(ambient_light=False)
11+
display = presto.display
12+
WIDTH, HEIGHT = display.get_bounds()
13+
14+
ltr = BreakoutLTR559(I2C())
15+
LUX_MAX = 200
16+
LUX_MIN = 0
17+
18+
CX = WIDTH // 2
19+
CY = HEIGHT // 2
20+
21+
# Colours
22+
BLACK = display.create_pen(0, 0, 0)
23+
hue = 0.8
24+
BACKGROUND = display.create_pen_hsv(hue, 0.8, 1.0) # We'll use this one for the background.
25+
FOREGROUND = display.create_pen_hsv(hue, 0.5, 1.0) # Slightly lighter for foreground elements.
26+
TEXT_COLOUR = display.create_pen_hsv(hue, 0.2, 1.0)
27+
28+
# Pico Vector
29+
vector = PicoVector(display)
30+
vector.set_antialiasing(ANTIALIAS_BEST)
31+
t = Transform()
32+
vector.set_transform(t)
33+
34+
# We'll use a rect with rounded corners for the background.
35+
background_rect = Polygon()
36+
background_rect.rectangle(0, 0, WIDTH, HEIGHT)
37+
background_rect.rectangle(0, 0, WIDTH, HEIGHT, (10, 10, 10, 10))
38+
39+
# Vector icon
40+
AUTO_BACKLIGHT_PATHS = [[(-7.43, 6.46), (-4.6, 6.46), (-3.19, 2.39), (3.27, 2.39), (4.69, 6.46), (7.43, 6.46), (1.42, -9.47), (-1.42, -9.47), (-7.43, 6.46)], [(-2.39, 0.09), (-0.09, -6.55), (0.09, -6.55), (2.39, 0.09), (-2.39, 0.09)], [(-0.0, 19.38), (-5.93, 13.54), (-14.16, 13.54), (-14.16, 5.31), (-20.0, -0.62), (-14.16, -6.55), (-14.16, -14.78), (-5.93, -14.78), (-0.0, -20.62), (5.93, -14.78), (14.16, -14.78), (14.16, -6.55), (20.0, -0.62), (14.16, 5.31), (14.16, 13.54), (5.93, 13.54), (-0.0, 19.38)], [(-0.0, 14.42), (4.42, 10.0), (10.62, 10.0), (10.62, 3.81), (15.04, -0.62), (10.62, -5.04), (10.62, -11.24), (4.42, -11.24), (-0.0, -15.66), (-4.42, -11.24), (-10.62, -11.24), (-10.62, -5.04), (-15.04, -0.62), (-10.62, 3.81), (-10.62, 10.0), (-4.42, 10.0), (-0.0, 14.42)]]
41+
auto_backlight_icon = Polygon()
42+
43+
for path in AUTO_BACKLIGHT_PATHS:
44+
auto_backlight_icon.path(*path)
45+
46+
# Store our last 5 lux readings.
47+
# We've put some low values in to start things off.
48+
lux_readings = [10, 10, 10, 10, 10]
49+
50+
51+
def show_message(text):
52+
display.set_pen(BACKGROUND)
53+
display.clear()
54+
display.set_pen(FOREGROUND)
55+
display.text(f"{text}", 5, 10, WIDTH, 2)
56+
presto.update()
57+
58+
59+
while True:
60+
61+
# Clear the screen
62+
display.set_pen(BACKGROUND)
63+
display.clear()
64+
65+
reading = ltr.get_reading()
66+
67+
if reading is not None:
68+
# Lux reading, capped between 0 and 200.
69+
lux = max(min(round(reading[BreakoutLTR559.LUX]), LUX_MAX), LUX_MIN)
70+
lux_readings.append(lux)
71+
72+
# We'll use the average from the last 5 readings to reduce flicker.
73+
if len(lux_readings) > 5:
74+
lux_readings.pop(0)
75+
76+
lux_avg = round(sum(lux_readings) / len(lux_readings))
77+
78+
# Lux normalised with the lower bounds capped at 0.1 to keep the screen on.
79+
lux_norm = max((lux_avg - LUX_MIN) / (LUX_MAX - LUX_MIN), 0.1)
80+
81+
# Set the backlight!
82+
presto.set_backlight(lux_norm)
83+
84+
display.set_pen(FOREGROUND)
85+
display.text(f"Brightness Level: {round(lux_norm * 10)}", 10, CY + 100, WIDTH, 1)
86+
87+
else:
88+
display.set_pen(FOREGROUND)
89+
display.text("Unable to get reading.\nCheck your multi-sensor stick and try again", 7, CY + 90, WIDTH, 1)
90+
91+
# Draw the min/max brightness icons
92+
display.set_pen(FOREGROUND)
93+
t.translate(CX, CY)
94+
t.scale(3.0, 3.0)
95+
vector.draw(auto_backlight_icon)
96+
t.reset()
97+
98+
# Draw the rounded corners
99+
display.set_pen(BLACK)
100+
vector.draw(background_rect)
101+
102+
presto.update()

examples/backlight_slider.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# ICON [[(0.0, 7.0), (3.0, 4.0), (8.0, 4.0), (8.0, -1.0), (11.0, -4.0), (8.0, -7.0), (8.0, -12.0), (3.0, -12.0), (0.0, -15.0), (-3.0, -12.0), (-8.0, -12.0), (-8.0, -7.0), (-11.0, -4.0), (-8.0, -1.0), (-8.0, 4.0), (-3.0, 4.0), (0.0, 7.0)], [(0.0, 2.0), (0.0, -10.0), (1.9, -9.71), (3.4, -8.97), (4.8, -7.62), (5.59, -6.24), (5.85, -5.39), (6.0, -4.1), (5.96, -3.3), (5.56, -1.7), (5.04, -0.71), (4.07, 0.42), (2.74, 1.36), (1.84, 1.73), (0.05, 2.0)], [(-16.0, 12.0), (-16.87, 11.91), (-18.0, 11.49), (-18.81, 10.88), (-19.31, 10.27), (-19.82, 9.22), (-20.0, 8.05), (-20.0, -16.0), (-19.91, -16.88), (-19.52, -17.92), (-19.12, -18.51), (-18.19, -19.34), (-16.72, -19.94), (-16.05, -20.0), (16.0, -20.0), (17.35, -19.77), (18.4, -19.17), (19.01, -18.58), (19.8, -17.25), (20.0, -16.05), (20.0, 8.0), (19.89, 8.96), (19.58, 9.79), (18.84, 10.81), (17.99, 11.47), (17.05, 11.87), (16.05, 12.0), (-16.0, 12.0)], [(-16.0, 8.0), (16.0, 8.0), (16.0, -16.0), (-16.0, -16.0), (-16.0, 8.0)]]
2+
# NAME Backlight Control Demo
3+
# DESC Let there be light.
4+
from presto import Presto
5+
from picovector import ANTIALIAS_BEST, PicoVector, Polygon, Transform
6+
7+
# Setup for the Presto display
8+
presto = Presto(ambient_light=False)
9+
display = presto.display
10+
WIDTH, HEIGHT = display.get_bounds()
11+
12+
CX = WIDTH // 2
13+
CY = HEIGHT // 2
14+
15+
# Colours
16+
BLACK = display.create_pen(0, 0, 0)
17+
hue = 0.6
18+
BACKGROUND = display.create_pen_hsv(hue, 0.8, 1.0) # We'll use this one for the background.
19+
FOREGROUND = display.create_pen_hsv(hue, 0.5, 1.0) # Slightly lighter for foreground elements.
20+
TEXT_COLOUR = display.create_pen_hsv(hue, 0.2, 1.0)
21+
22+
# Pico Vector
23+
vector = PicoVector(display)
24+
vector.set_antialiasing(ANTIALIAS_BEST)
25+
t = Transform()
26+
vector.set_transform(t)
27+
28+
# We'll use a rect with rounded corners for the background.
29+
background_rect = Polygon()
30+
background_rect.rectangle(0, 0, WIDTH, HEIGHT)
31+
background_rect.rectangle(0, 0, WIDTH, HEIGHT, (10, 10, 10, 10))
32+
33+
# Constants for our slider
34+
BAR_W = 50
35+
BAR_H = 170
36+
BAR_X = CX - BAR_W // 2
37+
BAR_Y = CY - BAR_H // 2
38+
SLIDER_HEIGHT = BAR_H // 4
39+
40+
# Vector icons
41+
BACKLIGHT_MAX_PATHS = [[(-0.0, 9.69), (-2.96, 6.77), (-7.08, 6.77), (-7.08, 2.65), (-10.0, -0.31), (-7.08, -3.27), (-7.08, -7.39), (-2.96, -7.39), (-0.0, -10.31), (2.96, -7.39), (7.08, -7.39), (7.08, -3.27), (10.0, -0.31), (7.08, 2.65), (7.08, 6.77), (2.96, 6.77), (-0.0, 9.69)], [(-0.0, 4.12), (0.94, 4.02), (1.52, 3.86), (1.99, 3.66), (2.67, 3.23), (3.1, 2.85), (3.67, 2.17), (4.07, 1.44), (4.24, 0.98), (4.39, 0.27), (4.42, -0.29), (4.33, -1.25), (4.16, -1.85), (3.84, -2.52), (3.48, -3.03), (3.18, -3.37), (2.7, -3.81), (2.36, -4.06), (1.78, -4.37), (1.2, -4.58), (0.65, -4.69), (-0.2, -4.73), (-0.79, -4.67), (-1.24, -4.57), (-1.93, -4.3), (-2.4, -4.04), (-2.89, -3.67), (-3.57, -2.94), (-3.9, -2.44), (-4.22, -1.68), (-4.39, -0.87), (-4.4, 0.15), (-4.32, 0.7), (-4.17, 1.22), (-3.81, 1.98), (-3.45, 2.49), (-3.16, 2.82), (-2.73, 3.19), (-2.31, 3.48), (-1.82, 3.74), (-1.18, 3.97), (-0.79, 4.05), (-0.02, 4.11)], [(-0.0, 7.21), (2.21, 5.0), (5.31, 5.0), (5.31, 1.9), (7.52, -0.31), (5.31, -2.52), (5.31, -5.62), (2.21, -5.62), (-0.0, -7.83), (-2.21, -5.62), (-5.31, -5.62), (-5.31, -2.52), (-7.52, -0.31), (-5.31, 1.9), (-5.31, 5.0), (-2.21, 5.0), (-0.0, 7.21)]]
42+
BACKLIGHT_MIN_PATHS = [[(-0.0, 9.69), (-2.96, 6.77), (-7.08, 6.77), (-7.08, 2.65), (-10.0, -0.31), (-7.08, -3.27), (-7.08, -7.39), (-2.96, -7.39), (-0.0, -10.31), (2.96, -7.39), (7.08, -7.39), (7.08, -3.27), (10.0, -0.31), (7.08, 2.65), (7.08, 6.77), (2.96, 6.77), (-0.0, 9.69)], [(-0.0, 7.21), (2.21, 5.0), (5.31, 5.0), (5.31, 1.9), (7.52, -0.31), (5.31, -2.52), (5.31, -5.62), (2.21, -5.62), (-0.0, -7.83), (-2.21, -5.62), (-5.31, -5.62), (-5.31, -2.52), (-7.52, -0.31), (-5.31, 1.9), (-5.31, 5.0), (-2.21, 5.0), (-0.0, 7.21)]]
43+
backlight_max_icon = Polygon()
44+
backlight_min_icon = Polygon()
45+
46+
for path in BACKLIGHT_MAX_PATHS:
47+
backlight_max_icon.path(*path)
48+
49+
for path in BACKLIGHT_MIN_PATHS:
50+
backlight_min_icon.path(*path)
51+
52+
53+
class Slider(object):
54+
def __init__(self, x, y, w, h, bar_colour, slider_colour):
55+
self.x = x
56+
self.y = y
57+
self.w = w
58+
self.h = h
59+
self.slider_h = self.h // 4
60+
self.value = 1.0
61+
self.slider_vector = Polygon().rectangle(self.x, self.y, self.w, self.slider_h, (10, 10, 10, 10))
62+
self.bar_vector = Polygon().rectangle(self.x, self.y, self.w, self.h, (10, 10, 10, 10))
63+
self.slider_colour = slider_colour
64+
self.bar_colour = bar_colour
65+
self.touch = presto.touch
66+
67+
def update(self):
68+
self.touch.poll()
69+
70+
if self.touch.state and self.touch.x >= self.x and self.touch.x <= self.x + self.w and self.touch.y >= self.y - self.slider_h and self.touch.y <= (self.y + self.h):
71+
72+
new_pos_y = max(min(self.touch.y, (self.y + self.h) - self.slider_h), self.y)
73+
self.slider_vector = Polygon().rectangle(self.x, new_pos_y, self.w, self.slider_h, (10, 10, 10, 10))
74+
self.value = 1.0 - (new_pos_y - self.y) / ((self.y + self.h - self.slider_h) - self.y)
75+
76+
def draw(self):
77+
display.set_pen(self.bar_colour)
78+
vector.draw(self.bar_vector)
79+
display.set_pen(self.slider_colour)
80+
vector.draw(self.slider_vector)
81+
82+
def get_value(self):
83+
return self.value
84+
85+
86+
# Create our slider object
87+
s = Slider(BAR_X, BAR_Y, BAR_W, BAR_H, FOREGROUND, TEXT_COLOUR)
88+
89+
while True:
90+
91+
s.update()
92+
93+
brightness = max(s.get_value(), 0.1)
94+
presto.set_backlight(brightness)
95+
96+
# Clear the screen
97+
display.set_pen(BACKGROUND)
98+
display.clear()
99+
100+
# Draw the slider
101+
s.draw()
102+
103+
# Draw the min/max brightness icons
104+
display.set_pen(TEXT_COLOUR)
105+
t.translate(CX, BAR_Y + 20)
106+
vector.draw(backlight_max_icon)
107+
t.reset()
108+
t.translate(CX, BAR_Y + BAR_H - 20)
109+
vector.draw(backlight_min_icon)
110+
t.reset()
111+
112+
# Draw thhe rounded corners
113+
display.set_pen(BLACK)
114+
vector.draw(background_rect)
115+
116+
presto.update()

0 commit comments

Comments
 (0)