Skip to content

Commit

Permalink
Merge branch 'main' into system-features
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/system.rs
  • Loading branch information
FloppyDisck committed Oct 29, 2023
2 parents 7f9946a + c944ce6 commit 5ebc2e5
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 100 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ crate-type = ["staticlib", "cdylib"]

[dependencies]
anyhow = { version = "1.0.31", default-features = false }
arrayvec = { version = "0.7.4", default-features = false }
crankstart-sys = { version = "0.1.2", path = "crankstart-sys" }
euclid = { version = "0.22.9", default-features = false, features = [ "libm" ] }
hashbrown = "0.14.0"
heapless = "0.6.1"

[dev-dependencies]
randomize = "3.0.1"
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ crankstart-sys = { path = "../crankstart/crankstart-sys" }
anyhow = { version = "1.0.31", default-features = false }
euclid = { version = "0.22.9", default-features = false, features = [ "libm" ] }
hashbrown = "0.14.0"
heapless = "0.6.1"

[dependencies.cstr_core]
version = "=0.1.2"
Expand Down
9 changes: 4 additions & 5 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl FileSystem {
}

pub fn listfiles(&self, path: &str, show_invisible: bool) -> Result<Vec<String>, Error> {
let mut files: Box<Vec<String>> = Box::new(Vec::new());
let mut files: Box<Vec<String>> = Box::default();
let files_ptr: *mut Vec<String> = &mut *files;
let c_path = CString::new(path).map_err(Error::msg)?;
let result = pd_func_caller!(
Expand Down Expand Up @@ -99,7 +99,7 @@ impl FileSystem {
let c_path = CString::new(path).map_err(Error::msg)?;
let raw_file = pd_func_caller!((*self.0).open, c_path.as_ptr(), options)?;
ensure!(
raw_file != ptr::null_mut(),
!raw_file.is_null(),
"Failed to open file at {} with options {:?}",
path,
options
Expand All @@ -109,11 +109,10 @@ impl FileSystem {

pub fn read_file_as_string(&self, path: &str) -> Result<String, Error> {
let stat = self.stat(path)?;
let mut buffer = Vec::with_capacity(stat.size as usize);
buffer.resize(stat.size as usize, 0);
let mut buffer = alloc::vec![0; stat.size as usize];
let sd_file = self.open(path, FileOptions::kFileRead | FileOptions::kFileReadData)?;
sd_file.read(&mut buffer)?;
Ok(String::from_utf8(buffer).map_err(Error::msg)?)
String::from_utf8(buffer).map_err(Error::msg)
}
}

Expand Down
69 changes: 37 additions & 32 deletions src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl BitmapInner {
width,
height,
rowbytes,
hasmask: mask_ptr != ptr::null_mut(),
hasmask: !mask_ptr.is_null(),
})
}

Expand All @@ -88,7 +88,7 @@ impl BitmapInner {
self.raw_bitmap,
location.x,
location.y,
flip.into(),
flip,
)?;
Ok(())
}
Expand Down Expand Up @@ -153,7 +153,7 @@ impl BitmapInner {
location.y,
size.width,
size.height,
flip.into(),
flip,
)?;
Ok(())
}
Expand Down Expand Up @@ -213,7 +213,7 @@ impl BitmapInner {
self.raw_bitmap,
&mut out_err
)?;
if out_err != ptr::null_mut() {
if !out_err.is_null() {
let err_msg = unsafe { CStr::from_ptr(out_err).to_string_lossy().into_owned() };
Err(anyhow!(err_msg))
} else {
Expand Down Expand Up @@ -360,15 +360,15 @@ fn raw_bitmap(bitmap: OptionalBitmap<'_>) -> *mut crankstart_sys::LCDBitmap {
if let Some(bitmap) = bitmap {
bitmap.inner.borrow().raw_bitmap
} else {
ptr::null_mut() as *mut crankstart_sys::LCDBitmap
ptr::null_mut()
}
}

pub struct Font(*mut crankstart_sys::LCDFont);

impl Font {
pub fn new(font: *mut crankstart_sys::LCDFont) -> Result<Self, Error> {
anyhow::ensure!(font != ptr::null_mut(), "Null pointer passed to Font::new");
anyhow::ensure!(!font.is_null(), "Null pointer passed to Font::new");
Ok(Self(font))
}
}
Expand Down Expand Up @@ -396,7 +396,7 @@ impl BitmapTableInner {
index as c_int
)?;
ensure!(
raw_bitmap != ptr::null_mut(),
!raw_bitmap.is_null(),
"Failed to load bitmap {} from table {:?}",
index,
self.raw_bitmap_table
Expand All @@ -417,7 +417,7 @@ impl BitmapTableInner {
self.raw_bitmap_table,
&mut out_err
)?;
if out_err != ptr::null_mut() {
if !out_err.is_null() {
let err_msg = unsafe { CStr::from_ptr(out_err).to_string_lossy().into_owned() };
Err(anyhow!(err_msg))
} else {
Expand Down Expand Up @@ -483,7 +483,7 @@ impl Graphics {

/// Allows drawing directly into an image rather than the framebuffer, for example for
/// drawing text into a sprite's image.
pub fn with_context<F, T>(&self, bitmap: &mut Bitmap, f: F) -> Result<T, Error>
pub fn with_context<F, T>(&self, bitmap: &Bitmap, f: F) -> Result<T, Error>
where
F: FnOnce() -> Result<T, Error>,
{
Expand All @@ -507,28 +507,22 @@ impl Graphics {

pub fn get_frame(&self) -> Result<&'static mut [u8], Error> {
let ptr = pd_func_caller!((*self.0).getFrame)?;
anyhow::ensure!(
ptr != ptr::null_mut(),
"Null pointer returned from getFrame"
);
anyhow::ensure!(!ptr.is_null(), "Null pointer returned from getFrame");
let frame = unsafe { slice::from_raw_parts_mut(ptr, (LCD_ROWSIZE * LCD_ROWS) as usize) };
Ok(frame)
}

pub fn get_display_frame(&self) -> Result<&'static mut [u8], Error> {
let ptr = pd_func_caller!((*self.0).getDisplayFrame)?;
anyhow::ensure!(
ptr != ptr::null_mut(),
"Null pointer returned from getDisplayFrame"
);
anyhow::ensure!(!ptr.is_null(), "Null pointer returned from getDisplayFrame");
let frame = unsafe { slice::from_raw_parts_mut(ptr, (LCD_ROWSIZE * LCD_ROWS) as usize) };
Ok(frame)
}

pub fn get_debug_bitmap(&self) -> Result<Bitmap, Error> {
let raw_bitmap = pd_func_caller!((*self.0).getDebugBitmap)?;
anyhow::ensure!(
raw_bitmap != ptr::null_mut(),
!raw_bitmap.is_null(),
"Null pointer returned from getDebugImage"
);
Ok(Bitmap::new(raw_bitmap, false))
Expand All @@ -537,14 +531,14 @@ impl Graphics {
pub fn get_framebuffer_bitmap(&self) -> Result<Bitmap, Error> {
let raw_bitmap = pd_func_caller!((*self.0).copyFrameBufferBitmap)?;
anyhow::ensure!(
raw_bitmap != ptr::null_mut(),
!raw_bitmap.is_null(),
"Null pointer returned from getFrameBufferBitmap"
);
Ok(Bitmap::new(raw_bitmap, true))
}

pub fn set_background_color(&self, color: LCDSolidColor) -> Result<(), Error> {
pd_func_caller!((*self.0).setBackgroundColor, color.into())
pd_func_caller!((*self.0).setBackgroundColor, color)
}

pub fn set_draw_mode(&self, mode: LCDBitmapDrawMode) -> Result<(), Error> {
Expand Down Expand Up @@ -572,7 +566,7 @@ impl Graphics {
bg_color.into()
)?;
anyhow::ensure!(
raw_bitmap != ptr::null_mut(),
!raw_bitmap.is_null(),
"Null pointer returned from new_bitmap"
);
Ok(Bitmap::new(raw_bitmap, true))
Expand All @@ -582,8 +576,8 @@ impl Graphics {
let c_path = CString::new(path).map_err(Error::msg)?;
let mut out_err: *const crankstart_sys::ctypes::c_char = ptr::null_mut();
let raw_bitmap = pd_func_caller!((*self.0).loadBitmap, c_path.as_ptr(), &mut out_err)?;
if raw_bitmap == ptr::null_mut() {
if out_err != ptr::null_mut() {
if raw_bitmap.is_null() {
if !out_err.is_null() {
let err_msg = unsafe { CStr::from_ptr(out_err).to_string_lossy().into_owned() };
Err(anyhow!(err_msg))
} else {
Expand Down Expand Up @@ -612,8 +606,8 @@ impl Graphics {
let mut out_err: *const crankstart_sys::ctypes::c_char = ptr::null_mut();
let raw_bitmap_table =
pd_func_caller!((*self.0).loadBitmapTable, c_path.as_ptr(), &mut out_err)?;
if raw_bitmap_table == ptr::null_mut() {
if out_err != ptr::null_mut() {
if raw_bitmap_table.is_null() {
if !out_err.is_null() {
let err_msg = unsafe { CStr::from_ptr(out_err).to_string_lossy().into_owned() };
Err(anyhow!(err_msg))
} else {
Expand Down Expand Up @@ -657,14 +651,13 @@ impl Graphics {
let n_pts = coords.len();
let mut coords_seq = coords
.iter()
.map(|pt| [pt.x, pt.y])
.flatten()
.flat_map(|pt| [pt.x, pt.y])
.collect::<alloc::vec::Vec<_>>();

pd_func_caller!(
(*self.0).fillPolygon,
n_pts as i32,
coords_seq.as_mut_ptr() as *mut i32,
coords_seq.as_mut_ptr(),
color.into(),
fillrule
)?;
Expand Down Expand Up @@ -761,8 +754,20 @@ impl Graphics {

pub fn load_font(&self, path: &str) -> Result<Font, Error> {
let c_path = CString::new(path).map_err(Error::msg)?;
let font = pd_func_caller!((*self.0).loadFont, c_path.as_ptr(), ptr::null_mut())?;
Font::new(font)
let mut out_err: *const crankstart_sys::ctypes::c_char = ptr::null_mut();
let font = pd_func_caller!((*self.0).loadFont, c_path.as_ptr(), &mut out_err)?;
if font.is_null() {
if !out_err.is_null() {
let err_msg = unsafe { CStr::from_ptr(out_err).to_string_lossy().into_owned() };
Err(anyhow!(err_msg))
} else {
Err(anyhow!(
"load_font failed without providing an error message"
))
}
} else {
Font::new(font)
}
}

pub fn set_font(&self, font: &Font) -> Result<(), Error> {
Expand All @@ -775,7 +780,7 @@ impl Graphics {
pd_func_caller!(
(*self.0).drawText,
c_text.as_ptr() as *const core::ffi::c_void,
text.len() as usize,
text.len(),
PDStringEncoding::kUTF8Encoding,
position.x,
position.y,
Expand All @@ -788,7 +793,7 @@ impl Graphics {
(*self.0).getTextWidth,
font.0,
c_text.as_ptr() as *const core::ffi::c_void,
text.len() as usize,
text.len(),
PDStringEncoding::kUTF8Encoding,
tracking,
)
Expand Down
Loading

0 comments on commit 5ebc2e5

Please sign in to comment.