Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_Button/AP_Button.h
Views: 1798
/*1This program is free software: you can redistribute it and/or modify2it under the terms of the GNU General Public License as published by3the Free Software Foundation, either version 3 of the License, or4(at your option) any later version.56This program is distributed in the hope that it will be useful,7but WITHOUT ANY WARRANTY; without even the implied warranty of8MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9GNU General Public License for more details.1011You should have received a copy of the GNU General Public License12along with this program. If not, see <http://www.gnu.org/licenses/>.13*/14#pragma once1516#include <AP_HAL/AP_HAL_Boards.h>1718#ifndef HAL_BUTTON_ENABLED19#define HAL_BUTTON_ENABLED 120#endif2122#if HAL_BUTTON_ENABLED2324#include <AP_HAL/AP_HAL.h>25#include <AP_Param/AP_Param.h>2627// allow buttons for up to 4 pins28#define AP_BUTTON_NUM_PINS 42930// how often we send reports31#define AP_BUTTON_REPORT_PERIOD_MS 10003233class AP_Button {34public:35// constructor36AP_Button(void);3738/* Do not allow copies */39CLASS_NO_COPY(AP_Button);4041static const struct AP_Param::GroupInfo var_info[];4243// update button state and send messages, called periodically by main loop44void update(void);4546static AP_Button *get_singleton(void) {47return _singleton;48}4950// get state of a button51// used by scripting52bool get_button_state(uint8_t number);5354// check settings are valid55bool arming_checks(size_t buflen, char *buffer) const;5657private:5859static AP_Button *_singleton;6061AP_Int8 enable;62AP_Int8 pin[AP_BUTTON_NUM_PINS];63AP_Int8 options[AP_BUTTON_NUM_PINS]; // if a pin's bit is set then it uses PWM assertion6465bool is_pwm_input(uint8_t n) const {66return ((uint8_t)options[n].get() & (1U<<0)) != 0;67}68bool is_input_inverted(uint8_t n) const {69return ((uint8_t)options[n].get() & (1U<<1)) != 0;70}7172AP_Int16 pin_func[AP_BUTTON_NUM_PINS]; // from the RC_Channel functions7374// number of seconds to send change notifications75AP_Int16 report_send_time;7677// last button press mask78uint8_t last_mask;7980// debounced button press mask81uint8_t debounce_mask;8283// current state of PWM pins:84uint8_t pwm_state;85uint8_t tentative_pwm_state; // for debouncing86uint64_t pwm_start_debounce_ms;8788// mask indicating which action was most recent taken for pins89uint8_t state_actioned_mask;9091// pwm sources are used when using PWM on the input92AP_HAL::PWMSource pwm_pin_source[AP_BUTTON_NUM_PINS];9394// state from the timer interrupt:95HAL_Semaphore last_debounced_change_ms_sem;96// last time GPIO pins were debounced:97uint64_t last_debounced_change_ms;9899// time at least last debounce changes across both PWM and GPIO100// pins was detected:101uint64_t last_debounce_ms;102103// when the mask last changed104uint64_t last_change_time_ms;105106// when button change events were last actioned107uint64_t last_action_time_ms;108109// true if allocated aux functions have been initialised110bool aux_functions_initialised;111// call init_aux_function for all used functions112void run_aux_functions(bool force);113114// time of last report115uint32_t last_report_ms;116117// has the timer been installed?118bool initialised:1;119120// called by timer thread121void timer_update(void);122123// get current mask124uint8_t get_mask(void);125126// send a BUTTON_CHANGE report127void send_report(void) const;128129// setup pins as pullup input130void setup_pins();131};132133namespace AP {134AP_Button &button();135};136137#endif138139140