application: make door open/close sequence more generic/clean

This commit is contained in:
King Kévin 2021-04-23 15:21:11 +02:00
parent 2758b5d4c4
commit 80d5a2b704
1 changed files with 18 additions and 10 deletions

View File

@ -177,9 +177,6 @@ static uint32_t last_finished = 0;
/** folder number for kanguru tracks */
#define KANGURU_FOLDER 7
/** RTC timestamps when the door has previously been closed (even index, oldest first) or opened (odd index, oldest first) */
static uint32_t door_timestamps[4] = {0};
size_t putc(char c)
{
size_t length = 0; // number of characters printed
@ -781,6 +778,9 @@ void main(void)
exti_set_trigger(GPIO_EXTI(DOOR_PIN), EXTI_TRIGGER_BOTH); // trigger on change
exti_enable_request(GPIO_EXTI(DOOR_PIN)); // enable external interrupt
nvic_enable_irq(GPIO_NVIC_EXTI_IRQ(DOOR_PIN)); // enable interrupt
uint32_t door_sequence[4] = {0}; // duration of past door states, in RTC ticks, oldest first, updated every time the door is opened/closed
uint32_t door_timestamp = 0; // RTC timestamps when the door state has changed
puts("OK\n");
// setup terminal
@ -828,7 +828,7 @@ void main(void)
sleep_ms(100); // wait a bit to de-noise before we check the door lock state
const bool closed = (0 == gpio_get(GPIO_PORT(DOOR_PIN), GPIO_PIN(DOOR_PIN))); // get door lock state
door_flag = false; // clear flag
const uint32_t door_timestamp = rtc_get_counter_val(); // save when the door has been open/closed
const uint32_t change_timestamp = rtc_get_counter_val(); // save when the door has been open/closed
action = true; // action has been performed
if (closed && 0 == timer_door_closed) { // door has been closed
puts("door closed\n");
@ -836,12 +836,18 @@ void main(void)
leds_sign(true); // show on the sign that the toilet is occupied
led_tm1637_time(0, 0); // start showing time on display
led_tm1637_on(); // ensure the display is on
// update door change sequence
for (uint8_t i = 0; i < LENGTH(door_sequence) - 1; i++) {
door_sequence[i] = door_sequence[i + 1]; // shift previous changes
}
door_sequence[LENGTH(door_sequence) - 1] = change_timestamp - door_timestamp; // not underflow safe
door_timestamp = change_timestamp; // remember the change
// depending on the open/close sequence, enter secret mode
if ((door_timestamp - door_timestamps[3]) < (2 * RTC_TICKS_SECOND) && (door_timestamps[3] - door_timestamps[2]) < (2 * RTC_TICKS_SECOND) && (door_timestamps[2] - door_timestamps[1]) < (2 * RTC_TICKS_SECOND) && (door_timestamps[1] - door_timestamps[0]) < (2 * RTC_TICKS_SECOND)) { // sequence detected CLOSED->OPENED->CLOSED->OPEN->CLOSED, each within 2 seconds, then enter secret mode 2
if (door_sequence[LENGTH(door_sequence) - 1] < (2 * RTC_TICKS_SECOND) && door_sequence[LENGTH(door_sequence) - 2] < (2 * RTC_TICKS_SECOND) && door_sequence[LENGTH(door_sequence) - 3] < (2 * RTC_TICKS_SECOND) && door_sequence[LENGTH(door_sequence) - 4] < (2 * RTC_TICKS_SECOND)) { // sequence detected CLOSED->OPENED->CLOSED->OPEN->CLOSED, each within 2 seconds, then enter secret mode 2
puts("entering secret kanguru mode\n");
mp3_command(MP3_CMD_PLAY_FOLDER_FILE, (KANGURU_FOLDER << 8) + (rtc_get_counter_val() % KANGURU_TRACKS) + 1); // play random kanguru sketch track
playing_state = PLAYING_STATE_KANGURU; // remember we are playing kanguru sketches
} else if ((door_timestamp - door_timestamps[3]) < (2 * RTC_TICKS_SECOND) && (door_timestamps[3] - door_timestamps[2]) < (2 * RTC_TICKS_SECOND)) { // sequence detected CLOSED->OPENED->CLOSED, each within 2 seconds, then enter secret mode 1
} else if (door_sequence[LENGTH(door_sequence) - 1] < (2 * RTC_TICKS_SECOND) && door_sequence[LENGTH(door_sequence) - 2] < (2 * RTC_TICKS_SECOND)) { // sequence detected CLOSED->OPENED->CLOSED, each within 2 seconds, then enter secret mode 1
puts("entering secret techno mode\n");
mp3_command(MP3_CMD_PLAY_FOLDER_FILE, (TECHNO_FOLDER << 8) + (rtc_get_counter_val() % TECHNO_TRACKS) + 1); // play random techno music track
playing_state = PLAYING_STATE_TECHNO; // remember we are playing techno music
@ -849,8 +855,6 @@ void main(void)
mp3_command(MP3_CMD_PLAY_FOLDER_FILE, (WELCOME_FOLDER << 8) + (rtc_get_counter_val() % WELCOME_TRACKS) + 1); // play random welcome track
playing_state = PLAYING_STATE_INTRO; // remember we are playing the welcome message
}
door_timestamps[0] = door_timestamps[2]; // backup previous closed time stamp
door_timestamps[2] = door_timestamp; // backup closed time stamp
} else if (!closed && timer_door_closed) { // door has been opened
puts("door opened\n");
timer_door_closed = 0; // remember door is now open
@ -858,8 +862,12 @@ void main(void)
led_tm1637_off(); // stop showing time
mp3_command(MP3_CMD_PLAY_FOLDER_FILE, (EXIT_FOLDER << 8) + (rtc_get_counter_val() % EXIT_TRACKS) + 1); // play random exit message track
playing_state = PLAYING_STATE_EXIT; // we are playing the exit track
door_timestamps[1] = door_timestamps[3]; // backup previous opened time stamp
door_timestamps[3] = door_timestamp; // backup opened time stamp
// update door change sequence
for (uint8_t i = 0; i < LENGTH(door_sequence) - 1; i++) {
door_sequence[i] = door_sequence[i + 1]; // shift previous changes
}
door_sequence[LENGTH(door_sequence) - 1] = change_timestamp - door_timestamp; // not underflow safe
door_timestamp = change_timestamp; // remember the change
}
}
if (mp3_rx_flag) { // data from MP3 player received