//#DOC Main, program entry and main loop
#include "main.h"
#include <otI2cBus.h>
#include <otTimer.h>
#include <otSerial.h>
#include <otMalloc.h>
#include <otPrintf.h>
#include <otDigitalLens.h>

otDigitalLens   mzl;    // Motorized zoom lens

void setup()
{
    // Enable and init UART 0 for console I/O
    UartEnableSignals(UART0);
    UartSetBaud(UART0, 115200);
    // SMB bus init
    I2cBusReset();
    I2cBusFrequency(100, 30);
    // Init printf
    _initMalloc();
    setStdout(4096, UART0); // printf output is now directed on UART0
    // Init digital lens
    mzl.init();
}

void loop()
{
    if(UartCheckChar(UART0))
    {
        char ch = UartChar(UART0);
        switch(ch)
        {
            case '+':   // Increase focal length
                mzl.zoom(10, 2000);
                break;
            case '-':   // Decrease focal length
                mzl.zoom(-10, 2000);
                break;  
            case '<':   // Change the focus
                mzl.focus(10, 2000);
                break;
            case '>':   // Change the focus
                mzl.focus(-10, 2000);
                break;
            case 'I':   // Insert IR cut
                mzl.irCut(true);
                DelayMs(100);
                mzl.shutdown();
                break;
            case 'V':   // Remove IR cut
                mzl.irCut(false);
                DelayMs(100);
                mzl.shutdown();
                break;
            case 'C':   // Clear position counters
                mzl.clear(true);
                mzl.clear(false);
                break;
            case '?':   // Display counters value
                printf("Z: %d, F: %d\n", mzl.zoom(), mzl.focus());
                break;
            default:
                printf("*** Invalid command ***\n");
                break;                    
        }      
    }
}