-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboundary.ino
151 lines (138 loc) · 3.39 KB
/
boundary.ino
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <SoftwareSerial.h>
#include <ArduinoJson.h>
class nodeNameNumberMap
{
public:
char id;
unsigned short num;
nodeNameNumberMap *next = NULL;
};
char myName = 'b';
char myNext[2] = {'a','c'};
unsigned short numNodes = 0;
nodeNameNumberMap *startMap = NULL;
unsigned short adj[6][6] = {{0,1,1,0,0,0},{1,0,0,0,0,0},{1,0,0,0,0,0},{0,0,0,0,0,0},{0,0,0,0,0,0},{0,0,0,0,0,0}};
bool loopDetected = false;
SoftwareSerial radio(10, 11); // RX, TX
const char myId = "b";
unsigned long lastHi = 0;
void addNodeToMap(char nodeid)
{
nodeNameNumberMap *nextitem = startMap;
nodeNameNumberMap *newnode = new nodeNameNumberMap();
newnode->id = nodeid;
newnode->num = ++numNodes;
newnode->next=NULL;
if(startMap == NULL)
{
startMap = newnode;
}
else
{
while(nextitem->next != NULL)
{
nextitem = nextitem->next;
}
nextitem->next = newnode;
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("HardSerial Started.");
// set the data rate for the SoftwareSerial port
radio.begin(9600);
Serial.println("SoftSerial Started.");
//radio.print("{\"source\": \"b\",\"msg\": \"hi\"}");
addNodeToMap(myName);
addNodeToMap('a');
addNodeToMap('c');
radio.println("{\"source\": \"b\",\"msg\": \"ac\"}");
}
void printNodeMap()
{
nodeNameNumberMap *nextitem = startMap;
Serial.print("PrintMap: Map List uptill now: ");
while(nextitem != NULL)
{
Serial.print(nextitem->id);
nextitem = nextitem->next;
}
Serial.println("");
}
void loop() {
// put your main code here, to run repeatedly:
if (radio.available()) {
parsePacket((String)radio.readString()); // assuming we recieve {"source": "a","msg": "fb"}
//Serial.write(radio.read());
}
if (Serial.available()) {
radio.write(Serial.read());
}
}
bool isMyNeighbour(char *nodeid)
{
// Serial.print("isMyNeighbour?:");
// Serial.println(nodeid);
// Serial.print("isMyNeighbour: Comparing with ");
// Serial.print(myNext[0]);
// Serial.print(" and ");
// Serial.println(myNext[1]);
if(String(nodeid).equals(String(myNext[0])) || String(nodeid).equals(String(myNext[1])))
{//Serial.println("isMyNeighbour?: Yes");
return true; }
else
{//Serial.println("isMyNeighbour?: No");
return false; }
}
bool inNodeMap(char *source)
{
//printNodeMap();
nodeNameNumberMap *nextitem = startMap;
if(startMap == NULL)
{
Serial.println("NodeMap Empty");
return false;
}
while(nextitem->next != NULL)
{
// Serial.print("inNodeMap: ");
// Serial.print(String(nextitem->id));
// Serial.print(" and ");
// Serial.println(String(source));
if(String(nextitem->id).equals(String(source)))
{
return true;
}
nextitem = nextitem->next;
}
return false;
}
void parsePacket(String packet)
{
DynamicJsonDocument doc(1024);
deserializeJson(doc, packet);
const char* source = doc["source"];
const char* msg = doc["msg"];
printNodeMap();
if(!String(source).equals(String(myName)) && !isMyNeighbour(source))
{
Serial.println("RX a route packet from afar.");
if(inNodeMap(source))
{
Serial.println("Source is already in NodeMap");
}
}
else if(!String(source).equals(String(myName)) && isMyNeighbour(source))
{
Serial.println("RX from Neigbour");
if(inNodeMap(source))
{
Serial.println("Source is already in NodeMap");
Serial.println(msg[0]);
}
}
}