Last active
November 14, 2020 04:11
-
-
Save MadMaxMcKinney/2c124fd868f77e9278d6b5dbbe9726cd to your computer and use it in GitHub Desktop.
A simple 'busy light' made for Adafruit MatrixPortal for displaying your meeting status to those outside your home office.
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
# Author: Max McKinney | |
# Description: A simple 'busy light' for displaying your meeting status to those outside your home office. | |
import time | |
import random | |
import board | |
import terminalio | |
import displayio | |
import neopixel | |
from digitalio import DigitalInOut, Direction, Pull | |
from adafruit_matrixportal.matrixportal import MatrixPortal | |
from adafruit_bitmap_font import bitmap_font | |
from adafruit_display_text.label import Label | |
from adafruit_display_shapes.rect import Rect | |
from adafruit_matrixportal.network import Network | |
# --- Vars | |
UPDATE_DELAY = 3 | |
IS_BUSY = True | |
IS_DIMMED = False | |
# --- | |
# --- Display setup --- | |
matrixportal = MatrixPortal(debug=False) | |
display = matrixportal.display | |
# Turn off onboard neopixel | |
pixels = neopixel.NeoPixel(board.NEOPIXEL, 1) | |
pixels[0] = (0, 0, 0) | |
# Create groups to hold content | |
contentGroup = displayio.Group() | |
emptyGroup = displayio.Group() | |
# --- Content --- | |
# Background object | |
background = Rect(0,0,64,32, fill=0x000000) | |
contentGroup.append(background) | |
# Create status label (with a default y spacing for the 'connecting' text | |
font = bitmap_font.load_font("/IBMPlexMono-Medium-24_jep.bdf") | |
status_label = Label(font,y=14, max_glyphs=20) | |
# Default text show as connecting, once the internet connection has happened it will resume the code flow and update the text | |
status_label.text = "..." | |
contentGroup.append(status_label) | |
# --- | |
def update_bg(): | |
# Pop the background off the bottom of the stack | |
background = contentGroup.pop(0) | |
# Change the background color of the background | |
if IS_BUSY: | |
background.fill = 0xFF0000 | |
else: | |
background.fill = 0x00FF00 | |
# Insert the modified background back at the bottom of the stack | |
contentGroup.insert(0, background) | |
def update_text(): | |
# Set status text | |
status_label.text = "BUSY" if IS_BUSY else "FREE" | |
# Set status text position | |
# Get the bouding box info from the label component so we can center it. I subtract 1 from the final value to offset it due to the custom font sizing. | |
bbx, bby, bbwidth, bbh = status_label.bounding_box | |
status_label.x = round((display.width / 2 - bbwidth / 2) - 1) | |
status_label.y = display.height // 2 | |
def fetch_updates(): | |
print("Fetching status...") | |
# Returns a JSON object as a python dict object from the selected AdafruitIO feed | |
meetingFeed = matrixportal.get_io_feed("meeting-status.inmeeting") | |
meetingValue = meetingFeed['last_value'] | |
isDimmedFeed = matrixportal.get_io_feed("meeting-status.isdimmed") | |
isDimmedValue = isDimmedFeed['last_value'] | |
# Update the value with the converted string | |
global IS_BUSY | |
IS_BUSY = str_to_bool(meetingValue) | |
global IS_DIMMED | |
IS_DIMMED = str_to_bool(isDimmedValue) | |
def update_screen(): | |
fetch_updates() | |
update_bg() | |
update_text() | |
def str_to_bool(s): | |
if s == 'True': | |
return True | |
elif s == 'False': | |
return False | |
else: | |
raise ValueError # evil ValueError that doesn't tell you what the wrong value was | |
# --- Initial display --- | |
display.show(contentGroup) | |
update_screen() | |
last_update = time.monotonic() | |
# --- Loop --- | |
while True: | |
# Check if dimmed, then hide content | |
# Once a bug within Circuitpython has been fixed I can use display.brightness | |
# https://github.com/adafruit/circuitpython/issues/3664 | |
if IS_DIMMED: | |
display.show(emptyGroup) | |
else: | |
display.show(contentGroup) | |
# Status checking interval | |
if time.monotonic() > last_update + UPDATE_DELAY: | |
update_screen() | |
last_update = time.monotonic() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment