/* ----------------------------------------------------------------------------- This source file is part of OpenSpace3D For the latest info, see http://www.openspace3d.com Copyright (c) 2012 I-maginer This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA, or go to http://www.gnu.org/copyleft/lesser.txt ----------------------------------------------------------------------------- */ /*-------------------- Global variables --------------------*/ struct TUsb = [ TUSB_inst : PInstance, TUSB_object : ObjTUsb ] mkTUsb;; /*------------------ Delete TUSB respiration belt instance -------------------*/ fun deleteOb(inst, tUsbStr)= // Close connection to respiration belt if tUsbStr.TUSB_object == nil then nil else _DSTUsbDevice tUsbStr.TUSB_object; 0;; /*----------------------- Events ------------------------*/ /* TUSB respiration belt connected */ fun cbConnected(tusb, tUsbStr)= // Send connection event SendPluginEvent tUsbStr.TUSB_inst "Connected" nil nil; 0;; /* TUSB respiration belt disconnected */ fun cbDisconnected(tusb, tUsbStr)= SendPluginEvent tUsbStr.TUSB_inst "Disconnected" nil nil; 0;; /* New sample data for TUSB respiration belt */ fun cbNewData(tusb, tUsbStr, beltData)= let sizelist beltData -> size in let 0 -> i in ( while i < size do ( let nth_list beltData i -> beltExtension in SendPluginEvent tUsbStr.TUSB_inst "Chest extension" ftoa beltExtension nil; set i = i + 1; ); ); 0;; /* TUSB respiration belt battery level */ fun cbBatteryLevel(tusb, tUsbStr, battery)= SendPluginEvent tUsbStr.TUSB_inst "Battery" ftoa battery nil; 0;; /*--------------- Create new TUSB respiration belt instance ----------------*/ fun createInstance(inst)= // Try to connect to respiration belt let _CRTUsbDevice _channel -> tusb in if (tusb == nil) then nil else ( // Set the TUSB respiration belt global structure let mkTUsb [inst tusb] -> tUsbStr in ( // Define the callback to call when the plugin instance is deleted setPluginInstanceCbDel inst mkfun2 @deleteOb tUsbStr; // Associate callbacks to events _CBTUsbConnected tusb @cbConnected tUsbStr; _CBTUsbDisconnected tusb @cbDisconnected tUsbStr; _CBTUsbNewData tusb @cbNewData tUsbStr; _CBTUsbBatteryLevel tusb @cbBatteryLevel tUsbStr; ); ); 0;; /*----------------------- Actions ------------------------*/ /* Enable device */ fun cbEnableDevice(inst, from, action, param, rep, p)= createInstance inst; 0;; /* Disable device */ fun cbDisableDevice(inst, from, action, param, rep, p)= SendPluginEvent inst "Disconnected" nil nil; deleteOb inst p; 0;; /*--------------- Create new plugIT object ----------------*/ fun newOb(inst)= // Get plugIT parameter let atoi (getPluginInstanceParam inst "onInit") -> onInit in // Create belt instance if we have to connect the device on start if !onInit then nil else createInstance inst; // Register plugIT actions PluginRegisterAction inst "Enable" mkfun6 @cbEnableDevice nil; PluginRegisterAction inst "Disable" mkfun6 @cbDisableDevice nil; 0;; /*------------------- Initialize plugIT --------------------*/ fun IniPlug(file)= PlugRegister @newOb nil; setPluginEditor @dynamicedit; 0;;