Merge pull request #1307 from wolfmanjm/upstreamedge
[clinton/Smoothieware.git] / src / libs / StepTicker.h
index ffc3993..6b9cf62 100644 (file)
@@ -7,44 +7,59 @@
 
 
 
-#ifndef STEPTICKER_H
-#define STEPTICKER_H
+#pragma once
 
-using namespace std;
-#include <vector>
-#include "libs/nuts_bolts.h"
-#include "libs/Module.h"
-#include "libs/Kernel.h"
-#include "libs/actuators/StepperMotor.h"
+#include <stdint.h>
+#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:
         StepTicker();
-        void set_frequency( double frequency );
-        void tick();
-        void signal_moves_finished();
-        StepperMotor* add_stepper_motor(StepperMotor* stepper_motor);
-        void set_reset_delay( double seconds );
-        void reset_tick();
-        void add_motor_to_active_list(StepperMotor* motor);
-        void remove_motor_from_active_list(StepperMotor* motor);
-
-        double frequency;
-        vector<StepperMotor*> stepper_motors;
-        uint32_t delay;
-        uint32_t period;
-        uint32_t debug;
-        uint32_t last_duration;
-        bool has_axes;
+        ~StepTicker();
+        void set_frequency( float frequency );
+        void set_unstep_time( float microseconds );
+        int register_motor(StepperMotor* motor);
+        float get_frequency() const { return frequency; }
+        void unstep_tick();
+        const Block *get_current_block() const { return current_block; }
 
-        bool moves_finished;
-        bool reset_step_pins;
+        void step_tick (void);
+        void handle_finish (void);
+        void start();
 
-        StepperMotor* active_motors[12];
-        uint32_t active_motor_bm;
+        // 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();
 
-#endif
+        float frequency;
+        uint32_t period;
+        std::array<StepperMotor*, k_max_actuators> motor;
+        std::bitset<k_max_actuators> unstep;
+
+        Block *current_block;
+        uint32_t current_tick{0};
+
+        struct {
+            volatile bool running:1;
+            uint8_t num_motors:4;
+        };
+};