Convert endstop debounce_ms to a uint32 to avoid potential overflow bug

This commit is contained in:
Unknown
2020-03-09 21:08:49 -04:00
parent f9b4c78667
commit a498a1f984
2 changed files with 4 additions and 4 deletions
+2 -2
View File
@@ -10,14 +10,14 @@ void Endstop::update() {
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);
float now = axis_->loop_counter_ * current_meas_period;
uint32_t now = static_cast<uint32_t>(axis_->loop_counter_ * current_meas_period);
if (pin_state_ != last_pin_state) {
debounce_timer_ = now;
}
if (config_.enabled) {
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
debounce_timer_ = config_.debounce_ms; // Ensure timer doesn't have overflow issues
} else {
endstop_state_ = endstop_state_; // Do nothing
}
+2 -2
View File
@@ -5,7 +5,7 @@ class Endstop {
public:
struct Config_t {
float offset = 0;
float debounce_ms = 50.0f;
uint32_t debounce_ms = 50;
uint16_t gpio_num = 0;
bool enabled = false;
bool is_active_high = false;
@@ -42,6 +42,6 @@ class Endstop {
private:
bool pin_state_ = false;
float pos_when_pressed_ = 0.0f;
volatile float debounce_timer_ = 0;
uint32_t debounce_timer_ = 0;
};
#endif