#include "IOBoard.h"
#include "ArduinoConfig.h"

#include <Arduino.h>
#include <arduino-timer.h>
#include <avr/io.h>

//SPI is formally a library, so it is not part of the objects compiled
//from the core and we need to include it explicitly
#include <SPI.h>

// Crete default timer which is able to manage 10 concurrent tasks
// and uses milliseconds as a timebase.
auto timer = timer_create_default();
bool periodicHandler1(void* args);

void setup() {
    // Set data direction of selected port to output.
    CS_DDR = 0xff;
    // Drive all the slave selects high as required for the SPI protocol
    // if no transfer is going on.
    CS_PORT = 0xff;
    Serial.begin(BAUD_RATE);
#if PROGRAMMING_OUTPUT == 1
    Serial.println("-AI- Setting up Arduino IO interface board.");
    Serial.print("-AI- Configured baud rate for serial communication: ");
    Serial.println(BAUD_RATE, DEC);
    Serial.print("-AI- Size of serial receiver buffer: ");
    Serial.print(SERIAL_RX_BUFFER_SIZE, DEC);
    Serial.println(" bytes");
#endif
    SPI.begin();
    // Call periodic handler with certain interval
    timer.every(RING_BUFFER_CHECK_INTVL, periodicHandler1);
}

bool periodicHandler1(void* args) {
    if(args) {};
    IOBoard::handleNewData();
    // repeat action
    return true;
}

void loop() {
    timer.tick();
}