add morse code output

This commit is contained in:
King Kévin 2017-05-05 18:12:00 +02:00
parent 4bdc673561
commit f621403d8f
1 changed files with 133 additions and 5 deletions

138
main.c
View File

@ -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)) {
if (morse[morse_i]) { // skip empty codes
if (morse_i%2) { // switch buzzer on for odd code
timer_disable_counter(TIM(BUZZER_TIMER)); // stop buzzing
} else {
timer_enable_counter(TIM(BUZZER_TIMER)); // start buzzing
}
morse[morse_i]--; // decrement beeping
break; // buzz for one frame
} else {
morse_i++; // got to next code
}
}
if (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");