Skip to content

Commit aca789e

Browse files
authored
Merge branch 'adafruit:main' into factest_37in_qualia
2 parents 91839ea + 4c7517f commit aca789e

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed
819 KB
Binary file not shown.

LEGO_Carousel_PropMaker/code.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# SPDX-FileCopyrightText: 2024 Noe Ruiz for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import board
6+
import audiocore
7+
import audiobusio
8+
import audiomixer
9+
from digitalio import DigitalInOut, Direction
10+
import pwmio
11+
import neopixel
12+
from adafruit_led_animation.animation.rainbow import Rainbow
13+
from adafruit_motor import servo
14+
15+
# enable external power pin
16+
# provides power to the external components
17+
external_power = DigitalInOut(board.EXTERNAL_POWER)
18+
external_power.direction = Direction.OUTPUT
19+
external_power.value = True
20+
21+
# i2s playback
22+
wave_file = open("carousel-loop.wav", "rb")
23+
wave = audiocore.WaveFile(wave_file)
24+
audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA)
25+
mixer = audiomixer.Mixer(voice_count=1, sample_rate=22050, channel_count=1,
26+
bits_per_sample=16, samples_signed=True)
27+
audio.play(mixer)
28+
mixer.voice[0].play(wave, loop=True)
29+
30+
# servo control
31+
pwm = pwmio.PWMOut(board.EXTERNAL_SERVO, frequency=5)
32+
prop_servo = servo.ContinuousServo(pwm)
33+
34+
# external neopixels
35+
num_pixels = 43
36+
pixels = neopixel.NeoPixel(board.EXTERNAL_NEOPIXELS, num_pixels)
37+
pixels.brightness = 0.3
38+
rainbow = Rainbow(pixels, speed=0.05, period=2)
39+
40+
while True:
41+
prop_servo.throttle = 1
42+
rainbow.animate()
43+
mixer.voice[0].level = 1
+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import os
7+
import ssl
8+
import io
9+
import binascii
10+
import jpegio
11+
import microcontroller
12+
import wifi
13+
import socketpool
14+
import displayio
15+
from adafruit_qualia.graphics import Graphics, Displays
16+
import adafruit_minimqtt.adafruit_minimqtt as MQTT
17+
18+
aio_username = os.getenv("ADAFRUIT_AIO_USERNAME")
19+
aio_key = os.getenv("ADAFRUIT_AIO_KEY")
20+
21+
print(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}")
22+
wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
23+
print(f"Connected to {os.getenv('CIRCUITPY_WIFI_SSID')}!")
24+
25+
camera_feed = aio_username + "/feeds/camera"
26+
27+
graphics = Graphics(Displays.ROUND40, default_bg=None, auto_refresh=True)
28+
29+
def center(g, b):
30+
# center the image
31+
g.x -= ((b.width * 2) - 720) // 4
32+
g.y -= ((b.height * 2) - 720) // 4
33+
34+
def decode_image(base64_msg):
35+
# Decode the base64 image into raw binary JPEG data
36+
decoded_image = binascii.a2b_base64(base64_msg)
37+
# Create a JpegDecoder instance
38+
decoder = jpegio.JpegDecoder()
39+
# Use io.BytesIO to treat the decoded image as a file-like object
40+
jpeg_data = io.BytesIO(decoded_image)
41+
# Open the JPEG data source from the BytesIO object
42+
width, height = decoder.open(jpeg_data)
43+
print(width, height)
44+
# Create a Bitmap with the dimensions of the JPEG image
45+
bitmap = displayio.Bitmap(width, height, 65536) # Use 65536 colors for RGB565
46+
# Decode the JPEG into the bitmap
47+
decoder.decode(bitmap)
48+
# pylint: disable=line-too-long
49+
grid = displayio.TileGrid(bitmap, pixel_shader=displayio.ColorConverter(input_colorspace=displayio.Colorspace.RGB565_SWAPPED))
50+
center(grid, bitmap)
51+
group = displayio.Group(scale=2)
52+
group.append(grid)
53+
graphics.display.root_group = group
54+
graphics.display.refresh()
55+
56+
57+
# Define callback methods which are called when events occur
58+
def connected(client, userdata, flags, rc): # pylint: disable=unused-argument
59+
# This function will be called when the client is connected
60+
# successfully to the broker.
61+
print(f"Connected to Adafruit IO! Listening for topic changes on {camera_feed}")
62+
# Subscribe to all changes on the onoff_feed.
63+
client.subscribe(camera_feed)
64+
65+
66+
def disconnected(client, userdata, rc): # pylint: disable=unused-argument
67+
# This method is called when the client is disconnected
68+
print("Disconnected from Adafruit IO!")
69+
70+
71+
def message(client, topic, msg): # pylint: disable=unused-argument
72+
# This method is called when a topic the client is subscribed to
73+
# has a new message.
74+
print(f"New message on topic {topic}")
75+
decode_image(msg)
76+
77+
pool = socketpool.SocketPool(wifi.radio)
78+
ssl_context = ssl.create_default_context()
79+
# Initialize an Adafruit IO HTTP API object
80+
mqtt_client = MQTT.MQTT(
81+
broker="io.adafruit.com",
82+
port=1883,
83+
username=aio_username,
84+
password=aio_key,
85+
socket_pool=pool,
86+
ssl_context=ssl_context,
87+
)
88+
# Setup the callback methods above
89+
mqtt_client.on_connect = connected
90+
mqtt_client.on_disconnect = disconnected
91+
mqtt_client.on_message = message
92+
93+
# Connect the client to the MQTT broker.
94+
print("Connecting to Adafruit IO...")
95+
mqtt_client.connect()
96+
while True:
97+
# Poll the message queue
98+
try:
99+
mqtt_client.loop(timeout=1)
100+
time.sleep(5)
101+
except Exception as error: # pylint: disable=broad-except
102+
print(error)
103+
time.sleep(5)
104+
microcontroller.reset()

0 commit comments

Comments
 (0)