2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
26 #include "common/utils.h"
28 #include "config/feature.h"
30 #include "drivers/dshot_command.h"
31 #include "drivers/io.h"
32 #include "drivers/pwm_output.h"
33 #include "drivers/sound_beeper.h"
34 #include "drivers/system.h"
35 #include "drivers/time.h"
37 #include "flight/mixer.h"
39 #include "config/config.h"
41 #include "fc/runtime_config.h"
43 #include "io/statusindicator.h"
44 #include "io/vtx_control.h"
50 #include "pg/beeper.h"
52 #include "sensors/battery.h"
53 #include "sensors/sensors.h"
57 #ifdef BEEPER_INVERTED
58 #define IS_OPEN_DRAIN false
59 #define IS_INVERTED true
61 #define IS_OPEN_DRAIN true
62 #define IS_INVERTED false
67 #define BEEPER_PWM_HZ 0
70 #define BEEPER_PIN NONE
71 #define BEEPER_PWM_HZ 0
78 #define MAX_MULTI_BEEPS 64 //size limit for 'beep_multiBeeps[]'
80 #define BEEPER_COMMAND_REPEAT 0xFE
81 #define BEEPER_COMMAND_STOP 0xFF
84 static timeUs_t lastDshotBeaconCommandTimeUs
;
88 /* Beeper Sound Sequences: (Square wave generation)
89 * Sequence must end with 0xFF or 0xFE. 0xFE repeats the sequence from
90 * start when 0xFF stops the sound when it's completed.
92 * "Sound" Sequences are made so that 1st, 3rd, 5th.. are the delays how
93 * long the beeper is on and 2nd, 4th, 6th.. are the delays how long beeper
94 * is off. Delays are in milliseconds/10 (i.e., 5 => 50ms).
97 static const uint8_t beep_shortBeep
[] = {
98 10, 10, BEEPER_COMMAND_STOP
101 static const uint8_t beep_armingBeep
[] = {
102 30, 5, 5, 5, BEEPER_COMMAND_STOP
104 // Arming when GPS is fixed
105 static const uint8_t beep_armingGpsFix
[] = {
106 5, 5, 15, 5, 5, 5, 15, 30, BEEPER_COMMAND_STOP
108 // Arming when GPS is not fixed
109 static const uint8_t beep_armingGpsNoFix
[] = {
110 30, 5, 30, 5, 30, 5, BEEPER_COMMAND_STOP
112 // armed beep (first pause, then short beep)
113 static const uint8_t beep_armedBeep
[] = {
114 0, 245, 10, 5, BEEPER_COMMAND_STOP
117 static const uint8_t beep_disarmBeep
[] = {
118 15, 5, 15, 5, BEEPER_COMMAND_STOP
120 // beeps while stick held in disarm position (after pause)
121 static const uint8_t beep_disarmRepeatBeep
[] = {
122 0, 100, 10, BEEPER_COMMAND_STOP
124 // Long beep and pause after that
125 static const uint8_t beep_lowBatteryBeep
[] = {
126 25, 50, BEEPER_COMMAND_STOP
128 // critical battery beep
129 static const uint8_t beep_critBatteryBeep
[] = {
130 50, 2, BEEPER_COMMAND_STOP
133 // transmitter-signal-lost tone
134 static const uint8_t beep_txLostBeep
[] = {
135 50, 50, BEEPER_COMMAND_STOP
138 static const uint8_t beep_sos
[] = {
139 10, 10, 10, 10, 10, 40, 40, 10, 40, 10, 40, 40, 10, 10, 10, 10, 10, 70, BEEPER_COMMAND_STOP
141 // Ready beeps. When gps has fix and copter is ready to fly.
142 static const uint8_t beep_readyBeep
[] = {
143 4, 5, 4, 5, 8, 5, 15, 5, 8, 5, 4, 5, 4, 5, BEEPER_COMMAND_STOP
145 // 2 fast short beeps
146 static const uint8_t beep_2shortBeeps
[] = {
147 5, 5, 5, 5, BEEPER_COMMAND_STOP
150 static const uint8_t beep_2longerBeeps
[] = {
151 20, 15, 35, 5, BEEPER_COMMAND_STOP
154 static const uint8_t beep_gyroCalibrated
[] = {
155 20, 10, 20, 10, 20, 10, BEEPER_COMMAND_STOP
158 // Cam connection opened
159 static const uint8_t beep_camOpenBeep
[] = {
160 5, 15, 10, 15, 20, BEEPER_COMMAND_STOP
163 // Cam connection close
164 static const uint8_t beep_camCloseBeep
[] = {
165 10, 8, 5, BEEPER_COMMAND_STOP
168 // RC Smoothing filter not initialized - 3 short + 1 long
169 static const uint8_t beep_rcSmoothingInitFail
[] = {
170 10, 10, 10, 10, 10, 10, 50, 25, BEEPER_COMMAND_STOP
173 // array used for variable # of beeps (reporting GPS sat count, etc)
174 static uint8_t beep_multiBeeps
[MAX_MULTI_BEEPS
+ 1];
176 #define BEEPER_CONFIRMATION_BEEP_DURATION 2
177 #define BEEPER_CONFIRMATION_BEEP_GAP_DURATION 20
179 #define BEEPER_WARNING_LONG_BEEP_MULTIPLIER 5
181 #define BEEPER_WARNING_BEEP_1_DURATION 20
182 #define BEEPER_WARNING_BEEP_2_DURATION 5
183 #define BEEPER_WARNING_BEEP_GAP_DURATION 10
185 static bool beeperIsOn
= false;
187 // Place in current sequence
188 static uint16_t beeperPos
= 0;
189 // Time when beeper routine must act next time
190 static uint32_t beeperNextToggleTime
= 0;
191 // Time of last arming beep in microseconds (for blackbox)
192 static uint32_t armingBeepTimeMicros
= 0;
194 static void beeperProcessCommand(timeUs_t currentTimeUs
);
196 typedef struct beeperTableEntry_s
{
198 uint8_t priority
; // 0 = Highest
199 const uint8_t *sequence
;
203 } beeperTableEntry_t
;
206 #define BEEPER_ENTRY(a,b,c,d) a,b,c,d
208 #define BEEPER_ENTRY(a,b,c,d) a,b,c
211 // IMPORTANT: these are in priority order, 0 = Highest
212 static const beeperTableEntry_t beeperTable
[] = {
213 { BEEPER_ENTRY(BEEPER_GYRO_CALIBRATED
, 0, beep_gyroCalibrated
, "GYRO_CALIBRATED") },
214 { BEEPER_ENTRY(BEEPER_RX_LOST
, 1, beep_txLostBeep
, "RX_LOST") },
215 { BEEPER_ENTRY(BEEPER_RX_LOST_LANDING
, 2, beep_sos
, "RX_LOST_LANDING") },
216 { BEEPER_ENTRY(BEEPER_DISARMING
, 3, beep_disarmBeep
, "DISARMING") },
217 { BEEPER_ENTRY(BEEPER_ARMING
, 4, beep_armingBeep
, "ARMING") },
218 { BEEPER_ENTRY(BEEPER_ARMING_GPS_FIX
, 5, beep_armingGpsFix
, "ARMING_GPS_FIX") },
219 { BEEPER_ENTRY(BEEPER_ARMING_GPS_NO_FIX
, 6, beep_armingGpsNoFix
, "ARMING_GPS_NO_FIX") },
220 { BEEPER_ENTRY(BEEPER_BAT_CRIT_LOW
, 7, beep_critBatteryBeep
, "BAT_CRIT_LOW") },
221 { BEEPER_ENTRY(BEEPER_BAT_LOW
, 8, beep_lowBatteryBeep
, "BAT_LOW") },
222 { BEEPER_ENTRY(BEEPER_GPS_STATUS
, 9, beep_multiBeeps
, "GPS_STATUS") },
223 { BEEPER_ENTRY(BEEPER_RX_SET
, 10, beep_shortBeep
, "RX_SET") },
224 { BEEPER_ENTRY(BEEPER_ACC_CALIBRATION
, 11, beep_2shortBeeps
, "ACC_CALIBRATION") },
225 { BEEPER_ENTRY(BEEPER_ACC_CALIBRATION_FAIL
, 12, beep_2longerBeeps
, "ACC_CALIBRATION_FAIL") },
226 { BEEPER_ENTRY(BEEPER_READY_BEEP
, 13, beep_readyBeep
, "READY_BEEP") },
227 { BEEPER_ENTRY(BEEPER_MULTI_BEEPS
, 14, beep_multiBeeps
, "MULTI_BEEPS") }, // FIXME having this listed makes no sense since the beep array will not be initialised.
228 { BEEPER_ENTRY(BEEPER_DISARM_REPEAT
, 15, beep_disarmRepeatBeep
, "DISARM_REPEAT") },
229 { BEEPER_ENTRY(BEEPER_ARMED
, 16, beep_armedBeep
, "ARMED") },
230 { BEEPER_ENTRY(BEEPER_SYSTEM_INIT
, 17, NULL
, "SYSTEM_INIT") },
231 { BEEPER_ENTRY(BEEPER_USB
, 18, NULL
, "ON_USB") },
232 { BEEPER_ENTRY(BEEPER_BLACKBOX_ERASE
, 19, beep_2shortBeeps
, "BLACKBOX_ERASE") },
233 { BEEPER_ENTRY(BEEPER_CRASH_FLIP_MODE
, 20, beep_2longerBeeps
, "CRASH_FLIP") },
234 { BEEPER_ENTRY(BEEPER_CAM_CONNECTION_OPEN
, 21, beep_camOpenBeep
, "CAM_CONNECTION_OPEN") },
235 { BEEPER_ENTRY(BEEPER_CAM_CONNECTION_CLOSE
, 22, beep_camCloseBeep
, "CAM_CONNECTION_CLOSE") },
236 { BEEPER_ENTRY(BEEPER_RC_SMOOTHING_INIT_FAIL
,23, beep_rcSmoothingInitFail
, "RC_SMOOTHING_INIT_FAIL") },
237 { BEEPER_ENTRY(BEEPER_ALL
, 24, NULL
, "ALL") },
240 static const beeperTableEntry_t
*currentBeeperEntry
= NULL
;
242 #define BEEPER_TABLE_ENTRY_COUNT (sizeof(beeperTable) / sizeof(beeperTableEntry_t))
245 * Called to activate/deactivate beeper, using the given "BEEPER_..." value.
246 * This function returns immediately (does not block).
248 void beeper(beeperMode_e mode
)
251 mode
== BEEPER_SILENCE
|| (
252 (beeperConfigMutable()->beeper_off_flags
& BEEPER_GET_FLAG(BEEPER_USB
))
253 && getBatteryState() == BATTERY_NOT_PRESENT
260 const beeperTableEntry_t
*selectedCandidate
= NULL
;
261 for (uint32_t i
= 0; i
< BEEPER_TABLE_ENTRY_COUNT
; i
++) {
262 const beeperTableEntry_t
*candidate
= &beeperTable
[i
];
263 if (candidate
->mode
!= mode
) {
267 if (!currentBeeperEntry
) {
268 selectedCandidate
= candidate
;
272 if (candidate
->priority
< currentBeeperEntry
->priority
) {
273 selectedCandidate
= candidate
;
279 if (!selectedCandidate
) {
283 currentBeeperEntry
= selectedCandidate
;
286 beeperNextToggleTime
= 0;
289 void beeperSilence(void)
297 beeperNextToggleTime
= 0;
300 currentBeeperEntry
= NULL
;
304 * Emits the given number of 20ms beeps (with 200ms spacing).
305 * This function returns immediately (does not block).
307 void beeperConfirmationBeeps(uint8_t beepCount
)
310 uint32_t cLimit
= beepCount
* 2;
311 if (cLimit
> MAX_MULTI_BEEPS
) {
312 cLimit
= MAX_MULTI_BEEPS
;
315 beep_multiBeeps
[i
++] = BEEPER_CONFIRMATION_BEEP_DURATION
;
316 beep_multiBeeps
[i
++] = BEEPER_CONFIRMATION_BEEP_GAP_DURATION
;
317 } while (i
< cLimit
);
318 beep_multiBeeps
[i
] = BEEPER_COMMAND_STOP
;
320 beeper(BEEPER_MULTI_BEEPS
);
323 void beeperWarningBeeps(uint8_t beepCount
)
325 uint8_t longBeepCount
= beepCount
/ BEEPER_WARNING_LONG_BEEP_MULTIPLIER
;
326 uint8_t shortBeepCount
= beepCount
% BEEPER_WARNING_LONG_BEEP_MULTIPLIER
;
331 while (i
< MAX_MULTI_BEEPS
- 1 && count
< WARNING_FLASH_COUNT
) {
332 beep_multiBeeps
[i
++] = WARNING_FLASH_DURATION_MS
/ 10;
333 if (++count
< WARNING_FLASH_COUNT
) {
334 beep_multiBeeps
[i
++] = WARNING_FLASH_DURATION_MS
/ 10;
336 beep_multiBeeps
[i
++] = WARNING_PAUSE_DURATION_MS
/ 10;
340 while (i
< MAX_MULTI_BEEPS
- 1 && longBeepCount
> 0) {
341 beep_multiBeeps
[i
++] = WARNING_CODE_DURATION_LONG_MS
/ 10;
342 if (--longBeepCount
> 0) {
343 beep_multiBeeps
[i
++] = WARNING_CODE_DURATION_LONG_MS
/ 10;
345 beep_multiBeeps
[i
++] = WARNING_PAUSE_DURATION_MS
/ 10;
349 while (i
< MAX_MULTI_BEEPS
- 1 && shortBeepCount
> 0) {
350 beep_multiBeeps
[i
++] = WARNING_CODE_DURATION_SHORT_MS
/ 10;
351 if (--shortBeepCount
> 0) {
352 beep_multiBeeps
[i
++] = WARNING_CODE_DURATION_LONG_MS
/ 10;
356 beep_multiBeeps
[i
] = BEEPER_COMMAND_STOP
;
358 beeper(BEEPER_MULTI_BEEPS
);
362 static void beeperGpsStatus(void)
364 if (!(beeperConfigMutable()->beeper_off_flags
& BEEPER_GET_FLAG(BEEPER_GPS_STATUS
))) {
365 // if GPS fix then beep out number of satellites
366 if (STATE(GPS_FIX
) && gpsSol
.numSat
>= 5) {
369 beep_multiBeeps
[i
++] = 5;
370 beep_multiBeeps
[i
++] = 10;
371 } while (i
< MAX_MULTI_BEEPS
&& gpsSol
.numSat
> i
/ 2);
373 beep_multiBeeps
[i
- 1] = 50; // extend last pause
374 beep_multiBeeps
[i
] = BEEPER_COMMAND_STOP
;
376 beeper(BEEPER_MULTI_BEEPS
); //initiate sequence
383 * Beeper handler function to be called periodically in loop. Updates beeper
384 * state via time schedule.
386 void beeperUpdate(timeUs_t currentTimeUs
)
388 // If beeper option from AUX switch has been selected
389 if (IS_RC_MODE_ACTIVE(BOXBEEPERON
)) {
390 beeper(BEEPER_RX_SET
);
392 } else if (featureIsEnabled(FEATURE_GPS
) && IS_RC_MODE_ACTIVE(BOXBEEPGPSCOUNT
)) {
397 // Beeper routine doesn't need to update if there aren't any sounds ongoing
398 if (currentBeeperEntry
== NULL
) {
402 if (beeperNextToggleTime
> currentTimeUs
) {
408 if (!areMotorsRunning()
409 && ((currentBeeperEntry
->mode
== BEEPER_RX_SET
&& !(beeperConfig()->dshotBeaconOffFlags
& BEEPER_GET_FLAG(BEEPER_RX_SET
)))
410 || (currentBeeperEntry
->mode
== BEEPER_RX_LOST
&& !(beeperConfig()->dshotBeaconOffFlags
& BEEPER_GET_FLAG(BEEPER_RX_LOST
))))) {
412 if ((currentTimeUs
- getLastDisarmTimeUs() > DSHOT_BEACON_GUARD_DELAY_US
) && !isTryingToArm()) {
413 lastDshotBeaconCommandTimeUs
= currentTimeUs
;
414 dshotCommandWrite(ALL_MOTORS
, getMotorCount(), beeperConfig()->dshotBeaconTone
, false);
419 if (currentBeeperEntry
->sequence
[beeperPos
] != 0) {
420 if (!(beeperConfigMutable()->beeper_off_flags
& BEEPER_GET_FLAG(currentBeeperEntry
->mode
))) {
427 // if this was arming beep then mark time (for blackbox)
430 && (currentBeeperEntry
->mode
== BEEPER_ARMING
|| currentBeeperEntry
->mode
== BEEPER_ARMING_GPS_FIX
431 || currentBeeperEntry
->mode
== BEEPER_ARMING_GPS_NO_FIX
)) {
432 armingBeepTimeMicros
= micros();
436 if (currentBeeperEntry
->sequence
[beeperPos
] != 0) {
445 beeperProcessCommand(currentTimeUs
);
449 * Calculates array position when next to change beeper state is due.
451 static void beeperProcessCommand(timeUs_t currentTimeUs
)
453 if (currentBeeperEntry
->sequence
[beeperPos
] == BEEPER_COMMAND_REPEAT
) {
455 } else if (currentBeeperEntry
->sequence
[beeperPos
] == BEEPER_COMMAND_STOP
) {
458 // Otherwise advance the sequence and calculate next toggle time
459 beeperNextToggleTime
= currentTimeUs
+ 1000 * 10 * currentBeeperEntry
->sequence
[beeperPos
];
465 * Returns the time that the last arming beep occurred (in system-uptime
466 * microseconds). This is fetched and logged by blackbox.
468 uint32_t getArmingBeepTimeMicros(void)
470 return armingBeepTimeMicros
;
474 * Returns the 'beeperMode_e' value for the given beeper-table index,
475 * or BEEPER_SILENCE if none.
477 beeperMode_e
beeperModeForTableIndex(int idx
)
479 return (idx
>= 0 && idx
< (int)BEEPER_TABLE_ENTRY_COUNT
) ? beeperTable
[idx
].mode
: BEEPER_SILENCE
;
483 * Returns the binary mask for the 'beeperMode_e' value corresponding to a given
484 * beeper-table index, or 0 if the beeperMode is BEEPER_SILENCE.
486 uint32_t beeperModeMaskForTableIndex(int idx
)
488 beeperMode_e beeperMode
= beeperModeForTableIndex(idx
);
489 if (beeperMode
== BEEPER_SILENCE
)
491 return BEEPER_GET_FLAG(beeperMode
);
495 * Returns the name for the given beeper-table index, or NULL if none.
497 const char *beeperNameForTableIndex(int idx
)
503 return (idx
>= 0 && idx
< (int)BEEPER_TABLE_ENTRY_COUNT
) ? beeperTable
[idx
].name
: NULL
;
508 * Returns the number of entries in the beeper-sounds table.
510 int beeperTableEntryCount(void)
512 return (int)BEEPER_TABLE_ENTRY_COUNT
;
516 * Returns true if the beeper is on, false otherwise
518 bool isBeeperOn(void)
525 // Stub out beeper functions if #BEEPER not defined
526 void beeper(beeperMode_e mode
) {UNUSED(mode
);}
527 void beeperSilence(void) {}
528 void beeperConfirmationBeeps(uint8_t beepCount
) {UNUSED(beepCount
);}
529 void beeperWarningBeeps(uint8_t beepCount
) {UNUSED(beepCount
);}
530 void beeperUpdate(timeUs_t currentTimeUs
) {UNUSED(currentTimeUs
);}
531 uint32_t getArmingBeepTimeMicros(void) {return 0;}
532 beeperMode_e
beeperModeForTableIndex(int idx
) {UNUSED(idx
); return BEEPER_SILENCE
;}
533 uint32_t beeperModeMaskForTableIndex(int idx
) {UNUSED(idx
); return 0;}
534 const char *beeperNameForTableIndex(int idx
) {UNUSED(idx
); return NULL
;}
535 int beeperTableEntryCount(void) {return 0;}
536 bool isBeeperOn(void) {return false;}
541 timeUs_t
getLastDshotBeaconCommandTimeUs(void)
543 return lastDshotBeaconCommandTimeUs
;