67 lines
1.6 KiB
C++
67 lines
1.6 KiB
C++
#include <Arduino.h>
|
|
#include "config.h"
|
|
#include "clockMsg.h"
|
|
#include "master.h"
|
|
|
|
static struct clockMsg_s clockMsg;
|
|
|
|
void masterInit() {
|
|
clockMsg.hour = 0;
|
|
clockMsg.minute = 0;
|
|
clockMsg.second = 0;
|
|
}
|
|
|
|
void masterLoop(RHDatagram Datagram)
|
|
{
|
|
static unsigned long nextTimeTick_ms = millis();
|
|
static unsigned long updateEvery_ms = 1000;
|
|
static uint8_t hour=0, minute=0, second=0;
|
|
|
|
if (Datagram.available())
|
|
{
|
|
// Should be a message for us now
|
|
uint8_t buf[RH_MAX_MESSAGE_LEN];
|
|
uint8_t len = sizeof(buf);
|
|
uint8_t from, to, id, flags;
|
|
|
|
if (Datagram.recvfrom(buf, &len, &from, &to, &id, &flags)) {
|
|
Serial.print("got request: ");
|
|
Serial.println((char*)buf);
|
|
}
|
|
else
|
|
{
|
|
Serial.println("*** Datagram.recvfrom failed");
|
|
}
|
|
} else {
|
|
// prepare clock info
|
|
if (nextTimeTick_ms < millis()) {
|
|
nextTimeTick_ms += updateEvery_ms;
|
|
second++;
|
|
if (second >= 60) {
|
|
second -= 60;
|
|
minute++;
|
|
if (minute >= 60) {
|
|
minute -= 60;
|
|
hour++;
|
|
if (hour >= 24) {
|
|
hour -= 24;
|
|
}
|
|
}
|
|
}
|
|
clockMsg.msgType = msgType_Clock;
|
|
clockMsg.hour = hour;
|
|
clockMsg.minute = minute;
|
|
clockMsg.second = second;
|
|
|
|
// send clock info as a broadcast message
|
|
|
|
if (Datagram.sendto((uint8_t *) &clockMsg, sizeof(clockMsg), RH_BROADCAST_ADDRESS)) {
|
|
Serial.print(hour); Serial.print(":");
|
|
Serial.print(minute); Serial.print(":");
|
|
Serial.print(second); Serial.print(" - ");
|
|
Serial.println("Sent new clock tick");
|
|
}
|
|
}
|
|
}
|
|
}
|