From b6320b0ddfc3b45ac05048f9d1864bb6c63cf31e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Sun, 14 May 2017 18:35:07 +0200 Subject: [PATCH] add clap button --- main.c | 298 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 171 insertions(+), 127 deletions(-) diff --git a/main.c b/main.c index 2f1b81c..38c4128 100644 --- a/main.c +++ b/main.c @@ -53,6 +53,7 @@ volatile bool rtc_tick_flag = false; /**< flag set when RTC ticked */ volatile bool frame_flag = false; /**< flag set when a frame has passed */ volatile bool keep_alive_flag = false; /**< flag to restart shutdown counter on power switch activity */ +volatile bool clap_flag = false; /**< flag set on clap */ /** @} */ #define SQUARE_WAVE_PORT B /**< port connected to RTC DS1307 square wave output */ @@ -114,6 +115,15 @@ struct number_t numbers[] = { #define BUTTONS_READ_PIN2 6 #define BUTTONS_READ_PIN3 15 +#define CLAP_BUTTON_PORT B +#define CLAP_BUTTON_PIN 14 + +/** Morse code variables, to buzz scene and take */ +#define MORSE_DOT 1 /**< unit Morse code duration in frames */ +uint8_t morse[2*4*5*2] = {0}; /**< to encode 2 4-digit numbers (scene and take) into Morse code (5 sign+space) */ +uint8_t morse_i = 0; /**< index in Morse array */ +int16_t morse_delay = -1; /**< number of frames before beeping (-1=no need to beep yet, -2=beeping completed) */ + /** user input command */ static char command[32] = {0}; /** user input command index */ @@ -278,115 +288,122 @@ static void mux_select(uint8_t output) } } -/** unit Morse code duration in frames */ -#define MORSE_DOT 10 -uint8_t morse[1*4*5*2] = {0}; -uint8_t morse_i = 0; - static void encode_morse(void) { - bool not_zero = false; - for (uint8_t digit=0; digit<4; digit++) { - uint16_t number = 42; - // get only the digit from the number - for (uint8_t divide=digit; divide<3; divide++) { - number /= 10; - } - number %= 10; - // remember when we found the first non-zero digit - if (number!=0 || digit==3) { - not_zero = true; - } - // encode the number in Morse code (1=short, 3=long) - switch (number) { - case 1: - morse[digit*2*5+0] = 1*MORSE_DOT; - morse[digit*2*5+2] = 3*MORSE_DOT; - morse[digit*2*5+4] = 3*MORSE_DOT; - morse[digit*2*5+6] = 3*MORSE_DOT; - morse[digit*2*5+8] = 3*MORSE_DOT; - break; - case 2: - morse[digit*2*5+0] = 1*MORSE_DOT; - morse[digit*2*5+2] = 1*MORSE_DOT; - morse[digit*2*5+4] = 3*MORSE_DOT; - morse[digit*2*5+6] = 3*MORSE_DOT; - morse[digit*2*5+8] = 3*MORSE_DOT; - break; - case 3: - morse[digit*2*5+0] = 1*MORSE_DOT; - morse[digit*2*5+2] = 1*MORSE_DOT; - morse[digit*2*5+4] = 1*MORSE_DOT; - morse[digit*2*5+6] = 3*MORSE_DOT; - morse[digit*2*5+8] = 3*MORSE_DOT; - break; - case 4: - morse[digit*2*5+0] = 1*MORSE_DOT; - morse[digit*2*5+2] = 1*MORSE_DOT; - morse[digit*2*5+4] = 1*MORSE_DOT; - morse[digit*2*5+6] = 1*MORSE_DOT; - morse[digit*2*5+8] = 3*MORSE_DOT; - break; - case 5: - morse[digit*2*5+0] = 1*MORSE_DOT; - morse[digit*2*5+2] = 1*MORSE_DOT; - morse[digit*2*5+4] = 1*MORSE_DOT; - morse[digit*2*5+6] = 1*MORSE_DOT; - morse[digit*2*5+8] = 1*MORSE_DOT; - break; - case 6: - morse[digit*2*5+0] = 3*MORSE_DOT; - morse[digit*2*5+2] = 1*MORSE_DOT; - morse[digit*2*5+4] = 1*MORSE_DOT; - morse[digit*2*5+6] = 1*MORSE_DOT; - morse[digit*2*5+8] = 1*MORSE_DOT; - break; - case 7: - morse[digit*2*5+0] = 3*MORSE_DOT; - morse[digit*2*5+2] = 3*MORSE_DOT; - morse[digit*2*5+4] = 1*MORSE_DOT; - morse[digit*2*5+6] = 1*MORSE_DOT; - morse[digit*2*5+8] = 1*MORSE_DOT; - break; - case 8: - morse[digit*2*5+0] = 3*MORSE_DOT; - morse[digit*2*5+2] = 3*MORSE_DOT; - morse[digit*2*5+4] = 3*MORSE_DOT; - morse[digit*2*5+6] = 1*MORSE_DOT; - morse[digit*2*5+8] = 1*MORSE_DOT; - break; - case 9: - morse[digit*2*5+0] = 3*MORSE_DOT; - morse[digit*2*5+2] = 3*MORSE_DOT; - morse[digit*2*5+4] = 3*MORSE_DOT; - morse[digit*2*5+6] = 3*MORSE_DOT; - morse[digit*2*5+8] = 1*MORSE_DOT; - break; - case 0: - if (not_zero) { - morse[digit*2*5+0] = 3*MORSE_DOT; - morse[digit*2*5+2] = 3*MORSE_DOT; - morse[digit*2*5+4] = 3*MORSE_DOT; - morse[digit*2*5+6] = 3*MORSE_DOT; - morse[digit*2*5+8] = 3*MORSE_DOT; - } else { //don't encode the first digits if they are zero - morse[digit*2*5+0] = 0; - morse[digit*2*5+2] = 0; - morse[digit*2*5+4] = 0; - morse[digit*2*5+6] = 0; - morse[digit*2*5+8] = 0; + // encode numbers (scene and take) in Morse code + uint16_t morse_numbers[] = {numbers[0].number, numbers[1].number}; + printf("scene: %u, take: %u\n", morse_numbers[0], morse_numbers[1]); + for (uint8_t morse_number=0; morse_number0) { + morse_delay--; + } else if (0==morse_delay) { while (morse_i=LENGTH(morse)) { // all codes done - announce = false; // announce finished + morse_delay = -2; // Morse completed } } @@ -758,24 +804,6 @@ void main(void) } } - // display time and frame number - char time[] = "00000000"; // time to display - time[0] += (rtc_ds1307_time[2]/10)%10; // display hours - time[1] += (rtc_ds1307_time[2])%10; // display hours - time[1] |= 0x80; // dot for minutes on display - time[2] += (rtc_ds1307_time[1]/10)%10; // display minutes - time[3] += (rtc_ds1307_time[1])%10; // display minutes - if (0==(rtc_seconds%2)) { // even seconds - time[3] |= 0x80; // add dot for seconds - } - time[4] += (rtc_seconds/10)%10; // display seconds - time[5] += (rtc_seconds)%10; // display seconds - if (0==(frame_count%2)) { // even frames - time[5] |= 0x80; // add dot for frame - } - time[6] += (frame_count/10)%10; // display frame - time[7] += (frame_count)%10; // display frame - led_max7219_text(time, 0); // display frame time on 1st display } while (rtc_tick_flag) { // the external RTC ticked rtc_tick_flag = false; // reset flag @@ -808,9 +836,18 @@ void main(void) } } while (keep_alive_flag) { // power switch is detecting movement to keep clapperboard running + action = true; // action has been performed keep_alive_flag = false; // clear flag standby_timer = 0; // restart standby timer } + while (clap_flag) { // clap detected + action = true; // action has been performed + clap_flag = false; // clear flag + if (morse_delay<0) { // if beeping is not already queued + encode_morse(); // encode scene and take number into Morse code + morse_delay = FRAME_RATE; // wait 1 second + } + } if (action) { // go to sleep if nothing had to be done, else recheck for activity action = false; } else { @@ -853,3 +890,10 @@ void EXTI_ISR(POWER_BUTTON_PIN)(void) exti_reset_request(EXTI(POWER_BUTTON_PIN)); // reset interrupt keep_alive_flag = true; // perform button action } + +/** clap */ +void EXTI_ISR(CLAP_BUTTON_PIN)(void) +{ + exti_reset_request(EXTI(CLAP_BUTTON_PIN)); // reset interrupt + clap_flag = true; // perform clap action +}