2016-07-21 22:04:24 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2019-05-20 11:55:59 -03:00
|
|
|
#pragma once
|
2016-07-21 22:04:24 -03:00
|
|
|
|
2021-08-12 00:38:18 -03:00
|
|
|
#include <AP_HAL/AP_HAL_Boards.h>
|
|
|
|
|
|
|
|
#ifndef HAL_BUTTON_ENABLED
|
|
|
|
#define HAL_BUTTON_ENABLED 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAL_BUTTON_ENABLED
|
|
|
|
|
2016-07-21 22:04:24 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_Param/AP_Param.h>
|
|
|
|
|
|
|
|
// allow buttons for up to 4 pins
|
|
|
|
#define AP_BUTTON_NUM_PINS 4
|
|
|
|
|
|
|
|
// how often we send reports
|
|
|
|
#define AP_BUTTON_REPORT_PERIOD_MS 1000
|
|
|
|
|
|
|
|
class AP_Button {
|
|
|
|
public:
|
|
|
|
// constructor
|
|
|
|
AP_Button(void);
|
|
|
|
|
2019-11-25 09:14:52 -04:00
|
|
|
/* Do not allow copies */
|
|
|
|
AP_Button(const AP_Button &other) = delete;
|
|
|
|
AP_Button &operator=(const AP_Button&) = delete;
|
|
|
|
|
2016-07-21 22:04:24 -03:00
|
|
|
static const struct AP_Param::GroupInfo var_info[];
|
|
|
|
|
|
|
|
// update button state and send messages, called periodically by main loop
|
|
|
|
void update(void);
|
2019-11-25 09:14:52 -04:00
|
|
|
|
|
|
|
static AP_Button *get_singleton(void) {
|
|
|
|
return _singleton;
|
|
|
|
}
|
|
|
|
|
2019-12-23 19:22:29 -04:00
|
|
|
// get state of a button
|
|
|
|
// used by scripting
|
|
|
|
bool get_button_state(uint8_t number);
|
|
|
|
|
2021-07-24 04:07:13 -03:00
|
|
|
// check settings are valid
|
|
|
|
bool arming_checks(size_t buflen, char *buffer) const;
|
|
|
|
|
2016-07-21 22:04:24 -03:00
|
|
|
private:
|
2019-11-25 09:14:52 -04:00
|
|
|
|
|
|
|
static AP_Button *_singleton;
|
|
|
|
|
2016-07-21 22:04:24 -03:00
|
|
|
AP_Int8 enable;
|
|
|
|
AP_Int8 pin[AP_BUTTON_NUM_PINS];
|
2020-10-01 01:13:47 -03:00
|
|
|
AP_Int8 options[AP_BUTTON_NUM_PINS]; // if a pin's bit is set then it uses PWM assertion
|
|
|
|
|
|
|
|
bool is_pwm_input(uint8_t n) const {
|
|
|
|
return ((uint8_t)options[n].get() & (1U<<0)) != 0;
|
|
|
|
}
|
2020-10-01 02:04:16 -03:00
|
|
|
bool is_input_inverted(uint8_t n) const {
|
|
|
|
return ((uint8_t)options[n].get() & (1U<<1)) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AP_Int16 pin_func[AP_BUTTON_NUM_PINS]; // from the RC_Channel functions
|
2016-07-21 22:04:24 -03:00
|
|
|
|
|
|
|
// number of seconds to send change notifications
|
|
|
|
AP_Int16 report_send_time;
|
|
|
|
|
|
|
|
// last button press mask
|
|
|
|
uint8_t last_mask;
|
|
|
|
|
2019-12-23 19:22:29 -04:00
|
|
|
// debounced button press mask
|
|
|
|
uint8_t debounce_mask;
|
|
|
|
|
2020-10-01 01:13:47 -03:00
|
|
|
// current state of PWM pins:
|
|
|
|
uint8_t pwm_state;
|
2021-08-13 02:48:43 -03:00
|
|
|
uint8_t tentative_pwm_state; // for debouncing
|
|
|
|
uint64_t pwm_start_debounce_ms;
|
2020-10-01 01:13:47 -03:00
|
|
|
|
|
|
|
// mask indicating which action was most recent taken for pins
|
|
|
|
uint8_t state_actioned_mask;
|
|
|
|
|
|
|
|
// pwm sources are used when using PWM on the input
|
|
|
|
AP_HAL::PWMSource pwm_pin_source[AP_BUTTON_NUM_PINS];
|
|
|
|
|
2020-10-01 02:04:16 -03:00
|
|
|
// state from the timer interrupt:
|
|
|
|
HAL_Semaphore last_debounced_change_ms_sem;
|
|
|
|
// last time GPIO pins were debounced:
|
2020-10-01 01:13:47 -03:00
|
|
|
uint64_t last_debounced_change_ms;
|
|
|
|
|
2020-10-01 02:04:16 -03:00
|
|
|
// time at least last debounce changes across both PWM and GPIO
|
|
|
|
// pins was detected:
|
|
|
|
uint64_t last_debounce_ms;
|
|
|
|
|
2016-07-21 22:04:24 -03:00
|
|
|
// when the mask last changed
|
|
|
|
uint64_t last_change_time_ms;
|
|
|
|
|
2020-10-01 02:04:16 -03:00
|
|
|
// when button change events were last actioned
|
|
|
|
uint64_t last_action_time_ms;
|
|
|
|
|
|
|
|
// true if allocated aux functions have been initialised
|
|
|
|
bool aux_functions_initialised;
|
|
|
|
// call init_aux_function for all used functions
|
|
|
|
void run_aux_functions(bool force);
|
|
|
|
|
2016-07-21 22:04:24 -03:00
|
|
|
// time of last report
|
|
|
|
uint32_t last_report_ms;
|
|
|
|
|
|
|
|
// has the timer been installed?
|
|
|
|
bool initialised:1;
|
|
|
|
|
|
|
|
// called by timer thread
|
|
|
|
void timer_update(void);
|
|
|
|
|
|
|
|
// get current mask
|
|
|
|
uint8_t get_mask(void);
|
|
|
|
|
|
|
|
// send a BUTTON_CHANGE report
|
2021-02-01 12:26:26 -04:00
|
|
|
void send_report(void) const;
|
2016-07-21 22:04:24 -03:00
|
|
|
|
|
|
|
// setup pins as pullup input
|
|
|
|
void setup_pins();
|
|
|
|
};
|
|
|
|
|
2019-11-25 09:14:52 -04:00
|
|
|
namespace AP {
|
|
|
|
AP_Button &button();
|
|
|
|
};
|
2021-08-12 00:38:18 -03:00
|
|
|
|
|
|
|
#endif
|