forked from surfdado/bldc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuzzer.c
81 lines (69 loc) · 1.71 KB
/
buzzer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "buzzer.h"
#ifdef HAS_EXT_BUZZER
#ifndef EXT_BUZZER_ON
#error Missing definition of EXT_BUZZER_ON despite HAS_EXT_BUZZER being defined
#endif
#ifndef EXT_BUZZER_OFF
#error Missing definition of EXT_BUZZER_OFF despite HAS_EXT_BUZZER being defined
#endif
// TODO: Make this configurable from the app
#define ALERT_MIN_BEEP_MS 1200
#define BEEP_SHORT 0
#define BEEP_LONG 1
static int alert_beep_num_left = 0;
static systime_t alert_beep_time;
static unsigned int alert_beep_duration = BEEP_SHORT;
static bool is_enabled = true;
void buzzer_enable(bool enable) {
is_enabled = enable;
if (!enable) {
EXT_BUZZER_OFF();
}
}
bool is_buzzer_enabled() {
return is_enabled;
}
void update_beep_alert(void)
{
if (!is_enabled)
return;
if (alert_beep_num_left > 0) {
if (chVTGetSystemTimeX() - alert_beep_time > alert_beep_duration) {
alert_beep_time = chVTGetSystemTimeX();
alert_beep_num_left--;
if (alert_beep_num_left & 0x1)
EXT_BUZZER_ON();
else
EXT_BUZZER_OFF();
}
}
}
void beep_alert(int num_beeps, bool longbeep)
{
if (!is_enabled)
return;
if (alert_beep_num_left == 0) {
alert_beep_num_left = num_beeps * 2;
alert_beep_time = chVTGetSystemTimeX();
alert_beep_duration = longbeep ? 4 * ALERT_MIN_BEEP_MS : ALERT_MIN_BEEP_MS;
EXT_BUZZER_ON();
}
}
void beep_off(bool force)
{
// don't mess with the buzzer if we're in the process of doing a multi-beep
if (force || (alert_beep_num_left == 0))
EXT_BUZZER_OFF();
}
void beep_on(bool force)
{
if (!is_enabled)
return;
// don't mess with the buzzer if we're in the process of doing a multi-beep
if (force || (alert_beep_num_left == 0))
EXT_BUZZER_ON();
}
#else
#define update_beep_alert(void) {}
#define beep_alert(int, bool) {}
#endif