2020-12-06 10:33:42 +00:00
|
|
|
#include "RemoteHardwarePlugin.h"
|
|
|
|
#include "MeshService.h"
|
|
|
|
#include "NodeDB.h"
|
|
|
|
#include "RTC.h"
|
|
|
|
#include "Router.h"
|
|
|
|
#include "configuration.h"
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
RemoteHardwarePlugin remoteHardwarePlugin;
|
|
|
|
|
2020-12-07 02:18:11 +00:00
|
|
|
#define NUM_GPIOS 64
|
|
|
|
|
|
|
|
// A macro for clearing a struct, FIXME, move elsewhere
|
|
|
|
#define CLEAR_STRUCT(r) memset(&r, 0, sizeof(r))
|
|
|
|
|
|
|
|
bool RemoteHardwarePlugin::handleReceivedProtobuf(const MeshPacket &req, const HardwareMessage &p)
|
2020-12-06 10:33:42 +00:00
|
|
|
{
|
2020-12-07 02:18:11 +00:00
|
|
|
switch (p.typ) {
|
|
|
|
case HardwareMessage_Type_WRITE_GPIOS:
|
|
|
|
// Print notification to LCD screen
|
|
|
|
screen->print("Write GPIOs");
|
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);
|
|
|
|
pinMode(i, OUTPUT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HardwareMessage_Type_READ_GPIOS: {
|
|
|
|
// Print notification to LCD screen
|
|
|
|
screen->print("Read GPIOs");
|
2020-12-06 10:33:42 +00:00
|
|
|
|
2020-12-07 02:18:11 +00:00
|
|
|
uint64_t res = 0;
|
|
|
|
for (uint8_t i = 0; i < NUM_GPIOS; i++) {
|
|
|
|
uint64_t mask = 1 << i;
|
|
|
|
if (p.gpio_mask & mask) {
|
|
|
|
pinMode(i, INPUT_PULLUP);
|
|
|
|
if (digitalRead(i))
|
|
|
|
res |= (1 << i);
|
|
|
|
}
|
|
|
|
}
|
2020-12-06 10:33:42 +00:00
|
|
|
|
2020-12-07 02:18:11 +00:00
|
|
|
// Send the reply
|
|
|
|
HardwareMessage reply;
|
|
|
|
CLEAR_STRUCT(reply);
|
|
|
|
reply.typ = HardwareMessage_Type_READ_GPIOS_REPLY;
|
|
|
|
reply.gpio_value = res;
|
|
|
|
MeshPacket *p = allocDataProtobuf(reply);
|
|
|
|
setReplyTo(p, req);
|
|
|
|
service.sendToMesh(p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
DEBUG_MSG("Hardware operation %d not yet implemented! FIXME\n", p.typ);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true; // handled
|
|
|
|
}
|