Browse Source

Introduced convenient functions

feature/program_options
Maximilian Stiefel 4 years ago
parent
commit
a19d0c566f
  1. 35
      drivers/a4988.cpp
  2. 25
      include/a4988.h
  3. 8
      main.cpp

35
drivers/a4988.cpp

@ -38,10 +38,20 @@ namespace simon {
std::this_thread::sleep_for(std::chrono::microseconds(t_sleep)); 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, 0);
} }
else if (*cmd == "enable") {
auto enable = std::static_pointer_cast<c_enable>(cmd);
gpioWrite(m_pset->gpio_en, enable->enabled);
}
else if (*cmd == "kill") {
m_alive = false;
}
else {
// TODO: Throw an exception here
}
m_queue.pop(); m_queue.pop();
} }
else { else {
m_alive = false; // Idle
} }
} }
} }
@ -49,6 +59,14 @@ namespace simon {
{ {
m_queue.push(cmd); m_queue.push(cmd);
} }
void c_allegro_4988::post_move(const unsigned steps, const bool dir,
const unsigned period_us)
{
auto move(std::make_shared<c_move>(steps, dir, period_us));
auto cmd = std::static_pointer_cast<c_cmd>(move);
m_queue.push(cmd);
}
void c_allegro_4988::initialize(void) void c_allegro_4988::initialize(void)
{ {
// TODO: proper error management throwing exceptions // TODO: proper error management throwing exceptions
@ -73,15 +91,26 @@ namespace simon {
int c_allegro_4988::enable(void) int c_allegro_4988::enable(void)
{ {
// TODO: proper error handling // TODO: proper error handling
gpioWrite(m_pset->gpio_en, 0); auto enable(std::make_shared<c_enable>(true));
auto cmd = std::static_pointer_cast<c_cmd>(enable);
m_queue.push(cmd);
return 0; return 0;
} }
int c_allegro_4988::disable(void) int c_allegro_4988::disable(void)
{ {
// TODO: proper error handling // TODO: proper error handling
gpioWrite(m_pset->gpio_en, 1); auto enable(std::make_shared<c_enable>(false));
auto cmd = std::static_pointer_cast<c_cmd>(enable);
m_queue.push(cmd);
return 0; return 0;
} }
void c_allegro_4988::kill(void)
{
// TODO: proper error handling
auto kill(std::make_shared<c_kill>());
auto cmd = std::static_pointer_cast<c_cmd>(kill);
m_queue.push(cmd);
}
int c_allegro_4988::set_pwm_freq(const unsigned pwm_freq) int c_allegro_4988::set_pwm_freq(const unsigned pwm_freq)
{ {
if (pwm_freq > PI_HW_PWM_MAX_FREQ) { if (pwm_freq > PI_HW_PWM_MAX_FREQ) {

25
include/a4988.h

@ -34,17 +34,27 @@ namespace simon {
class c_move : public c_cmd class c_move : public c_cmd
{ {
public: public:
unsigned steps; const unsigned steps;
bool dir; const bool dir;
unsigned period_us; const unsigned period_us;
explicit c_move(void) : c_cmd("move"){} c_move(const unsigned isteps, const bool idir,
const unsigned iperiod_us)
: c_cmd("move"), steps(isteps), dir(idir), period_us(iperiod_us){}
}; };
class c_enable : public c_cmd class c_enable : public c_cmd
{ {
public: public:
bool enabled; const bool enabled;
explicit c_enable(void) : c_cmd("enable"){} explicit c_enable(const bool ienabled)
: c_cmd("enable"), enabled(ienabled) {}
};
class c_kill : public c_cmd
{
public:
c_kill(void)
: c_cmd("kill") {}
}; };
class c_allegro_4988 class c_allegro_4988
@ -53,7 +63,10 @@ namespace simon {
c_allegro_4988(a4988_settings_t* set); c_allegro_4988(a4988_settings_t* set);
int enable(void); int enable(void);
int disable(void); int disable(void);
void kill(void);
void post(std::shared_ptr<c_cmd> cmd); void post(std::shared_ptr<c_cmd> cmd);
void post_move(const unsigned steps, const bool dir,
const unsigned period_us);
void run(void); void run(void);
private: private:
a4988_settings_t* m_pset; a4988_settings_t* m_pset;

8
main.cpp

@ -16,13 +16,9 @@ int main()
} }
}; };
c_allegro_4988 a4988(&a4988_set); c_allegro_4988 a4988(&a4988_set);
auto move(std::make_shared<c_move>());
move->steps = 32768;
move->dir = true;
move->period_us = 1000;
auto cmd = std::static_pointer_cast<c_cmd>(move);
std::thread t1(std::bind(&c_allegro_4988::run, &a4988)); std::thread t1(std::bind(&c_allegro_4988::run, &a4988));
a4988.post(cmd); a4988.post_move(32768, true, 100);
a4988.kill();
t1.join(); t1.join();
std::cout << "Hello camera" << std::endl; std::cout << "Hello camera" << std::endl;
return 0; return 0;

Loading…
Cancel
Save