/* ----------------------------------------------------------------------------- This source file is part of OpenSpace3D For the latest info, see http://www.openspace3d.com Copyright (c) 2016 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 ----------------------------------------------------------------------------- */ /* Version : 1.0 First version : 05/29/2016 Author : Bourineau Bastien */ struct PlugGPIOecho = [ GPIOECHO_inst : PInstance, GPIOECHO_objEcho : ObjGpioEcho, GPIOECHO_fDistance : F, GPIOECHO_iLastValue : I, GPIOECHO_iLastTime : I, GPIOECHO_bState : I ]mkPlugGPIOecho;; /*! \brief Callback on instance destruction * * Prototype: fun [PInstance PlugGPIOecho] I * * \param PInstance : destroyed plugIT instance * \param PlugGPIOecho : gpio structure * * \return I : 0 **/ fun deleteOb(inst, obstr)= setPluginInstanceCbScenePreRender inst nil; _dsGPIOecho obstr.GPIOECHO_objEcho; 0;; fun cbPreRender(inst, sessionstr, etime, obstr)= let _getGPIOechoValue obstr.GPIOECHO_objEcho -> value in let ((itof value) /. 100.0) -> dmeter in if (value == 0) then nil else ( if (value == obstr.GPIOECHO_iLastValue) then nil else SendPluginEvent inst "Current distance" ftoa dmeter inst.INST_sName; if ((dmeter <. obstr.GPIOECHO_fDistance) && !obstr.GPIOECHO_bState) then ( if (obstr.GPIOECHO_iLastTime != nil) then nil else set obstr.GPIOECHO_iLastTime = _tickcount; if (obstr.GPIOECHO_iLastTime == nil || ((_tickcount - obstr.GPIOECHO_iLastTime) < 250)) then nil else ( set obstr.GPIOECHO_bState = 1; SendPluginEvent inst "In" nil nil; set obstr.GPIOECHO_iLastTime = _tickcount; ); ) else if ((dmeter >=. obstr.GPIOECHO_fDistance) && obstr.GPIOECHO_bState && ((obstr.GPIOECHO_iLastTime == nil) || ((_tickcount - obstr.GPIOECHO_iLastTime) >= 250))) then ( set obstr.GPIOECHO_bState = 0; SendPluginEvent inst "Out" nil nil; set obstr.GPIOECHO_iLastTime = nil; ) else nil; set obstr.GPIOECHO_iLastValue = value; ); 0;; /*! \brief Callback on new plugIT instance * * Read the parameters from editor values and init GPIO pin * * Prototype: fun [PInstance] I * * \param PInstance : plugIT instance * * \return I : 0 **/ fun newOb(inst)= let atoi (getPluginInstanceParam inst "triggerpin") -> triggerpin in let if triggerpin == nil then 2 else triggerpin -> triggerpin in let atoi (getPluginInstanceParam inst "echopin") -> echopin in let if echopin == nil then 3 else echopin -> echopin in let atof (getPluginInstanceParam inst "distance") -> dist in let if dist == nil then 1.0 else dist -> dist in let mkPlugGPIOecho [inst nil dist 0 nil 0] -> obstr in ( set obstr.GPIOECHO_objEcho = _crGPIOecho _channel triggerpin echopin; setPluginInstanceCbScenePreRender inst mkfun4 @cbPreRender obstr; setPluginInstanceCbDel inst mkfun2 @deleteOb obstr; ); 0;; /*! \brief Global plugIT function to initialize the plugIT callbacks * * Prototype: fun [S] I * * \param S : plugIT file path * * \return I : 0 **/ fun IniPlug(file)= PlugRegister @newOb nil; setPluginEditor @dynamicedit; 0;;