APM:Libraries
AP_Button.cpp
Go to the documentation of this file.
1 /*
2  This program is free software: you can redistribute it and/or modify
3  it under the terms of the GNU General Public License as published by
4  the Free Software Foundation, either version 3 of the License, or
5  (at your option) any later version.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program. If not, see <http://www.gnu.org/licenses/>.
14  */
15 
16 
17 #include "AP_Button.h"
19 #include <GCS_MAVLink/GCS.h>
20 
21 extern const AP_HAL::HAL& hal;
22 
24 
25  // @Param: ENABLE
26  // @DisplayName: Enable button reporting
27  // @Description: This enables the button checking module. When this is disabled the parameters for setting button inputs are not visible
28  // @Values: 0:Disabled, 1:Enabled
29  // @User: Advanced
30  AP_GROUPINFO_FLAGS("ENABLE", 0, AP_Button, enable, 0, AP_PARAM_FLAG_ENABLE),
31 
32  // @Param: PIN1
33  // @DisplayName: First button Pin
34  // @Description: Digital pin number for first button input.
35  // @User: Standard
36  // @Values: -1:Disabled,50:Pixhawk AUXOUT1,51:Pixhawk AUXOUT2,52:Pixhawk AUXOUT3,53:Pixhawk AUXOUT4,54:Pixhawk AUXOUT5,55:Pixhawk AUXOUT6,111:PX4 FMU Relay1,112:PX4 FMU Relay2,113:PX4IO Relay1,114:PX4IO Relay2,115:PX4IO ACC1,116:PX4IO ACC2
37  AP_GROUPINFO("PIN1", 1, AP_Button, pin[0], -1),
38 
39  // @Param: PIN2
40  // @DisplayName: Second button Pin
41  // @Description: Digital pin number for second button input.
42  // @User: Standard
43  // @Values: -1:Disabled,50:Pixhawk AUXOUT1,51:Pixhawk AUXOUT2,52:Pixhawk AUXOUT3,53:Pixhawk AUXOUT4,54:Pixhawk AUXOUT5,55:Pixhawk AUXOUT6,111:PX4 FMU Relay1,112:PX4 FMU Relay2,113:PX4IO Relay1,114:PX4IO Relay2,115:PX4IO ACC1,116:PX4IO ACC2
44  AP_GROUPINFO("PIN2", 2, AP_Button, pin[1], -1),
45 
46  // @Param: PIN3
47  // @DisplayName: Third button Pin
48  // @Description: Digital pin number for third button input.
49  // @User: Standard
50  // @Values: -1:Disabled,50:Pixhawk AUXOUT1,51:Pixhawk AUXOUT2,52:Pixhawk AUXOUT3,53:Pixhawk AUXOUT4,54:Pixhawk AUXOUT5,55:Pixhawk AUXOUT6,111:PX4 FMU Relay1,112:PX4 FMU Relay2,113:PX4IO Relay1,114:PX4IO Relay2,115:PX4IO ACC1,116:PX4IO ACC2
51  AP_GROUPINFO("PIN3", 3, AP_Button, pin[2], -1),
52 
53  // @Param: PIN4
54  // @DisplayName: Fourth button Pin
55  // @Description: Digital pin number for fourth button input.
56  // @User: Standard
57  // @Values: -1:Disabled,50:Pixhawk AUXOUT1,51:Pixhawk AUXOUT2,52:Pixhawk AUXOUT3,53:Pixhawk AUXOUT4,54:Pixhawk AUXOUT5,55:Pixhawk AUXOUT6,111:PX4 FMU Relay1,112:PX4 FMU Relay2,113:PX4IO Relay1,114:PX4IO Relay2,115:PX4IO ACC1,116:PX4IO ACC2
58  AP_GROUPINFO("PIN4", 4, AP_Button, pin[3], -1),
59 
60  // @Param: REPORT_SEND
61  // @DisplayName: Report send time
62  // @Description: The duration in seconds that a BUTTON_CHANGE report is repeatedly sent to the GCS regarding a button changing state. Note that the BUTTON_CHANGE message is MAVLink2 only.
63  // @User: Standard
64  // @Range: 0 3600
65  AP_GROUPINFO("REPORT_SEND", 5, AP_Button, report_send_time, 10),
66 
68 };
69 
70 
71 // constructor
73 {
75 }
76 
77 /*
78  update and report, called from main loop
79  */
81 {
82  if (!enable) {
83  return;
84  }
85 
86  // call setup pins at update rate (5Hz) to allow for runtime parameter change of pins
87  setup_pins();
88 
89  if (!initialised) {
90  initialised = true;
91 
92  // get initial mask
93  last_mask = get_mask();
94 
95  // register 1kHz timer callback
97  }
98 
99  if (last_change_time_ms != 0 &&
102  // send a change report
104 
105  // send a report to GCS
106  send_report();
107  }
108 }
109 
110 /*
111  get current mask
112  */
113 uint8_t AP_Button::get_mask(void)
114 {
115  uint8_t mask = 0;
116  for (uint8_t i=0; i<AP_BUTTON_NUM_PINS; i++) {
117  if (pin[i] == -1) {
118  continue;
119  }
120  mask |= hal.gpio->read(pin[i]) << i;
121  }
122  return mask;
123 }
124 
125 /*
126  called at 1kHz to check for button state change
127  */
129 {
130  if (!enable) {
131  return;
132  }
133  uint8_t mask = get_mask();
134  if (mask != last_mask) {
135  last_mask = mask;
137  }
138 }
139 
140 /*
141  send a BUTTON_CHANGE report to the GCS
142  */
144 {
145  uint8_t chan_mask = GCS_MAVLINK::active_channel_mask();
146  uint32_t now = AP_HAL::millis();
147  for (uint8_t i=0; i<MAVLINK_COMM_NUM_BUFFERS; i++) {
148  if ((chan_mask & (1U<<i)) == 0) {
149  // not active
150  continue;
151  }
152  mavlink_channel_t chan = (mavlink_channel_t)i;
153  if (HAVE_PAYLOAD_SPACE(chan, BUTTON_CHANGE)) {
154  mavlink_msg_button_change_send(chan,
155  now,
156  (uint32_t)last_change_time_ms,
157  last_mask);
158  }
159  }
160 }
161 
162 /*
163  setup the pins as input with pullup. We need pullup to give reliable
164  input with a pulldown button
165  */
167 {
168  for (uint8_t i=0; i<AP_BUTTON_NUM_PINS; i++) {
169  if (pin[i] == -1) {
170  continue;
171  }
172  hal.gpio->pinMode(pin[i], HAL_GPIO_INPUT);
173  // setup pullup
174  hal.gpio->write(pin[i], 1);
175  }
176 }
uint64_t last_change_time_ms
Definition: AP_Button.h:46
#define AP_PARAM_FLAG_ENABLE
Definition: AP_Param.h:56
void timer_update(void)
Definition: AP_Button.cpp:128
Interface definition for the various Ground Control System.
#define AP_GROUPINFO(name, idx, clazz, element, def)
Definition: AP_Param.h:102
uint64_t millis64()
Definition: system.cpp:167
virtual void write(uint8_t pin, uint8_t value)=0
void setup_pins()
Definition: AP_Button.cpp:166
void send_report(void)
Definition: AP_Button.cpp:143
#define AP_GROUPINFO_FLAGS(name, idx, clazz, element, def, flags)
Definition: AP_Param.h:93
#define HAVE_PAYLOAD_SPACE(chan, id)
Definition: GCS.h:28
uint8_t get_mask(void)
Definition: AP_Button.cpp:113
#define AP_BUTTON_REPORT_PERIOD_MS
Definition: AP_Button.h:23
uint32_t millis()
Definition: system.cpp:157
virtual void pinMode(uint8_t pin, uint8_t output)=0
bool initialised
Definition: AP_Button.h:52
AP_Int16 report_send_time
Definition: AP_Button.h:40
const AP_HAL::HAL & hal
Definition: AC_PID_test.cpp:14
#define HAL_GPIO_INPUT
Definition: GPIO.h:7
virtual void register_timer_process(AP_HAL::MemberProc)=0
virtual uint8_t read(uint8_t pin)=0
#define AP_BUTTON_NUM_PINS
Definition: AP_Button.h:20
AP_HAL::AnalogSource * chan
Definition: AnalogIn.cpp:8
AP_HAL::GPIO * gpio
Definition: HAL.h:111
uint8_t last_mask
Definition: AP_Button.h:43
void update(void)
Definition: AP_Button.cpp:80
static const struct AP_Param::GroupInfo var_info[]
Definition: AP_Button.h:30
#define FUNCTOR_BIND_MEMBER(func, rettype,...)
Definition: functor.h:31
AP_Int8 pin[AP_BUTTON_NUM_PINS]
Definition: AP_Button.h:37
static int8_t pin
Definition: AnalogIn.cpp:15
uint32_t last_report_ms
Definition: AP_Button.h:49
AP_Int8 enable
Definition: AP_Button.h:36
AP_Button(void)
Definition: AP_Button.cpp:72
#define AP_GROUPEND
Definition: AP_Param.h:121
AP_HAL::Scheduler * scheduler
Definition: HAL.h:114
static void setup_object_defaults(const void *object_pointer, const struct GroupInfo *group_info)
Definition: AP_Param.cpp:1214