Shouldn't copyright be there too ? (#5200)
[opentx.git] / radio / sdcard / horus / CROSSFIRE / crossfire.lua
blob6606f0aaabdd3e7eeb46bf6275a4345f63cb8d0c
1 ---- #########################################################################
2 ---- # #
3 ---- # Copyright (C) OpenTX #
4 -----# #
5 ---- # License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html #
6 ---- # #
7 ---- # This program is free software; you can redistribute it and/or modify #
8 ---- # it under the terms of the GNU General Public License version 2 as #
9 ---- # published by the Free Software Foundation. #
10 ---- # #
11 ---- # This program is distributed in the hope that it will be useful #
12 ---- # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 ---- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 ---- # GNU General Public License for more details. #
15 ---- # #
16 ---- #########################################################################
17 local devices = { }
18 local lineIndex = 1
19 local pageOffset = 0
21 local function createDevice(id, name)
22 local device = {
23 id = id,
24 name = name,
25 timeout = 0
27 return device
28 end
30 local function getDevice(name)
31 for i=1, #devices do
32 if devices[i].name == name then
33 return devices[i]
34 end
35 end
36 return nil
37 end
39 local function parseDeviceInfoMessage(data)
40 local id = data[2]
41 local name = ""
42 local i = 3
43 while data[i] ~= 0 do
44 name = name .. string.char(data[i])
45 i = i + 1
46 end
47 local device = getDevice(name)
48 if device == nil then
49 device = createDevice(id, name)
50 devices[#devices + 1] = device
51 end
52 local time = getTime()
53 device.timeout = time + 3000 -- 30s
54 if lineIndex == 0 then
55 lineIndex = 1
56 end
57 end
59 local devicesRefreshTimeout = 0
60 local function refreshNext()
61 local command, data = crossfireTelemetryPop()
62 if command == nil then
63 local time = getTime()
64 if time > devicesRefreshTimeout then
65 devicesRefreshTimeout = time + 100 -- 1s
66 crossfireTelemetryPush(0x28, { 0x00, 0xEA })
67 end
68 elseif command == 0x29 then
69 parseDeviceInfoMessage(data)
70 end
71 end
73 local function selectDevice(step)
74 lineIndex = 1 + ((lineIndex + step - 1 + #devices) % #devices)
75 end
77 -- Init
78 local function init()
79 lineIndex = 0
80 pageOffset = 0
81 end
83 NoCross = { 110, LCD_H - 28, "Waiting for Crossfire devices...", TEXT_COLOR + INVERS + BLINK }
85 -- Main
86 local function run(event)
87 if event == nil then
88 error("Cannot be run as a model script!")
89 return 2
90 elseif event == EVT_EXIT_BREAK then
91 return 2
92 elseif event == EVT_ROT_LEFT then
93 selectDevice(1)
94 elseif event == EVT_ROT_RIGHT then
95 selectDevice(-1)
96 end
98 lcd.clear()
99 lcd.drawFilledRectangle(0, 0, LCD_W, 30, TITLE_BGCOLOR)
100 lcd.drawText(1, 5,"CROSSFIRE SETUP", MENU_TITLE_COLOR)
103 if #devices == 0 then
104 lcd.drawText(NoCross[1],NoCross[2],NoCross[3],NoCross[4])
105 else
106 for i=1, #devices do
107 local attr = (lineIndex == i and INVERS or 0)
108 if event == EVT_ROT_BREAK and attr == INVERS then
109 crossfireTelemetryPush(0x28, { devices[i].id, 0xEA })
110 return "device.lua"
112 lcd.drawText(5, i*22+10, devices[i].name, attr)
116 refreshNext()
118 return 0
121 return { init=init, run=run }