diff --git a/cardputer-bsc/src/keyboard.rs b/cardputer-bsc/src/keyboard.rs index 3c6c95e..63a71b1 100644 --- a/cardputer-bsc/src/keyboard.rs +++ b/cardputer-bsc/src/keyboard.rs @@ -16,19 +16,19 @@ // Adapted from https://github.com/syurazo/cardputer which is distributed under the following license // MIT License -// +// // Copyright (c) 2024 syurazo -// +// // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. -// +// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -37,10 +37,8 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. - use esp_idf_hal::{ - gpio::{Gpio11, Gpio13, Gpio15, Gpio3, Gpio4, Gpio5, Gpio6, Gpio7, Gpio8, Gpio9}, - gpio::{Input, Level, Output, PinDriver}, + gpio::{Gpio11, Gpio13, Gpio15, Gpio3, Gpio4, Gpio5, Gpio6, Gpio7, Gpio8, Gpio9, Input, InputOutput, Level, Output, PinDriver}, peripheral::Peripheral, }; use esp_idf_sys::EspError; @@ -301,6 +299,8 @@ pub enum KeyboardError { PinDriverError(EspError), #[error("Failed to set pin level: {0}")] SetLevelError(EspError), + #[error("Failed to set pull: {0}")] + SetPullError(EspError), } impl<'a> Keyboard<'a> { @@ -318,7 +318,7 @@ impl<'a> Keyboard<'a> { y5: impl Peripheral

+ 'a, y6: impl Peripheral

+ 'a, ) -> Result { - Ok(Self { + let mut keyboard = Self { addr0: PinDriver::output(a0).map_err(KeyboardError::PinDriverError)?, addr1: PinDriver::output(a1).map_err(KeyboardError::PinDriverError)?, addr2: PinDriver::output(a2).map_err(KeyboardError::PinDriverError)?, @@ -329,7 +329,37 @@ impl<'a> Keyboard<'a> { y4: PinDriver::input(y4).map_err(KeyboardError::PinDriverError)?, y5: PinDriver::input(y5).map_err(KeyboardError::PinDriverError)?, y6: PinDriver::input(y6).map_err(KeyboardError::PinDriverError)?, - }) + }; + keyboard + .y0 + .set_pull(esp_idf_hal::gpio::Pull::Up) + .map_err(KeyboardError::SetPullError)?; + keyboard + .y1 + .set_pull(esp_idf_hal::gpio::Pull::Up) + .map_err(KeyboardError::SetPullError)?; + keyboard + .y2 + .set_pull(esp_idf_hal::gpio::Pull::Up) + .map_err(KeyboardError::SetPullError)?; + keyboard + .y3 + .set_pull(esp_idf_hal::gpio::Pull::Up) + .map_err(KeyboardError::SetPullError)?; + keyboard + .y4 + .set_pull(esp_idf_hal::gpio::Pull::Up) + .map_err(KeyboardError::SetPullError)?; + keyboard + .y5 + .set_pull(esp_idf_hal::gpio::Pull::Up) + .map_err(KeyboardError::SetPullError)?; + keyboard + .y6 + .set_pull(esp_idf_hal::gpio::Pull::Up) + .map_err(KeyboardError::SetPullError)?; + + Ok(keyboard) } /// Scan the keyboard and return the Vector of KeyImprint. @@ -343,17 +373,21 @@ impl<'a> Keyboard<'a> { } } - - impl KeyboardScanner for Keyboard<'_> { type Error = KeyboardError; fn scan_pressed_keytypes(&mut self) -> Result, Self::Error> { let mut keys: Vec = vec![]; for i in 0..8 { - self.addr0.set_level(pin_level!(i & 0b00000001)).map_err(KeyboardError::SetLevelError)?; - self.addr1.set_level(pin_level!(i & 0b00000010)).map_err(KeyboardError::SetLevelError)?; - self.addr2.set_level(pin_level!(i & 0b00000100)).map_err(KeyboardError::SetLevelError)?; + self.addr0 + .set_level(pin_level!(i & 0b00000001)) + .map_err(KeyboardError::SetLevelError)?; + self.addr1 + .set_level(pin_level!(i & 0b00000010)) + .map_err(KeyboardError::SetLevelError)?; + self.addr2 + .set_level(pin_level!(i & 0b00000100)) + .map_err(KeyboardError::SetLevelError)?; let inputs: [Level; 7] = [ self.y0.get_level(), @@ -423,7 +457,10 @@ pub struct KeyboardState { impl KeyboardState { /// Get the latest key state and update the Pressed/Released state - pub fn update(&mut self, keyboard: &mut impl KeyboardScanner) -> Result<(), KeyboardError> { + pub fn update( + &mut self, + keyboard: &mut impl KeyboardScanner, + ) -> Result<(), KeyboardError> { let mut new_hold_keys: Vec = Vec::new(); self.pressed_keys.clear(); @@ -497,4 +534,4 @@ impl KeyboardState { pub fn is_alt_pressed(&self) -> bool { self.is_alt_pressed } -} \ No newline at end of file +}