FremoClockRF/src/main.cpp

82 lines
1.8 KiB
C++
Raw Normal View History

2018-10-18 19:05:40 +00:00
#include <Arduino.h>
#include <SPI.h>
#include <RH_NRF24.h>
#include <RHDatagram.h>
#include "config.h"
2018-10-18 19:05:40 +00:00
#include "clockMsg.h"
#include "master.h"
2018-10-19 22:20:57 +00:00
#include "client.h"
2018-10-18 19:05:40 +00:00
#if WITH_DISPLAY
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET /*4*/
Adafruit_SSD1306 display(OLED_RESET);
#endif
// Singleton instance of the radio driver
RH_NRF24 nrf24(PIN_NRF24_CSN, PIN_NRF24_CE);
2018-10-18 19:05:40 +00:00
// Address RH_BROADCAST_ADDRESS can be used for broadcasts as destination
RHDatagram Datagram(nrf24, THIS_ADRESS);
int masterConfigPin = PIN_MASTER_CLIENT_SELECT;
2018-10-18 19:05:40 +00:00
static boolean isMaster = true;
void setup() {
Serial.begin(9600);
while (!Serial)
; // wait for serial port to connect. Needed for Leonardo only
// what is our role?
pinMode(masterConfigPin, INPUT);
2018-10-18 19:05:40 +00:00
if (!digitalRead(masterConfigPin)) {
isMaster = true;
masterInit();
2018-10-18 19:05:40 +00:00
Serial.println("In Master-Mode");
} else {
isMaster = false;
2018-10-19 22:20:57 +00:00
clientInit();
2018-10-18 19:05:40 +00:00
Serial.println("In Client-Mode");
}
if (!Datagram.init())
Serial.println("Init datagram with nrf24 failed");
#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!");
#endif
}
void loop() {
2018-10-19 22:20:57 +00:00
if (isMaster) {
masterLoop(Datagram);
} else {
#if WITH_DISPLAY
clientLoop(Datagram, display);
#else
clientLoop(Datagram);
#endif
}
2018-10-18 19:05:40 +00:00
}