-- ############################################################################# -- # Automatic trainer switch - Lua application for JETI DC/DS transmitters -- # -- # Copyright (c) 2016, JETI model s.r.o. -- # All rights reserved. -- # -- # Redistribution and use in source and binary forms, with or without -- # modification, are permitted provided that the following conditions are met: -- # -- # 1. Redistributions of source code must retain the above copyright notice, this -- # list of conditions and the following disclaimer. -- # 2. Redistributions in binary form must reproduce the above copyright notice, -- # this list of conditions and the following disclaimer in the documentation -- # and/or other materials provided with the distribution. -- # -- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -- # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -- # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -- # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -- # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -- # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -- # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -- # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -- # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -- # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -- # -- # The views and conclusions contained in the software and documentation are those -- # of the authors and should not be interpreted as representing official policies, -- # either expressed or implied, of the FreeBSD Project. -- # -- # V1.0 - Initial release -- ############################################################################# -------------------------------------------------------------------- local appName="Auto Trainer Switch" local prevP1, prevP2, prevP3, prevP4 local playedFileT = "" local playedFileS = "" local playedType = 1 local switch local prevVal = 1 local teacher = 0 local ctrlIdx -------------------------------------------------------------------- local function initForm(formID) form.addLabel({label="Automatic Trainer Switch",font=2}) form.addRow(2) form.addLabel({label="Audio Teacher"}) form.addAudioFilebox(playedFileT, function(value) playedFileT=value; system.pSave("fileT",value) end) form.addRow(2) form.addLabel({label="Audio Student"}) form.addAudioFilebox(playedFileS, function(value) playedFileS=value; system.pSave("fileS",value) end) form.addRow(2) form.addLabel({label="Switch"}) form.addInputbox(switch,true, function(value) switch=value;system.pSave("switch",value); end ) end local function keyPressed(key) end local function printForm() form.setButton(1,teacher > 0 and "On" or "Off" ,ENABLED) end -------------------------------------------------------------------- -- Init function local function init() playedFileT = system.pLoad("fileT","") playedFileS = system.pLoad("fileS","") switch = system.pLoad("switch") system.registerForm(1,MENU_ADVANCED,appName,initForm,keyPressed,printForm); ctrlIdx = system.registerControl(1, "Trainer Switch","T/S") end -------------------------------------------------------------------- -- Loop function local function loop() local val = system.getInputsVal(switch) if(val and val>0 and prevVal==0) then prevVal=1 if system.getProperty("WirelessMode") ~= "Teacher" then system.setProperty("WirelessMode","Teacher") system.playFile(playedFileT,AUDIO_IMMEDIATE) else prevP1,prevP2,prevP3,prevP4 = system.getInputs("P1","P2","P3","P4") teacher = 1 system.playFile(playedFileS,AUDIO_IMMEDIATE) end elseif(val and val<=0) then prevVal=0 end if teacher>0 then local P1,P2,P3,P4 = system.getInputs("P1","P2","P3","P4") if(math.abs(P1 - prevP1) > 0.1 or math.abs(P2 - prevP2) > 0.1 or math.abs(P3 - prevP3) > 0.1 or math.abs(P4 - prevP4) > 0.1) then teacher=0 system.playFile(playedFileT,AUDIO_IMMEDIATE) end end if(ctrlIdx) then system.setControl(ctrlIdx, teacher,0) end end -------------------------------------------------------------------- return { init=init, loop=loop, author="JETI model", version="1.00",name=appName}