-
Notifications
You must be signed in to change notification settings - Fork 491
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #568 from pimoroni/hel-gfx-pack
GFX pack examples and docs tweaks
- Loading branch information
Showing
9 changed files
with
294 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Pico GFX Pack MicroPython Examples <!-- omit in toc --> | ||
|
||
- [About Pico GFX Pack](#about-pico-gfx-pack) | ||
- [Pico GFX Pack and PicoGraphics](#pico-gfx-pack-and-picographics) | ||
- [Basic Examples](#basic-examples) | ||
- [Balls Demo](#balls-demo) | ||
- [Button Test](#button-test) | ||
- [Rainbow](#rainbow) | ||
- [Snake](#snake) | ||
- [Advanced Examples](#advanced-examples) | ||
- [CO2](#co2) | ||
- [Thermometer](#thermometer) | ||
|
||
## About Pico GFX Pack | ||
|
||
GFX Pack adds a 128x64 LCD Matrix display to your headered Raspberry Pi Pico or PicoW, with RGBW backlight and 5 input buttons for all your display and control needs. | ||
|
||
- :link: [Pico GFX Pack store page](https://shop.pimoroni.com/products/pico-gfx-pack) | ||
|
||
You'll need to download the most recent version of our MicroPython firmware for your board from the link below. | ||
|
||
- [MicroPython releases](https://github.com/pimoroni/pimoroni-pico/releases) | ||
- [Installing MicroPython](../../../setting-up-micropython.md) | ||
|
||
## Pico GFX Pack and PicoGraphics | ||
|
||
The easiest way to start displaying cool stuff on GFX Pack is using our GFX Pack helper module (which contains a bunch of useful shortcuts for interacting with the board) and our PicoGraphics display library which handles drawing things on the screen. | ||
|
||
- [GFX Pack function reference](../../modules_py/gfx_pack.md) | ||
- [PicoGraphics function reference](../../modules/picographics/README.md) | ||
|
||
## Basic Examples | ||
|
||
### Balls Demo | ||
|
||
[balls_demo.py](balls_demo.py) | ||
|
||
LCD demo with a bunch of bouncy balls! | ||
|
||
### Button Test | ||
|
||
[button_test.py](button_test.py) | ||
|
||
Shows how to read the buttons, display text and change the colour of the RGBW backlight. | ||
|
||
### Rainbow | ||
|
||
[rainbow.py](rainbow.py) | ||
|
||
This example borrows a CircuitPython hsv to rgb function to cycle through some rainbows on GFX Pack's screen. | ||
|
||
### Snake | ||
|
||
[snake.py](snake.py) | ||
|
||
Basic Snake demo for GFX Pack. | ||
|
||
- A = up | ||
- B = down | ||
- C = reset | ||
- D = left | ||
- E = right | ||
|
||
## Advanced Examples | ||
|
||
These examples require additional hardware. We connected our breakouts using a JST-SH to JST-SH cable: | ||
|
||
- :link: [Qw/ST cable store page](https://shop.pimoroni.com/products/jst-sh-cable-qwiic-stemma-qt-compatible?variant=31910609813587) | ||
|
||
### CO2 | ||
|
||
[co2.py](co2.py) | ||
|
||
Add a SCD41 sensor breakout to your GFX Pack to make a handy CO2 detector! | ||
Press A to reset the high/low values. | ||
|
||
- :link: [SCD41 breakout store page](https://shop.pimoroni.com/products/scd41-co2-sensor-breakout) | ||
|
||
### Thermometer | ||
|
||
[thermometer.py](thermometer.py) | ||
|
||
This demo uses a BME680 or BME688 breakout to measure temperature, pressure, and humidity and display it on the GFX display. | ||
|
||
To use the Pico's internal temperature sensor in place of the BME68x breakout, just change `use_bme68x_breakout` to `False`. | ||
|
||
- :link: [BME680 breakout store page](https://shop.pimoroni.com/products/bme680-breakout) | ||
- :link: [BME688 breakout store page](https://shop.pimoroni.com/products/bme688-breakout) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# This example shows you a simple, non-interrupt way of reading GFX Pack's buttons with a loop that checks to see if buttons are pressed. | ||
|
||
import time | ||
from gfx_pack import GfxPack, SWITCH_A, SWITCH_B, SWITCH_C, SWITCH_D, SWITCH_E | ||
|
||
gp = GfxPack() | ||
display = gp.display | ||
|
||
WIDTH, HEIGHT = display.get_bounds() | ||
display.set_backlight(0) # turn off the white component of the backlight | ||
|
||
|
||
# sets up a handy function we can call to clear the screen | ||
def clear(): | ||
display.set_pen(0) | ||
display.clear() | ||
display.set_pen(15) | ||
|
||
|
||
# set up | ||
display.set_font("bitmap8") | ||
|
||
while True: | ||
if gp.switch_pressed(SWITCH_A): # if a button press is detected... # clear to black | ||
gp.set_backlight(255, 0, 0, 0) # red, green, blue, white | ||
clear() | ||
display.text("Button A pressed", 0, 0, WIDTH, 2) # display some text on the screen | ||
display.update() # update the display | ||
time.sleep(1) | ||
elif gp.switch_pressed(SWITCH_B): | ||
gp.set_backlight(255, 125, 0, 0) | ||
clear() | ||
display.text("Button B pressed", 0, 0, WIDTH, 2) | ||
display.update() | ||
time.sleep(1) | ||
elif gp.switch_pressed(SWITCH_C): | ||
gp.set_backlight(0, 255, 0, 0) | ||
clear() | ||
display.text("Button C pressed", 0, 0, WIDTH, 2) | ||
display.update() | ||
time.sleep(1) | ||
elif gp.switch_pressed(SWITCH_D): | ||
gp.set_backlight(0, 0, 255, 0) | ||
clear() | ||
display.text("Button D pressed", 0, 0, WIDTH, 2) | ||
display.update() | ||
time.sleep(1) | ||
elif gp.switch_pressed(SWITCH_E): | ||
gp.set_backlight(255, 0, 255, 0) | ||
clear() | ||
display.text("Button E pressed", 0, 0, WIDTH, 2) | ||
display.update() | ||
time.sleep(1) | ||
else: | ||
gp.set_backlight(0, 0, 0, 125) | ||
clear() | ||
display.set_pen(15) | ||
display.text("Press any button!", 0, 0, WIDTH, 2) | ||
display.update() | ||
time.sleep(0.01) # this number is how frequently the Pico checks for button presses |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
# Add a SCD41 sensor breakout to your GFX Pack to make a handy CO2 detector! | ||
# https://shop.pimoroni.com/products/scd41-co2-sensor-breakout | ||
# Press A to reset the high/low values. | ||
|
||
from gfx_pack import GfxPack, SWITCH_A | ||
import breakout_scd41 | ||
|
||
gp = GfxPack() | ||
display = gp.display | ||
|
||
WIDTH, HEIGHT = display.get_bounds() | ||
display.set_backlight(0) # turn off the white component of the backlight | ||
|
||
# the range of readings to map to colours (and scale our graph to) | ||
# 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 | ||
|
||
BRIGHTNESS = 0.5 | ||
|
||
# the area of the screen we want to draw our graph into | ||
GRAPH_TOP = 24 | ||
GRAPH_BOTTOM = 54 | ||
|
||
|
||
# sets up a handy function we can call to clear the screen | ||
def clear(): | ||
display.set_pen(0) | ||
display.clear() | ||
display.set_pen(15) | ||
|
||
|
||
# From CPython Lib/colorsys.py | ||
def hsv_to_rgb(h, s, v): | ||
if s == 0.0: | ||
return v, v, v | ||
i = int(h * 6.0) | ||
f = (h * 6.0) - i | ||
p = v * (1.0 - s) | ||
q = v * (1.0 - s * f) | ||
t = v * (1.0 - s * (1.0 - f)) | ||
i = i % 6 | ||
if i == 0: | ||
return v, t, p | ||
if i == 1: | ||
return q, v, p | ||
if i == 2: | ||
return p, v, t | ||
if i == 3: | ||
return p, q, v | ||
if i == 4: | ||
return t, p, v | ||
if i == 5: | ||
return v, p, q | ||
|
||
|
||
highest = 0.0 | ||
lowest = 4000.0 | ||
readings = [] | ||
|
||
# set up | ||
breakout_scd41.init(gp.i2c) | ||
breakout_scd41.start() | ||
|
||
gp.set_backlight(0, 0, 0, 127) | ||
display.set_font("bitmap8") | ||
display.set_pen(15) | ||
display.text("Waiting for sensor to be ready", 0, 0, WIDTH, 2) | ||
display.update() | ||
|
||
while True: | ||
if gp.switch_pressed(SWITCH_A): | ||
# reset recorded high / low values | ||
highest = 0.0 | ||
lowest = 4000.0 | ||
|
||
if breakout_scd41.ready(): | ||
# read the sensor | ||
co2, temperature, humidity = breakout_scd41.measure() | ||
|
||
# update highest / lowest values | ||
if co2 < lowest: | ||
lowest = co2 | ||
if co2 > highest: | ||
highest = co2 | ||
|
||
# calculates a colour from the sensor reading | ||
hue = max(0, HUE_START + ((co2 - MIN) * (HUE_END - HUE_START) / (MAX - MIN))) | ||
|
||
# set the leds | ||
r, g, b = [int(255 * c) for c in hsv_to_rgb(hue / 360, 1.0, BRIGHTNESS)] | ||
gp.set_backlight(r, g, b, 0) | ||
|
||
# keep track of readings in a list (so we can draw the graph) | ||
readings.append(co2) | ||
# we only need to save a screen's worth of readings, so delete the oldest | ||
if len(readings) > WIDTH: | ||
readings.pop(0) | ||
|
||
# draw the graph | ||
clear() | ||
for r in range(len(readings)): | ||
# this line scales the y axis of the graph into the available space | ||
y = round(GRAPH_BOTTOM + ((readings[r] - MIN) * (GRAPH_TOP - GRAPH_BOTTOM) / (MAX - MIN))) | ||
display.pixel(r, y) | ||
|
||
# draw the text | ||
display.text("CO2", 0, 0, scale=2) | ||
display.text(f"Temp {temperature:.0f}°c", 0, 16, scale=1) | ||
display.text(f"Low {lowest:.0f}ppm", 0, HEIGHT - 8, scale=1) | ||
# measure the rest of the text before drawing so that we can right align it | ||
text_width = display.measure_text(f"{co2:.0f}ppm", scale=2) | ||
display.text(f"{co2:.0f}ppm", WIDTH - text_width, 0, scale=2) | ||
text_width = display.measure_text(f"Humidity {humidity:.0f}%", scale=1) | ||
display.text(f"Humidity {humidity:.0f}%", WIDTH - text_width, 16, scale=1) | ||
text_width = display.measure_text(f"High {highest:.0f}ppm", scale=1) | ||
display.text(f"High {highest:.0f}ppm", WIDTH - text_width, HEIGHT - 8, scale=1) | ||
|
||
display.update() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters