From 5f8267c9561de87c220f496bdcb8bbb40378268d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Thu, 24 Nov 2022 10:11:42 +0100 Subject: [PATCH] use the buzzer for external notification module --- src/buzz/buzz.cpp | 23 ++++++------------ src/modules/ExternalNotificationModule.cpp | 27 +++++++++++++--------- variants/rak4631/variant.h | 3 ++- 3 files changed, 25 insertions(+), 28 deletions(-) diff --git a/src/buzz/buzz.cpp b/src/buzz/buzz.cpp index f8314fe51..88e3f3096 100644 --- a/src/buzz/buzz.cpp +++ b/src/buzz/buzz.cpp @@ -2,14 +2,6 @@ #include "configuration.h" #include "NodeDB.h" -#ifndef PIN_BUZZER - -// Noop methods for boards w/o buzzer -void playBeep(){}; -void playStartMelody(){}; -void playShutdownMelody(){}; - -#else #ifdef M5STACK #include "Speaker.h" TONE Tone; @@ -43,7 +35,11 @@ const int DURATION_1_8 = 125; // 1/8 note const int DURATION_1_4 = 250; // 1/4 note void playTones(const ToneDuration *tone_durations, int size) { - if (config.network.eth_enabled != true) { +#ifdef PIN_BUZZER + if (!config.device.buzzer_gpio) + config.device.buzzer_gpio = PIN_BUZZER; +#endif + if (config.device.buzzer_gpio) { for (int i = 0; i < size; i++) { const auto &tone_duration = tone_durations[i]; #ifdef M5STACK @@ -51,7 +47,7 @@ void playTones(const ToneDuration *tone_durations, int size) { delay(tone_duration.duration_ms); Tone.mute(); #else - tone(PIN_BUZZER, tone_duration.frequency_khz, tone_duration.duration_ms); + tone(config.device.buzzer_gpio, 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); @@ -59,14 +55,11 @@ void playTones(const ToneDuration *tone_durations, int size) { } } -#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_FS3, DURATION_1_8}, @@ -75,11 +68,9 @@ void playStartMelody() { playTones(melody, sizeof(melody) / sizeof(ToneDuration)); } - void playShutdownMelody() { ToneDuration melody[] = {{NOTE_CS4, DURATION_1_8}, {NOTE_AS3, DURATION_1_8}, {NOTE_FS3, DURATION_1_4}}; playTones(melody, sizeof(melody) / sizeof(ToneDuration)); } -#endif \ No newline at end of file diff --git a/src/modules/ExternalNotificationModule.cpp b/src/modules/ExternalNotificationModule.cpp index 0d901ecb4..5a95ceec1 100644 --- a/src/modules/ExternalNotificationModule.cpp +++ b/src/modules/ExternalNotificationModule.cpp @@ -76,7 +76,7 @@ int32_t ExternalNotificationModule::runOnce() // moduleConfig.external_notification.output_ms = 1000; // moduleConfig.external_notification.output = 13; - if (externalCurrentState) { + if (externalCurrentState && !moduleConfig.external_notification.use_pwm) { // If the output is turned on, turn it back off after the given period of time. if (externalTurnedOn + (moduleConfig.external_notification.output_ms @@ -84,13 +84,13 @@ int32_t ExternalNotificationModule::runOnce() : EXT_NOTIFICATION_MODULE_OUTPUT_MS) < millis()) { DEBUG_MSG("Turning off external notification\n"); - if (output != PIN_BUZZER) { - setExternalOff(); - } + setExternalOff(); } } - - return (25); + if (moduleConfig.external_notification.use_pwm) + return INT32_MAX; // we don't need this thread here... + else + return 25; } void ExternalNotificationModule::setExternalOn() @@ -137,14 +137,19 @@ ExternalNotificationModule::ExternalNotificationModule() ? moduleConfig.external_notification.output : EXT_NOTIFICATION_MODULE_OUTPUT; - if (output != PIN_BUZZER) { + if (!moduleConfig.external_notification.use_pwm) { // Set the direction of a pin DEBUG_MSG("Using Pin %i in digital mode\n", output); pinMode(output, OUTPUT); // Turn off the pin setExternalOff(); - } else{ - DEBUG_MSG("Using Pin %i in PWM mode\n", output); + } else { + config.device.buzzer_gpio = config.device.buzzer_gpio + ? config.device.buzzer_gpio + : PIN_BUZZER; + + // in PWM Mode we force the buzzer pin if it is set + DEBUG_MSG("Using Pin %i in PWM mode\n", config.device.buzzer_gpio); } } else { DEBUG_MSG("External Notification Module Disabled\n"); @@ -165,7 +170,7 @@ ProcessMessage ExternalNotificationModule::handleReceived(const MeshPacket &mp) DEBUG_MSG("externalNotificationModule - Notification Bell\n"); for (int i = 0; i < p.payload.size; i++) { if (p.payload.bytes[i] == ASCII_BELL) { - if (output != PIN_BUZZER) { + if (!moduleConfig.external_notification.use_pwm) { setExternalOn(); } else { playBeep(); @@ -176,7 +181,7 @@ ProcessMessage ExternalNotificationModule::handleReceived(const MeshPacket &mp) if (moduleConfig.external_notification.alert_message) { DEBUG_MSG("externalNotificationModule - Notification Module\n"); - if (output != PIN_BUZZER) { + if (!moduleConfig.external_notification.use_pwm) { setExternalOn(); } else { playBeep(); diff --git a/variants/rak4631/variant.h b/variants/rak4631/variant.h index 904491b6a..51da8590f 100644 --- a/variants/rak4631/variant.h +++ b/variants/rak4631/variant.h @@ -205,7 +205,8 @@ static const uint8_t SCK = PIN_SPI_SCK; #define RV3028_RTC (uint8_t) 0b1010010 // RAK18001 Buzzer in Slot C -#define PIN_BUZZER 21 // IO3 is PWM2 +// #define PIN_BUZZER 21 // IO3 is PWM2 +// NEW: set this via protobuf instead! // Battery // The battery sense is hooked to pin A0 (5)