-
Notifications
You must be signed in to change notification settings - Fork 30
/
rn2483.cpp
172 lines (143 loc) · 3.87 KB
/
rn2483.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "WebSocketToSerial.h"
radio_state_e _radio_state;
bool radio_continuous_send = false;
void radioInit(uint32_t baudrate) {
/*
// Close proper
SERIAL_DEVICE.flush();
//SERIAL_DEVICE.end();
// enable auto baud rate (see RN2483 datasheet)
SERIAL_DEVICE.begin(baudrate);
SERIAL_DEVICE.write((byte) 0x00);
SERIAL_DEVICE.flush();
SERIAL_DEVICE.write(0x55);
SERIAL_DEVICE.flush();
SERIAL_DEVICE.write(0x0A);
SERIAL_DEVICE.write(0x0D);
*/
_radio_state = RADIO_IDLE;
}
// Send a radio command
void radioExec( char * cmd) {
// Set to blue immediatly
LedRGBON(COLOR_BLUE, true);
ws.textAll(cmd);
execCommand(NULL, cmd);
_radio_state = RADIO_WAIT_OK;
}
// Send a radio command
void radioExec( PGM_P cmd) {
String str = cmd;
radioExec( str.c_str());
}
// Send data
bool radioSend(uint32_t value) {
if (_radio_state != RADIO_IDLE)
return false;
// Set to BLUE immediatly
LedRGBON(COLOR_BLUE, true);
char cmd[32];
sprintf_P(cmd, PSTR("radio tx %08X"), value);
ws.textAll(cmd);
execCommand(NULL, cmd);
_radio_state = RADIO_WAIT_OK_SEND;
return true;
}
// Put RN2483 into listening mode
void radioListen(void) {
// Idle or previously listening ?
if ( _radio_state==RADIO_IDLE || _radio_state==RADIO_LISTENING ) {
char cmd[32];
// Set receive Watchdog to 1H
//sprintf_P( cmd, PSTR("radio set wdt 3600000"));
//ws.textAll(cmd);
//execCommand(NULL, cmd);
//delay(250);
// Enable Receive mode
sprintf_P( cmd, PSTR("radio rx 0"));
ws.textAll(cmd);
execCommand(NULL, cmd);
// Wait ok listenning
_radio_state = RADIO_WAIT_OK_LISTENING;
}
}
// we received a RN2483 serial response
bool radioResponse(String inputString) {
// got OK
if (inputString == "ok") {
if (_radio_state == RADIO_WAIT_OK_LISTENING) {
_radio_state = RADIO_LISTENING;
} else if (_radio_state == RADIO_WAIT_OK_SEND) {
_radio_state = RADIO_SENDING;
} else {
_radio_state = RADIO_IDLE;
}
// "radio_tx_ok"
} else if (inputString == "radio_tx_ok") {
_radio_state = RADIO_IDLE;
// Set to GREEN immediatly
LedRGBON(COLOR_GREEN, true);
// "radio_rx data"
} else if (inputString.startsWith("radio_rx ")) {
// Stop Animation for being sure to start a full one
LedRGBOFF();
// Set to GREEN immediatly
LedRGBON(COLOR_GREEN, true);
_radio_state = RADIO_RECEIVED_DATA;
// got something to do
return (true);
// radio_err
} else if (inputString == "radio_err") {
// We were listening, restart listen (timed out)
// not really an error
if ( _radio_state == RADIO_LISTENING ) {
radioListen();
} else {
_radio_state = RADIO_ERROR;
// Set to RED immediatly
LedRGBON(COLOR_RED, true);
}
}
return false;
}
// Manage radio state machine
void radioManageState(btn_action_e btn) {
static radio_state_e old_radio_state = _radio_state;
// Action due to push button?
if (btn != BTN_NONE) {
if ( btn == BTN_QUICK_PRESS) {
radioSend(millis()/1000);
} else if ( btn == BTN_PRESSED_12) {
LedRGBON(COLOR_CYAN, true);
radioListen();
} else if ( btn == BTN_PRESSED_23) {
if (!radio_continuous_send) {
LedRGBON(COLOR_MAGENTA, true);
radio_continuous_send = true;
} else {
LedRGBOFF();
radio_continuous_send = false;
}
}
}
// Check radio state changed ?
if (_radio_state != old_radio_state) {
old_radio_state = _radio_state;
// Set Breathing to cyan when listening
if (_radio_state == RADIO_LISTENING ) {
LedRGBON(COLOR_CYAN);
} else if (_radio_state == RADIO_RECEIVED_DATA) {
_radio_state=RADIO_IDLE;
// Listen back
radioListen();
} else if (_radio_state == RADIO_SENDING) {
LedRGBON(COLOR_RED, true);
} else if (_radio_state == RADIO_IDLE) {
//LedRGBOFF();
}
} // Radio state changed
}
// get radio state machine
radio_state_e radioState(void) {
return _radio_state;
}