extern crate emergency_stop;

use self::emergency_stop::EmergencyStop;

use std::mem::transmute;
use std::ptr::null_mut;
use std::sync::atomic::Ordering;

#[no_mangle]
pub extern "C" fn emergency_stop_init(stop: u8) -> *mut EmergencyStop {
    match EmergencyStop::new(stop) {
        Err(_) => null_mut(),
        Ok(emergency_stop) => unsafe { transmute(Box::new(emergency_stop)) },
    }
}

#[no_mangle]
pub extern "C" fn emergency_stop_clean(emergency_stop: *mut EmergencyStop) {
    let _emergency_stop: Box<EmergencyStop> = unsafe { transmute(emergency_stop) };
}

#[no_mangle]
pub extern "C" fn emergency_stop_get_state(emergency_stop: *mut EmergencyStop) -> bool {
    let emergency_stop = unsafe { &mut *emergency_stop };

    emergency_stop.state.load(Ordering::Relaxed)
}
