update to released servicepoint version

This commit is contained in:
Vinzenz Schroeter 2025-05-03 17:57:55 +02:00
parent 10ebccdf47
commit bdfe4439a1
5 changed files with 137 additions and 119 deletions

View file

@ -1,6 +1,6 @@
use crate::{heap_drop, heap_move, heap_move_nonnull, heap_remove, ByteSlice};
use servicepoint::{
BinaryOperation, BitVecCommand, BitVecU8Msb0, CompressionCode, Packet,
BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Packet,
};
use std::ptr::NonNull;
@ -12,7 +12,7 @@ use std::ptr::NonNull;
/// sp_bitvec_set(vec, 5, true);
/// sp_bitvec_free(vec);
/// ```
pub struct SPBitVec(pub(crate) BitVecU8Msb0);
pub struct SPBitVec(pub(crate) DisplayBitVec);
impl Clone for SPBitVec {
fn clone(&self) -> Self {
@ -33,7 +33,7 @@ impl Clone for SPBitVec {
/// - when `size` is not divisible by 8.
#[no_mangle]
pub unsafe extern "C" fn sp_bitvec_new(size: usize) -> NonNull<SPBitVec> {
heap_move_nonnull(SPBitVec(BitVecU8Msb0::repeat(false, size)))
heap_move_nonnull(SPBitVec(DisplayBitVec::repeat(false, size)))
}
/// Interpret the data as a series of bits and load then into a new [SPBitVec] instance.
@ -42,7 +42,7 @@ pub unsafe extern "C" fn sp_bitvec_new(size: usize) -> NonNull<SPBitVec> {
#[no_mangle]
pub unsafe extern "C" fn sp_bitvec_load(data: ByteSlice) -> NonNull<SPBitVec> {
let data = unsafe { data.as_slice() };
heap_move_nonnull(SPBitVec(BitVecU8Msb0::from_slice(data)))
heap_move_nonnull(SPBitVec(DisplayBitVec::from_slice(data)))
}
/// Clones a [SPBitVec].

View file

@ -32,9 +32,10 @@ pub use crate::byte_slice::*;
pub use crate::char_grid::*;
pub use crate::cp437_grid::*;
pub use crate::packet::*;
pub use crate::typed_command::*;
pub use crate::udp::*;
pub use servicepoint::CommandCode;
use std::ptr::NonNull;
pub use typed_command::*;
mod bitmap;
mod bitvec;
@ -44,13 +45,8 @@ mod char_grid;
mod cp437_grid;
mod packet;
mod typed_command;
#[cfg(feature = "protocol_udp")]
mod udp;
#[cfg(feature = "protocol_udp")]
pub use udp::*;
use std::time::Duration;
/// Actual hardware limit is around 28-29ms/frame. Rounded up for less dropped packets.

View file

@ -1,7 +1,7 @@
use crate::{heap_drop, heap_move, heap_remove};
use servicepoint::{Connection, Header, Packet, TypedCommand, UdpConnection};
use servicepoint::{Header, Packet, TypedCommand, UdpSocketExt};
use std::ffi::{c_char, CStr};
use std::net::{Ipv4Addr, SocketAddrV4};
use std::net::{Ipv4Addr, SocketAddrV4, UdpSocket};
use std::ptr::NonNull;
/// Creates a new instance of [UdpConnection].
@ -16,13 +16,11 @@ use std::ptr::NonNull;
/// sp_udp_send_command(connection, sp_command_clear());
/// ```
#[no_mangle]
pub unsafe extern "C" fn sp_udp_open(
host: NonNull<c_char>,
) -> *mut UdpConnection {
pub unsafe extern "C" fn sp_udp_open(host: NonNull<c_char>) -> *mut UdpSocket {
let host = unsafe { CStr::from_ptr(host.as_ptr()) }
.to_str()
.expect("Bad encoding");
let connection = match UdpConnection::open(host) {
let connection = match UdpSocket::bind_connect(host) {
Err(_) => return std::ptr::null_mut(),
Ok(value) => value,
};
@ -48,9 +46,9 @@ pub unsafe extern "C" fn sp_udp_open_ipv4(
ip3: u8,
ip4: u8,
port: u16,
) -> *mut UdpConnection {
) -> *mut UdpSocket {
let addr = SocketAddrV4::new(Ipv4Addr::from([ip1, ip2, ip3, ip4]), port);
let connection = match UdpConnection::open(addr) {
let connection = match UdpSocket::bind_connect(addr) {
Err(_) => return std::ptr::null_mut(),
Ok(value) => value,
};
@ -64,11 +62,11 @@ pub unsafe extern "C" fn sp_udp_open_ipv4(
/// returns: true in case of success
#[no_mangle]
pub unsafe extern "C" fn sp_udp_send_packet(
connection: NonNull<UdpConnection>,
connection: NonNull<UdpSocket>,
packet: NonNull<Packet>,
) -> bool {
let packet = unsafe { heap_remove(packet) };
unsafe { connection.as_ref().send(packet) }.is_ok()
unsafe { connection.as_ref().send(&Vec::from(packet)) }.is_ok()
}
/// Sends a [TypedCommand] to the display using the [UdpConnection].
@ -84,11 +82,11 @@ pub unsafe extern "C" fn sp_udp_send_packet(
/// ```
#[no_mangle]
pub unsafe extern "C" fn sp_udp_send_command(
connection: NonNull<UdpConnection>,
connection: NonNull<UdpSocket>,
command: NonNull<TypedCommand>,
) -> bool {
let command = unsafe { heap_remove(command) };
unsafe { connection.as_ref().send(command) }.is_ok()
unsafe { connection.as_ref().send_command(command) }.is_some()
}
/// Sends a [Header] to the display using the [UdpConnection].
@ -102,18 +100,20 @@ pub unsafe extern "C" fn sp_udp_send_command(
/// ```
#[no_mangle]
pub unsafe extern "C" fn sp_udp_send_header(
udp_connection: NonNull<UdpConnection>,
udp_connection: NonNull<UdpSocket>,
header: Header,
) -> bool {
let packet = Packet {
header,
payload: vec![],
};
unsafe { udp_connection.as_ref() }.send(packet).is_ok()
unsafe { udp_connection.as_ref() }
.send(&Vec::from(packet))
.is_ok()
}
/// Closes and deallocates a [UdpConnection].
#[no_mangle]
pub unsafe extern "C" fn sp_udp_free(connection: NonNull<UdpConnection>) {
pub unsafe extern "C" fn sp_udp_free(connection: NonNull<UdpSocket>) {
unsafe { heap_drop(connection) }
}