Fix reading from keyboard

This commit is contained in:
Zac Wilson 2025-07-06 11:02:29 +01:00
parent de3519cfe3
commit 4be3a470c2

View file

@ -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<P = Gpio6> + 'a,
y6: impl Peripheral<P = Gpio7> + 'a,
) -> Result<Self, KeyboardError> {
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<Vec<KeyType>, Self::Error> {
let mut keys: Vec<KeyType> = 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<Error = KeyboardError>) -> Result<(), KeyboardError> {
pub fn update(
&mut self,
keyboard: &mut impl KeyboardScanner<Error = KeyboardError>,
) -> Result<(), KeyboardError> {
let mut new_hold_keys: Vec<ConversionRule> = Vec::new();
self.pressed_keys.clear();