6e2a086ba735d8e5334f8df4ddcb329cb1a632d5
[clinton/Smoothieware.git] / src / libs / StepTicker.h
1 /*
2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
3 Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
4 Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
6 */
7
8
9
10 #ifndef STEPTICKER_H
11 #define STEPTICKER_H
12
13 #include <stdint.h>
14 #include <vector>
15 #include <bitset>
16 #include <functional>
17
18 class StepperMotor;
19
20 class StepTicker{
21 public:
22 friend class StepperMotor;
23 static StepTicker* global_step_ticker;
24
25 StepTicker();
26 ~StepTicker();
27 void set_frequency( float frequency );
28 void signal_a_move_finished();
29 void set_reset_delay( float seconds );
30 int register_motor(StepperMotor* motor);
31 void add_motor_to_active_list(StepperMotor* motor);
32 void remove_motor_from_active_list(StepperMotor* motor);
33 void set_acceleration_ticks_per_second(uint32_t acceleration_ticks_per_second);
34
35 void reset_tick();
36 void TIMER0_IRQHandler (void);
37 void PendSV_IRQHandler (void);
38
39 void register_acceleration_tick_handler(std::function<void(void)> cb){
40 acceleration_tick_handlers.push_back(cb);
41 }
42
43 private:
44 float frequency;
45 uint32_t delay;
46 uint32_t period;
47 uint32_t last_duration;
48 uint32_t acceleration_tick_period;
49 uint32_t acceleration_tick_cnt;
50 std::vector<std::function<void(void)>> acceleration_tick_handlers;
51 std::vector<StepperMotor*> motor;
52 std::bitset<32> active_motor; // limit to 32 motors
53 struct {
54 uint8_t num_motors:5;
55 volatile bool a_move_finished:1;
56 volatile bool pending_sv:1;
57 volatile bool do_acceleration_tick:1;
58 bool reset_step_pins:1;
59 };
60
61 };
62
63
64
65 #endif