227 lines
8.3 KiB
C++
227 lines
8.3 KiB
C++
#include <Arduino.h>
|
|
#include "config.h"
|
|
#include "clockMsg.h"
|
|
#include "client.h"
|
|
|
|
|
|
static struct clockMsg_s clockMsg;
|
|
int holdRelay_ms = DEFAULT_HOLD_RELAY_MS;
|
|
int minRelayOffTime_ms = DEFAULT_MIN_RELAY_OFF_TIME_MS;
|
|
boolean relayActiveLow = DEFAULT_RELAY_ACTIVE_LOW;
|
|
|
|
static struct {
|
|
uint8_t hour;
|
|
uint8_t minute;
|
|
uint8_t second;
|
|
} displayedTime;
|
|
|
|
|
|
void clientInit() {
|
|
clockMsg.hour = 0;
|
|
clockMsg.minute = 0;
|
|
clockMsg.second = 0;
|
|
displayedTime.hour = 0;
|
|
displayedTime.minute = 0;
|
|
displayedTime.second = 0;
|
|
pinMode(PIN_RELAY1, OUTPUT);
|
|
pinMode(PIN_RELAY2, OUTPUT);
|
|
#if WITH_DISPLAY
|
|
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
|
|
display.display();
|
|
delay(2000);
|
|
|
|
// Clear the buffer.
|
|
display.clearDisplay();
|
|
|
|
// draw a single pixel
|
|
display.drawPixel(10, 10, WHITE);
|
|
// Show the display buffer on the hardware.
|
|
// NOTE: You _must_ call display after making any drawing commands
|
|
// to make them visible on the display hardware!
|
|
display.display();
|
|
delay(2000);
|
|
display.clearDisplay();
|
|
display.setTextSize(1);
|
|
display.setTextColor(WHITE);
|
|
display.setCursor(0,0);
|
|
display.println("Hello, world!");
|
|
display.println("FREMO FastClock");
|
|
#endif
|
|
}
|
|
|
|
static void updateRelays(struct clockMsg_s currentTime) {
|
|
// to move forward for one minute, one of the relays is turned
|
|
// on for holdRelay_ms milliseconds, then turned off. Next minute,
|
|
// the other relay is turned on for holdRelay_ms.
|
|
static long lastChange_ms = 0;
|
|
static boolean relay1WasActiveLast = false;
|
|
static enum {relayIdle, relayOn, relayOff} relayStatus = relayIdle;
|
|
long current_ms = millis();
|
|
|
|
// Serial.print("currentTime="); Serial.print(currentTime.hour); Serial.print(":"); Serial.print(currentTime.minute); Serial.print(":"); Serial.println(currentTime.second);
|
|
if (relayStatus == relayIdle) {
|
|
if (!((currentTime.hour % 12) == displayedTime.hour && currentTime.minute == displayedTime.minute)) {
|
|
// change updateRelays
|
|
digitalWrite(relay1WasActiveLast ? PIN_RELAY2 : PIN_RELAY1, relayActiveLow ? LOW : HIGH);
|
|
digitalWrite(relay1WasActiveLast ? PIN_RELAY1 : PIN_RELAY2, relayActiveLow ? HIGH : LOW);
|
|
Serial.print("Relay "); Serial.print(relay1WasActiveLast ? 2 : 1); Serial.print(relayActiveLow ? ": LOW, " : ": HIGH, ");
|
|
Serial.print("Relay "); Serial.print(relay1WasActiveLast ? 1 : 2); Serial.println(relayActiveLow ? ": HIGH" : ": LOW");
|
|
Serial.print("last change: "); Serial.print(lastChange_ms); Serial.print(", current: "); Serial.print(current_ms);
|
|
Serial.println(", new relay status: ON");
|
|
relay1WasActiveLast = !relay1WasActiveLast;
|
|
lastChange_ms = current_ms;
|
|
relayStatus = relayOn;
|
|
displayedTime.minute++;
|
|
if (displayedTime.minute >= 60) {
|
|
displayedTime.minute = 0;
|
|
displayedTime.hour++;
|
|
if (displayedTime.hour >= 12) {
|
|
displayedTime.hour = 0;
|
|
}
|
|
}
|
|
Serial.print("displayedTime="); Serial.print(displayedTime.hour); Serial.print(":"); Serial.println(displayedTime.minute);
|
|
}
|
|
} else if (relayStatus == relayOn && current_ms > lastChange_ms + holdRelay_ms) {
|
|
digitalWrite(PIN_RELAY1, relayActiveLow ? HIGH : LOW);
|
|
digitalWrite(PIN_RELAY2, relayActiveLow ? HIGH : LOW);
|
|
Serial.print("Relay 1: "); Serial.print(relayActiveLow ? "HIGH, " : "LOW, ");
|
|
Serial.print("Relay 2: "); Serial.println(relayActiveLow ? "HIGH" : "LOW");
|
|
Serial.print("last change: "); Serial.print(lastChange_ms); Serial.print(", current: "); Serial.print(current_ms);
|
|
Serial.println(", new relay status: OFF");
|
|
lastChange_ms = current_ms;
|
|
relayStatus = relayOff;
|
|
} else if (relayStatus == relayOff && current_ms > lastChange_ms + minRelayOffTime_ms) {
|
|
Serial.print("last change: "); Serial.print(lastChange_ms); Serial.print(", current: "); Serial.print(current_ms);
|
|
Serial.println(", new relay status: IDLE");
|
|
lastChange_ms = current_ms;
|
|
relayStatus = relayIdle;
|
|
}
|
|
}
|
|
|
|
#ifdef FCRF_RadioHead
|
|
#if WITH_DISPLAY
|
|
void clientLoop(RHDatagram Datagram, Adafruit_SSD1306 display)
|
|
#else
|
|
void clientLoop(RHDatagram Datagram)
|
|
#endif
|
|
{
|
|
// if (nrf24.available())
|
|
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)) {
|
|
if (len == sizeof(clockMsg) && buf[0]==msgType_Clock) {
|
|
Serial.print("Clock Msg: ");
|
|
memcpy(&clockMsg, buf, sizeof(clockMsg));
|
|
Serial.print(" h:m:s="); Serial.print(clockMsg.hour); Serial.print(":"); Serial.print(clockMsg.minute); Serial.print(":"); Serial.println(clockMsg.second);
|
|
updateRelays(clockMsg);
|
|
} else {
|
|
Serial.print("got request: "); Serial.print(len); Serial.println(" bytes");
|
|
Serial.print(buf[0], HEX); Serial.print(" ");
|
|
Serial.print(buf[1], HEX); Serial.print(" ");
|
|
Serial.print(buf[2], HEX); Serial.print(" ");
|
|
Serial.print(buf[3], HEX); Serial.print(" ");
|
|
Serial.print(buf[4], HEX); Serial.print(" ");
|
|
Serial.print(buf[5], HEX); Serial.print(" ");
|
|
Serial.print(buf[6], HEX); Serial.print(" ");
|
|
Serial.println(buf[7], HEX);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Serial.println("*** Datagram.recvfrom failed");
|
|
}
|
|
}
|
|
|
|
#if WITH_DISPLAY
|
|
display.clearDisplay();
|
|
display.setTextSize(1);
|
|
display.setTextColor(WHITE);
|
|
display.setCursor(0,0);
|
|
display.println("FREMO FastClock 1.0");
|
|
display.print(displayedTime.hour); display.print(":");
|
|
display.print(displayedTime.minute);
|
|
#endif
|
|
}
|
|
#endif
|
|
|
|
#ifdef FCRF_RF24
|
|
#define RF24_MAX_MESSAGE_LEN 32
|
|
extern const byte *addresses[];
|
|
static bool TX=1,RX=0,role=TX;
|
|
|
|
/*
|
|
static void switchToSenderRole(RF24 radio)
|
|
{
|
|
Serial.println("*** client: CHANGING TO TRANSMIT ROLE");
|
|
radio.openWritingPipe(addresses[1]);
|
|
radio.openReadingPipe(1,addresses[0]);
|
|
radio.stopListening();
|
|
role = TX; // Become the primary transmitter (ping out)
|
|
}
|
|
*/
|
|
|
|
static void switchToReceiverRole(RF24 radio)
|
|
{
|
|
Serial.println("*** client: CHANGING TO RECEIVER ROLE");
|
|
radio.openWritingPipe(addresses[0]);
|
|
radio.openReadingPipe(1,addresses[1]);
|
|
radio.startListening();
|
|
role = RX; // Become the primary receiver (pong back)
|
|
}
|
|
|
|
static unsigned long rxTimer=0, receivedCounter=0;
|
|
|
|
#if WITH_DISPLAY
|
|
void clientLoop(RF24 radio, Adafruit_SSD1306 display)
|
|
#else
|
|
void clientLoop(RF24 radio)
|
|
#endif
|
|
{
|
|
//Serial.println("*** RF Rcf loop");
|
|
uint8_t buf[RF24_MAX_MESSAGE_LEN];
|
|
uint8_t len = sizeof(buf);
|
|
unsigned int counter=0;
|
|
|
|
switchToReceiverRole(radio);
|
|
if (radio.available()) {
|
|
radio.read(buf, len);
|
|
counter++;
|
|
Serial.print(counter); Serial.print(": ");
|
|
Serial.print(buf[0], HEX); Serial.print(" ");
|
|
Serial.print(buf[1], HEX); Serial.print(" ");
|
|
Serial.print(buf[2], HEX); Serial.print(" ");
|
|
Serial.print(buf[3], HEX); Serial.print(" ");
|
|
Serial.print(buf[4], HEX); Serial.print(" ");
|
|
Serial.print(buf[5], HEX); Serial.println(" ");
|
|
if (len == sizeof(clockMsg) && buf[0]==msgType_Clock) {
|
|
Serial.print("Clock Msg: ");
|
|
memcpy(&clockMsg, buf, sizeof(clockMsg));
|
|
Serial.print(" h:m:s="); Serial.print(clockMsg.hour); Serial.print(":"); Serial.print(clockMsg.minute); Serial.print(":"); Serial.println(clockMsg.second);
|
|
updateRelays(clockMsg);
|
|
} else {
|
|
Serial.print("got request: "); Serial.print(len); Serial.println(" bytes");
|
|
Serial.print(buf[0], HEX); Serial.print(" ");
|
|
Serial.print(buf[1], HEX); Serial.print(" ");
|
|
Serial.print(buf[2], HEX); Serial.print(" ");
|
|
Serial.print(buf[3], HEX); Serial.print(" ");
|
|
Serial.print(buf[4], HEX); Serial.print(" ");
|
|
Serial.print(buf[5], HEX); Serial.print(" ");
|
|
Serial.print(buf[6], HEX); Serial.print(" ");
|
|
Serial.println(buf[7], HEX);
|
|
}
|
|
}
|
|
if (millis() - rxTimer > 1000) {
|
|
rxTimer = millis();
|
|
receivedCounter += counter;
|
|
unsigned long numBytes = counter*len;
|
|
Serial.print("Bytes: "); Serial.print(numBytes);
|
|
Serial.print(", Msg count: "); Serial.println(counter);
|
|
counter = 0;
|
|
}
|
|
}
|
|
#endif
|