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];
31 if (tunepos
>= _numTones
|| (_tune
[tunepos
][0] == 0 && _tune
[tunepos
][1] == 0))
33 noTone(GPIO_PIN_BUZZER
);
34 pinMode(GPIO_PIN_BUZZER
, INPUT
);
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
);
46 tone(GPIO_PIN_BUZZER
, _tune
[tunepos
][0], _tune
[tunepos
][1]);
49 return _tune
[tunepos
-1][1];
52 static int startTune(const uint16_t tune
[][2], int numTones
)
56 pinMode(GPIO_PIN_BUZZER
, OUTPUT
);
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
;
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
;
93 if (GPIO_PIN_BUZZER
== UNDEF_PIN
|| firmwareOptions
.buzzer_mode
== buzzerQuiet
)
95 return DURATION_NEVER
;
97 static connectionState_e lastConnectionState
= MODE_STATES
;
100 if (connectionState
!= lastConnectionState
)
102 DBGVLN(">> starting %d -> %d", lastConnectionState
, connectionState
);
103 lastConnectionState
= connectionState
;
104 return updateBuzzer();
106 lastConnectionState
= connectionState
;
107 return DURATION_NEVER
;
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;
117 DBGVLN(">> ignored %d -> %d", lastConnectionState
, connectionState
);
119 return DURATION_IGNORE
;
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();
145 device_t Buzzer_device
= {
146 .initialize
= initializeBuzzer
,