Skip to content

Commit

Permalink
Begin updating bt setup mode with information
Browse files Browse the repository at this point in the history
  • Loading branch information
ah- committed Mar 10, 2018
1 parent 5568e59 commit 533667a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
22 changes: 22 additions & 0 deletions src/bluetooth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ use debug::UnwrapLog;
use nb;
use rtfm::Threshold;

#[derive(Copy, Clone)]
pub enum BluetoothMode {
Unknown,
Legacy,
Ble,
}

pub struct Bluetooth<BUFFER: 'static + Unsize<[u8]>> {
pub serial: Serial<BluetoothUsart, BUFFER>,
pub rx_transfer: Option<Transfer<BUFFER>>,
mode: BluetoothMode,
}

impl<BUFFER> Bluetooth<BUFFER>
Expand All @@ -27,6 +35,7 @@ where
Bluetooth {
serial,
rx_transfer: Some(rx_transfer),
mode: BluetoothMode::Unknown,
}
}

Expand Down Expand Up @@ -77,6 +86,10 @@ where
)
}

pub fn update_led(&self, led: &mut Led<BUFFER>) -> nb::Result<(), !> {
led.bluetooth_mode(self.mode)
}

pub fn handle_message(&mut self, message: &Message, led: &mut Led<BUFFER>) {
match message.msg_type {
MsgType::System => {
Expand Down Expand Up @@ -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();
}
_ => {
Expand Down
2 changes: 1 addition & 1 deletion src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
16 changes: 13 additions & 3 deletions src/led.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
Expand All @@ -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,
];

Expand All @@ -132,6 +139,9 @@ where
// data: [theme id, brightness, animation speed]
//debug!("Led AckConfigCmd {:?}", message.data).ok();
}
LedOp::AckSetIndividualKeys => {
// data: [202]
}
_ => {
debug!(
"lmsg: {:?} {} {:?}",
Expand Down

0 comments on commit 533667a

Please sign in to comment.