Revamp Endstop timers

This commit is contained in:
Unknown
2020-03-27 21:11:39 -04:00
parent f98da473f1
commit 8a0a22eb6f
3 changed files with 28 additions and 18 deletions
+20 -15
View File
@@ -3,24 +3,25 @@
Endstop::Endstop(Endstop::Config_t& config)
: config_(config) {
update_config();
debounceTimer_.setInterval(current_meas_period);
}
void Endstop::update() {
uint16_t gpio_pin = get_gpio_pin_by_pin(config_.gpio_num);
GPIO_TypeDef* gpio_port = get_gpio_port_by_pin(config_.gpio_num);
bool last_pin_state = pin_state_;
pin_state_ = HAL_GPIO_ReadPin(gpio_port, gpio_pin);
uint32_t now = static_cast<uint32_t>(axis_->loop_counter_ * current_meas_period * 1000);
if (pin_state_ != last_pin_state) {
debounce_timer_ = now;
}
debounceTimer_.update();
if (config_.enabled) {
if ((now - debounce_timer_) >= config_.debounce_ms) { // Debounce timer expired, take the new pin state
bool last_pin_state = pin_state_;
uint16_t gpio_pin = get_gpio_pin_by_pin(config_.gpio_num);
GPIO_TypeDef* gpio_port = get_gpio_port_by_pin(config_.gpio_num);
pin_state_ = HAL_GPIO_ReadPin(gpio_port, gpio_pin);
// If the pin state has changed, reset the timer
if (pin_state_ != last_pin_state)
debounceTimer_.reset();
if (debounceTimer_.expired())
endstop_state_ = config_.is_active_high ? pin_state_ : !pin_state_; // endstop_state is the logical state
debounce_timer_ = now - config_.debounce_ms; // Ensure timer doesn't have overflow issues
} else {
endstop_state_ = endstop_state_; // Do nothing
}
} else {
endstop_state_ = false;
}
@@ -30,11 +31,13 @@ bool Endstop::get_state() {
return endstop_state_;
}
void Endstop::update_config(){
void Endstop::update_config() {
set_enabled(config_.enabled);
debounceTimer_.setInterval(config_.debounce_ms * 0.001f);
}
void Endstop::set_enabled(bool enable) {
debounceTimer_.reset();
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);
@@ -45,6 +48,8 @@ void Endstop::set_enabled(bool enable) {
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = config_.pullup ? GPIO_PULLUP : GPIO_PULLDOWN;
HAL_GPIO_Init(gpio_port, &GPIO_InitStruct);
}
debounceTimer_.start();
} else
debounceTimer_.stop();
}
}