APM:Libraries
Buzzer.cpp
Go to the documentation of this file.
1 /*
2  Buzzer driver
3 */
4 /*
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 #include "Buzzer.h"
19 
20 #include <AP_HAL/AP_HAL.h>
21 
22 #include "AP_Notify.h"
23 
24 #ifndef HAL_BUZZER_ON
25  #define HAL_BUZZER_ON 1
26  #define HAL_BUZZER_OFF 0
27 #endif
28 
29 
30 
31 extern const AP_HAL::HAL& hal;
32 
33 
35 {
36 #if defined(HAL_BUZZER_PIN)
38 #else
40 #endif
41  if(!_pin) return false;
42 
43  // setup the pin and ensure it's off
45  on(false);
46 
47  // set initial boot states. This prevents us issuing a arming
48  // warning in plane and rover on every boot
51  return true;
52 }
53 
54 // update - updates led according to timed_updated. Should be called at 50Hz
56 {
57  // return immediately if disabled
58  if (!AP_Notify::flags.external_leds) {
59  return;
60  }
61 
62  // check for arming failed event
63  if (AP_Notify::events.arming_failed) {
64  // arming failed buzz
66  }
67 
68  // reduce 50hz call down to 10hz
69  _counter++;
70  if (_counter < 5) {
71  return;
72  }
73  _counter = 0;
74 
75  // complete currently played pattern
76  if (_pattern != NONE) {
78  switch (_pattern) {
79  case SINGLE_BUZZ:
80  // buzz for 10th of a second
81  if (_pattern_counter == 1) {
82  on(true);
83  }else{
84  on(false);
85  _pattern = NONE;
86  }
87  return;
88  case DOUBLE_BUZZ:
89  // buzz for 10th of a second
90  switch (_pattern_counter) {
91  case 1:
92  on(true);
93  break;
94  case 2:
95  on(false);
96  break;
97  case 3:
98  on(true);
99  break;
100  case 4:
101  default:
102  on(false);
103  _pattern = NONE;
104  break;
105  }
106  return;
107  case ARMING_BUZZ:
108  // record start time
109  if (_pattern_counter == 1) {
111  on(true);
112  } else {
113  // turn off buzzer after 3 seconds
116  on(false);
117  _pattern = NONE;
118  }
119  }
120  return;
121  case BARO_GLITCH:
122  // four fast tones
123  switch (_pattern_counter) {
124  case 1:
125  case 3:
126  case 5:
127  case 7:
128  case 9:
129  on(true);
130  break;
131  case 2:
132  case 4:
133  case 6:
134  case 8:
135  on(false);
136  break;
137  case 10:
138  on(false);
139  _pattern = NONE;
140  break;
141  default:
142  // do nothing
143  break;
144  }
145  return;
146  case EKF_BAD:
147  // four tones getting shorter)
148  switch (_pattern_counter) {
149  case 1:
150  case 5:
151  case 8:
152  case 10:
153  on(true);
154  break;
155  case 4:
156  case 7:
157  case 9:
158  on(false);
159  break;
160  case 11:
161  on(false);
162  _pattern = NONE;
163  break;
164  default:
165  // do nothing
166  break;
167  }
168  return;
169  default:
170  // do nothing
171  break;
172  }
173  }
174 
175  // check if armed status has changed
178  if (_flags.armed) {
179  // double buzz when armed
181  }else{
182  // single buzz when disarmed
184  }
185  return;
186  }
187 
188  // check ekf bad
191  if (_flags.ekf_bad) {
192  // ekf bad warning buzz
194  }
195  return;
196  }
197 
198  // if vehicle lost was enabled, starting beep
199  if (AP_Notify::flags.vehicle_lost) {
201  }
202 
203  // if battery failsafe constantly single buzz
204  if (AP_Notify::flags.failsafe_battery) {
206  }
207 }
208 
209 // on - turns the buzzer on or off
210 void Buzzer::on(bool turn_on)
211 {
212  // return immediately if nothing to do
213  if (_flags.on == turn_on) {
214  return;
215  }
216 
217  // update state
218  _flags.on = turn_on;
219 
220  // pull pin high or low
222 }
223 
226 {
227  _pattern = pattern_id;
228  _pattern_counter = 0;
229 }
230 
BuzzerPattern
Definition: Buzzer.h:47
uint8_t _pattern_counter
Definition: Buzzer.h:73
uint8_t _pin
Definition: Buzzer.h:75
#define HAL_BUZZER_ON
Definition: Buzzer.cpp:25
uint32_t _arming_buzz_start_ms
Definition: Buzzer.h:74
virtual void write(uint8_t pin, uint8_t value)=0
bool init(void)
init - initialise the buzzer
Definition: Buzzer.cpp:34
#define HAL_BUZZER_OFF
Definition: Buzzer.cpp:26
void update()
update - updates buzzer according to timed_updated. Should be called at 50Hz
Definition: Buzzer.cpp:55
uint32_t millis()
Definition: system.cpp:157
BuzzerPattern _pattern
Definition: Buzzer.h:72
virtual void pinMode(uint8_t pin, uint8_t output)=0
#define BUZZER_ARMING_BUZZ_MS
Definition: Buzzer.h:22
uint8_t get_buzz_pin() const
Definition: AP_Notify.h:146
#define HAL_GPIO_OUTPUT
Definition: GPIO.h:8
const AP_Notify * pNotify
Definition: NotifyDevice.h:24
const AP_HAL::HAL & hal
-*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
Definition: AC_PID_test.cpp:14
struct Buzzer::buzzer_flag_type _flags
static struct notify_flags_and_values_type flags
Definition: AP_Notify.h:117
AP_HAL::GPIO * gpio
Definition: HAL.h:111
void play_pattern(BuzzerPattern pattern_id)
play_pattern - plays the defined buzzer pattern
Definition: Buzzer.cpp:225
uint8_t _counter
Definition: Buzzer.h:71
#define HAL_BUZZER_PIN
Definition: vrbrain.h:50
uint8_t failsafe_battery
Definition: Buzzer.h:67
static struct notify_events_type events
Definition: AP_Notify.h:118
void on(bool on_off)
on - turns the buzzer on or off
Definition: Buzzer.cpp:210