-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
88 lines (71 loc) · 1.8 KB
/
main.cpp
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
/*
* Test harness
*/
#include <avr/interrupt.h>
#include <util/delay.h>
#include "nrf24l01.h"
#include "mqttsn/mqttsn-messages.h"
#define TRANSMIT
//#define RECEIVE
#ifdef RECEIVE
#include "receive.h"
#include "transport.h"
#include "mqttsn/mqttsn.h"
class DummyMQ : public virtual IReceive {
public:
DummyMQ(ITransport *delegate) : delegate(delegate) {}
void receive(const uint8_t *data) {
message_header *msg = (message_header*)data;
if(msg->type == CONNECT) {
const msg_connect *con = reinterpret_cast<const msg_connect*>(data);
PORTD |= (1<<PD3); //stage 1 LED lit
if(con->flags == FLAG_CLEAN) {
PORTD |= (1<<PD4); //stage 2 LED lit
}
_delay_ms(500); //hold on
PORTD &= ~( (1<<PD3) | (1<<PD4) ); //turn them off
}
else {
//Error case, flash LED on PD4
for(uint8_t i=0;i<10;++i) {
PORTD ^= (1<<PD4);
_delay_ms(100);
PORTD ^= (1<<PD4);
}
}
}
private:
ITransport *delegate;
};
#endif //RECEIVE
NRF24L01 radio_link;
int main(void) {
#ifdef TRANSMIT
MQTTSN mq(&radio_link);
sei();
for(;;) {
mq.connect(FLAG_CLEAN, 300, "client-1");
_delay_ms(2000);
}
#endif //TRANSMIT
#ifdef RECEIVE
DummyMQ dmq(&radio_link);
radio_link.listen();
sei();
for(;;){} //Interrupts from here on
#endif //RECEIVE
return 0;
}
/*
int main(void) {
MQTTSN protocol(&radio_link);
sei(); //enable interrupts globally
protocol.connect(FLAG_CLEAN, 300, "clientid-1");
//when waiting for messages from base station
radio_link.listen();
return 0;
}
*/
ISR(INT0_vect) {
radio_link.receive();
}