#include "Define.h"
//
#include "StepperDRV8825.h"
//
CStepperDRV8825 StepperDRV8825(PIN_MOTOR_STEP, 
                               PIN_MOTOR_DIRECTION, 
                               PIN_MOTOR_PRECISIONM0, 
                               PIN_MOTOR_PRECISIONM1, 
                               PIN_MOTOR_PRECISIONM2,
                               PIN_MOTOR_ENABLEN);
//
enum EMainAction
{
  maIdle = 0,
  maFullPositive = 1,
  maFullNegative = 2,
  maHalfPositive = 3,
  maHalfNegative = 4,
  maQuarterPositive = 5,
  maQuarterNegative = 6,
  maEightPositive = 7,
  maEightNegative = 8,
  maSixteenPositive = 9,
  maSixteenNegative = 10,
  maThirtyTwoPositive = 11,
  maThirtyTwoNegative = 12
};
//
EMainAction MainAction = maFullPositive;
long unsigned MillisPreset;
//
//---------------------------------------------------
//  Setup
//---------------------------------------------------
void setup() 
{
  SerialPC.begin(115200);
  delay(333);
  SerialPC.println("");
  SerialPC.println("***************************");
  SerialPC.println("**** Esp32StepperMotor ****");
  SerialPC.println("* Version....: 01V11      *");
  SerialPC.println("* DateTime...: 2207191213 *");
  SerialPC.println("* Author.....: OMdevelop  *");
  SerialPC.println("***************************");
  //
  StepperDRV8825.Open();
  SerialPC.println("Initialisation done");
  MainAction = maIdle;
  SerialPC.println("MotorState: --- Idle ---");
}
//
//---------------------------------------------------
//  MainLoop
//---------------------------------------------------
void loop()
{  
  StepperDRV8825.Execute();
  //
  if (ssIdle == StepperDRV8825.GetStateStep())
  {
    switch (MainAction)
    {
      case maIdle:
        if (MILLIS_IDLE <= (millis() - MillisPreset))
        {
          StepperDRV8825.Disable();
          MainAction = maFullPositive;
          SerialPC.println("MotorState: FullPositive");
          StepperDRV8825.Enable();
          StepperDRV8825.Move(sdPositive, srFull, STEPPERIOD_US, STEPCOUNT);
        }
        break;
      case maFullPositive:
        StepperDRV8825.Disable();
        MainAction = maFullNegative;
        SerialPC.println("MotorState: FullNegative");
        StepperDRV8825.Enable();
        StepperDRV8825.Move(sdNegative, srFull, STEPPERIOD_US, STEPCOUNT);
        break;
      case maFullNegative:
        StepperDRV8825.Disable();
        MainAction = maHalfPositive;
        SerialPC.println("MotorState: HalfPositive");
        StepperDRV8825.Enable();
        StepperDRV8825.Move(sdPositive, srHalf, STEPPERIOD_US, STEPCOUNT);
        break;
      case maHalfPositive:
        StepperDRV8825.Disable();
        MainAction = maHalfNegative;
        SerialPC.println("MotorState: HalfNegative");
        StepperDRV8825.Enable();
        StepperDRV8825.Move(sdNegative, srHalf, STEPPERIOD_US, STEPCOUNT);
        break;
      case maHalfNegative:
        StepperDRV8825.Disable();
        MainAction = maQuarterPositive;
        SerialPC.println("MotorState: QuarterPositive");
        StepperDRV8825.Enable();
        StepperDRV8825.Move(sdPositive, srQuarter, STEPPERIOD_US, STEPCOUNT);
        break;
      case maQuarterPositive:
        StepperDRV8825.Disable();
        MainAction = maQuarterNegative;
        SerialPC.println("MotorState: QuarterNegative");
        StepperDRV8825.Enable();
        StepperDRV8825.Move(sdNegative, srQuarter, STEPPERIOD_US, STEPCOUNT);
        break;
      case maQuarterNegative:
        StepperDRV8825.Disable();
        MainAction = maEightPositive;
        SerialPC.println("MotorState: EightPositive");
        StepperDRV8825.Enable();
        StepperDRV8825.Move(sdPositive, srEight, STEPPERIOD_US, STEPCOUNT);
        break;
      case maEightPositive:
        StepperDRV8825.Disable();
        MainAction = maEightNegative;
        SerialPC.println("MotorState: EightNegative");
        StepperDRV8825.Enable();
        StepperDRV8825.Move(sdNegative, srEight, STEPPERIOD_US, STEPCOUNT);
        break;
      case maEightNegative:
        StepperDRV8825.Disable();
        MainAction = maSixteenPositive;
        SerialPC.println("MotorState: SixteenPositive");
        StepperDRV8825.Enable();
        StepperDRV8825.Move(sdPositive, srSixteen, STEPPERIOD_US, STEPCOUNT);
        break;
      case maSixteenPositive:
        StepperDRV8825.Disable();
        MainAction = maSixteenNegative;
        SerialPC.println("MotorState: SixteenNegative");
        StepperDRV8825.Enable();
        StepperDRV8825.Move(sdNegative, srSixteen, STEPPERIOD_US, STEPCOUNT);
        break;
      case maSixteenNegative:
        StepperDRV8825.Disable();
        MainAction = maThirtyTwoPositive;
        SerialPC.println("MotorState: ThirtyTwoPositive");
        StepperDRV8825.Enable();
        StepperDRV8825.Move(sdPositive, srThirtyTwo, STEPPERIOD_US, STEPCOUNT);
        break;
      case maThirtyTwoPositive:
        StepperDRV8825.Disable();
        MainAction = maThirtyTwoNegative;
        SerialPC.println("MotorState: ThirtyTwoNegative");
        StepperDRV8825.Enable();
        StepperDRV8825.Move(sdNegative, srThirtyTwo, STEPPERIOD_US, STEPCOUNT);
        break;
      case maThirtyTwoNegative:
        StepperDRV8825.Disable();
        MainAction = maIdle;
        SerialPC.println("MotorState: --- Idle ---");
        MillisPreset = millis();
        break;
    }
  }
}