From f09b86b26227e860a7705f62cc0ef5c4cb532c33 Mon Sep 17 00:00:00 2001 From: Paul Guenette Date: Sun, 19 May 2019 22:49:18 +0200 Subject: [PATCH] Trigger an update to GPIO configuration on property write --- Firmware/MotorControl/endstop.cpp | 38 ++++++++++++++++++------------- Firmware/MotorControl/endstop.hpp | 11 +++++---- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/Firmware/MotorControl/endstop.cpp b/Firmware/MotorControl/endstop.cpp index d01c361b..809e52f1 100644 --- a/Firmware/MotorControl/endstop.cpp +++ b/Firmware/MotorControl/endstop.cpp @@ -1,8 +1,8 @@ #include -Endstop::Endstop(Endstop::Config_t &config) +Endstop::Endstop(Endstop::Config_t& config) : config_(config) { - set_endstop_enabled(config_.enabled); + set_endstop_enabled(config_.enabled); } void Endstop::update() { @@ -10,14 +10,14 @@ void Endstop::update() { GPIO_TypeDef* gpio_port = get_gpio_port_by_pin(config_.gpio_num); auto last_pin_state = pin_state_; pin_state_ = HAL_GPIO_ReadPin(gpio_port, gpio_pin); - if(pin_state_ != last_pin_state){ + if (pin_state_ != last_pin_state) { debounce_timer_ = axis_->loop_counter_ * current_meas_period; } if (config_.enabled) { float now = axis_->loop_counter_ * current_meas_period; - if ((now - debounce_timer_) >= (config_.debounce_ms * 0.001f)) { // Debounce timer expired, take the new pin state - endstop_state_ = config_.is_active_high ? pin_state_ : !pin_state_; // endstop_state is the logical state - debounce_timer_ = now - (config_.debounce_ms * 0.001f); // Ensure timer doesn't have overflow issues + if ((now - debounce_timer_) >= (config_.debounce_ms * 0.001f)) { // Debounce timer expired, take the new pin state + endstop_state_ = config_.is_active_high ? pin_state_ : !pin_state_; // endstop_state is the logical state + debounce_timer_ = now - (config_.debounce_ms * 0.001f); // Ensure timer doesn't have overflow issues } else { endstop_state_ = endstop_state_; // Do nothing } @@ -30,15 +30,21 @@ bool Endstop::getEndstopState() { return endstop_state_; } -void Endstop::set_endstop_enabled(bool enable){ - uint16_t gpio_pin = get_gpio_pin_by_pin(config_.gpio_num); - GPIO_TypeDef* gpio_port = get_gpio_port_by_pin(config_.gpio_num); - if(enable){ - HAL_GPIO_DeInit(gpio_port, gpio_pin); - GPIO_InitTypeDef GPIO_InitStruct; - GPIO_InitStruct.Pin = gpio_pin; - GPIO_InitStruct.Mode = GPIO_MODE_INPUT; - GPIO_InitStruct.Pull = config_.is_active_high ? GPIO_PULLDOWN : GPIO_PULLUP;; - HAL_GPIO_Init(gpio_port, &GPIO_InitStruct); +void Endstop::update_endstop_config(){ + set_endstop_enabled(config_.enabled); +} + +void Endstop::set_endstop_enabled(bool enable) { + if (config_.gpio_num != 0) { + uint16_t gpio_pin = get_gpio_pin_by_pin(config_.gpio_num); + GPIO_TypeDef* gpio_port = get_gpio_port_by_pin(config_.gpio_num); + if (enable) { + HAL_GPIO_DeInit(gpio_port, gpio_pin); + GPIO_InitTypeDef GPIO_InitStruct; + GPIO_InitStruct.Pin = gpio_pin; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = config_.is_active_high ? GPIO_PULLDOWN : GPIO_PULLUP; + HAL_GPIO_Init(gpio_port, &GPIO_InitStruct); + } } } \ No newline at end of file diff --git a/Firmware/MotorControl/endstop.hpp b/Firmware/MotorControl/endstop.hpp index 9ff3f079..cdca3fdc 100644 --- a/Firmware/MotorControl/endstop.hpp +++ b/Firmware/MotorControl/endstop.hpp @@ -16,19 +16,22 @@ class Endstop { Endstop::Config_t& config_; Axis* axis_ = nullptr; + void update_endstop_config(); void set_endstop_enabled(bool enable); - void update(); + void update(); bool getEndstopState(); bool endstop_state_ = false; auto make_protocol_definitions() { return make_protocol_member_list( - make_protocol_ro_property("endstop_state_", &endstop_state_), + make_protocol_ro_property("endstop_state", &endstop_state_), make_protocol_object("config", - make_protocol_property("gpio_num", &config_.gpio_num), - make_protocol_property("enabled", &config_.enabled), + make_protocol_property("gpio_num", &config_.gpio_num, + [](void* ctx) { static_cast(ctx)->update_endstop_config(); }, this), + make_protocol_property("enabled", &config_.enabled, + [](void* ctx) { static_cast(ctx)->update_endstop_config(); }, this), make_protocol_property("offset", &config_.offset), make_protocol_property("is_active_high", &config_.is_active_high), make_protocol_property("debounce_ms", &config_.debounce_ms)));