From fc2c86a1b630941ea6eff775927ff6c90016b433 Mon Sep 17 00:00:00 2001 From: Dirk Jahnke Date: Thu, 15 Nov 2018 20:31:35 +0100 Subject: [PATCH] Initial commit --- .gitignore | 5 ++ .travis.yml | 67 ++++++++++++++++++ include/README | 39 +++++++++++ lib/README | 46 ++++++++++++ platformio.ini | 17 +++++ src/main.cpp | 187 +++++++++++++++++++++++++++++++++++++++++++++++++ test/README | 11 +++ 7 files changed, 372 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 include/README create mode 100644 lib/README create mode 100644 platformio.ini create mode 100644 src/main.cpp create mode 100644 test/README diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f152028 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.pioenvs +.piolibdeps +.clang_complete +.gcc-flags.json diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..7c486f1 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,67 @@ +# Continuous Integration (CI) is the practice, in software +# engineering, of merging all developer working copies with a shared mainline +# several times a day < https://docs.platformio.org/page/ci/index.html > +# +# Documentation: +# +# * Travis CI Embedded Builds with PlatformIO +# < https://docs.travis-ci.com/user/integration/platformio/ > +# +# * PlatformIO integration with Travis CI +# < https://docs.platformio.org/page/ci/travis.html > +# +# * User Guide for `platformio ci` command +# < https://docs.platformio.org/page/userguide/cmd_ci.html > +# +# +# Please choose one of the following templates (proposed below) and uncomment +# it (remove "# " before each line) or use own configuration according to the +# Travis CI documentation (see above). +# + + +# +# Template #1: General project. Test it using existing `platformio.ini`. +# + +# language: python +# python: +# - "2.7" +# +# sudo: false +# cache: +# directories: +# - "~/.platformio" +# +# install: +# - pip install -U platformio +# - platformio update +# +# script: +# - platformio run + + +# +# Template #2: The project is intended to be used as a library with examples. +# + +# language: python +# python: +# - "2.7" +# +# sudo: false +# cache: +# directories: +# - "~/.platformio" +# +# env: +# - PLATFORMIO_CI_SRC=path/to/test/file.c +# - PLATFORMIO_CI_SRC=examples/file.ino +# - PLATFORMIO_CI_SRC=path/to/test/directory +# +# install: +# - pip install -U platformio +# - platformio update +# +# script: +# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..03b0b40 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,17 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:nanoatmega168] +platform = atmelavr +board = nanoatmega168 +framework = arduino +lib_deps = RF24 +upload_speed = 19200 +upload_port = COM8 diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..b7cdbb1 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,187 @@ +/* +* Getting Started example sketch for nRF24L01+ radios +* This is a very basic example of how to send data from one node to another +* Updated: Dec 2014 by TMRh20 +*/ + +#include +#include "nRF24L01.h" +#include "RF24.h" + +/****************** User Config ***************************/ +/*** Set this radio as radio number 0 or 1 ***/ +bool radioNumber = 0; + +/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */ +RF24 radio(8, 10); // CSN, CE +/**********************************************************/ + +byte addresses[][6] = {"1ClkM","2ClkM"}; + +// Used to control whether this node is sending or receiving +bool role = 0; + +void setup() { + Serial.begin(19200); + Serial.println(F("*** RECEIVE DUMP ****")); + //Serial.println(F("*** PRESS 'T' to begin transmitting to the other node")); + + radio.begin(); + + // Set the PA Level low to prevent power supply related issues since this is a + // getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default. + radio.setPALevel(RF24_PA_LOW); + radio.setChannel(1); + radio.setDataRate(RF24_2MBPS); + radio.setCRCLength(RF24_CRC_8); + radio.setAutoAck(0); + + // Open a writing and reading pipe on each radio, with opposite addresses + if (radioNumber) { + radio.openWritingPipe(addresses[1]); + radio.openReadingPipe(1,addresses[0]); + } else { + radio.openWritingPipe(addresses[0]); + radio.openReadingPipe(1,addresses[1]); + } + radio.powerUp(); + Serial.println("Radio details:"); + radio.printDetails(); + + // Start the radio listening for data + radio.startListening(); +} + +void loop() { + char buffer[32]; + memset(buffer, 0, 32); + + if (radio.available()) { + radio.read(buffer, 32); + Serial.print(F("Received: type=")); + Serial.print(buffer[0]); + if (buffer[0] == 'c') { + Serial.print(F(", day=")); + Serial.print((int) buffer[1]); + Serial.print(F(", time=")); + Serial.print((int) buffer[2]); + Serial.print(':'); + Serial.print((int) buffer[3]); + Serial.print(':'); + Serial.print((int) buffer[4]); + Serial.print(F(", speed=1:")); + Serial.println(1000.0 / ((int) buffer[5] << 2)); + } else if (buffer[0] == 'R') { + Serial.print(F(", clockName=")); + Serial.println(buffer+1); + } else { + Serial.print(F("Unknown message received. Type=")); + Serial.print(buffer[0]); + for (int i=0; i<8; ++i) { + Serial.print(' '); Serial.print(buffer[i+1], HEX); + } + Serial.println(F(" ...")); + } + } + + static unsigned long lastTick = 0; + if (millis() - lastTick > 5000) { + lastTick = millis(); + Serial.print("#"); + } + + + + +#if 0 +/****************** Ping Out Role ***************************/ +if (role == 1) { + + radio.stopListening(); // First, stop listening so we can talk. + + + Serial.println(F("Now sending")); + + unsigned long start_time = micros(); // Take the time, and send it. This will block until complete + if (!radio.write( &start_time, sizeof(unsigned long) )){ + Serial.println(F("failed")); + } + + radio.startListening(); // Now, continue listening + + unsigned long started_waiting_at = micros(); // Set up a timeout period, get the current microseconds + boolean timeout = false; // Set up a variable to indicate if a response was received or not + + while ( ! radio.available() ){ // While nothing is received + if (micros() - started_waiting_at > 200000 ){ // If waited longer than 200ms, indicate timeout and exit while loop + timeout = true; + break; + } + } + + if ( timeout ){ // Describe the results + Serial.println(F("Failed, response timed out.")); + }else{ + unsigned long got_time; // Grab the response, compare, and send to debugging spew + radio.read( &got_time, sizeof(unsigned long) ); + unsigned long end_time = micros(); + + // Spew it + Serial.print(F("Sent ")); + Serial.print(start_time); + Serial.print(F(", Got response ")); + Serial.print(got_time); + Serial.print(F(", Round-trip delay ")); + Serial.print(end_time-start_time); + Serial.println(F(" microseconds")); + } + + // Try again 1s later + delay(1000); + } + + + +/****************** Pong Back Role ***************************/ + + if ( role == 0 ) + { + unsigned long got_time; + + if( radio.available()){ + // Variable for the received timestamp + while (radio.available()) { // While there is data ready + radio.read( &got_time, sizeof(unsigned long) ); // Get the payload + } + + radio.stopListening(); // First, stop listening so we can talk + radio.write( &got_time, sizeof(unsigned long) ); // Send the final one back. + radio.startListening(); // Now, resume listening so we catch the next packets. + Serial.print(F("Sent response ")); + Serial.println(got_time); + } + } + + + + +/****************** Change Roles via Serial Commands ***************************/ + + if ( Serial.available() ) + { + char c = toupper(Serial.read()); + if ( c == 'T' && role == 0 ){ + Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK")); + role = 1; // Become the primary transmitter (ping out) + + }else + if ( c == 'R' && role == 1 ){ + Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK")); + role = 0; // Become the primary receiver (pong back) + radio.startListening(); + + } + } +#endif + +} // Loop diff --git a/test/README b/test/README new file mode 100644 index 0000000..df5066e --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PIO Unit Testing and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PIO Unit Testing: +- https://docs.platformio.org/page/plus/unit-testing.html