Silence unused-variable warning (#2872)
[ExpressLRS.git] / src / lib / BUZZER / devBuzzer.cpp
blob4468c44fc9517b23ba334bdc2f06e2b7dabaaec4
1 #include "targets.h"
2 #include "common.h"
3 #include "device.h"
5 #include "helpers.h"
6 #include "logging.h"
7 #include "options.h"
9 // Even though we aren't using anything this keeps the PIO dependency analyzer happy!
10 #include "POWERMGNT.h"
12 #if defined(GPIO_PIN_BUZZER)
14 static void initializeBuzzer()
16 pinMode(GPIO_PIN_BUZZER, OUTPUT);
19 static const uint16_t failedTune[][2] = {{480, 200},{400, 200}};
20 static const uint16_t crossfireTune[][2] = {{520, 150},{676, 300},{0,1000}}; // we have a dead-time to stop spamming
21 static const uint16_t noCrossfireTune[][2] = {{676, 300},{520, 150}};
23 static uint8_t tunepos = 0;
24 static bool callAfterComplete = false;
25 static bool startComplete = false;
26 static const uint16_t (*_tune)[2];
27 static int _numTones;
29 static int playTune()
31 if (tunepos >= _numTones || (_tune[tunepos][0] == 0 && _tune[tunepos][1] == 0))
33 noTone(GPIO_PIN_BUZZER);
34 pinMode(GPIO_PIN_BUZZER, INPUT);
35 tunepos = 0;
36 DBGVLN(">> end tune");
37 return DURATION_NEVER;
39 if (_tune[tunepos][0] == 0)
41 // we have dead-time so play no-tone and just set the timeout
42 noTone(GPIO_PIN_BUZZER);
44 else
46 tone(GPIO_PIN_BUZZER, _tune[tunepos][0], _tune[tunepos][1]);
48 tunepos++;
49 return _tune[tunepos-1][1];
52 static int startTune(const uint16_t tune[][2], int numTones)
54 _tune = tune;
55 _numTones = numTones;
56 pinMode(GPIO_PIN_BUZZER, OUTPUT);
57 tunepos = 0;
58 return playTune();
61 static int updateBuzzer()
63 if (connectionState == radioFailed && firmwareOptions.buzzer_mode > buzzerOne)
65 DBGVLN(">> start failed tune");
66 return startTune(failedTune, ARRAY_SIZE(failedTune));
68 else if (connectionState == noCrossfire && firmwareOptions.buzzer_mode > buzzerOne)
70 DBGVLN(">> start no-xfire tune");
71 return startTune(noCrossfireTune, ARRAY_SIZE(noCrossfireTune));
73 else if ((connectionState == connected || connectionState == disconnected))
75 DBGVLN(">> start conn/disconn tune");
76 return startTune(crossfireTune, ARRAY_SIZE(crossfireTune));
78 return DURATION_NEVER;
81 static int start()
83 if (GPIO_PIN_BUZZER != UNDEF_PIN && firmwareOptions.buzzer_mode == buzzerTune)
85 DBGVLN(">> start startup tune");
86 return startTune(firmwareOptions.buzzer_melody, ARRAY_SIZE(firmwareOptions.buzzer_melody));
88 return DURATION_NEVER;
91 static int event()
93 if (GPIO_PIN_BUZZER == UNDEF_PIN || firmwareOptions.buzzer_mode == buzzerQuiet)
95 return DURATION_NEVER;
97 static connectionState_e lastConnectionState = MODE_STATES;
98 if (tunepos == 0)
100 if (connectionState != lastConnectionState)
102 DBGVLN(">> starting %d -> %d", lastConnectionState, connectionState);
103 lastConnectionState = connectionState;
104 return updateBuzzer();
106 lastConnectionState = connectionState;
107 return DURATION_NEVER;
109 if(!startComplete)
111 // if we are currently playing the startup tune then set a flag so that we trigger an update after it completes
112 DBGVLN(">> call after");
113 callAfterComplete = true;
115 else
117 DBGVLN(">> ignored %d -> %d", lastConnectionState, connectionState);
119 return DURATION_IGNORE;
122 static int timeout()
124 if (GPIO_PIN_BUZZER == UNDEF_PIN || firmwareOptions.buzzer_mode == buzzerQuiet)
126 return DURATION_NEVER;
129 int duration = playTune();
130 DBGVLN(">> timeout %d", duration);
131 if (duration == DURATION_NEVER && !startComplete)
133 startComplete = true;
134 if (callAfterComplete)
136 // tune completed and the flag is set to start the next one
137 callAfterComplete = false;
138 DBGVLN(">> call after update");
139 duration = updateBuzzer();
142 return duration;
145 device_t Buzzer_device = {
146 .initialize = initializeBuzzer,
147 .start = start,
148 .event = event,
149 .timeout = timeout
152 #endif