|
| 1 | +import time |
| 2 | +from galactic import GalacticUnicorn |
| 3 | +from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY |
| 4 | + |
| 5 | +""" |
| 6 | +Auto brightness feature for the galactic 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 | +gu = GalacticUnicorn() |
| 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 | +mode = "auto" |
| 66 | +last = time.ticks_ms() |
| 67 | +while True: |
| 68 | + current = time.ticks_ms() |
| 69 | + |
| 70 | + # get light sensor value from the sensor |
| 71 | + ls_value = gu.light() |
| 72 | + brightness_value = calculate_brightness(ls_value) |
| 73 | + gu.set_brightness(brightness_value) |
| 74 | + # calculate brightness percentage |
| 75 | + bp = (brightness_value / MAX_RANGE) * 100 |
| 76 | + |
| 77 | + # deactivate auto brightness by pressing A |
| 78 | + if gu.is_pressed(GalacticUnicorn.SWITCH_A): |
| 79 | + print("Auto brightness off") |
| 80 | + mode = "off" |
| 81 | + |
| 82 | + # reactivate auto brightness by pressing A |
| 83 | + if gu.is_pressed(GalacticUnicorn.SWITCH_B): |
| 84 | + print("Auto brightness on") |
| 85 | + mode = "auto" |
| 86 | + |
| 87 | + # set brightness to default value if off |
| 88 | + if mode == "off": |
| 89 | + gu.set_brightness(0.5) |
| 90 | + |
| 91 | + # set text update rate after a certain time to reduce flickering |
| 92 | + if current - last >= TEXT_SLEEP: |
| 93 | + clear() |
| 94 | + |
| 95 | + # calculate colour from the brightness value |
| 96 | + hue = max(0, HUE_START + ((bp - 0) * (HUE_END - HUE_START) / (100 - 0))) |
| 97 | + |
| 98 | + # create pens with this colour (and with the high / low colours) |
| 99 | + CURRENT_COLOUR = graphics.create_pen_hsv(hue / 360, 1.0, 0.8) |
| 100 | + HIGH_COLOUR = graphics.create_pen_hsv(HUE_END / 360, 1.0, 0.8) |
| 101 | + LOW_COLOUR = graphics.create_pen_hsv(HUE_START / 360, 1.0, 0.8) |
| 102 | + |
| 103 | + # draw the text |
| 104 | + graphics.set_pen(CURRENT_COLOUR) |
| 105 | + graphics.text("BRT: ", 0, 1, scale=1) |
| 106 | + # measure the rest of the text before drawing to right align it |
| 107 | + text_width = graphics.measure_text(f"{bp:.0f}/°", scale=1) |
| 108 | + graphics.text(f"{bp:.0f}%", WIDTH - text_width, 1, scale=1) |
| 109 | + |
| 110 | + # draw a bar for the background |
| 111 | + graphics.set_pen(GREY) |
| 112 | + graphics.rectangle(0, 9, WIDTH, 10) |
| 113 | + |
| 114 | + # draw a bar for the current brightness percentage |
| 115 | + graphics.set_pen(CURRENT_COLOUR) |
| 116 | + graphics.rectangle(0, 9, int((bp / 100) * WIDTH), 10) |
| 117 | + last = current |
| 118 | + |
| 119 | + # time to update the display |
| 120 | + gu.update(graphics) |
| 121 | + # time.sleep(1) |
0 commit comments