2022-02-27 08:18:35 +00:00
|
|
|
#include "ExternalNotificationModule.h"
|
2021-01-28 03:18:16 +00:00
|
|
|
#include "MeshService.h"
|
|
|
|
#include "NodeDB.h"
|
|
|
|
#include "RTC.h"
|
|
|
|
#include "Router.h"
|
2022-10-22 11:35:34 +00:00
|
|
|
#include "buzz/buzz.h"
|
2022-05-07 10:31:21 +00:00
|
|
|
#include "configuration.h"
|
2021-01-28 03:18:16 +00:00
|
|
|
#include <Arduino.h>
|
|
|
|
|
2022-10-22 11:35:34 +00:00
|
|
|
#ifndef PIN_BUZZER
|
|
|
|
#define PIN_BUZZER false
|
|
|
|
#endif
|
|
|
|
|
2021-02-14 21:31:11 +00:00
|
|
|
//#include <assert.h>
|
2021-01-28 03:18:16 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
2021-01-30 17:36:17 +00:00
|
|
|
Documentation:
|
2022-10-31 07:32:38 +00:00
|
|
|
https://github.com/meshtastic/firmware/blob/master/docs/software/modules/ExternalNotificationModule.md
|
2021-01-28 05:35:07 +00:00
|
|
|
|
2022-02-27 08:29:05 +00:00
|
|
|
This module supports:
|
2022-10-31 07:32:38 +00:00
|
|
|
https://github.com/meshtastic/firmware/issues/654
|
2021-01-28 05:20:18 +00:00
|
|
|
|
|
|
|
|
2021-01-30 17:36:17 +00:00
|
|
|
Quick reference:
|
2021-01-28 05:20:18 +00:00
|
|
|
|
2022-05-22 11:27:56 +00:00
|
|
|
moduleConfig.external_notification.enabled
|
2021-01-30 17:36:17 +00:00
|
|
|
0 = Disabled (Default)
|
|
|
|
1 = Enabled
|
2021-01-30 17:17:40 +00:00
|
|
|
|
2022-05-22 11:27:56 +00:00
|
|
|
moduleConfig.external_notification.active
|
2021-01-30 17:36:17 +00:00
|
|
|
0 = Active Low (Default)
|
|
|
|
1 = Active High
|
2021-01-30 17:17:40 +00:00
|
|
|
|
2022-05-22 11:27:56 +00:00
|
|
|
moduleConfig.external_notification.alert_message
|
2021-01-30 17:36:17 +00:00
|
|
|
0 = Disabled (Default)
|
|
|
|
1 = Alert when a text message comes
|
2021-01-30 17:17:40 +00:00
|
|
|
|
2022-05-22 11:27:56 +00:00
|
|
|
moduleConfig.external_notification.alert_bell
|
2021-01-30 17:36:17 +00:00
|
|
|
0 = Disabled (Default)
|
|
|
|
1 = Alert when the bell character is received
|
|
|
|
|
2022-05-22 11:27:56 +00:00
|
|
|
moduleConfig.external_notification.output
|
2021-01-30 17:36:17 +00:00
|
|
|
GPIO of the output. (Default = 13)
|
|
|
|
|
2022-05-22 11:27:56 +00:00
|
|
|
moduleConfig.external_notification.output_ms
|
2021-01-30 17:36:17 +00:00
|
|
|
Amount of time in ms for the alert. Default is 1000.
|
2021-01-28 03:18:16 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2021-01-30 17:17:40 +00:00
|
|
|
// Default configurations
|
2022-10-22 12:13:45 +00:00
|
|
|
#ifdef EXT_NOTIFY_OUT
|
2022-02-27 09:49:24 +00:00
|
|
|
#define EXT_NOTIFICATION_MODULE_OUTPUT EXT_NOTIFY_OUT
|
2022-10-22 12:13:45 +00:00
|
|
|
#else
|
|
|
|
#define EXT_NOTIFICATION_MODULE_OUTPUT 0
|
|
|
|
#endif
|
2022-02-27 09:49:24 +00:00
|
|
|
#define EXT_NOTIFICATION_MODULE_OUTPUT_MS 1000
|
2021-01-28 05:20:18 +00:00
|
|
|
|
|
|
|
#define ASCII_BELL 0x07
|
|
|
|
|
2021-01-29 07:02:00 +00:00
|
|
|
bool externalCurrentState = 0;
|
|
|
|
uint32_t externalTurnedOn = 0;
|
|
|
|
|
2022-02-27 09:49:24 +00:00
|
|
|
int32_t ExternalNotificationModule::runOnce()
|
2021-01-28 03:18:16 +00:00
|
|
|
{
|
|
|
|
/*
|
2022-02-27 08:29:05 +00:00
|
|
|
Uncomment the preferences below if you want to use the module
|
2021-01-28 03:18:16 +00:00
|
|
|
without having to configure it from the PythonAPI or WebUI.
|
2021-01-30 17:17:40 +00:00
|
|
|
*/
|
2021-01-28 03:18:16 +00:00
|
|
|
|
2022-05-22 11:27:56 +00:00
|
|
|
// moduleConfig.external_notification.enabled = 1;
|
|
|
|
// moduleConfig.external_notification.alert_message = 1;
|
2021-01-30 03:35:03 +00:00
|
|
|
|
2022-05-22 11:27:56 +00:00
|
|
|
// moduleConfig.external_notification.active = 1;
|
|
|
|
// moduleConfig.external_notification.alert_bell = 1;
|
|
|
|
// moduleConfig.external_notification.output_ms = 1000;
|
|
|
|
// moduleConfig.external_notification.output = 13;
|
2021-01-28 03:18:16 +00:00
|
|
|
|
2021-03-13 05:32:23 +00:00
|
|
|
if (externalCurrentState) {
|
2021-01-28 04:06:39 +00:00
|
|
|
|
2021-03-13 05:32:23 +00:00
|
|
|
// If the output is turned on, turn it back off after the given period of time.
|
2022-05-22 11:27:56 +00:00
|
|
|
if (externalTurnedOn + (moduleConfig.external_notification.output_ms
|
|
|
|
? moduleConfig.external_notification.output_ms
|
2022-02-27 09:49:24 +00:00
|
|
|
: EXT_NOTIFICATION_MODULE_OUTPUT_MS) <
|
2021-03-13 05:32:23 +00:00
|
|
|
millis()) {
|
|
|
|
DEBUG_MSG("Turning off external notification\n");
|
2022-10-22 11:35:34 +00:00
|
|
|
if (output != PIN_BUZZER) {
|
|
|
|
setExternalOff();
|
|
|
|
}
|
2021-01-28 03:18:16 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-13 05:32:23 +00:00
|
|
|
|
|
|
|
return (25);
|
2021-01-28 03:18:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-27 09:49:24 +00:00
|
|
|
void ExternalNotificationModule::setExternalOn()
|
2021-01-28 04:06:39 +00:00
|
|
|
{
|
2021-01-28 05:20:18 +00:00
|
|
|
externalCurrentState = 1;
|
2021-01-28 05:35:07 +00:00
|
|
|
externalTurnedOn = millis();
|
2021-01-28 05:20:18 +00:00
|
|
|
|
2022-10-22 11:35:34 +00:00
|
|
|
digitalWrite(output,
|
2022-05-22 11:27:56 +00:00
|
|
|
(moduleConfig.external_notification.active ? true : false));
|
2021-01-28 04:06:39 +00:00
|
|
|
}
|
|
|
|
|
2022-02-27 09:49:24 +00:00
|
|
|
void ExternalNotificationModule::setExternalOff()
|
2021-01-28 04:06:39 +00:00
|
|
|
{
|
2021-01-28 05:20:18 +00:00
|
|
|
externalCurrentState = 0;
|
|
|
|
|
2022-10-22 11:35:34 +00:00
|
|
|
digitalWrite(output,
|
2022-05-22 11:27:56 +00:00
|
|
|
(moduleConfig.external_notification.active ? false : true));
|
2021-01-28 04:06:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-28 03:18:16 +00:00
|
|
|
// --------
|
|
|
|
|
2022-02-27 09:49:24 +00:00
|
|
|
ExternalNotificationModule::ExternalNotificationModule()
|
2022-03-09 08:01:43 +00:00
|
|
|
: SinglePortModule("ExternalNotificationModule", PortNum_TEXT_MESSAGE_APP), concurrency::OSThread(
|
2022-05-07 10:31:21 +00:00
|
|
|
"ExternalNotificationModule")
|
2021-03-13 05:32:23 +00:00
|
|
|
{
|
|
|
|
/*
|
2022-02-27 09:49:24 +00:00
|
|
|
Uncomment the preferences below if you want to use the module
|
2021-03-13 05:32:23 +00:00
|
|
|
without having to configure it from the PythonAPI or WebUI.
|
|
|
|
*/
|
|
|
|
|
2022-05-22 11:27:56 +00:00
|
|
|
// moduleConfig.external_notification.enabled = 1;
|
|
|
|
// moduleConfig.external_notification.alert_message = 1;
|
2021-03-13 05:32:23 +00:00
|
|
|
|
2022-05-22 11:27:56 +00:00
|
|
|
// moduleConfig.external_notification.active = 1;
|
|
|
|
// moduleConfig.external_notification.alert_bell = 1;
|
|
|
|
// moduleConfig.external_notification.output_ms = 1000;
|
|
|
|
// moduleConfig.external_notification.output = 13;
|
2022-11-04 18:56:44 +00:00
|
|
|
|
2022-05-22 11:27:56 +00:00
|
|
|
if (moduleConfig.external_notification.enabled) {
|
2021-03-13 05:32:23 +00:00
|
|
|
|
2022-02-27 09:49:24 +00:00
|
|
|
DEBUG_MSG("Initializing External Notification Module\n");
|
2021-03-13 05:32:23 +00:00
|
|
|
|
2022-10-22 11:35:34 +00:00
|
|
|
output = moduleConfig.external_notification.output
|
|
|
|
? moduleConfig.external_notification.output
|
|
|
|
: EXT_NOTIFICATION_MODULE_OUTPUT;
|
2021-03-13 05:32:23 +00:00
|
|
|
|
2022-10-22 11:35:34 +00:00
|
|
|
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);
|
|
|
|
}
|
2021-03-13 05:32:23 +00:00
|
|
|
} else {
|
2022-02-27 09:49:24 +00:00
|
|
|
DEBUG_MSG("External Notification Module Disabled\n");
|
2021-03-13 05:32:23 +00:00
|
|
|
enabled = false;
|
|
|
|
}
|
2021-03-13 05:14:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-27 09:49:24 +00:00
|
|
|
ProcessMessage ExternalNotificationModule::handleReceived(const MeshPacket &mp)
|
2021-01-28 03:18:16 +00:00
|
|
|
{
|
2022-05-22 11:27:56 +00:00
|
|
|
if (moduleConfig.external_notification.enabled) {
|
2021-01-28 03:18:16 +00:00
|
|
|
|
2021-03-05 02:19:27 +00:00
|
|
|
if (getFrom(&mp) != nodeDB.getNodeNum()) {
|
2021-01-28 03:18:16 +00:00
|
|
|
|
2021-01-30 17:17:40 +00:00
|
|
|
// TODO: This may be a problem if messages are sent in unicide, but I'm not sure if it will.
|
|
|
|
// Need to know if and how this could be a problem.
|
2022-05-22 11:27:56 +00:00
|
|
|
if (moduleConfig.external_notification.alert_bell) {
|
2022-01-24 18:39:17 +00:00
|
|
|
auto &p = mp.decoded;
|
2022-02-27 09:49:24 +00:00
|
|
|
DEBUG_MSG("externalNotificationModule - Notification Bell\n");
|
2021-01-28 05:20:18 +00:00
|
|
|
for (int i = 0; i < p.payload.size; i++) {
|
|
|
|
if (p.payload.bytes[i] == ASCII_BELL) {
|
2022-10-22 11:35:34 +00:00
|
|
|
if (output != PIN_BUZZER) {
|
|
|
|
setExternalOn();
|
|
|
|
} else {
|
|
|
|
playBeep();
|
|
|
|
}
|
2021-01-28 05:20:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-28 03:18:16 +00:00
|
|
|
|
2022-05-22 11:27:56 +00:00
|
|
|
if (moduleConfig.external_notification.alert_message) {
|
2022-02-27 09:49:24 +00:00
|
|
|
DEBUG_MSG("externalNotificationModule - Notification Module\n");
|
2022-10-22 11:35:34 +00:00
|
|
|
if (output != PIN_BUZZER) {
|
|
|
|
setExternalOn();
|
|
|
|
} else {
|
|
|
|
playBeep();
|
|
|
|
}
|
2021-01-28 05:20:18 +00:00
|
|
|
}
|
2021-01-28 03:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2022-02-27 09:49:24 +00:00
|
|
|
DEBUG_MSG("External Notification Module Disabled\n");
|
2021-01-28 03:18:16 +00:00
|
|
|
}
|
|
|
|
|
2021-09-23 01:42:09 +00:00
|
|
|
return ProcessMessage::CONTINUE; // Let others look at this message also if they want
|
2021-01-28 03:18:16 +00:00
|
|
|
}
|