use brightness type from base crate

This commit is contained in:
Vinzenz Schroeter 2025-04-12 13:12:50 +02:00
parent 40fed5ba04
commit 597614bc95
3 changed files with 43 additions and 34 deletions

View file

@ -20,18 +20,10 @@
//! ```
use crate::SPByteSlice;
use servicepoint::{BrightnessGrid, DataRef, Grid};
use std::convert::Into;
use servicepoint::{Brightness, BrightnessGrid, DataRef, Grid};
use std::mem::transmute;
use std::ptr::NonNull;
/// see [servicepoint::Brightness::MIN]
pub const SP_BRIGHTNESS_MIN: u8 = 0;
/// see [servicepoint::Brightness::MAX]
pub const SP_BRIGHTNESS_MAX: u8 = 11;
/// Count of possible brightness values
pub const SP_BRIGHTNESS_LEVELS: u8 = 12;
/// Creates a new [SPBrightnessGrid] with the specified dimensions.
///
/// returns: [SPBrightnessGrid] initialized to 0. Will never return NULL.
@ -165,8 +157,8 @@ pub unsafe extern "C" fn sp_brightness_grid_get(
brightness_grid: NonNull<BrightnessGrid>,
x: usize,
y: usize,
) -> u8 {
unsafe { brightness_grid.as_ref().get(x, y) }.into()
) -> Brightness {
unsafe { brightness_grid.as_ref().get(x, y) }
}
/// Sets the value of the specified position in the [SPBrightnessGrid].
@ -196,11 +188,9 @@ pub unsafe extern "C" fn sp_brightness_grid_set(
brightness_grid: NonNull<BrightnessGrid>,
x: usize,
y: usize,
value: u8,
value: Brightness,
) {
let brightness = servicepoint::Brightness::try_from(value)
.expect("invalid brightness value");
unsafe { (*brightness_grid.as_ptr()).set(x, y, brightness) };
unsafe { (*brightness_grid.as_ptr()).set(x, y, value) };
}
/// Sets the value of all cells in the [SPBrightnessGrid].
@ -224,11 +214,9 @@ pub unsafe extern "C" fn sp_brightness_grid_set(
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_fill(
brightness_grid: NonNull<BrightnessGrid>,
value: u8,
value: Brightness,
) {
let brightness = servicepoint::Brightness::try_from(value)
.expect("invalid brightness value");
unsafe { (*brightness_grid.as_ptr()).fill(brightness) };
unsafe { (*brightness_grid.as_ptr()).fill(value) };
}
/// Gets the width of the [SPBrightnessGrid] instance.
@ -302,8 +290,9 @@ pub unsafe extern "C" fn sp_brightness_grid_height(
pub unsafe extern "C" fn sp_brightness_grid_unsafe_data_ref(
brightness_grid: NonNull<BrightnessGrid>,
) -> SPByteSlice {
assert_eq!(size_of::<servicepoint::Brightness>(), 1);
//noinspection RsAssertEqual
const _: () = assert!(size_of::<Brightness>() == 1);
let data = unsafe { (*brightness_grid.as_ptr()).data_ref_mut() };
// this assumes more about the memory layout than rust guarantees. yikes!
unsafe { SPByteSlice::from_slice(transmute(data)) }
}

View file

@ -3,10 +3,7 @@
//! prefix `sp_command_`
use crate::SPBitVec;
use servicepoint::{
BinaryOperation, Bitmap, BrightnessGrid, CharGrid, CompressionCode,
Cp437Grid, GlobalBrightnessCommand, Packet, TypedCommand,
};
use servicepoint::{BinaryOperation, Bitmap, Brightness, BrightnessGrid, CharGrid, CompressionCode, Cp437Grid, GlobalBrightnessCommand, Packet, TypedCommand};
use std::ptr::NonNull;
/// A low-level display command.
@ -152,10 +149,8 @@ pub unsafe extern "C" fn sp_command_fade_out() -> NonNull<TypedCommand> {
/// by explicitly calling `sp_command_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_brightness(
brightness: u8,
brightness: Brightness,
) -> NonNull<TypedCommand> {
let brightness = servicepoint::Brightness::try_from(brightness)
.expect("invalid brightness");
let result = Box::new(GlobalBrightnessCommand::from(brightness).into());
NonNull::from(Box::leak(result))
}