aboutsummaryrefslogtreecommitdiffstats
path: root/src/testapp1/morse.c
blob: 072e6592be732638f01c276fd4c2e579c08aeb81 (plain)
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
#include <stdio.h>
#include <avr/pgmspace.h>
#include <avr/io.h>
#include <avr/interrupt.h>

#include "delay.h"
#include "morse.h"

#define WORDSPACE (2*DIT_DURATION)

const uint16_t morse_mapping[60] PROGMEM =
{
    // Read bits from right to left

    0b110101, //+ ASCII 43
    0b110101, //, ASCII 44
    0b1011110, //- ASCII 45

    0b1010101, //., ASCII 46
    0b110110, // / ASCII 47

    0b100000, // 0, ASCII 48
    0b100001, // 1
    0b100011,
    0b100111,
    0b101111,
    0b111111,
    0b111110,
    0b111100,
    0b111000,
    0b110000, // 9, ASCII 57

    // the colon stands in as error
    0b111111111, // :

    // The following are mostly invalid, but
    // required to fill the gap in ASCII between
    // numerals and capital letters
    0b10, // ;
    0b10, // <
    0b10, // =
    0b10, // >
    0b1110011, // ?
    0b1101001, //@

    0b101, // A  ASCII 65
    0b11110,
    0b11010,
    0b1110,
    0b11,
    0b11011,
    0b1100,
    0b11111,
    0b111,
    0b10001,
    0b1010,
    0b11101,
    0b100, //M
    0b110,
    0b1000,
    0b11001,
    0b10100,
    0b1101,
    0b1111,
    0b10,
    0b1011,
    0b10111,
    0b1001,
    0b10110,
    0b10010,
    0b11100, // Z

    0b101010, //Start, ASCII [
    0b1010111, // SK , ASCII '\'
};

void morse_symbol(uint8_t sym)
{
    uint8_t p = 0;
    uint16_t val = pgm_read_word(&morse_mapping[sym]);

    while((val >> p) != 0b1) {

        if (((val >> p) & 0b1) == 0b1) {
            dit();
        }
        else {
            dah();
        }

        p++;
    }

    delay_ms(WORDSPACE);
}

void morse(char* text)
{
    char* sym = text;
    do {
        if (*sym == ' ') {
            delay_ms(WORDSPACE);
        }
        else {
            morse_symbol(*sym - '+');
        }
        sym++;
    } while (*sym != '\0');
}

void morse_char(char letter)
{
    morse_symbol(letter - '+');
}