afsd

Fork of a4998 by Keegan Hu

a4988.cpp

Committer:
Bilybill
Date:
2018-05-11
Revision:
1:88f743f3e7a7
Parent:
0:173936452e07

File content as of revision 1:88f743f3e7a7:

#include "a4988.h"
#include "mbed.h"
Stepper::Stepper(PinName _en, PinName _stepPin, PinName _direction):en(_en),
    stepPin(_stepPin),
    direction(_direction)
{
    
}
void Stepper::step(int dir, long long int frequency ,volatile long long int _remain)
{
    //fun = &Stepper::step_control;
    if (dir == 1) {
        direction = 0;
    } else if (dir == 0) {
        direction = 1;
    }
    remain = _remain;
    step_ticker.attach(this, &Stepper::step_control,0.5/frequency);
}
 
void Stepper::enable()
{
    en = 0;
}
 
void Stepper::disable()
{
    en = 1;
}

void Stepper::step_control()
{
        if(remain == 0)
        {         
            return;
        }
        if(remain < 0 )
        {
            if(stepPin){
                stepPin = 0; //STEP 1->0
            }else{
                stepPin = 1; //STEP 0->1
            }
        }
            
        if(stepPin){
            stepPin = 0; //STEP 1->0
            remain--;
        }else{
            stepPin = 1; //STEP 0->1
        }
}