2021-01-28 03:18:16 +00:00
|
|
|
#include "ExternalNotificationPlugin.h"
|
|
|
|
#include "MeshService.h"
|
|
|
|
#include "NodeDB.h"
|
|
|
|
#include "RTC.h"
|
|
|
|
#include "Router.h"
|
|
|
|
#include "configuration.h"
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
2021-01-28 05:20:18 +00:00
|
|
|
This plugin supports:
|
|
|
|
https://github.com/meshtastic/Meshtastic-device/issues/654
|
2021-01-28 05:35:07 +00:00
|
|
|
|
2021-01-28 05:20:18 +00:00
|
|
|
|
2021-01-28 03:18:16 +00:00
|
|
|
bool ext_notification_plugin_enabled = 126;
|
|
|
|
bool ext_notification_plugin_active = 129;
|
|
|
|
bool ext_notification_plugin_alert_message = 130;
|
|
|
|
bool ext_notification_plugin_alert_bell = 131;
|
2021-01-28 05:20:18 +00:00
|
|
|
uint32 ext_notification_plugin_output = 128;
|
|
|
|
|
|
|
|
|
|
|
|
uint32 ext_notification_plugin_output_ms = 127;
|
2021-01-28 03:18:16 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2021-01-29 07:02:00 +00:00
|
|
|
#define EXT_NOTIFICATION_PLUGIN_ENABLED 0
|
|
|
|
#define EXT_NOTIFICATION_PLUGIN_ACTIVE 0
|
2021-01-29 07:16:10 +00:00
|
|
|
#define EXT_NOTIFICATION_PLUGIN_ALERT_MESSAGE 0
|
2021-01-29 07:02:00 +00:00
|
|
|
#define EXT_NOTIFICATION_PLUGIN_ALERT_BELL 0
|
2021-01-28 05:20:18 +00:00
|
|
|
#define EXT_NOTIFICATION_PLUGIN_OUTPUT 13
|
2021-01-29 07:02:00 +00:00
|
|
|
#define EXT_NOTIFICATION_PLUGIN_OUTPUT_MS 1000
|
2021-01-28 05:20:18 +00:00
|
|
|
|
|
|
|
#define ASCII_BELL 0x07
|
|
|
|
|
2021-01-28 03:18:16 +00:00
|
|
|
ExternalNotificationPlugin *externalNotificationPlugin;
|
|
|
|
ExternalNotificationPluginRadio *externalNotificationPluginRadio;
|
|
|
|
|
|
|
|
ExternalNotificationPlugin::ExternalNotificationPlugin() : concurrency::OSThread("ExternalNotificationPlugin") {}
|
|
|
|
|
2021-01-29 07:02:00 +00:00
|
|
|
bool externalCurrentState = 0;
|
|
|
|
uint32_t externalTurnedOn = 0;
|
|
|
|
|
2021-01-28 03:18:16 +00:00
|
|
|
int32_t ExternalNotificationPlugin::runOnce()
|
|
|
|
{
|
|
|
|
#ifndef NO_ESP32
|
|
|
|
|
|
|
|
/*
|
|
|
|
Uncomment the preferences below if you want to use the plugin
|
|
|
|
without having to configure it from the PythonAPI or WebUI.
|
|
|
|
|
2021-01-30 03:35:03 +00:00
|
|
|
EXT_NOTIFICATION_PLUGIN_ENABLED
|
|
|
|
0 = Disabled (Default)
|
|
|
|
1 = Enabled
|
|
|
|
|
|
|
|
EXT_NOTIFICATION_PLUGIN_ACTIVE
|
|
|
|
0 = Active Low (Default)
|
|
|
|
1 = Active High
|
|
|
|
|
|
|
|
EXT_NOTIFICATION_PLUGIN_ALERT_MESSAGE
|
|
|
|
0 = Disabled (Default)
|
|
|
|
1 = Alert when a text message comes
|
|
|
|
|
|
|
|
EXT_NOTIFICATION_PLUGIN_ALERT_BELL
|
|
|
|
0 = Disabled (Default)
|
|
|
|
1 = Alert when the bell character is received
|
|
|
|
|
|
|
|
EXT_NOTIFICATION_PLUGIN_OUTPUT
|
|
|
|
GPIO of the output. (Default = 13)
|
|
|
|
|
|
|
|
EXT_NOTIFICATION_PLUGIN_OUTPUT_MS
|
|
|
|
Amount of time in ms for the alert. Default is 1000.
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
2021-01-28 03:18:16 +00:00
|
|
|
|
2021-01-28 05:20:18 +00:00
|
|
|
if (EXT_NOTIFICATION_PLUGIN_ENABLED) {
|
2021-01-28 03:18:16 +00:00
|
|
|
|
|
|
|
if (firstTime) {
|
|
|
|
|
|
|
|
DEBUG_MSG("Initializing External Notification Plugin\n");
|
|
|
|
|
2021-01-28 04:06:39 +00:00
|
|
|
// Set the direction of a pin
|
2021-01-28 05:20:18 +00:00
|
|
|
pinMode(EXT_NOTIFICATION_PLUGIN_OUTPUT, OUTPUT);
|
2021-01-28 04:06:39 +00:00
|
|
|
|
2021-01-29 07:16:10 +00:00
|
|
|
// Turn off the pin
|
|
|
|
setExternalOff();
|
2021-01-28 04:06:39 +00:00
|
|
|
|
2021-01-29 07:02:00 +00:00
|
|
|
externalNotificationPluginRadio = new ExternalNotificationPluginRadio();
|
|
|
|
|
|
|
|
firstTime = 0;
|
|
|
|
|
2021-01-28 03:18:16 +00:00
|
|
|
} else {
|
2021-01-28 05:35:07 +00:00
|
|
|
if (externalCurrentState) {
|
2021-01-28 03:18:16 +00:00
|
|
|
|
2021-01-30 03:35:03 +00:00
|
|
|
// If the output is turned on, turn it back off after the given period of time.
|
2021-01-28 05:35:07 +00:00
|
|
|
if (externalTurnedOn + EXT_NOTIFICATION_PLUGIN_OUTPUT_MS < millis()) {
|
2021-01-29 07:02:00 +00:00
|
|
|
DEBUG_MSG("Turning off external notification\n");
|
2021-01-28 05:35:07 +00:00
|
|
|
setExternalOff();
|
|
|
|
}
|
|
|
|
}
|
2021-01-28 03:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (25);
|
|
|
|
} else {
|
|
|
|
DEBUG_MSG("External Notification Plugin Disabled\n");
|
|
|
|
|
|
|
|
return (INT32_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-01-28 04:06:39 +00:00
|
|
|
void ExternalNotificationPlugin::setExternalOn()
|
|
|
|
{
|
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
|
|
|
|
2021-01-28 04:06:39 +00:00
|
|
|
// if ext_notification_plugin_active
|
2021-01-30 03:35:03 +00:00
|
|
|
digitalWrite(EXT_NOTIFICATION_PLUGIN_OUTPUT, (EXT_NOTIFICATION_PLUGIN_ACTIVE ? true : false));
|
2021-01-28 04:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalNotificationPlugin::setExternalOff()
|
|
|
|
{
|
2021-01-28 05:20:18 +00:00
|
|
|
externalCurrentState = 0;
|
|
|
|
|
2021-01-28 04:06:39 +00:00
|
|
|
// if ext_notification_plugin_active
|
2021-01-30 03:35:03 +00:00
|
|
|
digitalWrite(EXT_NOTIFICATION_PLUGIN_OUTPUT, (EXT_NOTIFICATION_PLUGIN_ACTIVE ? false : true));
|
2021-01-28 04:06:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-28 03:18:16 +00:00
|
|
|
// --------
|
|
|
|
|
|
|
|
bool ExternalNotificationPluginRadio::handleReceived(const MeshPacket &mp)
|
|
|
|
{
|
|
|
|
#ifndef NO_ESP32
|
|
|
|
|
2021-01-28 05:20:18 +00:00
|
|
|
if (EXT_NOTIFICATION_PLUGIN_ENABLED) {
|
2021-01-28 03:18:16 +00:00
|
|
|
|
|
|
|
auto &p = mp.decoded.data;
|
2021-01-29 07:16:10 +00:00
|
|
|
// DEBUG_MSG("Processing handleReceived\n");
|
2021-01-28 03:18:16 +00:00
|
|
|
|
|
|
|
if (mp.from != nodeDB.getNodeNum()) {
|
2021-01-29 07:02:00 +00:00
|
|
|
DEBUG_MSG("handleReceived from some other device\n");
|
2021-01-28 03:18:16 +00:00
|
|
|
|
2021-01-28 05:20:18 +00:00
|
|
|
if (EXT_NOTIFICATION_PLUGIN_ALERT_BELL) {
|
|
|
|
for (int i = 0; i < p.payload.size; i++) {
|
|
|
|
if (p.payload.bytes[i] == ASCII_BELL) {
|
|
|
|
externalNotificationPlugin->setExternalOn();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-28 03:18:16 +00:00
|
|
|
|
2021-01-28 05:20:18 +00:00
|
|
|
if (EXT_NOTIFICATION_PLUGIN_ALERT_MESSAGE) {
|
2021-01-29 07:16:10 +00:00
|
|
|
// DEBUG_MSG("Turning on alert\n");
|
2021-01-28 05:20:18 +00:00
|
|
|
externalNotificationPlugin->setExternalOn();
|
|
|
|
}
|
2021-01-28 03:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
DEBUG_MSG("External Notification Plugin Disabled\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true; // Let others look at this message also if they want
|
|
|
|
}
|