diff --git a/.github/workflows/build_arduino.yml b/.github/workflows/build_arduino.yml new file mode 100644 index 00000000..f9e96c4b --- /dev/null +++ b/.github/workflows/build_arduino.yml @@ -0,0 +1,90 @@ +name: Arduino CLI build + +on: + pull_request: + paths: + - ".github/workflows/build_arduino.yml" + - "examples/**" + + push: + paths: + - ".github/workflows/build_arduino.yml" + - "examples/**" + +jobs: + check_formatting: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Check code formatting + uses: per1234/artistic-style-action@main + with: + options-file-path: ./examples/examples_formatter.conf + name-patterns: | + - '*.ino' + - '*.cpp' + - '*.hpp' + - '*.h' + target-paths: | + - examples + build: + needs: check_formatting + runs-on: ubuntu-latest + + strategy: + fail-fast: false + + matrix: + fqbn: + - "arduino:avr:yun" + - "arduino:avr:uno" + - "arduino:avr:diecimila" + - "arduino:avr:nano" + - "arduino:avr:mega" + - "arduino:avr:megaADK" + - "arduino:avr:leonardo" + - "arduino:avr:micro" + - "arduino:avr:esplora" + - "arduino:avr:mini" + - "arduino:avr:ethernet" + - "arduino:avr:fio" + - "arduino:avr:bt" + # - "arduino:avr:LilyPad" # board not found + - "arduino:avr:LilyPadUSB" + - "arduino:avr:pro" + - "arduino:avr:atmegang" + - "arduino:avr:robotControl" + # - "arduino:avr:gemma" # does not support SPI + - "arduino:avr:circuitplay32u4cat" + - "arduino:avr:yunmini" + - "arduino:avr:chiwawa" + - "arduino:avr:one" + - "arduino:avr:unowifi" + - "arduino:mbed:nano33ble" + - "arduino:samd:mkr1000" + - "arduino:samd:mkrzero" + - "arduino:samd:mkrwifi1010" + - "arduino:samd:nano_33_iot" + - "arduino:samd:mkrfox1200" + - "arduino:samd:mkrwan1300" + - "arduino:samd:mkrwan1310" + - "arduino:samd:mkrgsm1400" + - "arduino:samd:mkrnb1500" + - "arduino:samd:mkrvidor4000" + - "arduino:samd:adafruit_circuitplayground_m0" + - "arduino:samd:mzero_pro_bl" + - "arduino:samd:mzero_bl" + - "arduino:samd:tian" + - "arduino:megaavr:uno2018" + # - "arduino:megaavr:nano4809" # board not found + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Compile examples + uses: arduino/compile-sketches@main + with: + fqbn: ${{ matrix.fqbn }} diff --git a/Sync.cpp b/Sync.cpp deleted file mode 100644 index 62f4fe07..00000000 --- a/Sync.cpp +++ /dev/null @@ -1,100 +0,0 @@ -/* - Copyright (C) 2011 J. Coliz - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - version 2 as published by the Free Software Foundation. - */ - -// STL headers -// C headers -#include - -#if defined(__AVR_ATxmega64D3__) || defined(__AVR_ATxmega128D3__) || defined(__AVR_ATxmega192D3__) || defined(__AVR_ATxmega256D3__) || defined(__AVR_ATxmega384D3__) /* Could not find the previous definition so it is redefined here */ - #define XMEGA - #define XMEGA_D3 - #include "RF24Network.h" - #include "Sync.h" -#else - // Framework headers - // Library headers - #include - // Project headers - // This component's header - #include -#endif -/****************************************************************************/ - -void Sync::update(void) -{ - // Pump the network - network.update(); - - // Look for changes to the data - uint8_t message[32]; - uint8_t *mptr = message; - unsigned at = 0; - while ( at < len ) - { - if ( app_data && internal_data && app_data[at] != internal_data[at] ) - { - // Compose a message with the deltas - *mptr++ = at + 1; - *mptr++ = app_data[at]; - - // Update our internal view - internal_data[at] = app_data[at]; - } - ++at; - } - // Zero out the remainder - while ( at++ < sizeof(message) ) - *mptr++ = 0; - - // If changes, send a message - if ( *message ) - { - // TODO handle the case where this has to be broken into - // multiple messages - RF24NetworkHeader header(/*to node*/ to_node, /*type*/ 'S' /*Sync*/); - network.write(header,message,sizeof(message)); - } - - // Look for messages from the network - // Is there anything ready for us? - if ( network.available() ) - { - // If so, take a look at it - RF24NetworkHeader header; - network.peek(header); - - switch (header.type) - { - case 'S': - IF_SERIAL_DEBUG(printf_P(PSTR("%lu: SYN Received sync message\n\r"),millis())); - - network.read(header,&message,sizeof(message)); - // Parse the message and update the vars - mptr = message; - at = 0; - while ( mptr < message + sizeof(message) ) - { - // A '0' in the first position means we are done - if ( !*mptr ) - break; - uint8_t pos = (*mptr++) - 1; - uint8_t val = *mptr++; - - IF_SERIAL_DEBUG(printf_P(PSTR("%lu: SYN Updated position %u to value %u\n\r"),millis(),pos,val)); - - app_data[pos] = val; - internal_data[pos] = val; - } - break; - default: - // Leave other messages for the app - break; - }; - } -} -// vim:cin:ai:sts=2 sw=2 ft=cpp diff --git a/Sync.h b/Sync.h deleted file mode 100644 index 7378b8b1..00000000 --- a/Sync.h +++ /dev/null @@ -1,89 +0,0 @@ -/* - Copyright (C) 2011 J. Coliz - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - version 2 as published by the Free Software Foundation. - */ - -#ifndef __SYNC_H__ -#define __SYNC_H__ - -// STL headers -// C headers -#include -#include -// Framework headers -// Library headers -#if defined(XMEGA_D3) - #include "RF24Network_config.h" -#else - #include -#endif -// Project headers - -class RF24Network; - -/** - * Synchronizes a shared set of variables between multiple nodes - */ - -class Sync -{ -private: - RF24Network& network; - uint8_t* app_data; /**< Application's copy of the data */ - uint8_t* internal_data; /**< Our copy of the data */ - size_t len; /**< Length of the data in bytes */ - uint16_t to_node; /**< The other node we're syncing with */ - -protected: -public: - /** - * Constructor - * - * @param _network Which network to syncrhonize over - */ - Sync(RF24Network& _network): network(_network), app_data(NULL), - internal_data(NULL), len(0), to_node(0) - { - } - /** - * Begin the object - * - * @param _to_node Which node we are syncing with - */ - void begin(uint16_t _to_node) - { - to_node = _to_node; - } - /** - * Declare the shared data set - * - * @param _data Location of shared data to be syncrhonized - */ - template - void register_me(T& _data) - { - app_data = reinterpret_cast(&_data); - len = sizeof(_data); - internal_data = reinterpret_cast(malloc(len)); - reset(); - } - - /** - * Reset the internal copy of the shared data set - */ - void reset(void) - { - memcpy(internal_data,app_data,len); - } - - /** - * Update the network and the shared data set - */ - void update(void); -}; - -#endif // __SYNC_H__ -// vim:cin:ai:sts=2 sw=2 ft=cpp diff --git a/examples/AStyle.exe b/examples/AStyle.exe new file mode 100644 index 00000000..2dc842c2 Binary files /dev/null and b/examples/AStyle.exe differ diff --git a/examples/Network_Ping/Network_Ping.ino b/examples/Network_Ping/Network_Ping.ino index 049bf4a3..f9c1fb45 100644 --- a/examples/Network_Ping/Network_Ping.ino +++ b/examples/Network_Ping/Network_Ping.ino @@ -1,15 +1,15 @@ -/* - Copyright (C) 2011 James Coliz, Jr. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - version 2 as published by the Free Software Foundation. +/** + * Copyright (C) 2011 James Coliz, Jr. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. */ /** * Example: Network topology, and pinging across a tree/mesh network * - * Using this sketch, each node will send a ping to every other node in the network every few seconds. + * Using this sketch, each node will send a ping to every other node in the network every few seconds. * The RF24Network library will route the message across the mesh to the correct node. * * This sketch is greatly complicated by the fact that at startup time, each @@ -23,7 +23,7 @@ * Update: The logical node address of each node is set below, and are grouped in twos for demonstration. * Number 0 is the master node. Numbers 1-2 represent the 2nd layer in the tree (02,05). * Number 3 (012) is the first child of number 1 (02). Number 4 (015) is the first child of number 2. - * Below that are children 5 (022) and 6 (025), and so on as shown below + * Below that are children 5 (022) and 6 (025), and so on as shown below * The tree below represents the possible network topology with the addresses defined lower down * * Addresses/Topology Node Numbers (To simplify address assignment in this demonstration) @@ -42,10 +42,10 @@ */ #include -#include -#include -#include #include "printf.h" +#include +#include +#include /*********************************************************************** ************* Set the Node Address ************************************* @@ -53,13 +53,13 @@ // These are the Octal addresses that will be assigned const uint16_t node_address_set[10] = { 00, 02, 05, 012, 015, 022, 025, 032, 035, 045 }; - + // 0 = Master -// 1-2 (02,05) = Children of Master(00) -// 3,5 (012,022) = Children of (02) -// 4,6 (015,025) = Children of (05) -// 7 (032) = Child of (02) -// 8,9 (035,045) = Children of (05) +// 1-2 (02, 05) = Children of Master(00) +// 3,5 (012, 022) = Children of (02) +// 4,6 (015, 025) = Children of (05) +// 7 (032) = Child of (02) +// 8,9 (035, 045) = Children of (05) uint8_t NODE_ADDRESS = 0; // Use numbers 0 through to select an address from the array @@ -67,8 +67,8 @@ uint8_t NODE_ADDRESS = 0; // Use numbers 0 through to select an address from th /***********************************************************************/ -RF24 radio(7,8); // CE & CS pins to use (Using 7,8 on Uno,Nano) -RF24Network network(radio); +RF24 radio(7, 8); // CE & CS pins to use (Using 7,8 on Uno,Nano) +RF24Network network(radio); uint16_t this_node; // Our node address @@ -89,152 +89,178 @@ void handle_N(RF24NetworkHeader& header); void add_node(uint16_t node); -void setup(){ - +void setup() { + Serial.begin(115200); printf_begin(); - printf_P(PSTR("\n\rRF24Network/examples/meshping/\n\r")); + if (!Serial) { + // some boards need this because of native USB capability + } + Serial.println(F("RF24Network/examples/meshping/")); this_node = node_address_set[NODE_ADDRESS]; // Which node are we? - + SPI.begin(); // Bring up the RF network - radio.begin(); + if (!radio.begin()) { + Serial.println(F("Radio hardware not responding!")); + while (1) { + // hold in infinite loop + } + } radio.setPALevel(RF24_PA_HIGH); network.begin(/*channel*/ 100, /*node address*/ this_node ); } -void loop(){ - +void loop() { + network.update(); // Pump the network regularly - while ( network.available() ) { // Is there anything ready for us? - + while (network.available()) { // Is there anything ready for us? + RF24NetworkHeader header; // If so, take a look at it network.peek(header); - - switch (header.type){ // Dispatch the message to the correct handler. - case 'T': handle_T(header); break; - case 'N': handle_N(header); break; - default: printf_P(PSTR("*** WARNING *** Unknown message type %c\n\r"),header.type); - network.read(header,0,0); - break; - }; - } - + switch (header.type) { // Dispatch the message to the correct handler. + case 'T': + handle_T(header); + break; + case 'N': + handle_N(header); + break; + default: + Serial.print(F("*** WARNING *** Unknown message type "); + Serial.println(header.type); + network.read(header, 0, 0); + break; + }; + } + + unsigned long now = millis(); // Send a ping to the next node every 'interval' ms - if ( now - last_time_sent >= interval ){ + if (now - last_time_sent >= interval) { last_time_sent = now; uint16_t to = 00; // Who should we send to? By default, send to base - - - if ( num_active_nodes ){ // Or if we have active nodes, - to = active_nodes[next_ping_node_index++]; // Send to the next active node - if ( next_ping_node_index > num_active_nodes ){ // Have we rolled over? - next_ping_node_index = 0; // Next time start at the beginning - to = 00; // This time, send to node 00. - } + + + if (num_active_nodes) { // Or if we have active nodes, + to = active_nodes[next_ping_node_index++]; // Send to the next active node + if (next_ping_node_index > num_active_nodes) { // Have we rolled over? + next_ping_node_index = 0; // Next time start at the beginning + to = 00; // This time, send to node 00. + } } bool ok; - - if ( this_node > 00 || to == 00 ){ // Normal nodes send a 'T' ping - ok = send_T(to); - }else{ // Base node sends the current active nodes out - ok = send_N(to); + if (this_node > 00 || to == 00) { // Normal nodes send a 'T' ping + ok = send_T(to); + } else { // Base node sends the current active nodes out + ok = send_N(to); } - - if (ok){ // Notify us of the result - printf_P(PSTR("%lu: APP Send ok\n\r"),millis()); - }else{ - printf_P(PSTR("%lu: APP Send failed\n\r"),millis()); - last_time_sent -= 100; // Try sending at a different time next time + + if (ok) { // Notify us of the result + Serial.print(millis()); + Serial.println(F(": APP Send ok")); + } else { + Serial.print(millis()); + Serial.println(F(": APP Send failed")); + last_time_sent -= 100; // Try sending at a different time next time } } -// delay(50); // Delay to allow completion of any serial printing -// if(!network.available()){ -// network.sleepNode(2,0); // Sleep this node for 2 seconds or a payload is received (interrupt 0 triggered), whichever comes first -// } + // delay(50); // Delay to allow completion of any serial printing + // if(!network.available()){ + // network.sleepNode(2,0); // Sleep this node for 2 seconds or a payload is received (interrupt 0 triggered), whichever comes first + // } } /** * Send a 'T' message, the current time */ -bool send_T(uint16_t to) -{ +bool send_T(uint16_t to) { RF24NetworkHeader header(/*to node*/ to, /*type*/ 'T' /*Time*/); - + // The 'T' message that we send is just a ulong, containing the time unsigned long message = millis(); - printf_P(PSTR("---------------------------------\n\r")); - printf_P(PSTR("%lu: APP Sending %lu to 0%o...\n\r"),millis(),message,to); - return network.write(header,&message,sizeof(unsigned long)); + Serial.println(F("---------------------------------")); + Serial.print(millis()); + Serial.print(F(": APP Sending ")); + Serial.print(message); + Serial.print(F(" to ")); + Serial.print(to); + Serial.println(F("...")); + return network.write(header, &message, sizeof(unsigned long)); } /** * Send an 'N' message, the active node list */ -bool send_N(uint16_t to) -{ +bool send_N(uint16_t to) { RF24NetworkHeader header(/*to node*/ to, /*type*/ 'N' /*Time*/); - - printf_P(PSTR("---------------------------------\n\r")); - printf_P(PSTR("%lu: APP Sending active nodes to 0%o...\n\r"),millis(),to); - return network.write(header,active_nodes,sizeof(active_nodes)); + + Serial.println(F("---------------------------------")); + Serial.print(millis()); + Serial.print(F(": APP Sending active nodes to ")); + Serial.print(to); + Serial.println(F("...")); + return network.write(header, active_nodes, sizeof(active_nodes)); } /** * Handle a 'T' message * Add the node to the list of active nodes */ -void handle_T(RF24NetworkHeader& header){ +void handle_T(RF24NetworkHeader& header) { unsigned long message; // The 'T' message is just a ulong, containing the time - network.read(header,&message,sizeof(unsigned long)); - printf_P(PSTR("%lu: APP Received %lu from 0%o\n\r"),millis(),message,header.from_node); - - - if ( header.from_node != this_node || header.from_node > 00 ) // If this message is from ourselves or the base, don't bother adding it to the active nodes. - add_node(header.from_node); + network.read(header, &message, sizeof(unsigned long)); + Serial.print(millis()); + Serial.print(F(": APP Received "); + Serial.print(message); + Serial.print(" from "); + Serial.println(header.from_node); + + if (header.from_node != this_node || header.from_node > 00) // If this message is from ourselves or the base, don't bother adding it to the active nodes. + add_node(header.from_node); } /** * Handle an 'N' message, the active node list */ -void handle_N(RF24NetworkHeader& header) -{ +void handle_N(RF24NetworkHeader& header) { static uint16_t incoming_nodes[max_active_nodes]; - network.read(header,&incoming_nodes,sizeof(incoming_nodes)); - printf_P(PSTR("%lu: APP Received nodes from 0%o\n\r"),millis(),header.from_node); + network.read(header, &incoming_nodes, sizeof(incoming_nodes)); + Serial.print(millis()); + Serial.print(F(": APP Received nodes from "); + Serial.println(header.from_node); - int i = 0; - while ( i < max_active_nodes && incoming_nodes[i] > 00 ) - add_node(incoming_nodes[i++]); + int i = 0; + while ( i < max_active_nodes && incoming_nodes[i] > 00 ) + add_node(incoming_nodes[i++]); } /** * Add a particular node to the current list of active nodes */ -void add_node(uint16_t node){ - - short i = num_active_nodes; // Do we already know about this node? - while (i--) { - if ( active_nodes[i] == node ) - break; +void add_node(uint16_t node) { + + short i = num_active_nodes; // Do we already know about this node? + while (i--) { + if (active_nodes[i] == node) + break; } - - if ( i == -1 && num_active_nodes < max_active_nodes ){ // If not, add it to the table - active_nodes[num_active_nodes++] = node; - printf_P(PSTR("%lu: APP Added 0%o to list of active nodes.\n\r"),millis(),node); + + if (i == -1 && num_active_nodes < max_active_nodes) { // If not, add it to the table + active_nodes[num_active_nodes++] = node; + Serial.print(millis()); + Serial.print(F(": APP Added ")); + Serial.print(node); + Serial.println(F(" to list of active nodes.")); } } - - diff --git a/examples/Network_Ping_Sleep/Network_Ping_Sleep.ino b/examples/Network_Ping_Sleep/Network_Ping_Sleep.ino index 4ec8c897..54a41490 100644 --- a/examples/Network_Ping_Sleep/Network_Ping_Sleep.ino +++ b/examples/Network_Ping_Sleep/Network_Ping_Sleep.ino @@ -1,17 +1,17 @@ -/* - Copyright (C) 2011 James Coliz, Jr. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - version 2 as published by the Free Software Foundation. - - 2014 - TMRh20: New sketch included with updated library +/** + * Copyright (C) 2011 James Coliz, Jr. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * 2014 - TMRh20: New sketch included with updated library */ /** * Example: Network topology, and pinging across a tree/mesh network with sleeping nodes * - * Using this sketch, each node will send a ping to every other node in the network every few seconds. + * Using this sketch, each node will send a ping to every other node in the network every few seconds. * The RF24Network library will route the message across the mesh to the correct node. * * This sketch demonstrates the new functionality of nodes sleeping in STANDBY-I mode. In receive mode, @@ -20,8 +20,8 @@ * * How it Works: * The enhanced sleep mode utilizes the ACK payload functionality, as radios that are in Primary Transmitter - * mode (PTX) are able to receive ACK payloads while in STANDBY-I mode. - * 1. The radio is configured to use Dynamic Payloads and ACK payloads with Auto-Ack enabled + * mode (PTX) are able to receive ACK payloads while in STANDBY-I mode. + * 1. The radio is configured to use Dynamic Payloads and ACK payloads with Auto-Ack enabled * 2. The radio enters PTX mode and attaches an interrupt handler to the radio interrupt input pin (pin 2) * 3. The radio uses the Watchdog Timer to awake at set 1 second intervals in this example * 4. Every interval, it sends out a 'sleep' payload and goes back to sleep. Incoming payloads will then be treated as ACK payloads, while the radio remains in STANDBY-I mode. @@ -33,12 +33,12 @@ */ #include -#include -#include -#include -#include "printf.h" #include #include +#include "printf.h" +#include +#include +#include /*********************************************************************** ************* Set the Node Address ************************************* @@ -48,11 +48,11 @@ const uint16_t node_address_set[10] = { 00, 02, 05, 012, 015, 022, 025, 032, 035, 045 }; // 0 = Master -// 1-2 (02,05) = Children of Master(00) -// 3,5 (012,022) = Children of (02) -// 4,6 (015,025) = Children of (05) -// 7 (032) = Child of (02) -// 8,9 (035,045) = Children of (05) +// 1-2 (02, 05) = Children of Master(00) +// 3, 5 (012, 022) = Children of (02) +// 4, 6 (015, 025) = Children of (05) +// 7 (032) = Child of (02) +// 8, 9 (035, 045) = Children of (05) uint8_t NODE_ADDRESS = 1; // Use numbers 0 through 9 to select an address from the array @@ -60,8 +60,8 @@ uint8_t NODE_ADDRESS = 1; // Use numbers 0 through 9 to select an address from t /***********************************************************************/ -RF24 radio(7,8); // CE & CS pins to use (Using 7,8 on Uno,Nano) -RF24Network network(radio); +RF24 radio(7, 8); // CE & CS pins to use (Using 7,8 on Uno,Nano) +RF24Network network(radio); uint16_t this_node; // Our node address @@ -87,97 +87,112 @@ typedef enum { wdt_16ms = 0, wdt_32ms, wdt_64ms, wdt_128ms, wdt_250ms, wdt_500ms unsigned long awakeTime = 500; // How long in ms the radio will stay awake after leaving sleep mode unsigned long sleepTimer = 0; // Used to keep track of how long the system has been awake -void setup(){ - +void setup() { + Serial.begin(115200); printf_begin(); - printf_P(PSTR("\n\rRF24Network/examples/meshping/\n\r")); + if (!Serial) { + // some boards need this because of native USB capability + } + Serial.println(F("RF24Network/examples/meshping/")); this_node = node_address_set[NODE_ADDRESS]; // Which node are we? - + SPI.begin(); // Bring up the RF network - radio.begin(); + if (!radio.begin()) { + Serial.println(F("Radio hardware not responding!")); + while (1) { + // hold in infinite loop + } + } radio.setPALevel(RF24_PA_HIGH); network.begin(/*channel*/ 100, /*node address*/ this_node ); -/******************************** This is the configuration for sleep mode ***********************/ + /******************************** This is the configuration for sleep mode ***********************/ network.setup_watchdog(wdt_1s); //The watchdog timer will wake the MCU and radio every second to send a sleep payload, then go back to sleep } -void loop(){ - +void loop() { + network.update(); // Pump the network regularly - while ( network.available() ) { // Is there anything ready for us? - + while (network.available()) { // Is there anything ready for us? + RF24NetworkHeader header; // If so, take a look at it network.peek(header); - - switch (header.type){ // Dispatch the message to the correct handler. - case 'T': handle_T(header); break; - case 'N': handle_N(header); break; - - /************* SLEEP MODE *********/ - // Note: A 'sleep' header has been defined, and should only need to be ignored if a node is routing traffic to itself - // The header is defined as: RF24NetworkHeader sleepHeader(/*to node*/ 00, /*type*/ 'S' /*Sleep*/); - case 'S': /*This is a sleep payload, do nothing*/ break; - - default: printf_P(PSTR("*** WARNING *** Unknown message type %c\n\r"),header.type); - network.read(header,0,0); - break; - }; - } - -/***************************** CALLING THE NEW SLEEP FUNCTION ************************/ - - if(millis() - sleepTimer > awakeTime&& NODE_ADDRESS){ // Want to make sure the Arduino stays awake for a little while when data comes in. Do NOT sleep if master node. - Serial.println("Sleep"); - sleepTimer = millis(); // Reset the timer value - delay(100); // Give the Serial print some time to finish up - radio.stopListening(); // Switch to PTX mode. Payloads will be seen as ACK payloads, and the radio will wake up - network.sleepNode(8,0); // Sleep the node for 8 cycles of 1second intervals - Serial.println("Awake"); + + switch (header.type) { // Dispatch the message to the correct handler. + case 'T': + handle_T(header); + break; + case 'N': + handle_N(header); + break; + + /************* SLEEP MODE *********/ + // Note: A 'sleep' header has been defined, and should only need to be ignored if a node is routing traffic to itself + // The header is defined as: RF24NetworkHeader sleepHeader(/*to node*/ 00, /*type*/ 'S' /*Sleep*/); + case 'S': + /*This is a sleep payload, do nothing*/ + break; + + default: + Serial.print(F("*** WARNING *** Unknown message type "); + Serial.println(header.type); + network.read(header, 0, 0); + break; + }; + } + + /***************************** CALLING THE NEW SLEEP FUNCTION ************************/ + + if (millis() - sleepTimer > awakeTime && NODE_ADDRESS) { // Want to make sure the Arduino stays awake for a little while when data comes in. Do NOT sleep if master node. + Serial.println(F("Sleep")); + sleepTimer = millis(); // Reset the timer value + delay(100); // Give the Serial print some time to finish up + radio.stopListening(); // Switch to PTX mode. Payloads will be seen as ACK payloads, and the radio will wake up + network.sleepNode(8, 0); // Sleep the node for 8 cycles of 1second intervals + Serial.println(F("Awake")); } - - //Examples: - // network.sleepNode( cycles, interrupt-pin ); - // network.sleepNode(0,0); // The WDT is configured in this example to sleep in cycles of 1 second. This will sleep 1 second, or until a payload is received - // network.sleepNode(1,255); // Sleep this node for 1 second. Do not wake up until then, even if a payload is received ( no interrupt ) Payloads will be lost. - - /**** end sleep section ***/ - - + + //Examples: + // network.sleepNode( cycles, interrupt-pin ); + // network.sleepNode(0,0); // The WDT is configured in this example to sleep in cycles of 1 second. This will sleep 1 second, or until a payload is received + // network.sleepNode(1,255); // Sleep this node for 1 second. Do not wake up until then, even if a payload is received ( no interrupt ) Payloads will be lost. + + /**** end sleep section ***/ + + unsigned long now = millis(); // Send a ping to the next node every 'interval' ms - if ( now - last_time_sent >= interval ){ + if (now - last_time_sent >= interval) { last_time_sent = now; - uint16_t to = 00; // Who should we send to? By default, send to base - - - if ( num_active_nodes ){ // Or if we have active nodes, - to = active_nodes[next_ping_node_index++]; // Send to the next active node - if ( next_ping_node_index > num_active_nodes ){ // Have we rolled over? - next_ping_node_index = 0; // Next time start at the beginning - to = 00; // This time, send to node 00. - } + + if (num_active_nodes) { // Or if we have active nodes, + to = active_nodes[next_ping_node_index++]; // Send to the next active node + if (next_ping_node_index > num_active_nodes) { // Have we rolled over? + next_ping_node_index = 0; // Next time start at the beginning + to = 00; // This time, send to node 00. + } } bool ok; - - if ( this_node > 00 || to == 00 ){ // Normal nodes send a 'T' ping - ok = send_T(to); - }else{ // Base node sends the current active nodes out - ok = send_N(to); + if (this_node > 00 || to == 00) { // Normal nodes send a 'T' ping + ok = send_T(to); + } else { // Base node sends the current active nodes out + ok = send_N(to); } - - if (ok){ // Notify us of the result - printf_P(PSTR("%lu: APP Send ok\n\r"),millis()); - }else{ - printf_P(PSTR("%lu: APP Send failed\n\r"),millis()); - last_time_sent -= 100; // Try sending at a different time next time + + if (ok) { // Notify us of the result + Serial.print(millis()); + Serial.println(F(": APP Send ok")); + } else { + Serial.print(millis()); + Serial.println(F(": APP Send failed")); + last_time_sent -= 100; // Try sending at a different time next time } } } @@ -185,41 +200,50 @@ void loop(){ /** * Send a 'T' message, the current time */ -bool send_T(uint16_t to) -{ +bool send_T(uint16_t to) { RF24NetworkHeader header(/*to node*/ to, /*type*/ 'T' /*Time*/); - + // The 'T' message that we send is just a ulong, containing the time unsigned long message = millis(); - printf_P(PSTR("---------------------------------\n\r")); - printf_P(PSTR("%lu: APP Sending %lu to 0%o...\n\r"),millis(),message,to); - return network.write(header,&message,sizeof(unsigned long)); + Serial.println(F("---------------------------------")); + Serial.print(millis()); + Serial.print(F(": APP Sending ")); + Serial.print(message); + Serial.print(F(" to ")); + Serial.print(to); + Serial.println(F("...")); + return network.write(header, &message, sizeof(unsigned long)); } /** * Send an 'N' message, the active node list */ -bool send_N(uint16_t to) -{ +bool send_N(uint16_t to) { RF24NetworkHeader header(/*to node*/ to, /*type*/ 'N' /*Time*/); - - printf_P(PSTR("---------------------------------\n\r")); - printf_P(PSTR("%lu: APP Sending active nodes to 0%o...\n\r"),millis(),to); - return network.write(header,active_nodes,sizeof(active_nodes)); + + Serial.println(F("---------------------------------")); + Serial.print(millis()); + Serial.print(F(": APP Sending active nodes to ")); + Serial.print(to); + Serial.println(F("...")); + return network.write(header, active_nodes, sizeof(active_nodes)); } /** * Handle a 'T' message * Add the node to the list of active nodes */ -void handle_T(RF24NetworkHeader& header){ +void handle_T(RF24NetworkHeader& header) { unsigned long message; // The 'T' message is just a ulong, containing the time - network.read(header,&message,sizeof(unsigned long)); - printf_P(PSTR("%lu: APP Received %lu from 0%o\n\r"),millis(),message,header.from_node); - - - if ( header.from_node != this_node || header.from_node > 00 ) // If this message is from ourselves or the base, don't bother adding it to the active nodes. + network.read(header, &message, sizeof(unsigned long)); + Serial.print(millis()); + Serial.print(F(": APP Received ")); + Serial.print(message); + Serial.print(F(" from ")); + Serial.print(header.from_node); + + if (header.from_node != this_node || header.from_node > 00) // If this message is from ourselves or the base, don't bother adding it to the active nodes. add_node(header.from_node); } @@ -230,27 +254,32 @@ void handle_N(RF24NetworkHeader& header) { static uint16_t incoming_nodes[max_active_nodes]; - network.read(header,&incoming_nodes,sizeof(incoming_nodes)); - printf_P(PSTR("%lu: APP Received nodes from 0%o\n\r"),millis(),header.from_node); + network.read(header, &incoming_nodes, sizeof(incoming_nodes)); + Serial.print(millis()); + Serial.print(F(": APP Received nodes from ")); + Serial.println(header.from_node); int i = 0; - while ( i < max_active_nodes && incoming_nodes[i] > 00 ) + while (i < max_active_nodes && incoming_nodes[i] > 00) add_node(incoming_nodes[i++]); } /** * Add a particular node to the current list of active nodes */ -void add_node(uint16_t node){ - - short i = num_active_nodes; // Do we already know about this node? - while (i--) { - if ( active_nodes[i] == node ) - break; +void add_node(uint16_t node) { + + short i = num_active_nodes; // Do we already know about this node? + while (i--) { + if (active_nodes[i] == node) + break; } - - if ( i == -1 && num_active_nodes < max_active_nodes ){ // If not, add it to the table - active_nodes[num_active_nodes++] = node; - printf_P(PSTR("%lu: APP Added 0%o to list of active nodes.\n\r"),millis(),node); + + if (i == -1 && num_active_nodes < max_active_nodes) { // If not, add it to the table + active_nodes[num_active_nodes++] = node; + Serial.print(millis()); + Serial.print(F(": APP Added ")); + Serial.print(node); + Serial.print(F(" to list of active nodes.")); } } diff --git a/examples/Network_Priority_RX/Network_Priority_RX.ino b/examples/Network_Priority_RX/Network_Priority_RX.ino index 26f4c2cc..2ea918f9 100644 --- a/examples/Network_Priority_RX/Network_Priority_RX.ino +++ b/examples/Network_Priority_RX/Network_Priority_RX.ino @@ -1,24 +1,24 @@ -/* - Copyright (C) 2020 TMRh20(tmrh20@gmail.com) - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - version 2 as published by the Free Software Foundation. +/** + * Copyright (C) 2020 TMRh20(tmrh20@gmail.com) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. */ /** * This sketch demonstrates handling of external data - * - * RF24Network contains a buffer for storing user payloads that have been received via the network.update() + * + * RF24Network contains a buffer for storing user payloads that have been received via the network.update() * function. If using protocols like TCP/IP over RF24Network, the memory on small devices is very limited. - * Instead of using the user-payload buffer for such large payloads, they can be designated as an + * Instead of using the user-payload buffer for such large payloads, they can be designated as an * EXTERNAL_DATA_TYPE in the header.type field. This allows users to prioritize these payloads, as they are * often very large, and would take up most or all of the user data buffer. - * + * * The network.update function will return immediately upon receiving a payload marked as EXTERNAL_DATA_TYPE * Users can then process the data immediately. * All other payload types are handled via the network.available() and network.read() functionality. - * + * * Functionality: * The TX node will send normal user data designated with header.type = 33, along with additional data * marked as header.type = EXTERNAL_DATA_TYPE. @@ -26,12 +26,12 @@ * normally vs data that needs to be passed elsewhere, like network interface for TCP/IP packets. * These methods are used in RF24Gateway & RF24Ethernet TCP/IP libraries for nrf24l01+. */ - + +#include "printf.h" #include #include -#include "printf.h" -RF24 radio(7,8); // nRF24L01(+) radio attached using Getting Started board +RF24 radio(7, 8); // nRF24L01(+) radio attached using Getting Started board RF24Network network(radio); // Network uses that radio @@ -41,15 +41,26 @@ const uint16_t other_node = 01; // Address of the other node in Octal forma uint32_t myVariable = 0; void setup() { - + Serial.begin(115200); - Serial.println("RF24Network/examples/Network_Separation_RX/"); + if (!Serial) { + // some boards need this because of native USB capability + } printf_begin(); //Used to enable printf on AVR devices - - radio.begin(); + if (!Serial) { + // some boards need this because of native USB capability + } + Serial.println(F("RF24Network/examples/Network_Separation_RX/")); + + if (!radio.begin()) { + Serial.println(F("Radio hardware not responding!")); + while (1) { + // hold in infinite loop + } + } network.begin(/*channel*/ 90, /*node address*/ this_node); radio.printDetails(); - + }//setup @@ -70,56 +81,53 @@ uint32_t userDataTimer = 0; * The main loop behaviour demonstrates the different prioritization of handling data * External data is handled immediately upon reception, with the network.update() function being * called very regularly to handle incoming/outgoing radio traffic. - * + * * The network.available() function is only called every 5 seconds, to simulate a busy microcontroller, * so the user payloads will only print out every 5 seconds - * + * * The radio has 3, 32-byte FIFO buffers operating independantly of the MCU, and RF24Network will buffer * up to MAX_PAYLOAD_SIZE (see RF24Network_config.h) of user data. */ - void loop() { + // Immediate handling of data with header type EXTERNAL_DATA_TYPE - // Immediate handling of data with header type EXTERNAL_DATA_TYPE - - if(network.update() == EXTERNAL_DATA_TYPE){ - uint16_t size = network.frag_ptr->message_size; - memcpy(&dataBuffer,network.frag_ptr->message_buffer,network.frag_ptr->message_size); + if (network.update() == EXTERNAL_DATA_TYPE) { + uint16_t size = network.frag_ptr->message_size; + memcpy(&dataBuffer, network.frag_ptr->message_buffer, network.frag_ptr->message_size); - // Handle the external data however... - Serial.print("External Data RX, size: "); - Serial.println(network.frag_ptr->message_size); + // Handle the external data however... + Serial.print("External Data RX, size: "); + Serial.println(network.frag_ptr->message_size); - for(uint16_t i=0; imessage_size; i++){ - Serial.print(dataBuffer[i]); - Serial.print(":"); - } - Serial.println(); + for (uint16_t i = 0; i < network.frag_ptr->message_size; i++) { + Serial.print(dataBuffer[i]); + Serial.print(":"); } - - - // Use a timer to simulate a busy MCU where normal network data cannot be processed in a timely manner - if(millis() - userDataTimer > 5000){ - userDataTimer = millis(); - - // Handling of standard RF24Network User Data - while(network.available()){ - - RF24NetworkHeader header; // Create an empty header - uint16_t dataSize = network.peek(header); // Peek to get the size of the data - uint32_t someVariable; - if(header.type = '32'){ // If a certain header type is recieved - network.read(header,&someVariable,sizeof(someVariable)); // Handle the data a specific way - Serial.print("RX User Data:\nHeader Type "); - Serial.print(header.type); - Serial.print(" Value "); - Serial.println(someVariable); - }else{ - network.read(header,&someVariable,0); // Clear the user data from the buffer if - // some other header type is received - } + Serial.println(); + } + + + // Use a timer to simulate a busy MCU where normal network data cannot be processed in a timely manner + if (millis() - userDataTimer > 5000) { + userDataTimer = millis(); + + // Handling of standard RF24Network User Data + while (network.available()) { + + RF24NetworkHeader header; // Create an empty header + uint16_t dataSize = network.peek(header); // Peek to get the size of the data + uint32_t someVariable; + if (header.type = '32') { // If a certain header type is recieved + network.read(header, &someVariable, sizeof(someVariable)); // Handle the data a specific way + Serial.print("RX User Data:\nHeader Type "); + Serial.print(header.type); + Serial.print(" Value "); + Serial.println(someVariable); + } else { + network.read(header, &someVariable, 0); // Clear the user data from the buffer if + // some other header type is received } } - + } }//loop diff --git a/examples/Network_Priority_TX/Network_Priority_TX.ino b/examples/Network_Priority_TX/Network_Priority_TX.ino index 955a8d06..ca94c2fe 100644 --- a/examples/Network_Priority_TX/Network_Priority_TX.ino +++ b/examples/Network_Priority_TX/Network_Priority_TX.ino @@ -1,24 +1,24 @@ -/* - Copyright (C) 2020 TMRh20(tmrh20@gmail.com) - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - version 2 as published by the Free Software Foundation. +/** + * Copyright (C) 2020 TMRh20(tmrh20@gmail.com) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. */ /** * This sketch demonstrates handling of external data - * - * RF24Network contains a buffer for storing user payloads that have been received via the network.update() + * + * RF24Network contains a buffer for storing user payloads that have been received via the network.update() * function. If using protocols like TCP/IP over RF24Network, the memory on small devices is very limited. - * Instead of using the user-payload buffer for such large payloads, they can be designated as an + * Instead of using the user-payload buffer for such large payloads, they can be designated as an * EXTERNAL_DATA_TYPE in the header.type field. This allows users to prioritize these payloads, as they are * often very large, and would take up most or all of the user data buffer. - * + * * The network.update function will return immediately upon receiving a payload marked as EXTERNAL_DATA_TYPE * Users can then process the data immediately. * All other payload types are handled via the network.available() and network.read() functionality. - * + * * Functionality: * The TX node will send normal user data designated with header.type = 33, along with additional data * marked as header.type = EXTERNAL_DATA_TYPE. @@ -26,12 +26,12 @@ * normally vs data that needs to be passed elsewhere, like network interface for TCP/IP packets. * These methods are used in RF24Gateway & RF24Ethernet TCP/IP libraries for nrf24l01+. */ - + #include #include #include "printf.h" -RF24 radio(7,8); // nRF24L01(+) radio attached using Getting Started board +RF24 radio(7, 8); // nRF24L01(+) radio attached using Getting Started board RF24Network network(radio); // Network uses that radio @@ -41,56 +41,64 @@ const uint16_t other_node = 00; // Address of the other node in Octal forma uint8_t dataBuffer[33]; void setup() { - + Serial.begin(115200); - Serial.println("RF24Network/examples/Network_Separation_TX/"); + if (!Serial) { + // some boards need this because of native USB capability + } + Serial.println(F("RF24Network/examples/Network_Separation_TX/")); printf_begin(); //Used to enable printf on AVR devices - - radio.begin(); + + if (!radio.begin()) { + Serial.println(F("Radio hardware not responding!")); + while (1) { + // hold in infinite loop + } + } network.begin(/*channel*/ 90, /*node address*/ this_node); radio.printDetails(); // Load our data buffer with numbered data - for(uint16_t i=0;i<33;i++){ - dataBuffer[i] = i; + for (uint16_t i = 0; i < 33; i++) { + dataBuffer[i] = i; } - + }//setup uint32_t sendTimer = 0; /* - * The main loop sends two types of data to be processed with different priority per the RX + * The main loop sends two types of data to be processed with different priority per the RX * example */ void loop() { - network.update(); - - if(millis() - sendTimer > 1000){ - sendTimer = millis(); - - Serial.println("Sending data..."); - - // Sending of External data, which will be handled immediately - RF24NetworkHeader header(other_node,EXTERNAL_DATA_TYPE); - bool ok = network.write(header,&dataBuffer,33); - Serial.println(ok ? "OK 1":"Fail 1"); - - // Sending normal user data, which may be buffered and handled later - RF24NetworkHeader header2(other_node,32); - uint32_t someVariable = 1234; - ok = network.write(header2,&someVariable,sizeof(someVariable)); - Serial.println(ok ? "OK 2":"Fail 2"); - } - - // Dummy operation to read 0 bytes from all incoming user payloads - // Ensures the buffer doesnt fill up - if(network.available()){ - RF24NetworkHeader header; - network.read(header,&dataBuffer,0); - } + network.update(); + + if (millis() - sendTimer > 1000) { + sendTimer = millis(); + + Serial.println(F("Sending data...")); + + // Sending of External data, which will be handled immediately + RF24NetworkHeader header(other_node, EXTERNAL_DATA_TYPE); + bool ok = network.write(header, &dataBuffer, 33); + Serial.println(ok ? F("OK 1") : F("Fail 1")); + + // Sending normal user data, which may be buffered and handled later + RF24NetworkHeader header2(other_node, 32); + uint32_t someVariable = 1234; + ok = network.write(header2, &someVariable, sizeof(someVariable)); + Serial.println(ok ? F("OK 2") : F("Fail 2")); + } + + // Dummy operation to read 0 bytes from all incoming user payloads + // Ensures the buffer doesnt fill up + if (network.available()) { + RF24NetworkHeader header; + network.read(header, &dataBuffer, 0); + } }//loop diff --git a/examples/examples_formatter.conf b/examples/examples_formatter.conf new file mode 100644 index 00000000..57270c83 --- /dev/null +++ b/examples/examples_formatter.conf @@ -0,0 +1,31 @@ +# This configuration file contains a selection of the available options provided by the formatting tool "Artistic Style" +# http://astyle.sourceforge.net/astyle.html +# +# If you wish to change them, don't edit this file. +# Instead, copy it in the same folder of file "preferences.txt" and modify the copy. This way, you won't lose your custom formatter settings when upgrading the IDE +# If you don't know where file preferences.txt is stored, open the IDE, File -> Preferences and you'll find a link + +mode=c + +# 2 spaces indentation +indent=spaces=2 + +# also indent macros +indent-preprocessor + +# indent classes, switches (and cases), comments starting at column 1 +indent-classes +indent-switches +indent-cases +indent-col1-comments + +# put a space around operators +pad-oper + +# put a space after if/for/while +pad-header + +# if you like one-liners, keep them +keep-one-line-statements + +# remove-comment-prefix diff --git a/examples/helloworld_rx/helloworld_rx.ino b/examples/helloworld_rx/helloworld_rx.ino index 0cf4e355..158096ff 100644 --- a/examples/helloworld_rx/helloworld_rx.ino +++ b/examples/helloworld_rx/helloworld_rx.ino @@ -1,11 +1,11 @@ -/* - Copyright (C) 2012 James Coliz, Jr. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - version 2 as published by the Free Software Foundation. - - Update 2014 - TMRh20 +/** + * Copyright (C) 2012 James Coliz, Jr. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * Update 2014 - TMRh20 */ /** @@ -15,43 +15,49 @@ * Listens for messages from the transmitter and prints them out. */ -#include -#include #include +#include +#include -RF24 radio(7,8); // nRF24L01(+) radio attached using Getting Started board +RF24 radio(7, 8); // nRF24L01(+) radio attached using Getting Started board -RF24Network network(radio); // Network uses that radio -const uint16_t this_node = 00; // Address of our node in Octal format ( 04,031, etc) -const uint16_t other_node = 01; // Address of the other node in Octal format +RF24Network network(radio); // Network uses that radio +const uint16_t this_node = 00; // Address of our node in Octal format (04, 031, etc) +const uint16_t other_node = 01; // Address of the other node in Octal format -struct payload_t { // Structure of our payload +struct payload_t { // Structure of our payload unsigned long ms; unsigned long counter; }; -void setup(void) -{ +void setup(void) { Serial.begin(115200); - Serial.println("RF24Network/examples/helloworld_rx/"); - + if (!Serial) { + // some boards need this because of native USB capability + } + Serial.println(F("RF24Network/examples/helloworld_rx/")); + SPI.begin(); - radio.begin(); + if (!radio.begin()) { + Serial.println(F("Radio hardware not responding!")); + while (1) { + // hold in infinite loop + } + } network.begin(/*channel*/ 90, /*node address*/ this_node); } -void loop(void){ - +void loop(void) { + network.update(); // Check the network regularly - - while ( network.available() ) { // Is there anything ready for us? - + while (network.available()) { // Is there anything ready for us? + RF24NetworkHeader header; // If so, grab it and print it out payload_t payload; - network.read(header,&payload,sizeof(payload)); + network.read(header, &payload, sizeof(payload)); Serial.print("Received packet #"); Serial.print(payload.counter); Serial.print(" at "); diff --git a/examples/helloworld_rx_advanced/helloworld_rx_advanced.ino b/examples/helloworld_rx_advanced/helloworld_rx_advanced.ino index 89bd8d2e..473ea87c 100644 --- a/examples/helloworld_rx_advanced/helloworld_rx_advanced.ino +++ b/examples/helloworld_rx_advanced/helloworld_rx_advanced.ino @@ -1,27 +1,27 @@ -/* - Copyright (C) 2020 TMRh20(tmrh20@gmail.com) - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - version 2 as published by the Free Software Foundation. +/** + * Copyright (C) 2020 TMRh20(tmrh20@gmail.com) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. */ /** - * More advanced example of using RF24Network: + * More advanced example of using RF24Network: * Fragmentation and Reassembly: - * - nrf24l01+ radios can tx/rx 32 bytes of data per transmission + * - nrf24l01+ radios can tx/rx 32 bytes of data per transmission * - RF24Network will fragment and re-assemble payloads of any size * Demonstrates use of differing sized payloads using peek() function - * + * * RECEIVER NODE * Every X milliseconds, send a payload to the receiver node. */ +#include "printf.h" #include #include -#include "printf.h" -RF24 radio(7,8); // nRF24L01(+) radio attached using Getting Started board +RF24 radio(7, 8); // nRF24L01(+) radio attached using Getting Started board RF24Network network(radio); // Network uses that radio const uint16_t this_node = 00; // Address of our node in Octal format ( 04,031, etc) @@ -34,41 +34,56 @@ const uint16_t other_node = 01; // Address of the other node in Octal format uint8_t dataBuffer[MAX_PAYLOAD_SIZE]; //MAX_PAYLOAD_SIZE is defined in RF24Network_config.h -void setup(void) -{ +void setup(void) { + Serial.begin(115200); - Serial.println("RF24Network/examples/helloworld_rx_advanced/"); - printf_begin(); //Used to enable printf on AVR devices + if (!Serial) { + // some boards need this because of native USB capability + } + Serial.println(F("RF24Network/examples/helloworld_rx_advanced/")); - radio.begin(); + if (!radio.begin()) { + Serial.println(F("Radio hardware not responding!")); + while (1) { + // hold in infinite loop + } + } network.begin(/*channel*/ 90, /*node address*/ this_node); - radio.printDetails(); + + printf_begin(); // Used to enable printf on AVR devices + radio.printDetails(); // requires printf support } // Variable for calculating how long between RX uint32_t timeBetweenPackets = 0; -void loop(void){ - - network.update(); // Check the network regularly - - while ( network.available() ) { // Is there anything ready for us? - - RF24NetworkHeader header; // If so, grab it and print it out - uint16_t payloadSize = network.peek(header); // Use peek() to get the size of the payload - network.read(header,&dataBuffer,payloadSize); // Get the data - Serial.print("Received packet, size "); // Print info about received data +void loop(void) { + + network.update(); // Check the network regularly + + while (network.available()) { // Is there anything ready for us? + + RF24NetworkHeader header; // If so, grab it and print it out + uint16_t payloadSize = network.peek(header); // Use peek() to get the size of the payload + network.read(header, &dataBuffer, payloadSize); // Get the data + Serial.print("Received packet, size "); // Print info about received data Serial.print(payloadSize); Serial.print("("); - Serial.print(millis()-timeBetweenPackets); + Serial.print(millis() - timeBetweenPackets); Serial.println("ms since last)"); timeBetweenPackets = millis(); + // Uncomment below to print the entire payload - /*for(uint32_t i=0;i - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - version 2 as published by the Free Software Foundation. - - Update 2014 - TMRh20 +/** + * Copyright (C) 2012 James Coliz, Jr. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * Update 2014 - TMRh20 */ /** - * Simplest possible example of using RF24Network + * Simplest possible example of using RF24Network * * TRANSMITTER NODE * Every 2 seconds, send a payload to the receiver node. */ -#include -#include #include +#include +#include -RF24 radio(7,8); // nRF24L01(+) radio attached using Getting Started board +RF24 radio(7, 8); // nRF24L01(+) radio attached using Getting Started board RF24Network network(radio); // Network uses that radio -const uint16_t this_node = 01; // Address of our node in Octal format -const uint16_t other_node = 00; // Address of the other node in Octal format +const uint16_t this_node = 01; // Address of our node in Octal format +const uint16_t other_node = 00; // Address of the other node in Octal format -const unsigned long interval = 2000; //ms // How often to send 'hello world to the other unit +const unsigned long interval = 2000; // How often (in ms) to send 'hello world' to the other unit unsigned long last_sent; // When did we last send? unsigned long packets_sent; // How many have we sent already -struct payload_t { // Structure of our payload +struct payload_t { // Structure of our payload unsigned long ms; unsigned long counter; }; -void setup(void) -{ +void setup(void) { Serial.begin(115200); - Serial.println("RF24Network/examples/helloworld_tx/"); - + if (!Serial) { + // some boards need this because of native USB capability + } + Serial.println(F("RF24Network/examples/helloworld_tx/")); + SPI.begin(); - radio.begin(); + if (!radio.begin()) { + Serial.println(F("Radio hardware not responding!")); + while (1) { + // hold in infinite loop + } + } network.begin(/*channel*/ 90, /*node address*/ this_node); } void loop() { - - network.update(); // Check the network regularly - - unsigned long now = millis(); // If it's time to send a message, send it! - if ( now - last_sent >= interval ) - { + network.update(); // Check the network regularly + + unsigned long now = millis(); + + // If it's time to send a message, send it! + if (now - last_sent >= interval) { last_sent = now; Serial.print("Sending..."); payload_t payload = { millis(), packets_sent++ }; RF24NetworkHeader header(/*to node*/ other_node); - bool ok = network.write(header,&payload,sizeof(payload)); + bool ok = network.write(header, &payload, sizeof(payload)); if (ok) Serial.println("ok."); else diff --git a/examples/helloworld_tx_advanced/helloworld_tx_advanced.ino b/examples/helloworld_tx_advanced/helloworld_tx_advanced.ino index 72996474..cc4b71a5 100644 --- a/examples/helloworld_tx_advanced/helloworld_tx_advanced.ino +++ b/examples/helloworld_tx_advanced/helloworld_tx_advanced.ino @@ -1,27 +1,27 @@ -/* - Copyright (C) 2020 TMRh20(tmrh20@gmail.com) - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - version 2 as published by the Free Software Foundation. +/** + * Copyright (C) 2020 TMRh20(tmrh20@gmail.com) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. */ /** - * More advanced example of using RF24Network: + * More advanced example of using RF24Network: * Fragmentation and Reassembly: - * - nrf24l01+ radios can tx/rx 32 bytes of data per transmission + * - nrf24l01+ radios can tx/rx 32 bytes of data per transmission * - RF24Network will fragment and re-assemble payloads of any size * Demonstrates use of differing sized payloads using peek() function - * + * * TRANSMITTER NODE * Every X milliseconds, send a payload to the receiver node. */ - + +#include "printf.h" #include #include -#include "printf.h" -RF24 radio(7,8); // nRF24L01(+) radio attached using Getting Started board +RF24 radio(7, 8); // nRF24L01(+) radio attached using Getting Started board RF24Network network(radio); // Network uses that radio @@ -38,19 +38,26 @@ unsigned long last_sent; // When did we last send? */ uint8_t dataBuffer[MAX_PAYLOAD_SIZE]; -void setup(void) -{ +void setup(void) { Serial.begin(115200); - Serial.println("RF24Network/examples/helloworld_tx_advanced/"); + if (!Serial) { + // some boards need this because of native USB capability + } + Serial.println(F("RF24Network/examples/helloworld_tx_advanced/")); printf_begin(); //Used to enable printf on AVR devices - - radio.begin(); + + if (!radio.begin()) { + Serial.println(F("Radio hardware not responding!")); + while (1) { + // hold in infinite loop + } + } network.begin(/*channel*/ 90, /*node address*/ this_node); radio.printDetails(); - + // Load our data buffer with numbered data - for(uint16_t i=0;i= interval && !stopSending ){ + if ( now - last_sent >= interval && !stopSending ) { last_sent = now; Serial.print("Sending size "); Serial.print(sizeofSend ); // Fragmentation/reassembly is transparent. Just send payloads as usual. RF24NetworkHeader header(/*to node*/ other_node); - bool ok = network.write(header,&dataBuffer,sizeofSend++); - + bool ok = network.write(header, &dataBuffer, sizeofSend++); + // If the size of data to be sent is larger than max payload size, reset at 0 - if(sizeofSend > MAX_PAYLOAD_SIZE){ + if (sizeofSend > MAX_PAYLOAD_SIZE) { sizeofSend = 0; } - + if (ok) Serial.println(" ok."); else