//! CoE EMCY 紧急消息历史
//!
//! 对应 C# Slave/CoE_Emcy.cs
//! 将 EMCY 功能封装到 CoE 实例中。

use crate::utils::ffi;

/// 从 DLL 获取 EMCY 历史记录
///
/// 对应 C# EmcyGetHistory
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
}

/// 清除 EMCY 历史记录
///
/// 对应 C# EmcyClearHistory
pub fn emcy_clear_history(master_index: u16, slave_index: u16) {
    unsafe { ffi::EmcyClearHistory(master_index, slave_index); }
}

/// 获取 EMCY 记录数量
///
/// 对应 C# EmcyGetCount
pub fn emcy_get_count(master_index: u16, slave_index: u16) -> i32 {
    unsafe { ffi::EmcyGetCount(master_index, slave_index) }
}
