diff --git a/src/src/main.cpp b/src/src/main.cpp index 0835fe4..212a766 100644 --- a/src/src/main.cpp +++ b/src/src/main.cpp @@ -27,6 +27,9 @@ int password = 0; // 0-9999 int passwordValue = 0; String savedPassword = ""; +// stav zámku +bool isOpen = false; + // enkodér int lastCLK = HIGH; @@ -36,8 +39,8 @@ unsigned long lastEncoderMove = 0; bool masked = false; // přeformátování kódu na 4 cifry -String formatCode(int value) { - String s = String(value); +String formatCode(const int value) { + auto s = String(value); while (s.length() < 4) s = "0" + s; return s; } @@ -48,8 +51,8 @@ void CloseLock() { servo.write(CLOSED_POS); } // chování bzučáku void OpenSound() { - int melody[] = { 262, 330, 392, 523 }; // C4, E4, G4, C5 - int durations[] = { 150, 150, 150, 300 }; + const int melody[] = { 262, 330, 392, 523 }; // C4, E4, G4, C5 + const int durations[] = { 150, 150, 150, 300 }; for (int i = 0; i < 4; i++) { tone(BUZZER_PIN, melody[i], durations[i]); @@ -59,8 +62,8 @@ void OpenSound() { } void CloseSound() { - tone(BUZZER_PIN, 262, 150); - delay(500); + tone(BUZZER_PIN, 200, 200); delay(230); + tone(BUZZER_PIN, 150, 400); delay(430); noTone(BUZZER_PIN); } @@ -110,15 +113,28 @@ void ButtonPressed() { } } -// setup hesla -void showSetupScreen(bool showValue) { +// zobrazení zadávací obrazovky +void showEntryScreen(const bool showValue) { + lcd.setCursor(0, 0); + lcd.printstr("Zadejte heslo: "); + lcd.setCursor(0, 1); + if (showValue) { + String code = formatCode(password); + const String display = code[0] + String(" ") + code[1] + String(" ") + code[2] + String(" ") + code[3] + String(" "); + lcd.printstr(display.c_str()); + } else { + lcd.printstr("* * * * "); + } +} + +// zobrazení setup obrazovky +void showSetupScreen(const bool showValue) { lcd.setCursor(0, 0); lcd.printstr("Nastavte heslo: "); lcd.setCursor(0, 1); if (showValue) { String code = formatCode(passwordValue); - // mezery okolo pro vizuální oddělení číslic - String display = code[0] + String(" ") + code[1] + String(" ") + code[2] + String(" ") + code[3] + String(" "); + const String display = code[0] + String(" ") + code[1] + String(" ") + code[2] + String(" ") + code[3] + String(" "); lcd.printstr(display.c_str()); } else { lcd.printstr("* * * * "); @@ -137,10 +153,9 @@ void runPasswordSetup() { while (savedPassword == "") { // čtení enkodéru - int currentCLK = digitalRead(ENCODER_CLK_PIN); + const int currentCLK = digitalRead(ENCODER_CLK_PIN); if (currentCLK != lastCLK && currentCLK == LOW) { - int dt = digitalRead(ENCODER_DT_PIN); - if (dt != currentCLK) { + if (const int dt = digitalRead(ENCODER_DT_PIN); dt != currentCLK) { passwordValue = (passwordValue + 1) % 10000; } else { passwordValue = (passwordValue - 1 + 10000) % 10000; @@ -227,5 +242,72 @@ void setup() { } void loop() { + // čtení enkodéru – jen pokud je zamčeno + // (při odemčeném stavu enkodér nemá smysl) + if (!isOpen) { + const int currentCLK = digitalRead(ENCODER_CLK_PIN); + if (currentCLK != lastCLK && currentCLK == LOW) { + if (const int dt = digitalRead(ENCODER_DT_PIN); dt != currentCLK) { + password = (password + 1) % 10000; + } else { + password = (password - 1 + 10000) % 10000; + } + masked = false; + lastEncoderMove = millis(); + showEntryScreen(true); + } + lastCLK = currentCLK; -} \ No newline at end of file + // maskování po nečinnosti + if (!masked && millis() - lastEncoderMove >= MASK_DELAY_MS) { + masked = true; + showEntryScreen(false); + } + } + + // tlačítko + if (digitalRead(BUTTON_PIN) == HIGH) { + // debounce + while (digitalRead(BUTTON_PIN) == HIGH) {} + delay(50); + + if (!isOpen) { + // pokus o odemčení + if (formatCode(password) == savedPassword) { + isOpen = true; + OpenLock(); + OpenLED(); + OpenSound(); + + lcd.setCursor(0, 0); + lcd.printstr("Odemceno! "); + lcd.setCursor(0, 1); + lcd.printstr("Stiskni pro zamk"); + } else { + CloseSound(); + + lcd.setCursor(0, 0); + lcd.printstr("Spatne heslo! "); + lcd.setCursor(0, 1); + lcd.printstr(" "); + delay(1500); + + // návrat na zadávací obrazovku, hodnota zachována + masked = false; + lastEncoderMove = millis(); + showEntryScreen(true); + } + } else { + // zamčení + isOpen = false; + CloseLock(); + CloseLED(); + CloseSound(); + + password = 0; + masked = false; + lastEncoderMove = millis(); + showEntryScreen(true); + } + } +}