//#DOC Main, program entry and main loop
#include "main.h"
#include <otSonar.h>
#include <otSerial.h>
#include <otTimer.h>
#include <otPrintfLite.h>

// Important! Remember that the SONAR module cannot be connected directly to the electronics but requires a level shifter.
// Its outputs are 5V while the electronics works at 3.3V
otSonar sonar;

void setup()
{
    // Setup the UART0 used by printf
    UartEnableSignals(UART0);
    UartSetBaud(UART0, 115200);
    // Init printf
    setStdout(UART0); // printf output is now directed on UART0
    // Set TIMER0 for elapsed time evaluation
    TimerSetAsCounter(TIMER0);
    TimerEnable(TIMER0, true);
    // Use PF15 for TRIGGER, PF13 for SONAR 0 and PF14 for SONAR 1
    // You can use up to 8 SONAR module
    // The reading is not simultaneous but in sequence,
    // the maximum time required by each sensor is maximum 25ms
    sonar.init(TIMER0, 15, 2, 13, 14);
}

void loop()
{
    sonar.process();
    // ...
    // This delay is necessary to reduce the number of reads, is too fast!
    DelayMs(500);
    printf("S0 = %d mm, S1 = %d mm\n", sonar.measure(0), sonar.measure(1));
}