Browse Source

AP_Button: add crude debouncing and get_button_state function

zr-v5.1
Peter Hall 5 years ago committed by WickedShell
parent
commit
ecd7e12dd3
  1. 23
      libraries/AP_Button/AP_Button.cpp
  2. 7
      libraries/AP_Button/AP_Button.h

23
libraries/AP_Button/AP_Button.cpp

@ -18,6 +18,9 @@
#include <GCS_MAVLink/GCS_MAVLink.h> #include <GCS_MAVLink/GCS_MAVLink.h>
#include <GCS_MAVLink/GCS.h> #include <GCS_MAVLink/GCS.h>
// very crude debounce method
#define DEBOUNCE_MS 50
extern const AP_HAL::HAL& hal; extern const AP_HAL::HAL& hal;
AP_Button *AP_Button::_singleton; AP_Button *AP_Button::_singleton;
@ -98,6 +101,7 @@ void AP_Button::update(void)
// get initial mask // get initial mask
last_mask = get_mask(); last_mask = get_mask();
debounce_mask = last_mask;
// register 1kHz timer callback // register 1kHz timer callback
hal.scheduler->register_timer_process(FUNCTOR_BIND_MEMBER(&AP_Button::timer_update, void)); hal.scheduler->register_timer_process(FUNCTOR_BIND_MEMBER(&AP_Button::timer_update, void));
@ -114,6 +118,18 @@ void AP_Button::update(void)
} }
} }
// get state of a button
// used by scripting
bool AP_Button::get_button_state(uint8_t number)
{
// pins params are 1 indexed not zero
if (number == 0 || number > AP_BUTTON_NUM_PINS) {
return false;
}
return ( ((1 << (number - 1)) & debounce_mask) != 0);
};
/* /*
get current mask get current mask
*/ */
@ -138,9 +154,14 @@ void AP_Button::timer_update(void)
return; return;
} }
uint8_t mask = get_mask(); uint8_t mask = get_mask();
uint64_t now = AP_HAL::millis64();
if (mask != last_mask) { if (mask != last_mask) {
last_mask = mask; last_mask = mask;
last_change_time_ms = AP_HAL::millis64(); last_change_time_ms = now;
}
if ((now - last_change_time_ms) > DEBOUNCE_MS) {
// crude de-bouncing, debounces all buttons as one, not individually
debounce_mask = last_mask;
} }
} }

7
libraries/AP_Button/AP_Button.h

@ -41,6 +41,10 @@ public:
return _singleton; return _singleton;
} }
// get state of a button
// used by scripting
bool get_button_state(uint8_t number);
private: private:
static AP_Button *_singleton; static AP_Button *_singleton;
@ -54,6 +58,9 @@ private:
// last button press mask // last button press mask
uint8_t last_mask; uint8_t last_mask;
// debounced button press mask
uint8_t debounce_mask;
// when the mask last changed // when the mask last changed
uint64_t last_change_time_ms; uint64_t last_change_time_ms;

Loading…
Cancel
Save