/* ----------------------------------------------------------------------------- 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 PlugGPIOrgbLed = [ GPIORGBLED_iPinR : ObjGpioPin, GPIORGBLED_iPinG : ObjGpioPin, GPIORGBLED_iPinB : ObjGpioPin, GPIORGBLED_iColor : I, GPIORGBLED_iState : I ]mkPlugGPIOrgbLed;; /*! \brief Callback on instance destruction * * Prototype: fun [PInstance PlugGPIOrgbLed] I * * \param PInstance : destroyed plugIT instance * \param PlugGPIOrgbLed : gpio structure * * \return I : 0 **/ fun deleteOb(inst, obstr)= _dsGPIOpin obstr.GPIORGBLED_iPinR; _dsGPIOpin obstr.GPIORGBLED_iPinG; _dsGPIOpin obstr.GPIORGBLED_iPinB; 0;; fun updateColor(obstr)= if (!obstr.GPIORGBLED_iState) then ( _GPIOpwmWrite obstr.GPIORGBLED_iPinR 0; _GPIOpwmWrite obstr.GPIORGBLED_iPinG 0; _GPIOpwmWrite obstr.GPIORGBLED_iPinB 0; ) else let G2Dgetrgb obstr.GPIORGBLED_iColor -> [red green blue] in let ftoi ((100.0 /. 255.0) *. (itof red))-> red in let ftoi ((100.0 /. 255.0) *. (itof green)) -> green in let ftoi ((100.0 /. 255.0) *. (itof blue)) -> blue in ( _GPIOpwmWrite obstr.GPIORGBLED_iPinR red; _GPIOpwmWrite obstr.GPIORGBLED_iPinG green; _GPIOpwmWrite obstr.GPIORGBLED_iPinB blue; ); 0;; /*! \brief Callback on "SetColor" dms action * * Set param link data as color * * Prototype: fun [PInstance DMI S S I PlugGPIOrgbLed] 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 PlugGPIOrgbLed : gpio structure * * \return I : 0 **/ fun cbSetColor(inst, from, action, param, reply, obstr)= let hd strextr param -> lp in let if ((sizelist lp) == 3) then let atoi hd lp -> pr in let atoi hd tl lp -> pg in let atoi hd tl tl lp -> pb in make_rgb (if pr == nil then 0 else min pr 255) (if pg == nil then 0 else min pg 255) (if pb == nil then 0 else min pb 255) else htoi param -> color in ( set obstr.GPIORGBLED_iColor = color; updateColor obstr; ); 0;; /*! \brief Callback on "On" dms action * * Light on the led * * Prototype: fun [PInstance DMI S S I PlugGPIOrgbLed] 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 PlugGPIOrgbLed : gpio structure * * \return I : 0 **/ fun cbOn(inst, from, action, param, reply, obstr)= set obstr.GPIORGBLED_iState = 1; updateColor obstr; 0;; /*! \brief Callback on "Off" dms action * * Light off the led * * Prototype: fun [PInstance DMI S S I PlugGPIOrgbLed] 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 PlugGPIOrgbLed : gpio structure * * \return I : 0 **/ fun cbOff(inst, from, action, param, reply, obstr)= set obstr.GPIORGBLED_iState = 0; updateColor obstr; 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 "pinR") -> pinR in let if pinR == nil then 2 else pinR -> pinR in let atoi (getPluginInstanceParam inst "pinG") -> pinG in let if pinG == nil then 3 else pinG -> pinG in let atoi (getPluginInstanceParam inst "pinB") -> pinB in let if pinB == nil then 4 else pinB -> pinB in let atoi (getPluginInstanceParam inst "color") -> color in let if color == nil then (make_rgb 255 0 0) else color -> color in let atoi (getPluginInstanceParam inst "init") -> init in let if init == nil then 1 else init -> init in let mkPlugGPIOrgbLed [nil nil nil color init] -> obstr in ( set obstr.GPIORGBLED_iPinR = _crGPIOpin _channel pinR GPIO_PWM_OUTPUT; set obstr.GPIORGBLED_iPinG = _crGPIOpin _channel pinG GPIO_PWM_OUTPUT; set obstr.GPIORGBLED_iPinB = _crGPIOpin _channel pinB GPIO_PWM_OUTPUT; updateColor obstr; PluginRegisterAction inst "Set color" mkfun6 @cbSetColor obstr; PluginRegisterAction inst "On" mkfun6 @cbOn obstr; PluginRegisterAction inst "Off" mkfun6 @cbOff 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;;