-
Notifications
You must be signed in to change notification settings - Fork 1
/
morse.c
122 lines (104 loc) · 2.36 KB
/
morse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "adc.h"
#include "globals.h"
#include "leds.h"
#include "morse.h"
#include "morse_patterns.h"
#include "serial.h"
#include <stdio.h>
#include <avr/pgmspace.h>
#include <string.h>
static uint8_t morse_char_ctr, morse_tick_ctr, morse_element_ctr;
bool_t morse_sending = false;
char morse_out[64];
void morse_send(void) {
morse_char_ctr = 0;
morse_tick_ctr = 0;
morse_element_ctr = 0;
morse_sending = true;
//adc_block();
}
void morse_handle(void) {
//led_unblock();
if(morse_sending) {
char currentChar = morse_out[morse_char_ctr];
if (currentChar == '\0') {
morse_sending = false;
//adc_unblock();
return;
}
uint8_t omcc = morse_char_ctr;
uint8_t omtc = morse_tick_ctr;
uint8_t omec = morse_element_ctr;
char element = 'x';
bool_t wantLeds = false;
int8_t idx = -1;
if (currentChar == ' ') {
// LEDs stay off
if (morse_tick_ctr > 2) {
morse_tick_ctr = 0;
morse_element_ctr = 0;
morse_char_ctr++;
} else {
morse_tick_ctr++;
}
} else {
const char* const * table;
if (currentChar >= '0' && currentChar <= '9') {
idx = currentChar - '0';
table = morse_num_table;
} else {
table = morse_alpha_table;
if (currentChar >= 'A' && currentChar <= 'Z') {
idx = currentChar - 'A';
} else if (currentChar >= 'a' && currentChar <= 'z') {
idx = currentChar - 'a';
}
}
if (idx >= 0) {
char *elements = (char*)pgm_read_word(&(table[idx]));
element = pgm_read_byte(&elements[morse_element_ctr]);
if (element == '\0') {
if (morse_tick_ctr > 0) {
morse_element_ctr = 0;
morse_tick_ctr = 0;
morse_char_ctr++;
} else {
morse_tick_ctr++;
}
element = '*';
} else {
uint8_t ticks;
if (element == '.') {
ticks = 1;
} else {
ticks = 3;
}
if (morse_tick_ctr < ticks) {
wantLeds = true;
morse_tick_ctr++;
} else {
morse_tick_ctr = 0;
morse_element_ctr++;
}
}
}
}
sprintf(serial_out, "%d %d %d %c %c %d\r\n", omcc, omec, omtc, currentChar, element, wantLeds);
if (wantLeds) {
led_want(0);
led_want(1);
}
/*
char* p = serial_out;
p[0] = currentChar;
p[1] = ':';
p[2] = ' ';
p += 3;
p = strcpy(p, elements);
strcpy(p, "\r\n");
*/
usart_send();
while (flag_serial_sending);
//morse_char_ctr++;
}
}