Skip to content

Commit 81cbbb6

Browse files
committed
Lyrat Mini Button Support
1 parent 9213f4c commit 81cbbb6

File tree

5 files changed

+217
-65
lines changed

5 files changed

+217
-65
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
#include "AudioTools.h"
3+
#include "AudioTools/AudioLibs/AudioBoardStream.h"
4+
5+
AudioBoardStream lyrat(LyratMini);
6+
7+
void rec(bool, int, void*) {
8+
Serial.println("rec");
9+
}
10+
11+
void mode(bool, int, void*) {
12+
Serial.println("mode");
13+
}
14+
15+
void play(bool, int, void*) {
16+
Serial.println("play");
17+
}
18+
19+
void set(bool, int, void*) {
20+
Serial.println("set");
21+
}
22+
23+
void volUp(bool, int, void*) {
24+
Serial.println("vol+");
25+
}
26+
27+
void volDown(bool, int, void*) {
28+
Serial.println("vol-");
29+
}
30+
31+
32+
// Arduino Setup
33+
void setup(void) {
34+
Serial.begin(115200);
35+
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Warning);
36+
37+
// start board
38+
lyrat.begin(lyrat.defaultConfig(TX_MODE));
39+
40+
lyrat.addAction(KEY_REC, rec);
41+
lyrat.addAction(KEY_MODE, mode);
42+
lyrat.addAction(KEY_PLAY, play);
43+
lyrat.addAction(KEY_SET, set);
44+
lyrat.addAction(KEY_VOLUME_UP, volUp);
45+
lyrat.addAction(KEY_VOLUME_DOWN, volDown);
46+
47+
}
48+
49+
void loop() {
50+
lyrat.processActions();
51+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
3+
#include "AudioTools.h"
4+
#include "AudioTools/AudioLibs/AudioBoardStream.h"
5+
6+
AudioInfo info(44100, 1, 16);
7+
AudioBoardStream i2s(LyratMini); // Access I2S as stream
8+
CsvOutput<int16_t> csvStream(Serial);
9+
StreamCopy copier(csvStream, i2s); // copy i2s to csvStream
10+
11+
// Arduino Setup
12+
void setup(void) {
13+
Serial.begin(115200);
14+
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Warning);
15+
16+
auto cfg = i2s.defaultConfig(RX_MODE);
17+
cfg.copyFrom(info);
18+
//cfg.input_device = ADC_INPUT_LINE2;
19+
i2s.begin(cfg);
20+
21+
// make sure that we have the correct number of channels set up
22+
csvStream.begin(info);
23+
24+
}
25+
26+
// Arduino loop - copy data
27+
void loop() {
28+
copier.copy();
29+
}

