mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
add keypress handling and no_std Cursor implementation
This commit is contained in:
parent
d9709997fb
commit
9a839e3fe6
1 changed files with 202 additions and 26 deletions
|
|
@ -1,17 +1,36 @@
|
|||
use core::fmt::Display;
|
||||
|
||||
use cramium_hal::iox::{IoGpio, IoSetup, Iox, IoxPort, IoxValue};
|
||||
use cramium_hal::minigfx::{FrameBuffer, Point};
|
||||
use cramium_hal::sh1107::Mono;
|
||||
use cramium_hal::udma;
|
||||
use simple_fatfs::io::{IOBase, Read, Seek, SeekFrom, Write};
|
||||
use simple_fatfs::{IOError, IOErrorKind, PathBuf};
|
||||
use utralib::generated::*;
|
||||
|
||||
use crate::platform::cramium::gfx;
|
||||
use crate::platform::cramium::usb;
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
pub enum KeyPress {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right,
|
||||
Select,
|
||||
Start,
|
||||
A,
|
||||
B,
|
||||
Invalid,
|
||||
None,
|
||||
}
|
||||
|
||||
pub fn scan_keyboard<T: IoSetup + IoGpio>(
|
||||
iox: &T,
|
||||
rows: &[(IoxPort, u8)],
|
||||
cols: &[(IoxPort, u8)],
|
||||
) -> [Option<(u8, u8)>; 4] {
|
||||
let mut key_presses: [Option<(u8, u8)>; 4] = [None; 4];
|
||||
) -> [KeyPress; 4] {
|
||||
let mut key_presses: [KeyPress; 4] = [KeyPress::None; 4];
|
||||
let mut key_press_index = 0; // no Vec in no_std, so we have to manually track it
|
||||
|
||||
for (row, (port, pin)) in rows.iter().enumerate() {
|
||||
|
|
@ -19,7 +38,17 @@ pub fn scan_keyboard<T: IoSetup + IoGpio>(
|
|||
for (col, (col_port, col_pin)) in cols.iter().enumerate() {
|
||||
if iox.get_gpio_pin_value(*col_port, *col_pin) == IoxValue::Low {
|
||||
if key_press_index < key_presses.len() {
|
||||
key_presses[key_press_index] = Some((row as u8, col as u8));
|
||||
key_presses[key_press_index] = match (row, col) {
|
||||
(0, 2) => KeyPress::Select,
|
||||
(2, 1) => KeyPress::Start,
|
||||
(1, 2) => KeyPress::Left,
|
||||
(1, 1) => KeyPress::Up,
|
||||
(0, 1) => KeyPress::Down,
|
||||
(2, 0) => KeyPress::Right,
|
||||
(0, 0) => KeyPress::A,
|
||||
(1, 0) => KeyPress::B,
|
||||
_ => KeyPress::Invalid,
|
||||
};
|
||||
key_press_index += 1;
|
||||
}
|
||||
}
|
||||
|
|
@ -56,35 +85,182 @@ pub fn process_update(perclk: u32) {
|
|||
while !key_pressed {
|
||||
let kps = scan_keyboard(&iox, &rows, &cols);
|
||||
for kp in kps {
|
||||
match kp {
|
||||
// SELECT
|
||||
Some((0, 2)) => {
|
||||
crate::println!("SELECT detected");
|
||||
do_update = true;
|
||||
key_pressed = true;
|
||||
}
|
||||
// START
|
||||
Some((2, 1)) => {
|
||||
crate::println!("START detected");
|
||||
key_pressed = true;
|
||||
}
|
||||
Some((r, c)) => {
|
||||
crate::println!("{},{} pressed", r, c);
|
||||
// this causes the system to boot if *any* key is pressed.
|
||||
key_pressed = true;
|
||||
}
|
||||
None => (),
|
||||
if kp != KeyPress::None {
|
||||
crate::println!("Got key: {:?}", kp);
|
||||
key_pressed = true;
|
||||
}
|
||||
if kp == KeyPress::Select {
|
||||
do_update = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if do_update {
|
||||
update();
|
||||
crate::platform::cramium::usb::init_usb();
|
||||
// it's all unsafe because USB is global mutable state
|
||||
unsafe {
|
||||
if let Some(ref mut usb_ref) = crate::platform::cramium::usb::USB {
|
||||
let usb = &mut *core::ptr::addr_of_mut!(*usb_ref);
|
||||
usb.reset();
|
||||
let mut poweron = 0;
|
||||
loop {
|
||||
usb.udc_handle_interrupt();
|
||||
if usb.pp() {
|
||||
poweron += 1; // .pp() is a sham. MPW has no way to tell if power is applied. This needs to be fixed for NTO.
|
||||
}
|
||||
crate::platform::delay(100);
|
||||
if poweron >= 4 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
usb.reset();
|
||||
usb.init();
|
||||
usb.start();
|
||||
usb.update_current_speed();
|
||||
|
||||
crate::println!("hw started...");
|
||||
|
||||
loop {
|
||||
let kps = scan_keyboard(&iox, &rows, &cols);
|
||||
// only consider the first key returned in case of multi-key hit, for simplicity
|
||||
if kps[0] == KeyPress::Select {
|
||||
break;
|
||||
} else if kps[0] != KeyPress::None {
|
||||
crate::println!("Got key {:?}; ignoring", kps[0]);
|
||||
}
|
||||
}
|
||||
|
||||
let disk = usb::conjure_disk();
|
||||
let mut cursor = SliceCursor::new(disk);
|
||||
|
||||
// We can either pass by value of by (mutable) reference
|
||||
let mut fs = simple_fatfs::FileSystem::from_storage(&mut cursor).unwrap();
|
||||
match fs.read_dir(PathBuf::from("/")) {
|
||||
Ok(dir) => {
|
||||
for entry in dir {
|
||||
crate::println!("file: {:?}", entry);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
crate::println!("Couldn't list dir: {:?}", e);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
crate::println!("USB core not allocated, can't do update!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn update() {
|
||||
crate::platform::cramium::usb::init_usb();
|
||||
unsafe {
|
||||
crate::platform::cramium::usb::test_usb();
|
||||
pub struct SliceCursor<'a> {
|
||||
slice: &'a mut [u8],
|
||||
pos: u64,
|
||||
}
|
||||
|
||||
impl<'a> SliceCursor<'a> {
|
||||
pub fn new(slice: &'a mut [u8]) -> Self { Self { slice, pos: 0 } }
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
||||
pub enum IOErrorFatfs {
|
||||
UnexpectedEof,
|
||||
Interrupted,
|
||||
InvalidData,
|
||||
Description,
|
||||
SeekUnderflow,
|
||||
SeekOverflow,
|
||||
SeekOutOfBounds,
|
||||
}
|
||||
|
||||
impl Display for IOErrorFatfs {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
match self {
|
||||
IOErrorFatfs::UnexpectedEof => write!(f, "Unexpected EOF"),
|
||||
IOErrorFatfs::Interrupted => write!(f, "Interrupted"),
|
||||
IOErrorFatfs::InvalidData => write!(f, "Invalid Data"),
|
||||
IOErrorFatfs::Description => write!(f, "Unsupported string description"),
|
||||
IOErrorFatfs::SeekOutOfBounds => write!(f, "Seek out of bounds"),
|
||||
IOErrorFatfs::SeekOverflow => write!(f, "Seek overflow"),
|
||||
IOErrorFatfs::SeekUnderflow => write!(f, "Seek underflow"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for IOErrorFatfs {
|
||||
fn from(_value: &str) -> Self { IOErrorFatfs::Description }
|
||||
}
|
||||
impl IOErrorKind for IOErrorFatfs {
|
||||
fn new_unexpected_eof() -> Self { Self::UnexpectedEof }
|
||||
|
||||
fn new_invalid_data() -> Self { Self::InvalidData }
|
||||
|
||||
fn new_interrupted() -> Self { Self::Interrupted }
|
||||
}
|
||||
impl IOError for IOErrorFatfs {
|
||||
type Kind = IOErrorFatfs;
|
||||
|
||||
fn new<M>(kind: Self::Kind, _msg: M) -> Self
|
||||
where
|
||||
M: core::fmt::Display,
|
||||
{
|
||||
kind
|
||||
}
|
||||
|
||||
fn kind(&self) -> Self::Kind { *self }
|
||||
}
|
||||
|
||||
impl simple_fatfs::Error for IOErrorFatfs {}
|
||||
|
||||
impl IOBase for SliceCursor<'_> {
|
||||
type Error = IOErrorFatfs;
|
||||
}
|
||||
|
||||
impl Seek for SliceCursor<'_> {
|
||||
fn seek(&mut self, seek_from: simple_fatfs::io::SeekFrom) -> Result<u64, Self::Error> {
|
||||
let new_pos = match seek_from {
|
||||
SeekFrom::Start(offset) => offset,
|
||||
SeekFrom::End(offset) => {
|
||||
let result = if offset < 0 {
|
||||
self.slice.len().checked_sub((-offset) as usize).ok_or(IOErrorFatfs::SeekUnderflow)?
|
||||
} else {
|
||||
self.slice.len().checked_add(offset as usize).ok_or(IOErrorFatfs::SeekOverflow)?
|
||||
};
|
||||
result as u64
|
||||
}
|
||||
SeekFrom::Current(offset) => {
|
||||
if offset < 0 {
|
||||
self.pos.checked_sub((-offset) as u64).ok_or(IOErrorFatfs::SeekUnderflow)?
|
||||
} else {
|
||||
self.pos.checked_add(offset as u64).ok_or(IOErrorFatfs::SeekUnderflow)?
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if new_pos > self.slice.len() as u64 {
|
||||
Err(IOErrorFatfs::SeekOutOfBounds)
|
||||
} else {
|
||||
self.pos = new_pos;
|
||||
Ok(self.pos)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Read for SliceCursor<'_> {
|
||||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
|
||||
let available = &self.slice[self.pos as usize..];
|
||||
let to_read = buf.len().min(available.len());
|
||||
buf[..to_read].copy_from_slice(&available[..to_read]);
|
||||
self.pos += to_read as u64;
|
||||
Ok(to_read)
|
||||
}
|
||||
}
|
||||
impl Write for SliceCursor<'_> {
|
||||
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
|
||||
let available = &mut self.slice[self.pos as usize..];
|
||||
let to_write = buf.len().min(available.len());
|
||||
available[..to_write].copy_from_slice(&buf[..to_write]);
|
||||
self.pos += to_write as u64;
|
||||
Ok(to_write)
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> Result<(), Self::Error> { Ok(()) }
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue