use crate::{Phidget, Result, ReturnCode};
use phidget_sys::{self as ffi, PhidgetHandle, PhidgetVoltageInputHandle};
use std::{ffi::c_void, mem, ptr};
pub type VoltageChangeCallback = dyn Fn(&VoltageInput, f64) + Send + 'static;
pub type AttachCallback = dyn Fn(&mut VoltageInput) + Send + 'static;
pub type DetachCallback = dyn Fn(&mut VoltageInput) + Send + 'static;
pub struct VoltageInput {
chan: PhidgetVoltageInputHandle,
cb: Option<*mut c_void>,
attach_cb: Option<*mut c_void>,
detach_cb: Option<*mut c_void>,
}
impl VoltageInput {
pub fn new() -> Self {
let mut chan: PhidgetVoltageInputHandle = ptr::null_mut();
unsafe {
ffi::PhidgetVoltageInput_create(&mut chan);
}
Self::from(chan)
}
unsafe extern "C" fn on_attach(phid: PhidgetHandle, ctx: *mut c_void) {
if !ctx.is_null() {
let cb: &mut Box<AttachCallback> = &mut *(ctx as *mut _);
let mut sensor = Self::from(phid as PhidgetVoltageInputHandle);
cb(&mut sensor);
mem::forget(sensor);
}
}
unsafe extern "C" fn on_detach(phid: PhidgetHandle, ctx: *mut c_void) {
if !ctx.is_null() {
let cb: &mut Box<DetachCallback> = &mut *(ctx as *mut _);
let mut sensor = Self::from(phid as PhidgetVoltageInputHandle);
cb(&mut sensor);
mem::forget(sensor);
}
}
unsafe extern "C" fn on_voltage_change(
chan: PhidgetVoltageInputHandle,
ctx: *mut c_void,
voltage: f64,
) {
if !ctx.is_null() {
let cb: &mut Box<VoltageChangeCallback> = &mut *(ctx as *mut _);
let sensor = Self::from(chan);
cb(&sensor, voltage);
mem::forget(sensor);
}
}
pub fn as_channel(&self) -> &PhidgetVoltageInputHandle {
&self.chan
}
pub fn voltage(&self) -> Result<f64> {
let mut v: f64 = 0.0;
ReturnCode::result(unsafe { ffi::PhidgetVoltageInput_getVoltage(self.chan, &mut v) })?;
Ok(v)
}
pub fn set_on_voltage_change_handler<F>(&mut self, cb: F) -> Result<()>
where
F: Fn(&VoltageInput, f64) + Send + 'static,
{
let cb: Box<Box<VoltageChangeCallback>> = Box::new(Box::new(cb));
let ctx = Box::into_raw(cb) as *mut c_void;
self.cb = Some(ctx);
ReturnCode::result(unsafe {
ffi::PhidgetVoltageInput_setOnVoltageChangeHandler(
self.chan,
Some(Self::on_voltage_change),
ctx,
)
})
}
pub fn set_on_attach_handler<F>(&mut self, cb: F) -> Result<()>
where
F: Fn(&mut VoltageInput) + Send + 'static,
{
let cb: Box<Box<AttachCallback>> = Box::new(Box::new(cb));
let ctx = Box::into_raw(cb) as *mut c_void;
ReturnCode::result(unsafe {
ffi::Phidget_setOnAttachHandler(self.as_mut_handle(), Some(Self::on_attach), ctx)
})?;
self.attach_cb = Some(ctx);
Ok(())
}
pub fn set_on_detach_handler<F>(&mut self, cb: F) -> Result<()>
where
F: Fn(&mut VoltageInput) + Send + 'static,
{
let cb: Box<Box<DetachCallback>> = Box::new(Box::new(cb));
let ctx = Box::into_raw(cb) as *mut c_void;
ReturnCode::result(unsafe {
ffi::Phidget_setOnDetachHandler(self.as_mut_handle(), Some(Self::on_detach), ctx)
})?;
self.detach_cb = Some(ctx);
Ok(())
}
}
impl Phidget for VoltageInput {
fn as_mut_handle(&mut self) -> PhidgetHandle {
self.chan as PhidgetHandle
}
fn as_handle(&self) -> PhidgetHandle {
self.chan as PhidgetHandle
}
}
unsafe impl Send for VoltageInput {}
impl Default for VoltageInput {
fn default() -> Self {
Self::new()
}
}
impl From<PhidgetVoltageInputHandle> for VoltageInput {
fn from(chan: PhidgetVoltageInputHandle) -> Self {
Self {
chan,
cb: None,
attach_cb: None,
detach_cb: None,
}
}
}
impl Drop for VoltageInput {
fn drop(&mut self) {
if let Ok(true) = self.is_open() {
let _ = self.close();
}
unsafe {
ffi::PhidgetVoltageInput_delete(&mut self.chan);
crate::drop_cb::<VoltageChangeCallback>(self.cb.take());
crate::drop_cb::<AttachCallback>(self.attach_cb.take());
crate::drop_cb::<DetachCallback>(self.detach_cb.take());
}
}
}