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
//! An embedded-hal crate for @arturo182's BlackBerry Q10 PMOD Keyboard
//!
//! Written on James' [Office Hours Stream](https://youtu.be/GQ0uzTIx9gY)
//!

// Developer's note:
//
// Interface for this keyboard
// To read the FW version register
//
// 1. WRITE (ADDRESS) 0x01
// 2. READ (ADDRESS) (read 1 byte)

#![no_std]

use embedded_hal::blocking::i2c::{Read, Write};

// DEFAULT ADDRESS, not currently changeable
const KBD_ADDR: u8 = 0x1F;

/// The Error type for this crate
#[derive(Debug)]
pub enum Error {
    /// A generic embedded-hal I2C error
    I2c,
}

/// The Result type for this crate
pub type Result<T> = core::result::Result<T, Error>;

/// A struct representing our BlackBerry Q10 PMOD Keyboard
pub struct Bbq10Kbd<I2C>
where
    I2C: Read + Write
{
    i2c: I2C
}

/// The version identifier of our keyboard's firmware
#[derive(Debug, PartialEq, Eq)]
pub struct Version {
    pub major: u8,
    pub minor: u8,
}

/// A raw key event from the management FIFO
#[derive(Debug)]
pub enum KeyRaw {
    Invalid,
    Pressed(u8),
    Held(u8),
    Released(u8),
}

/// The current state of NumLock
///
/// Numlock is enabled by pressing `alt` + `Left Shift`, and
/// disabled by double clicking either Shift key
#[derive(Debug)]
pub enum NumLockState {
    Off,
    On,
}

/// The current state of CapsLock
///
/// Capslock is enabled by pressing `alt` + `Right Shift`, and
/// disabled by double clicking either Shift key.
///
/// NOTE: Due to a firmware bug, the Fifo count may roll over
/// into the CapsLock bit. In this case, an `Unknown` value will
/// be returned.
#[derive(Debug)]
pub enum CapsLockState {
    Off,
    On,
    Unknown,
}

/// The current number of key events waiting in the FIFO queue
///
/// Each event contains a state + key id
///
/// NOTE: Due to a firmware bug, the Fifo count may roll over
/// into the CapsLock bit. In this case, an `EmptyOr32` value will
/// be returned, and there are either zero or 32 elements in the
/// fifo currently.
#[derive(Debug)]
pub enum FifoCount {
    Known(u8),
    EmptyOr32,
}

/// The current key status register reported by the keyboard firmware
#[derive(Debug)]
pub struct KeyStatus {
    num_lock: NumLockState,
    caps_lock: CapsLockState,
    fifo_count: FifoCount,
}

impl<I2C> Bbq10Kbd<I2C>
where
    I2C: Read + Write
{
    /// Create a new BBQ10 Keyboard instance
    pub fn new(i2c: I2C) -> Self {
        Self {
            i2c
        }
    }

    /// Consume self, returning the inner I2C device
    pub fn release(self) -> I2C {
        self.i2c
    }

    /// Get the version reported by the keyboard's firmware
    pub fn get_version(&mut self) -> Result<Version> {
        const VERSION_REGISTER: u8 = 0x01;
        let mut buf = [0u8; 1];

        buf[0] = VERSION_REGISTER;

        self.i2c
            .write(KBD_ADDR, &buf)
            .map_err(|_| Error::I2c)?;

        buf[0] = 0;

        self.i2c
            .read(KBD_ADDR, &mut buf)
            .map_err(|_| Error::I2c)?;

        let val = buf[0];

        Ok(Version {
            major: (val & 0xF0) >> 4,
            minor: (val & 0x0F),
        })
    }

    /// Obtain a single fifo item from the keyboard's firmware
    pub fn get_fifo_key_raw(&mut self) -> Result<KeyRaw> {
        const FIFO_REGISTER: u8 = 0x09;
        let mut buf = [0u8; 2];

        buf[0] = FIFO_REGISTER;

        self.i2c
            .write(KBD_ADDR, &buf[..1])
            .map_err(|_| Error::I2c)?;

        buf[0] = 0;

        self.i2c
            .read(KBD_ADDR, &mut buf)
            .map_err(|_| Error::I2c)?;

        Ok(match buf {
            [1, n] => KeyRaw::Pressed(n),
            [2, n] => KeyRaw::Held(n),
            [3, n] => KeyRaw::Released(n),
            [_, _] => KeyRaw::Invalid,
        })
    }

    /// Get the current level of backlight. All u8 values are valid
    pub fn get_backlight(&mut self) -> Result<u8> {
        const BACKLIGHT_REGISTER_READ: u8 = 0x05;
        let mut buf = [0u8; 1];

        buf[0] = BACKLIGHT_REGISTER_READ;

        self.i2c
            .write(KBD_ADDR, &buf)
            .map_err(|_| Error::I2c)?;

        buf[0] = 0;

        self.i2c
            .read(KBD_ADDR, &mut buf)
            .map_err(|_| Error::I2c)?;

        Ok(buf[0])
    }

    /// Set the current level of backlight. All u8 values are valid
    pub fn set_backlight(&mut self, level: u8) -> Result<()> {
        const BACKLIGHT_REGISTER_WRITE: u8 = 0x85;
        let mut buf = [0u8; 2];

        buf[0] = BACKLIGHT_REGISTER_WRITE;
        buf[1] = level;

        self.i2c
            .write(KBD_ADDR, &buf)
            .map_err(|_| Error::I2c)
    }

    /// Reset the device via software
    ///
    /// WARNING: Device may take >= 10ms to reboot. It
    /// will not be responsive during this time
    pub fn sw_reset(&mut self) -> Result<()> {
        const RESET_REGISTER: u8 = 0x08;
        let mut buf = [0u8; 1];

        buf[0] = RESET_REGISTER;

        // This is enough to reset the device
        self.i2c
            .write(KBD_ADDR, &buf)
            .map_err(|_| Error::I2c)
    }

    /// Get the reported status of the keyboard
    pub fn get_key_status(&mut self) -> Result<KeyStatus> {
        const KEY_STATUS_REGISTER: u8 = 0x04;
        let mut buf = [0u8; 1];

        buf[0] = KEY_STATUS_REGISTER;

        self.i2c
            .write(KBD_ADDR, &buf)
            .map_err(|_| Error::I2c)?;

        buf[0] = 0;

        self.i2c
            .read(KBD_ADDR, &mut buf)
            .map_err(|_| Error::I2c)?;

        let mut resp = buf[0];

        let num_lock = if (resp & 0b0100_0000) != 0 {
            NumLockState::On
        } else {
            NumLockState::Off
        };
        resp = resp & 0b1011_1111;

        let capslock = (resp & 0b0010_0000) != 0;
        let fifo_ct = resp & 0b0001_1111;

        Ok(match (capslock, fifo_ct) {
            (true, 0) => {
                KeyStatus {
                    caps_lock: CapsLockState::Unknown,
                    num_lock,
                    fifo_count: FifoCount::EmptyOr32,
                }
            },
            (true, n) => {
                KeyStatus {
                    caps_lock: CapsLockState::On,
                    num_lock,
                    fifo_count: FifoCount::Known(n),
                }
            }
            (false, n) => {
                KeyStatus {
                    caps_lock: CapsLockState::Off,
                    num_lock,
                    fifo_count: FifoCount::Known(n),
                }
            }
        })
    }

}