#include #include #ifdef FCRF_RadioHead #include #include #endif #ifdef FCRF_RF24 #include #endif #include "config.h" #include "clockMsg.h" #include "master.h" #include "client.h" #if WITH_DISPLAY #include #include #define OLED_RESET /*4*/ Adafruit_SSD1306 display(OLED_RESET); #endif #ifdef FCRF_RadioHead // Singleton instance of the radio driver RH_NRF24 nrf24(PIN_NRF24_CSN, PIN_NRF24_CE); // Address RH_BROADCAST_ADDRESS can be used for broadcasts as destination RHDatagram Datagram(nrf24, THIS_ADRESS); #endif #ifdef FCRF_RF24 RF24 radio(PIN_NRF24_CE, PIN_NRF24_CSN); // const uint64_t pipes[2] = { 0xABCDABCD71LL, 0x544d52687CLL }; const byte *addresses[] = {"1Mstr","2Mstr"}; byte data[32]; unsigned long sendFailedCounter=0, rxTimer; //Counter and timer for keeping track transfer info unsigned long receivedCounter=0; unsigned long startTime, stopTime, pauseTime; #endif int masterConfigPin = PIN_MASTER_CLIENT_SELECT; static boolean isMaster = true; void setup() { Serial.begin(115200); while (!Serial) ; // wait for serial port to connect. Needed for Leonardo only // what is our role? pinMode(masterConfigPin, INPUT); if (!digitalRead(masterConfigPin)) { isMaster = true; masterInit(); Serial.println("In Master-Mode"); } else { isMaster = false; clientInit(); Serial.println("In Client-Mode"); } #ifdef FCRF_RadioHead if (!Datagram.init()) Serial.println("Init datagram with nrf24 failed"); #endif #ifdef FCRF_RF24 radio.begin(); if (radio.isChipConnected()) { Serial.println("*** RF chip found"); } else { Serial.println("*** ERROR: RF chip not found!"); } radio.setChannel(1); radio.setPALevel(RF24_PA_MAX); radio.setDataRate(RF24_2MBPS); radio.setAutoAck(0); //radio.setRetries(2,15); // Optionally, increase the delay between retries & # of retries radio.setCRCLength(RF24_CRC_8); radio.openWritingPipe(addresses[0]); radio.openReadingPipe(1,addresses[1]); radio.startListening(); radio.printDetails(); // @TODO: real random seed! randomSeed(analogRead(0)); //randomSeed(22); radio.powerUp(); Serial.print("*** RF payload size="); Serial.print(radio.getPayloadSize()); Serial.println(" bytes"); if (radio.testCarrier() || radio.testRPD()) { Serial.println("*** Carrier/RPD seen on radio"); } if (radio.failureDetected) { Serial.println("*** Radio error detected!"); } #endif #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() { #ifdef FCRF_RadioHead if (isMaster) { masterLoop(Datagram); } else { #if WITH_DISPLAY clientLoop(Datagram, display); #else clientLoop(Datagram); #endif } #endif #ifdef FCRF_RF24 if (isMaster) { masterLoop(radio); } else { #if WITH_DISPLAY clientLoop(radio, display); #else clientLoop(radio); #endif } #endif }