#include <Arduino.h>

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

// TODO: Copy this header into arduino_core, so it can be used without
// Eclipse Sloeber as well.
#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>

auto timer = timer_create_default();

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();
}

bool periodicHandler1(void* args) {
    if(args) {};
    IOBoard::handleNewData();
    return false;
}

void loop() {
    timer.tick();
    timer.every(RING_BUFFER_CHECK_INTVL, periodicHandler1);
    //delay(1000);
}