-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomModule.cpp
More file actions
83 lines (66 loc) · 2.23 KB
/
CustomModule.cpp
File metadata and controls
83 lines (66 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "CustomModule.h"
#include "MeshService.h"
#include "NodeDB.h"
#include "PowerFSM.h"
#include "buzz.h"
#include "configuration.h"
#include <cstdio>
#include <regex>
CustomModule *customModule;
ProcessMessage CustomModule::handleReceived(const meshtastic_MeshPacket &mp)
{
// #ifdef DEBUG_PORT
// meshtastic_MeshPacket &p = mp.decoded;
// LOG_INFO("Received text msg from=0x%0x, id=0x%x, msg=%.*s", mp.from, mp.id, p.payload.size, p.payload.bytes);
// #endif
std::string msg = reinterpret_cast<const char *>(mp.decoded.payload.bytes);
std::regex pattern(R"(^(\w+)\s+(\d+)\s+(\d+)$)");
std::smatch matches;
if (std::regex_match(msg, matches, pattern)) {
std::string cmd = matches[1];
int pin = std::stoi(matches[2]);
int val = std::stoi(matches[3]);
if (cmd == "pin") {
if ((availablePins >> pin) & 1)
{
digitalWrite(pin, val);
const char *reply = val ? "PIN ON" : "PIN OFF";
sendReply(reply, mp);
}
else
{
sendReply("Pin not available", mp);
}
}
}
return ProcessMessage::CONTINUE; // Let others look at this message also if they want
}
bool CustomModule::wantPacket(const meshtastic_MeshPacket *p)
{
return MeshService::isTextPayload(p);
}
meshtastic_MeshPacket *CustomModule::allocReply()
{
auto reply = allocDataPacket(); // Allocate a packet for sending
return reply;
}
/**
* Sends a payload to a specified destination node.
*
* @param dest The destination node number.
* @param wantReplies Whether or not to request replies from the destination node.
*/
void CustomModule::sendReply(const char * text, const meshtastic_MeshPacket &req)
{
const meshtastic_Channel *ch = (boundChannel != NULL) ? &channels.getByName(boundChannel) : NULL;
meshtastic_MeshPacket *p = allocReply();
setReplyTo(p, req);
if (ch != NULL) {
p->channel = ch->index;
}
p->decoded.want_response = false;
p->want_ack = true;
p->decoded.payload.size = strlen(text); // You must specify how many bytes are in the reply
memcpy(p->decoded.payload.bytes, text, p->decoded.payload.size);
service->sendToMesh(p);
}