Skip to content

Commit

Permalink
Merge pull request #1839 from meshtastic/pwm-notify
Browse files Browse the repository at this point in the history
use PWM buzzer on notification module.
  • Loading branch information
caveman99 authored Oct 22, 2022
2 parents 0bda4c2 + 62b3509 commit 05147c0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
62 changes: 36 additions & 26 deletions src/modules/ExternalNotificationModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
#include "NodeDB.h"
#include "RTC.h"
#include "Router.h"
#include "buzz/buzz.h"
#include "configuration.h"
#include <Arduino.h>

#ifndef PIN_BUZZER
#define PIN_BUZZER false
#endif

//#include <assert.h>

/*
Expand Down Expand Up @@ -44,7 +49,11 @@
*/

// Default configurations
#ifdef EXT_NOTIFY_OUT
#define EXT_NOTIFICATION_MODULE_OUTPUT EXT_NOTIFY_OUT
#else
#define EXT_NOTIFICATION_MODULE_OUTPUT 0
#endif
#define EXT_NOTIFICATION_MODULE_OUTPUT_MS 1000

#define ASCII_BELL 0x07
Expand Down Expand Up @@ -75,7 +84,9 @@ int32_t ExternalNotificationModule::runOnce()
: EXT_NOTIFICATION_MODULE_OUTPUT_MS) <
millis()) {
DEBUG_MSG("Turning off external notification\n");
setExternalOff();
if (output != PIN_BUZZER) {
setExternalOff();
}
}
}

Expand All @@ -84,27 +95,19 @@ int32_t ExternalNotificationModule::runOnce()

void ExternalNotificationModule::setExternalOn()
{
#ifdef EXT_NOTIFY_OUT
externalCurrentState = 1;
externalTurnedOn = millis();

digitalWrite((moduleConfig.external_notification.output
? moduleConfig.external_notification.output
: EXT_NOTIFICATION_MODULE_OUTPUT),
digitalWrite(output,
(moduleConfig.external_notification.active ? true : false));
#endif
}

void ExternalNotificationModule::setExternalOff()
{
#ifdef EXT_NOTIFY_OUT
externalCurrentState = 0;

digitalWrite((moduleConfig.external_notification.output
? moduleConfig.external_notification.output
: EXT_NOTIFICATION_MODULE_OUTPUT),
digitalWrite(output,
(moduleConfig.external_notification.active ? false : true));
#endif
}

// --------
Expand All @@ -116,8 +119,6 @@ ExternalNotificationModule::ExternalNotificationModule()
// restrict to the admin channel for rx
boundChannel = Channels::gpioChannel;

#ifdef EXT_NOTIFY_OUT

/*
Uncomment the preferences below if you want to use the module
without having to configure it from the PythonAPI or WebUI.
Expand All @@ -135,25 +136,27 @@ ExternalNotificationModule::ExternalNotificationModule()

DEBUG_MSG("Initializing External Notification Module\n");

// Set the direction of a pin
pinMode((moduleConfig.external_notification.output
? moduleConfig.external_notification.output
: EXT_NOTIFICATION_MODULE_OUTPUT),
OUTPUT);
output = moduleConfig.external_notification.output
? moduleConfig.external_notification.output
: EXT_NOTIFICATION_MODULE_OUTPUT;

// Turn off the pin
setExternalOff();
if (output != PIN_BUZZER) {
// 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 {
DEBUG_MSG("External Notification Module Disabled\n");
enabled = false;
}
#endif
}

ProcessMessage ExternalNotificationModule::handleReceived(const MeshPacket &mp)
{
#ifdef EXT_NOTIFY_OUT

if (moduleConfig.external_notification.enabled) {

if (getFrom(&mp) != nodeDB.getNodeNum()) {
Expand All @@ -165,21 +168,28 @@ 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) {
setExternalOn();
if (output != PIN_BUZZER) {
setExternalOn();
} else {
playBeep();
}
}
}
}

if (moduleConfig.external_notification.alert_message) {
DEBUG_MSG("externalNotificationModule - Notification Module\n");
setExternalOn();
if (output != PIN_BUZZER) {
setExternalOn();
} else {
playBeep();
}
}
}

} else {
DEBUG_MSG("External Notification Module Disabled\n");
}
#endif

return ProcessMessage::CONTINUE; // Let others look at this message also if they want
}
2 changes: 2 additions & 0 deletions src/modules/ExternalNotificationModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
class ExternalNotificationModule : public SinglePortModule, private concurrency::OSThread
{
uint32_t output = 0;

public:
ExternalNotificationModule();

Expand Down

0 comments on commit 05147c0

Please sign in to comment.