
//      ******************************************************************
//      *                                                                *
//      *                Bipolar Stepper Motor Library                   *
//      *       for a bipolar motor connected to an L298N H-bridge       *
//      *                                                                *
//		*			 This Library is based on the unipolar               *
//      *                TinyStepper_28BYJ_48 library                    *
//      *                    by Stan Reifel 11/8/2017                    *
//      *               Copyright (c) S. Reifel & Co, 2017               *
//      *																 *
//      * 			  For the original code and licence see              *
//      *      https://github.com/Stan-Reifel/TinyStepper_28BYJ_48       *
//      *                                                                *
//      *             The following changes have been made               *
//      *              - the library name (to avoid confusion)		     *
//      *              - full step bipolar stepper pattern added         *
//      *              - half step bipolar stepper pattern added         *
//      *              - wave step bipolar stepper pattern added         *
//      *              - 28BYJ-48 unipolar stepper pattern removed       *
//      *                                                                *
//      *             Changes made by lingib 29 January 2022             *
//      *   https://www.instructables.com/member/lingib/instructables/   *
//      *                                                                *
//      ******************************************************************
//

#ifndef TinyStepper_h
#define TinyStepper_h

#include <arduino.h>
#include <stdlib.h>


//
// the TinyStepper class
//
class TinyStepper
{
  public:
    //
    // public functions
    //
    TinyStepper();
    void connectToPins(byte in1PinNumber, byte in2PinNumber, byte in3PinNumber, byte in4PinNumber);
    void setCurrentPositionInSteps(long currentPositionInSteps);
    long getCurrentPositionInSteps();
    void setupStop();
    void setSpeedInStepsPerSecond(float speedInStepsPerSecond);
    void setAccelerationInStepsPerSecondPerSecond(float accelerationInStepsPerSecondPerSecond);
    void moveRelativeInSteps(long distanceToMoveInSteps);
    void setupRelativeMoveInSteps(long distanceToMoveInSteps);
    void moveToPositionInSteps(long absolutePositionToMoveToInSteps);
    void setupMoveInSteps(long absolutePositionToMoveToInSteps);
    bool motionComplete();
    float getCurrentVelocityInStepsPerSecond(); 
    bool processMovement(void);
    void disableMotor();


  private:
    //
    // private functions
    //
    void setNextHalfStep(int direction);
    void setNextFullStep(int direction);
    void setNextWaveStep(int direction);
    
    //
    // private member variables
    //
    byte in1Pin = 0;
    byte in2Pin = 0;
    byte in3Pin = 0;
    byte in4Pin = 0;
    float desiredSpeed_InStepsPerSecond;
    float acceleration_InStepsPerSecondPerSecond;
    long targetPosition_InSteps;
    bool startNewMove;
    float desiredStepPeriod_InUS;
    long decelerationDistance_InSteps;
    int direction_Scaler;
    float ramp_InitialStepPeriod_InUS;
    float ramp_NextStepPeriod_InUS;
    unsigned long ramp_LastStepTime_InUS;
    float acceleration_InStepsPerUSPerUS;
    float currentStepPeriod_InUS;
    long currentPosition_InSteps;
    int stepPhase;
};

// ------------------------------------ End ---------------------------------
#endif

