Skip to content

Instantly share code, notes, and snippets.

@vitorio
Last active January 15, 2024 01:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vitorio/3a2a5926913a023b2b1b1cb92e8f1056 to your computer and use it in GitHub Desktop.
Save vitorio/3a2a5926913a023b2b1b1cb92e8f1056 to your computer and use it in GitHub Desktop.
CO2 monitor using Pimoroni Breakout Garden + 5x5 RGB Matrix + SCD41 breakouts, MicroPython 1.21
# Quick script to convert Adafruit GFX font into binary file.
# Author: Tony DiCola
# License: MIT (https://opensource.org/licenses/MIT)
# Taken from glcdfont.c from Adafruit GFX Arduino library.
FONT = bytes((
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0b11111, 0b11111, #0
0b00000, 0b11111, #1
0b10111, 0b11101, #2
0b10101, 0b11111, #3
0b11100, 0b00111, #4
0b11101, 0b10111, #5
0b11111, 0b00111, #6
0b10000, 0b11111, #7
0b11011, 0b11011, #8
0b11100, 0b11111, #9
0b00000, 0b01010 #:
))
if __name__ == '__main__':
with open('font2x5.bin', 'wb') as outfile:
# Write a byte each for the character width, character height.
outfile.write(bytes((2, 5)))
# Now write all of the font character bytes.
for font_byte in FONT:
outfile.write(font_byte.to_bytes(1, 'big'))
import utime
from pimoroni import BREAKOUT_GARDEN_I2C_PINS
from pimoroni_i2c import PimoroniI2C
from breakout_rgbmatrix5x5 import BreakoutRGBMatrix5x5
import breakout_scd41
import bitmapfont # https://github.com/adafruit/micropython-adafruit-bitmap-font
MESSAGE = '0000'
DISPLAY_WIDTH = 5 # Display width in pixels.
DISPLAY_HEIGHT = 5 # Display height in pixels.
SPEED = 5.0 # Scroll speed in pixels per second.
co2 = 0
i2c = PimoroniI2C(**BREAKOUT_GARDEN_I2C_PINS)
matrix = BreakoutRGBMatrix5x5(i2c)
breakout_scd41.init(i2c)
breakout_scd41.start()
with bitmapfont.BitmapFont(5, 5, matrix.set_pixel, 'font2x5.bin') as bf:
# Global state:
pos = DISPLAY_WIDTH # X position of the message start.
message_width = bf.width(MESSAGE) # Message width in pixels.
last = utime.ticks_ms() # Last frame millisecond tick time.
speed_ms = SPEED / 1000.0 # Scroll speed in pixels/ms.
# Main loop:
while True:
# Compute the time delta in milliseconds since the last frame.
current = utime.ticks_ms()
delta_ms = utime.ticks_diff(current, last)
last = current
# Compute position using speed and time delta.
pos -= speed_ms*delta_ms
if pos < -message_width:
pos = DISPLAY_WIDTH
if breakout_scd41.ready():
co2, temperature, humidity = breakout_scd41.measure()
MESSAGE = f'{co2: <4}' # this is okay because it's a fixed-width font
# Clear the matrix and draw the text at the current position.
matrix.clear()
if co2 < 600:
bf.text(MESSAGE, int(pos), 0, 0, 255, 0)
elif co2 >= 800:
bf.text(MESSAGE, int(pos), 0, 255, 0, 000)
else:
bf.text(MESSAGE, int(pos), 0, 255, 255, 0)
# Update the matrix LEDs.
matrix.update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment