From d67b0f672a635a5fb6dc563197e79385dc6745e6 Mon Sep 17 00:00:00 2001 From: Dirk Jahnke Date: Fri, 19 Oct 2018 23:48:54 +0200 Subject: [PATCH] Added configuration to config.h, splitted master code from main. --- README.md | 14 +++++++- src/config.h | 22 +++++++++++++ src/main.cpp | 87 ++++++++------------------------------------------ src/master.cpp | 66 ++++++++++++++++++++++++++++++++++++++ src/master.h | 8 +++++ 5 files changed, 122 insertions(+), 75 deletions(-) create mode 100644 src/config.h create mode 100644 src/master.cpp create mode 100644 src/master.h diff --git a/README.md b/README.md index 1e0c931..3f348b3 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,15 @@ # FREMO Fast Clock -This is a fast clock implementation based on a 2.4GHz wireless transmission layer based on NRF24 controllers. +This is a fast clock implementation based on a 2.4GHz wireless transmission layer based on NRF24 controllers. The software runs in two modes: Master or Client. A GPIO pin decides which role the hardware should play. + +The definitions in config.h define the ports to be used: + - PIN_MASTER_CLIENT_SELECT defines the pin to be used to identifiy master or client. If that pin is LOW, then master is identified. + - PIN_NRF24_CE defines the pin that is used for the CE signal + - PIN_NRF24_CSN defines the pin that is used for the CSN signal + - PIN_RELAY1 defines the pin that controls relay 1 + - PIN_RELAY2 defines the pin that controls relay 2 + - DEFAULT_HOLD_RELAY_MS 150 + - DEFAULT_MIN_RELAY_OFF_TIME_MS 80 + - DEFAULT_RELAY_ACTIVE_LOW true + +Depending of the controller used, an SD1306 controlled display can be used. See the "-D WITH_DISPLAY" flag in the build_flags definition of the appropriate environment. diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..a7ad86b --- /dev/null +++ b/src/config.h @@ -0,0 +1,22 @@ +#ifndef config_h_included +#define config_h_included + +#define PIN_MASTER_CLIENT_SELECT 2 +#define ROLE_MASTER LOW +#define ROLE_CLIENT HIGH +#define PIN_NRF24_CE 10 +#define PIN_NRF24_CSN 8 + +#define PIN_RELAY1 5 +#define PIN_RELAY2 6 + +// communication protocol definitions +#define nRF_Channel 1 +#define THIS_ADRESS 0 // uint8_t address of this node + +// relay based clock control behaviour +#define DEFAULT_HOLD_RELAY_MS 150 +#define DEFAULT_MIN_RELAY_OFF_TIME_MS 80 +#define DEFAULT_RELAY_ACTIVE_LOW true + +#endif diff --git a/src/main.cpp b/src/main.cpp index e4f95d6..0110257 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,7 +2,9 @@ #include #include #include +#include "config.h" #include "clockMsg.h" +#include "master.h" #if WITH_DISPLAY #include @@ -13,28 +15,23 @@ Adafruit_SSD1306 display(OLED_RESET); #endif // Singleton instance of the radio driver -RH_NRF24 nrf24(8, 10); // (CSN, CE) -// RH_NRF24 nrf24(8, 7); // use this to be electrically compatible with Mirf -// RH_NRF24 nrf24(8, 10);// For Leonardo, need explicit SS pin -// RH_NRF24 nrf24(8, 7); // For RFM73 on Anarduino Mini +RH_NRF24 nrf24(PIN_NRF24_CSN, PIN_NRF24_CE); -#define nRF_Channel 1 -#define THIS_ADRESS 0 // uint8_t address of this node // Address RH_BROADCAST_ADDRESS can be used for broadcasts as destination RHDatagram Datagram(nrf24, THIS_ADRESS); struct clockMsg_s clockMsg; -int masterConfigPin = 2; +int masterConfigPin = PIN_MASTER_CLIENT_SELECT; static boolean isMaster = true; // relays for client's physical clock -int relay1pin = 5; -int relay2pin = 6; -// configs: -int holdRelay_ms = 150; -int minRelayOffTime_ms = 80; -boolean relayActiveLow = true; +int relay1pin = PIN_RELAY1; +int relay2pin = PIN_RELAY2; + +int holdRelay_ms = DEFAULT_HOLD_RELAY_MS; +int minRelayOffTime_ms = DEFAULT_MIN_RELAY_OFF_TIME_MS; +boolean relayActiveLow = DEFAULT_RELAY_ACTIVE_LOW; struct { uint8_t hour; @@ -98,6 +95,7 @@ void setup() { // what is our role? pinMode(masterConfigPin, INPUT); + pinMode(relay1pin, OUTPUT); pinMode(relay2pin, OUTPUT); displayedTime.hour = 0; @@ -105,6 +103,7 @@ void setup() { displayedTime.second = 0; if (!digitalRead(masterConfigPin)) { isMaster = true; + masterInit(); Serial.println("In Master-Mode"); } else { isMaster = false; @@ -113,12 +112,6 @@ void setup() { if (!Datagram.init()) Serial.println("Init datagram with nrf24 failed"); - // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm - /*if (!nrf24.setChannel(nRF_Channel)) - Serial.println("setChannel failed"); - if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm)) - Serial.println("setRF failed"); - */ #if WITH_DISPLAY display.begin(SSD1306_SWITCHCAPVCC, 0x3C); @@ -143,60 +136,6 @@ void setup() { #endif } -void masterLoop() -{ - 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_NRF24_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"); - } - } - } -} - void clientLoop() { @@ -226,6 +165,6 @@ void clientLoop() } void loop() { - if (isMaster) { masterLoop(); } + if (isMaster) { masterLoop(Datagram); } else { clientLoop(); } } diff --git a/src/master.cpp b/src/master.cpp new file mode 100644 index 0000000..fd88e02 --- /dev/null +++ b/src/master.cpp @@ -0,0 +1,66 @@ +#include +#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"); + } + } + } +} diff --git a/src/master.h b/src/master.h new file mode 100644 index 0000000..8acf0c7 --- /dev/null +++ b/src/master.h @@ -0,0 +1,8 @@ +#ifndef MASTER_H_INCLUDED +#define MASTER_H_INCLUDED +#include + +extern void masterInit(); +extern void masterLoop(RHDatagram Datagram); + +#endif