2022-02-27 08:18:35 +00:00
|
|
|
#include "RemoteHardwareModule.h"
|
2020-12-06 10:33:42 +00:00
|
|
|
#include "MeshService.h"
|
|
|
|
#include "NodeDB.h"
|
|
|
|
#include "RTC.h"
|
|
|
|
#include "Router.h"
|
2023-01-21 13:34:29 +00:00
|
|
|
#include "configuration.h"
|
2020-12-06 10:33:42 +00:00
|
|
|
#include "main.h"
|
|
|
|
|
2020-12-07 02:18:11 +00:00
|
|
|
#define NUM_GPIOS 64
|
|
|
|
|
2020-12-11 10:29:32 +00:00
|
|
|
// Because (FIXME) we currently don't tell API clients status on sent messages
|
|
|
|
// we need to throttle our sending, so that if a gpio is bouncing up and down we
|
2021-04-06 02:34:23 +00:00
|
|
|
// don't generate more messages than the net can send. So we limit watch messages to
|
2020-12-11 10:29:32 +00:00
|
|
|
// a max of one change per 30 seconds
|
|
|
|
#define WATCH_INTERVAL_MSEC (30 * 1000)
|
|
|
|
|
|
|
|
/// Set pin modes for every set bit in a mask
|
2021-04-06 02:34:23 +00:00
|
|
|
static void pinModes(uint64_t mask, uint8_t mode)
|
|
|
|
{
|
2022-01-28 00:11:16 +00:00
|
|
|
for (uint64_t i = 0; i < NUM_GPIOS; i++) {
|
2023-05-03 11:25:03 +00:00
|
|
|
if (mask & (1ULL << i)) {
|
2020-12-11 10:29:32 +00:00
|
|
|
pinMode(i, mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read all the pins mentioned in a mask
|
2021-04-06 02:34:23 +00:00
|
|
|
static uint64_t digitalReads(uint64_t mask)
|
|
|
|
{
|
2020-12-11 10:29:32 +00:00
|
|
|
uint64_t res = 0;
|
|
|
|
|
2023-05-04 02:09:18 +00:00
|
|
|
pinModes(mask, INPUT_PULLUP);
|
2020-12-11 10:29:32 +00:00
|
|
|
|
2022-01-28 00:11:16 +00:00
|
|
|
for (uint64_t i = 0; i < NUM_GPIOS; i++) {
|
2023-05-03 11:25:03 +00:00
|
|
|
uint64_t m = 1ULL << i;
|
2020-12-11 10:29:32 +00:00
|
|
|
if (mask & m) {
|
2022-01-28 00:11:16 +00:00
|
|
|
if (digitalRead(i)) {
|
2020-12-11 10:29:32 +00:00
|
|
|
res |= m;
|
2022-01-28 00:11:16 +00:00
|
|
|
}
|
2020-12-11 10:29:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2022-02-27 10:21:02 +00:00
|
|
|
RemoteHardwareModule::RemoteHardwareModule()
|
2023-01-21 17:39:58 +00:00
|
|
|
: ProtobufModule("remotehardware", meshtastic_PortNum_REMOTE_HARDWARE_APP, &meshtastic_HardwareMessage_msg),
|
|
|
|
concurrency::OSThread("RemoteHardwareModule")
|
2020-12-11 10:29:32 +00:00
|
|
|
{
|
|
|
|
}
|
2020-12-07 02:27:31 +00:00
|
|
|
|
2023-01-21 17:22:19 +00:00
|
|
|
bool RemoteHardwareModule::handleReceivedProtobuf(const meshtastic_MeshPacket &req, meshtastic_HardwareMessage *pptr)
|
2020-12-06 10:33:42 +00:00
|
|
|
{
|
2022-12-29 14:45:49 +00:00
|
|
|
if (moduleConfig.remote_hardware.enabled) {
|
|
|
|
auto p = *pptr;
|
2022-12-30 19:18:19 +00:00
|
|
|
LOG_INFO("Received RemoteHardware typ=%d\n", p.type);
|
2022-12-29 14:45:49 +00:00
|
|
|
|
|
|
|
switch (p.type) {
|
2023-01-21 17:22:19 +00:00
|
|
|
case meshtastic_HardwareMessage_Type_WRITE_GPIOS:
|
2022-12-29 14:45:49 +00:00
|
|
|
// Print notification to LCD screen
|
|
|
|
screen->print("Write GPIOs\n");
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < NUM_GPIOS; i++) {
|
2023-05-03 11:25:03 +00:00
|
|
|
uint64_t mask = 1ULL << i;
|
2022-12-29 14:45:49 +00:00
|
|
|
if (p.gpio_mask & mask) {
|
|
|
|
digitalWrite(i, (p.gpio_value & mask) ? 1 : 0);
|
|
|
|
}
|
2020-12-07 02:18:11 +00:00
|
|
|
}
|
2022-12-29 14:45:49 +00:00
|
|
|
pinModes(p.gpio_mask, OUTPUT);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2023-01-21 17:22:19 +00:00
|
|
|
case meshtastic_HardwareMessage_Type_READ_GPIOS: {
|
2022-12-29 14:45:49 +00:00
|
|
|
// Print notification to LCD screen
|
|
|
|
if (screen)
|
|
|
|
screen->print("Read GPIOs\n");
|
|
|
|
|
|
|
|
uint64_t res = digitalReads(p.gpio_mask);
|
|
|
|
|
|
|
|
// Send the reply
|
2023-01-21 17:22:19 +00:00
|
|
|
meshtastic_HardwareMessage r = meshtastic_HardwareMessage_init_default;
|
|
|
|
r.type = meshtastic_HardwareMessage_Type_READ_GPIOS_REPLY;
|
2022-12-29 14:45:49 +00:00
|
|
|
r.gpio_value = res;
|
|
|
|
r.gpio_mask = p.gpio_mask;
|
2023-01-21 17:22:19 +00:00
|
|
|
meshtastic_MeshPacket *p2 = allocDataProtobuf(r);
|
2022-12-29 14:45:49 +00:00
|
|
|
setReplyTo(p2, req);
|
|
|
|
myReply = p2;
|
|
|
|
break;
|
2020-12-07 02:18:11 +00:00
|
|
|
}
|
2020-12-08 00:16:58 +00:00
|
|
|
|
2023-01-21 17:22:19 +00:00
|
|
|
case meshtastic_HardwareMessage_Type_WATCH_GPIOS: {
|
2022-12-29 14:45:49 +00:00
|
|
|
watchGpios = p.gpio_mask;
|
2023-01-21 13:34:29 +00:00
|
|
|
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
|
2022-12-30 19:18:19 +00:00
|
|
|
LOG_INFO("Now watching GPIOs 0x%llx\n", watchGpios);
|
2022-12-29 14:45:49 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-12-11 10:29:32 +00:00
|
|
|
|
2023-01-21 17:22:19 +00:00
|
|
|
case meshtastic_HardwareMessage_Type_READ_GPIOS_REPLY:
|
|
|
|
case meshtastic_HardwareMessage_Type_GPIOS_CHANGED:
|
2022-12-29 14:45:49 +00:00
|
|
|
break; // Ignore - we might see our own replies
|
2020-12-08 00:16:58 +00:00
|
|
|
|
2022-12-29 14:45:49 +00:00
|
|
|
default:
|
2022-12-30 19:18:19 +00:00
|
|
|
LOG_ERROR("Hardware operation %d not yet implemented! FIXME\n", p.type);
|
2022-12-29 14:45:49 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-12-07 02:18:11 +00:00
|
|
|
}
|
2021-04-06 02:34:23 +00:00
|
|
|
|
|
|
|
return false;
|
2020-12-07 02:18:11 +00:00
|
|
|
}
|
2020-12-11 10:29:32 +00:00
|
|
|
|
2022-02-27 10:21:02 +00:00
|
|
|
int32_t RemoteHardwareModule::runOnce()
|
2021-04-06 02:34:23 +00:00
|
|
|
{
|
2022-12-29 14:45:49 +00:00
|
|
|
if (moduleConfig.remote_hardware.enabled && watchGpios) {
|
2020-12-11 10:29:32 +00:00
|
|
|
uint32_t now = millis();
|
|
|
|
|
2021-04-06 02:34:23 +00:00
|
|
|
if (now - lastWatchMsec >= WATCH_INTERVAL_MSEC) {
|
2020-12-11 10:29:32 +00:00
|
|
|
uint64_t curVal = digitalReads(watchGpios);
|
|
|
|
|
2021-04-06 02:34:23 +00:00
|
|
|
if (curVal != previousWatch) {
|
2020-12-11 10:29:32 +00:00
|
|
|
previousWatch = curVal;
|
2022-12-30 16:27:07 +00:00
|
|
|
LOG_INFO("Broadcasting GPIOS 0x%llx changed!\n", curVal);
|
2020-12-11 10:29:32 +00:00
|
|
|
|
|
|
|
// Something changed! Tell the world with a broadcast message
|
2023-01-21 17:22:19 +00:00
|
|
|
meshtastic_HardwareMessage r = meshtastic_HardwareMessage_init_default;
|
|
|
|
r.type = meshtastic_HardwareMessage_Type_GPIOS_CHANGED;
|
2021-04-06 02:34:23 +00:00
|
|
|
r.gpio_value = curVal;
|
2023-01-21 17:22:19 +00:00
|
|
|
meshtastic_MeshPacket *p = allocDataProtobuf(r);
|
2020-12-11 10:29:32 +00:00
|
|
|
service.sendToMesh(p);
|
|
|
|
}
|
|
|
|
}
|
2021-04-06 02:34:23 +00:00
|
|
|
} else {
|
2020-12-11 10:29:32 +00:00
|
|
|
// No longer watching anything - stop using CPU
|
2022-12-30 00:48:33 +00:00
|
|
|
return disable();
|
2020-12-11 10:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 200; // Poll our GPIOs every 200ms (FIXME, make adjustable via protobuf arg)
|
2022-01-24 18:39:17 +00:00
|
|
|
}
|