firmware/src/plugins/RemoteHardwarePlugin.cpp

64 lines
1.7 KiB
C++
Raw Normal View History

#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
2020-12-07 02:27:31 +00:00
2020-12-07 02:18:11 +00:00
bool RemoteHardwarePlugin::handleReceivedProtobuf(const MeshPacket &req, const HardwareMessage &p)
{
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-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;
2020-12-08 00:16:58 +00:00
2020-12-07 02:18:11 +00:00
case HardwareMessage_Type_READ_GPIOS: {
// Print notification to LCD screen
2020-12-08 00:16:58 +00:00
screen->print("Read GPIOs\n");
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-07 02:18:11 +00:00
// Send the reply
2020-12-07 02:27:31 +00:00
HardwareMessage reply = HardwareMessage_init_default;
2020-12-07 02:18:11 +00:00
reply.typ = HardwareMessage_Type_READ_GPIOS_REPLY;
reply.gpio_value = res;
MeshPacket *p = allocDataProtobuf(reply);
setReplyTo(p, req);
service.sendToMesh(p);
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;
}
return true; // handled
}