2021-06-27 17:56:28 +00:00
|
|
|
#include "configuration.h"
|
2020-12-06 10:33:42 +00:00
|
|
|
#include "RemoteHardwarePlugin.h"
|
|
|
|
#include "MeshService.h"
|
|
|
|
#include "NodeDB.h"
|
|
|
|
#include "RTC.h"
|
|
|
|
#include "Router.h"
|
|
|
|
#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)
|
|
|
|
{
|
2020-12-11 10:29:32 +00:00
|
|
|
for (uint8_t i = 0; i < NUM_GPIOS; i++) {
|
|
|
|
if (mask & (1 << i)) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
pinModes(mask, INPUT_PULLUP);
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < NUM_GPIOS; i++) {
|
|
|
|
uint64_t m = 1 << i;
|
|
|
|
if (mask & m) {
|
|
|
|
if (digitalRead(i))
|
|
|
|
res |= m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoteHardwarePlugin::RemoteHardwarePlugin()
|
2021-04-06 02:34:23 +00:00
|
|
|
: ProtobufPlugin("remotehardware", PortNum_REMOTE_HARDWARE_APP, HardwareMessage_fields), concurrency::OSThread(
|
|
|
|
"remotehardware")
|
2020-12-11 10:29:32 +00:00
|
|
|
{
|
|
|
|
}
|
2020-12-07 02:27:31 +00:00
|
|
|
|
2021-02-17 11:04:41 +00:00
|
|
|
bool RemoteHardwarePlugin::handleReceivedProtobuf(const MeshPacket &req, const HardwareMessage *pptr)
|
2020-12-06 10:33:42 +00:00
|
|
|
{
|
2021-02-17 11:04:41 +00:00
|
|
|
auto p = *pptr;
|
2020-12-13 07:59:26 +00:00
|
|
|
DEBUG_MSG("Received RemoteHardware typ=%d\n", p.typ);
|
|
|
|
|
2020-12-07 02:18:11 +00:00
|
|
|
switch (p.typ) {
|
|
|
|
case HardwareMessage_Type_WRITE_GPIOS:
|
|
|
|
// Print notification to LCD screen
|
2020-12-08 00:16:58 +00:00
|
|
|
screen->print("Write GPIOs\n");
|
2020-12-06 10:33:42 +00:00
|
|
|
|
2020-12-07 02:18:11 +00:00
|
|
|
for (uint8_t i = 0; i < NUM_GPIOS; i++) {
|
|
|
|
uint64_t mask = 1 << i;
|
|
|
|
if (p.gpio_mask & mask) {
|
|
|
|
digitalWrite(i, (p.gpio_value & mask) ? 1 : 0);
|
|
|
|
}
|
|
|
|
}
|
2020-12-11 10:29:32 +00:00
|
|
|
pinModes(p.gpio_mask, OUTPUT);
|
|
|
|
|
2020-12-07 02:18:11 +00:00
|
|
|
break;
|
2020-12-11 10:29:32 +00:00
|
|
|
|
2020-12-07 02:18:11 +00:00
|
|
|
case HardwareMessage_Type_READ_GPIOS: {
|
|
|
|
// Print notification to LCD screen
|
2021-04-06 02:34:23 +00:00
|
|
|
if (screen)
|
2021-03-13 05:14:27 +00:00
|
|
|
screen->print("Read GPIOs\n");
|
2020-12-06 10:33:42 +00:00
|
|
|
|
2020-12-11 10:29:32 +00:00
|
|
|
uint64_t res = digitalReads(p.gpio_mask);
|
2020-12-06 10:33:42 +00:00
|
|
|
|
2020-12-07 02:18:11 +00:00
|
|
|
// Send the reply
|
2021-04-06 02:34:23 +00:00
|
|
|
HardwareMessage r = HardwareMessage_init_default;
|
|
|
|
r.typ = HardwareMessage_Type_READ_GPIOS_REPLY;
|
|
|
|
r.gpio_value = res;
|
|
|
|
MeshPacket *p = allocDataProtobuf(r);
|
2020-12-07 02:18:11 +00:00
|
|
|
setReplyTo(p, req);
|
2021-04-06 02:34:23 +00:00
|
|
|
myReply = p;
|
2020-12-07 02:18:11 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-12-08 00:16:58 +00:00
|
|
|
|
2020-12-11 10:29:32 +00:00
|
|
|
case HardwareMessage_Type_WATCH_GPIOS: {
|
|
|
|
watchGpios = p.gpio_mask;
|
2021-04-06 02:34:23 +00:00
|
|
|
lastWatchMsec = 0; // Force a new publish soon
|
2020-12-11 10:29:32 +00:00
|
|
|
previousWatch = ~watchGpios; // generate a 'previous' value which is guaranteed to not match (to force an initial publish)
|
2021-04-06 02:34:23 +00:00
|
|
|
enabled = true; // Let our thread run at least once
|
2020-12-21 03:13:30 +00:00
|
|
|
DEBUG_MSG("Now watching GPIOs 0x%llx\n", watchGpios);
|
2020-12-11 10:29:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-12-08 00:16:58 +00:00
|
|
|
case HardwareMessage_Type_READ_GPIOS_REPLY:
|
|
|
|
case HardwareMessage_Type_GPIOS_CHANGED:
|
|
|
|
break; // Ignore - we might see our own replies
|
|
|
|
|
2020-12-07 02:18:11 +00:00
|
|
|
default:
|
|
|
|
DEBUG_MSG("Hardware operation %d not yet implemented! FIXME\n", p.typ);
|
|
|
|
break;
|
|
|
|
}
|
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
|
|
|
|
2021-04-06 02:34:23 +00:00
|
|
|
int32_t RemoteHardwarePlugin::runOnce()
|
|
|
|
{
|
|
|
|
if (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;
|
2020-12-21 03:13:30 +00:00
|
|
|
DEBUG_MSG("Broadcasting GPIOS 0x%llx changed!\n", curVal);
|
2020-12-11 10:29:32 +00:00
|
|
|
|
|
|
|
// Something changed! Tell the world with a broadcast message
|
2021-04-06 02:34:23 +00:00
|
|
|
HardwareMessage r = HardwareMessage_init_default;
|
|
|
|
r.typ = HardwareMessage_Type_GPIOS_CHANGED;
|
|
|
|
r.gpio_value = curVal;
|
|
|
|
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
|
|
|
|
enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 200; // Poll our GPIOs every 200ms (FIXME, make adjustable via protobuf arg)
|
|
|
|
}
|