Fix reading from keyboard
This commit is contained in:
parent
de3519cfe3
commit
4be3a470c2
1 changed files with 53 additions and 16 deletions
|
|
@ -16,19 +16,19 @@
|
||||||
|
|
||||||
// Adapted from https://github.com/syurazo/cardputer which is distributed under the following license
|
// Adapted from https://github.com/syurazo/cardputer which is distributed under the following license
|
||||||
// MIT License
|
// MIT License
|
||||||
//
|
//
|
||||||
// Copyright (c) 2024 syurazo
|
// Copyright (c) 2024 syurazo
|
||||||
//
|
//
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
// of this software and associated documentation files (the "Software"), to deal
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
// in the Software without restriction, including without limitation the rights
|
// in the Software without restriction, including without limitation the rights
|
||||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
// copies of the Software, and to permit persons to whom the Software is
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
// furnished to do so, subject to the following conditions:
|
// furnished to do so, subject to the following conditions:
|
||||||
//
|
//
|
||||||
// The above copyright notice and this permission notice shall be included in all
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
// copies or substantial portions of the Software.
|
// copies or substantial portions of the Software.
|
||||||
//
|
//
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
// 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
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
// SOFTWARE.
|
// SOFTWARE.
|
||||||
|
|
||||||
|
|
||||||
use esp_idf_hal::{
|
use esp_idf_hal::{
|
||||||
gpio::{Gpio11, Gpio13, Gpio15, Gpio3, Gpio4, Gpio5, Gpio6, Gpio7, Gpio8, Gpio9},
|
gpio::{Gpio11, Gpio13, Gpio15, Gpio3, Gpio4, Gpio5, Gpio6, Gpio7, Gpio8, Gpio9, Input, InputOutput, Level, Output, PinDriver},
|
||||||
gpio::{Input, Level, Output, PinDriver},
|
|
||||||
peripheral::Peripheral,
|
peripheral::Peripheral,
|
||||||
};
|
};
|
||||||
use esp_idf_sys::EspError;
|
use esp_idf_sys::EspError;
|
||||||
|
|
@ -301,6 +299,8 @@ pub enum KeyboardError {
|
||||||
PinDriverError(EspError),
|
PinDriverError(EspError),
|
||||||
#[error("Failed to set pin level: {0}")]
|
#[error("Failed to set pin level: {0}")]
|
||||||
SetLevelError(EspError),
|
SetLevelError(EspError),
|
||||||
|
#[error("Failed to set pull: {0}")]
|
||||||
|
SetPullError(EspError),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Keyboard<'a> {
|
impl<'a> Keyboard<'a> {
|
||||||
|
|
@ -318,7 +318,7 @@ impl<'a> Keyboard<'a> {
|
||||||
y5: impl Peripheral<P = Gpio6> + 'a,
|
y5: impl Peripheral<P = Gpio6> + 'a,
|
||||||
y6: impl Peripheral<P = Gpio7> + 'a,
|
y6: impl Peripheral<P = Gpio7> + 'a,
|
||||||
) -> Result<Self, KeyboardError> {
|
) -> Result<Self, KeyboardError> {
|
||||||
Ok(Self {
|
let mut keyboard = Self {
|
||||||
addr0: PinDriver::output(a0).map_err(KeyboardError::PinDriverError)?,
|
addr0: PinDriver::output(a0).map_err(KeyboardError::PinDriverError)?,
|
||||||
addr1: PinDriver::output(a1).map_err(KeyboardError::PinDriverError)?,
|
addr1: PinDriver::output(a1).map_err(KeyboardError::PinDriverError)?,
|
||||||
addr2: PinDriver::output(a2).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)?,
|
y4: PinDriver::input(y4).map_err(KeyboardError::PinDriverError)?,
|
||||||
y5: PinDriver::input(y5).map_err(KeyboardError::PinDriverError)?,
|
y5: PinDriver::input(y5).map_err(KeyboardError::PinDriverError)?,
|
||||||
y6: PinDriver::input(y6).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.
|
/// Scan the keyboard and return the Vector of KeyImprint.
|
||||||
|
|
@ -343,17 +373,21 @@ impl<'a> Keyboard<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
impl KeyboardScanner for Keyboard<'_> {
|
impl KeyboardScanner for Keyboard<'_> {
|
||||||
type Error = KeyboardError;
|
type Error = KeyboardError;
|
||||||
|
|
||||||
fn scan_pressed_keytypes(&mut self) -> Result<Vec<KeyType>, Self::Error> {
|
fn scan_pressed_keytypes(&mut self) -> Result<Vec<KeyType>, Self::Error> {
|
||||||
let mut keys: Vec<KeyType> = vec![];
|
let mut keys: Vec<KeyType> = vec![];
|
||||||
for i in 0..8 {
|
for i in 0..8 {
|
||||||
self.addr0.set_level(pin_level!(i & 0b00000001)).map_err(KeyboardError::SetLevelError)?;
|
self.addr0
|
||||||
self.addr1.set_level(pin_level!(i & 0b00000010)).map_err(KeyboardError::SetLevelError)?;
|
.set_level(pin_level!(i & 0b00000001))
|
||||||
self.addr2.set_level(pin_level!(i & 0b00000100)).map_err(KeyboardError::SetLevelError)?;
|
.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] = [
|
let inputs: [Level; 7] = [
|
||||||
self.y0.get_level(),
|
self.y0.get_level(),
|
||||||
|
|
@ -423,7 +457,10 @@ pub struct KeyboardState {
|
||||||
|
|
||||||
impl KeyboardState {
|
impl KeyboardState {
|
||||||
/// Get the latest key state and update the Pressed/Released state
|
/// 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();
|
let mut new_hold_keys: Vec<ConversionRule> = Vec::new();
|
||||||
|
|
||||||
self.pressed_keys.clear();
|
self.pressed_keys.clear();
|
||||||
|
|
@ -497,4 +534,4 @@ impl KeyboardState {
|
||||||
pub fn is_alt_pressed(&self) -> bool {
|
pub fn is_alt_pressed(&self) -> bool {
|
||||||
self.is_alt_pressed
|
self.is_alt_pressed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue