mirror of
https://github.com/meshtastic/firmware.git
synced 2025-12-19 09:12:45 +00:00
WIP on GPIO example
This commit is contained in:
@@ -8,10 +8,53 @@
|
||||
|
||||
RemoteHardwarePlugin remoteHardwarePlugin;
|
||||
|
||||
bool RemoteHardwarePlugin::handleReceivedProtobuf(const MeshPacket &mp, const HardwareMessage &p)
|
||||
#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)
|
||||
{
|
||||
switch (p.typ) {
|
||||
case HardwareMessage_Type_WRITE_GPIOS:
|
||||
// Print notification to LCD screen
|
||||
screen->print("Write GPIOs");
|
||||
|
||||
return false; // Let others look at this message also if they want
|
||||
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");
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user