|
| 1 | +import time |
| 2 | +from cosmic import CosmicUnicorn |
| 3 | +from picographics import PicoGraphics, DISPLAY_COSMIC_UNICORN as DISPLAY |
| 4 | + |
| 5 | +""" |
| 6 | +Light sensoring feature for the cosmic unicorn |
| 7 | +Uses the onboard light sensor to detect the light in the environment |
| 8 | +The brightness level is displayed as percentage. |
| 9 | +Brightness of the display is auto adjusted to the brightness level of the environment |
| 10 | +Press A to turn auto brightness on |
| 11 | +Press B to turn auto brightness off |
| 12 | +""" |
| 13 | +# set up unicorn and drawing variables |
| 14 | +cu = CosmicUnicorn() |
| 15 | +graphics = PicoGraphics(DISPLAY) |
| 16 | + |
| 17 | +WIDTH, HEIGHT = graphics.get_bounds() |
| 18 | +BLACK = graphics.create_pen(0, 0, 0) |
| 19 | +WHITE = graphics.create_pen(255, 255, 255) |
| 20 | +GREY = graphics.create_pen(20, 20, 20) |
| 21 | +HUE_START = 0 |
| 22 | +HUE_END = 100 |
| 23 | +graphics.set_font("bitmap8") |
| 24 | + |
| 25 | +# Text display sleep time in ms |
| 26 | +TEXT_SLEEP = 500 |
| 27 | + |
| 28 | + |
| 29 | +# the onboard light sensor has a wide range from 0 t0 4095 |
| 30 | +# It is therefore needed to set a lower max and a higher minimum |
| 31 | +MIN_LS_VALUE = 10 |
| 32 | +MAX_LS_VALUE = 295 # 4095 to use the full range |
| 33 | +MIN_RANGE = 0.1 |
| 34 | +MAX_RANGE = 1 |
| 35 | + |
| 36 | +# Rate of display change i.e the lower the value the slower the transition |
| 37 | +TRANSITION_RATE = 1.0 / 72.0 |
| 38 | + |
| 39 | +# perform linear interpolation to map a range of values to discrete |
| 40 | +def map_range( |
| 41 | + x, |
| 42 | + min_input=MIN_LS_VALUE, |
| 43 | + max_input=MAX_LS_VALUE, |
| 44 | + min_output=MIN_RANGE, |
| 45 | + max_output=MAX_RANGE, |
| 46 | +): |
| 47 | + return (x - min_input) * (max_output - min_output) / ( |
| 48 | + max_input - min_input |
| 49 | + ) + min_output |
| 50 | + |
| 51 | + |
| 52 | +# gets the light sensor value from onboard sensor and interpolates it |
| 53 | +# clamps the brightness value it outside the ranges specified |
| 54 | +def calculate_brightness(prev_brightness_val): |
| 55 | + current_lsv = cu.light() |
| 56 | + current_brightness_val = map_range(current_lsv) |
| 57 | + |
| 58 | + # uses the previous value to smooth out display changes reducing flickering |
| 59 | + brightness_diff = current_brightness_val - prev_brightness_val |
| 60 | + brightness_val = prev_brightness_val + (brightness_diff * TRANSITION_RATE) |
| 61 | + if brightness_val > 1: |
| 62 | + brightness_val = 1 |
| 63 | + elif brightness_val < 0.1: |
| 64 | + brightness_val = 0.1 |
| 65 | + |
| 66 | + return brightness_val |
| 67 | + |
| 68 | + |
| 69 | +# sets up a handy function we can call to clear the screen |
| 70 | +def clear(): |
| 71 | + graphics.set_pen(BLACK) |
| 72 | + graphics.clear() |
| 73 | + |
| 74 | + |
| 75 | +def draw_percentage(x, y): |
| 76 | + graphics.rectangle(x + 1, y + 1, 2, 2) |
| 77 | + graphics.line(x + 1, y + 5, x + 6, y) |
| 78 | + graphics.rectangle(x + 4, y + 4, 2, 2) |
| 79 | + |
| 80 | + |
| 81 | +# draws a sun icon |
| 82 | +def draw_sun(x, y, r): |
| 83 | + circle_x = x + 3 + r |
| 84 | + circle_y = y + 3 + r |
| 85 | + graphics.circle(circle_x, circle_y, r) |
| 86 | + graphics.line(circle_x, y, circle_x, y + 2) |
| 87 | + graphics.line(x, circle_y, x + 2, circle_y) |
| 88 | + graphics.line(circle_x, (y + 5 + 2 * r), circle_x, (y + 5 + 2 * r) + 2) |
| 89 | + graphics.line((x + 5 + 2 * r), circle_y, (x + 5 + 2 * r) + 2, circle_y) |
| 90 | + graphics.line( |
| 91 | + circle_x + 1 + r, circle_y - 1 - r, circle_x + 3 + r, circle_y - 3 - r |
| 92 | + ) |
| 93 | + graphics.line( |
| 94 | + circle_x + 1 + r, circle_y + 1 + r, circle_x + 3 + r, circle_y + 3 + r |
| 95 | + ) |
| 96 | + graphics.line( |
| 97 | + circle_x - 1 - r, circle_y - 1 - r, circle_x - 3 - r, circle_y - 3 - r |
| 98 | + ) |
| 99 | + graphics.line( |
| 100 | + circle_x - 1 - r, circle_y + 1 + r, circle_x - 3 - r, circle_y + 3 + r |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +mode = "auto" |
| 105 | +last = time.ticks_ms() |
| 106 | + |
| 107 | +brightness_value = MIN_RANGE # set the initial brightness level to the minimum |
| 108 | +while True: |
| 109 | + current = time.ticks_ms() |
| 110 | + |
| 111 | + # set the display brightness |
| 112 | + brightness_value = calculate_brightness(brightness_value) |
| 113 | + cu.set_brightness(brightness_value) |
| 114 | + |
| 115 | + bp = (brightness_value / MAX_RANGE) * 100 # gets brightness value in percentage relative to the MAX_LS_VALUE set |
| 116 | + |
| 117 | + # deactivate auto brightness by pressing A |
| 118 | + if cu.is_pressed(CosmicUnicorn.SWITCH_A): |
| 119 | + print("Auto brightness off") |
| 120 | + mode = "off" |
| 121 | + |
| 122 | + # reactivate auto brightness by pressing A |
| 123 | + if cu.is_pressed(CosmicUnicorn.SWITCH_B): |
| 124 | + print("Auto brightness on") |
| 125 | + mode = "auto" |
| 126 | + |
| 127 | + # set brightness to default value if off |
| 128 | + if mode == "off": |
| 129 | + cu.set_brightness(0.5) |
| 130 | + |
| 131 | + # set text update rate after a certain time to reduce flickering |
| 132 | + if current - last >= TEXT_SLEEP: |
| 133 | + clear() |
| 134 | + |
| 135 | + # calculate colour from the brightness value |
| 136 | + hue = max(0, HUE_START + ((bp - 0) * (HUE_END - HUE_START) / (100 - 0))) |
| 137 | + |
| 138 | + # create pens with this colour (and with the high / low colours) |
| 139 | + CURRENT_COLOUR = graphics.create_pen_hsv(hue / 360, 1.0, 0.8) |
| 140 | + HIGH_COLOUR = graphics.create_pen_hsv(HUE_END / 360, 1.0, 0.8) |
| 141 | + LOW_COLOUR = graphics.create_pen_hsv(HUE_START / 360, 1.0, 0.8) |
| 142 | + |
| 143 | + # draw the text |
| 144 | + graphics.set_pen(CURRENT_COLOUR) |
| 145 | + graphics.text("BRT: ", 0, 1, scale=1) |
| 146 | + graphics.text(f"{bp:.0f}", 7, 23, scale=1) |
| 147 | + draw_percentage((WIDTH - 10), 23) |
| 148 | + |
| 149 | + # draw sun icon |
| 150 | + draw_sun(0, 10, 2) |
| 151 | + |
| 152 | + # draw a bar for the background |
| 153 | + bar_width = WIDTH - 12 |
| 154 | + graphics.set_pen(GREY) |
| 155 | + graphics.rectangle(13, 10, bar_width, 11) |
| 156 | + |
| 157 | + # draw a bar for the current brightness percentage |
| 158 | + graphics.set_pen(CURRENT_COLOUR) |
| 159 | + graphics.rectangle(13, 10, int((bp / 100) * bar_width), 11) |
| 160 | + |
| 161 | + last = current |
| 162 | + |
| 163 | + # time to update the display |
| 164 | + cu.update(graphics) |
0 commit comments