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
use crate::handlers::{ProcessKeys, HandlerResult};
use crate::key_codes::AcceptsKeycode;
use crate::key_stream::{iter_unhandled_mut, Event, EventStatus};
use crate::USBKeyOut;
use no_std_compat::prelude::v1::*;

pub enum TapDanceEnd {
    Timeout,
    OtherKey
}

/// call backs for completed tap dances
pub trait TapDanceAction {
    fn on_tapdance( &mut self, trigger: u32, output: &mut impl USBKeyOut, tap_count: u8, tap_end: TapDanceEnd);
}


///Depending on how often a key has ben pressed repeatedly
///(within a timeout), do different things.
///
///You will need to implement a TapDanceAction
pub struct TapDance<M>{
    trigger: u32,
    tap_count: u8,
    action: M,
    //todo: add on_each_tap...
    timeout_ms: u16,
}

impl <M: TapDanceAction> TapDance<M> {
    pub fn new(trigger: impl AcceptsKeycode, action: M, timeout_ms: u16) -> TapDance<M> {
        TapDance {
            trigger: trigger.to_u32(),
            tap_count: 0,
            action,
            timeout_ms: timeout_ms,
        }
    }
}
impl<T: USBKeyOut, M: TapDanceAction> ProcessKeys<T> for TapDance<M> {
    fn process_keys(&mut self, events: &mut Vec<(Event, EventStatus)>, output: &mut T) ->HandlerResult {
        for (event, status) in iter_unhandled_mut(events) {
            match event {
                Event::KeyRelease(kc) => {
                    if kc.keycode == self.trigger {
                        *status = EventStatus::Handled;
                    }
                }
                Event::KeyPress(kc) => {
                    if kc.keycode != self.trigger {
                        if self.tap_count > 0 {
                            self.action.on_tapdance(self.trigger, output, self.tap_count, TapDanceEnd::OtherKey);
                            self.tap_count = 0;
                        }
                    } else {
                        self.tap_count += 1;
                        *status = EventStatus::Handled;
                    }
                }
                Event::TimeOut(ms_since_last) => {
                    if self.tap_count > 0 && *ms_since_last >= self.timeout_ms {
                            self.action.on_tapdance(self.trigger, output, self.tap_count, TapDanceEnd::Timeout);
                        self.tap_count = 0;
                    }
                }
            }
        }
        HandlerResult::NoOp
    }
}
#[cfg(test)]
//#[macro_use]
//extern crate std;
mod tests {
    use crate::handlers::{TapDance, USBKeyboard, TapDanceAction, TapDanceEnd};
    #[allow(unused_imports)]
    use crate::key_codes::KeyCode;
    #[allow(unused_imports)]
    use crate::test_helpers::{check_output, KeyOutCatcher, Checks};
    #[allow(unused_imports)]
    use crate::{
        Event, EventStatus, Keyboard, KeyboardState, ProcessKeys, USBKeyOut, UnicodeSendMode,
    };
    #[allow(unused_imports)]
    use no_std_compat::prelude::v1::*;
    use alloc::sync::Arc;
    use spin::RwLock;

    #[derive(Debug)]
    pub struct TapDanceLogger {
        pub other_key_taps: u16,
        pub timeout_taps: u16,
    }
    impl TapDanceLogger {
        fn new() -> TapDanceLogger {
            TapDanceLogger{other_key_taps: 0, timeout_taps: 0}
        }
    }
    impl TapDanceAction for Arc<RwLock<TapDanceLogger>> {
        fn on_tapdance( &mut self, _trigger: u32, output: &mut impl USBKeyOut, tap_count: u8, tap_end: TapDanceEnd){
            match tap_end {
                TapDanceEnd::OtherKey => self.write().other_key_taps += tap_count as u16,
                TapDanceEnd::Timeout => self.write().timeout_taps += tap_count as u16,
            }
            output.send_keys(&[KeyCode::A]);
        }
    }

    #[test]
    fn test_tapdance() {
        let record = Arc::new(RwLock::new(TapDanceLogger::new()));
        let l = TapDance::new( KeyCode::X, record.clone(), 250);
        let mut keyboard = Keyboard::new(KeyOutCatcher::new());
        keyboard.add_handler(Box::new(l));
        keyboard.add_handler(Box::new(USBKeyboard::new()));
        //simplest case, one press/release then another key
        keyboard.pc(KeyCode::X, &[&[]]);
        assert!(record.read().other_key_taps == 0);
        assert!(record.read().timeout_taps == 0);

        keyboard.rc(KeyCode::X, &[&[]]);
        assert!(record.read().other_key_taps == 0);
        assert!(record.read().timeout_taps == 0);

        keyboard.pc(KeyCode::Z, &[&[KeyCode::A], &[KeyCode::Z]]);
        assert!(record.read().other_key_taps == 1);
        assert!(record.read().timeout_taps == 0);

        keyboard.rc(KeyCode::Z, &[&[]]);
        assert!(record.read().other_key_taps == 1);
        assert!(record.read().timeout_taps == 0);

        //two taps, then another key
        keyboard.pc(KeyCode::X, &[&[]]);
        keyboard.rc(KeyCode::X, &[&[]]);
        keyboard.pc(KeyCode::X, &[&[]]);
        keyboard.rc(KeyCode::X, &[&[]]);
        assert!(record.read().other_key_taps == 1);
       assert!(record.read().timeout_taps == 0);

        keyboard.pc(KeyCode::Z, &[&[KeyCode::A], &[KeyCode::Z]]);
        assert!(record.read().other_key_taps == 3);
        assert!(record.read().timeout_taps == 0);

        keyboard.rc(KeyCode::Z, &[&[]]);
        assert!(record.read().other_key_taps == 3);
        assert!(record.read().timeout_taps == 0);



        //three taps, then a time out
        keyboard.pc(KeyCode::X, &[&[]]);
        keyboard.rc(KeyCode::X, &[&[]]);
        keyboard.pc(KeyCode::X, &[&[]]);
        keyboard.rc(KeyCode::X, &[&[]]);
        keyboard.pc(KeyCode::X, &[&[]]);
        keyboard.rc(KeyCode::X, &[&[]]);
        assert!(record.read().other_key_taps == 3);
        assert!(record.read().timeout_taps == 0);
        //not a timeout...
        keyboard.tc(249, &[&[]]); //remember, repeaeted empty ones are (supossed to be) ommited by the downstream USB handling
        keyboard.tc(250, &[&[KeyCode::A], &[]]);
        assert!(record.read().other_key_taps == 3);
        assert!(record.read().timeout_taps == 3);
    }
}