Send and transmit using radio works now. Sends initial request for registration.

This commit is contained in:
Dirk Jahnke 2018-11-15 18:23:35 +01:00
parent fbf8af5684
commit 2eda00c694
8 changed files with 65 additions and 17 deletions

View File

@ -5,10 +5,14 @@
struct clockMsg_s { struct clockMsg_s {
uint8_t msgType; uint8_t msgType;
uint8_t day;
uint8_t hour; uint8_t hour;
uint8_t minute; uint8_t minute;
uint8_t second; uint8_t second;
uint8_t speed; // msPerModelSecond / 4 --> 0..250
}; };
#define msgType_Clock 'c' #define msgType_Clock 'c' /* clock update, sent by master using broadcast */
#define msgType_ReqReg 'R' /* Request Registration, sent by Master using broadcast; ask clients to register */
#define msgType_Reg 'r' /* Registration message, sent by Client using 1:1 message to master */
#endif #endif

View File

@ -18,4 +18,8 @@
#define DEFAULT_MIN_RELAY_OFF_TIME_MS 80 #define DEFAULT_MIN_RELAY_OFF_TIME_MS 80
#define DEFAULT_RELAY_ACTIVE_LOW true #define DEFAULT_RELAY_ACTIVE_LOW true
// field sizes
#define MAX_CLOCK_NAME_LEN 8
#define MAX_CLIENT_NAME_LEN 10
#endif #endif

View File

@ -1,15 +1,16 @@
#ifndef display_h_included #ifndef display_h_included
#define display_h_included #define display_h_included
#include "config.h"
// avoid flickering of the display: // avoid flickering of the display:
#define TIME_BETWEEN_DISPLAY_UPDATES_ms 200 #define TIME_BETWEEN_DISPLAY_UPDATES_ms 200
#define BLINK_ON_OFF_TIME_ms 1000 #define BLINK_ON_OFF_TIME_ms 1000
#define MAX_CLOCK_NAME_LEN 8
#define MAX_CLOCK_SPEED_LEN 8 #define MAX_CLOCK_SPEED_LEN 8
#define MAX_CLOCK_WEEKDAY_LEN 4 #define MAX_CLOCK_WEEKDAY_LEN 4
#define MAX_NUMBER_CLIENTS_DISPLAYED 5 #define MAX_NUMBER_CLIENTS_DISPLAYED 5
#define MAX_CLIENT_NAME_LEN 10
#define MAX_LOG_MESSAGE_LEN 30 #define MAX_LOG_MESSAGE_LEN 30
#define MAX_NUMBER_LOG_LINES 5 #define MAX_NUMBER_LOG_LINES 5

View File

@ -67,7 +67,7 @@ void Fastclock::loop(void) {
// time has changed, send an update via rf // time has changed, send an update via rf
// @TODO implement sending radio message // @TODO implement sending radio message
Serial.println("Would send new time"); Serial.println("Would send new time");
radio->broadcastClock(hour, minute, second); radio->broadcastClock(weekday, hour, minute, second, msPerModelSecond);
} }
} }

View File

@ -1,6 +1,7 @@
#ifndef fastclock_h_included #ifndef fastclock_h_included
#define fastclock_h_included #define fastclock_h_included
#include "config.h"
#include "display.h" #include "display.h"
#include "radio.h" #include "radio.h"

View File

