#ifndef SERVO_H
#define SERVO_H

#include <Arduino.h>
#include "./common/Pin.h"
#define MOTORSTEPS 2000;
class Servo
{
public:
    volatile long ticksRemaining; // remaining ticks, 2 ticks = 1 pulse = 1 microstep/step

protected:
    /* for some stupid reason the Pin class requires initialization */
    Pin dirPin = Pin(1000);
    Pin stepPin = dirPin;
    bool paused;

public:
    Servo(uint8_t dirpin, uint8_t steppin);
    void init();
    void setPulse(long pulse);
    bool step(long steps, uint8_t direction);
    bool step(long steps, uint8_t direction, long pulse);
    long getRemainingSteps();
    long stop();
    void pause();
    void resume();
    bool isStepping();
    bool isStopped();
    bool isPaused();
    static void ticking();
    static long rpmToTickInterval(long targetRPM)
    {
        // rotation per sec = targetRPM/60
        float stepsPerSecond = (float)targetRPM / 60 * MOTORSTEPS;
        long pulseInMicroseconds = (long)(1000000L / stepsPerSecond) / 2;
        return pulseInMicroseconds;
    }

private:
    static Servo *firstInstance;
};
#endif
