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

otL6470 drv;

otMicrostepShield   mot;

void dumpSettings(otL6470 * drv)
{
    printf("Step mode      : %d\n", drv->getStepMode());
    printf("Max speed      : %f\n", drv->getMaxSpeed());
    printf("Min speed      : %f\n", drv->getMinSpeed());
    printf("Full speed     : %f\n", drv->getFullSpeed());
    printf("Acceleration   : %f\n", drv->getAcc());
    printf("Deceleration   : %f\n", drv->getDec());
    printf("O.C. Threshold : %d\n", drv->getOCThreshold());
    printf("O.C. Shutdown  : %d\n", drv->getOCShutdown());
    printf("Slew rate      : %d\n", drv->getSlewRate());
    printf("V. Compensate  : %d\n", drv->getVoltageComp());
    printf("Switch mode    : %d\n", drv->getSwitchMode());
    printf("Oscillator mode: %d\n", drv->getOscMode());
    printf("Acc. KVAL      : %d\n", drv->getAccKVAL());
    printf("Dec. KVAL      : %d\n", drv->getDecKVAL());
    printf("Run. KVAL      : %d\n", drv->getRunKVAL());
    printf("Hold KVAL      : %d\n", drv->getHoldKVAL());
    printf("Low Speed Opt. : %d\n", drv->getLoSpdOpt());
}

void setup()
{
    UartEnableSignals(UART0);
    UartSetBaud(UART0, 115200);
    
    setStdout(4096, UART0); // printf output is now directed on UART0
   
    FpgaInit();
    FpgaHiddenPort(HP_EXT_I2C, true);
    
    I2cBusReset();
    I2cBusFrequency(400, 30);
    
    DelayMs(1000);
    
    mot.init(&drv);
    
    for(U8 i = 0; i < 2; i++)
    {
        mot.driverSelect(i);
        dumpSettings(&drv);
        drv.setOCThreshold(DSPIN_OCD_TH_6000mA);
        drv.configStepMode(DSPIN_STEP_FS_128);
        drv.setFullSpeed(2000);
        drv.setMaxSpeed(200);
        drv.setAcc(100);
        drv.setDec(100);
    }
    drv.run(1, 100);
    DelayMs(2000);
    drv.hardStop();
}

void loop()
{
    int     i;
    
    mot.motorOff(); // Can write new motor settings when the motor is off
    
    printf("Full step  : +/-    800 steps @ 200Hz\n");
    for(i = 0; i < 2; i++)
    {
        mot.driverSelect(i);
        drv.getStatus(); // Free all errors
        drv.configStepMode(DSPIN_STEP_FS);
        drv.setFullSpeed(2000);
        drv.setMaxSpeed(200);
        drv.setAcc(100);
        drv.setDec(100);
    }
    
    for(i = 0; i < 1; i++)
    {
        mot.move(800, 800);
        mot.readyMove();

        mot.move(-800, -800);
        mot.readyMove();
    }
    
    mot.motorOff(); // Can write new motor settings when the motor is off
    
    printf("1/4 step   : +/-   2000 steps @ 200Hz\n");
    DelayMs(1000);
    
    for(i = 0; i < 2; i++)
    {
        mot.driverSelect(i);
        drv.getStatus(); // Free all errors
        drv.configStepMode(DSPIN_STEP_FS_4);
        drv.setFullSpeed(4000);
        drv.setMaxSpeed(200);
        drv.setAcc(100);
        drv.setDec(100);
    }
    
    for(i = 0; i < 1; i++)
    {
        mot.move(2000, 2000);
        mot.readyMove();

        mot.move(-2000, -2000);
        mot.readyMove();
    }
    
    mot.motorOff(); // Can write new motor settings when the motor is off
    
    printf("1/128 step : +/- 200000 steps @ 350Hz\n");
    DelayMs(1000);
    
    for(i = 0; i < 2; i++)
    {
        mot.driverSelect(i);
        drv.getStatus(); // Free all errors
        drv.configStepMode(DSPIN_STEP_FS_128);
        drv.setFullSpeed(10000);
        drv.setMaxSpeed(350);
        drv.setAcc(100);
        drv.setDec(100);
    }
    
    for(i = 0; i < 1; i++)
    {
        mot.move(200000, 200000);
        mot.readyMove();

        mot.move(-200000, -200000);
        mot.readyMove();
    }
}