unfinished changes, just to be able to transfer files

This commit is contained in:
Dirk Jahnke 2018-11-13 08:07:54 +01:00
parent 7ad2d6e404
commit 08a8edb68a
5 changed files with 167 additions and 129 deletions

15
mos.yml
View File

@ -11,19 +11,25 @@ platform: esp8266
sources: sources:
- src - src
- src/RadioHead #- src/RadioHead
- src/RF24
includes: includes:
- src/RadioHead #- src/RadioHead
- src/RF24
filesystem: filesystem:
- fs - fs
cdefs: cdefs:
MPIDE: 0 #RF_RadioHead
TEENSYDUINO: 0 # MPIDE: 0
RH_PLATFORM: RH_PLATFORM_ESP8266 RH_PLATFORM: RH_PLATFORM_ESP8266
MONGOOSE_OS: 1 MONGOOSE_OS: 1
ESP_PLATFORM:
RF_RF24:
RF24_SPI_TRANSACTIONS:
FAILURE_HANDLING:
libs: libs:
# common mgos libs # common mgos libs
@ -55,6 +61,7 @@ config_schema:
- ["i2c.scl_gpio", 5] - ["i2c.scl_gpio", 5]
- ["i2c.sda_gpio", 4] - ["i2c.sda_gpio", 4]
- ["i2c.freq", 1000000] - ["i2c.freq", 1000000]
- ["spi.enable", true]
- ["wifi.ap.ssid", "Fastclock-Master-??????"] - ["wifi.ap.ssid", "Fastclock-Master-??????"]
- ["wifi.ap.pass", "Fastclock-Master"] - ["wifi.ap.pass", "Fastclock-Master"]
- ["wifi.sta.ssid", "Pinguin"] - ["wifi.sta.ssid", "Pinguin"]

View File

@ -41,7 +41,7 @@ void RF24::csn(bool mode)
#if !defined (SOFTSPI) #if !defined (SOFTSPI)
_SPI.setBitOrder(MSBFIRST); _SPI.setBitOrder(MSBFIRST);
_SPI.setDataMode(SPI_MODE0); _SPI.setDataMode(SPI_MODE0);
_SPI.setClockDivider(SPI_CLOCK_DIV2); //_SPI.setClockDivider(SPI_CLOCK_DIV2);
#endif #endif
#elif defined (RF24_RPi) #elif defined (RF24_RPi)
if(!mode) if(!mode)

View File

@ -146,11 +146,13 @@
typedef uint16_t prog_uint16_t; typedef uint16_t prog_uint16_t;
#if !defined(PSTR)
#define PSTR(x) (x) #define PSTR(x) (x)
#define printf_P printf #define printf_P printf
#define strlen_P strlen #define strlen_P strlen
#define PROGMEM #define PROGMEM
#define pgm_read_word(p) (*(p)) #define pgm_read_word(p) (*(p))
#endif
#define PRIPSTR "%s" #define PRIPSTR "%s"
@ -161,4 +163,3 @@
#endif // __RF24_CONFIG_H__ #endif // __RF24_CONFIG_H__

View File

@ -886,7 +886,7 @@
// Try to be compatible with systems that support yield() and multitasking // Try to be compatible with systems that support yield() and multitasking
// instead of spin-loops // instead of spin-loops
// Recent Arduino IDE or Teensy 3 has yield() // Recent Arduino IDE or Teensy 3 has yield()
#if (RH_PLATFORM == RH_PLATFORM_ARDUINO && ARDUINO >= 155 && !defined(RH_PLATFORM_ATTINY)) || (TEENSYDUINO && defined(__MK20DX128__)) #if (RH_PLATFORM == RH_PLATFORM_ARDUINO && ARDUINO >= 155 && !defined(RH_PLATFORM_ATTINY)) || (defined(TEENSYDUINO) && TEENSYDUINO && defined(__MK20DX128__))
#define YIELD yield(); #define YIELD yield();
#elif (RH_PLATFORM == RH_PLATFORM_ESP8266) #elif (RH_PLATFORM == RH_PLATFORM_ESP8266)
// ESP8266 also hash it // ESP8266 also hash it

