Skip to content

More portalbase APIs, fixes for status_led and caption text #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions adafruit_fruitjam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def __init__( # noqa: PLR0912,PLR0913,Too many branches,Too many arguments in f

network = Network(
status_neopixel=self.peripherals.neopixels
if status_neopixel is None
if status_neopixel is None or status_neopixel == board.NEOPIXEL
else status_neopixel,
esp=esp,
external_spi=spi,
Expand Down Expand Up @@ -191,14 +191,14 @@ def __init__( # noqa: PLR0912,PLR0913,Too many branches,Too many arguments in f

# Convenience Shortcuts for compatibility

# self.sd_check = self.peripherals.sd_check
# self.play_file = self.peripherals.play_file
# self.stop_play = self.peripherals.stop_play
self.sd_check = self.peripherals.sd_check
self.play_file = self.peripherals.play_file
self.stop_play = self.peripherals.stop_play

self.image_converter_url = self.network.image_converter_url
self.wget = self.network.wget
# self.show_QR = self.graphics.qrcode
# self.hide_QR = self.graphics.hide_QR
self.show_QR = self.graphics.qrcode
self.hide_QR = self.graphics.hide_QR

if default_bg is not None:
self.graphics.set_background(default_bg)
Expand All @@ -207,7 +207,8 @@ def __init__( # noqa: PLR0912,PLR0913,Too many branches,Too many arguments in f
print("Init caption")
if caption_font:
self._caption_font = self._load_font(caption_font)
self.set_caption(caption_text, caption_position, caption_color)
if caption_text is not None:
self.set_caption(caption_text, caption_position, caption_color)

if text_font:
if text_position is not None and isinstance(text_position[0], (list, tuple)):
Expand Down
31 changes: 31 additions & 0 deletions adafruit_fruitjam/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,34 @@ def __init__(
if supervisor.runtime.display is None:
request_display_config(640, 480)
super().__init__(supervisor.runtime.display, default_bg=default_bg, debug=debug)

def qrcode(self, qr_data, *, qr_size=1, x=0, y=0, hide_background=False): # noqa: PLR0913 Too many arguments in function definition
"""Display a QR code

:param qr_data: The data for the QR code.
:param int qr_size: The scale of the QR code.
:param x: The x position of upper left corner of the QR code on the display.
:param y: The y position of upper left corner of the QR code on the display.
:param hide_background: Hide the background while showing the QR code.

"""
super().qrcode(
qr_data,
qr_size=qr_size,
x=x,
y=y,
)
if hide_background:
self.display.root_group = self._qr_group
self._qr_only = hide_background

def hide_QR(self):
"""Clear any QR codes that are currently on the screen"""

if self._qr_only:
self.display.root_group = self.root_group
else:
try:
self._qr_group.pop()
except (IndexError, AttributeError): # later test if empty
pass
63 changes: 63 additions & 0 deletions adafruit_fruitjam/peripherals.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,19 @@

"""

import os

import adafruit_sdcard
import adafruit_tlv320
import audiobusio
import audiocore
import board
import busio
import digitalio
import displayio
import framebufferio
import picodvi
import storage
import supervisor
from digitalio import DigitalInOut, Direction, Pull
from neopixel import NeoPixel
Expand Down Expand Up @@ -140,6 +147,35 @@ def __init__(self):

self._audio = audiobusio.I2SOut(board.I2S_BCLK, board.I2S_WS, board.I2S_DIN)

self._sd_mounted = False
sd_pins_in_use = False
SD_CS = board.SD_CS
# try to Connect to the sdcard card and mount the filesystem.
try:
# initialze CS pin
cs = digitalio.DigitalInOut(SD_CS)
except ValueError:
# likely the SDCard was auto-initialized by the core
sd_pins_in_use = True

# if placeholder.txt file does not exist
if "placeholder.txt" not in os.listdir("/sd/"):
self._sd_mounted = True

if not sd_pins_in_use:
try:
# if sd CS pin was not in use
# try to initialize and mount the SDCard
sdcard = adafruit_sdcard.SDCard(
busio.SPI(board.SD_SCK, board.SD_MOSI, board.SD_MISO), cs
)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")
self._sd_mounted = True
except OSError:
# sdcard init or mounting failed
self._sd_mounted = False

@property
def button1(self) -> bool:
"""
Expand Down Expand Up @@ -175,3 +211,30 @@ def dac(self):
@property
def audio(self):
return self._audio

def sd_check(self):
return self._sd_mounted

def play_file(self, file_name, wait_to_finish=True):
"""Play a wav file.

:param str file_name: The name of the wav file to play on the speaker.
:param bool wait_to_finish: flag to determine if this is a blocking call

"""

# can't use `with` because we need wavefile to remain open after return
self.wavfile = open(file_name, "rb")
wavedata = audiocore.WaveFile(self.wavfile)
self.audio.play(wavedata)
if not wait_to_finish:
return
while self.audio.playing:
pass
self.wavfile.close()

def stop_play(self):
"""Stops playing a wav file."""
self.audio.stop()
if self.wavfile is not None:
self.wavfile.close()
2 changes: 2 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
"framebufferio",
"picodvi",
"audiobusio",
"audiocore",
"storage",
"terminalio",
]

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ adafruit-circuitpython-esp32spi
adafruit-circuitpython-requests
adafruit-circuitpython-bitmap-font
adafruit-circuitpython-display-text
adafruit-circuitpython-sd