@ -207,12 +207,20 @@ void setup() {
fastclock.begin(); fastclock.begin();
} }
bool requestToRegisterSent = false;
void loop(void) void loop(void)
{ {
if (!display.showBootSequenceFinished(1000)) { if (!display.showBootSequenceFinished(1000)) {
return; return;
} }
if (!requestToRegisterSent) {
radio.broadcastRequestRegistration("testClock");
requestToRegisterSent = true;
return;
}
#if defined(AUTOSTART_CLOCK_AFTER_ms) #if defined(AUTOSTART_CLOCK_AFTER_ms)
#if AUTOSTART_CLOCK_AFTER_ms > 0 #if AUTOSTART_CLOCK_AFTER_ms > 0
static unsigned long autostart_ms = 0; static unsigned long autostart_ms = 0;

View File

@ -76,15 +76,44 @@ static void switchToReceiverRole(RF24 radio)
static int sendFailedCounter = 0; static int sendFailedCounter = 0;
static unsigned long stopTime = 0, pauseTime = 0; static unsigned long stopTime = 0, pauseTime = 0;
void Radio::broadcastClock(int hour, int minute, int second) { void Radio::broadcastRequestRegistration(const char *clockName) {
char sendBuffer[32];
memset(sendBuffer, 0, 32);
sendBuffer[0] = msgType_ReqReg;
strncpy(sendBuffer+1, clockName, MAX_CLOCK_NAME_LEN);
int msgLength = MAX_CLOCK_NAME_LEN + 1;
switchToSenderRole(rf24);
if (!rf24.writeFast(sendBuffer, msgLength, true /*multicast*/)) {
sendFailedCounter++;
Serial.print("*** ERROR: failed to send ReqRegistration msg for "); Serial.println(clockName);
}
//This is only required when NO ACK ( enableAutoAck(0) ) payloads are used
if (millis() - pauseTime > 3) {
pauseTime = millis();
rf24.txStandBy(); // Need to drop out of TX mode every 4ms if sending a steady stream of multicast data
//delayMicroseconds(130); // This gives the PLL time to sync back up
}
stopTime = millis();
//This should be called to wait for completion and put the radio in standby mode after transmission, returns 0 if data still in FIFO (timed out), 1 if success
if (!rf24.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
Serial.print("*** send finished, fail-counter="); Serial.println(sendFailedCounter);
}
void Radio::broadcastClock(int day, int hour, int minute, int second, int msPerModelSecond) {
struct clockMsg_s clockMsg; struct clockMsg_s clockMsg;
clockMsg.msgType = msgType_Clock; clockMsg.msgType = msgType_Clock;
clockMsg.day = day;
clockMsg.hour = hour; clockMsg.hour = hour;
clockMsg.minute = minute; clockMsg.minute = minute;
clockMsg.second = second; clockMsg.second = second;
clockMsg.speed = (msPerModelSecond >> 2) & 0xff; // divide by 4
switchToSenderRole(rf24); switchToSenderRole(rf24);
if (!rf24.write(&clockMsg, sizeof(clockMsg), true /*multicast*/)) { if (!rf24.writeFast(&clockMsg, sizeof(clockMsg), true /*multicast*/)) {
sendFailedCounter++; sendFailedCounter++;
Serial.print("*** ERROR: failed to send clock msg for "); Serial.print(hour); Serial.print(":"); Serial.print(minute); Serial.print(":"); Serial.println(second); Serial.print("*** ERROR: failed to send clock msg for "); Serial.print(hour); Serial.print(":"); Serial.print(minute); Serial.print(":"); Serial.println(second);
} }
@ -108,18 +137,18 @@ void Radio::loop(void) {
if (isMaster) { if (isMaster) {
switchToReceiverRole(rf24); switchToReceiverRole(rf24);
if (rf24.available()) { if (rf24.available()) {
struct clockMsg_s clockMsg; char buffer[32];
rf24.read(&clockMsg, sizeof(struct clockMsg_s)); rf24.read(buffer, 32);
// Spew it // Spew it
Serial.print(F("Received clock message, type=")); Serial.print(F("Received new message, type="));
Serial.print(clockMsg.msgType); Serial.print(buffer[0]);
Serial.print(F(", time=")); Serial.print(F(", bytes 1="));
Serial.print(clockMsg.hour); Serial.print(buffer[1]);
Serial.print(":"); Serial.print(", 2=");
Serial.print(clockMsg.minute); Serial.print(buffer[2]);
Serial.print(":"); Serial.print(", 3=");
Serial.println(clockMsg.second); Serial.println(buffer[3]);
} }
} }

View File

@ -8,7 +8,8 @@ public:
Radio(Display *d, bool _isMaster):display(d), isMaster(_isMaster) { }; Radio(Display *d, bool _isMaster):display(d), isMaster(_isMaster) { };
void begin(void); void begin(void);
void loop(void); void loop(void);
void broadcastClock(int hour, int minute, int second); void broadcastClock(int day, int hour, int minute, int second, int msPerModelSecond);
void broadcastRequestRegistration(const char *clockName);
private: private:
Display *display; Display *display;
bool isMaster; bool isMaster;