/* ----------------------------------------------------------------------------- This source file is part of OpenSpace3D For the latest info, see http://www.openspace3d.com Copyright (c) 2015 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 ----------------------------------------------------------------------------- */ struct MagnetSensor = [ PMS_inst : PInstance, PMS_lData : [[F F F] r1], PMS_offSetBase : [F F F], PMS_iTick : I, PMS_bValue : I ]mkMagnetSensor;; var iNumberOfSamples = 20;; /*------------------ Delete PlugIT instance -------------------*/ fun deleteOb(inst)= _SetSensorEnable SENSOR_TYPE_MAGNETIC_FIELD 0; setPluginInstanceCbScenePreRender inst nil; 0;; fun getOffsetBase(magnetstr)= let sizelist magnetstr.PMS_lData -> size in let 0 -> i in let [0.0 0.0 0.0] -> avg in ( while (i < size) do ( let nth_list magnetstr.PMS_lData i -> [px py pz] in set avg = addVectorF avg [px py pz]; set i = i + 1; ); let itof size -> divs in set magnetstr.PMS_offSetBase = divideVectorF avg [divs divs divs]; ); 0;; fun getOffsetMinMax(magnetstr, i)= let i + iNumberOfSamples -> size in let nth_list magnetstr.PMS_lData (sizelist magnetstr.PMS_lData) - 1 -> [bx by bz] in let 10000.0 -> vmin in let -.10000.0 -> vmax in ( while (i < size) do ( let nth_list magnetstr.PMS_lData i -> [px py pz] in let [(px -. bx) (py -. by) (pz -. bz)] -> [ox oy oz] in let (sqrt ox *. ox +. oy *. oy +. oz *. oz) -> magnitude in ( set vmin = minf vmin magnitude; set vmax = maxf vmax magnitude; ); set i = i + 1; ); [vmin vmax] );; fun cbPreRender(inst, sessionstr, etime, magnetstr)= if ((_tickcount - magnetstr.PMS_iTick) < (1000 / 100)) then nil else let _GetVectorData SENSOR_TYPE_MAGNETIC_FIELD -> [x y z] in ( if (!magnetstr.PMS_bValue) then nil else SendPluginEvent inst "Value" strcatn (ftoa x)::" "::(ftoa y)::" "::(ftoa z)::nil inst.INST_sName; if ((sizelist magnetstr.PMS_lData) < (iNumberOfSamples * 2)) then nil else ( if (magnetstr.PMS_offSetBase != nil) then nil else getOffsetBase magnetstr; set magnetstr.PMS_lData = tl magnetstr.PMS_lData; ); set magnetstr.PMS_lData = lcat magnetstr.PMS_lData [x y z]::nil; if (magnetstr.PMS_offSetBase == nil) then nil else ( let magnetstr.PMS_offSetBase -> [px py pz] in let [(x -. px) (y -. py) (z -. pz)] -> [ox oy oz] in let (sqrt ox *. ox +. oy *. oy +. oz *. oz) -> magnitude in SendPluginEvent inst "Magnitude" ftoa magnitude inst.INST_sName; ); if ((sizelist magnetstr.PMS_lData) < (iNumberOfSamples * 2)) then nil else ( let getOffsetMinMax magnetstr 0 -> [vmin _] in let getOffsetMinMax magnetstr iNumberOfSamples -> [_ vmax] in ( //addLogMessage strcatn ">>>> "::(ftoa vmin)::" > "::(ftoa vmax)::nil; if (vmin >. 30.0 || vmax <. 130.0) then nil else ( SendPluginEvent inst "CardBoard trigger" nil nil; set magnetstr.PMS_lData = nil; //reset offset set magnetstr.PMS_offSetBase = nil; ); ); ); set magnetstr.PMS_iTick = _tickcount; ); 0;; /*----------------------- Callbacks ------------------------*/ fun cbEnable(inst, from, action, param, rep, magnetstr)= if !(_IsSensorAvailable SENSOR_TYPE_MAGNETIC_FIELD) then nil else ( _SetSensorEnable SENSOR_TYPE_MAGNETIC_FIELD 1; setPluginInstanceCbScenePreRender inst mkfun4 @cbPreRender magnetstr; ); 0;; fun cbDisable(inst, from, action, param, rep, magnetstr)= if !(_IsSensorAvailable SENSOR_TYPE_MAGNETIC_FIELD) then nil else ( _SetSensorEnable SENSOR_TYPE_MAGNETIC_FIELD 0; setPluginInstanceCbScenePreRender inst nil; set magnetstr.PMS_offSetBase = nil; ); 0;; /*--------------- Create new PlugIT instance ----------------*/ fun newOb(inst)= if !(_IsSensorAvailable SENSOR_TYPE_MAGNETIC_FIELD) then ( SendPluginEvent inst "Sensor not found" nil nil; 0; ) else let (IsInEditor inst) || IsEventLinked inst "Value" -> bvalue in let mkMagnetSensor [inst nil nil _tickcount bvalue] -> magnetstr in ( _SetSensorEnable SENSOR_TYPE_MAGNETIC_FIELD 1; setPluginInstanceCbScenePreRender inst mkfun4 @cbPreRender magnetstr; PluginRegisterAction inst "Enable" mkfun6 @cbEnable magnetstr; PluginRegisterAction inst "Disable" mkfun6 @cbDisable magnetstr; SendPluginEvent inst "Sensor found" nil nil; 0; ); setPluginInstanceCbDel inst @deleteOb; 0;; /*------------------- Initialize PlugIT --------------------*/ fun IniPlug(file)= PlugRegister @newOb nil; setPluginEditor @dynamicedit; 0;;