Repository navigation
Expand file tree
/
Copy pathmultiColouredLedInterface.cpp
More file actions
78 lines (60 loc) · 2 KB
/
Copy pathmultiColouredLedInterface.cpp
File metadata and controls
78 lines (60 loc) · 2 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <RF24/RF24.h>
#include <stdlib.h>
using namespace std;
RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ); // Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 8Mhz
const uint8_t pipes[][6] = {"1Node","2Node"}; // Radio pipe addresses for the 2 nodes to communicate.
void setup(){
radio.begin();
radio.setRetries(15,15);
radio.printDetails(); // Dump the configuration of the rf unit for debugging
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1,pipes[1]);
}
bool sendColour(char *colour){
//This function send a message, the 'action', to the arduino and wait for answer
//Returns true if ACK package is received
radio.stopListening();
unsigned long message = strtoul(colour, NULL, 10);
printf("Now sending %lu...", message);
bool ok = radio.write(&message, sizeof(unsigned long)); //Send the message
if(ok){
printf("ok...");
}else{
printf("failed.\n\r");
}
radio.startListening(); //Listen for ACK
unsigned long started_waiting_at = millis();
bool timeout = false;
while (!radio.available() && !timeout) {
delay(10);
if (millis() - started_waiting_at > 50){
timeout = true;
}
}
if(timeout){
printf("Failed, response timed out.\n\r");
return false;
}else{
// If we received the reply in time, let's read it and print it along with the time taken
unsigned long got_time;
radio.read(&got_time, sizeof(unsigned long));
printf("Got response %lu, round-trip delay: %lu\n\r",got_time,millis()-got_time);
return true;
}
}
int main(int argc, char **argv){
setup(); // run the radio setup
int c;
char *colour = {0}; // initialise colour variable
// get the RGB value from the
while((c = getopt(argc, argv, "v:")) != -1){
colour = optarg;
}
cout << colour << "\n\r"; // print this to confirm read correctly
sendColour(colour); // send RBG value to sensor node
return 0; // exit when complete
}