diff --git a/Cargo.toml b/Cargo.toml index 9f3eea7..a60f69f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "esp" +name = "esp32-rust-split-keyboard" version = "0.1.0" authors = ["65787978"] edition = "2021" @@ -7,7 +7,7 @@ resolver = "2" rust-version = "1.77" [[bin]] -name = "esp" +name = "esp32-rust-split-keyboard" harness = false # do not use the built in cargo test harness -> resolve rust-analyzer errors [profile.release] @@ -24,12 +24,14 @@ pio = ["esp-idf-svc/pio"] std = ["alloc", "esp-idf-svc/binstart", "esp-idf-svc/std"] alloc = ["esp-idf-svc/alloc"] nightly = ["esp-idf-svc/nightly"] -experimental = ["esp-idf-svc/experimental", "esp-idf-svc/bt"] +experimental = ["esp-idf-svc/experimental"] embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"] [dependencies] log = { version = "0.4", default-features = false } esp-idf-svc = { version = "0.49", default-features = false } +esp-idf-hal = "0.44.1" +chrono = "0.4.38" [build-dependencies] -embuild = "0.32.0" \ No newline at end of file +embuild = "0.32.0" diff --git a/src/lib.rs b/src/lib.rs index ccb4986..9312311 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,18 +1,18 @@ /* - -___| 0 | 1 | 2 | 3 | 4 | 5 | ___| 0 | 1 | 2 | 3 | 4 | 5 | - 0 |_ESC_|__1__|__2__|__3__|__4__|__5__| 0 |__6__|__7__|__8__|__9__|__0__|__-__| - 1 |_TAB_|__Q__|__W__|__E__|__R__|__T__| 1 |__Y__|__U__|__I__|__O__|__P__|__{__| - 2 |_CAP_|__A__|__S__|__D__|__F__|__G__| 2 |__H__|__J__|__K__|__L__|__;__|__}__| - 3 |_SFT_|__Z__|__X__|__C__|__V__|__B__| 3 |__N__|__M__|__,__|__.__|__/__|__\__| - 4 |_____|_____|_____|_CTL_|_BSP_|_DEL_| 4 |_CTL_|_ENT_|_SPC_|_FUN_|_____|_____| +PINS PINS +___| 0 | 1 | 12 | 18 | 19 | 13 | ___| 0 | 1 | 12 | 18 | 19 | 13 | + 2 |_ESC_|__1__|__2__|__3__|__4__|__5__| 2 |__6__|__7__|__8__|__9__|__0__|__-__| + 3 |_TAB_|__Q__|__W__|__E__|__R__|__T__| 3 |__Y__|__U__|__I__|__O__|__P__|__{__| +10 |_CAP_|__A__|__S__|__D__|__F__|__G__| 10|__H__|__J__|__K__|__L__|__;__|__}__| + 6 |_SFT_|__Z__|__X__|__C__|__V__|__B__| 6 |__N__|__M__|__,__|__.__|__/__|__\__| + 7 |_____|_____|_____|_CTL_|_BSP_|_DEL_| 7 |_CTL_|_ENT_|_SPC_|_FUN_|_____|_____| */ use std::collections::HashMap; #[derive(Clone, Default, Debug)] pub struct KeyboardLeftSide { - pub key: HashMap<(u8, u8), bool>, + pub key: HashMap<(i32, i32), &'static str>, } impl KeyboardLeftSide { @@ -22,40 +22,40 @@ impl KeyboardLeftSide { } } - pub fn initialize_keys(&mut self) { - self.key.insert((0, 0), false); /* ESC */ - self.key.insert((0, 1), false); /* 1 */ - self.key.insert((0, 2), false); /* 2 */ - self.key.insert((0, 3), false); /* 3 */ - self.key.insert((0, 4), false); /* 4 */ - self.key.insert((0, 5), false); /* 5 */ - - self.key.insert((1, 0), false); /* TAB */ - self.key.insert((1, 1), false); /* Q */ - self.key.insert((1, 2), false); /* W */ - self.key.insert((1, 3), false); /* E */ - self.key.insert((1, 4), false); /* R */ - self.key.insert((1, 5), false); /* T */ - - self.key.insert((2, 0), false); /* CAP */ - self.key.insert((2, 1), false); /* A */ - self.key.insert((2, 2), false); /* S */ - self.key.insert((2, 3), false); /* D */ - self.key.insert((2, 4), false); /* F */ - self.key.insert((2, 5), false); /* G */ - - self.key.insert((3, 0), false); /* SFT */ - self.key.insert((3, 1), false); /* Z */ - self.key.insert((3, 2), false); /* X */ - self.key.insert((3, 3), false); /* C */ - self.key.insert((3, 4), false); /* V */ - self.key.insert((3, 5), false); /* B */ - - self.key.insert((4, 0), false); /* placeHolder */ - self.key.insert((4, 1), false); /* placeHolder */ - self.key.insert((4, 2), false); /* placeHolder */ - self.key.insert((4, 3), false); /* CTL */ - self.key.insert((4, 4), false); /* BSP */ - self.key.insert((4, 5), false); /* DEL */ + pub fn initialize_hashmap(&mut self) { + self.key.insert((2, 0), "ESC"); /* ESC */ + self.key.insert((2, 1), "1"); /* 1 */ + self.key.insert((2, 12), "2"); /* 2 */ + self.key.insert((2, 18), "3"); /* 3 */ + self.key.insert((2, 19), "4"); /* 4 */ + self.key.insert((2, 13), "5"); /* 5 */ + + self.key.insert((3, 0), "TAB"); /* TAB */ + self.key.insert((3, 1), "Q"); /* Q */ + self.key.insert((3, 12), "W"); /* W */ + self.key.insert((3, 18), "E"); /* E */ + self.key.insert((3, 19), "R"); /* R */ + self.key.insert((3, 13), "T"); /* T */ + + self.key.insert((10, 0), "CAP"); /* CAP */ + self.key.insert((10, 1), "A"); /* A */ + self.key.insert((10, 12), "S"); /* S */ + self.key.insert((10, 18), "D"); /* D */ + self.key.insert((10, 19), "F"); /* F */ + self.key.insert((10, 13), "G"); /* G */ + + self.key.insert((6, 0), "SFT"); /* SFT */ + self.key.insert((6, 1), "Z"); /* Z */ + self.key.insert((6, 12), "X"); /* X */ + self.key.insert((6, 18), "C"); /* C */ + self.key.insert((6, 19), "V"); /* V */ + self.key.insert((6, 13), "B"); /* B */ + + self.key.insert((7, 0), "PLACEHOLDER"); /* placeHolder */ + self.key.insert((7, 1), "PLACEHOLDER"); /* placeHolder */ + self.key.insert((7, 12), "PLACEHOLDER"); /* placeHolder */ + self.key.insert((7, 18), "CTL"); /* CTL */ + self.key.insert((7, 19), "BSP"); /* BSP */ + self.key.insert((7, 13), "DEL"); /* DEL */ } } diff --git a/src/main.rs b/src/main.rs index c51f5b8..9a0dc50 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,48 +1,242 @@ -use esp::KeyboardLeftSide; -use esp_idf_svc::{bt::*, log::EspLogger, sys}; -use std::{thread::sleep, time::Duration}; +/* +to flash: +espflash flash ../target/riscv32imc-esp-espidf/debug/esp32-rust-split-keyboard --monitor +*/ +use chrono::Utc; +use esp32_rust_split_keyboard::KeyboardLeftSide; +use esp_idf_hal::delay::FreeRtos; +use esp_idf_hal::gpio::*; +use esp_idf_hal::peripherals::Peripherals; +use esp_idf_svc::sys::link_patches; +use std::vec; + +enum Rows<'a> { + Row0(PinDriver<'a, Gpio2, Input>), + Row1(PinDriver<'a, Gpio3, Input>), + Row2(PinDriver<'a, Gpio10, Input>), + Row3(PinDriver<'a, Gpio6, Input>), + Row4(PinDriver<'a, Gpio7, Input>), +} +enum Cols<'a> { + Col0(PinDriver<'a, Gpio0, Input>), + Col1(PinDriver<'a, Gpio1, Input>), + Col2(PinDriver<'a, Gpio12, Input>), + Col3(PinDriver<'a, Gpio18, Input>), + Col4(PinDriver<'a, Gpio19, Input>), + Col5(PinDriver<'a, Gpio13, Input>), +} fn main() { // It is necessary to call this function once. Otherwise some patches to the runtime // implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71 - esp_idf_svc::sys::link_patches(); + link_patches(); // Bind the log crate to the ESP Logging facilities esp_idf_svc::log::EspLogger::initialize_default(); log::info!("Hello, world!"); - /* Declare keys */ - let mut keyboard_left_side = KeyboardLeftSide::new(); - keyboard_left_side.initialize_keys(); + let peripherals = Peripherals::take().expect("msg"); + + let row_0: PinDriver = PinDriver::input(peripherals.pins.gpio2).expect("msg"); + let row_1: PinDriver = PinDriver::input(peripherals.pins.gpio3).expect("msg"); + let row_2: PinDriver = PinDriver::input(peripherals.pins.gpio10).expect("msg"); + let row_3: PinDriver = PinDriver::input(peripherals.pins.gpio6).expect("msg"); + let row_4: PinDriver = PinDriver::input(peripherals.pins.gpio7).expect("msg"); + + let col_0: PinDriver = PinDriver::input(peripherals.pins.gpio0).expect("msg"); + let col_1: PinDriver = PinDriver::input(peripherals.pins.gpio1).expect("msg"); + let col_2: PinDriver = PinDriver::input(peripherals.pins.gpio12).expect("msg"); + let col_3: PinDriver = PinDriver::input(peripherals.pins.gpio18).expect("msg"); + let col_4: PinDriver = PinDriver::input(peripherals.pins.gpio19).expect("msg"); + let col_5: PinDriver = PinDriver::input(peripherals.pins.gpio13).expect("msg"); - let mut has_key_been_pressed_this_cycle = false; - let mut previous_key_pressed: (u8, u8) = (0, 0); + let row_vec = vec![ + Rows::Row0(row_0), + Rows::Row1(row_1), + Rows::Row2(row_2), + Rows::Row3(row_3), + Rows::Row4(row_4), + ]; + + let col_vec = vec![ + Cols::Col0(col_0), + Cols::Col1(col_1), + Cols::Col2(col_2), + Cols::Col3(col_3), + Cols::Col4(col_4), + Cols::Col5(col_5), + ]; + + let mut keyboard = KeyboardLeftSide::new(); + + keyboard.initialize_hashmap(); + + let mut key_pressed = (-1, -1); loop { - /* Implement hardware matrix scan */ - let key_pressed_matrix: Option<(u8, u8)> = Some((0, 0)); + let start_timestamp = Utc::now().timestamp_millis(); - match key_pressed_matrix { - Some(key_pressed) => { - if let Some(key) = keyboard_left_side.key.get_mut(&key_pressed) { - *key = true; - previous_key_pressed = key_pressed; - has_key_been_pressed_this_cycle = true; - log::info!("Key pressed: {:?}", key_pressed); + for row in &row_vec { + match row { + Rows::Row0(key) => { + if key.is_high() { + key_pressed.0 = 2; + } + } + Rows::Row1(key) => { + if key.is_high() { + key_pressed.0 = 3; + } + } + Rows::Row2(key) => { + if key.is_high() { + key_pressed.0 = 10; + } + } + Rows::Row3(key) => { + if key.is_high() { + key_pressed.0 = 6; + } + } + Rows::Row4(key) => { + if key.is_high() { + key_pressed.0 = 7; + } } } + } - /* If a key is not pressed, set the previous key to false */ - None => { - if has_key_been_pressed_this_cycle { - keyboard_left_side.key.insert(previous_key_pressed, false); - has_key_been_pressed_this_cycle = false; + for col in &col_vec { + match col { + Cols::Col0(key) => { + if key.is_high() { + key_pressed.1 = 0; + } + } + Cols::Col1(key) => { + if key.is_high() { + key_pressed.1 = 1; + } + } + Cols::Col2(key) => { + if key.is_high() { + key_pressed.1 = 12; + } + } + Cols::Col3(key) => { + if key.is_high() { + key_pressed.1 = 18; + } + } + Cols::Col4(key) => { + if key.is_high() { + key_pressed.1 = 19; + } + } + Cols::Col5(key) => { + if key.is_high() { + key_pressed.1 = 13; + } } } } - /* Sleep for 20 milliseconds before fetching the matrix */ - sleep(Duration::from_millis(20)); + if let Some(key_valid) = keyboard.key.get(&key_pressed) { + log::info!("Key pressed: {:?}", *key_valid); + + key_pressed = (-1, -1); + } + + let end_timestamp = Utc::now().timestamp_millis(); + log::info!("Total time taken: {}ms", end_timestamp - start_timestamp); + + FreeRtos::delay_ms(100); } + + // let rows = vec![ + // Rows::Row0(PinDriver::input(peripherals.pins.gpio2).expect("msg")), + // Rows::Row1(PinDriver::input(peripherals.pins.gpio3).expect("msg")), + // Rows::Row2(PinDriver::input(peripherals.pins.gpio10).expect("msg")), + // Rows::Row3(PinDriver::input(peripherals.pins.gpio6).expect("msg")), + // Rows::Row4(PinDriver::input(peripherals.pins.gpio7).expect("msg")), + // ]; + + // let cols = vec![ + // Cols::Col0(PinDriver::input(peripherals.pins.gpio0).expect("msg")), + // Cols::Col1(PinDriver::input(peripherals.pins.gpio1).expect("msg")), + // Cols::Col2(PinDriver::input(peripherals.pins.gpio12).expect("msg")), + // Cols::Col3(PinDriver::input(peripherals.pins.gpio18).expect("msg")), + // Cols::Col4(PinDriver::input(peripherals.pins.gpio19).expect("msg")), + // Cols::Col5(PinDriver::input(peripherals.pins.gpio13).expect("msg")), + // ]; + + // let col_0 = PinDriver::input(peripherals.pins.gpio0).expect("msg"); + // let col_1 = PinDriver::input(peripherals.pins.gpio1).expect("msg"); + // let col_2 = PinDriver::input(peripherals.pins.gpio12).expect("msg"); + // let col_3 = PinDriver::input(peripherals.pins.gpio18).expect("msg"); + // let col_4 = PinDriver::input(peripherals.pins.gpio19).expect("msg"); + // let col_5 = PinDriver::input(peripherals.pins.gpio13).expect("msg"); + + // let row_0 = PinDriver::input(peripherals.pins.gpio2).expect("msg"); + // let row_1 = PinDriver::input(peripherals.pins.gpio3).expect("msg"); + // let row_2 = PinDriver::input(peripherals.pins.gpio10).expect("msg"); + // let row_3 = PinDriver::input(peripherals.pins.gpio6).expect("msg"); + // let row_4 = PinDriver::input(peripherals.pins.gpio7).expect("msg"); + + // let mut rows = Vec::new(); + + // let mut col_vec = vec![col_0, col_1, col_2, col_3, col_4, col_5]; + // let mut row_vec = vec![row_0, row_1, row_2, row_3, row_4]; + + // let mut keyboard = KeyboardLeftSide::new(); + + // keyboard.initialize_hashmap(); + + // loop { + // // if row_0.is_high() && col_0.is_high() { + // // log::info!("ESC is pressed: PIN: {:?}", keyboard.key.keys()); + // // } + + // FreeRtos::delay_ms(100); + // } + // /* Declare keys */ + // let mut keyboard_left_side = KeyboardLeftSide::new(); + // keyboard_left_side.initialize_keys(); + + // let mut has_key_been_pressed_this_cycle = false; + // let mut previous_key_pressed: (u8, u8) = (0, 0); + + // loop { + // /* Implement hardware matrix scan */ + // let key_pressed_matrix: Option<(u8, u8)> = Some((0, 0)); + + // match key_pressed_matrix { + // Some(key_pressed) => { + // if let Some(key) = keyboard_left_side.key.get_mut(&key_pressed) { + // *key = true; + // previous_key_pressed = key_pressed; + // has_key_been_pressed_this_cycle = true; + // // log::info!("Key pressed: {:?}", key_pressed); + + // if key_pin.is_low() { + // log::info!("Key is HIGH") + // } else { + // log::info!("Key is LOW") + // } + // } + // } + + // /* If a key is not pressed, set the previous key to false */ + // None => { + // if has_key_been_pressed_this_cycle { + // keyboard_left_side.key.insert(previous_key_pressed, false); + // has_key_been_pressed_this_cycle = false; + // } + // } + // } + + // /* Sleep for 20 milliseconds before fetching the matrix */ + // FreeRtos::delay_ms(20); + // } }