examples/examples-custom-boards/lyrat-mini/output/outout.ino renamed to examples/examples-custom-boards/lyrat-mini/output/output.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ void setup(void) {
2020
Serial.println("starting I2S...");
2121
auto config = out.defaultConfig(TX_MODE);
2222
config.copyFrom(info);
23+
out.begin(config);
2324

2425
// additinal settings
2526
out.setVolume(0.5);

src/AudioTools/AudioLibs/AudioBoardStream.h

Lines changed: 69 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,18 @@ namespace audio_tools {
1717
* @copyright GPLv3
1818
*/
1919
class AudioBoardStream : public I2SCodecStream {
20-
public:
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:
2132
/**
2233
* @brief Default constructor: for available AudioBoard values check
2334
* the audioboard variables in
@@ -44,6 +55,32 @@ class AudioBoardStream : public I2SCodecStream {
4455
yield();
4556
}
4657

58+
59+
/**
60+
* @brief Defines a new action that is executed when the Button is pressed
61+
*/
62+
void addAction(AudioDriverKey key, void (*action)(bool, int, void *),
63+
void *ref = nullptr) {
64+
AudioBoardAction *abo = new AudioBoardAction(board(), key);
65+
abo->actionOn = action;
66+
abo->ref = (ref == nullptr) ? this : ref;
67+
actions.add(*abo);
68+
}
69+
70+
/**
71+
* @brief Defines a new action that is executed when the Button is pressed and released
72+
*/
73+
void addAction(AudioDriverKey key, void (*actionOn)(bool, int, void *),
74+
void (*actionOff)(bool, int, void *),
75+
void *ref = nullptr) {
76+
77+
AudioBoardAction *abo = new AudioBoardAction(board(), key);
78+
abo->actionOn = actionOn;
79+
abo->actionOn = actionOff;
80+
abo->ref = (ref == nullptr) ? this : ref;
81+
actions.add(*abo);
82+
}
83+
4784
/**
4885
* @brief Defines a new action that is executed when the indicated pin is
4986
* active
@@ -72,12 +109,12 @@ class AudioBoardStream : public I2SCodecStream {
72109
void addAction(int pin, void (*action)(bool, int, void *),
73110
AudioActions::ActiveLogic activeLogic, void *ref = nullptr) {
74111
TRACEI();
75-
actions.add(pin, action, activeLogic, ref == nullptr ? this : ref);
112+
actions.add(pin, action, activeLogic, ref == nullptr ? this : ref);
76113
}
77114

78115
/// Provides access to the AudioActions
79116
AudioActions &audioActions() { return actions; }
80-
117+
81118
AudioActions &getActions() { return actions; }
82119

83120
/**
@@ -98,7 +135,7 @@ class AudioBoardStream : public I2SCodecStream {
98135
*/
99136
static void actionVolumeUp(bool, int, void *ref) {
100137
TRACEI();
101-
AudioBoardStream *self = (AudioBoardStream*)ref;
138+
AudioBoardStream *self = (AudioBoardStream *)ref;
102139
self->incrementVolume(+self->actionVolumeIncrementValue());
103140
}
104141

@@ -108,18 +145,17 @@ class AudioBoardStream : public I2SCodecStream {
108145
*/
109146
static void actionVolumeDown(bool, int, void *ref) {
110147
TRACEI();
111-
AudioBoardStream *self = (AudioBoardStream*)ref;
148+
AudioBoardStream *self = (AudioBoardStream *)ref;
112149
self->incrementVolume(-self->actionVolumeIncrementValue());
113150
}
114151

115-
116152
/**
117153
* @brief Toggle start stop
118154
*
119155
*/
120156
static void actionStartStop(bool, int, void *ref) {
121157
TRACEI();
122-
AudioBoardStream *self = (AudioBoardStream*)ref;
158+
AudioBoardStream *self = (AudioBoardStream *)ref;
123159
self->active = !self->active;
124160
self->setActive(self->active);
125161
}
@@ -130,7 +166,7 @@ class AudioBoardStream : public I2SCodecStream {
130166
*/
131167
static void actionStart(bool, int, void *ref) {
132168
TRACEI();
133-
AudioBoardStream *self = (AudioBoardStream*)ref;
169+
AudioBoardStream *self = (AudioBoardStream *)ref;
134170
self->active = true;
135171
self->setActive(self->active);
136172
}
@@ -140,7 +176,7 @@ class AudioBoardStream : public I2SCodecStream {
140176
*/
141177
static void actionStop(bool, int, void *ref) {
142178
TRACEI();
143-
AudioBoardStream *self = (AudioBoardStream*)ref;
179+
AudioBoardStream *self = (AudioBoardStream *)ref;
144180
self->active = false;
145181
self->setActive(self->active);
146182
}
@@ -151,9 +187,8 @@ class AudioBoardStream : public I2SCodecStream {
151187
* This method complies with the
152188
*/
153189
static void actionHeadphoneDetection(bool, int, void *ref) {
154-
AudioBoardStream *self = (AudioBoardStream*)ref;
190+
AudioBoardStream *self = (AudioBoardStream *)ref;
155191
if (self->pinHeadphoneDetect() >= 0) {
156-
157192
// detect changes
158193
bool isConnected = self->headphoneStatus();
159194
if (self->headphoneIsConnected != isConnected) {
@@ -289,7 +324,7 @@ class AudioBoardStream : public I2SCodecStream {
289324
addAction(input_mode, actionStartStop);
290325
}
291326
}
292-
327+
293328
/// add volume up and volume down action
294329
void addVolumeActions() {
295330
// pin conflicts with SD Lyrat SD CS GpioPin and buttons / Conflict on
@@ -319,8 +354,9 @@ class AudioBoardStream : public I2SCodecStream {
319354
}
320355

321356
/**
322-
* @brief Setup the supported default actions (volume, start/stop, headphone detection)
323-
*/
357+
* @brief Setup the supported default actions (volume, start/stop, headphone
358+
* detection)
359+
*/
324360
void addDefaultActions() {
325361
TRACEI();
326362
addHeadphoneDetectionAction();
@@ -329,15 +365,18 @@ class AudioBoardStream : public I2SCodecStream {
329365
}
330366

331367
/// Defines the increment value used by actionVolumeDown/actionVolumeUp
332-
void setActionVolumeIncrementValue(float value){
368+
void setActionVolumeIncrementValue(float value) {
333369
action_increment_value = value;
334370
}
335371

336-
float actionVolumeIncrementValue() {
337-
return action_increment_value;
372+
float actionVolumeIncrementValue() { return action_increment_value; }
373+
374+
bool isKeyPressed(int key) {
375+
if (!board()) return false;
376+
return board().isKeyPressed(key);
338377
}
339378

340-
protected:
379+
protected:
341380
AudioActions actions;
342381
bool headphoneIsConnected = false;
343382
bool active = true;
@@ -346,8 +385,7 @@ class AudioBoardStream : public I2SCodecStream {
346385
int getSdCsPin() {
347386
static GpioPin sd_cs = -2;
348387
// execute only once
349-
if (sd_cs != -2)
350-
return sd_cs;
388+
if (sd_cs != -2) return sd_cs;
351389

352390
auto sd_opt = getPins().getSPIPins(PinFunction::SD);
353391
if (sd_opt) {
@@ -365,20 +403,19 @@ class AudioBoardStream : public I2SCodecStream {
365403
AudioActions::ActiveLogic getActionLogic(int pin) {
366404
auto opt = board().getPins().getPin(pin);
367405
PinLogic logic = PinLogic::Input;
368-
if (opt)
369-
logic = opt.value().pin_logic;
406+
if (opt) logic = opt.value().pin_logic;
370407
switch (logic) {
371-
case PinLogic::Input:
372-
case PinLogic::InputActiveLow:
373-
return AudioActions::ActiveLow;
374-
case PinLogic::InputActiveHigh:
375-
return AudioActions::ActiveHigh;
376-
case PinLogic::InputActiveTouch:
377-
return AudioActions::ActiveTouch;
378-
default:
379-
return AudioActions::ActiveLow;
408+
case PinLogic::Input:
409+
case PinLogic::InputActiveLow:
410+
return AudioActions::ActiveLow;
411+
case PinLogic::InputActiveHigh:
412+
return AudioActions::ActiveHigh;
413+
case PinLogic::InputActiveTouch:
414+
return AudioActions::ActiveTouch;
415+
default:
416+
return AudioActions::ActiveLow;
380417
}
381418
}
382419
};
383420

384-
} // namespace audio_tools
421+
} // namespace audio_tools

0 commit comments

Comments
 (0)