Created
August 22, 2021 13:27
-
-
Save tjclement/2deaa87c48a04812aba883d9f56f5824 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* See documentation at https://nRF24.github.io/RF24 | |
* See License information at root directory of this library | |
* Author: Brendan Doherty (2bndy5), Tom Clement (tjclement) | |
*/ | |
/** | |
* A modified example from the NRF24 library, that cycles through brightness levels of Neewer NL660-2.4 light panels. | |
*/ | |
#include <SPI.h> | |
#include "printf.h" | |
#include "RF24.h" | |
// instantiate an object for the nRF24L01 transceiver | |
RF24 radio(9, 10); // using pin 9 for the CE pin, and pin 10 for the CSN pin | |
char hexChar[2]; | |
uint8_t address[] = {0xD3, 0x5C, 0x6E}; | |
// Used to control whether this node is sending or receiving | |
bool role = true; // true = TX role, false = RX role | |
typedef struct { | |
uint8_t header; | |
uint8_t target_channel; | |
uint8_t unknown1; | |
uint8_t command_type; | |
uint8_t command1; | |
uint8_t command2; | |
uint8_t tail1; | |
uint8_t tail2; | |
uint8_t zeroes[24]; | |
} packet; | |
packet payload = { | |
.header = 0x77, | |
.target_channel = 0x58, | |
.unknown1 = 0x01, | |
.command_type = 0x82, | |
.command1 = 0x00, | |
.command2 = 0x52, | |
.tail1 = 0xBB, | |
.tail2 = 0x01 | |
}; | |
uint8_t brightness = 0; | |
int step = 5; | |
void setup() { | |
Serial.begin(115200); | |
while (!Serial) { | |
// some boards need to wait to ensure access to serial over USB | |
} | |
// initialize the transceiver on the SPI bus | |
if (!radio.begin()) { | |
Serial.println(F("radio hardware is not responding!!")); | |
while (1) {} // hold in infinite loop | |
} | |
// Set the PA Level low to try preventing power supply related problems | |
// because these examples are likely run with nodes in close proximity to | |
// each other. | |
radio.setPALevel(RF24_PA_HIGH); // RF24_PA_MAX is default. | |
// save on transmission time by setting the radio to only transmit the | |
// number of bytes we need to transmit a float | |
radio.setPayloadSize(32); // float datatype occupies 4 bytes | |
radio.setAutoAck(false); | |
radio.setAddressWidth(3); | |
radio.setChannel(10); | |
radio.setDataRate(RF24_250KBPS); | |
radio.setCRCLength(RF24_CRC_16); | |
// set the TX address of the RX node into the TX pipe | |
radio.openWritingPipe(address); // always uses pipe 0 | |
// set the RX address of the TX node into a RX pipe | |
radio.openReadingPipe(1, address); // using pipe 1 | |
// additional setup specific to the node's role | |
if (role) { | |
radio.stopListening(); // put radio in TX mode | |
} else { | |
radio.startListening(); // put radio in RX mode | |
} | |
// For debugging info | |
// printf_begin(); // needed only once for printing details | |
// radio.printDetails(); // (smaller) function that prints raw register values | |
// radio.printPrettyDetails(); // (larger) function that prints human readable data | |
} // setup | |
void loop() { | |
if (role) { | |
// This device is a TX node | |
payload.command1 = brightness; | |
payload.command2 = 0x52 + brightness; | |
unsigned long start_timer = micros(); // start the timer | |
bool report = radio.write(&payload, sizeof(payload)); // transmit & save the report | |
unsigned long end_timer = micros(); // end the timer | |
if (report) { | |
Serial.print(F("Transmission successful! ")); // payload was delivered | |
Serial.print(F("Time to transmit = ")); | |
Serial.print(end_timer - start_timer); // print the timer result | |
Serial.println(F(" us.")); | |
} else { | |
Serial.println(F("Transmission failed or timed out")); // payload was not delivered | |
} | |
brightness += step; | |
if (brightness >= 100) { | |
step = -10; | |
} else if (brightness == 0) { | |
step = 10; | |
} | |
delay(500); | |
} else { | |
// This device is a RX node | |
uint8_t pipe; | |
if (radio.available(&pipe)) { // is there a payload? get the pipe number that recieved it | |
uint8_t* cast_payload = (uint8_t*) &payload; | |
uint8_t bytes = radio.getPayloadSize(); // get the size of the payload | |
radio.read(&payload, bytes); // fetch payload from FIFO | |
Serial.print(F("Received ")); | |
Serial.print(bytes); // print the size of the payload | |
Serial.print(F(" bytes on pipe ")); | |
Serial.print(pipe); // print the pipe number | |
Serial.print(F(": ")); | |
for(int i=0; i<sizeof(payload); i++){ | |
sprintf(hexChar, "%02X", cast_payload[i]); | |
Serial.print(hexChar); | |
} | |
Serial.println(); | |
} | |
} // role | |
if (Serial.available()) { | |
// change the role via the serial monitor | |
char c = toupper(Serial.read()); | |
if (c == 'T' && !role) { | |
// Become the TX node | |
role = true; | |
Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK")); | |
radio.stopListening(); | |
} else if (c == 'R' && role) { | |
// Become the RX node | |
role = false; | |
Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK")); | |
radio.startListening(); | |
} | |
} | |
} // loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment