Skip to content

Commit a8e8e8d

Browse files
committed
refactor for fruitjam library new API
1 parent db6b00d commit a8e8e8d

File tree

5 files changed

+77
-112
lines changed

5 files changed

+77
-112
lines changed

Fruit_Jam/Larsio_Paint_Music/sound_manager.py

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@
1919
import adafruit_midi
2020
import audiocore
2121
import audiopwmio
22-
import audiobusio
2322
import audiomixer
2423
import synthio
2524
import board
2625
import adafruit_pathlib as pathlib
27-
import adafruit_tlv320
26+
import adafruit_fruitjam
2827
from adafruit_midi.note_on import NoteOn
2928
from adafruit_midi.note_off import NoteOff
3029
import usb_midi
@@ -67,9 +66,13 @@ def __init__(self, audio_output="pwm", seconds_per_eighth=0.25):
6766
else: # i2s
6867
# optional configuration file for speaker/headphone setting
6968
launcher_config = {}
70-
if pathlib.Path("/launcher.conf.json").exists():
71-
with open("/launcher.conf.json", "r") as f:
72-
launcher_config = json.load(f)
69+
for directory in ("/", "/sd/", "/saves/"):
70+
launcher_config_path = directory + "launcher.conf.json"
71+
if pathlib.Path(launcher_config_path).exists():
72+
with open(launcher_config_path, "r") as f:
73+
launcher_config = launcher_config | json.load(f)
74+
if "audio" not in launcher_config:
75+
launcher_config["audio"] = {}
7376

7477
try:
7578
# Import libraries needed for I2S
@@ -84,11 +87,17 @@ def __init__(self, audio_output="pwm", seconds_per_eighth=0.25):
8487
time.sleep(0.1) # Pause 100ms
8588
reset_pin.value = True # Set high to release from reset
8689

87-
i2c = board.STEMMA_I2C() # initialize I2C
90+
# Initialize TLV320
91+
fjPeriphs = adafruit_fruitjam.Peripherals(
92+
audio_output=launcher_config["audio"].get("output", "headphone"),
93+
safe_volume_limit=launcher_config["audio"].get("volume_override_danger",12),
94+
sample_rate=11025,
95+
bit_depth=16,
96+
i2c=board.STEMMA_I2C()
97+
)
8898

89-
bclck_pin = board.D9
90-
wsel_pin = board.D10
91-
din_pin = board.D11
99+
self.tlv = fjPeriphs.dac
100+
fjPeriphs.audio = audiobusio.I2SOut(board.D9, board.D10, board.D11)
92101

93102
elif 'Fruit Jam' in board_type:
94103
print("Fruit Jam setup")
@@ -98,38 +107,27 @@ def __init__(self, audio_output="pwm", seconds_per_eighth=0.25):
98107
time.sleep(0.1)
99108
reset_pin.value = True
100109

101-
i2c = busio.I2C(board.SCL, board.SDA)
110+
# Initialize TLV320
111+
fjPeriphs = adafruit_fruitjam.Peripherals(
112+
audio_output=launcher_config["audio"].get("output", "headphone"),
113+
safe_volume_limit=launcher_config["audio"].get("volume_override_danger",12),
114+
sample_rate=11025,
115+
bit_depth=16,
116+
i2c=board.I2C()
117+
)
102118

103-
bclck_pin = board.I2S_BCLK
104-
wsel_pin = board.I2S_WS
105-
din_pin = board.I2S_DIN
119+
self.tlv = fjPeriphs.dac
106120

107-
# Initialize TLV320
108-
self.tlv = adafruit_tlv320.TLV320DAC3100(i2c)
109-
110-
# set sample rate & bit depth
111-
self.tlv.configure_clocks(sample_rate=11025, bit_depth=16)
112-
113-
if "tlv320" in launcher_config:
114-
if launcher_config["tlv320"].get("output") == "speaker":
115-
# use speaker
116-
self.tlv.speaker_output = True
117-
self.tlv.dac_volume = launcher_config["tlv320"].get("volume",5) # dB
118-
else:
119-
# use headphones
120-
self.tlv.headphone_output = True
121-
self.tlv.dac_volume = launcher_config["tlv320"].get("volume",0) # dB
122-
else:
123-
# default to headphones
124-
self.tlv.headphone_output = True
125-
self.tlv.dac_volume = 0 # dB
121+
# If volume was specified use it, otherwise use the fruitjam library default
122+
if "volume_override_danger" in launcher_config["audio"]:
123+
fjPeriphs.volume = launcher_config["audio"]["volume_override_danger"]
124+
elif "volume" in launcher_config["audio"]:
125+
fjPeriphs.volume = launcher_config["audio"]["volume"] # FruitJam vol (1-20)
126126

