Skip to content

Commit

Permalink
hardware-mapping: debounce inputs in TestSwitch()
Browse files Browse the repository at this point in the history
Signed-off-by: H Hartley Sweeten <hsweeten@visionengravers.com>
  • Loading branch information
bigguiness authored and hzeller committed Jan 17, 2019
1 parent 3c66fc5 commit e13276a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/gcode-machine-control.cc
Expand Up @@ -384,14 +384,14 @@ void GCodeMachineControl::Impl::wait_for_start() {
if (pause_enabled_ && check_for_pause()) { if (pause_enabled_ && check_for_pause()) {
mprintf("// BeagleG: pause switch active\n"); mprintf("// BeagleG: pause switch active\n");
int pause_active = PAUSE_ACTIVE_DETECT; int pause_active = PAUSE_ACTIVE_DETECT;
// The switch reading is debounced. We add additional delay with
// the while loop to ensure that the pause switch has been cleared.
while (pause_active) { while (pause_active) {
usleep(1000); usleep(1000);
// TODO: we should probably have de-bouncing logic rather in the
// hardware mapping. E.g. something like TestPauseSwitch(2000).
if (check_for_pause()) if (check_for_pause())
pause_active = PAUSE_ACTIVE_DETECT; pause_active = PAUSE_ACTIVE_DETECT;
else else
pause_active--; pause_active--;
} }
mprintf("// BeagleG: pause switch cleared\n"); mprintf("// BeagleG: pause switch cleared\n");
} }
Expand Down
18 changes: 15 additions & 3 deletions src/hardware-mapping.cc
Expand Up @@ -305,9 +305,21 @@ HardwareMapping::AxisTrigger HardwareMapping::AvailableAxisSwitch(LogicAxis axis
bool HardwareMapping::TestSwitch(const int switch_number, bool def_result) { bool HardwareMapping::TestSwitch(const int switch_number, bool def_result) {
if (!is_hardware_initialized_) return def_result; if (!is_hardware_initialized_) return def_result;
GPIODefinition gpio_def = get_endstop_gpio_descriptor(switch_number); GPIODefinition gpio_def = get_endstop_gpio_descriptor(switch_number);
if (gpio_def != GPIO_NOT_MAPPED) if (gpio_def == GPIO_NOT_MAPPED) return def_result;
return (get_gpio(gpio_def) == trigger_level_[switch_number-1]); bool state = get_gpio(gpio_def);
return def_result; int debounce = 0;
for (;;) {
usleep(10);
bool new_state = get_gpio(gpio_def);
if (new_state == state) {
debounce++;
if (debounce == 2) break;
} else {
state = new_state;
debounce = 0;
}
}
return (state == trigger_level_[switch_number-1]);
} }


bool HardwareMapping::TestAxisSwitch(LogicAxis axis, AxisTrigger requested_trigger) { bool HardwareMapping::TestAxisSwitch(LogicAxis axis, AxisTrigger requested_trigger) {
Expand Down

0 comments on commit e13276a

Please sign in to comment.