-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
260 lines (203 loc) · 6.19 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "mbed.h"
#include "nRF24L01P.h"
#include <cstdio>
enum { TRANSFER_SIZE = 2 };
char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
char lastMessage[TRANSFER_SIZE];
Timeout timeout;
SPI spi(D11, D12, D13); // mosi, miso, sclk
DigitalOut cs(D10, 1);
const int leds[6] = {0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0xFE};
int currentLedIndex = 0;
const int NUMBER_OF_LEDS = 6;
bool isNextButtonClicked = false;
bool isBackButtonClicked = false;
static EventQueue event_queue(/* event count */ 10 * EVENTS_EVENT_SIZE);
BufferedSerial pc(USBTX, USBRX); // tx, rx
nRF24L01P my_nrf24l01p(D11, D12, D13, D8, D9, D7); // mosi, miso, sck, csn, ce, irq
FileHandle *mbed::mbed_override_console(int fd)
{
return &pc;
}
int SPIread() {
cs.write(0);
spi.write(0x47);
spi.write(9);
unsigned int value = spi.write(0x0);
cs.write(1);
return value;
}
void writeLed(int nextLedIndex) {
if (nextLedIndex >= NUMBER_OF_LEDS) {
nextLedIndex = 0;
} else if (nextLedIndex < 0) {
nextLedIndex = NUMBER_OF_LEDS - 1;
}
currentLedIndex = nextLedIndex;
cs = 0;
spi.write(0x46);
spi.write(0x0);
spi.write(leds[nextLedIndex]);
cs = 1;
}
char updateLEDChar(int ledIndex){
switch (ledIndex) {
case 0:
return '0';
case 1:
return '1';
case 2:
return '2';
case 3:
return '3';
case 4:
return '4';
case 5:
return '5';
default:
//updateLEDChar is called before write led can catch out of bounds indexs
if(ledIndex == 6){
return '0';
}else{
return '5';
}
}
}
void updateLEDIndex(char ledIndex){
switch (ledIndex) {
case '0':
writeLed(0);
break;
case '1':
writeLed(1);
break;
case '2':
writeLed(2);
break;
case '3':
writeLed(3);
break;
case '4':
writeLed(4);
break;
case '5':
writeLed(5);
break;
}
}
// class TransmitData{
// public:
// TransmitData(){
// }
void storeLastMessage(char message[TRANSFER_SIZE]){
lastMessage[0] = message[0];
lastMessage[1] = message[1];
}
void sendData(char message[TRANSFER_SIZE]){
storeLastMessage(message);
my_nrf24l01p.write( NRF24L01P_PIPE_P0, message, TRANSFER_SIZE );
}
void sendLastMessage(){
my_nrf24l01p.write( NRF24L01P_PIPE_P0, lastMessage, TRANSFER_SIZE );
}
// private:
// };
void ledLoop() {
int currentValue = SPIread();
if (!isNextButtonClicked && (currentValue & 128) == 0) {
isNextButtonClicked = true;
currentLedIndex += 1;
txData[0] = 'C';
// txData[1] = '2';
txData[1] = updateLEDChar(currentLedIndex);
sendData(txData);
timeout.attach(callback(&sendLastMessage), 2.0);
printf( "Increase!\r\n");
// writeLed(currentLedIndex);
} else if (!isBackButtonClicked && (currentValue & 64) == 0) {
isBackButtonClicked = true;
currentLedIndex -= 1;
txData[0] = 'C';
txData[1] = updateLEDChar(currentLedIndex);
sendData(txData);
timeout.attach(callback(&sendLastMessage), 2.0);
printf( "Decrease!\r\n");
// writeLed(currentLedIndex);
} else if ((currentValue & 64) != 0 && (currentValue & 128) != 0) {
isNextButtonClicked = false;
isBackButtonClicked = false;
}
}
void checkReceivedMessage(char message[TRANSFER_SIZE]) {
//printf("%s", message);
char commandType = message[0];
char ledIndex = message[1];
printf("Recived message: ");
//Check for command
if(commandType == 'C'){
updateLEDIndex(ledIndex);
pc.write(&message[0], TRANSFER_SIZE);
// storeLastMessage(message);
// ThisThread::sleep_for(300);
message[0] = 'A';
message[1] = ledIndex;
my_nrf24l01p.write( NRF24L01P_PIPE_P0, message, TRANSFER_SIZE );
}else if(commandType == 'A'){
timeout.detach();
updateLEDIndex(ledIndex);
pc.write(&message[0], TRANSFER_SIZE);
}
printf("\r\n");
//Check index
// if (ledIndex == '1') {
// writeLed(++currentLedIndex);
// // pc.write(&message[0], TRANSFER_SIZE);
// pc.write("Increase", sizeof("Increase"));
// }
// else if (ledIndex == '2') {
// writeLed(--currentLedIndex);
// pc.write("Decrease", sizeof("Decrease"));
// }
}
int main() {
spi.format(8, 0);
cs = 0;
spi.write(0x46);
spi.write(6);
spi.write(0x80 | 0x40);
cs = 1;
cs = 0;
spi.write(0x46);
spi.write(0);
spi.write(0xDF);
cs = 1;
my_nrf24l01p.setTxAddress(0xC74FB7CDC8);
my_nrf24l01p.setRxAddress(0xC74FB7CDC7);
my_nrf24l01p.powerUp();
// Display the (default) setup of the nRF24L01+ chip
printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() );
printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() );
printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
printf( "Type keys to test transfers:\r\n");
my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
my_nrf24l01p.setRfFrequency(NRF24L01P_MAX_RF_FREQUENCY);
my_nrf24l01p.setReceiveMode();
my_nrf24l01p.enable();
while (1) {
ledLoop();
// If we've received anything in the nRF24L01+...
if ( my_nrf24l01p.readable() ) {
// ...read the data into the receive buffer
my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) );
// Display the receive buffer contents via the host serial link
// pc.write(&rxData[0], sizeof( rxData ));
checkReceivedMessage(&rxData[0]);
/**
* Check for acknowledgment.
* Then update LED position.
**/
}
}
}