Fix "watch GPIOs" feature of Remote Hardware module (#3047)

* Fix watch GPIO feature of Remote Hardware

* Add Remote Hardware messages to JSON output

* Add curly brackets

---------

Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
pull/3048/head
GUVWAF 2024-01-03 22:08:28 +01:00 zatwierdzone przez GitHub
rodzic 4577646f8b
commit 943367edd0
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 31 dodań i 5 usunięć

Wyświetl plik

@ -56,7 +56,7 @@ bool RemoteHardwareModule::handleReceivedProtobuf(const meshtastic_MeshPacket &r
LOG_INFO("Received RemoteHardware type=%d\n", p.type);
switch (p.type) {
case meshtastic_HardwareMessage_Type_WRITE_GPIOS:
case meshtastic_HardwareMessage_Type_WRITE_GPIOS: {
// Print notification to LCD screen
screen->print("Write GPIOs\n");
@ -69,6 +69,7 @@ bool RemoteHardwareModule::handleReceivedProtobuf(const meshtastic_MeshPacket &r
pinModes(p.gpio_mask, OUTPUT);
break;
}
case meshtastic_HardwareMessage_Type_READ_GPIOS: {
// Print notification to LCD screen
@ -92,8 +93,9 @@ bool RemoteHardwareModule::handleReceivedProtobuf(const meshtastic_MeshPacket &r
watchGpios = p.gpio_mask;
lastWatchMsec = 0; // Force a new publish soon
previousWatch =
~watchGpios; // generate a 'previous' value which is guaranteed to not match (to force an initial publish)
enabled = true; // Let our thread run at least once
~watchGpios; // generate a 'previous' value which is guaranteed to not match (to force an initial publish)
enabled = true; // Let our thread run at least once
setInterval(2000); // Set a new interval so we'll run soon
LOG_INFO("Now watching GPIOs 0x%llx\n", watchGpios);
break;
}
@ -118,6 +120,7 @@ int32_t RemoteHardwareModule::runOnce()
if (now - lastWatchMsec >= WATCH_INTERVAL_MSEC) {
uint64_t curVal = digitalReads(watchGpios);
lastWatchMsec = now;
if (curVal != previousWatch) {
previousWatch = curVal;
@ -136,5 +139,5 @@ int32_t RemoteHardwareModule::runOnce()
return disable();
}
return 200; // Poll our GPIOs every 200ms (FIXME, make adjustable via protobuf arg)
}
return 2000; // Poll our GPIOs every 2000ms
}

Wyświetl plik

@ -10,6 +10,7 @@
#if defined(ARCH_ESP32)
#include "../mesh/generated/meshtastic/paxcount.pb.h"
#endif
#include "mesh/generated/meshtastic/remote_hardware.pb.h"
#include "sleep.h"
#if HAS_WIFI
#include "mesh/wifi/WiFiAPClient.h"
@ -747,6 +748,28 @@ std::string MQTT::meshPacketToJson(meshtastic_MeshPacket *mp)
break;
}
#endif
case meshtastic_PortNum_REMOTE_HARDWARE_APP: {
meshtastic_HardwareMessage scratch;
meshtastic_HardwareMessage *decoded = NULL;
memset(&scratch, 0, sizeof(scratch));
if (pb_decode_from_bytes(mp->decoded.payload.bytes, mp->decoded.payload.size, &meshtastic_HardwareMessage_msg,
&scratch)) {
decoded = &scratch;
if (decoded->type == meshtastic_HardwareMessage_Type_GPIOS_CHANGED) {
msgType = "gpios_changed";
msgPayload["gpio_value"] = new JSONValue((uint)decoded->gpio_value);
jsonObj["payload"] = new JSONValue(msgPayload);
} else if (decoded->type == meshtastic_HardwareMessage_Type_READ_GPIOS_REPLY) {
msgType = "gpios_read_reply";
msgPayload["gpio_value"] = new JSONValue((uint)decoded->gpio_value);
msgPayload["gpio_mask"] = new JSONValue((uint)decoded->gpio_mask);
jsonObj["payload"] = new JSONValue(msgPayload);
}
} else {
LOG_ERROR("Error decoding protobuf for RemoteHardware message!\n");
}
break;
}
// add more packet types here if needed
default:
break;