|
6 | 6 | #include "Utils/Optional.h" |
7 | 7 | #include "Utils/Vector.h" |
8 | 8 |
|
| 9 | +#ifndef TOUCH_LIMIT |
| 10 | +# define TOUCH_LIMIT 20 |
| 11 | +#endif |
| 12 | + |
9 | 13 | namespace audio_driver { |
10 | 14 |
|
11 | 15 | /** @file */ |
@@ -48,6 +52,22 @@ enum class PinFunction { |
48 | 52 | MCLK_SOURCE, |
49 | 53 | }; |
50 | 54 |
|
| 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 | + |
51 | 71 | /** |
52 | 72 | * @brief I2S pins |
53 | 73 | * @author Phil Schatzmann |
@@ -405,6 +425,15 @@ class DriverPins { |
405 | 425 | /// Returns true if some function pins have been defined |
406 | 426 | bool hasPins() { return !pins.empty(); } |
407 | 427 |
|
| 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 | + |
408 | 437 | protected: |
409 | 438 | audio_driver_local::Vector<PinsI2S> i2s{0}; |
410 | 439 | audio_driver_local::Vector<PinsSPI> spi{0}; |
@@ -488,12 +517,41 @@ class DriverPins { |
488 | 517 | } |
489 | 518 | }; |
490 | 519 |
|
| 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 | + |
491 | 549 | /** |
492 | 550 | * @brief Pins for Lyrat 4.3 - use the PinsLyrat43 object! |
493 | 551 | * @author Phil Schatzmann |
494 | 552 | * @copyright GPLv3 |
495 | 553 | */ |
496 | | -class PinsLyrat43Class : public DriverPins { |
| 554 | +class PinsLyrat43Class : public DriverTouchClass { |
497 | 555 | public: |
498 | 556 | PinsLyrat43Class() { |
499 | 557 | // sd pins |
@@ -522,7 +580,7 @@ class PinsLyrat43Class : public DriverPins { |
522 | 580 | * @author Phil Schatzmann |
523 | 581 | * @copyright GPLv3 |
524 | 582 | */ |
525 | | -class PinsLyrat42Class : public DriverPins { |
| 583 | +class PinsLyrat42Class : public DriverTouchClass { |
526 | 584 | public: |
527 | 585 | PinsLyrat42Class() { |
528 | 586 | // sd pins |
@@ -568,7 +626,28 @@ class PinsLyratMiniClass : public DriverPins { |
568 | 626 | addPin(PinFunction::LED, 22, PinLogic::Output, 1); |
569 | 627 | addPin(PinFunction::LED, 27, PinLogic::Output, 2); |
570 | 628 | addPin(PinFunction::MCLK_SOURCE, 0, PinLogic::Inactive); |
| 629 | + addPin(PinFunction::KEY, 39, PinLogic::Input, 0); |
571 | 630 | } |
| 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 | + |
572 | 651 | }; |
573 | 652 |
|
574 | 653 | /** |
|
0 commit comments