/*
-----------------------------------------------------------------------------
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 PlugGPIOshift = [
GPIOSHIFT_shiftObj : ObjGpioShiftDriver,
GPIOSHIFT_iNbOut : I,
GPIOSHIFT_iOutput : I
]mkPlugGPIOshift;;
/*! \brief Callback on instance destruction
*
* Prototype: fun [PInstance PlugGPIOshift] I
*
* \param PInstance : destroyed plugIT instance
* \param PlugGPIOshift : gpio structure
*
* \return I : 0
**/
fun deleteOb(inst, obstr)=
_dsGPIOshiftDriver obstr.GPIOSHIFT_shiftObj;
0;;
fun bitClear(value, bit)=
value & ~(1 << (bit));;
fun bitSet(value, bit)=
value | (1 << (bit));;
fun bitWrite(value, bit, bitvalue)=
if bitvalue then bitSet value bit else bitClear value bit;;
fun shiftWrite(obstr, pin, value)=
set value = if value > 0 then 1 else 0;
set obstr.GPIOSHIFT_iOutput = bitWrite obstr.GPIOSHIFT_iOutput pin value;
_setGPIOshiftDriverValues obstr.GPIOSHIFT_shiftObj [obstr.GPIOSHIFT_iOutput 0]::nil;
0;;
/*! \brief Callback on "Set pin X" dms action
*
* write pin state
*
* Prototype: fun [PInstance DMI S S I PlugGPIOshift] 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 PlugGPIOshift : gpio structure
*
* \return I : 0
**/
fun cbInput(inst, from, action, param, rep, obstr, pin) =
shiftWrite obstr pin atoi param;
0;;
/*! \brief Callback on "All on" dms action
*
* All shift pin to On
*
* Prototype: fun [PInstance DMI S S I PlugGPIOshift] 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 PlugGPIOshift : gpio structure
*
* \return I : 0
**/
fun cbOn(inst, from, action, param, reply, obstr)=
set obstr.GPIOSHIFT_iOutput = 0xffffffff;
_setGPIOshiftDriverValues obstr.GPIOSHIFT_shiftObj [0xffffffff 0]::nil;
0;;
/*! \brief Callback on "All off" dms action
*
* All shift pin to Off
*
* Prototype: fun [PInstance DMI S S I PlugGPIOshift] 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 PlugGPIOshift : gpio structure
*
* \return I : 0
**/
fun cbOff(inst, from, action, param, reply, obstr)=
set obstr.GPIOSHIFT_iOutput = 0;
_setGPIOshiftDriverValues obstr.GPIOSHIFT_shiftObj [0 0]::nil;
0;;
/*! \brief Callback on "Set data" dms action
*
* Set the bits data
*
* Prototype: fun [PInstance DMI S S I PlugGPIOshift] 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 PlugGPIOshift : gpio structure
*
* \return I : 0
**/
fun cbSetData(inst, from, action, param, reply, obstr)=
let strextr param -> l in
let nil -> data in
(
while l != nil do
(
let hd l -> lp in
let 0 -> i in
let 0 -> val in
let 0 -> tm in
(
while (lp != nil) do
(
let atoi (hd lp) -> p in
if (i > 7) then
set tm = p * 1000
else
set val = bitWrite val i p;
set lp = tl lp;
set i = i + 1;
);
set data = [val tm]::data;
);
set l = tl l;
);
_setGPIOshiftDriverValues obstr.GPIOSHIFT_shiftObj revertlist data;
);
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 atoi (getPluginInstanceParam inst "pinClock") -> pinclock in
let if pinclock == nil then 3 else pinclock -> pinclock in
let atoi (getPluginInstanceParam inst "pinLatch") -> pinlatch in
let if pinlatch == nil then 4 else pinlatch -> pinlatch in
let atoi (getPluginInstanceParam inst "nbOut") -> nbout in
let if nbout == nil then 8 else nbout -> nbout in
let atoi (getPluginInstanceParam inst "initval") -> initval in
let if initval == nil then 0 else initval -> initval in
let mkPlugGPIOshift [nil nbout 0] -> obstr in
(
set obstr.GPIOSHIFT_shiftObj = _crGPIOshiftDriver _channel pindata pinclock pinlatch nbout;
let 0 -> i in
while i < nbout do
(
PluginRegisterAction inst (strcat "Set pin " (itoa i+1)) mkfun6 mkfun7 @cbInput i obstr;
set i = i + 1;
);
if (!initval) then nil else
cbOn inst nil nil nil nil obstr;
PluginRegisterAction inst "All on" mkfun6 @cbOn obstr;
PluginRegisterAction inst "All off" mkfun6 @cbOff obstr;
PluginRegisterAction inst "Set data" mkfun6 @cbSetData 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;;