Skip to content

Commit 17d3868

Browse files
authored
Merge pull request #6 from FoamyGuy/more_portalbase_stuff
More portalbase APIs, fixes for status_led and caption text
2 parents 7de2ec3 + 2a5d7e8 commit 17d3868

File tree

5 files changed

+105
-7
lines changed

5 files changed

+105
-7
lines changed

adafruit_fruitjam/__init__.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def __init__( # noqa: PLR0912,PLR0913,Too many branches,Too many arguments in f
162162

163163
network = Network(
164164
status_neopixel=self.peripherals.neopixels
165-
if status_neopixel is None
165+
if status_neopixel is None or status_neopixel == board.NEOPIXEL
166166
else status_neopixel,
167167
esp=esp,
168168
external_spi=spi,
@@ -191,14 +191,14 @@ def __init__( # noqa: PLR0912,PLR0913,Too many branches,Too many arguments in f
191191

192192
# Convenience Shortcuts for compatibility
193193

194-
# self.sd_check = self.peripherals.sd_check
195-
# self.play_file = self.peripherals.play_file
196-
# self.stop_play = self.peripherals.stop_play
194+
self.sd_check = self.peripherals.sd_check
195+
self.play_file = self.peripherals.play_file
196+
self.stop_play = self.peripherals.stop_play
197197

198198
self.image_converter_url = self.network.image_converter_url
199199
self.wget = self.network.wget
200-
# self.show_QR = self.graphics.qrcode
201-
# self.hide_QR = self.graphics.hide_QR
200+
self.show_QR = self.graphics.qrcode
201+
self.hide_QR = self.graphics.hide_QR
202202

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

212213
if text_font:
213214
if text_position is not None and isinstance(text_position[0], (list, tuple)):

adafruit_fruitjam/graphics.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,34 @@ def __init__(
6666
if supervisor.runtime.display is None:
6767
request_display_config(640, 480)
6868
super().__init__(supervisor.runtime.display, default_bg=default_bg, debug=debug)
69+
70+
def qrcode(self, qr_data, *, qr_size=1, x=0, y=0, hide_background=False): # noqa: PLR0913 Too many arguments in function definition
71+
"""Display a QR code
72+
73+
:param qr_data: The data for the QR code.
74+
:param int qr_size: The scale of the QR code.
75+
:param x: The x position of upper left corner of the QR code on the display.
76+
:param y: The y position of upper left corner of the QR code on the display.
77+
:param hide_background: Hide the background while showing the QR code.
78+
79+
"""
80+
super().qrcode(
81+
qr_data,
82+
qr_size=qr_size,
83+
x=x,
84+
y=y,
85+
)
86+
if hide_background:
87+
self.display.root_group = self._qr_group
88+
self._qr_only = hide_background
89+
90+
def hide_QR(self):
91+
"""Clear any QR codes that are currently on the screen"""
92+
93+
if self._qr_only:
94+
self.display.root_group = self.root_group
95+
else:
96+
try:
97+
self._qr_group.pop()
98+
except (IndexError, AttributeError): # later test if empty
99+
pass

adafruit_fruitjam/peripherals.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,19 @@
2626
2727
"""
2828

29+
import os
30+
31+
import adafruit_sdcard
2932
import adafruit_tlv320
3033
import audiobusio
34+
import audiocore
3135
import board
36+
import busio
37+
import digitalio
3238
import displayio
3339
import framebufferio
3440
import picodvi
41+
import storage
3542
import supervisor
3643
from digitalio import DigitalInOut, Direction, Pull
3744
from neopixel import NeoPixel
@@ -140,6 +147,35 @@ def __init__(self):
140147

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

150+
self._sd_mounted = False
151+
sd_pins_in_use = False
152+
SD_CS = board.SD_CS
153+
# try to Connect to the sdcard card and mount the filesystem.
154+
try:
155+
# initialze CS pin
156+
cs = digitalio.DigitalInOut(SD_CS)
157+
except ValueError:
158+
# likely the SDCard was auto-initialized by the core
159+
sd_pins_in_use = True
160+
161+
# if placeholder.txt file does not exist
162+
if "placeholder.txt" not in os.listdir("/sd/"):
163+
self._sd_mounted = True
164+
165+
if not sd_pins_in_use:
166+
try:
167+
# if sd CS pin was not in use
168+
# try to initialize and mount the SDCard
169+
sdcard = adafruit_sdcard.SDCard(
170+
busio.SPI(board.SD_SCK, board.SD_MOSI, board.SD_MISO), cs
171+
)
172+
vfs = storage.VfsFat(sdcard)
173+
storage.mount(vfs, "/sd")
174+
self._sd_mounted = True
175+
except OSError:
176+
# sdcard init or mounting failed
177+
self._sd_mounted = False
178+
143179
@property
144180
def button1(self) -> bool:
145181
"""
@@ -175,3 +211,30 @@ def dac(self):
175211
@property
176212
def audio(self):
177213
return self._audio
214+
215+
def sd_check(self):
216+
return self._sd_mounted
217+
218+
def play_file(self, file_name, wait_to_finish=True):
219+
"""Play a wav file.
220+
221+
:param str file_name: The name of the wav file to play on the speaker.
222+
:param bool wait_to_finish: flag to determine if this is a blocking call
223+
224+
"""
225+
226+
# can't use `with` because we need wavefile to remain open after return
227+
self.wavfile = open(file_name, "rb")
228+
wavedata = audiocore.WaveFile(self.wavfile)
229+
self.audio.play(wavedata)
230+
if not wait_to_finish:
231+
return
232+
while self.audio.playing:
233+
pass
234+
self.wavfile.close()
235+
236+
def stop_play(self):
237+
"""Stops playing a wav file."""
238+
self.audio.stop()
239+
if self.wavfile is not None:
240+
self.wavfile.close()

docs/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
"framebufferio",
3131
"picodvi",
3232
"audiobusio",
33+
"audiocore",
34+
"storage",
3335
"terminalio",
3436
]
3537

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ adafruit-circuitpython-esp32spi
1212
adafruit-circuitpython-requests
1313
adafruit-circuitpython-bitmap-font
1414
adafruit-circuitpython-display-text
15+
adafruit-circuitpython-sd

0 commit comments

Comments
 (0)