-
Notifications
You must be signed in to change notification settings - Fork 15
/
CO2_Gadget_Buzzer.h
64 lines (56 loc) · 2.13 KB
/
CO2_Gadget_Buzzer.h
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
#ifndef CO2_Gadget_Buzzer_h
#define CO2_Gadget_Buzzer_h
/*****************************************************************************************************/
/********* *********/
/********* SETUP BUZZER *********/
/********* *********/
/*****************************************************************************************************/
// #define BUZZER_DEBUG
bool belowRedRange = true;
void beepBuzzer() {
static uint16_t numberOfBeepsLeft = 2;
static uint64_t timeNextBeep = 0;
if (millis() > timeNextBeep) {
if (numberOfBeepsLeft == 0) {
if (timeBetweenBuzzerBeeps > 0) {
timeNextBeep = millis() + timeBetweenBuzzerBeeps * 1000;
} else {
belowRedRange = false;
}
numberOfBeepsLeft = 2;
buzzerBeeping = false;
} else {
#ifdef BUZZER_DEBUG
Serial.printf("[BUZZ] Beeps left: %d. Next beep in: %d sec. Beep duration: %d ms\n", numberOfBeepsLeft, timeBetweenBuzzerBeeps, durationBuzzerBeep);
#endif
tone(BUZZER_PIN, toneBuzzerBeep, durationBuzzerBeep);
timeNextBeep = millis() + durationBuzzerBeep + (durationBuzzerBeep * 1.3);
--numberOfBeepsLeft;
buzzerBeeping = true;
}
}
}
void buzzerLoop() {
#ifdef SUPPORT_BUZZER
if (timeBetweenBuzzerBeeps == -1 || inMenu) { // Inside Menu stop BEEPING
buzzerBeeping = false;
belowRedRange = true;
return;
}
if (co2 > co2RedRange && belowRedRange) {
shouldWakeUpDisplay = true;
beepBuzzer();
} else if (co2 < (co2RedRange - BUZZER_HYSTERESIS))
belowRedRange = true;
#endif
}
void initBuzzer() {
#ifdef SUPPORT_BUZZER
Serial.printf("-->[BUZZ] Initializing Buzzer on GPIO %d...\n", BUZZER_PIN);
pinMode(BUZZER_PIN, OUTPUT);
// LEDC initialization
ledcSetup(0, 5000, 8);
ledcAttachPin(BUZZER_PIN, 0);
#endif
}
#endif