Update to SerialPlugin to take advantage of the configs

This commit is contained in:
Jm 2021-01-16 22:27:33 -08:00
parent b9d025dd58
commit d7f26493a5
2 changed files with 64 additions and 55 deletions

View File

@ -1,14 +1,15 @@
#include "plugins/NodeInfoPlugin.h" #include "plugins/NodeInfoPlugin.h"
#include "plugins/PositionPlugin.h" #include "plugins/PositionPlugin.h"
#include "plugins/RemoteHardwarePlugin.h"
#include "plugins/ReplyPlugin.h" #include "plugins/ReplyPlugin.h"
#include "plugins/SerialPlugin.h" #include "plugins/SerialPlugin.h"
#include "plugins/RemoteHardwarePlugin.h"
#include "plugins/TextMessagePlugin.h" #include "plugins/TextMessagePlugin.h"
/** /**
* Create plugin instances here. If you are adding a new plugin, you must 'new' it here (or somewhere else) * Create plugin instances here. If you are adding a new plugin, you must 'new' it here (or somewhere else)
*/ */
void setupPlugins() { void setupPlugins()
{
nodeInfoPlugin = new NodeInfoPlugin(); nodeInfoPlugin = new NodeInfoPlugin();
positionPlugin = new PositionPlugin(); positionPlugin = new PositionPlugin();
textMessagePlugin = new TextMessagePlugin(); textMessagePlugin = new TextMessagePlugin();
@ -18,5 +19,11 @@ void setupPlugins() {
new RemoteHardwarePlugin(); new RemoteHardwarePlugin();
new ReplyPlugin(); new ReplyPlugin();
#ifndef NO_ESP32
// Only run on an esp32 based device.
new SerialPlugin(); // Maintained by MC Hamster (Jm Casler) jm@casler.org new SerialPlugin(); // Maintained by MC Hamster (Jm Casler) jm@casler.org
#endif
} }

View File

@ -25,7 +25,7 @@
Basic Usage: Basic Usage:
1) Enable the plugin by setting SERIALPLUGIN_ENABLED to 1. 1) Enable the plugin by setting serialplugin_enabled in the device configuration to 1.
2) Set the pins (RXD2 / TXD2) for your preferred RX and TX GPIO pins. 2) Set the pins (RXD2 / TXD2) for your preferred RX and TX GPIO pins.
3) Set SERIALPLUGIN_TIMEOUT to the amount of time to wait before we consider 3) Set SERIALPLUGIN_TIMEOUT to the amount of time to wait before we consider
your packet as "done". your packet as "done".
@ -57,8 +57,6 @@
#define SERIALPLUGIN_STRING_MAX Constants_DATA_PAYLOAD_LEN #define SERIALPLUGIN_STRING_MAX Constants_DATA_PAYLOAD_LEN
#define SERIALPLUGIN_TIMEOUT 250 #define SERIALPLUGIN_TIMEOUT 250
#define SERIALPLUGIN_BAUD 38400 #define SERIALPLUGIN_BAUD 38400
#define SERIALPLUGIN_ENABLED 0
#define SERIALPLUGIN_ECHO 0
#define SERIALPLUGIN_ACK 1 #define SERIALPLUGIN_ACK 1
SerialPlugin *serialPlugin; SerialPlugin *serialPlugin;
@ -70,43 +68,48 @@ char serialStringChar[Constants_DATA_PAYLOAD_LEN];
int32_t SerialPlugin::runOnce() int32_t SerialPlugin::runOnce()
{ {
#ifdef NO_ESP32 #ifndef NO_ESP32
if (radioConfig.preferences.serialplugin_enabled) {
#if SERIALPLUGIN_ENABLED == 1 if (firstTime) {
if (firstTime) { // Interface with the serial peripheral from in here.
DEBUG_MSG("Initilizing serial peripheral interface\n");
// Interface with the serial peripheral from in here. if (radioConfig.preferences.serialplugin_rxd && radioConfig.preferences.serialplugin_txd) {
DEBUG_MSG("Initilizing serial peripheral interface\n"); Serial2.begin(SERIALPLUGIN_BAUD, SERIAL_8N1, radioConfig.preferences.serialplugin_rxd,
radioConfig.preferences.serialplugin_txd);
Serial2.begin(SERIALPLUGIN_BAUD, SERIAL_8N1, RXD2, TXD2); } else {
Serial2.setTimeout(SERIALPLUGIN_TIMEOUT); // Number of MS to wait to set the timeout for the string. Serial2.begin(SERIALPLUGIN_BAUD, SERIAL_8N1, RXD2, TXD2);
Serial2.setRxBufferSize(SERIALPLUGIN_RX_BUFFER); }
Serial2.setTimeout(SERIALPLUGIN_TIMEOUT); // Number of MS to wait to set the timeout for the string.
Serial2.setRxBufferSize(SERIALPLUGIN_RX_BUFFER);
serialPluginRadio = new SerialPluginRadio(); serialPluginRadio = new SerialPluginRadio();
firstTime = 0; firstTime = 0;
} else { } else {
String serialString; String serialString;
while (Serial2.available()) { while (Serial2.available()) {
serialString = Serial2.readString(); serialString = Serial2.readString();
serialString.toCharArray(serialStringChar, Constants_DATA_PAYLOAD_LEN); serialString.toCharArray(serialStringChar, Constants_DATA_PAYLOAD_LEN);
serialPluginRadio->sendPayload(); serialPluginRadio->sendPayload();
DEBUG_MSG("Received: %s\n", serialStringChar); DEBUG_MSG("Received: %s\n", serialStringChar);
}
} }
return (10);
} else {
DEBUG_MSG("Serial Plugin Disabled\n");
return (INT32_MAX);
} }
return (10);
#else
DEBUG_MSG("Serial Plugin Disabled\n");
return (INT32_MAX);
#endif
#endif #endif
} }
@ -135,43 +138,42 @@ void SerialPluginRadio::sendPayload(NodeNum dest, bool wantReplies)
bool SerialPluginRadio::handleReceived(const MeshPacket &mp) bool SerialPluginRadio::handleReceived(const MeshPacket &mp)
{ {
#ifdef NO_ESP32 #ifndef NO_ESP32
#if SERIALPLUGIN_ENABLED == 1 if (radioConfig.preferences.serialplugin_enabled) {
auto &p = mp.decoded.data; auto &p = mp.decoded.data;
// DEBUG_MSG("Received text msg self=0x%0x, from=0x%0x, to=0x%0x, id=%d, msg=%.*s\n", nodeDB.getNodeNum(), // DEBUG_MSG("Received text msg self=0x%0x, from=0x%0x, to=0x%0x, id=%d, msg=%.*s\n", nodeDB.getNodeNum(),
// mp.from, mp.to, mp.id, p.payload.size, p.payload.bytes); // mp.from, mp.to, mp.id, p.payload.size, p.payload.bytes);
if (mp.from == nodeDB.getNodeNum()) { if (mp.from == nodeDB.getNodeNum()) {
/* /*
* If SERIALPLUGIN_ECHO is true, then echo the packets that are sent out back to the TX * If radioConfig.preferences.serialplugin_echo is true, then echo the packets that are sent out back to the TX
* of the serial interface. * of the serial interface.
*/ */
if (SERIALPLUGIN_ECHO) { if (radioConfig.preferences.serialplugin_echo) {
// For some reason, we get the packet back twice when we send out of the radio. // For some reason, we get the packet back twice when we send out of the radio.
// TODO: need to find out why. // TODO: need to find out why.
if (lastRxID != mp.id) { if (lastRxID != mp.id) {
lastRxID = mp.id; lastRxID = mp.id;
// DEBUG_MSG("* * Message came this device\n"); // DEBUG_MSG("* * Message came this device\n");
// Serial2.println("* * Message came this device"); // Serial2.println("* * Message came this device");
Serial2.printf("%s", p.payload.bytes); Serial2.printf("%s", p.payload.bytes);
}
} }
} else {
// DEBUG_MSG("* * Message came from the mesh\n");
// Serial2.println("* * Message came from the mesh");
Serial2.printf("%s", p.payload.bytes);
} }
} else { } else {
// DEBUG_MSG("* * Message came from the mesh\n"); DEBUG_MSG("Serial Plugin Disabled\n");
// Serial2.println("* * Message came from the mesh");
Serial2.printf("%s", p.payload.bytes);
} }
#else
DEBUG_MSG("Serial Plugin Disabled\n");
#endif
#endif #endif
return true; // Let others look at this message also if they want return true; // Let others look at this message also if they want