Added configuration to config.h, splitted master code from main.
This commit is contained in:
parent
3b107cd653
commit
d67b0f672a
14
README.md
14
README.md
|
@ -1,3 +1,15 @@
|
||||||
# FREMO Fast Clock
|
# 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.
|
||||||
|
|
|
@ -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
|
87
src/main.cpp
87
src/main.cpp
|
@ -2,7 +2,9 @@
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include <RH_NRF24.h>
|
#include <RH_NRF24.h>
|
||||||
#include <RHDatagram.h>
|
#include <RHDatagram.h>
|
||||||
|
#include "config.h"
|
||||||
#include "clockMsg.h"
|
#include "clockMsg.h"
|
||||||
|
#include "master.h"
|
||||||
|
|
||||||
#if WITH_DISPLAY
|
#if WITH_DISPLAY
|
||||||
#include <Adafruit_GFX.h>
|
#include <Adafruit_GFX.h>
|
||||||
|
@ -13,28 +15,23 @@ Adafruit_SSD1306 display(OLED_RESET);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Singleton instance of the radio driver
|
// Singleton instance of the radio driver
|
||||||
RH_NRF24 nrf24(8, 10); // (CSN, CE)
|
RH_NRF24 nrf24(PIN_NRF24_CSN, PIN_NRF24_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
|
|
||||||
|
|
||||||
#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
|
// Address RH_BROADCAST_ADDRESS can be used for broadcasts as destination
|
||||||
RHDatagram Datagram(nrf24, THIS_ADRESS);
|
RHDatagram Datagram(nrf24, THIS_ADRESS);
|
||||||
|
|
||||||
struct clockMsg_s clockMsg;
|
struct clockMsg_s clockMsg;
|
||||||
|
|
||||||
int masterConfigPin = 2;
|
int masterConfigPin = PIN_MASTER_CLIENT_SELECT;
|
||||||
static boolean isMaster = true;
|
static boolean isMaster = true;
|
||||||
|
|
||||||
// relays for client's physical clock
|
// relays for client's physical clock
|
||||||
int relay1pin = 5;
|
int relay1pin = PIN_RELAY1;
|
||||||
int relay2pin = 6;
|
int relay2pin = PIN_RELAY2;
|
||||||
// configs:
|
|
||||||
int holdRelay_ms = 150;
|
int holdRelay_ms = DEFAULT_HOLD_RELAY_MS;
|
||||||
int minRelayOffTime_ms = 80;
|
int minRelayOffTime_ms = DEFAULT_MIN_RELAY_OFF_TIME_MS;
|
||||||
boolean relayActiveLow = true;
|
boolean relayActiveLow = DEFAULT_RELAY_ACTIVE_LOW;
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
uint8_t hour;
|
uint8_t hour;
|
||||||
|
@ -98,6 +95,7 @@ void setup() {
|
||||||
|
|
||||||
// what is our role?
|
// what is our role?
|
||||||
pinMode(masterConfigPin, INPUT);
|
pinMode(masterConfigPin, INPUT);
|
||||||
|
|
||||||
pinMode(relay1pin, OUTPUT);
|
pinMode(relay1pin, OUTPUT);
|
||||||
pinMode(relay2pin, OUTPUT);
|
pinMode(relay2pin, OUTPUT);
|
||||||
displayedTime.hour = 0;
|
displayedTime.hour = 0;
|
||||||
|
@ -105,6 +103,7 @@ void setup() {
|
||||||
displayedTime.second = 0;
|
displayedTime.second = 0;
|
||||||
if (!digitalRead(masterConfigPin)) {
|
if (!digitalRead(masterConfigPin)) {
|
||||||
isMaster = true;
|
isMaster = true;
|
||||||
|
masterInit();
|
||||||
Serial.println("In Master-Mode");
|
Serial.println("In Master-Mode");
|
||||||
} else {
|
} else {
|
||||||
isMaster = false;
|
isMaster = false;
|
||||||
|
@ -113,12 +112,6 @@ void setup() {
|
||||||
|
|
||||||
if (!Datagram.init())
|
if (!Datagram.init())
|
||||||
Serial.println("Init datagram with nrf24 failed");
|
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
|
#if WITH_DISPLAY
|
||||||
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
|
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
|
||||||
|
@ -143,60 +136,6 @@ void setup() {
|
||||||
#endif
|
#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()
|
void clientLoop()
|
||||||
{
|
{
|
||||||
|
@ -226,6 +165,6 @@ void clientLoop()
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
if (isMaster) { masterLoop(); }
|
if (isMaster) { masterLoop(Datagram); }
|
||||||
else { clientLoop(); }
|
else { clientLoop(); }
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,66 @@
|
||||||
|
#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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
#ifndef MASTER_H_INCLUDED
|
||||||
|
#define MASTER_H_INCLUDED
|
||||||
|
#include <RHDatagram.h>
|
||||||
|
|
||||||
|
extern void masterInit();
|
||||||
|
extern void masterLoop(RHDatagram Datagram);
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue