aboutsummaryrefslogtreecommitdiffstats
path: root/sw/dart-70/src/state.rs
blob: c38f75d92b11f5b8b1d57d9fc0f9ff628b5e81a7 (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
pub const VHF_BAND_EDGE : u32 = 70_000_000;
pub const VHF_INITIAL_VFO : u32 = 70_300_000;
// Values below are measured with the assembled XTAL filter
pub const XTAL_FILT_FREQ : u32 = 11_057_500;
pub const BFO_LSB : u32 = XTAL_FILT_FREQ + 1_400;
pub const BFO_USB : u32 = XTAL_FILT_FREQ - 1_400;
pub const BFO_CW : u32 = XTAL_FILT_FREQ - 1_400;

// Defines which parameter is changed by the encoder
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum UISelection {
    VFO,
    RIT,
    Mode,
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum MenuPage {
    One,
    Two,
}

#[derive(Clone, Copy)]
pub enum VFOSelection {
    A,
    B,
}

#[derive(PartialEq, Eq, Clone, Copy)]
pub enum CWMode {
    StraightKey,
    Iambic,
}

#[derive(PartialEq, Eq, Clone, Copy)]
pub enum Mode {
    LSB,
    USB,
    CustomShift(u32),
    CW(CWMode),
}

#[derive(Clone, PartialEq, Eq)]
pub enum SequenceState {
    Rx,
    MutingSpkr,
    SwitchingSSB,
    TxSSB,
    SwitchingCW,
    TxCW,
}

impl SequenceState {
    fn apply_rit(&self) -> bool {
        *self == SequenceState::Rx
    }
}

#[derive(Clone)]
pub struct State {
    pub menu_page : MenuPage,
    pub ui_sel : UISelection,
    pub vfo_a : u32,
    pub vfo_b : u32,
    pub vfo_sel : VFOSelection,
    pub rit : i32,
    pub mode : Mode,
    pub send_tone : bool,
    pub sequence_state : SequenceState,
    pub update_disp_counter : u8,
    pub cw_wpm : u32,
}

impl State {
    pub fn new() -> Self {
        State {
            menu_page : MenuPage::One,
            ui_sel : UISelection::VFO,
            vfo_a : VHF_INITIAL_VFO,
            vfo_b : VHF_INITIAL_VFO,
            vfo_sel : VFOSelection::A,
            rit : 0,
            mode : Mode::USB,
            send_tone : false,
            sequence_state : SequenceState::Rx,
            update_disp_counter : 0,
            cw_wpm : 14,
        }
    }

    pub fn bfo(&self) -> u32 {
        if self.send_tone {
            0
        }
        else {
            match self.mode {
                Mode::LSB => BFO_LSB,
                Mode::USB => BFO_USB,
                Mode::CustomShift(fs) => fs,
                Mode::CW(_) => match self.sequence_state {
                    SequenceState::SwitchingCW | SequenceState::TxCW => 0,
                    _ => BFO_CW,
                },
            }
        }
    }

    pub fn vhf_qrg(&self) -> u32 {
        match self.vfo_sel {
            VFOSelection::A => self.vfo_a,
            VFOSelection::B => self.vfo_b,
        }
    }

    pub fn vfo(&self) -> u32 {
        let cw_offset = match self.sequence_state {
            SequenceState::SwitchingCW | SequenceState::TxCW => 500,
            _ => 0,
        };

        let vfo = (self.vhf_qrg() - self.bfo()) as i32 +
            if self.sequence_state.apply_rit() { self.rit } else { 0 } +
                cw_offset;
        vfo as u32
    }
}