-#ifndef STEPTICKER_H
-#define STEPTICKER_H
+#pragma once
#include <stdint.h>
-#include <vector>
+#include <array>
#include <bitset>
#include <functional>
#include <atomic>
+#include "ActuatorCoordinates.h"
+#include "TSRingBuffer.h"
+
class StepperMotor;
+class Block;
+
+// handle 2.62 Fixed point
+#define STEPTICKER_FPSCALE (1LL<<62)
+#define STEPTICKER_FROMFP(x) ((float)(x)/STEPTICKER_FPSCALE)
class StepTicker{
public:
- friend class StepperMotor;
- static StepTicker* global_step_ticker;
-
StepTicker();
~StepTicker();
void set_frequency( float frequency );
- void signal_a_move_finished();
- void set_reset_delay( float seconds );
+ void set_unstep_time( float microseconds );
int register_motor(StepperMotor* motor);
- void add_motor_to_active_list(StepperMotor* motor);
- void remove_motor_from_active_list(StepperMotor* motor);
- void set_acceleration_ticks_per_second(uint32_t acceleration_ticks_per_second);
+ float get_frequency() const { return frequency; }
+ void unstep_tick();
+ const Block *get_current_block() const { return current_block; }
- void reset_tick();
- void TIMER0_IRQHandler (void);
- void PendSV_IRQHandler (void);
+ void step_tick (void);
+ void handle_finish (void);
+ void start();
- void register_acceleration_tick_handler(std::function<void(void)> cb){
- acceleration_tick_handlers.push_back(cb);
- }
+ // whatever setup the block should register this to know when it is done
+ std::function<void()> finished_fnc{nullptr};
+
+ static StepTicker *getInstance() { return instance; }
private:
+ static StepTicker *instance;
+
+ bool start_next_block();
+
float frequency;
- uint32_t delay;
uint32_t period;
- uint32_t acceleration_tick_period;
- uint32_t acceleration_tick_cnt;
- std::vector<std::function<void(void)>> acceleration_tick_handlers;
- std::vector<StepperMotor*> motor;
- std::bitset<32> active_motor; // limit to 32 motors
- std::atomic_uchar do_move_finished;
- uint8_t num_motors:5;
- volatile bool a_move_finished;
- volatile bool do_acceleration_tick;
- volatile bool reset_step_pins;
-};
+ std::array<StepperMotor*, k_max_actuators> motor;
+ std::bitset<k_max_actuators> unstep;
+ Block *current_block;
+ uint32_t current_tick{0};
-
-#endif
+ struct {
+ volatile bool running:1;
+ uint8_t num_motors:4;
+ };
+};