Threaded version
Diff: stepper.cpp
- Revision:
- 3:e98454afa038
- Parent:
- 2:ad11f550b379
--- a/stepper.cpp Wed Nov 03 18:41:28 2010 +0000 +++ b/stepper.cpp Wed Aug 28 19:02:39 2019 +0000 @@ -23,39 +23,55 @@ #include "mbed.h" // define the Stepper Motor save start/stop speed -#define START_STOP_SPEED 300 +#define START_STOP_SPEED 500 // define Library version number #define VERSION 0.3 // led4, used for testing the direction signal -DigitalOut led4(LED4); -stepper::stepper(PinName clk, PinName dir) : _clk(clk), _dir(dir) { +stepper::stepper(PinName clk, PinName dir) : _clk(clk), _dir(dir), s_thread(osPriorityNormal) { _clk = 0, _dir = 0; + s_thread.start(this, &stepper::run); } void stepper::step(int n_steps, bool direction, int speed, bool accel) { - int accelspeed; - if(accel) accelspeed = START_STOP_SPEED; - else accelspeed = speed; - for(int i=0; i<n_steps; i++) - { - if(accel)//linear acceleration - { - if(i < START_STOP_SPEED) if (--accelspeed == speed) accelspeed ++; - if(i > (n_steps-START_STOP_SPEED)) if(++accelspeed == START_STOP_SPEED) accelspeed--; - } - _dir = led4 = direction; - _clk = 1; - wait_us(1); - _clk = 0; - wait_us(1); - wait_us(accelspeed); + if(accel) s_accelspeed = START_STOP_SPEED; + else s_accelspeed = speed; + s_speed = speed; + s_direction = direction; + s_accel = accel; + s_n_steps = n_steps; + +} +void stepper::run() { + printf("stepper thread running"); + while (true) { + if (s_n_steps != 0){ + for(int i=0; i<s_n_steps; i++) + { + if(s_accel)//linear acceleration + { + if(i < START_STOP_SPEED) if (--s_accelspeed == s_speed) s_accelspeed ++; + if(i > (s_n_steps-START_STOP_SPEED)) if(++s_accelspeed == START_STOP_SPEED) s_accelspeed--; + } + _dir = s_direction; + _clk = 1; + wait_us(1); + _clk = 0; + wait_us(1); + wait_us(s_accelspeed); + } + s_n_steps = 0; + } + + + ThisThread::sleep_for(10); } + } // version() returns the version number of the library float stepper::version(void) { return VERSION; -} \ No newline at end of file +}