diff --git a/The Code b/The Code new file mode 100644 index 0000000000000000000000000000000000000000..8f0f8c233295ff5b494f79f03783c16b477c13ab --- /dev/null +++ b/The Code @@ -0,0 +1,201 @@ +#include "mbed.h" +using namespace std::chrono; + +//=====[Hardware Inputs]======================================================= + +DigitalIn enterButton(BUTTON1); +DigitalIn gasDetector(D2); +DigitalIn overTempDetector(D3); +DigitalIn aButton(D4); +DigitalIn bButton(D5); +DigitalIn cButton(D6); +DigitalIn dButton(D7); + +//=====[Outputs and Serial]==================================================== + +DigitalOut alarmLed(LED1); +DigitalOut incorrectCodeLed(LED3); +DigitalOut systemBlockedLed(LED2); +UnbufferedSerial pc(USBTX, USBRX, 115200); + +//=====[Timers and State Variables]============================================ + +Timer lockoutTimer; + +bool alarmState = false; +bool emergencyMode = false; +int incorrectAttempts = 0; +bool systemLocked = false; +bool blinkingLED2 = false; + +// For serial test simulation +bool sim_gas = false; +bool sim_temp = false; +bool sim_enter = false; +bool sim_a = false, sim_b = false, sim_c = false, sim_d = false; + +bool lastAlarmLed = false; +bool lastIncorrectLed = false; +bool lastSystemBlockedLed = false; + +void print(const char* msg) { + pc.write(msg, strlen(msg)); +} + +void printLEDStates() { + if (alarmLed != lastAlarmLed) { + print(alarmLed ? "LED1 ON (Alarm)\r\n" : "LED1 OFF (Alarm)\r\n"); + lastAlarmLed = alarmLed; + } + if (incorrectCodeLed != lastIncorrectLed) { + print(incorrectCodeLed ? "LED3 ON (Incorrect Code)\r\n" : "LED3 OFF (Incorrect Code)\r\n"); + lastIncorrectLed = incorrectCodeLed; + } + if (systemBlockedLed != lastSystemBlockedLed) { + print(systemBlockedLed ? "LED2 ON (Emergency)\r\n" : "LED2 OFF (Emergency)\r\n"); + lastSystemBlockedLed = systemBlockedLed; + } +} + +void lockSystem() { + systemLocked = true; + lockoutTimer.start(); + incorrectCodeLed = 0; + + print("!!! SYSTEM LOCKED FOR 60 SECONDS !!!\r\n"); + while (lockoutTimer.read() < 60) { + incorrectCodeLed = !incorrectCodeLed; + printLEDStates(); + ThisThread::sleep_for(500ms); + } + + lockoutTimer.stop(); + lockoutTimer.reset(); + incorrectAttempts = 0; + systemLocked = false; + incorrectCodeLed = 0; + print("System unlocked.\r\n"); +} + +void resetAlarm() { + alarmState = false; + emergencyMode = false; + blinkingLED2 = false; + systemBlockedLed = 0; + print("Alarm and emergency mode deactivated.\r\n"); +} + +void processSerialCommand(char* cmd) { + if (strcmp(cmd, "trigger gas") == 0) { + sim_gas = true; + print("Simulated gas triggered.\r\n"); + } else if (strcmp(cmd, "trigger temp") == 0) { + sim_temp = true; + print("Simulated temperature triggered.\r\n"); + } else if (strcmp(cmd, "clear") == 0) { + sim_gas = sim_temp = false; + print("Simulated sensors cleared.\r\n"); + } else if (strncmp(cmd, "enter code ", 11) == 0) { + sim_a = sim_b = sim_c = sim_d = false; + for (int i = 11; cmd[i]; ++i) { + switch (cmd[i]) { + case 'a': sim_a = true; break; + case 'b': sim_b = true; break; + case 'c': sim_c = true; break; + case 'd': sim_d = true; break; + } + } + sim_enter = true; + print("Simulated code entry received.\r\n"); + } else if (strcmp(cmd, "wrong") == 0) { + sim_enter = true; + sim_a = sim_b = sim_c = sim_d = false; + print("Simulated wrong code.\r\n"); + } else { + print("Unknown command.\r\n"); + } +} + +//=====[Main]================================================================== + +int main() { + // Set input pin modes + gasDetector.mode(PullDown); + overTempDetector.mode(PullDown); + aButton.mode(PullDown); + bButton.mode(PullDown); + cButton.mode(PullDown); + dButton.mode(PullDown); + + alarmLed = incorrectCodeLed = systemBlockedLed = 0; + print("System ready. Serial commands: trigger gas/temp, clear, enter code ab, wrong\r\n"); + + char cmd[32]; + int idx = 0; + + while (true) { + // Read serial commands + if (pc.readable()) { + char c; + pc.read(&c, 1); + if (c == '\r' || c == '\n') { + cmd[idx] = '\0'; + processSerialCommand(cmd); + idx = 0; + } else if (idx < sizeof(cmd) - 1) { + cmd[idx++] = c; + } + } + + if (systemLocked) { + lockSystem(); + continue; + } + + // Combine real and simulated inputs + bool gas = gasDetector.read() || sim_gas; + bool temp = overTempDetector.read() || sim_temp; + bool enter = enterButton.read() || sim_enter; + + bool a = aButton.read() || sim_a; + bool b = bButton.read() || sim_b; + bool c = cButton.read() || sim_c; + bool d = dButton.read() || sim_d; + + if (gas || temp) { + if (!alarmState) print("Alarm triggered.\r\n"); + alarmState = true; + } + + if (gas && temp) { + if (!emergencyMode) print("Emergency mode activated.\r\n"); + emergencyMode = true; + blinkingLED2 = true; + } + + if (blinkingLED2) { + systemBlockedLed = !systemBlockedLed; + printLEDStates(); + ThisThread::sleep_for(250ms); + } + + alarmLed = alarmState; + printLEDStates(); + + if (enter && !systemLocked) { + sim_enter = false; // Reset simulated press + + if (a && b && !c && !d) { + resetAlarm(); + } else { + incorrectCodeLed = 1; + incorrectAttempts++; + print("Incorrect code entered.\r\n"); + + if (incorrectAttempts >= 5) { + lockSystem(); + } + } + } + } +}