From 672d5baf238c3517aa6dd5fbdfdd5c866b806747 Mon Sep 17 00:00:00 2001 From: Dirk Jahnke Date: Wed, 14 Nov 2018 21:34:51 +0100 Subject: [PATCH] Added new platform: Wemos ESP8266 D1 mini pro. Now radio starts but still does not send any clock information. --- lib/readme.txt | 41 +++++++++++++ platformio.ini | 5 ++ src/config.h | 7 +-- src/display.cpp | 5 +- src/main.cpp | 149 +++++------------------------------------------- src/radio.cpp | 124 ++++++++++++++++++++++++++++++++++++++++ src/radio.h | 15 +++++ 7 files changed, 206 insertions(+), 140 deletions(-) create mode 100644 lib/readme.txt create mode 100644 src/radio.cpp create mode 100644 src/radio.h diff --git a/lib/readme.txt b/lib/readme.txt new file mode 100644 index 0000000..131f1bf --- /dev/null +++ b/lib/readme.txt @@ -0,0 +1,41 @@ + +This directory is intended for the project specific (private) libraries. +PlatformIO will compile them to static libraries and link to executable file. + +The source code of each library should be placed in separate directory, like +"lib/private_lib/[here are source files]". + +For example, see how can be organized `Foo` and `Bar` libraries: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) http://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- readme.txt --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +Then in `src/main.c` you should use: + +#include +#include + +// rest H/C/CPP code + +PlatformIO will find your libraries automatically, configure preprocessor's +include paths and build them. + +More information about PlatformIO Library Dependency Finder +- http://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini index 7354227..db4748c 100644 --- a/platformio.ini +++ b/platformio.ini @@ -8,6 +8,11 @@ ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html +[env:d1_mini_pro] +platform = espressif8266 +board = d1_mini_pro +framework = arduino + [env:heltec_wifi_kit_8] platform = espressif8266 board = heltec_wifi_kit_8 diff --git a/src/config.h b/src/config.h index 3700460..60d730a 100644 --- a/src/config.h +++ b/src/config.h @@ -1,11 +1,8 @@ #ifndef config_h_included #define config_h_included -#define PIN_MASTER_CLIENT_SELECT 2 -#define ROLE_MASTER LOW -#define ROLE_CLIENT HIGH -#define PIN_NRF24_CE 10 -#define PIN_NRF24_CSN 8 +#define PIN_NRF24_CE 2 /*10*/ +#define PIN_NRF24_CSN 15 /*8*/ #define PIN_RELAY1 5 #define PIN_RELAY2 6 diff --git a/src/display.cpp b/src/display.cpp index b4b9773..540b665 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -6,7 +6,10 @@ // display -U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ 16, /* clock=*/ 5, /* data=*/ 4); +//U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ 16, /* clock=*/ 5, /* data=*/ 4); +U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0, /* reset=16*/ -1, /* clock=*/ 5, /* data=*/ 4); +//U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0); +//U8X8_SH1106_128X64_NONAME_4W_HW_SPI u8g2(/* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8); #define LOGO16_GLCD_HEIGHT 16 #define LOGO16_GLCD_WIDTH 16 diff --git a/src/main.cpp b/src/main.cpp index 9edb7c3..154b1e3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,11 +9,11 @@ #include #include -#include #include "config.h" #include "display.h" #include "fastclock.h" +#include "radio.h" #define AUTOSTART_CLOCK_AFTER_ms 10000 @@ -30,14 +30,12 @@ char static_sn[16] = "255.255.255.0"; //flag for saving data bool shouldSaveConfig = false; -RF24 radio(PIN_NRF24_CE, PIN_NRF24_CSN); -byte addresses[][6] = {NETWORK_ADDRESS_MASTER_SEND, NETWORK_ADDRESS_MASTER_RECEIVE}; - // FastClock bool isMaster = true; Display display; Fastclock fastclock(&display); +Radio radio(&display, true /*isMaster*/); //callback notifying us of the need to save config void saveConfigCallback () { @@ -205,23 +203,7 @@ void setup() { // setupWifiConnection(); - // setting up RF24 - #if 0 - display.addLogMessage("Start RF24 radio"); radio.begin(); - // Open a writing and reading pipe on each radio, with opposite addresses - if (isMaster){ - radio.openWritingPipe(addresses[1]); - radio.openReadingPipe(1,addresses[0]); - } else { - radio.openWritingPipe(addresses[0]); - radio.openReadingPipe(1,addresses[1]); - } - - // Start the radio listening for data - radio.startListening(); - #endif - fastclock.begin(); } @@ -231,122 +213,21 @@ void loop(void) return; } -#if defined(AUTOSTART_CLOCK_AFTER_ms) -#if AUTOSTART_CLOCK_AFTER_ms > 0 - static unsigned long autostart_ms = 0; - static bool autostartDone = false; - if (autostart_ms == 0) { - autostart_ms = millis() + AUTOSTART_CLOCK_AFTER_ms; - } else if (!autostartDone && millis() > autostart_ms) { - autostartDone = true; - fastclock.setClockHalted(false); - Serial.println("Clock started automatically"); - } -#endif -#endif + #if defined(AUTOSTART_CLOCK_AFTER_ms) + #if AUTOSTART_CLOCK_AFTER_ms > 0 + static unsigned long autostart_ms = 0; + static bool autostartDone = false; + if (autostart_ms == 0) { + autostart_ms = millis() + AUTOSTART_CLOCK_AFTER_ms; + } else if (!autostartDone && millis() > autostart_ms) { + autostartDone = true; + fastclock.setClockHalted(false); + Serial.println("Clock started automatically"); + } + #endif + #endif fastclock.loop(); display.showDashboard(); - #if 0 - // put your main code here, to run repeatedly: - /****************** Ping Out Role ***************************/ - if (isMaster) { - - radio.stopListening(); - - Serial.println(F("Now sending")); - - unsigned long start_time = micros(); - if (!radio.write(&start_time, sizeof(unsigned long))) { - Serial.println(F("failed")); - } - - radio.startListening(); - - unsigned long started_waiting_at = micros(); - boolean timeout = false; - - while (!radio.available()) { - if (micros() - started_waiting_at > 200000 ) { // If waited longer than 200ms, indicate timeout and exit while loop - timeout = true; - break; - } - } - - if (timeout) { - Serial.println(F("Failed, response timed out.")); - } else { - unsigned long got_time; - 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 (!isMaster) - { - 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); - } - } - #endif } - - -#if 0 -#ifdef RF_RadioHead - if (!Datagram.init()) { - LOG(LL_ERROR, ("*** Datagram init failed")); - } -#endif - -#ifdef RF_RF24 - radio.begin(); - if (radio.isChipConnected()) { LOG(LL_INFO, ("*** RF chip found")); } - else { LOG(LL_ERROR, ("*** ERROR: RF chip not found!")); } - radio.setChannel(1); - radio.setPALevel(RF24_PA_MAX); - radio.setDataRate(RF24_2MBPS); - radio.setAutoAck(0); - //radio.setRetries(2,15); // Optionally, increase the delay between retries & # of retries - radio.setCRCLength(RF24_CRC_8); - radio.openWritingPipe(pipes[0]); - radio.openReadingPipe(1,pipes[1]); - radio.startListening(); - radio.printDetails(); - // @TODO: real random seed! - //randomSeed(analogRead(0)); - //randomSeed(22); - radio.powerUp(); - LOG(LL_INFO, ("*** RF payload size=%d bytes", radio.getPayloadSize())); - if (radio.testCarrier() || radio.testRPD()) { LOG(LL_INFO, ("*** Carrier/RPD seen on radio")); } - if (radio.failureDetected) { LOG(LL_ERROR, ("*** Radio error detected!")); } -#endif -#endif diff --git a/src/radio.cpp b/src/radio.cpp new file mode 100644 index 0000000..5a1f4c6 --- /dev/null +++ b/src/radio.cpp @@ -0,0 +1,124 @@ +#include +#include +#include +#include "config.h" +#include "radio.h" + +static RF24 rf24(PIN_NRF24_CE, PIN_NRF24_CSN); // 10, 8 +static byte addresses[][6] = {NETWORK_ADDRESS_MASTER_SEND, NETWORK_ADDRESS_MASTER_RECEIVE}; + +void Radio::begin(void) { + display->addLogMessage("Start RF24 radio"); + pinMode(PIN_NRF24_CSN, OUTPUT); + pinMode(PIN_NRF24_CE, OUTPUT); + return; + rf24.begin(); + if (rf24.isChipConnected()) { display->addLogMessage("*** RF chip found"); } + else { display->addLogMessage("*** ERROR: RF chip not found!"); } + rf24.setChannel(1); + rf24.setPALevel(RF24_PA_MAX); + rf24.setDataRate(RF24_2MBPS); + rf24.setAutoAck(0); + //radio.setRetries(2,15); // Optionally, increase the delay between retries & # of retries + rf24.setCRCLength(RF24_CRC_8); + rf24.openWritingPipe(addresses[0]); + rf24.openReadingPipe(1, addresses[1]); + rf24.startListening(); + rf24.printDetails(); + // @TODO: real random seed! + //randomSeed(analogRead(0)); + //randomSeed(22); + rf24.powerUp(); + Serial.print("*** RF payload size="); Serial.print(rf24.getPayloadSize()); Serial.println(" bytes"); + if (rf24.testCarrier() || rf24.testRPD()) { display->addLogMessage("*** Carrier/RPD seen on radio"); } + if (rf24.failureDetected) { display->addLogMessage("*** Radio error detected!"); } + + +#if 0 + // Open a writing and reading pipe on each radio, with opposite addresses + if (isMaster){ + rf24.openWritingPipe(addresses[1]); + rf24.openReadingPipe(1,addresses[0]); + } else { + rf24.openWritingPipe(addresses[0]); + rf24.openReadingPipe(1,addresses[1]); + } + +#endif + + // Start the radio listening for data + rf24.startListening(); +} + + +void Radio::loop(void) { + #if 0 + // put your main code here, to run repeatedly: + /****************** Ping Out Role ***************************/ + if (isMaster) { + + radio.stopListening(); + + Serial.println(F("Now sending")); + + unsigned long start_time = micros(); + if (!radio.write(&start_time, sizeof(unsigned long))) { + Serial.println(F("failed")); + } + + radio.startListening(); + + unsigned long started_waiting_at = micros(); + boolean timeout = false; + + while (!radio.available()) { + if (micros() - started_waiting_at > 200000 ) { // If waited longer than 200ms, indicate timeout and exit while loop + timeout = true; + break; + } + } + + if (timeout) { + Serial.println(F("Failed, response timed out.")); + } else { + unsigned long got_time; + 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 (!isMaster) + { + 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); + } + } + #endif +} diff --git a/src/radio.h b/src/radio.h new file mode 100644 index 0000000..2cf0c5f --- /dev/null +++ b/src/radio.h @@ -0,0 +1,15 @@ +#ifndef radio_h_included +#define radio_h_included +#include "config.h" +#include "display.h" + +class Radio { +public: + Radio(Display *d, bool _isMaster):display(d), isMaster(_isMaster) { }; + void begin(void); + void loop(void); +private: + Display *display; + bool isMaster; +}; +#endif