Merge branch 'micropeak-1.1'
[fw/altos] / src / micropeak / ao_morse.c
1 static void
2 pause(uint8_t   j)
3 {
4         int64_t i;
5
6         while (j--) {
7                 for (i = 0; i < 2000; i++)
8                         ao_arch_nop();
9         }
10 }
11
12 #define BIT(i,x)           ((x) ? (1 << (i)) : 0)
13 #define MORSE1(a)          (1 | BIT(3,a))
14 #define MORSE2(a,b)        (2 | BIT(3,a) | BIT(4,b))
15 #define MORSE3(a,b,c)      (3 | BIT(3,a) | BIT(4,b) | BIT(5,c))
16 #define MORSE4(a,b,c,d)    (4 | BIT(3,a) | BIT(4,b) | BIT(5,c) | BIT(6,d))
17 #define MORSE5(a,b,c,d,e)  (5 | BIT(3,a) | BIT(4,b) | BIT(5,c) | BIT(6,d) | BIT(7,e))
18
19 #define ___     1
20 #define _       0
21
22 static const uint8_t    morse[26] = {
23         MORSE2(0,1),            /* A */
24         MORSE4(1,0,0,0),        /* B */
25         MORSE4(1,0,1,0),        /* C */
26         MORSE3(1,0,0),          /* D */
27         MORSE1(0),              /* E */
28         MORSE4(0,0,1,0),        /* F */
29         MORSE3(1,1,0),          /* G */
30         MORSE4(0,0,0,0),        /* H */
31         MORSE2(0,0),            /* I */
32         MORSE4(0,1,1,1),        /* J */
33         MORSE3(1,0,1),          /* K */
34         MORSE4(0,1,0,0),        /* L */
35         MORSE2(1,1),            /* M */
36         MORSE2(1,1),            /* N */
37         MORSE3(1,1,1),          /* O */
38         MORSE4(0,1,1,0),        /* P */
39         MORSE4(1,1,0,1),        /* Q */
40         MORSE3(0,1,0),          /* R */
41         MORSE3(0,0,0),          /* S */
42         MORSE1(1),              /* T */
43         MORSE3(0,0,1),          /* U */
44         MORSE4(0,0,0,1),        /* V */
45         MORSE3(0,1,1),          /* W */
46         MORSE4(1,0,0,1),        /* X */
47         MORSE4(1,0,1,1),        /* Y */
48         MORSE4(1,1,0,0),        /* Z */
49 };
50
51 static void
52 on(void)
53 {
54         PORTB |= (1 << 4);
55 }
56
57 static void
58 off(void)
59 {
60         PORTB &= ~(1 << 4);
61 }
62
63 static void
64 morse_char (char c)
65 {
66         uint8_t r = morse[c - 'a'];
67         uint8_t l = r & 7;
68
69         if (!r)
70                 return;
71         while (l--) {
72                 on();
73                 if (r & 8)
74                         pause(3);
75                 else
76                         pause(1);
77                 off();
78                 pause(1);
79                 r >>= 1;
80         }
81         pause(2);
82 }
83
84 static void
85 morse_string(char *s) {
86         char    c;
87
88         while ((c = *s++)) {
89                 if (c == ' ')
90                         pause(5);
91                 else
92                         morse_char(c);
93         }
94 }
95