|
| 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