#include "pico/stdlib.h"
#include "hardware/gpio.h"
#define DEBOUNCE_DELAY 100000 // Microseconds (adjust as needed)
#define MIN_SEQUENCE_LENGTH 2
#define MAX_SEQUENCE_LENGTH 4
// Simulating hardware registers for buttons and LEDs
int btn_pin[4] = {4, 5, 6, 7}; // Replace these with actual GPIO pin numbers for buttons
int led_pin[6] = {0, 1, 2, 3, 26, 27}; // Replace these with actual GPIO pin numbers for LEDs
int btn_state[4] = {0}; // 0: released, 1: pressed
int led_state[6] = {0}; // 0: OFF, 1: ON
int lock_sequence[MAX_SEQUENCE_LENGTH] = {2, 3, 1, 3}; // Example correct button sequence for unlocking
int admin_reset_sequence[MIN_SEQUENCE_LENGTH] = {2, 3}; // Example admin sequence for resetting
int correct_unlock_sequence_flag = 0; // Flag to check if the correct unlock sequence is entered
int correct_admin_sequence_flag = 0; // Flag to check if the correct admin sequence is entered
void setLedState(int led, int state) {
// Simulate setting the LED state
printf("Set LED_%d state to %s\n", led, (state == 1) ? "HIGH" : "LOW");
led_state[led] = state;
}
void handleButtonPress(int btn) {
if (btn_state[btn] == 0) {
btn_state[btn] = 1; // Set button state to pressed
setLedState(btn, 1); // Set the corresponding LED to HIGH
}
}
void handleButtonRelease(int btn) {
if (btn_state[btn] == 1) {
btn_state[btn] = 0; // Set button state to released
setLedState(btn, 0); // Set the corresponding LED to LOW
}
}
void simulateButtonPress(int btn) {
// Simulate button press (change the state and wait for debounce)
handleButtonPress(btn);
usleep(DEBOUNCE_DELAY);
}
void simulateButtonRelease(int btn) {
// Simulate button release (change the state and wait for debounce)
handleButtonRelease(btn);
usleep(DEBOUNCE_DELAY);
}
int verifyButtonSequence(int sequence_index, int* sequence, int* correct_sequence_flag) {
if (sequence_index >= MIN_SEQUENCE_LENGTH && sequence_index <= MAX_SEQUENCE_LENGTH) {
if (memcmp(sequence, lock_sequence, sequence_index * sizeof(int)) == 0) {
*correct_sequence_flag = 1;
setLedState(4, 1); // Green LED ON
printf("Locker unlocked - Green LED ON\n");
return 1; // Return 1 to indicate the correct sequence
} else if (memcmp(sequence, admin_reset_sequence, sequence_index * sizeof(int)) == 0) {
*correct_sequence_flag = 1;
setLedState(4, 0); // Green LED OFF
setLedState(5, 1); // Red LED ON
printf("Admin sequence detected - Resetting - Red LED ON\n");
return 2; // Return 2 to indicate the admin sequence
}
}
setLedState(5, 1); // Red LED ON
printf("Incorrect sequence - Alarm activated - Red LED ON\n");
return 0; // Return 0 to indicate an incorrect sequence
}
int main() {
int sequence[MAX_SEQUENCE_LENGTH];
int sequence_index = 0;
// Simulating button presses and releases (replace these with actual button events in your application)
simulateButtonPress(0); // Simulate Btn_0 press
simulateButtonRelease(0); // Simulate Btn_0 release
simulateButtonPress(1); // Simulate Btn_1 press
simulateButtonRelease(1); // Simulate Btn_1 release
simulateButtonPress(2); // Simulate Btn_2 press
simulateButtonRelease(2); // Simulate Btn_2 release
simulateButtonPress(3); // Simulate Btn_3 press
simulateButtonRelease(3); // Simulate Btn_3 release
// Verify the button sequence
int result = verifyButtonSequence(sequence_index, sequence, &correct_unlock_sequence_flag);
// Handle the result
if (result == 1) {
// Handle correct unlock sequence
} else if (result == 2) {
// Handle admin sequence (reset)
}
return 0;
}
/*
bool is_locked = true;
bool alarm_triggered = false;
// Function prototypes
void initialize_gpio();
void update_leds();
bool is_button_pressed(uint pin);
void check_sequence(uint8_t sequence);
void button_pressed(uint pin);
// Function to initialize GPIO pins
void initialize_gpio() {
// Set initial LED states
gpio_put(LED_0, false);
gpio_put(LED_1, false);
gpio_put(LED_2, false);
gpio_put(LED_3, false);
gpio_put(ALARM_LED, false);
gpio_put(UNLOCKED_LED, false);
// Initialize buttons
gpio_init(E_BTN1);
gpio_init(S_BTN2);
gpio_init(W_BTN3);
gpio_init(N_BTN4);
// Enable pull-up for buttons
gpio_pull_up(E_BTN1);
gpio_pull_up(S_BTN2);
gpio_pull_up(W_BTN3);
gpio_pull_up(N_BTN4);
// Set directions for buttons
gpio_set_dir(E_BTN1, GPIO_IN);
gpio_set_dir(S_BTN2, GPIO_IN);
gpio_set_dir(W_BTN3, GPIO_IN);
gpio_set_dir(N_BTN4, GPIO_IN);
// Initialize LEDs
gpio_init(LED_0);
gpio_init(LED_1);
gpio_init(LED_2);
gpio_init(LED_3);
gpio_init(ALARM_LED);
gpio_init(UNLOCKED_LED);
// Set directions for LEDs
gpio_set_dir(LED_0, GPIO_OUT);
gpio_set_dir(LED_1, GPIO_OUT);
gpio_set_dir(LED_2, GPIO_OUT);
gpio_set_dir(LED_3, GPIO_OUT);
gpio_set_dir(ALARM_LED, GPIO_OUT);
gpio_set_dir(UNLOCKED_LED, GPIO_OUT);
}
// Function to update LED states based on the lock status
void update_leds() {
gpio_put(UNLOCKED_LED, !is_locked);
}
// Function to check if a button is pressed (with debouncing)
bool is_button_pressed(uint pin) {
sleep_ms(15); // Adjust debounce delay as needed
return gpio_get(pin) == 0;
}
// Function to handle button press logic
void button_pressed(uint pin) {
switch (pin) {
case E_BTN1:
printf("Button E_BTN1 pressed\n");
gpio_put(LED_0, true);
check_sequence(E_BTN1);
gpio_put(LED_0, false);
break;
case S_BTN2:
printf("Button S_BTN2 pressed\n");
gpio_put(LED_1, true);
check_sequence(S_BTN2);
gpio_put(LED_1, false);
break;
case W_BTN3:
printf("Button W_BTN3 pressed\n");
gpio_put(LED_2, true);
check_sequence(W_BTN3);
gpio_put(LED_2, false);
break;
case N_BTN4:
printf("Button N_BTN4 pressed\n");
gpio_put(LED_3, true);
check_sequence(N_BTN4);
gpio_put(LED_3, false);
break;
default:
break;
}
}
// Function to check the input sequence and take appropriate actions
void check_sequence(uint8_t sequence) {
static uint8_t input_sequence = 0;
if (sequence == UNLOCK_SEQUENCE) {
if (!is_locked) {
printf("Lock closed.\n");
is_locked = true;
update_leds();
} else {
printf("UNLOCKED! Lock open.\n");
is_locked = false;
update_leds();
}
input_sequence = 0
return;
}
input_sequence = (input_sequence << 2) | sequence;
if (input_sequence == ADMIN_SEQUENCE && alarm_triggered) {
printf("Alarm deactivated. Resetting...\n");
alarm_triggered = false;
gpio_put(ALARM_LED, false); // Turn off alarm LED
input_sequence = 0; // Reset input sequence
return;
}
if (input_sequence != UNLOCK_SEQUENCE && input_sequence != ADMIN_SEQUENCE) {
// If four buttons pressed and not an unlock or admin sequence, trigger alarm
if (input_sequence != 0b1111) {
alarm_triggered = true;
gpio_put(ALARM_LED, true); // Turn on alarm LED
printf("ALARM triggered!\n");
// Your alarm signal logic here
input_sequence = 0; // Reset input sequence
}
}
}
int main() {
stdio_init_all();
initialize_gpio();
// Main loop
while (1) {
if (is_locked || alarm_triggered) {
if (is_button_pressed(E_BTN1)) {
button_pressed(E_BTN1);
}
if (is_button_pressed(S_BTN2)) {
button_pressed(S_BTN2);
}
if (is_button_pressed(W_BTN3)) {
button_pressed(W_BTN3);
}
if (is_button_pressed(N_BTN4)) {
button_pressed(N_BTN4);
}
} else {
printf("Press any key to close the lock.\n");
// Read button inputs here and set is_locked to true when any button is pressed
// For example: if (any_button_pressed) is_locked = true;
}
sleep_ms(100); // Add a delay to reduce CPU usage
}
return 0;
}