Skip to content

Commit

Permalink
Change type reprensentation to c_**
Browse files Browse the repository at this point in the history
  • Loading branch information
Hayashi-Yudai committed Nov 24, 2021
1 parent 5bd82ce commit f4677b6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 30 deletions.
21 changes: 13 additions & 8 deletions src/helpers/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use synthrs::filter::{convolve, cutoff_from_frequency, lowpass_filter};
///
/// * e - エラーコード
/// * func_name - エラーの発生元のメソッド名
pub fn parse_error(e: i32, func_name: &str) {
pub fn parse_error(e: i16, func_name: &str) {
match e {
0 => {}
1 => println!("{}: Invalid ID", func_name),
Expand Down Expand Up @@ -46,7 +46,7 @@ impl Data {
/// # Argument
///
/// * id - 装置のユニット番号選択スイッチの数字
fn get_ranges(id: i32) -> (u8, u8) {
fn get_ranges(id: c_short) -> (u8, u8) {
let mut ch1_range: u8 = 0;
let mut ch2_range: u8 = 0;
let err;
Expand All @@ -58,7 +58,7 @@ fn get_ranges(id: i32) -> (u8, u8) {
}

if err != 0 {
parse_error(err, "TUSB0216AD_Input_Check");
parse_error(err as c_short, "TUSB0216AD_Input_Check");
}

(ch1_range, ch2_range)
Expand Down Expand Up @@ -91,7 +91,12 @@ fn calc_range_width(range: u8) -> f32 {
/// # Returns
///
/// CH1, CH2の出力電圧値
fn convert_to_voltage(ch1_range: u8, ch2_range: u8, ch1_data: f32, ch2_data: f32) -> (f32, f32) {
pub fn convert_to_voltage(
ch1_range: u8,
ch2_range: u8,
ch1_data: f32,
ch2_data: f32,
) -> (f32, f32) {
let ch1_width: f32 = calc_range_width(ch1_range);
let ch2_width: f32 = calc_range_width(ch2_range);

Expand Down Expand Up @@ -181,17 +186,17 @@ fn update_data(
/// * id - 装置のユニット番号選択スイッチの数字
/// * seconds - データ取り込みを行う秒数
/// * flag - データ取り込み中であるかを判別するフラグ
pub fn continuous_read(id: i32, seconds: u64, flag: Arc<Mutex<i8>>) {
pub fn continuous_read(id: c_short, seconds: u64, flag: Arc<Mutex<i8>>) {
println!("Timer start!");
let sleeping_time = time::Duration::from_secs(seconds);
let mut error: i32;
let mut error: c_short;

unsafe {
// CH1, 2ともに+/-10Vの入力を受け付ける
// 入力が+/-10VなのはSR830の仕様
error = TUSB0216AD_Input_Set(id, 0, 0);
parse_error(error, "TUSB0216AD_Input_Set");
error = TUSB0216AD_Start(id, &(2 as u8), 0, 0, 0);
error = TUSB0216AD_Start(id, 2, 0, 0, 0);
parse_error(error, "TUSB0216AD_Start");
error = TUSB0216AD_Trigger(id);
parse_error(error, "TUSB0216AD_Trigger");
Expand Down Expand Up @@ -219,7 +224,7 @@ pub fn continuous_read(id: i32, seconds: u64, flag: Arc<Mutex<i8>>) {
/// * position - CH1のデータを収納するベクトル
/// * intensity - CH2のデータを収納するベクトル
pub fn get_data(
id: i32,
id: c_short,
flag: Arc<Mutex<i8>>,
position: Arc<Mutex<Vec<f32>>>,
intensity: Arc<Mutex<Vec<f32>>>,
Expand Down
51 changes: 34 additions & 17 deletions src/helpers/operation.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,76 @@
use std::f64::consts::PI;
// use std::f64::consts::PI;
use std::os::raw::{c_int, c_short, c_uchar, c_uint};

/// TUSB16ADのドライバに定義されいるMicrosoft Visual Cインターフェース群
#[link(name = "TUSB16AD", kind = "dylib")]
//#[link(name = "ad_mock.dll", kind = "dylib")] // Mock
#[allow(dead_code)]
#[cfg(feature = "release")]
extern "C" {
pub fn TUSB0216AD_Device_Open(id: i32) -> i32;
pub fn TUSB0216AD_Device_Close(id: i32);
pub fn TUSB0216AD_Device_Open(id: c_short) -> c_short;
pub fn TUSB0216AD_Device_Close(id: c_short);
/// 指定IDのデバイスのデジタル入力ポートの入力値を読み取りDataに格納する
pub fn TUSB0216AD_DIO_In(id: i32, Data: &u8) -> i32;
pub fn TUSB0216AD_DIO_In(id: c_short, Data: *mut c_uchar) -> c_short;
/// 指定IDのデバイスのデジタル入力ポートの出力値を読み取りDataに格納する
pub fn TUSB0216AD_DIO_Out(id: i32, Data: &u8) -> i32;
pub fn TUSB0216AD_DIO_Out(id: c_short, Data: c_uchar) -> c_short;
/// 指定IDのデバイスのデジタル入力ポートの出力値を確認する
pub fn TUSB0216AD_DIO_Chk(id: i32, Data: &u8) -> i32;
pub fn TUSB0216AD_DIO_Chk(id: c_short, Data: *mut c_uchar) -> c_short;
/// 指定IDのデバイスのアナログ入力電圧をデジタル変換して取得
/// channel1, 2を1回ずつ変換
/// 連続測定時には使用不可
pub fn TUSB0216AD_Ad_Single(id: i32, Data: &i32) -> i32;
pub fn TUSB0216AD_Ad_Single(id: c_short, Data: *mut c_int) -> c_short;
/// 連続測定を開始
/// ch: 0 (1chのみ)、1(2chのみ)、2(1, 2ch同時)
/// PreLen: プレトリガ長
/// TrigType: トリガ種類、0: 内部トリガ、1: 外部デジタル、2: アナログ立ち上がり、3: アナログ立下り
/// TrigCh: アナログトリガのトリガチャネル、0: ch1, 1: ch2
pub fn TUSB0216AD_Start(id: i32, ch: &u8, PreLen: i32, TrigType: u8, TrgCh: u8) -> i32;
pub fn TUSB0216AD_Start(
id: c_short,
ch: c_uchar,
PreLen: c_int,
TrigType: c_uchar,
TrgCh: c_uchar,
) -> c_short;
/// 連続取り込み停止
pub fn TUSB0216AD_Stop(id: i32) -> i32;
pub fn TUSB0216AD_Stop(id: c_short) -> c_short;
/// 連続取り込みの状態確認
/// status: 0 or 2: 停止中、1: トリガ待ち、3: トリガ後変換中
/// overflow: overflow状態, [ch1, ch2]という構造、0: overflowなし, 1: overflow
/// datalen: 取り込み済みデータ数、[ch1, ch2]という構造
pub fn TUSB0216AD_Ad_Status(id: i32, status: &u8, overflow: &u8, datalen: &u16) -> i32;
pub fn TUSB0216AD_Ad_Status(
id: c_short,
status: *mut c_uchar,
overflow: *mut c_uchar,
datalen: *mut c_uint,
) -> c_short;
/// 取り込み済みデータを取得
/// data: 取り込みデータの格納先のポインタ
/// datalen: 取り込み要求長。1~262144、戻るときには実際に取得された数が入っている
pub fn TUSB0216AD_Ad_Data(id: i32, ch: u8, data: *mut i32, datalen: *mut u32) -> i32;
pub fn TUSB0216AD_Ad_Data(
id: c_short,
ch: c_uchar,
data: *mut c_int,
datalen: *mut c_uint,
) -> c_short;
/// クロック時間の設定
/// ClkTime: 内部クロック周期設定、500 ~ 2147483647。クロック周期 = ClkTime * 20 ns
/// sel: クロックソース、0: 内部クロック、1: 外部クロック
pub fn TUSB0216AD_AdClk_Set(id: i32, ClkTime: i32, sel: u8) -> i32;
pub fn TUSB0216AD_AdClk_Set(id: c_short, ClkTime: c_int, sel: c_uchar) -> c_short;
/// 連続サンプリング時のアナログトリガ基準レベルの設定
/// level: アナログ立ち上がり、下がり時の基準トリガ, 1 ~ 65534
/// hys: ノイズ除去レベル。0 ~ 660でノイズより十分大きく信号振幅より小さな値
pub fn TUSB0216AD_Level_Set(id: i32, level: i32, hys: i32) -> i32;
pub fn TUSB0216AD_Level_Set(id: c_short, level: c_int, hys: c_short) -> c_short;
/// 入力レンジの設定
/// type1: ch1のレンジ設定
/// type2: ch2のレンジ設定
/// 0: +/-10V, 1: +/-5V, 2: +/-2.5V, 3: +/-1.25V, 4: 10V, 5: 5V, 6: 2.5V
pub fn TUSB0216AD_Input_Set(id: i32, type1: u8, type2: u8) -> i32;
pub fn TUSB0216AD_Input_Set(id: c_short, type1: c_uchar, type2: c_uchar) -> c_short;
/// 入力レンジの確認
/// type1, type2 にはそれぞれのチャネルでのレンジの番号が入る
/// 返り値はエラーコード
pub fn TUSB0216AD_Input_Check(id: i32, type1: *mut u8, type2: *mut u8) -> i32;
pub fn TUSB0216AD_Input_Check(id: c_short, type1: *mut c_uchar, type2: *mut c_uchar)
-> c_short;
/// ソフトウェアトリガを掛ける
pub fn TUSB0216AD_Trigger(id: i32) -> i32;
pub fn TUSB0216AD_Trigger(id: c_short) -> c_short;
}

// Debug用 mock
Expand Down
11 changes: 6 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ extern crate serde_json;

use dotenv::dotenv;
use helpers::{helper, operation, post};
use std::os::raw::{c_int, c_short, c_uchar, c_uint};
use std::sync::{Arc, Mutex};
use std::thread;

#[no_mangle]
pub extern "C" fn open(id: i32) -> i32 {
let error: i32;
pub extern "C" fn open(id: c_short) -> i16 {
let error: c_short;
unsafe {
error = operation::TUSB0216AD_Device_Open(id);
}
Expand All @@ -21,15 +22,15 @@ pub extern "C" fn open(id: i32) -> i32 {
}

#[no_mangle]
pub extern "C" fn close(id: i32) {
pub extern "C" fn close(id: c_short) {
unsafe {
operation::TUSB0216AD_Device_Close(id);
}
}

#[no_mangle]
pub extern "C" fn set_clock(id: i32, clock_time: i32, sel: u8) -> i32 {
let error: i32;
pub extern "C" fn set_clock(id: c_short, clock_time: c_int, sel: c_uchar) -> c_short {
let error: c_short;
unsafe {
error = operation::TUSB0216AD_AdClk_Set(id, clock_time, sel);
}
Expand Down

0 comments on commit f4677b6

Please sign in to comment.