-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
61 lines (53 loc) · 1.45 KB
/
main.cpp
File metadata and controls
61 lines (53 loc) · 1.45 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
#include "mbed.h"
#include <chrono>
#include <cstdint>
#include <cstring>
#include "Zigbee.h"
#define BUFFSIZE 64
BufferedSerial pc(USBTX,USBRX, 115200);
Zigbee xbee(PB_9,PB_8); // Zigbee module configured with 115200 baud rate - Change in Zigbee.cpp if required.
char buffer[BUFFSIZE] = {0};
char msgBuff[BUFFSIZE] = {0};
char rcvBuff[BUFFSIZE] = {0};
int counter = 0;
int len = 0;
Thread readThread;
Mutex serialMutex;
void reader()
{
while(1)
{
if(xbee.receiveMessage(rcvBuff))
{
if(serialMutex.trylock_for(chrono::milliseconds(500)))
{
len = snprintf(msgBuff, BUFFSIZE, "\r\n%s", rcvBuff);
pc.write(msgBuff, len);
serialMutex.unlock();
}
}
ThisThread::sleep_for(chrono::milliseconds(10));
}
}
// main() runs in its own thread in the OS
int main()
{
pc.set_format(
/* bits */ 8,
/* parity */ BufferedSerial::None,
/* stop bit */ 1
);
readThread.start(reader);
if(serialMutex.trylock_for(chrono::milliseconds(500)))
{
len = snprintf(buffer, BUFFSIZE, "\r\nHello, I have started :)\r\n");
pc.write(buffer, len);
serialMutex.unlock();
}
while (true) {
len = snprintf(msgBuff, BUFFSIZE, "counter %d", counter);
xbee.sendMessage(msgBuff);
counter++;
ThisThread::sleep_for(chrono::seconds(10));
}
}