View File

@ -2,13 +2,14 @@
* Copyright (c) 2014-2017 Cesanta Software Limited * Copyright (c) 2014-2017 Cesanta Software Limited
* All rights reserved * All rights reserved
*/ */
#define RF_RadioHead //#define RF_RadioHead
//#define RF_RF24 //#define RF_RF24
#include <Arduino.h> #include <Arduino.h>
#include <Wire.h> #include <Wire.h>
#include <Adafruit_GFX.h> #include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h> #include <Adafruit_SSD1306.h>
//#include <SPI.h>
#ifdef RF_RadioHead #ifdef RF_RadioHead
#include "RadioHead/RH_NRF24.h" #include "RadioHead/RH_NRF24.h"
#include "RadioHead/RHDatagram.h" #include "RadioHead/RHDatagram.h"
@ -67,17 +68,18 @@ RHDatagram Datagram(nrf24, THIS_ADRESS);
#endif #endif
#ifdef RF_RF24 #ifdef RF_RF24
RF24 radio(PIN_NRF24_CSN,PIN_NRF24_CE); // Set up nRF24L01 radio on SPI bus plus pins 7 & 8 RF24 radio(PIN_NRF24_CE, PIN_NRF24_CSN); // Set up nRF24L01 radio on SPI bus plus pins 7 & 8
const uint64_t pipes[2] = { 0xABCDABCD71LL, 0x544d52687CLL }; // Radio pipe addresses for the 2 nodes to communicate. const uint64_t pipes[2] = { 0xABCDABCD71LL, 0x544d52687CLL }; // Radio pipe addresses for the 2 nodes to communicate.
byte data[32]; //Data buffer for testing data transfer speeds byte data[32]; //Data buffer for testing data transfer speeds
unsigned long sendFailedCounter=0, rxTimer; //Counter and timer for keeping track transfer info unsigned long sendFailedCounter=0, rxTimer; //Counter and timer for keeping track transfer info
unsigned long receivedCounter=0; unsigned long receivedCounter=0;
unsigned long startTime, stopTime; unsigned long startTime, stopTime, pauseTime;
bool TX=1,RX=0,role=0; bool TX=1,RX=0,role=0;
#endif #endif
//static SPIImpl SPI;
static void setSmallTextSize(void) { display->setFont(&TomThumb); } static void setSmallTextSize(void) { display->setFont(&TomThumb); }
@ -91,6 +93,8 @@ static uint8_t getNormalTextHeight() { return Org_01.yAdvance; }
static void setLargeTextSize(void) { display->setFont(&FreeMonoBold9pt7b); } static void setLargeTextSize(void) { display->setFont(&FreeMonoBold9pt7b); }
//static uint8_t getLargeTextHeight() { return FreeMonoBold9pt7b.yAdvance; } //static uint8_t getLargeTextHeight() { return FreeMonoBold9pt7b.yAdvance; }
//static uint8_t getLargeTextCharsPerLine() { return 12; } //static uint8_t getLargeTextCharsPerLine() { return 12; }
// Forward declarations:
static void updateDisplay_cb(void *arg); static void updateDisplay_cb(void *arg);
static void fastclockRF_receive_cb(void *arg); static void fastclockRF_receive_cb(void *arg);
static void fastclockRF_send_cb(void *arg); static void fastclockRF_send_cb(void *arg);
@ -140,19 +144,25 @@ static void initDisplay_cb(void *arg) {
break; break;
} }
++step; ++step;
if (step <= 3) mgos_set_timer(600 /* ms */, false /* repeat */, initDisplay_cb, NULL); if (step <= 3) mgos_set_timer(400 /* ms */, false /* repeat */, initDisplay_cb, NULL);
} }
void setup(void) { void setup(void) {
LOG(LL_INFO, ("*** Setup started")); LOG(LL_INFO, ("*** Setup started"));
#ifdef RF_RadioHead
if (!Datagram.init()) #if 0
LOG(LL_ERROR, ("*** Datagram init failed")); struct mgos_spi *spi;
spi = mgos_spi_get_global();
if (spi == NULL) {
LOG(LL_ERROR, ("SPI is not configured, make sure spi.enable is true"));
return;
} }
#endif #endif
LOG(LL_INFO, ("*** Setting timer")); LOG(LL_INFO, ("*** Setting timer"));
mgos_set_timer(2000 /* ms */, false /* repeat */, initDisplay_cb, NULL); mgos_set_timer(1000 /* ms */, false /* repeat */, initDisplay_cb, NULL);
mgos_set_timer(7000 /* ms */, false /* repeat */, initFastclockRF_cb, NULL); mgos_set_timer(5000 /* ms */, false /* repeat */, initFastclockRF_cb, NULL);
LOG(LL_INFO, ("*** Setup done")); LOG(LL_INFO, ("*** Setup done"));
} }
@ -267,6 +277,7 @@ static void switchToReceiverRole()
static void fastclockRF_receive_cb(void *arg) { static void fastclockRF_receive_cb(void *arg) {
(void) arg; (void) arg;
LOG(LL_INFO, ("*** Rcv RF"));
#ifdef RF_RadioHead #ifdef RF_RadioHead
// check for incoming messages // check for incoming messages
if (Datagram.available()) if (Datagram.available())
@ -287,22 +298,24 @@ static void fastclockRF_receive_cb(void *arg) {
#endif #endif
#ifdef RF_RF24 #ifdef RF_RF24
uint8_t buf[RH_MAX_MESSAGE_LEN]; #define RF24_MAX_MESSAGE_LEN 32
uint8_t buf[RF24_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf); uint8_t len = sizeof(buf);
unsigned int counger=0; unsigned int counter=0;
switchToReceiverRole(); switchToReceiverRole();
while (radio.available()) { if (radio.available()) {
radio.read(buf, len); radio.read(buf, len);
counter++; counter++;
LOG(LL_INFO, ("%04d: %02x %02x %02x %02x %02x %02x", counter, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]));
} }
if (millis() - rxTimer > 1000) { if (millis() - rxTimer > 1000) {
rxTimer = millis(); rxTimer = millis();
receivedCounter += counter; receivedCounter += counter;
unsigned long numBytes = counter*len; unsigned long numBytes = counter*len;
LOG(LL_INFO, ("Bytes: %d, Msg count: %d", numBytes, counter)); LOG(LL_INFO, ("Bytes: %ld, Msg count: %d", numBytes, counter));
counter = 0; counter = 0;
} }
#endif #endif
} }
@ -327,8 +340,8 @@ static void fastclockRF_send_cb(void *arg) {
#ifdef RF_RF24 #ifdef RF_RF24
switchToSenderRole(); switchToSenderRole();
if (!radio.writeFast(&clockMsg,sizeof(clockMsg))) { //Write to the FIFO buffers if (!radio.write(&clockMsg, sizeof(clockMsg), true /*multicast*/)) {
sendFailedCounter++; //Keep count of failed payloads sendFailedCounter++;
} }
//This is only required when NO ACK ( enableAutoAck(0) ) payloads are used //This is only required when NO ACK ( enableAutoAck(0) ) payloads are used
@ -342,6 +355,7 @@ static void fastclockRF_send_cb(void *arg) {
if (!radio.txStandBy()) { sendFailedCounter += 3; } //Standby, block only until FIFO empty or auto-retry timeout. Flush TX FIFO if failed if (!radio.txStandBy()) { sendFailedCounter += 3; } //Standby, block only until FIFO empty or auto-retry timeout. Flush TX FIFO if failed
//radio.txStandBy(1000); //Standby, using extended timeout period of 1 second //radio.txStandBy(1000); //Standby, using extended timeout period of 1 second
#endif #endif
LOG(LL_INFO, ("*** send finished"));
} }
static void timeTick_cb(void *arg) { static void timeTick_cb(void *arg) {
@ -352,23 +366,32 @@ static void timeTick_cb(void *arg) {
incrementClockByMilliseconds(fastclockTimeAdvance); incrementClockByMilliseconds(fastclockTimeAdvance);
//lastSentTimeTick += fastclockTimeAdvance * msPerModelSecond/1000; //lastSentTimeTick += fastclockTimeAdvance * msPerModelSecond/1000;
lastSentTimeTick = newTimeTick; lastSentTimeTick = newTimeTick;
LOG(LL_INFO, ("*** tick (adv=%d)", fastclockTimeAdvance));
} }
static void initFastclockRF_cb(void *arg) { static void initFastclockRF_cb(void *arg) {
(void) arg; (void) arg;
LOG(LL_INFO, ("*** Setting up RF"));
lastSentTimeTick = millis(); lastSentTimeTick = millis();
LOG(LL_INFO, ("*** Setting up RF, init lastSentTimeTick=%ld", lastSentTimeTick));
fastclock.day = 0; fastclock.day = 0;
fastclock.hour = 0; fastclock.hour = 0;
fastclock.minute = 0; fastclock.minute = 0;
fastclock.second = 0; fastclock.second = 0;
fastclock.millisecond = 0; fastclock.millisecond = 0;
#ifdef RF_RadioHead
if (!Datagram.init()) {
LOG(LL_ERROR, ("*** Datagram init failed"));
}
#endif
#ifdef RF_RF24 #ifdef RF_RF24
radio.begin(); radio.begin();
if (radio.isChipConnected()) { LOG(LL_INFO, ("*** RF chip found")); }
else { LOG(LL_ERROR, ("*** ERROR: RF chip not found!")); }
radio.setChannel(1); radio.setChannel(1);
radio.setPALevel(RF24_PA_MAX); radio.setPALevel(RF24_PA_MAX);
radio.setDataRate(RF24_1MBPS); radio.setDataRate(RF24_2MBPS);
radio.setAutoAck(0); radio.setAutoAck(0);
//radio.setRetries(2,15); // Optionally, increase the delay between retries & # of retries //radio.setRetries(2,15); // Optionally, increase the delay between retries & # of retries
radio.setCRCLength(RF24_CRC_8); radio.setCRCLength(RF24_CRC_8);
@ -376,14 +399,21 @@ static void initFastclockRF_cb(void *arg) {
radio.openReadingPipe(1,pipes[1]); radio.openReadingPipe(1,pipes[1]);
radio.startListening(); radio.startListening();
radio.printDetails(); radio.printDetails();
randomSeed(analogRead(0)); // @TODO: real random seed!
//randomSeed(analogRead(0));
//randomSeed(22);
radio.powerUp(); 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
LOG(LL_INFO, ("*** Setting up timer tasks"));
mgos_set_timer(100 /* ms */, true /* repeat */, fastclockRF_receive_cb, NULL); mgos_set_timer(100 /* ms */, true /* repeat */, fastclockRF_receive_cb, NULL);
mgos_set_timer(500 /* ms */, true /* repeat */, timeTick_cb, NULL); mgos_set_timer(500 /* ms */, true /* repeat */, timeTick_cb, NULL);
mgos_set_timer(3000 /* ms */, true /* repeat */, fastclockRF_send_cb, NULL); mgos_set_timer(3000 /* ms */, true /* repeat */, fastclockRF_send_cb, NULL);
mgos_set_timer(1000 /* ms */, true /* repeat */, updateDisplay_cb, NULL); mgos_set_timer(1000 /* ms */, true /* repeat */, updateDisplay_cb, NULL);
LOG(LL_INFO, ("*** Setting up RF done"));
} }
static void updateDisplay_cb(void *arg) { static void updateDisplay_cb(void *arg) {