|
| 1 | +from machine import ADC |
| 2 | +import time |
| 3 | +from stellar import StellarUnicorn |
| 4 | +from picographics import PicoGraphics, DISPLAY_STELLAR_UNICORN |
| 5 | + |
| 6 | +""" |
| 7 | +Reads the internal temperature sensor on the Pico W... |
| 8 | +... and displays an appropriately coloured pulsing blob. |
| 9 | +""" |
| 10 | + |
| 11 | +# The range of readings that we want to map to colours |
| 12 | +MIN = 15 |
| 13 | +MAX = 35 |
| 14 | + |
| 15 | +# pick what bits of the colour wheel to use (from 0-360°) |
| 16 | +# https://www.cssscript.com/demo/hsv-hsl-color-wheel-picker-reinvented/ |
| 17 | +HUE_START = 230 # blue |
| 18 | +HUE_END = 359 # red |
| 19 | + |
| 20 | +# rainbow party mode |
| 21 | +rainbow_orb = False |
| 22 | + |
| 23 | +# set up the Unicron |
| 24 | +su = StellarUnicorn() |
| 25 | +graphics = PicoGraphics(DISPLAY_STELLAR_UNICORN) |
| 26 | + |
| 27 | +# set up the ADC |
| 28 | +sensor_temp = ADC(ADC.CORE_TEMP) |
| 29 | +conversion_factor = 3.3 / 65535 # used for calculating a temperature from the raw sensor reading |
| 30 | + |
| 31 | +# set up constants and variables for drawing |
| 32 | +WIDTH, HEIGHT = graphics.get_bounds() |
| 33 | + |
| 34 | +BLACK = graphics.create_pen(0, 0, 0) |
| 35 | +WHITE = graphics.create_pen(255, 255, 255) |
| 36 | + |
| 37 | +forward = True |
| 38 | +orb_brightness = 0.5 |
| 39 | +hue = 0.0 |
| 40 | + |
| 41 | +graphics.set_font("bitmap8") |
| 42 | + |
| 43 | +while True: |
| 44 | + |
| 45 | + # read the onboard sensor |
| 46 | + # the following two lines do some maths to convert the number from the temp sensor into celsius |
| 47 | + reading = sensor_temp.read_u16() * conversion_factor |
| 48 | + temperature = 27 - (reading - 0.706) / 0.001721 |
| 49 | + |
| 50 | + print(f""" |
| 51 | + Temperature: {temperature:.2f} °C |
| 52 | + """) |
| 53 | + |
| 54 | + # fills the screen with black |
| 55 | + graphics.set_pen(BLACK) |
| 56 | + graphics.clear() |
| 57 | + |
| 58 | + # draw a weird orb: |
| 59 | + # three overlapping circles with varying saturations |
| 60 | + if rainbow_orb is True: |
| 61 | + graphics.set_pen(graphics.create_pen_hsv((hue / 360), 0.5, orb_brightness)) |
| 62 | + graphics.circle(8, 8, 7) |
| 63 | + graphics.set_pen(graphics.create_pen_hsv((hue / 360), 0.7, orb_brightness)) |
| 64 | + graphics.circle(7, 7, 7) |
| 65 | + graphics.set_pen(graphics.create_pen_hsv((hue / 360), 1.0, orb_brightness)) |
| 66 | + graphics.circle(7, 7, 5) |
| 67 | + hue += 0.01 * 360 |
| 68 | + else: |
| 69 | + # calculate a colour from the temperature |
| 70 | + hue = max(0, HUE_START + ((temperature - MIN) * (HUE_END - HUE_START) / (MAX - MIN))) |
| 71 | + graphics.set_pen(graphics.create_pen_hsv((hue / 360), 0.6, orb_brightness)) |
| 72 | + graphics.circle(8, 8, 7) |
| 73 | + graphics.set_pen(graphics.create_pen_hsv((hue / 360), 0.8, orb_brightness)) |
| 74 | + graphics.circle(7, 7, 7) |
| 75 | + graphics.set_pen(graphics.create_pen_hsv((hue / 360), 1.0, orb_brightness)) |
| 76 | + graphics.circle(7, 7, 5) |
| 77 | + |
| 78 | + # pulse the orb! |
| 79 | + if forward is True: |
| 80 | + orb_brightness += 0.01 |
| 81 | + if orb_brightness >= 0.7: |
| 82 | + orb_brightness = 0.7 |
| 83 | + forward = False |
| 84 | + |
| 85 | + if forward is False: |
| 86 | + orb_brightness -= 0.01 |
| 87 | + if orb_brightness <= 0.3: |
| 88 | + orb_brightness = 0.3 |
| 89 | + forward = True |
| 90 | + |
| 91 | + # draw the temperature |
| 92 | + # try BLACK for a funky negative space effect |
| 93 | + graphics.set_pen(WHITE) |
| 94 | + graphics.text(f"{temperature:.0f}°", 2, 5, scale=1) |
| 95 | + |
| 96 | + # or uncomment these lines if you'd prefer it in Freedom Units |
| 97 | + # graphics.set_pen(WHITE) |
| 98 | + # fahrenheit = (temperature_average * 9 / 5) + 32 |
| 99 | + # graphics.text(f"{fahrenheit:.0f}°", 2, 5, scale=1) |
| 100 | + |
| 101 | + # time to update the display |
| 102 | + su.update(graphics) |
| 103 | + time.sleep(0.1) |
0 commit comments