Merge pull request #700 from pimoroni/examples/galactic-co2

Galactic: add CO2 breakout example
pull/708/head v1.19.15
Philip Howard 2023-03-02 11:44:04 +00:00 zatwierdzone przez GitHub
commit 6be46dd429
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 116 dodań i 3 usunięć

Wyświetl plik

@ -4,6 +4,7 @@
- [Galactic Unicorn and PicoGraphics](#galactic-unicorn-and-picographics)
- [Examples](#examples)
- [Clock](#clock)
- [CO2](#co2)
- [Eighties Super Computer](#eighties-super-computer)
- [Feature Test](#feature-test)
- [Feature Test With Audio](#feature-test-with-audio)
@ -13,6 +14,7 @@
- [Rainbow](#rainbow)
- [Scrolling Text](#scrolling-text)
- [Wireless Examples](#wireless-examples)
- [Cheerlights](#cheerlights)
- [Cheerlights History](#cheerlights-history)
- [Galactic Paint](#galactic-paint)
- [Other Examples](#other-examples)
@ -45,6 +47,12 @@ The easiest way to start displaying cool stuff on Galactic Unicorn is using our
Clock example with (optional) NTP synchronization. You can adjust the brightness with LUX + and -, and resync the time by pressing A.
### CO2
[co2.py](co2.py)
Add a [SCD41 sensor breakout](https://shop.pimoroni.com/products/scd41-co2-sensor-breakout) to make an unsubtle carbon dioxide detector. Press A to reset the high/low values.
### Eighties Super Computer
[eighties_super_computer.py](eighties_super_computer.py)
@ -100,18 +108,21 @@ Display scrolling wisdom, quotes or greetz. You can adjust the brightness with L
## Wireless Examples
These examples need `WIFI_CONFIG.py` (from the `common` directory) to be saved to your Pico W. Open up `WIFI_CONFIG.py` in Thonny to add your wifi details (and save it when you're done).
These examples require `WIFI_CONFIG.py` and `network_manager.py` (from the `common` directory) to be saved to your Pico W. Open up `WIFI_CONFIG.py` in Thonny to add your wifi details (and save it when you're done).
- [micropython/examples/common](../../examples/common)
### Cheerlights
[cheerlights.py](cheerlights.py)
Displays the most recent #Cheerlights colour. Find out more about the Cheerlights API at https://cheerlights.com/
### Cheerlights History
[cheerlights_history.py](cheerlights_history.py)
Updates one pixel every five minutes to display the most recent #Cheerlights colour. Discover the most popular colours over time, or use it as an avant garde (but colourful) 53 hour clock! Find out more about the Cheerlights API at https://cheerlights.com/
Requires `WIFI_CONFIG.py` and `network_manager.py` from the `common` directory.
You can adjust the brightness with LUX + and -.
### Galactic Paint

Wyświetl plik

@ -0,0 +1,102 @@
# Add a SCD41 sensor breakout to your Galactic Unicorn to make a handy CO2 detector!
# https://shop.pimoroni.com/products/scd41-co2-sensor-breakout
# Press A to reset the high/low values.
from galactic import GalacticUnicorn
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY
import breakout_scd41
from pimoroni_i2c import PimoroniI2C
from pimoroni import BREAKOUT_GARDEN_I2C_PINS
gu = GalacticUnicorn()
graphics = PicoGraphics(DISPLAY)
i2c = PimoroniI2C(**BREAKOUT_GARDEN_I2C_PINS)
WIDTH, HEIGHT = graphics.get_bounds()
# the range of readings to map to colours
# https://www.kane.co.uk/knowledge-centre/what-are-safe-levels-of-co-and-co2-in-rooms
MIN = 400
MAX = 2000
# pick what bits of the colour wheel to use (from 0-360°)
# https://www.cssscript.com/demo/hsv-hsl-color-wheel-picker-reinvented/
HUE_START = 100 # green
HUE_END = 0 # red
# some pen colours to use
BLACK = graphics.create_pen(0, 0, 0)
WHITE = graphics.create_pen(255, 255, 255)
GREY = graphics.create_pen(20, 20, 20)
# sets up a handy function we can call to clear the screen
def clear():
graphics.set_pen(BLACK)
graphics.clear()
# some variables to keep track of high / low readings
highest = 0.0
lowest = 4000.0
# set up
breakout_scd41.init(i2c)
breakout_scd41.start()
graphics.set_font("bitmap8")
graphics.set_pen(WHITE)
graphics.text("Waiting!", 0, 0, scale=1)
gu.update(graphics)
while True:
if gu.is_pressed(GalacticUnicorn.SWITCH_A):
# reset recorded high / low values
highest = 0.0
lowest = 4000.0
if breakout_scd41.ready():
clear()
# read the sensor
co2, temperature, humidity = breakout_scd41.measure()
# update highest / lowest values
if co2 < lowest:
lowest = co2
if co2 > highest:
highest = co2
# calculate a colour from the co2 reading
hue = max(0, HUE_START + ((co2 - MIN) * (HUE_END - HUE_START) / (MAX - MIN)))
# create pens with this colour (and with the high / low colours)
CURRENT_COLOUR = graphics.create_pen_hsv(hue / 360, 1.0, 0.8)
HIGH_COLOUR = graphics.create_pen_hsv(HUE_END / 360, 1.0, 0.8)
LOW_COLOUR = graphics.create_pen_hsv(HUE_START / 360, 1.0, 0.8)
# draw the text
graphics.set_pen(CURRENT_COLOUR)
graphics.text("CO2", 0, 0, scale=1)
# measure the rest of the text before drawing so that we can right align it
text_width = graphics.measure_text(f"{co2:.0f}ppm", scale=1)
graphics.text(f"{co2:.0f}ppm", WIDTH - text_width, 0, scale=1)
# draw a bar for the background
graphics.set_pen(GREY)
graphics.rectangle(0, 9, WIDTH, 10)
# draw a bar for the current co2 level
graphics.set_pen(CURRENT_COLOUR)
graphics.rectangle(0, 9, int(co2 / WIDTH), 10)
# draw a bar for the highest co2 level seen
graphics.set_pen(HIGH_COLOUR)
graphics.pixel(int(highest / WIDTH), 9)
graphics.pixel(int(highest / WIDTH), 10)
# draw a bar for the lowest co2 level seen
graphics.set_pen(LOW_COLOUR)
graphics.pixel(int(lowest / WIDTH), 9)
graphics.pixel(int(lowest / WIDTH), 10)
gu.update(graphics)