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

otRotaryEncoder enc;

void setup()
{
    UartEnableSignals(UART0);
    UartSetBaud(UART0, 115200);
    setStdout(UART0); // printf output is now directed on UART0
    // Init FPGA
    FpgaInit();
    FpgaPortPxSet(PD_INPUT, PD_INPUT, PD_INPUT, PD_INPUT, PD_INPUT, PD_INPUT, PD_INPUT, PD_INPUT);
    // Set PX0-1 for encoder phases and PX2 for the button
    enc.init(0, 2);
}

void loop()
{
    // Read encoder action
    S32 c = enc.read();
    // Dsiplay encoder counts
    printf("%d\n", enc.counts());
    // Show occurred actions
    if(c == 1) printf("CW  action\n");
    else
        if(c == -1) printf("CCW action\n");
    
    if(enc.button()) printf("Button pressed\n");
    // Wait something ...
    DelayMs(500);
}