diff --git a/src/clockMsg.h b/src/clockMsg.h index af430d3..82dddb8 100644 --- a/src/clockMsg.h +++ b/src/clockMsg.h @@ -5,10 +5,14 @@ struct clockMsg_s { uint8_t msgType; + uint8_t day; uint8_t hour; uint8_t minute; 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 diff --git a/src/config.h b/src/config.h index 60d730a..720d671 100644 --- a/src/config.h +++ b/src/config.h @@ -18,4 +18,8 @@ #define DEFAULT_MIN_RELAY_OFF_TIME_MS 80 #define DEFAULT_RELAY_ACTIVE_LOW true +// field sizes +#define MAX_CLOCK_NAME_LEN 8 +#define MAX_CLIENT_NAME_LEN 10 + #endif diff --git a/src/display.h b/src/display.h index d711a51..613cdbf 100644 --- a/src/display.h +++ b/src/display.h @@ -1,15 +1,16 @@ #ifndef display_h_included #define display_h_included +#include "config.h" + // avoid flickering of the display: #define TIME_BETWEEN_DISPLAY_UPDATES_ms 200 #define BLINK_ON_OFF_TIME_ms 1000 -#define MAX_CLOCK_NAME_LEN 8 + #define MAX_CLOCK_SPEED_LEN 8 #define MAX_CLOCK_WEEKDAY_LEN 4 #define MAX_NUMBER_CLIENTS_DISPLAYED 5 -#define MAX_CLIENT_NAME_LEN 10 #define MAX_LOG_MESSAGE_LEN 30 #define MAX_NUMBER_LOG_LINES 5 diff --git a/src/fastclock.cpp b/src/fastclock.cpp index c4bc69d..fe6c898 100644 --- a/src/fastclock.cpp +++ b/src/fastclock.cpp @@ -67,7 +67,7 @@ void Fastclock::loop(void) { // time has changed, send an update via rf // @TODO implement sending radio message Serial.println("Would send new time"); - radio->broadcastClock(hour, minute, second); + radio->broadcastClock(weekday, hour, minute, second, msPerModelSecond); } } diff --git a/src/fastclock.h b/src/fastclock.h index 563e58d..edc49d2 100644 --- a/src/fastclock.h +++ b/src/fastclock.h @@ -1,6 +1,7 @@ #ifndef fastclock_h_included #define fastclock_h_included +#include "config.h" #include "display.h" #include "radio.h" diff --git a/src/main.cpp b/src/main.cpp index 1e2f151..e03276e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -207,12 +207,20 @@ void setup() { fastclock.begin(); } +bool requestToRegisterSent = false; + void loop(void) { if (!display.showBootSequenceFinished(1000)) { return; } + if (!requestToRegisterSent) { + radio.broadcastRequestRegistration("testClock"); + requestToRegisterSent = true; + return; + } + #if defined(AUTOSTART_CLOCK_AFTER_ms) #if AUTOSTART_CLOCK_AFTER_ms > 0 static unsigned long autostart_ms = 0; diff --git a/src/radio.cpp b/src/radio.cpp index 37eff42..9fd0d12 100644 --- a/src/radio.cpp +++ b/src/radio.cpp @@ -76,15 +76,44 @@ static void switchToReceiverRole(RF24 radio) static int sendFailedCounter = 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; clockMsg.msgType = msgType_Clock; + clockMsg.day = day; clockMsg.hour = hour; clockMsg.minute = minute; clockMsg.second = second; + clockMsg.speed = (msPerModelSecond >> 2) & 0xff; // divide by 4 switchToSenderRole(rf24); - if (!rf24.write(&clockMsg, sizeof(clockMsg), true /*multicast*/)) { + if (!rf24.writeFast(&clockMsg, sizeof(clockMsg), true /*multicast*/)) { sendFailedCounter++; 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) { switchToReceiverRole(rf24); if (rf24.available()) { - struct clockMsg_s clockMsg; - rf24.read(&clockMsg, sizeof(struct clockMsg_s)); + char buffer[32]; + rf24.read(buffer, 32); // Spew it - Serial.print(F("Received clock message, type=")); - Serial.print(clockMsg.msgType); - Serial.print(F(", time=")); - Serial.print(clockMsg.hour); - Serial.print(":"); - Serial.print(clockMsg.minute); - Serial.print(":"); - Serial.println(clockMsg.second); + Serial.print(F("Received new message, type=")); + Serial.print(buffer[0]); + Serial.print(F(", bytes 1=")); + Serial.print(buffer[1]); + Serial.print(", 2="); + Serial.print(buffer[2]); + Serial.print(", 3="); + Serial.println(buffer[3]); } } diff --git a/src/radio.h b/src/radio.h index c35c57e..72218d1 100644 --- a/src/radio.h +++ b/src/radio.h @@ -8,7 +8,8 @@ public: Radio(Display *d, bool _isMaster):display(d), isMaster(_isMaster) { }; void begin(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: Display *display; bool isMaster;