use crate::utils::ffi;
#[derive(Debug, Clone, Copy)]
pub struct EmergencyMessage {
pub error_code: u16,
pub error_register: u8,
pub data: [u8; 5],
pub slave_index: u16,
pub timestamp_ms: u64,
}
impl Default for EmergencyMessage {
fn default() -> Self {
Self {
error_code: 0,
error_register: 0,
data: [0; 5],
slave_index: 0,
timestamp_ms: 0,
}
}
}
impl From<&ffi::EmcyRecord> for EmergencyMessage {
fn from(r: &ffi::EmcyRecord) -> Self {
let error_code = unsafe { std::ptr::addr_of!(r.error_code).read_unaligned() };
let error_register = unsafe { std::ptr::addr_of!(r.error_register).read_unaligned() };
let data = unsafe { std::ptr::addr_of!(r.data).read_unaligned() };
let slave_index = unsafe { std::ptr::addr_of!(r.slave_index).read_unaligned() };
let ts = unsafe { std::ptr::addr_of!(r.timestamp_ms).read_unaligned() };
Self {
error_code,
error_register,
data,
slave_index,
timestamp_ms: ts as u64,
}
}
}
pub fn emcy_get_history(
master_index: u16,
slave_index: u16,
max_records: usize,
) -> Vec<crate::utils::ffi::EmcyRecord> {
let mut records = vec![crate::utils::ffi::EmcyRecord::default(); max_records];
let count = unsafe {
ffi::EmcyGetHistory(
master_index,
slave_index,
records.as_mut_ptr(),
max_records as i32,
)
};
if count > 0 {
records.truncate(count as usize);
} else {
records.clear();
}
records
}
pub fn emcy_clear_history(master_index: u16, slave_index: u16) {
unsafe { ffi::EmcyClearHistory(master_index, slave_index); }
}
pub fn emcy_get_count(master_index: u16, slave_index: u16) -> i32 {
unsafe { ffi::EmcyGetCount(master_index, slave_index) }
}