CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Tools/AP_Periph/buzzer.cpp
Views: 1798
1
#include "AP_Periph.h"
2
3
#if defined(HAL_PERIPH_ENABLE_NOTIFY) || defined(HAL_PERIPH_ENABLE_BUZZER_WITHOUT_NOTIFY)
4
5
/*
6
buzzer support
7
*/
8
9
#include <dronecan_msgs.h>
10
11
extern const AP_HAL::HAL &hal;
12
13
static uint32_t buzzer_start_ms;
14
static uint32_t buzzer_len_ms;
15
16
/*
17
handle BeepCommand
18
*/
19
void AP_Periph_FW::handle_beep_command(CanardInstance* canard_instance, CanardRxTransfer* transfer)
20
{
21
uavcan_equipment_indication_BeepCommand req;
22
if (uavcan_equipment_indication_BeepCommand_decode(transfer, &req)) {
23
return;
24
}
25
static bool initialised;
26
if (!initialised) {
27
initialised = true;
28
hal.rcout->init();
29
// just one buzzer type supported:
30
hal.util->toneAlarm_init(uint8_t(AP_Notify::BuzzerType::BUILTIN));
31
}
32
buzzer_start_ms = AP_HAL::millis();
33
buzzer_len_ms = req.duration*1000;
34
#ifdef HAL_PERIPH_ENABLE_BUZZER_WITHOUT_NOTIFY
35
float volume = constrain_float(periph.g.buzz_volume*0.01f, 0, 1);
36
#elif defined(HAL_PERIPH_ENABLE_NOTIFY)
37
float volume = constrain_float(periph.notify.get_buzz_volume()*0.01f, 0, 1);
38
#endif
39
hal.util->toneAlarm_set_buzzer_tone(req.frequency, volume, uint32_t(req.duration*1000));
40
}
41
42
/*
43
update buzzer
44
*/
45
void AP_Periph_FW::can_buzzer_update(void)
46
{
47
if (buzzer_start_ms != 0) {
48
uint32_t now = AP_HAL::millis();
49
if (now - buzzer_start_ms > buzzer_len_ms) {
50
hal.util->toneAlarm_set_buzzer_tone(0, 0, 0);
51
buzzer_start_ms = 0;
52
}
53
}
54
}
55
56
#endif // (HAL_PERIPH_ENABLE_BUZZER_WITHOUT_NOTIFY) || (HAL_PERIPH_ENABLE_NOTIFY)
57
58