|
9 | 9 | import math |
10 | 10 | import time |
11 | 11 | import array |
| 12 | +import json |
12 | 13 | import gc |
13 | 14 | import os |
14 | 15 | import digitalio |
|
22 | 23 | import audiomixer |
23 | 24 | import synthio |
24 | 25 | import board |
| 26 | +import adafruit_pathlib as pathlib |
25 | 27 | import adafruit_tlv320 |
26 | 28 | from adafruit_midi.note_on import NoteOn |
27 | 29 | from adafruit_midi.note_off import NoteOff |
@@ -63,6 +65,15 @@ def __init__(self, audio_output="pwm", seconds_per_eighth=0.25): |
63 | 65 | # Setup PWM audio output on D10 |
64 | 66 | self.audio = audiopwmio.PWMAudioOut(board.D10) |
65 | 67 | else: # i2s |
| 68 | + # optional configuration file for speaker/headphone setting, check current and root directory |
| 69 | + 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) |
| 73 | + elif pathlib.Path("/launcher.conf.json").exists(): |
| 74 | + with open("/launcher.conf.json", "r") as f: |
| 75 | + launcher_config = json.load(f) |
| 76 | + |
66 | 77 | try: |
67 | 78 | # Import libraries needed for I2S |
68 | 79 | #check for Metro RP2350 vs. Fruit Jam |
@@ -96,20 +107,49 @@ def __init__(self, audio_output="pwm", seconds_per_eighth=0.25): |
96 | 107 | wsel_pin = board.I2S_WS |
97 | 108 | din_pin = board.I2S_DIN |
98 | 109 |
|
| 110 | + # Check if DAC is connected |
| 111 | + while not i2c.try_lock(): |
| 112 | + time.sleep(0.01) |
| 113 | + if 0x18 in i2c.scan(): |
| 114 | + ltv320_present = True |
| 115 | + else: |
| 116 | + ltv320_present = False |
| 117 | + i2c.unlock() |
| 118 | + |
99 | 119 | # Initialize TLV320 |
100 | | - self.tlv = adafruit_tlv320.TLV320DAC3100(i2c) |
101 | | - self.tlv.configure_clocks(sample_rate=11025, bit_depth=16) |
102 | | - self.tlv.headphone_output = True |
103 | | - self.tlv.headphone_volume = -15 # dB |
104 | | - |
105 | | - # Setup I2S audio output - important to do this AFTER configuring the DAC |
106 | | - self.audio = audiobusio.I2SOut( |
107 | | - bit_clock=bclck_pin, |
108 | | - word_select=wsel_pin, |
109 | | - data=din_pin |
110 | | - ) |
111 | | - |
112 | | - print("TLV320 I2S DAC initialized successfully") |
| 120 | + if ltv320_present: |
| 121 | + self.tlv = adafruit_tlv320.TLV320DAC3100(i2c) |
| 122 | + |
| 123 | + # set sample rate & bit depth |
| 124 | + self.tlv.configure_clocks(sample_rate=11025, bit_depth=16) |
| 125 | + |
| 126 | + if "sound" in launcher_config: |
| 127 | + if launcher_config["sound"] == "speaker": |
| 128 | + # use speaker |
| 129 | + self.tlv.speaker_output = True |
| 130 | + self.tlv.speaker_volume = -60 |
| 131 | + else: |
| 132 | + # use headphones |
| 133 | + self.tlv.headphone_output = True |
| 134 | + self.tlv.headphone_volume = -15 # dB |
| 135 | + else: |
| 136 | + # default to headphones |
| 137 | + self.tlv.headphone_output = True |
| 138 | + self.tlv.headphone_volume = -15 # dB |
| 139 | + |
| 140 | + # Setup I2S audio output - important to do this AFTER configuring the DAC |
| 141 | + self.audio = audiobusio.I2SOut( |
| 142 | + bit_clock=bclck_pin, |
| 143 | + word_select=wsel_pin, |
| 144 | + data=din_pin |
| 145 | + ) |
| 146 | + |
| 147 | + print("TLV320 I2S DAC initialized successfully") |
| 148 | + |
| 149 | + else: |
| 150 | + print("TLV320 DAC not found, falling back to PWM audio output") |
| 151 | + self.audio = audiopwmio.PWMAudioOut(board.D10) |
| 152 | + |
113 | 153 | except Exception as e: |
114 | 154 | print(f"Error initializing TLV320 DAC: {e}") |
115 | 155 | print("Falling back to PWM audio output") |
|
0 commit comments