-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhothouse.cpp
More file actions
278 lines (228 loc) · 8.67 KB
/
Copy pathhothouse.cpp
File metadata and controls
278 lines (228 loc) · 8.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Hardware proxy for Hothouse DIY DSP Platform
// Copyright (C) 2024 Cleveland Music Co. <code@clevelandmusicco.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "hothouse.h"
#include "optional"
using clevelandmusicco::Hothouse;
using daisy::System;
#ifndef SAMPLE_RATE
// #define SAMPLE_RATE DSY_AUDIO_SAMPLE_RATE
#define SAMPLE_RATE 48014.f
#endif
// Hardware related defines.
// Switches
constexpr Pin PIN_SW_1_UP = daisy::seed::D9;
constexpr Pin PIN_SW_1_DOWN = daisy::seed::D10;
constexpr Pin PIN_SW_2_UP = daisy::seed::D7;
constexpr Pin PIN_SW_2_DOWN = daisy::seed::D8;
constexpr Pin PIN_SW_3_UP = daisy::seed::D5;
constexpr Pin PIN_SW_3_DOWN = daisy::seed::D6;
constexpr Pin PIN_FSW_1 = daisy::seed::D25;
constexpr Pin PIN_FSW_2 = daisy::seed::D26;
// Knobs
constexpr Pin PIN_KNOB_1 = daisy::seed::D16;
constexpr Pin PIN_KNOB_2 = daisy::seed::D17;
constexpr Pin PIN_KNOB_3 = daisy::seed::D18;
constexpr Pin PIN_KNOB_4 = daisy::seed::D19;
constexpr Pin PIN_KNOB_5 = daisy::seed::D20;
constexpr Pin PIN_KNOB_6 = daisy::seed::D21;
const uint32_t Hothouse::HOLD_THRESHOLD_MS;
void Hothouse::Init(bool boost) {
// Initialize the hardware.
seed.Configure();
seed.Init(boost);
InitSwitches();
InitAnalogControls();
SetAudioBlockSize(48);
}
void Hothouse::DelayMs(size_t del) { seed.DelayMs(del); }
void Hothouse::SetHidUpdateRates() {
for (size_t i = 0; i < KNOB_LAST; i++) {
knobs[i].SetSampleRate(AudioCallbackRate());
}
}
void Hothouse::StartAudio(AudioHandle::InterleavingAudioCallback cb) {
seed.StartAudio(cb);
}
void Hothouse::StartAudio(AudioHandle::AudioCallback cb) {
seed.StartAudio(cb);
}
void Hothouse::ChangeAudioCallback(AudioHandle::InterleavingAudioCallback cb) {
seed.ChangeAudioCallback(cb);
}
void Hothouse::ChangeAudioCallback(AudioHandle::AudioCallback cb) {
seed.ChangeAudioCallback(cb);
}
void Hothouse::StopAudio() { seed.StopAudio(); }
void Hothouse::SetAudioBlockSize(size_t size) {
seed.SetAudioBlockSize(size);
SetHidUpdateRates();
}
size_t Hothouse::AudioBlockSize() { return seed.AudioBlockSize(); }
void Hothouse::SetAudioSampleRate(SaiHandle::Config::SampleRate samplerate) {
seed.SetAudioSampleRate(samplerate);
SetHidUpdateRates();
}
float Hothouse::AudioSampleRate() { return seed.AudioSampleRate(); }
float Hothouse::AudioCallbackRate() { return seed.AudioCallbackRate(); }
void Hothouse::StartAdc() { seed.adc.Start(); }
void Hothouse::StopAdc() { seed.adc.Stop(); }
void Hothouse::ProcessAnalogControls() {
for (size_t i = 0; i < KNOB_LAST; i++) {
knobs[i].Process();
}
}
float Hothouse::GetKnobValue(Knob k) {
size_t idx;
idx = k < KNOB_LAST ? k : KNOB_1;
return knobs[idx].Value();
}
void Hothouse::ProcessDigitalControls() {
for (size_t i = 0; i < SWITCH_LAST; i++) {
switches[i].Debounce();
}
ProcessFootswitchPresses(FOOTSWITCH_1);
ProcessFootswitchPresses(FOOTSWITCH_2);
}
void Hothouse::InitSwitches() {
constexpr Pin pin_numbers[SWITCH_LAST] = {
PIN_SW_1_UP, PIN_SW_1_DOWN, PIN_SW_2_UP, PIN_SW_2_DOWN,
PIN_SW_3_UP, PIN_SW_3_DOWN, PIN_FSW_1, PIN_FSW_2,
};
for (size_t i = 0; i < SWITCH_LAST; i++) {
switches[i].Init(pin_numbers[i]);
}
}
void Hothouse::InitAnalogControls() {
constexpr Pin knob_pins[KNOB_LAST] = {PIN_KNOB_1, PIN_KNOB_2, PIN_KNOB_3,
PIN_KNOB_4, PIN_KNOB_5, PIN_KNOB_6};
// Set order of ADCs based on CHANNEL NUMBER
AdcChannelConfig cfg[KNOB_LAST];
// Initialize ADC configuration with Single Pins
for (size_t i = 0; i < KNOB_LAST; ++i) {
cfg[i].InitSingle(knob_pins[i]);
}
// Initialize ADC with configuration
seed.adc.Init(cfg, KNOB_LAST);
// Get the audio callback rate once
float callback_rate = AudioCallbackRate();
// Initialize knobs with ADC pointers and callback rate
for (size_t i = 0; i < KNOB_LAST; ++i) {
knobs[i].Init(seed.adc.GetPtr(i), callback_rate);
}
}
// Public convenience function to get position of toggleswitches 1-3.
Hothouse::ToggleswitchPosition Hothouse::GetToggleswitchPosition(
Toggleswitch tsw) {
switch (tsw) {
case (TOGGLESWITCH_1):
return GetLogicalSwitchPosition(switches[SWITCH_1_UP],
switches[SWITCH_1_DOWN]);
case (TOGGLESWITCH_2):
return GetLogicalSwitchPosition(switches[SWITCH_2_UP],
switches[SWITCH_2_DOWN]);
case (TOGGLESWITCH_3):
return GetLogicalSwitchPosition(switches[SWITCH_3_UP],
switches[SWITCH_3_DOWN]);
default:
seed.PrintLine(
"ERROR: Unexpected value provided for Toggleswitch 'tsw'. "
"Returning TOGGLESWITCH_UNKNOWN by default.");
return TOGGLESWITCH_UNKNOWN;
}
}
void Hothouse::CheckResetToBootloader() {
if (switches[Hothouse::FOOTSWITCH_1].Pressed()) {
if (footswitch_start_time[0] == 0) {
footswitch_start_time [0]= System::GetNow();
} else if (System::GetNow() - footswitch_start_time[0] >= HOLD_THRESHOLD_MS) {
// Shut 'er down so the LEDs always flash
StopAdc();
StopAudio();
daisy::Led _led_1, _led_2;
_led_1.Init(seed.GetPin(22), false);
_led_2.Init(seed.GetPin(23), false);
// Alternately flash the LEDs 3 times
for (int i = 0; i < 3; i++) {
_led_1.Set(1);
_led_2.Set(0);
_led_1.Update();
_led_2.Update();
System::Delay(100);
_led_1.Set(0);
_led_2.Set(1);
_led_1.Update();
_led_2.Update();
System::Delay(100);
}
// Reset system to bootloader after LED flashing
System::ResetToBootloader();
}
} else {
// Reset the hold timer if the footswitch is released
footswitch_start_time[0] = 0;
}
}
Hothouse::ToggleswitchPosition Hothouse::GetLogicalSwitchPosition(Switch up,
Switch down) {
return up.Pressed()
? TOGGLESWITCH_UP
: (down.Pressed() ? TOGGLESWITCH_DOWN : TOGGLESWITCH_MIDDLE);
}
void Hothouse::RegisterFootswitchCallbacks(FootswitchCallbacks *callbacks) {
footswitchCallbacks = callbacks;
}
// Watches for normal, double, and long presses of the footswitches.
void Hothouse::ProcessFootswitchPresses(Switches footswitch) {
if (footswitchCallbacks == NULL) {
return; // Nothing to do if callbacks have not been registered
}
bool is_pressed = switches[footswitch].RisingEdge();
int footswitch_index = footswitch == Hothouse::FOOTSWITCH_1 ? 0 : 1;
uint32_t now = System::GetNow();
if (is_pressed == true && footswitch_last_state[footswitch_index] == false) {
// Footswitch is pressed
footswitch_start_time[footswitch_index] = now;
if ((now - footswitch_last_press_time[footswitch_index]) <= DOUBLE_PRESS_THRESHOLD_MS) {
footswitch_press_count[footswitch_index]++;
} else {
footswitch_press_count[footswitch_index] = 1;
}
footswitch_last_press_time[footswitch_index] = now;
footswitch_long_press_triggered[footswitch_index] = false; // Reset long press trigger when pressed
}
uint32_t press_duration = now - footswitch_start_time[footswitch_index];
if (switches[footswitch].Pressed() && press_duration >= HOLD_THRESHOLD_MS && !footswitch_long_press_triggered[footswitch_index]) {
// Footswitch is being held down
if (footswitchCallbacks->HandleLongPress != NULL) {
footswitchCallbacks->HandleLongPress(footswitch);
}
footswitch_long_press_triggered[footswitch_index] = true; // Ensure long press is only triggered once
}
if (is_pressed == false && footswitch_last_state[footswitch_index] == true) {
// Button released
if (!footswitch_long_press_triggered[footswitch_index]) {
if (footswitch_press_count[footswitch_index] >= 2) {
if (footswitchCallbacks->HandleDoublePress != NULL) {
footswitchCallbacks->HandleDoublePress(footswitch);
}
footswitch_press_count[footswitch_index] = 0;
} else if (press_duration < HOLD_THRESHOLD_MS && footswitchCallbacks->HandleNormalPress != NULL) {
footswitchCallbacks->HandleNormalPress(footswitch);
}
}
}
footswitch_last_state[footswitch_index] = is_pressed;
}