-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorse.c
80 lines (69 loc) · 1.84 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
#include <stdint.h>
#include <avr/io.h>
#include <stdio.h>
#include "morse.h"
#define MORSE_PERIOD 3000
#define MORSE_QUEUE_LEN 16
char morse_queue[MORSE_QUEUE_LEN];
uint8_t morse_queue_read = 0;
uint8_t morse_queue_write = 0;
void morse_send(char *s) {
while (*s) {
morse_queue[morse_queue_write++] = *s++;
if (morse_queue_write >= MORSE_QUEUE_LEN)
morse_queue_write = 0;
}
}
extern const int8_t sintable[];
extern uint8_t morsechar(char c);
uint8_t morse_cur_char = 0;
uint16_t morse_beep = 0;
uint16_t morse_silent = 0;
uint8_t morse_active() {
return (morse_beep > 0) || (morse_silent > 0);
}
void morse_tick() {
if (morse_beep > 0) {
static uint16_t cntsin;
cntsin += 13;
if (cntsin >= 1024)
cntsin -= 1024;
OCR0B = 128 + (sintable[cntsin] >> 3);
morse_beep --;
return;
}
if (morse_silent > 0) {
OCR0B = 128;
morse_silent --;
return;
}
if (morse_cur_char != 0) {
if (morse_cur_char & 1) {
morse_beep = MORSE_PERIOD*3;
} else {
morse_beep = MORSE_PERIOD;
}
morse_silent = MORSE_PERIOD*1;
morse_cur_char >>= 1;
if (morse_cur_char == 1) {
morse_silent = MORSE_PERIOD*3;
morse_cur_char = 0;
}
} else {
if (morse_queue_read != morse_queue_write) {
char c = morse_queue[morse_queue_read];
morse_queue_read ++;
if (morse_queue_read >= MORSE_QUEUE_LEN)
morse_queue_read = 0;
printf("m >%c< %d\r\n", c, c);
if (c == ' ') {
morse_silent = MORSE_PERIOD * 7;
morse_cur_char = 0;
} else {
morse_cur_char = morsechar(c);
return;
}
}
}
return;
}