diff --git a/sketch/obd2_gauge/meter.h b/sketch/obd2_gauge/meter.h index 1373466..9379c7e 100644 --- a/sketch/obd2_gauge/meter.h +++ b/sketch/obd2_gauge/meter.h @@ -1365,6 +1365,53 @@ void updateMeter(uint8_t pidNo, String response) { // update parameter on screen } /*-------------------------------*/ +// Non-blocking alarm sound generator: classic Casio double-chirp [bip-bip] every 1 second +uint32_t lastAlarmSoundCycle = 0; +uint8_t alarmSoundStep = 0; +uint32_t alarmSoundStepTime = 0; + +void handleAlarmSound(bool anyAlarmActive) { + if (!anyAlarmActive || buzzerVolumeLevel == 0) { + if (alarmSoundStep != 0) { + ledcWrite(buzzerChannel, 0); + alarmSoundStep = 0; + } + return; + } + + uint32_t now = millis(); + uint16_t duty = VOL_DUTY_TABLE[buzzerVolumeLevel > 5 ? 3 : buzzerVolumeLevel]; + + // Pattern: [25ms chirp] -> 35ms pause -> [25ms chirp] -> pause until 1000ms + if (alarmSoundStep == 0) { + if (now - lastAlarmSoundCycle >= 1000) { + lastAlarmSoundCycle = now; + alarmSoundStep = 1; + alarmSoundStepTime = now; + ledcSetup(buzzerChannel, 4096, 10); + ledcWrite(buzzerChannel, duty); + } + } else if (alarmSoundStep == 1) { // 25ms first chirp + if (now - alarmSoundStepTime >= 25) { + ledcWrite(buzzerChannel, 0); + alarmSoundStep = 2; + alarmSoundStepTime = now; + } + } else if (alarmSoundStep == 2) { // 35ms gap + if (now - alarmSoundStepTime >= 35) { + ledcSetup(buzzerChannel, 4096, 10); + ledcWrite(buzzerChannel, duty); + alarmSoundStep = 3; + alarmSoundStepTime = now; + } + } else if (alarmSoundStep == 3) { // 25ms second chirp + if (now - alarmSoundStepTime >= 25) { + ledcWrite(buzzerChannel, 0); + alarmSoundStep = 0; + } + } +} + // Non-blocking handler to blink/flash arcs and numerical values on alarm (when in TFT_RED) void handleArcAlarmFlash() { uint32_t now = millis(); @@ -1375,6 +1422,8 @@ void handleArcAlarmFlash() { toggleTick = true; } + bool anyInRedAlarm = false; + if (fullScreenCell >= 0 && fullScreenCell < 4) { uint8_t cell = fullScreenCell; if (last_valid_data[cell] != -999) { @@ -1383,6 +1432,7 @@ void handleArcAlarmFlash() { float data = last_valid_data[cell]; uint16_t col = getMeterColor(pidIdx, data, warn); bool inRed = (col == TFT_RED); + if (inRed) anyInRedAlarm = true; bool digit = pidConfig[pidIdx][7].toInt(); String numStr = String(data, digit); @@ -1466,6 +1516,7 @@ void handleArcAlarmFlash() { float data = last_valid_data[cell]; uint16_t col = getMeterColor(pidIdx, data, warn); bool inRed = (col == TFT_RED); + if (inRed) anyInRedAlarm = true; bool digit = pidConfig[pidIdx][7].toInt(); String numStr = String(data, digit); int x = cell4_x[cell]; @@ -1544,6 +1595,7 @@ void handleArcAlarmFlash() { } } } + handleAlarmSound(anyInRedAlarm); } // Target and current brightness variables for smooth fade diff --git a/sketch/obd2_gauge/obd2_gauge.ino b/sketch/obd2_gauge/obd2_gauge.ino index f3aad5b..8c4add2 100644 --- a/sketch/obd2_gauge/obd2_gauge.ino +++ b/sketch/obd2_gauge/obd2_gauge.ino @@ -94,7 +94,7 @@ String warningValue[4] = { "106", "5000", "15.0", "130" }; bool alarmEnabled[4] = { true, true, true, true }; // Buzzer volume control table (0: Silencio, 1: Ultra Bajo, 2: Muy Bajo, 3: Bajo/Casio, 4: Medio, 5: Alto) -const uint16_t VOL_DUTY_TABLE[6] = { 0, 5, 18, 50, 140, 380 }; +const uint16_t VOL_DUTY_TABLE[6] = { 0, 2, 8, 25, 80, 250 }; const char* const VOL_NAME_TABLE[6] = { "SILENCIO", "ULTRA BAJO", "MUY BAJO", "BAJO (CASIO)", "MEDIO", "ALTO" }; uint8_t buzzerVolumeLevel = 3; // Default 3: Bajo (Casio suave)