127127
# Setup I2S audio output - important to do this AFTER configuring the DAC
128-
self.audio = audiobusio.I2SOut(
129-
bit_clock=bclck_pin,
130-
word_select=wsel_pin,
131-
data=din_pin
132-
)
128+
# Fruitjam library actually does this before we modify the configuration
129+
# but after the initial default configuration is performed
130+
self.audio = fjPeriphs.audio
133131

134132
print("TLV320 I2S DAC initialized successfully")
135133
except Exception as e:
@@ -617,6 +615,7 @@ def deinit(self):
617615
try:
618616
# For TLV320DAC3100, headphone_output = False will power down the output
619617
self.tlv.headphone_output = False
618+
self.tlv.speaker_output = False
620619
except Exception:
621620
pass
622621

Metro/Metro_RP2350_Chips_Challenge/audio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def __init__(self, audio_bus, sounds):
1414
self.play(tuple(self._wav_files.keys())[0], wait=True)
1515

1616
def play(self, sound_name, wait=False):
17-
if not PLAY_SOUNDS:
17+
if not PLAY_SOUNDS or self._audio is None:
1818
return
1919
if sound_name in self._wav_files:
2020
with open(self._wav_files[sound_name], "rb") as wave_file:

Metro/Metro_RP2350_Chips_Challenge/code.py

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5+
import json
56
import time
67
import board
7-
import picodvi
8-
import framebufferio
9-
import displayio
8+
import supervisor
109
import audiobusio
1110
from audio import Audio
11+
import adafruit_pathlib as pathlib
12+
import adafruit_fruitjam.Peripherals
1213
from game import Game
1314
from definitions import SECOND_LENGTH, TICKS_PER_SECOND
1415

@@ -36,24 +37,41 @@
3637
"TIME_UP": "sounds/bell.wav"
3738
}
3839

39-
displayio.release_displays()
40+
# optional configuration file for speaker/headphone setting
41+
launcher_config = {}
42+
for directory in ("/", "/sd/", "/saves/"):
43+
launcher_config_path = directory + "launcher.conf.json"
44+
if pathlib.Path(launcher_config_path).exists():
45+
with open(launcher_config_path, "r") as f:
46+
launcher_config = launcher_config | json.load(f)
47+
if "audio" not in launcher_config:
48+
launcher_config["audio"] = {}
4049

41-
if hasattr(board, "I2S_BCLK"):
42-
audio_bus = audiobusio.I2SOut(board.I2S_BCLK, board.I2S_WS, board.I2S_DIN)
43-
elif hasattr(board, "D9") and hasattr(board, "D10") and hasattr(board, "D11"):
44-
audio_bus = audiobusio.I2SOut(board.D9, board.D10, board.D11)
50+
51+
fjPeriphs = adafruit_fruitjam.Peripherals.Peripherals(
52+
audio_output=launcher_config["audio"].get("output", "headphone"),
53+
safe_volume_limit=launcher_config["audio"].get("volume_override_danger",12),
54+
sample_rate=44100,
55+
bit_depth=16,
56+
i2c=board.I2C()
57+
)
58+
if not hasattr(board, "I2S_BCLK") and hasattr(board, "D9") and hasattr(board, "D10") and hasattr(board, "D11"):
59+
fjPeriphs.audio = audiobusio.I2SOut(board.D9, board.D10, board.D11)
60+
61+
# If volume was specified use it, otherwise use the fruitjam library default
62+
if "volume_override_danger" in launcher_config["audio"]:
63+
fjPeriphs.volume = launcher_config["audio"]["volume_override_danger"]
64+
elif "volume" in launcher_config["audio"]:
65+
fjPeriphs.volume = launcher_config["audio"]["volume"] # FruitJam vol (1-20)
66+
67+
if fjPeriphs.audio is not None:
68+
audio = Audio(fjPeriphs.audio, SOUND_EFFECTS)
4569
else:
46-
audio_bus = None
47-
audio = Audio(audio_bus, SOUND_EFFECTS)
70+
audio = None
4871

