Last active
March 14, 2024 14:33
-
-
Save mjvo/73ca4928af808be00817c59e98830b14 to your computer and use it in GitHub Desktop.
Circuitpython code accompanying https://editor.p5js.org/mjvo/sketches/JeV10d8kX
This file contains hidden or 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
""" | |
Read the Serial port to receive color data for the neopixel. | |
This uses the optional second serial port available in Circuitpython 7.x | |
Activate it in the boot.py file with the following code | |
import usb_cdc | |
usb_cdc.enable(console=True, data=True) | |
Some boards might require disabling USB endpoints to enable the data port. | |
""" | |
import time | |
import board | |
import digitalio | |
import json | |
import usb_cdc | |
import keypad | |
################################################################ | |
# select the serial Data port | |
################################################################ | |
serial = usb_cdc.data | |
################################################################ | |
# init bluefruit's onboard LED for visual indicator | |
# init bluefruit's button A for sending message | |
# replace with your own pins and stuff | |
################################################################ | |
led = digitalio.DigitalInOut(board.LED) | |
led.switch_to_output() | |
btn = keypad.Keys((board.BUTTON_A,), value_when_pressed=True, pull=True) | |
# above used keypad library for debouncing | |
# see https://learn.adafruit.com/key-pad-matrix-scanning-in-circuitpython | |
while True: | |
# read the secondary serial line by line when there's data | |
# note that this assumes that the host always sends a full line, terminated by \n | |
if serial.in_waiting > 0: | |
data_in = serial.readline() | |
# try to convert the data to a dict (with JSON) | |
data = None | |
if data_in: | |
print(data_in) | |
try: | |
#data = data_in.decode() | |
data = json.loads(data_in.decode()) | |
except ValueError: | |
data = {"raw": data_in.decode()} | |
# by using a dictionary, you can add any entry and data into it | |
# to transmit any command you want and parse it here | |
if isinstance(data, dict): | |
# looks for key "toggle" in data dictionary | |
if "toggle" in data: | |
print(data["toggle"]) # log to local console | |
led.value = data["toggle"] # toggle LED on and off | |
# by using a dictionary, you can add any entry and data into it | |
# to transmit any command you want and parse it here | |
# this is where the rest of your code goes | |
# if the code does a lot you don't need a call to sleep, but if possible | |
# it's good to have the microcontroller sleep from time to time so it's | |
# not constantly chugging | |
event = btn.events.get() | |
# event will be None if nothing has happened. | |
if event: | |
if event.pressed: | |
print(event) | |
msg = b"button A pressed\n" | |
serial.write(msg) | |
time.sleep(0.1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment