From 533667ae47cf79c777ad70c668d9437ec853f0d5 Mon Sep 17 00:00:00 2001 From: Andreas Heider Date: Sat, 10 Mar 2018 18:27:41 +0000 Subject: [PATCH] Begin updating bt setup mode with information --- src/bluetooth.rs | 22 ++++++++++++++++++++++ src/keyboard.rs | 2 +- src/led.rs | 16 +++++++++++++--- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/bluetooth.rs b/src/bluetooth.rs index 5ef577e..486fc8d 100644 --- a/src/bluetooth.rs +++ b/src/bluetooth.rs @@ -10,9 +10,17 @@ use debug::UnwrapLog; use nb; use rtfm::Threshold; +#[derive(Copy, Clone)] +pub enum BluetoothMode { + Unknown, + Legacy, + Ble, +} + pub struct Bluetooth> { pub serial: Serial, pub rx_transfer: Option>, + mode: BluetoothMode, } impl Bluetooth @@ -27,6 +35,7 @@ where Bluetooth { serial, rx_transfer: Some(rx_transfer), + mode: BluetoothMode::Unknown, } } @@ -77,6 +86,10 @@ where ) } + pub fn update_led(&self, led: &mut Led) -> nb::Result<(), !> { + led.bluetooth_mode(self.mode) + } + pub fn handle_message(&mut self, message: &Message, led: &mut Led) { match message.msg_type { MsgType::System => { @@ -163,6 +176,15 @@ where debug!("bt disconnect").ok(); } BleOp::AckHostListQuery => { + if message.data.len() == 3 { + self.mode = match message.data[2] { + 0 => BluetoothMode::Ble, + 1 => BluetoothMode::Legacy, + _ => BluetoothMode::Unknown, + } + } + + self.update_led(led).log_error(); debug!("bt host list: {:?}", message.data).ok(); } _ => { diff --git a/src/keyboard.rs b/src/keyboard.rs index 9ef2440..c9f4d33 100644 --- a/src/keyboard.rs +++ b/src/keyboard.rs @@ -70,7 +70,7 @@ impl Keyboard { let bt_layer_current: bool = self.layers.current & (1 << LAYER_BT) != 0; let bt_layer_next: bool = self.layers.next & (1 << LAYER_BT) != 0; if bt_layer_next && !bt_layer_current { - led.bluetooth_mode().log_error(); + bluetooth.update_led(led).log_error(); } else if bt_layer_current && !bt_layer_next { // TODO: go back to previous theme? led.next_theme().log_error(); diff --git a/src/led.rs b/src/led.rs index 630dc72..cb414cb 100644 --- a/src/led.rs +++ b/src/led.rs @@ -2,6 +2,7 @@ use super::keymatrix::{to_packed_bits, KeyState}; use super::protocol::{LedOp, Message, MsgType}; use super::serial::{Serial, Transfer}; use super::serial::led_usart::LedUsart; +use bluetooth::BluetoothMode; use core::marker::Unsize; use embedded_hal::digital::OutputPin; use hal::gpio::{Input, Output}; @@ -102,7 +103,13 @@ where .send(MsgType::Led, LedOp::SetIndividualKeys as u8, payload) } - pub fn bluetooth_mode(&mut self) -> nb::Result<(), !> { + pub fn bluetooth_mode(&mut self, mode: BluetoothMode) -> nb::Result<(), !> { + let mode_color = match mode { + BluetoothMode::Unknown => (0, 0, 0xff), + BluetoothMode::Ble => (0, 0xff, 0), + BluetoothMode::Legacy => (0xff, 0xff, 0), + }; + #[cfg_attr(rustfmt, rustfmt_skip)] let payload = &[0xca, 0x0a, KeyIndex::Escape as u8, 0xff, 0xff, 0x00, LedMode::On as u8, @@ -112,8 +119,8 @@ where KeyIndex::N4 as u8, 0xff, 0x00, 0x00, LedMode::On as u8, KeyIndex::Equal as u8, 0x00, 0xff, 0x00, LedMode::On as u8, KeyIndex::B as u8, 0x00, 0xff, 0x00, LedMode::Flash as u8, - KeyIndex::Minus as u8, 0xff, 0x00, 0x00, LedMode::On as u8, - KeyIndex::N0 as u8, 0x00, 0xff, 0x00, LedMode::On as u8, + KeyIndex::Minus as u8, 0x00, 0xff, 0x00, LedMode::On as u8, + KeyIndex::N0 as u8, mode_color.0, mode_color.1, mode_color.2, LedMode::On as u8, KeyIndex::A as u8, 0x00, 0xff, 0x00, LedMode::On as u8, ]; @@ -132,6 +139,9 @@ where // data: [theme id, brightness, animation speed] //debug!("Led AckConfigCmd {:?}", message.data).ok(); } + LedOp::AckSetIndividualKeys => { + // data: [202] + } _ => { debug!( "lmsg: {:?} {} {:?}",