From e1d49a3632ac130bc5f46b4171202b3ce0a6c1f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Tue, 10 May 2022 11:07:49 +0200 Subject: [PATCH] Buzzer Support for M5Stack --- src/buzz/buzz.cpp | 18 +++++++++++++++ variants/m5stack_core/Speaker.cpp | 33 ++++++++++++++++++++++++++++ variants/m5stack_core/Speaker.h | 30 +++++++++++++++++++++++++ variants/m5stack_core/platformio.ini | 5 ++++- variants/m5stack_core/variant.h | 7 +++--- 5 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 variants/m5stack_core/Speaker.cpp create mode 100644 variants/m5stack_core/Speaker.h diff --git a/src/buzz/buzz.cpp b/src/buzz/buzz.cpp index 279c6484..3253684d 100644 --- a/src/buzz/buzz.cpp +++ b/src/buzz/buzz.cpp @@ -8,8 +8,13 @@ void playBeep(){}; void playStartMelody(){}; void playShutdownMelody(){}; +#else +#ifdef M5STACK +#include "Speaker.h" +TONE Tone; #else #include "Tone.h" +#endif extern "C" void delay(uint32_t dwMs); @@ -38,13 +43,26 @@ const int DURATION_1_4 = 250; // 1/4 note void playTones(const ToneDuration *tone_durations, int size) { for (int i = 0; i < size; i++) { const auto &tone_duration = tone_durations[i]; +#ifdef M5STACK + Tone.tone(tone_duration.frequency_khz); + delay(tone_duration.duration_ms); + Tone.mute(); +#else tone(PIN_BUZZER, tone_duration.frequency_khz, tone_duration.duration_ms); +#endif // to distinguish the notes, set a minimum time between them. delay(1.3 * tone_duration.duration_ms); } } +#ifdef M5STACK +void playBeep() { + ToneDuration melody[] = {{NOTE_B3, DURATION_1_4}}; + playTones(melody, sizeof(melody) / sizeof(ToneDuration)); +} +#else void playBeep() { tone(PIN_BUZZER, NOTE_B3, DURATION_1_4); } +#endif void playStartMelody() { ToneDuration melody[] = {{NOTE_B3, DURATION_1_4}, diff --git a/variants/m5stack_core/Speaker.cpp b/variants/m5stack_core/Speaker.cpp new file mode 100644 index 00000000..0aaff123 --- /dev/null +++ b/variants/m5stack_core/Speaker.cpp @@ -0,0 +1,33 @@ +#include "Speaker.h" + +TONE::TONE(void) { + _volume = 5; + _begun = false; +} + +void TONE::begin() { + _begun = true; + ledcSetup(TONE_PIN_CHANNEL, 0, 13); + ledcAttachPin(PIN_BUZZER, TONE_PIN_CHANNEL); +} + +void TONE::end() { + mute(); + ledcDetachPin(PIN_BUZZER); + _begun = false; +} + +void TONE::tone(uint16_t frequency) { + if(!_begun) begin(); + ledcWriteTone(TONE_PIN_CHANNEL, frequency); + ledcWrite(TONE_PIN_CHANNEL, 0x400 >> _volume); +} + +void TONE::setVolume(uint8_t volume) { + _volume = 11 - volume; +} + +void TONE::mute() { + ledcWriteTone(TONE_PIN_CHANNEL, 0); + digitalWrite(PIN_BUZZER, 0); +} \ No newline at end of file diff --git a/variants/m5stack_core/Speaker.h b/variants/m5stack_core/Speaker.h new file mode 100644 index 00000000..2ab877d9 --- /dev/null +++ b/variants/m5stack_core/Speaker.h @@ -0,0 +1,30 @@ +#ifndef _SPEAKER_H_ + #define _SPEAKER_H_ + + #include "configuration.h" + + #ifdef __cplusplus + extern "C" + { + #endif /* __cplusplus */ + #include "esp32-hal-dac.h" + #ifdef __cplusplus + } + #endif /* __cplusplus */ + + class TONE { + public: + TONE(void); + + void begin(); + void end(); + void mute(); + void tone(uint16_t frequency); + void setVolume(uint8_t volume); + + private: + uint8_t _volume; + bool _begun; + bool speaker_on; + }; +#endif diff --git a/variants/m5stack_core/platformio.ini b/variants/m5stack_core/platformio.ini index dffba3d2..69b9eb92 100644 --- a/variants/m5stack_core/platformio.ini +++ b/variants/m5stack_core/platformio.ini @@ -4,6 +4,9 @@ board = m5stack-core-esp32 upload_port = COM8 monitor_port = COM8 monitor_filters = esp32_exception_decoder +src_filter = + ${esp32_base.src_filter} + +<../variants/m5stack_core> build_flags = ${esp32_base.build_flags} -D PRIVATE_HW -I variants/m5stack_core -DILI9341_DRIVER @@ -24,4 +27,4 @@ lib_ignore = m5stack-core lib_deps = ${esp32_base.lib_deps} - bodmer/TFT_eSPI@^2.4.61 + bodmer/TFT_eSPI@^2.4.61 \ No newline at end of file diff --git a/variants/m5stack_core/variant.h b/variants/m5stack_core/variant.h index f8069526..0fd294c6 100644 --- a/variants/m5stack_core/variant.h +++ b/variants/m5stack_core/variant.h @@ -10,6 +10,9 @@ #define BUTTON_PIN 38 +#define PIN_BUZZER 25 +#define TONE_PIN_CHANNEL 0 + #undef RF95_SCK #undef RF95_MISO #undef RF95_MOSI @@ -32,11 +35,9 @@ #define GPS_RX_PIN 16 #define GPS_TX_PIN 17 -#define NO_GPS - // Define if screen should be mirrored left to right #define SCREEN_ROTATE // LCD screens are slow, so slowdown the wipe so it looks better #define SCREEN_TRANSITION_MSECS 1 -#define SCREEN_TRANSITION_FRAMERATE 1 // fps \ No newline at end of file +#define SCREEN_TRANSITION_FRAMERATE 1 // fps