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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/*
The MIT License (MIT)
Copyright (c) 2023 Matthias P. Braendli
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
use crate::state::*;
use core::fmt;
use core::fmt::Write;
use stm32f1xx_hal::{
gpio::gpioa::*,
gpio::gpiob::*,
gpio::gpioc::*,
gpio::{Input, Floating},
};
use embedded_hal::blocking::delay::{DelayMs, DelayUs};
use hd44780_driver::HD44780;
#[derive(PartialEq, Eq, Clone, Copy)]
struct ButtonState {
pub a : bool,
pub b : bool,
pub c : bool,
pub d : bool,
pub enc : bool,
pub ptt : bool,
}
const VFO_INCR : i32 = 2;
const RIT_INCR : i32 = 1;
const BFO_INCR : i32 = 10;
impl ButtonState {
fn edge_detection(&self, old_state : &ButtonState) -> ButtonState {
ButtonState {
a : !old_state.a && self.a,
b : !old_state.b && self.b,
c : !old_state.c && self.c,
d : !old_state.d && self.d,
enc : !old_state.enc && self.enc,
ptt : self.ptt, // Don't do edge detection for PTT!
}
}
}
impl Default for ButtonState {
fn default() -> Self {
ButtonState {
a : false,
b : false,
c : false,
d : false,
enc : false,
ptt : false,
}
}
}
impl fmt::Display for ButtonState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}{}{}{}{}{}",
if self.a { "A" } else { "a" },
if self.b { "B" } else { "b" },
if self.c { "C" } else { "c" },
if self.d { "D" } else { "d" },
if self.enc { "X" } else { "x" },
if self.ptt { "P" } else { "p" })
}
}
#[derive(Default)]
pub struct ButtonResult {
pub display_update : bool,
pub ptt : bool,
}
pub struct UI {
btn0 : PB1<Input<Floating>>,
btn1 : PB0<Input<Floating>>,
btn2 : PB12<Input<Floating>>,
btn3 : PB13<Input<Floating>>,
btn_enc : PC15<Input<Floating>>,
mic_ptt : PA3<Input<Floating>>,
previous_button_state : ButtonState,
}
impl UI {
pub fn new(
mic_ptt: PA3<Input<Floating>>,
btn0: PB1<Input<Floating>>,
btn1: PB0<Input<Floating>>,
btn2: PB12<Input<Floating>>,
btn3: PB13<Input<Floating>>,
btn_enc : PC15<Input<Floating>>) -> UI {
UI {
btn0,
btn1,
btn2,
btn3,
btn_enc,
mic_ptt,
previous_button_state: ButtonState::default(),
}
}
fn read_buttons(&mut self) -> ButtonState {
let mut buttons = ButtonState::default();
buttons.ptt = self.mic_ptt.is_high();
if self.btn0.is_low() {
buttons.a = true;
}
if self.btn1.is_low() {
buttons.b = true;
}
if self.btn2.is_low() {
buttons.c = true;
}
if self.btn3.is_low() {
buttons.d = true;
}
if self.btn_enc.is_low() {
buttons.enc = true;
}
buttons
}
pub fn handle_buttons(&mut self, state: &mut State) -> ButtonResult {
let mut result = ButtonResult::default();
let button_state = self.read_buttons();
let button_updates = button_state.edge_detection(&self.previous_button_state);
self.previous_button_state = button_state;
result.ptt = button_updates.ptt;
match state.menu_page {
MenuPage::One => {
if button_updates.a {
state.vfo_sel = match (state.ui_sel, state.vfo_sel) {
(UISelection::VFO, VFOSelection::A) => VFOSelection::B,
(UISelection::VFO, VFOSelection::B) => VFOSelection::A,
_ => state.vfo_sel.clone(),
};
state.ui_sel = UISelection::VFO;
result.display_update = true;
}
if button_updates.b {
state.ui_sel = UISelection::RIT;
result.display_update = true;
}
if button_updates.c {
let (new_ui_sel, new_filter_shift) = match (state.ui_sel, state.mode) {
(UISelection::Mode, Mode::USB) => (UISelection::Mode, Mode::LSB),
(UISelection::Mode, Mode::LSB) => (UISelection::Mode, Mode::CW(CWMode::StraightKey)),
(UISelection::Mode, Mode::CW(CWMode::StraightKey)) => (UISelection::Mode, Mode::CW(CWMode::Iambic)),
(UISelection::Mode, Mode::CW(CWMode::Iambic)) => (UISelection::Mode, Mode::DIG),
(UISelection::Mode, Mode::CustomShift(_)) => (UISelection::Mode, Mode::DIG),
(UISelection::Mode, Mode::DIG) => (UISelection::Mode, Mode::USB),
(_, f) => (UISelection::Mode, f),
};
state.ui_sel = new_ui_sel;
state.mode = new_filter_shift;
result.display_update = true;
}
if button_updates.d {
state.menu_page = MenuPage::Two;
result.display_update = true;
}
if button_updates.enc {
match state.ui_sel {
UISelection::VFO => {},
UISelection::RIT => {
state.rit = 0;
},
UISelection::Mode => {
state.mode = Mode::USB;
},
}
result.display_update = true;
}
},
MenuPage::Two => {
state.ui_sel = UISelection::VFO;
if button_updates.a {
state.send_tone = true;
}
if button_updates.b {
state.send_tone = false;
}
if button_updates.c {
}
if button_updates.d {
state.menu_page = MenuPage::One;
result.display_update = true;
}
if button_updates.enc {
}
},
}
if result.ptt {
state.send_tone = false;
}
result
}
// Returns true if bfo must be reprogrammed
pub fn update_encoder(&mut self, state: &mut State, counter_delta : i32) -> bool {
let delta = (17 * counter_delta + 3 * (counter_delta * counter_delta * counter_delta))/20;
match state.ui_sel {
UISelection::VFO => {
match state.vfo_sel {
VFOSelection::A => {
state.vfo_a = (state.vfo_a as i32 + delta * VFO_INCR) as u32;
},
VFOSelection::B => {
state.vfo_b = (state.vfo_b as i32 + delta * VFO_INCR) as u32;
},
}
false
},
UISelection::RIT => {
state.rit = state.rit + delta * RIT_INCR;
false
},
UISelection::Mode => {
match state.mode {
Mode::CW(CWMode::Iambic) => {
let mut new_wpm = state.cw_wpm as i32 + counter_delta / 4;
if new_wpm < 1 {
new_wpm = 1;
}
if new_wpm > 40 {
new_wpm = 40;
}
let wpm = new_wpm as u32;
state.cw_wpm = wpm;
state.mode = Mode::CW(CWMode::Iambic);
false
},
_ => {
let new_bfo = (state.bfo() as i32 + counter_delta * BFO_INCR) as u32;
state.mode = Mode::CustomShift(new_bfo);
true
},
}
},
}
}
}
pub fn update_disp<T: hd44780_driver::bus::DataBus, D: DelayUs<u16> + DelayMs<u8>>(lcd: &mut HD44780<T>, state: &State, delay: &mut D, s_meter_value: u8, bfo_tune_fail: bool)
{
let mut string = arrayvec::ArrayString::<16>::new();
/* Shorten the QRG to avoid using three digits for nothing */
let disp_freq = (state.vhf_qrg() as i32) - (VHF_BAND_EDGE as i32);
write!(string, "{:<03}.{:<03} ", disp_freq / 1000, disp_freq % 1000).unwrap();
/* By default, show RIT, unless UIselection is on mode, and mode is CW Iambic, in which case we show speed */
match (state.ui_sel, state.mode) {
(UISelection::Mode, Mode::CW(CWMode::Iambic)) => {
write!(string, "CW{:<02}", state.cw_wpm).unwrap();
},
(UISelection::Mode, Mode::CustomShift(shift)) => {
write!(string, "{:<04}", shift/10).unwrap();
},
_ => {
write!(string, "{}{:<03}", if state.rit >= 0 { "+" } else { "-" }, state.rit.abs()/10).unwrap();
},
}
if string.len() <= 16 - 4 {
// Avoids crash when frequency is very negative
write!(string, "S{:3}", s_meter_value).unwrap();
/*
write!(string, " {:2}",
match state.sequence_state {
SequenceState::Rx => "Rx",
SequenceState::MutingSpkr => "Ms",
SequenceState::SwitchingSSB => "Ss",
SequenceState::TxSSB => "Ts",
SequenceState::SwitchingCW => "Sc",
SequenceState::TxCW => "Tc",
SequenceState::SwitchingDIG => "Sd",
SequenceState::TxDIG => "Td",
}).unwrap();
*/
}
lcd.set_cursor_pos(0, delay).unwrap();
lcd.write_str(&string, delay).unwrap();
string.clear();
match state.menu_page {
MenuPage::One => {
match (bfo_tune_fail, &state.vfo_sel) {
(true, _) => write!(string, "VFO!").unwrap(),
(false, VFOSelection::A) => write!(string, "VFOa").unwrap(),
(false, VFOSelection::B) => write!(string, "VFOb").unwrap(),
}
write!(string, "{}", if state.ui_sel == UISelection::RIT { ">RIT" } else { " RIT" }).unwrap();
write!(string, "{}", if state.ui_sel == UISelection::Mode { ">" } else { " " }).unwrap();
let mode = match state.mode {
Mode::USB => "USB",
Mode::LSB => "LSB",
Mode::CustomShift(_) => "IFs",
Mode::CW(CWMode::StraightKey) => "CWs",
Mode::CW(CWMode::Iambic) => "CWp",
Mode::DIG => "DIG",
};
write!(string, "{} 1/2", mode).unwrap();
},
MenuPage::Two => {
write!(string, "TUT STOP --- 2/2").unwrap();
},
}
lcd.set_cursor_pos(40, delay).unwrap();
lcd.write_str(&string, delay).unwrap();
}
|