Skip to content

Commit a15d563

Browse files
committed
support for isKeyPressed()
1 parent ae84e5c commit a15d563

File tree

3 files changed

+102
-7
lines changed

3 files changed

+102
-7
lines changed

src/AudioBoard.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ class AudioBoard {
9393
return *p_driver;
9494
}
9595

96+
// platform specific logic to determine if key is pressed
97+
bool isKeyPressed(uint8_t key) { return p_pins->isKeyPressed(key); }
98+
9699
operator bool() { return is_active && p_driver != nullptr && p_pins != nullptr;}
97100

98101
protected:
@@ -104,6 +107,9 @@ class AudioBoard {
104107
bool is_active = false;
105108
};
106109

110+
111+
112+
107113
// -- Boards
108114
/// @ingroup audio_driver
109115
static AudioBoard AudioKitEs8388V1{AudioDriverES8388, PinsAudioKitEs8388v1};

src/Driver.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,15 +1417,25 @@ class AudioDriverLyratMiniClass : public AudioDriver {
14171417
AD_LOGI("AudioDriverLyratMiniClass::begin");
14181418
p_pins = &pins;
14191419
codec_cfg = codecCfg;
1420+
1421+
// setup SPI for SD
1422+
pins.setSPIActiveForSD(codecCfg.sd_active);
1423+
1424+
// Start ADC
14201425
bool ok = true;
1421-
if (codecCfg.output_device != DAC_OUTPUT_NONE){
1422-
AD_LOGI("starting DAC");
1423-
ok = dac.begin(codecCfg, pins);
1424-
}
14251426
if (codecCfg.input_device != ADC_INPUT_NONE){
14261427
AD_LOGI("starting ADC");
1427-
ok = ok && adc.begin(codecCfg, pins);
1428+
ok = ok && adc.setConfig(codecCfg);
14281429
}
1430+
1431+
// Start DAC
1432+
if (codecCfg.output_device != DAC_OUTPUT_NONE){
1433+
AD_LOGI("starting DAC");
1434+
ok = dac.setConfig(codecCfg);
1435+
adc.setPAPower(true);
1436+
setVolume(DRIVER_DEFAULT_VOLUME);
1437+
}
1438+
14291439
if (!ok) {
14301440
AD_LOGI("AudioDriverLyratMiniClass::begin failed");
14311441
}

src/DriverPins.h

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
#include "Utils/Optional.h"
77
#include "Utils/Vector.h"
88

9+
#ifndef TOUCH_LIMIT
10+
# define TOUCH_LIMIT 20
11+
#endif
12+
913
namespace audio_driver {
1014

1115
/** @file */
@@ -48,6 +52,22 @@ enum class PinFunction {
4852
MCLK_SOURCE,
4953
};
5054

55+
56+
/**
57+
* @enum AudioDriverKey
58+
* @brief Key names
59+
* @ingroup enumerations
60+
* @ingroup audio_driver
61+
*/
62+
enum AudioDriverKeys {
63+
KEY_REC = 0,
64+
KEY_MODE,
65+
KEY_PLAY,
66+
KEY_SET,
67+
KEY_VOLUME_DOWN,
68+
KEY_VOLUME_UP
69+
};
70+
5171
/**
5272
* @brief I2S pins
5373
* @author Phil Schatzmann
@@ -405,6 +425,15 @@ class DriverPins {
405425
/// Returns true if some function pins have been defined
406426
bool hasPins() { return !pins.empty(); }
407427

428+
/// returns true if pressed
429+
virtual bool isKeyPressed(uint8_t key) {
430+
auto pin_opt = getPin(PinFunction::KEY, key);
431+
if (!pin_opt) return false;
432+
auto pin = pin_opt.value();
433+
bool value = digitalRead(pin.pin);
434+
return pin.pin_logic == PinLogic::InputActiveLow ? !value : value;
435+
}
436+
408437
protected:
409438
audio_driver_local::Vector<PinsI2S> i2s{0};
410439
audio_driver_local::Vector<PinsSPI> spi{0};
@@ -488,12 +517,41 @@ class DriverPins {
488517
}
489518
};
490519

520+
/**
521+
* @brief Support for Touch
522+
* @author Phil Schatzmann
523+
* @copyright GPLv3
524+
*/
525+
class DriverTouchClass : public DriverPins {
526+
#ifdef ESP32
527+
bool isKeyPressed(uint8_t key) override {
528+
auto pin_opt = getPin(PinFunction::KEY, key);
529+
if (!pin_opt) return false;
530+
auto pin = pin_opt.value();
531+
int value = touchRead(pin.pin);
532+
bool result = value <= touch_limit;
533+
if (result) {
534+
// retry to confirm reading
535+
value = touchRead(pin.pin);
536+
result = value <= touch_limit;
537+
}
538+
return result;
539+
}
540+
#endif
541+
void setTouchLimit(int limit){
542+
touch_limit = limit;
543+
}
544+
545+
protected:
546+
int touch_limit = TOUCH_LIMIT;
547+
};
548+
491549
/**
492550
* @brief Pins for Lyrat 4.3 - use the PinsLyrat43 object!
493551
* @author Phil Schatzmann
494552
* @copyright GPLv3
495553
*/
496-
class PinsLyrat43Class : public DriverPins {
554+
class PinsLyrat43Class : public DriverTouchClass {
497555
public:
498556
PinsLyrat43Class() {
499557
// sd pins
@@ -522,7 +580,7 @@ class PinsLyrat43Class : public DriverPins {
522580
* @author Phil Schatzmann
523581
* @copyright GPLv3
524582
*/
525-
class PinsLyrat42Class : public DriverPins {
583+
class PinsLyrat42Class : public DriverTouchClass {
526584
public:
527585
PinsLyrat42Class() {
528586
// sd pins
@@ -568,7 +626,28 @@ class PinsLyratMiniClass : public DriverPins {
568626
addPin(PinFunction::LED, 22, PinLogic::Output, 1);
569627
addPin(PinFunction::LED, 27, PinLogic::Output, 2);
570628
addPin(PinFunction::MCLK_SOURCE, 0, PinLogic::Inactive);
629+
addPin(PinFunction::KEY, 39, PinLogic::Input, 0);
571630
}
631+
632+
bool isKeyPressed(uint8_t key) override {
633+
if (key > 5) return false;
634+
int value = analogRead(39);
635+
return inRange(value, analog_values[key]);
636+
}
637+
638+
void setRange(int value) {
639+
range = value;
640+
}
641+
642+
protected:
643+
// analog values for rec, mute, play, set, vol-, vol+
644+
int analog_values[6] {2802, 2270, 1754, 1284, 827, 304};
645+
int range = 5;
646+
647+
bool inRange(int in, int toBe){
648+
return in >= (toBe-range) && in <= (toBe+range);
649+
}
650+
572651
};
573652

574653
/**

0 commit comments

Comments
 (0)