Traxxas TQ 1st gen: try 5
[DIY-Multiprotocol-TX-Module.git] / Lua_scripts / MultiLOLI.lua
blob4d309e37eb95244c60a1815ffd58af17133b726a
2 local toolName = "TNS|Multi LOLI RX config|TNE"
4 ---- #########################################################################
5 ---- # #
6 ---- # Copyright (C) OpenTX #
7 -----# #
8 ---- # License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html #
9 ---- # #
10 ---- # This program is free software; you can redistribute it and/or modify #
11 ---- # it under the terms of the GNU General Public License version 2 as #
12 ---- # published by the Free Software Foundation. #
13 ---- # #
14 ---- # This program is distributed in the hope that it will be useful #
15 ---- # but WITHOUT ANY WARRANTY; without even the implied warranty of #
16 ---- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
17 ---- # GNU General Public License for more details. #
18 ---- # #
19 ---- #########################################################################
21 local loli_nok = false
22 local channels={ { 768, "PWM", 100, 102, "PPM", 50, -768, "Servo", 0, -2048, "Switch", -100 }, -- CH1
23 { 768, "PWM", 100, -768, "Servo", 0, -2048, "Switch", -100 }, -- CH2
24 { -768, "Servo", 0, -2048, "Switch", -100 }, -- CH3
25 { -768, "Servo", 0, -2048, "Switch", -100 }, -- CH4
26 { 102, "SBUS", 50, -768, "Servo", 0, -2048, "Switch", -100 }, -- CH5
27 { -768, "Servo", 0, -2048, "Switch", -100 }, -- CH6
28 { 768, "PWM", 100, -768, "Servo", 0, -2048, "Switch", -100 }, -- CH7
29 { -768, "Servo", 0, -2048, "Switch", -100 } } -- CH8
31 local sel = 1
32 local edit = false
34 local blink = 0
35 local BLINK_SPEED = 15
37 local function drawScreenTitle(title)
38 if LCD_W == 480 then
39 lcd.drawFilledRectangle(0, 0, LCD_W, 30, TITLE_BGCOLOR)
40 lcd.drawText(1, 5, title, MENU_TITLE_COLOR)
41 else
42 lcd.drawScreenTitle(title, 0, 0)
43 end
44 end
46 local function LOLI_Draw_LCD(event)
47 local line = 0
49 lcd.clear()
51 --Display settings
52 local lcd_opt = 0
53 if LCD_W == 480 then
54 drawScreenTitle("Multi - LOLI RX configuration tool")
55 x_pos = 152
56 x_inc = 90
57 y_pos = 40
58 y_inc = 20
59 else
60 x_pos = 5
61 x_inc = 30
62 y_pos = 1
63 y_inc = 8
64 lcd_opt = SMLSIZE
65 end
67 --Multi Module detection
68 if loli_nok then
69 if LCD_W == 480 then
70 lcd.drawText(10,50,"The LOLI protocol is not selected...", lcd_opt)
71 else
72 --Draw on LCD_W=128
73 lcd.drawText(2,17,"LOLI protocol not selected...",SMLSIZE)
74 end
75 return
76 end
78 --Display current config
79 if LCD_W == 480 then
80 line = line + 1
81 lcd.drawText(x_pos, y_pos+y_inc*line -2, "Channel", lcd_opt)
82 lcd.drawText(x_pos+x_inc, y_pos+y_inc*line -2, "Function", lcd_opt)
83 lcd.drawRectangle(x_pos-4, y_pos+y_inc*line -4 , 2*x_inc +2, 188)
84 lcd.drawLine(x_pos-4, y_pos+y_inc*line +18, x_pos-4 +2*x_inc +1, y_pos+y_inc*line +18, SOLID, 0)
85 lcd.drawLine(x_pos+x_inc -5, y_pos+y_inc*line -4, x_pos+x_inc -5, y_pos+y_inc*line -5 +188, SOLID, 0)
86 line = line + 1
87 end
89 local out
90 for i = 1, 8 do
91 out = getValue("ch"..(i+8))
92 lcd.drawText(x_pos, y_pos+y_inc*line, "CH"..i, lcd_opt)
93 for j = 1, #channels[i], 3 do
94 if out > channels[i][j] then
95 if sel == i then
96 invert = INVERS
97 if edit == true then
98 blink = blink + 1
99 if blink > BLINK_SPEED then
100 invert = 0
101 if blink > BLINK_SPEED * 2 then
102 blink = 0
106 else
107 invert = 0
109 lcd.drawText(x_pos+x_inc, y_pos+y_inc*line, channels[i][j+1], lcd_opt + invert)
110 break
113 line = line + 1
117 local function LOLI_Change_Value(dir)
118 local pos = 0
119 local out
120 --look for the current position
121 out = getValue("ch"..(sel+8))
122 for j = 1, #channels[sel], 3 do
123 if out > channels[sel][j] then
124 pos = j
125 break
129 --decrement or increment
130 if dir < 0 and pos > 1 then
131 pos = pos - 3
132 elseif dir > 0 and pos + 3 < #channels[sel] then
133 pos = pos + 3
134 else
135 return
138 --delete all mixers for the selected channel
139 local num_mix = model.getMixesCount(sel-1 +8)
140 for i = 1, num_mix do
141 model.deleteMix(sel-1 +8, 0);
144 --create new mixer
145 local source_max = getFieldInfo("cyc1")
147 local val = { name = channels[sel][pos+1],
148 source = source_max.id - 1, -- MAX=100 on TX16S
149 weight = channels[sel][pos+2],
150 offset = 0,
151 switch = 0,
152 multiplex = 0,
153 curveType = 0,
154 curveValue = 0,
155 flightModes = 0,
156 carryTrim = false,
157 mixWarn = 0,
158 delayUp = 0,
159 delayDown = 0,
160 speedUp = 0,
161 speedDown = 0 }
162 model.insertMix(sel-1 +8, 0, val)
165 local function LOLI_Menu(event)
166 if event == EVT_VIRTUAL_NEXT then
167 if edit == false then
168 -- not changing a value
169 if sel < 8 then
170 sel = sel + 1
172 else
173 -- need to inc the value
174 LOLI_Change_Value(1)
176 elseif event == EVT_VIRTUAL_PREV then
177 if edit == false then
178 -- not changing a value
179 if sel > 1 then
180 sel = sel - 1
182 else
183 -- need to dec the value
184 LOLI_Change_Value(-1)
186 elseif event == EVT_VIRTUAL_ENTER then
187 if edit == false then
188 edit = true
189 blink = BLINK_SPEED
190 else
191 edit = false
196 -- Init
197 local function LOLI_Init()
198 local module_conf = model.getModule(0)
199 if module_conf["Type"] ~= 6 or module_conf["protocol"] ~= 82 then
200 module_conf = model.getModule(1)
201 if module_conf["Type"] ~= 6 or module_conf["protocol"] ~= 82 then
202 loli_nok = true
207 -- Main
208 local function LOLI_Run(event)
209 if event == nil then
210 error("Cannot be run as a model script!")
211 return 2
212 elseif event == EVT_VIRTUAL_EXIT then
213 return 2
214 else
215 LOLI_Menu(event)
216 LOLI_Draw_LCD(event)
217 return 0
221 return { init=LOLI_Init, run=LOLI_Run }