/*
-----------------------------------------------------------------------------
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 PlugGPIOservo = [
GPIOSERVO_inst : PInstance,
GPIOSERVO_iPinData : I,
GPIOSERVO_fAngle : F,
GPIOSERVO_fCurAngle : F,
GPIOSERVO_fSpeed : F,
GPIOSERVO_servo : ObjGpioServo
]mkPlugGPIOservo;;
/*! \brief Callback on instance destruction
*
* Prototype: fun [PInstance PlugGPIOservo] I
*
* \param PInstance : destroyed plugIT instance
* \param PlugGPIOservo : gpio structure
*
* \return I : 0
**/
fun deleteOb(inst, obstr)=
setPluginInstanceCbScenePreRender inst nil;
_dsGPIOservo obstr.GPIOSERVO_servo;
0;;
fun cbPreRender(inst, sessionstr, etime, obstr)=
if (obstr.GPIOSERVO_fAngle == obstr.GPIOSERVO_fCurAngle) then
(
setPluginInstanceCbScenePreRender obstr.GPIOSERVO_inst nil;
0;
)
else
(
let obstr.GPIOSERVO_fSpeed -> degpsec in
let (itof etime) /. 1000.0 -> ftime in
let if (obstr.GPIOSERVO_fCurAngle >. obstr.GPIOSERVO_fAngle) then -.1.0 else 1.0 -> dir in
let ftime *. degpsec *. dir -> ang in
(
set obstr.GPIOSERVO_fCurAngle = if (dir >. 0.0) && ((obstr.GPIOSERVO_fCurAngle +. ang) >. obstr.GPIOSERVO_fAngle) ||
(dir <. 0.0) && ((obstr.GPIOSERVO_fCurAngle +. ang) <. obstr.GPIOSERVO_fAngle)
then
obstr.GPIOSERVO_fAngle
else
obstr.GPIOSERVO_fCurAngle +. ang;
_setGPIOservoAngle obstr.GPIOSERVO_servo obstr.GPIOSERVO_fCurAngle;
//addLogMessage ftoa obstr.GPIOSERVO_fCurAngle;
);
0;
);
0;;
/*! \brief Callback on "Set speed" dms action
*
* change rotation speed
*
* Prototype: fun [PInstance DMI S S I PlugGPIOservo] I
*
* \param PInstance : plugIT instance
* \param DMI : DMS module who call the action (not used)
* \param S : name of the launched action
* \param S : data posted in DMS action link
* \param I : reply flag (not used)
* \param PlugGPIOservo : gpio structure
*
* \return I : 0
**/
fun cbSetSpeed(inst, from, action, param, rep, obstr) =
let if (atof param) <=. 0.0 then 0.00001 else atof param -> speed in
set obstr.GPIOSERVO_fSpeed = speed;
0;;
/*! \brief Callback on "Set angle" dms action
*
* change servo angle
*
* Prototype: fun [PInstance DMI S S I PlugGPIOservo] I
*
* \param PInstance : plugIT instance
* \param DMI : DMS module who call the action (not used)
* \param S : name of the launched action
* \param S : data posted in DMS action link
* \param I : reply flag (not used)
* \param PlugGPIOservo : gpio structure
*
* \return I : 0
**/
fun cbSetAngle(inst, from, action, param, rep, obstr) =
let atof param -> angle in
let if angle <. 0.0 then 0.0 else if angle >. 180.0 then 180.0 else angle -> angle in
set obstr.GPIOSERVO_fAngle = angle;
if (obstr.GPIOSERVO_servo == nil) then nil else
setPluginInstanceCbScenePreRender inst mkfun4 @cbPreRender obstr;
0;;
/*! \brief Callback on "Enable" dms action
*
* Enable servo
*
* Prototype: fun [PInstance DMI S S I PlugGPIOservo] I
*
* \param PInstance : plugIT instance
* \param DMI : DMS module who call the action (not used)
* \param S : name of the launched action
* \param S : data posted in DMS action link
* \param I : reply flag (not used)
* \param PlugGPIOservo : gpio structure
*
* \return I : 0
**/
fun cbEnable(inst, from, action, param, reply, obstr)=
if (obstr.GPIOSERVO_servo != nil) then nil else
(
set obstr.GPIOSERVO_servo = _crGPIOservo _channel obstr.GPIOSERVO_iPinData obstr.GPIOSERVO_fCurAngle;
setPluginInstanceCbScenePreRender inst mkfun4 @cbPreRender obstr;
);
0;;
/*! \brief Callback on "Disable" dms action
*
* Disable servo
*
* Prototype: fun [PInstance DMI S S I PlugGPIOservo] I
*
* \param PInstance : plugIT instance
* \param DMI : DMS module who call the action (not used)
* \param S : name of the launched action
* \param S : data posted in DMS action link
* \param I : reply flag (not used)
* \param PlugGPIOservo : gpio structure
*
* \return I : 0
**/
fun cbDisable(inst, from, action, param, reply, obstr)=
setPluginInstanceCbScenePreRender inst nil;
_dsGPIOservo obstr.GPIOSERVO_servo;
set obstr.GPIOSERVO_servo = nil;
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 "pinData") -> pindata in
let if pindata == nil then 2 else pindata -> pindata in
let atof (getPluginInstanceParam inst "angle") -> angle in
let if angle == nil then 90.0 else angle -> angle in
let atof (getPluginInstanceParam inst "speed") -> speed in
let if speed == nil then 180.0 else speed -> speed in
let atoi (getPluginInstanceParam inst "init") -> init in
let if init == nil then 0 else init -> init in
let mkPlugGPIOservo [inst pindata angle angle speed nil] -> obstr in
(
if (!init) then nil else
cbEnable inst nil nil nil nil obstr;
PluginRegisterAction inst "Set angle" mkfun6 @cbSetAngle obstr;
PluginRegisterAction inst "Set speed" mkfun6 @cbSetSpeed obstr;
PluginRegisterAction inst "Enable" mkfun6 @cbEnable obstr;
PluginRegisterAction inst "Disable" mkfun6 @cbDisable 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;;