//#DOC Main, program entry and main loop
#include "main.h"
#include <otRTC.h>
#include <otSerial.h>
#include <otTimer.h>
#include <otPrintf.h>
#include <otCamera.h>
#include <otImageSensor.h>
#include <otImageFile.h>
#include <otImage.h>
#include <otI2cBus.h>
#include <otFile.h>
#include <otString.h>

otCamera        camera;     // Library for the camera control
otImageSensor   sensor;     // Library for image sensor settings
otImageFile     imgFile;    // Library to save different image file format
otImage         image;      // Image store and processing library
otFILE          file;       // File I/O library

void setup()
{
    // Init connection with the console via UART0
    UartEnableSignals(UART0);
    UartSetBaud(UART0, 115200);
    // Init stout used by printf
    setStdout(4096, UART0);   // printf output is now directed on UART0
    // Init the timer for time measure
    TimerSetAsCounter(TIMER0);
    TimerEnable(TIMER0, true);
    // SMB bus init
    I2cBusReset();
    I2cBusFrequency(100, 30);
    RtcOn();
    // This test has been written in this date
    RtcSetDate(2020, 5, 11);
    RtcSetTime(0, 0, 0);
    // Wait the console has been started
    DelayMs(1000);      // Wait terminal start
    // Init the standard camera based on MT9V032
    camera.init(otCamera::CM_STANDARD);
    // Init image sensor
    if(!sensor.init(otImageSensor::IS_MT9V032, otImageSensor::MT9V03x_00))
    {
        printf("Can't find image sensor!\n");
        IDLE;
    }
    // Show sensor resolution
    printf("Sensor resolution: %dx%d\n", sensor.horPixel(), sensor.verPixel());
    if(!file.init())
    {
        printf("Can't init SD card!\n");
        IDLE;
    }
    // Set default procedure for library message
    file.setStdout(printf);
    // Set I/O access
    imgFile.init(&file);
}

void loop()
{
    sensor.setBinning(1, 1);
    // Create the image buffer to store 10 grabbed images with 8 bit depth
    // Warning! The depth of the 8bit image buffer has been set,
    // this is compatible with BMP, JPEG and AVI formats.
    // If we had set a 16bit format it was necessary to convert the image
    // to 8bit before saving it in the aforementioned formats.
    image.create(sensor.horPixel(), sensor.verPixel(), 10, otImage::IMG_U8);
    // Set Automatic Exposure Control
    sensor.setAec(true);
    // Grab n images specified by image class
    camera.grab(image, TIMER0);
    
    printf("Save RAW ... ");
    ElapsedTimeStart(TIMER0);
    if(!imgFile.save("image.raw", &image))
    {
        printf("Error saving: image.raw\n");
        IDLE;
    }
    printf("ET = %5u ms\n", ElapsedTimeEnd(TIMER0));
    
    // This format is very slow!
    printf("Save BMP ... ");
    ElapsedTimeStart(TIMER0);
    if(!imgFile.save("image.bmp", &image))
    {
        printf("Error saving: image.bmp\n");
        IDLE;
    }
    printf("ET = %5u ms\n", ElapsedTimeEnd(TIMER0));
    
    printf("Save JPG ... ");
    ElapsedTimeStart(TIMER0);
    if(!imgFile.save("image.jpg", &image))
    {
        printf("Error saving: image.jpg\n");
        IDLE;
    }
    printf("ET = %5u ms\n", ElapsedTimeEnd(TIMER0));
    
    printf("Save AVI ... ");
    ElapsedTimeStart(TIMER0);
    if(!imgFile.save("image.avi", &image))
    {
        printf("Error saving: image.avi\n");
        IDLE;
    }
    printf("ET = %5u ms\n", ElapsedTimeEnd(TIMER0));
    
    file.ls("/");
    file.end();
    // Free image buffer
    image.close();
    IDLE;
}

/* PROGRAM OUTPUT **************************************************************
********************************************************************************

Sensor resolution: 752x480
Save RAW ... ET =   393 ms
Save BMP ... ET = 32749 ms
Save JPG ... ET =  1862 ms
Save AVI ... ET =  1922 ms
Volume: [SD-DISK] SN: 50ab-004e
----A 2020/05/11 00:00   3609612  image.raw
----A 2020/05/11 00:00   1082934  image_0000.bmp
----A 2020/05/11 00:00   1082934  image_0001.bmp
----A 2020/05/11 00:00   1082934  image_0002.bmp
----A 2020/05/11 00:00   1082934  image_0003.bmp
----A 2020/05/11 00:00   1082934  image_0004.bmp
----A 2020/05/11 00:00   1082934  image_0005.bmp
----A 2020/05/11 00:00   1082934  image_0006.bmp
----A 2020/05/11 00:00   1082934  image_0007.bmp
----A 2020/05/11 00:00   1082934  image_0008.bmp
----A 2020/05/11 00:00   1082934  image_0009.bmp
----A 2020/05/11 00:00      6684  image_0000.jpg
----A 2020/05/11 00:00      6744  image_0001.jpg
----A 2020/05/11 00:00      6673  image_0002.jpg
----A 2020/05/11 00:00      6714  image_0003.jpg
----A 2020/05/11 00:00      6713  image_0004.jpg
----A 2020/05/11 00:00      6668  image_0005.jpg
----A 2020/05/11 00:00      6690  image_0006.jpg
----A 2020/05/11 00:00      6697  image_0007.jpg
----A 2020/05/11 00:00      6707  image_0008.jpg
----A 2020/05/11 00:00      6700  image_0009.jpg
----A 2020/05/11 00:00     71360  image.avi
  22 File(s),      14224 Kbytes
   0 Dir(s),    31241696 Kbytes free

*/