49-
fb = picodvi.Framebuffer(320, 240, clk_dp=board.CKP, clk_dn=board.CKN,
50-
red_dp=board.D0P, red_dn=board.D0N,
51-
green_dp=board.D1P, green_dn=board.D1N,
52-
blue_dp=board.D2P, blue_dn=board.D2N,
53-
color_depth=8)
54-
display = framebufferio.FramebufferDisplay(fb)
72+
adafruit_fruitjam.Peripherals.request_display_config(320, 240, 8)
5573

56-
game = Game(display, DATA_FILE, audio)
74+
game = Game(supervisor.runtime.display, DATA_FILE, audio)
5775
tick_length = SECOND_LENGTH / 1000 / TICKS_PER_SECOND
5876
while True:
5977
start = time.monotonic()

Metro/Metro_RP2350_Chips_Challenge/definitions.py

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,10 @@
11
# SPDX-FileCopyrightText: 2025 Melissa LeBlanc-Williams
22
#
33
# SPDX-License-Identifier: MIT
4-
import json
5-
import time
6-
import board
74
from micropython import const
8-
import adafruit_pathlib as pathlib
9-
import adafruit_tlv320
10-
11-
# optional configuration file for speaker/headphone setting
12-
launcher_config = {}
13-
if pathlib.Path("/launcher.conf.json").exists():
14-
with open("/launcher.conf.json", "r") as f:
15-
launcher_config = json.load(f)
16-
17-
# Check if DAC is connected
18-
if "I2C" in dir(board):
19-
i2c = board.I2C()
20-
for i in range(500): # try for 5 seconds
21-
if i2c.try_lock():
22-
break
23-
time.sleep(0.01)
24-
if 0x18 in i2c.scan():
25-
tlv320_present = True
26-
else:
27-
tlv320_present = False
28-
i2c.unlock()
29-
else:
30-
tlv320_present = False
31-
32-
if tlv320_present:
33-
dac = adafruit_tlv320.TLV320DAC3100(i2c)
34-
35-
# set sample rate & bit depth
36-
dac.configure_clocks(sample_rate=44100, bit_depth=16)
37-
38-
if "tlv320" in launcher_config:
39-
if launcher_config["tlv320"].get("output") == "speaker":
40-
# use speaker
41-
dac.speaker_output = True
42-
dac.dac_volume = launcher_config["tlv320"].get("volume",5) # dB
43-
else:
44-
# use headphones
45-
dac.headphone_output = True
46-
dac.dac_volume = launcher_config["tlv320"].get("volume",0) # dB
47-
else:
48-
# default to headphones
49-
dac.headphone_output = True
50-
dac.dac_volume = 0 # dB
51-
52-
if tlv320_present:
53-
_default_play_sounds = True
54-
else:
55-
_default_play_sounds = False
56-
57-
if "sound" in launcher_config:
58-
if launcher_config["tlv320"].get("output") == "mute":
59-
_default_play_sounds = False
605

616
# Settings
62-
PLAY_SOUNDS = _default_play_sounds
7+
PLAY_SOUNDS = True
638

649
# Timing Constants
6510
TICKS_PER_SECOND = const(20)

Metro/Metro_RP2350_Chips_Challenge/savestate.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ def _mount_sd_card(self):
3232
except OSError:
3333
pass
3434

35-
self._card_detect = DigitalInOut(board.SD_CARD_DETECT)
35+
try:
36+
self._card_detect = DigitalInOut(board.SD_CARD_DETECT)
37+
except ValueError:
38+
return False
3639
self._card_detect.switch_to_input(pull=Pull.UP)
3740
if self._card_detect.value:
3841
return False

0 commit comments

Comments
 (0)