format apps/transientdisk

This commit is contained in:
bunnie 2024-01-14 18:56:19 +08:00
parent d3eb53ba89
commit 19ebae2715
2 changed files with 22 additions and 58 deletions

View file

@ -17,13 +17,9 @@ impl FlashDrive {
return Err(FlashDriveError::CapacityNotPageAligned); return Err(FlashDriveError::CapacityNotPageAligned);
} }
let mut backing = xous::syscall::map_memory( let mut backing =
None, xous::syscall::map_memory(None, None, capacity, xous::MemoryFlags::R | xous::MemoryFlags::W)
None, .unwrap();
capacity,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.unwrap();
// initialize backing slice with bogus data. Safety: all `[u32]` // initialize backing slice with bogus data. Safety: all `[u32]`
// values are valid // values are valid
@ -32,18 +28,11 @@ impl FlashDrive {
*d = index as u32; *d = index as u32;
} }
Ok(Self { Ok(Self { capacity, block_size, memory: backing })
capacity,
block_size,
memory: backing,
})
} }
pub fn read(&mut self, msg: &mut MessageEnvelope) { pub fn read(&mut self, msg: &mut MessageEnvelope) {
let body = msg let body = msg.body.memory_message_mut().expect("incorrect message type received");
.body
.memory_message_mut()
.expect("incorrect message type received");
let lba = body.offset.map(|v| v.get()).unwrap_or_default(); let lba = body.offset.map(|v| v.get()).unwrap_or_default();
// Safety: all values of `[u32]` are valid // Safety: all values of `[u32]` are valid
let data = unsafe { body.buf.as_slice_mut::<u8>() }; let data = unsafe { body.buf.as_slice_mut::<u8>() };
@ -52,10 +41,7 @@ impl FlashDrive {
} }
pub fn write(&mut self, msg: &mut MessageEnvelope) { pub fn write(&mut self, msg: &mut MessageEnvelope) {
let body = msg let body = msg.body.memory_message_mut().expect("incorrect message type received");
.body
.memory_message_mut()
.expect("incorrect message type received");
let lba = body.offset.map(|v| v.get()).unwrap_or_default(); let lba = body.offset.map(|v| v.get()).unwrap_or_default();
// Safety: all values of `[u32]` are valid // Safety: all values of `[u32]` are valid
let data = unsafe { body.buf.as_slice_mut::<u8>() }; let data = unsafe { body.buf.as_slice_mut::<u8>() };
@ -85,7 +71,5 @@ impl FlashDrive {
.copy_from_slice(&data[..self.block_size]); .copy_from_slice(&data[..self.block_size]);
} }
fn max_lba_inner(&self) -> u32 { fn max_lba_inner(&self) -> u32 { (self.capacity as u32 / 512) - 1 }
(self.capacity as u32 / 512) - 1
}
} }

View file

@ -1,6 +1,7 @@
#![cfg_attr(target_os = "none", no_std)] #![cfg_attr(target_os = "none", no_std)]
#![cfg_attr(target_os = "none", no_main)] #![cfg_attr(target_os = "none", no_main)]
use core::fmt::Write; use core::fmt::Write;
use graphics_server::api::GlyphStyle; use graphics_server::api::GlyphStyle;
use graphics_server::{DrawStyle, Gid, PixelColor, Point, Rectangle, TextBounds, TextView}; use graphics_server::{DrawStyle, Gid, PixelColor, Point, Rectangle, TextBounds, TextView};
use num_traits::*; use num_traits::*;
@ -53,18 +54,9 @@ impl UI {
.expect("Could not register GAM UX") .expect("Could not register GAM UX")
.unwrap(); .unwrap();
let content = gam let content = gam.request_content_canvas(gam_token).expect("Could not get content canvas");
.request_content_canvas(gam_token) let screensize = gam.get_canvas_bounds(content).expect("Could not get canvas dimensions");
.expect("Could not get content canvas"); Self { gam, _gam_token: gam_token, content, screensize }
let screensize = gam
.get_canvas_bounds(content)
.expect("Could not get canvas dimensions");
Self {
gam,
_gam_token: gam_token,
content,
screensize,
}
} }
/// Clear the entire screen. /// Clear the entire screen.
@ -75,11 +67,7 @@ impl UI {
Rectangle::new_with_style( Rectangle::new_with_style(
Point::new(0, 0), Point::new(0, 0),
self.screensize, self.screensize,
DrawStyle { DrawStyle { fill_color: Some(PixelColor::Light), stroke_color: None, stroke_width: 0 },
fill_color: Some(PixelColor::Light),
stroke_color: None,
stroke_width: 0,
},
), ),
) )
.expect("can't clear content area"); .expect("can't clear content area");
@ -92,10 +80,7 @@ impl UI {
let mut text_view = TextView::new( let mut text_view = TextView::new(
self.content, self.content,
TextBounds::GrowableFromBr( TextBounds::GrowableFromBr(
Point::new( Point::new(self.screensize.x - 45, self.screensize.y - (self.screensize.y / 2)),
self.screensize.x - 45,
self.screensize.y - (self.screensize.y / 2),
),
(self.screensize.x / 5 * 4) as u16, (self.screensize.x / 5 * 4) as u16,
), ),
); );
@ -105,12 +90,9 @@ impl UI {
text_view.clear_area = true; text_view.clear_area = true;
text_view.rounded_border = Some(3); text_view.rounded_border = Some(3);
text_view.style = GlyphStyle::ExtraLarge; text_view.style = GlyphStyle::ExtraLarge;
write!(text_view.text, "1.44mb flash drive now available.") write!(text_view.text, "1.44mb flash drive now available.").expect("Could not write to text view");
.expect("Could not write to text view");
self.gam self.gam.post_textview(&mut text_view).expect("Could not render text view");
.post_textview(&mut text_view)
.expect("Could not render text view");
self.gam.redraw().expect("Could not redraw screen"); self.gam.redraw().expect("Could not redraw screen");
} }
} }
@ -123,17 +105,16 @@ fn main() -> ! {
let xns = xous_names::XousNames::new().unwrap(); let xns = xous_names::XousNames::new().unwrap();
// Register the server with xous // Register the server with xous
let sid = xns let sid = xns.register_name(SERVER_NAME_TRANSIENTDISK, None).expect("can't register server");
.register_name(SERVER_NAME_TRANSIENTDISK, None)
.expect("can't register server");
#[cfg(not(feature = "mass-storage"))] #[cfg(not(feature = "mass-storage"))]
log::warn!("transientdisk has been compiled without the mass-storage feature, needed to make the USB subsystem behave like a mass-storage device."); log::warn!(
"transientdisk has been compiled without the mass-storage feature, needed to make the USB subsystem behave like a mass-storage device."
);
let mut ui = UI::new(&xns, sid); let mut ui = UI::new(&xns, sid);
let mut fd = let mut fd = flash_drive::FlashDrive::new(1445888, 512).expect("cannot create flash drive instance");
flash_drive::FlashDrive::new(1445888, 512).expect("cannot create flash drive instance");
#[cfg(feature = "mass-storage")] #[cfg(feature = "mass-storage")]
let usb = usb_device_xous::UsbHid::new(); let usb = usb_device_xous::UsbHid::new();
@ -181,8 +162,7 @@ fn main() -> ! {
usb.set_block_device_sid(sid.clone()); usb.set_block_device_sid(sid.clone());
usb.switch_to_core(usb_device_xous::UsbDeviceType::MassStorage) usb.switch_to_core(usb_device_xous::UsbDeviceType::MassStorage).unwrap();
.unwrap();
} }
usb_setup = true; usb_setup = true;
} }