-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeaker_control.c
89 lines (61 loc) · 2.09 KB
/
speaker_control.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
82
83
84
85
86
87
88
89
/***
* Noptel LRF rangefinder sampler for the Flipper Zero
* Version: 2.0
*
* Speaker control
***/
/*** Includes ***/
#include <furi_hal.h>
#include "common.h"
/*** Routines ***/
/** Speaker control timer callback **/
static void speaker_control_timer_callback(void *ctx) {
SpeakerControl *spc = (SpeakerControl *)ctx;
/* Should we start beeping? */
if(spc->play_beep) {
/* Start the speaker beeping if it wasn't beeping already */
if(!spc->beep_playing && furi_hal_speaker_acquire(500)) {
furi_hal_speaker_start(beep_frequency, 1);
spc->beep_playing = true;
}
spc->play_beep = false;
/* Reschedule ourselves in one beep duration */
furi_timer_start(spc->speaker_control_timer, spc->beep_duration);
}
/* If the speaker is beeping, stop it and don't reschedule ourselves */
else
if(spc->beep_playing && furi_hal_speaker_is_mine()) {
furi_hal_speaker_stop();
furi_hal_speaker_release();
spc->beep_playing = false;
}
}
/** Setup the speaker control **/
void set_speaker_control(SpeakerControl *spc) {
/* No beep is currently playing */
spc->play_beep = false;
spc->beep_playing = false;
spc->beep_duration = 0;
/* Setup the timer to control the speaker */
spc->speaker_control_timer = furi_timer_alloc(speaker_control_timer_callback,
FuriTimerTypeOnce, spc);
}
/** Release the speaker control **/
void release_speaker_control(SpeakerControl *spc) {
/* Make sure the speaker has stopped beeping */
spc->play_beep = false;
if(spc->beep_duration)
furi_delay_ms(spc->beep_duration * 2);
/* Stop and free the speaker control timer callback */
furi_timer_stop(spc->speaker_control_timer);
furi_timer_free(spc->speaker_control_timer);
}
/** Start a beep **/
void start_beep(SpeakerControl *spc, uint16_t duration) {
/* Store how long this beep should last */
spc->beep_duration = duration;
/* Raise the flag for the speaker control timer callback to start a beep */
spc->play_beep = true;
/* Schedule the speaker control timer control to run asap */
furi_timer_start(spc->speaker_control_timer, 1);
}