arduino-audio-tools
Loading...
Searching...
No Matches
AudioBoardStream.h
1#pragma once
2
3#include "AudioToolsConfig.h"
4#include "AudioTools/AudioLibs/I2SCodecStream.h"
5#include "AudioTools/CoreAudio/AudioActions.h"
6
7namespace audio_tools {
8
20 struct AudioBoardAction : public AudioActions::Action {
21 AudioBoardAction(AudioBoard &board, AudioDriverKey key) {
22 this->key = key;
23 this->p_board = &board;
24 }
25 AudioDriverKey key;
26 AudioBoard *p_board;
27 int id() override { return key | 0x400; }
28 bool readValue() override { return p_board->isKeyPressed(key); }
29 };
30
31 public:
39 AudioBoardStream(audio_driver::AudioBoard &board) : I2SCodecStream(board) {
40 // pin mode already set up by driver library
41 actions.setPinMode(false);
42 // use the AudioBoard
43 actions.setReadCallback([this](int pin) { return this->digitalRead(pin); });
44 }
45
46 bool begin() override { return I2SCodecStream::begin(); }
47
48 bool begin(I2SCodecConfig cfg) override { return I2SCodecStream::begin(cfg); }
49
55 // TRACED();
56 actions.processActions();
57 delay(1);
58 }
59
60
64 void addAction(AudioDriverKey key, void (*action)(bool, int, void *),
65 void *ref = nullptr) {
66 AudioBoardAction *abo = new AudioBoardAction(board(), key);
67 abo->actionOn = action;
68 abo->ref = (ref == nullptr) ? this : ref;
69 actions.add(*abo);
70 }
71
75 void addAction(AudioDriverKey key, void (*actionOn)(bool, int, void *),
76 void (*actionOff)(bool, int, void *),
77 void *ref = nullptr) {
78
79 AudioBoardAction *abo = new AudioBoardAction(board(), key);
80 abo->actionOn = actionOn;
81 abo->actionOn = actionOff;
82 abo->ref = (ref == nullptr) ? this : ref;
83 actions.add(*abo);
84 }
85
94 void addAction(int pin, void (*action)(bool, int, void *),
95 void *ref = nullptr) {
96 TRACEI();
97 // determine logic from config
98 AudioActions::ActiveLogic activeLogic = getActionLogic(pin);
99 actions.add(pin, action, activeLogic, ref == nullptr ? this : ref);
100 }
101
111 void addAction(int pin, void (*action)(bool, int, void *),
112 AudioActions::ActiveLogic activeLogic, void *ref = nullptr) {
113 TRACEI();
114 actions.add(pin, action, activeLogic, ref == nullptr ? this : ref);
115 }
116
118 AudioActions &audioActions() { return actions; }
119
120 AudioActions &getActions() { return actions; }
121
127 void incrementVolume(float inc) {
128 float current_volume = getVolume();
129 float new_volume = current_volume + inc;
130 LOGI("incrementVolume: %f -> %f", current_volume, new_volume);
131 setVolume(new_volume);
132 }
133
138 static void actionVolumeUp(bool, int, void *ref) {
139 TRACEI();
140 AudioBoardStream *self = (AudioBoardStream *)ref;
141 self->incrementVolume(+self->actionVolumeIncrementValue());
142 }
143
148 static void actionVolumeDown(bool, int, void *ref) {
149 TRACEI();
150 AudioBoardStream *self = (AudioBoardStream *)ref;
151 self->incrementVolume(-self->actionVolumeIncrementValue());
152 }
153
158 static void actionStartStop(bool, int, void *ref) {
159 TRACEI();
160 AudioBoardStream *self = (AudioBoardStream *)ref;
161 self->active = !self->active;
162 self->setActive(self->active);
163 }
164
169 static void actionStart(bool, int, void *ref) {
170 TRACEI();
171 AudioBoardStream *self = (AudioBoardStream *)ref;
172 self->active = true;
173 self->setActive(self->active);
174 }
175
179 static void actionStop(bool, int, void *ref) {
180 TRACEI();
181 AudioBoardStream *self = (AudioBoardStream *)ref;
182 self->active = false;
183 self->setActive(self->active);
184 }
185
191 static void actionHeadphoneDetection(bool, int, void *ref) {
192 AudioBoardStream *self = (AudioBoardStream *)ref;
193 if (self->pinHeadphoneDetect() >= 0) {
194 // detect changes
195 bool isConnected = self->headphoneStatus();
196 if (self->headphoneIsConnected != isConnected) {
197 self->headphoneIsConnected = isConnected;
198
199 // update if things have stabilized
200 bool powerActive = !isConnected;
201 LOGW("Headphone jack has been %s",
202 isConnected ? "inserted" : "removed");
203 self->setSpeakerActive(powerActive);
204 }
205 }
206 delay(1);
207 }
208
215 GpioPin pinAuxin() { return getPinID(PinFunction::AUXIN_DETECT); }
216
224 return getPinID(PinFunction::HEADPHONE_DETECT);
225 }
226
233 GpioPin pinPaEnable() { return getPinID(PinFunction::PA); }
234
235 // /**
236 // * @brief Get the gpio number for adc detection
237 // *
238 // * @return -1 non-existent
239 // * Others gpio number
240 // */
241 // GpioPin pinAdcDetect() { return getPin(AUXIN_DETECT); }
242
249 GpioPin pinInputRec() { return getPinID(PinFunction::KEY, 1); }
250
257 GpioPin pinInputMode() { return getPinID(PinFunction::KEY, 2); }
258
265 GpioPin pinInputSet() { return getPinID(PinFunction::KEY, 4); }
266
273 GpioPin pinInputPlay() { return getPinID(PinFunction::KEY, 3); }
274
281 GpioPin pinVolumeUp() { return getPinID(PinFunction::KEY, 6); }
282
289 GpioPin pinVolumeDown() { return getPinID(PinFunction::KEY, 5); }
290
297 GpioPin pinLed(int idx) { return getPinID(PinFunction::LED, idx); }
298
300 void setSpeakerActive(bool active) { setPAPower(active); }
301
309 int headphoneGpioPin = pinHeadphoneDetect();
310 return headphoneGpioPin > 0 ? !digitalRead(headphoneGpioPin) : false;
311 }
312
316 void setActive(bool active) { setMute(!active); }
317
320 // pin conflicts for pinInputMode() with the SD CS pin for AIThinker and
321 // buttons
322 int sd_cs = getSdCsPin();
323 int input_mode = pinInputMode();
324 if (input_mode != -1 && (input_mode != sd_cs || !cfg.sd_active)) {
325 LOGD("actionInputMode")
326 addAction(input_mode, actionStartStop);
327 }
328 }
329
332 // pin conflicts with SD Lyrat SD CS GpioPin and buttons / Conflict on
333 // Audiokit V. 2957
334 int sd_cs = getSdCsPin();
335 int vol_up = pinVolumeUp();
336 int vol_down = pinVolumeDown();
337 if ((vol_up != -1 && vol_down != -1) &&
338 (!cfg.sd_active || (vol_down != sd_cs && vol_up != sd_cs))) {
339 LOGD("actionVolumeDown")
340 addAction(vol_down, actionVolumeDown);
341 LOGD("actionVolumeUp")
342 addAction(vol_up, actionVolumeUp);
343 } else {
344 LOGW("Volume Buttons ignored because of conflict: %d ", pinVolumeDown());
345 }
346 }
347
350 // pin conflicts with AIThinker A101: key6 and headphone detection
351 int head_phone = pinHeadphoneDetect();
352 if (head_phone != -1 && (getPinID(PinFunction::KEY, 6) != head_phone)) {
353 actions.add(head_phone, actionHeadphoneDetection,
354 AudioActions::ActiveChange, this);
355 }
356 }
357
363 TRACEI();
367 }
368
371 action_increment_value = value;
372 }
373
374 float actionVolumeIncrementValue() { return action_increment_value; }
375
376 bool isKeyPressed(int key) {
377 if (!board()) return false;
378 return board().isKeyPressed(key);
379 }
380
381 protected:
382 AudioActions actions;
383 bool headphoneIsConnected = false;
384 bool active = true;
385 float action_increment_value = 0.02;
386
387 int getSdCsPin() {
388 static GpioPin sd_cs = -2;
389 // execute only once
390 if (sd_cs != -2) return sd_cs;
391
392 auto sd_opt = getPins().getSPIPins(PinFunction::SD);
393 if (sd_opt) {
394 sd_cs = sd_opt.value().cs;
395 } else {
396 // no spi -> no sd
397 LOGI("No sd defined -> sd_active=false")
398 cfg.sd_active = false;
399 sd_cs = -1;
400 }
401 return sd_cs;
402 }
403
405 AudioActions::ActiveLogic getActionLogic(int pin) {
406 auto opt = board().getPins().getPin(pin);
407 PinLogic logic = PinLogic::Input;
408 if (opt) logic = opt.value().pin_logic;
409 switch (logic) {
410 case PinLogic::Input:
411 case PinLogic::InputActiveLow:
412 return AudioActions::ActiveLow;
413 case PinLogic::InputActiveHigh:
414 return AudioActions::ActiveHigh;
415 case PinLogic::InputActiveTouch:
416 return AudioActions::ActiveTouch;
417 default:
418 return AudioActions::ActiveLow;
419 }
420 }
421};
422
423} // namespace audio_tools
A simple class to assign functions to gpio pins e.g. to implement a simple navigation control or volu...
Definition AudioActions.h:29
void setReadCallback(std::function< bool(int)> read_cb_par)
Sets a callback function to read the pin state.
Definition AudioActions.h:242
void processActions()
Execute all actions if the corresponding pin is low To minimize the runtime: With each call we proces...
Definition AudioActions.h:185
void add(Action &action)
Adds an Action.
Definition AudioActions.h:139
void setPinMode(bool active)
setup pin mode when true
Definition AudioActions.h:232
New functionality which replaces the AudioKitStream that is based on the legacy AudioKit library....
Definition AudioBoardStream.h:19
void addAction(AudioDriverKey key, void(*actionOn)(bool, int, void *), void(*actionOff)(bool, int, void *), void *ref=nullptr)
Defines a new action that is executed when the Button is pressed and released.
Definition AudioBoardStream.h:75
GpioPin pinInputRec()
Get the record-button id for adc-button.
Definition AudioBoardStream.h:249
void addAction(int pin, void(*action)(bool, int, void *), void *ref=nullptr)
Defines a new action that is executed when the indicated pin is active.
Definition AudioBoardStream.h:94
GpioPin pinVolumeDown()
Get number for volume down function.
Definition AudioBoardStream.h:289
static void actionVolumeUp(bool, int, void *ref)
Increase the volume.
Definition AudioBoardStream.h:138
void setSpeakerActive(bool active)
the same as setPAPower()
Definition AudioBoardStream.h:300
static void actionHeadphoneDetection(bool, int, void *ref)
Switch off the PA if the headphone in plugged in and switch it on again if the headphone is unplugged...
Definition AudioBoardStream.h:191
static void actionVolumeDown(bool, int, void *ref)
Decrease the volume.
Definition AudioBoardStream.h:148
void addAction(int pin, void(*action)(bool, int, void *), AudioActions::ActiveLogic activeLogic, void *ref=nullptr)
Defines a new action that is executed when the indicated pin is active.
Definition AudioBoardStream.h:111
void addAction(AudioDriverKey key, void(*action)(bool, int, void *), void *ref=nullptr)
Defines a new action that is executed when the Button is pressed.
Definition AudioBoardStream.h:64
GpioPin pinLed(int idx)
Get LED pin.
Definition AudioBoardStream.h:297
GpioPin pinAuxin()
Get the gpio number for auxin detection.
Definition AudioBoardStream.h:215
AudioActions::ActiveLogic getActionLogic(int pin)
Determines the action logic (ActiveLow or ActiveTouch) for the pin.
Definition AudioBoardStream.h:405
static void actionStartStop(bool, int, void *ref)
Toggle start stop.
Definition AudioBoardStream.h:158
GpioPin pinPaEnable()
Get the gpio number for PA enable.
Definition AudioBoardStream.h:233
GpioPin pinInputMode()
Get the number for mode-button.
Definition AudioBoardStream.h:257
AudioActions & audioActions()
Provides access to the AudioActions.
Definition AudioBoardStream.h:118
GpioPin pinInputPlay()
Get number for play function.
Definition AudioBoardStream.h:273
GpioPin pinHeadphoneDetect()
Get the gpio number for headphone detection.
Definition AudioBoardStream.h:223
GpioPin pinInputSet()
Get number for set function.
Definition AudioBoardStream.h:265
bool begin(I2SCodecConfig cfg) override
Starts the I2S interface.
Definition AudioBoardStream.h:48
void addHeadphoneDetectionAction()
Adds headphone determination.
Definition AudioBoardStream.h:349
void processActions()
Process input keys and pins.
Definition AudioBoardStream.h:54
void setActionVolumeIncrementValue(float value)
Defines the increment value used by actionVolumeDown/actionVolumeUp.
Definition AudioBoardStream.h:370
void addDefaultActions()
Setup the supported default actions (volume, start/stop, headphone detection)
Definition AudioBoardStream.h:362
void incrementVolume(float inc)
Relative volume control.
Definition AudioBoardStream.h:127
AudioBoardStream(audio_driver::AudioBoard &board)
Default constructor: for available AudioBoard values check the audioboard variables in https://pschat...
Definition AudioBoardStream.h:39
void setActive(bool active)
The oposite of setMute(): setActive(true) calls setMute(false)
Definition AudioBoardStream.h:316
void addVolumeActions()
add volume up and volume down action
Definition AudioBoardStream.h:331
void addStartStopAction()
add start/stop on inputMode
Definition AudioBoardStream.h:319
GpioPin pinVolumeUp()
number for volume up function
Definition AudioBoardStream.h:281
bool headphoneStatus()
Returns true if the headphone was detected.
Definition AudioBoardStream.h:308
static void actionStart(bool, int, void *ref)
Start.
Definition AudioBoardStream.h:169
static void actionStop(bool, int, void *ref)
Stop.
Definition AudioBoardStream.h:179
I2S Stream which also sets up a codec chip and i2s.
Definition I2SCodecStream.h:46
DriverPins & getPins()
Provides access to the pin information.
Definition I2SCodecStream.h:204
AudioBoard & board()
Provides the board.
Definition I2SCodecStream.h:180
bool setMute(bool mute)
Mute / unmote.
Definition I2SCodecStream.h:157
float getVolume()
legacy: same as volume()
Definition I2SCodecStream.h:154
bool digitalRead(int pin)
get value of digital pin
Definition I2SCodecStream.h:215
bool setPAPower(bool active)
Sets the output of the PA Power Pin.
Definition I2SCodecStream.h:168
bool setVolume(float vol) override
sets the volume (range 0.0f - 1.0f)
Definition I2SCodecStream.h:142
GpioPin getPinID(PinFunction function)
Provides the gpio for the indicated function.
Definition I2SCodecStream.h:189
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
Definition AudioActions.h:38
Configuration for I2SCodecStream.
Definition I2SCodecStream.h:24