aboutsummaryrefslogtreecommitdiffstats
path: root/sw/dart-70/src/cw.rs
blob: 4656f819e4173bfa23729cfb24f4bc8977725e67 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//! CW output using PWM on PA8, TIM1 CH1

use stm32f1xx_hal::{
    timer,
    pac::TIM1,
};

const CW_MAPPING : [u8; 50] = [ //{{{
    // 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 following are mostly invalid, but
    // required to fill the gap in ASCII between
    // numerals and capital letters
    0b10, // :
    0b10, // ;
    0b10, // <
    0b101110, // =
    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 '\'
]; //}}}

const CW_MACRO : &[u8; 28] = b"CQ DE HB9EGM HB9EGM HB9EGM K";

pub const SIDETONE_FREQ : u32 = 800;

pub struct CWPWM {
    channel : timer::pwm::PwmChannel<TIM1, { timer::pwm::C1 }>,
}

impl CWPWM {
    pub fn new(mut channel: timer::pwm::PwmChannel<TIM1, { timer::pwm::C1 }>) -> Self {
        channel.enable();
        channel.set_duty(0);
        CWPWM { channel }
    }

    pub fn on(&mut self) {
        let max = self.channel.get_max_duty();
        self.channel.set_duty(max / 2);
    }

    pub fn off(&mut self) {
        self.channel.set_duty(0);
    }
}

#[derive(PartialEq, Eq, Clone, Copy)]
enum MorseSign {
    Dot,
    Dash
}

impl MorseSign {
    fn length(&self) -> u32 {
        match self {
            Self::Dot => 1,
            Self::Dash => 3,
        }
    }

    fn other(&self) -> Self {
        match self {
            Self::Dot => Self::Dash,
            Self::Dash => Self::Dot,
        }
    }
}

fn other_pressed(sign: MorseSign, dot_pressed: bool, dash_pressed: bool) -> bool {
    match sign {
        MorseSign::Dot => dash_pressed,
        MorseSign::Dash => dot_pressed,
    }
}

#[derive(Eq, Clone, Copy)]
enum KeyerState {
    Idle,
    Beep{current: MorseSign, next: Option<MorseSign>},
    Pause{current: MorseSign, next: Option<MorseSign>},
    LastPause{next: Option<MorseSign>},
}

impl PartialEq for KeyerState {
    fn eq(&self, rhs: &Self) -> bool {
        match (self, rhs) {
            (Self::Idle, Self::Idle) => true,
            (Self::Beep{current : c1, next : _}, Self::Beep{current : c2, next : _}) => c1 == c2,
            (Self::Pause{current : c1, next : _}, Self::Pause{current : c2, next : _}) => c1 == c2,
            (Self::LastPause{next : _}, Self::LastPause{next : _}) => true,
            _ => false,
        }
    }
}

pub struct Keyer {
    // All durations are in ticks
    dot_length : u32,
    state : KeyerState,
    time_last_state_change : u32,
}

impl Keyer {
    pub fn new(wpm : u32, ticks_per_s: u32) -> Keyer {
        /* PARIS standard: 20 words per minute = dot length of 60 ms, inversely proportional:
         * 1 wpm = 1200 ms, 2 wpm = 600 ms */

        Keyer{
            dot_length : 1200 * ticks_per_s / (1000 * wpm),
            state : KeyerState::Idle,
            time_last_state_change : 0,
        }
    }
    pub fn set_speed(&mut self, wpm : u32, ticks_per_s: u32) { self.dot_length = 1200 * ticks_per_s / (1000 * wpm) }

    pub fn tick(&mut self, ticks_now: u32, dot_pressed: bool, dash_pressed: bool) -> bool {
        let mut transmit = false;

        let next_state = match self.state {
            KeyerState::Idle => {
                if dot_pressed {
                    transmit = true;
                    KeyerState::Beep{current: MorseSign::Dot, next: None}
                }
                else if dash_pressed {
                    transmit = true;
                    KeyerState::Beep{current: MorseSign::Dash, next: None}
                }
                else {
                    KeyerState::Idle
                }
            },
            KeyerState::Beep{current, next} => {
                transmit = true;

                let next = if other_pressed(current, dot_pressed, dash_pressed) {
                    Some(current.other())
                }
                else {
                    next
                };

                if self.time_last_state_change + self.dot_length * current.length() <= ticks_now {
                    KeyerState::Pause{current, next}
                }
                else {
                    KeyerState::Beep{current, next}
                }
            },
            KeyerState::Pause{current, next} => {
                let next = if other_pressed(current, dot_pressed, dash_pressed) {
                    Some(current.other())
                }
                else {
                    next
                };

                if self.time_last_state_change + self.dot_length <= ticks_now {
                    match next {
                        Some(state) => {
                            transmit = true;
                            KeyerState::Beep{current: state, next: None}
                        },
                        None => KeyerState::LastPause{next: None}
                    }
                }
                else {
                    KeyerState::Pause{current, next}
                }
            },
            KeyerState::LastPause{next} => {
                let next = if dot_pressed {
                    Some(MorseSign::Dot)
                }
                else if dash_pressed {
                    Some(MorseSign::Dash)
                }
                else {
                    next
                };

                if self.time_last_state_change + self.dot_length <= ticks_now {
                    KeyerState::LastPause{next}
                }
                else {
                    match next {
                        Some(MorseSign::Dot) => {
                            transmit = true;
                            KeyerState::Beep{current: MorseSign::Dot, next: None}
                        },
                        Some(MorseSign::Dash) => {
                            transmit = true;
                            KeyerState::Beep{current: MorseSign::Dash, next: None}
                        },
                        None => {
                            KeyerState::Idle
                        },
                    }
                }
            },
        };

        if next_state != self.state {
            self.time_last_state_change = ticks_now;
        }
        self.state = next_state;

        transmit
    }
}