From f621403d8f290fcdd9e043c94f000b74a59ead8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Fri, 5 May 2017 18:12:00 +0200 Subject: [PATCH] add morse code output --- main.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 133 insertions(+), 5 deletions(-) diff --git a/main.c b/main.c index 5aa85fe..3f00cc7 100644 --- a/main.c +++ b/main.c @@ -250,6 +250,118 @@ 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 = 40; + for (uint8_t divide=digit; divide<3; divide++) { + number /= 10; + } + number %= 10; + printf("%u: %u\n", digit, number); + // 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; + } + break; + } + // fill the spaces between the dots + for (uint8_t space=0; space<5; space++) { + if (0==morse[digit*2*5+space*2]) { + morse[digit*2*5+space*2+1] = 0; + } else { + morse[digit*2*5+space*2+1] = 1*MORSE_DOT; + } + } + } + morse_i = 0; // reset Morse index +} + /** program entry point * this is the firmware function started by the micro-controller */ @@ -422,6 +534,9 @@ void main(void) timer_generate_event(TIM(BUZZER_TIMER), TIM_EGR_UG); // generate update event to reload registers and reset counter printf("OK\n"); + encode_morse(); + bool announce = true; + // main loop printf("command input: ready\n"); bool action = false; // if an action has been performed don't go to sleep @@ -462,6 +577,24 @@ void main(void) while (frame_flag) { // a frame has passed frame_flag = false; // reset flag action = true; // action has been performed + if (announce) { // announce the scene and take using Morse code over the buzzer + while (morse_i=LENGTH(morse)) { // all codes done + announce = false; // announce finished + } + } 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 @@ -484,11 +617,6 @@ void main(void) rtc_tick_flag = false; // reset flag action = true; // action has been performed led_toggle(); // toggle LED (good to indicate if main function is stuck) - if (rtc_seconds%2) { - timer_enable_counter(TIM(BUZZER_TIMER)); // start buzzing - } else { - timer_disable_counter(TIM(BUZZER_TIMER)); // start buzzing - } if (standby_timer>=STANDBY_TIMEOUT) { // standby timeout complete // go into standby mode printf("shutting down\n");