mirror of
https://github.com/Polprzewodnikowy/SummerCart64.git
synced 2024-11-25 15:16:53 +01:00
little cleanup
This commit is contained in:
parent
b54fcad8bc
commit
3b54b6f8bc
@ -47,6 +47,8 @@ mod fatfs {
|
|||||||
NotEnoughCore,
|
NotEnoughCore,
|
||||||
TooManyOpenFiles,
|
TooManyOpenFiles,
|
||||||
InvalidParameter,
|
InvalidParameter,
|
||||||
|
DriverInstalled,
|
||||||
|
DriverNotInstalled,
|
||||||
Unknown,
|
Unknown,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,6 +74,8 @@ mod fatfs {
|
|||||||
Self::NotEnoughCore => "LFN working buffer could not be allocated",
|
Self::NotEnoughCore => "LFN working buffer could not be allocated",
|
||||||
Self::TooManyOpenFiles => "Number of open files > FF_FS_LOCK",
|
Self::TooManyOpenFiles => "Number of open files > FF_FS_LOCK",
|
||||||
Self::InvalidParameter => "Given parameter is invalid",
|
Self::InvalidParameter => "Given parameter is invalid",
|
||||||
|
Self::DriverInstalled => "FatFs driver is already installed",
|
||||||
|
Self::DriverNotInstalled => "FatFs driver is not installed",
|
||||||
Self::Unknown => "Unknown error",
|
Self::Unknown => "Unknown error",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -232,31 +236,21 @@ pub fn run<F: FnOnce(&mut FF) -> Result<(), super::Error>>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn install_driver(driver: impl FFDriver + 'static) -> Result<(), Error> {
|
fn install_driver(driver: impl FFDriver + 'static) -> Result<(), Error> {
|
||||||
match unsafe { DRIVER.lock() } {
|
let mut d = unsafe { DRIVER.lock().unwrap() };
|
||||||
Ok(mut d) => {
|
if d.is_some() {
|
||||||
if d.is_none() {
|
return Err(Error::DriverInstalled);
|
||||||
|
}
|
||||||
d.replace(Box::new(driver));
|
d.replace(Box::new(driver));
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
|
||||||
Err(Error::IntErr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(_) => Err(Error::IntErr),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn uninstall_driver() -> Result<(), Error> {
|
fn uninstall_driver() -> Result<(), Error> {
|
||||||
match unsafe { DRIVER.lock() } {
|
let mut d = unsafe { DRIVER.lock().unwrap() };
|
||||||
Ok(mut d) => {
|
if d.is_none() {
|
||||||
if let Some(mut d) = d.take() {
|
return Err(Error::DriverNotInstalled);
|
||||||
d.deinit();
|
}
|
||||||
|
d.take().unwrap().deinit();
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
|
||||||
Err(Error::IntErr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(_) => Err(Error::IntErr),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum IOCtl {
|
pub enum IOCtl {
|
||||||
@ -337,10 +331,8 @@ unsafe extern "C" fn disk_status(pdrv: fatfs::BYTE) -> fatfs::DSTATUS {
|
|||||||
if pdrv != 0 {
|
if pdrv != 0 {
|
||||||
return fatfs::DSTATUS_STA_NOINIT;
|
return fatfs::DSTATUS_STA_NOINIT;
|
||||||
}
|
}
|
||||||
if let Ok(mut driver) = DRIVER.lock() {
|
if let Some(d) = DRIVER.lock().unwrap().as_mut() {
|
||||||
if let Some(driver) = driver.as_mut() {
|
return d.status();
|
||||||
return driver.status();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
fatfs::DSTATUS_STA_NOINIT
|
fatfs::DSTATUS_STA_NOINIT
|
||||||
}
|
}
|
||||||
@ -350,10 +342,8 @@ unsafe extern "C" fn disk_initialize(pdrv: fatfs::BYTE) -> fatfs::DSTATUS {
|
|||||||
if pdrv != 0 {
|
if pdrv != 0 {
|
||||||
return fatfs::DSTATUS_STA_NOINIT;
|
return fatfs::DSTATUS_STA_NOINIT;
|
||||||
}
|
}
|
||||||
if let Ok(mut driver) = DRIVER.lock() {
|
if let Some(d) = DRIVER.lock().unwrap().as_mut() {
|
||||||
if let Some(driver) = driver.as_mut() {
|
return d.init();
|
||||||
return driver.init();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
fatfs::DSTATUS_STA_NOINIT
|
fatfs::DSTATUS_STA_NOINIT
|
||||||
}
|
}
|
||||||
@ -368,18 +358,13 @@ unsafe extern "C" fn disk_read(
|
|||||||
if pdrv != 0 {
|
if pdrv != 0 {
|
||||||
return fatfs::DRESULT_RES_PARERR;
|
return fatfs::DRESULT_RES_PARERR;
|
||||||
}
|
}
|
||||||
if let Ok(mut driver) = DRIVER.lock() {
|
if let Some(d) = DRIVER.lock().unwrap().as_mut() {
|
||||||
if let Some(driver) = driver.as_mut() {
|
return d.read(
|
||||||
return driver.read(
|
&mut *std::ptr::slice_from_raw_parts_mut(buff, (count as usize) * SD_CARD_SECTOR_SIZE),
|
||||||
&mut *std::ptr::slice_from_raw_parts_mut(
|
|
||||||
buff,
|
|
||||||
(count as usize) * SD_CARD_SECTOR_SIZE,
|
|
||||||
),
|
|
||||||
sector,
|
sector,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
fatfs::DRESULT_RES_NOTRDY
|
||||||
fatfs::DRESULT_RES_ERROR
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
@ -392,15 +377,13 @@ unsafe extern "C" fn disk_write(
|
|||||||
if pdrv != 0 {
|
if pdrv != 0 {
|
||||||
return fatfs::DRESULT_RES_PARERR;
|
return fatfs::DRESULT_RES_PARERR;
|
||||||
}
|
}
|
||||||
if let Ok(mut driver) = DRIVER.lock() {
|
if let Some(d) = DRIVER.lock().unwrap().as_mut() {
|
||||||
if let Some(driver) = driver.as_mut() {
|
return d.write(
|
||||||
return driver.write(
|
|
||||||
&*std::ptr::slice_from_raw_parts(buff, (count as usize) * SD_CARD_SECTOR_SIZE),
|
&*std::ptr::slice_from_raw_parts(buff, (count as usize) * SD_CARD_SECTOR_SIZE),
|
||||||
sector,
|
sector,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
fatfs::DRESULT_RES_NOTRDY
|
||||||
fatfs::DRESULT_RES_ERROR
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
@ -420,9 +403,8 @@ unsafe extern "C" fn disk_ioctl(
|
|||||||
fatfs::CTRL_TRIM => IOCtl::Trim,
|
fatfs::CTRL_TRIM => IOCtl::Trim,
|
||||||
_ => return fatfs::DRESULT_RES_PARERR,
|
_ => return fatfs::DRESULT_RES_PARERR,
|
||||||
};
|
};
|
||||||
if let Ok(mut driver) = DRIVER.lock() {
|
if let Some(d) = DRIVER.lock().unwrap().as_mut() {
|
||||||
if let Some(driver) = driver.as_mut() {
|
let result = d.ioctl(&mut ioctl);
|
||||||
let result = driver.ioctl(&mut ioctl);
|
|
||||||
if result == fatfs::DRESULT_RES_OK {
|
if result == fatfs::DRESULT_RES_OK {
|
||||||
match ioctl {
|
match ioctl {
|
||||||
IOCtl::GetSectorCount(count) => {
|
IOCtl::GetSectorCount(count) => {
|
||||||
@ -439,8 +421,7 @@ unsafe extern "C" fn disk_ioctl(
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
fatfs::DRESULT_RES_NOTRDY
|
||||||
fatfs::DRESULT_RES_ERROR
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
@ -485,9 +466,11 @@ impl PartialOrd for EntryInfo {
|
|||||||
|
|
||||||
impl std::fmt::Display for EntryInfo {
|
impl std::fmt::Display for EntryInfo {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
let size_formatter = |size: u64| -> String {
|
Ok(match self {
|
||||||
|
EntryInfo::Directory => f.write_fmt(format_args!("d ----",))?,
|
||||||
|
EntryInfo::File { size } => {
|
||||||
const UNITS: [&str; 4] = ["K", "M", "G", "T"];
|
const UNITS: [&str; 4] = ["K", "M", "G", "T"];
|
||||||
let mut reduced_size = size as f64;
|
let mut reduced_size = *size as f64;
|
||||||
let mut reduced_unit = "B";
|
let mut reduced_unit = "B";
|
||||||
for unit in UNITS {
|
for unit in UNITS {
|
||||||
if reduced_size >= 1000.0 {
|
if reduced_size >= 1000.0 {
|
||||||
@ -497,16 +480,12 @@ impl std::fmt::Display for EntryInfo {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if size >= 1000 && reduced_size <= 9.9 {
|
let formatted_size = if *size >= 1000 && reduced_size <= 9.9 {
|
||||||
format!("{:>1.1}{}", reduced_size, reduced_unit)
|
format!("{:>1.1}{}", reduced_size, reduced_unit)
|
||||||
} else {
|
} else {
|
||||||
format!("{:>3.0}{}", reduced_size, reduced_unit)
|
format!("{:>3.0}{}", reduced_size, reduced_unit)
|
||||||
}
|
|
||||||
};
|
};
|
||||||
Ok(match self {
|
f.write_fmt(format_args!("f {formatted_size}"))?
|
||||||
EntryInfo::Directory => f.write_fmt(format_args!("d ----",))?,
|
|
||||||
EntryInfo::File { size } => {
|
|
||||||
f.write_fmt(format_args!("f {}", size_formatter(*size),))?
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user