diff --git a/.gitignore b/.gitignore index 51fac7f..b1b30ca 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ - +*.swp main # Created by https://www.toptal.com/developers/gitignore/api/c++ diff --git a/drivers/a4988.cpp b/drivers/a4988.cpp index 5adf0db..f38f796 100644 --- a/drivers/a4988.cpp +++ b/drivers/a4988.cpp @@ -3,10 +3,13 @@ #include #include #include +#include +#include #include namespace simon { + namespace a4988 { c_allegro_4988::c_allegro_4988(a4988_settings_t* pset) : m_pset(pset), m_initialized(false), m_alive(true) { @@ -21,16 +24,20 @@ namespace simon { { std::cout << "In thread" << std::endl; if (!m_queue.empty()) { - auto& move = m_queue.front(); - if (!set_pwm_freq(std::round(1e6/move.period_us))) { - // TODO: proper error handling + auto cmd = m_queue.front(); + // Distinguish between different tytpes of commands and derive type + if (*cmd == "move") { + auto move = std::static_pointer_cast(cmd); + if (!set_pwm_freq(std::round(1e6/move->period_us))) { + // TODO: proper error handling + } + gpioHardwarePWM(m_pset->gpio_step, m_pwm_freq, PI_HW_PWM_RANGE/2); + gpioWrite(m_pset->gpio_dir, move->dir); + std::cout << move->period_us << " - " << move->steps << std::endl; + auto t_sleep = move->period_us*move->steps; + std::this_thread::sleep_for(std::chrono::microseconds(t_sleep)); + gpioHardwarePWM(m_pset->gpio_step, m_pwm_freq, 0); } - gpioHardwarePWM(m_pset->gpio_step, m_pwm_freq, PI_HW_PWM_RANGE/2); - gpioWrite(m_pset->gpio_dir, move.dir); - std::cout << move.period_us << " - " << move.steps << std::endl; - auto t_sleep = move.period_us*move.steps; - std::this_thread::sleep_for(std::chrono::microseconds(t_sleep)); - gpioHardwarePWM(m_pset->gpio_step, m_pwm_freq, 0); m_queue.pop(); } else { @@ -38,9 +45,9 @@ namespace simon { } } } - void c_allegro_4988::post(move_t move) + void c_allegro_4988::post(std::shared_ptr cmd) { - m_queue.push(move); + m_queue.push(cmd); } void c_allegro_4988::initialize(void) { @@ -83,5 +90,5 @@ namespace simon { m_pwm_freq = pwm_freq; return 0; } - -} + } // namespace a4988 +} // namespace simon diff --git a/include/a4988.h b/include/a4988.h index 368c11d..c17abb8 100644 --- a/include/a4988.h +++ b/include/a4988.h @@ -6,44 +6,66 @@ #define CFG_DEPRECATED_ARG 0 #include +#include +#include namespace simon { + namespace a4988 { + typedef struct + { + const unsigned gpio_en; + const unsigned gpio_step; + const unsigned gpio_dir; + const bool gpio_microsteps[3]; + } a4988_settings_t; - typedef struct - { - const unsigned gpio_en; - const unsigned gpio_step; - const unsigned gpio_dir; - const bool gpio_microsteps[3]; - } a4988_settings_t; - - typedef struct - { - unsigned steps; - bool dir; - unsigned period_us; - } move_t; - - - class c_allegro_4988 - { - public: - c_allegro_4988(a4988_settings_t* set); - int enable(void); - int disable(void); - void post(move_t move); - void run(void); - private: - a4988_settings_t* m_pset; - bool m_initialized; - std::queue m_queue; - unsigned m_pwm_freq; - bool m_alive; - private: - void initialize(void); - int set_pwm_freq(const unsigned pwm_freq); - }; + class c_cmd + { + public: + explicit c_cmd(const std::string cmd) : m_cmd(cmd) {} + bool operator==(const std::string& vgl) { + return (vgl==m_cmd ? true : false); + } + virtual ~c_cmd(void) noexcept {} + private: + const std::string m_cmd; + }; + class c_move : public c_cmd + { + public: + unsigned steps; + bool dir; + unsigned period_us; + explicit c_move(void) : c_cmd("move"){} + }; + + class c_enable : public c_cmd + { + public: + bool enabled; + explicit c_enable(void) : c_cmd("enable"){} + }; + + class c_allegro_4988 + { + public: + c_allegro_4988(a4988_settings_t* set); + int enable(void); + int disable(void); + void post(std::shared_ptr cmd); + void run(void); + private: + a4988_settings_t* m_pset; + bool m_initialized; + std::queue> m_queue; + unsigned m_pwm_freq; + bool m_alive; + private: + void initialize(void); + int set_pwm_freq(const unsigned pwm_freq); + }; + } // namespace a4988 } // namespace simon #endif /*INCLUDED_DRIVER_ALLEGRO_4988*/ diff --git a/main.cpp b/main.cpp index ea6e46f..cd91606 100644 --- a/main.cpp +++ b/main.cpp @@ -2,10 +2,11 @@ #include #include #include +#include int main() { - using namespace simon; + using namespace simon::a4988; a4988_settings_t a4988_set{ .gpio_en = 17, .gpio_step = 18, @@ -15,9 +16,13 @@ int main() } }; c_allegro_4988 a4988(&a4988_set); - move_t move{32768, true, 100}; + auto move(std::make_shared()); + move->steps = 32768; + move->dir = true; + move->period_us = 1000; + auto cmd = std::static_pointer_cast(move); std::thread t1(std::bind(&c_allegro_4988::run, &a4988)); - a4988.post(move); + a4988.post(cmd); t1.join(); std::cout << "Hello camera" << std::endl; return 0;