use the buzzer for external notification module

This commit is contained in:
Thomas Göttgens 2022-11-24 10:11:42 +01:00
parent cf783a5bae
commit 5f8267c956
3 changed files with 25 additions and 28 deletions

View File

@ -2,14 +2,6 @@
#include "configuration.h" #include "configuration.h"
#include "NodeDB.h" #include "NodeDB.h"
#ifndef PIN_BUZZER
// Noop methods for boards w/o buzzer
void playBeep(){};
void playStartMelody(){};
void playShutdownMelody(){};
#else
#ifdef M5STACK #ifdef M5STACK
#include "Speaker.h" #include "Speaker.h"
TONE Tone; TONE Tone;
@ -43,7 +35,11 @@ const int DURATION_1_8 = 125; // 1/8 note
const int DURATION_1_4 = 250; // 1/4 note const int DURATION_1_4 = 250; // 1/4 note
void playTones(const ToneDuration *tone_durations, int size) { 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++) { for (int i = 0; i < size; i++) {
const auto &tone_duration = tone_durations[i]; const auto &tone_duration = tone_durations[i];
#ifdef M5STACK #ifdef M5STACK
@ -51,7 +47,7 @@ void playTones(const ToneDuration *tone_durations, int size) {
delay(tone_duration.duration_ms); delay(tone_duration.duration_ms);
Tone.mute(); Tone.mute();
#else #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 #endif
// to distinguish the notes, set a minimum time between them. // to distinguish the notes, set a minimum time between them.
delay(1.3 * tone_duration.duration_ms); delay(1.3 * tone_duration.duration_ms);
@ -59,14 +55,11 @@ void playTones(const ToneDuration *tone_durations, int size) {
} }
} }
#ifdef M5STACK
void playBeep() { void playBeep() {
ToneDuration melody[] = {{NOTE_B3, DURATION_1_4}}; ToneDuration melody[] = {{NOTE_B3, DURATION_1_4}};
playTones(melody, sizeof(melody) / sizeof(ToneDuration)); playTones(melody, sizeof(melody) / sizeof(ToneDuration));
} }
#else
void playBeep() { tone(PIN_BUZZER, NOTE_B3, DURATION_1_4); }
#endif
void playStartMelody() { void playStartMelody() {
ToneDuration melody[] = {{NOTE_FS3, DURATION_1_8}, ToneDuration melody[] = {{NOTE_FS3, DURATION_1_8},
@ -75,11 +68,9 @@ void playStartMelody() {
playTones(melody, sizeof(melody) / sizeof(ToneDuration)); playTones(melody, sizeof(melody) / sizeof(ToneDuration));
} }
void playShutdownMelody() { void playShutdownMelody() {
ToneDuration melody[] = {{NOTE_CS4, DURATION_1_8}, ToneDuration melody[] = {{NOTE_CS4, DURATION_1_8},
{NOTE_AS3, DURATION_1_8}, {NOTE_AS3, DURATION_1_8},
{NOTE_FS3, DURATION_1_4}}; {NOTE_FS3, DURATION_1_4}};
playTones(melody, sizeof(melody) / sizeof(ToneDuration)); playTones(melody, sizeof(melody) / sizeof(ToneDuration));
} }
#endif

View File

@ -76,7 +76,7 @@ int32_t ExternalNotificationModule::runOnce()
// moduleConfig.external_notification.output_ms = 1000; // moduleConfig.external_notification.output_ms = 1000;
// moduleConfig.external_notification.output = 13; // 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 the output is turned on, turn it back off after the given period of time.
if (externalTurnedOn + (moduleConfig.external_notification.output_ms if (externalTurnedOn + (moduleConfig.external_notification.output_ms
@ -84,13 +84,13 @@ int32_t ExternalNotificationModule::runOnce()
: EXT_NOTIFICATION_MODULE_OUTPUT_MS) < : EXT_NOTIFICATION_MODULE_OUTPUT_MS) <
millis()) { millis()) {
DEBUG_MSG("Turning off external notification\n"); DEBUG_MSG("Turning off external notification\n");
if (output != PIN_BUZZER) { setExternalOff();
setExternalOff();
}
} }
} }
if (moduleConfig.external_notification.use_pwm)
return (25); return INT32_MAX; // we don't need this thread here...
else
return 25;
} }
void ExternalNotificationModule::setExternalOn() void ExternalNotificationModule::setExternalOn()
@ -137,14 +137,19 @@ ExternalNotificationModule::ExternalNotificationModule()
? moduleConfig.external_notification.output ? moduleConfig.external_notification.output
: EXT_NOTIFICATION_MODULE_OUTPUT; : EXT_NOTIFICATION_MODULE_OUTPUT;
if (output != PIN_BUZZER) { if (!moduleConfig.external_notification.use_pwm) {
// Set the direction of a pin // Set the direction of a pin
DEBUG_MSG("Using Pin %i in digital mode\n", output); DEBUG_MSG("Using Pin %i in digital mode\n", output);
pinMode(output, OUTPUT); pinMode(output, OUTPUT);
// Turn off the pin // Turn off the pin
setExternalOff(); setExternalOff();
} else{ } else {
DEBUG_MSG("Using Pin %i in PWM mode\n", output); 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 { } else {
DEBUG_MSG("External Notification Module Disabled\n"); DEBUG_MSG("External Notification Module Disabled\n");
@ -165,7 +170,7 @@ ProcessMessage ExternalNotificationModule::handleReceived(const MeshPacket &mp)
DEBUG_MSG("externalNotificationModule - Notification Bell\n"); DEBUG_MSG("externalNotificationModule - Notification Bell\n");
for (int i = 0; i < p.payload.size; i++) { for (int i = 0; i < p.payload.size; i++) {
if (p.payload.bytes[i] == ASCII_BELL) { if (p.payload.bytes[i] == ASCII_BELL) {
if (output != PIN_BUZZER) { if (!moduleConfig.external_notification.use_pwm) {
setExternalOn(); setExternalOn();
} else { } else {
playBeep(); playBeep();
@ -176,7 +181,7 @@ ProcessMessage ExternalNotificationModule::handleReceived(const MeshPacket &mp)
if (moduleConfig.external_notification.alert_message) { if (moduleConfig.external_notification.alert_message) {
DEBUG_MSG("externalNotificationModule - Notification Module\n"); DEBUG_MSG("externalNotificationModule - Notification Module\n");
if (output != PIN_BUZZER) { if (!moduleConfig.external_notification.use_pwm) {
setExternalOn(); setExternalOn();
} else { } else {
playBeep(); playBeep();

View File

@ -205,7 +205,8 @@ static const uint8_t SCK = PIN_SPI_SCK;
#define RV3028_RTC (uint8_t) 0b1010010 #define RV3028_RTC (uint8_t) 0b1010010
// RAK18001 Buzzer in Slot C // 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 // Battery
// The battery sense is hooked to pin A0 (5) // The battery sense is hooked to pin A0 (5)