[Crossfire] Fields update now OK (for text selection)

This commit is contained in:
Bertrand Songis
2016-06-22 07:29:21 +02:00
parent 155656c88a
commit 0bcd0a0c09
+63 -138
View File
@@ -1,63 +1,47 @@
local VALUE = 0 local COLUMN_2 = 140
local COMBO = 1
local COLUMN_2 = 150
local edit = false local edit = false
local page = 1 local pageIndex = 1
local current = 1 local fieldIndex = 1
local refreshState = 0
local refreshIndex = 0
local calibrationState = 0
local pageOffset = 0 local pageOffset = 0
local calibrationStep = 0
local pages = { } local pages = { }
-- Change display attribute to current field -- Change display attribute to current field
local function addField(step) local function addField(step)
field = fields[current] local fields = pages[pageIndex].fields
if field[2] == VALUE then local field = fields[fieldIndex]
min = field[5] if field.type == 9 then
max = field[6]
elseif field[2] == COMBO then
min = 0 min = 0
max = #(field[5]) - 1 max = #field.values - 1
end end
if (step < 0 and field[4] > min) or (step > 0 and field[4] < max) then if (step < 0 and field.value > min) or (step > 0 and field.value < max) then
field[4] = field[4] + step field.value = field.value + step
end end
end end
-- Select the next or previous page -- Select the next or previous page
local function selectPage(step) local function selectPage(step)
page = 1 + ((page + step - 1 + #pages) % #pages) pageIndex = 1 + ((pageIndex + step - 1 + #pages) % #pages)
refreshIndex = 0
calibrationStep = 0
end end
-- Select the next or previous editable field -- Select the next or previous editable field
local function selectField(step) local function selectField(step)
current = 1 + ((current + step - 1 + #fields) % #fields) local fields = pages[pageIndex].fields
if current > 7 + pageOffset then local newFieldIndex = fieldIndex
pageOffset = current - 7 repeat
elseif current <= pageOffset then newFieldIndex = 1 + ((newFieldIndex + step - 1 + #fields) % #fields)
pageOffset = current - 1 until newFieldIndex == fieldIndex or (pages[pageIndex].fields[newFieldIndex].type ~= 11 and pages[pageIndex].fields[newFieldIndex].name ~= nil)
fieldIndex = newFieldIndex
if fieldIndex > 7 + pageOffset then
pageOffset = fieldIndex - 7
elseif fieldIndex <= pageOffset then
pageOffset = fieldIndex - 1
end end
end end
local function drawProgressBar()
local width = (140 * refreshIndex) / #fields
lcd.drawRectangle(30, 1, 144, 6)
lcd.drawFilledRectangle(32, 3, width, 2);
end
local function drawDevicePage(page) local function drawDevicePage(page)
-- if refreshIndex < #fields then
-- drawProgressBar()
-- end
for index = 1, 7, 1 do for index = 1, 7, 1 do
field = page.fields[pageOffset+index] local field = page.fields[pageOffset+index]
if field == nil then if field == nil then
break break
end end
@@ -65,7 +49,7 @@ local function drawDevicePage(page)
if field.name == nil then if field.name == nil then
lcd.drawText(0, 1+8*index, "...") lcd.drawText(0, 1+8*index, "...")
else else
attr = current == (pageOffset+index) and ((edit == true and BLINK or 0) + INVERS) or 0 attr = fieldIndex == (pageOffset+index) and ((edit == true and BLINK or 0) + INVERS) or 0
lcd.drawText(0, 1+8*index, field.name) lcd.drawText(0, 1+8*index, field.name)
if field.functions ~= nil then if field.functions ~= nil then
field.functions.display(field, 1+8*index, attr) field.functions.display(field, 1+8*index, attr)
@@ -120,7 +104,7 @@ local function parseDeviceInfoMessage(data)
fields_count = data[i+13] fields_count = data[i+13]
pg = getPage(name) pg = getPage(name)
if pg == nil then if pg == nil then
pg = createPage(id, name, fields_count-1) -- TODO fields_count should be different pg = createPage(id, name, fields_count)
pages[#pages + 1] = pg pages[#pages + 1] = pg
end end
pg.pagetimeout = time + 3000 -- 30s pg.pagetimeout = time + 3000 -- 30s
@@ -146,6 +130,10 @@ local function fieldTextSelectionLoad(field, data, offset)
field.value = data[offset] field.value = data[offset]
end end
local function fieldTextSelectionSave(fieldIndex)
crossfireTelemetryPush(0x2D, { pages[pageIndex].id, 0xEA, fieldIndex, pages[pageIndex].fields[fieldIndex].value })
end
local function fieldTextSelectionDisplay(field, y, attr) local function fieldTextSelectionDisplay(field, y, attr)
lcd.drawText(COLUMN_2, y, field.values[field.value+1], attr) lcd.drawText(COLUMN_2, y, field.values[field.value+1], attr)
end end
@@ -160,24 +148,25 @@ local types_functions = {
nil, nil,
nil, nil,
nil, nil,
{ load=fieldTextSelectionLoad, display=fieldTextSelectionDisplay }, { load=fieldTextSelectionLoad, save=fieldTextSelectionSave, display=fieldTextSelectionDisplay },
nil, nil,
nil nil
} }
local function parseParameterInfoMessage(data) local function parseParameterInfoMessage(data)
index = data[3] local page = pages[pageIndex]
field = pages[page].fields[index] local index = data[3]
local field = page.fields[index]
field.parent = data[4] field.parent = data[4]
field.type = data[5] field.type = data[5]
field.functions = types_functions[field.type+1] field.functions = types_functions[field.type+1]
parent = field.parent local parent = field.parent
name = "" local name = ""
while parent ~= 0 do while parent ~= 0 do
name = name .. " " name = name .. " "
parent = pages[page].fields[parent].parent parent = page.fields[parent].parent
end end
i = 6 local i = 6
while data[i] ~= 0 do while data[i] ~= 0 do
name = name .. string.char(data[i]) name = name .. string.char(data[i])
i = i + 1 i = i + 1
@@ -187,10 +176,12 @@ local function parseParameterInfoMessage(data)
if field.functions ~= nil then if field.functions ~= nil then
field.functions.load(field, data, i) field.functions.load(field, data, i)
end end
pages[page].fieldstimeout = 0 if field.type ~= 11 and page.fields[fieldIndex].type == 11 then
fieldIndex = index
end
page.fieldstimeout = 0
end end
local telemetryPopTimeout = 0
local devicesRefreshTimeout = 0 local devicesRefreshTimeout = 0
local function refreshNext() local function refreshNext()
command, data = crossfireTelemetryPop() command, data = crossfireTelemetryPop()
@@ -203,12 +194,12 @@ local function refreshNext()
devicesRefreshTimeout = time + 1000 -- 10s devicesRefreshTimeout = time + 1000 -- 10s
end end
crossfireTelemetryPush(0x28, { 0x00, 0xEA }) crossfireTelemetryPush(0x28, { 0x00, 0xEA })
elseif page <= #pages and time > pages[page].fieldstimeout then elseif pageIndex <= #pages and time > pages[pageIndex].fieldstimeout then
for i=1, #pages[page].fields do for i=1, #pages[pageIndex].fields do
field = pages[page].fields[i] local field = pages[pageIndex].fields[i]
if field.name == nil then if field.name == nil then
crossfireTelemetryPush(0x2C, { pages[page].id, 0xEA, i }) crossfireTelemetryPush(0x2C, { pages[pageIndex].id, 0xEA, i })
pages[page].fieldstimeout = time + 200 -- 2s pages[pageIndex].fieldstimeout = time + 200 -- 2s
end end
end end
end end
@@ -217,82 +208,28 @@ local function refreshNext()
elseif command == 0x2B then elseif command == 0x2B then
parseParameterInfoMessage(data) parseParameterInfoMessage(data)
end end
-- if refreshState == 0 then
-- if calibrationState == 1 then
-- if telemetryWrite(0x9D, calibrationStep) == true then
-- refreshState = 1
-- calibrationState = 2
-- telemetryPopTimeout = getTime() + 80 -- normal delay is 500ms
-- end
-- elseif refreshIndex < #fields then
-- field = fields[refreshIndex + 1]
-- if telemetryRead(field[3]) == true then
-- refreshState = 1
-- telemetryPopTimeout = getTime() + 80 -- normal delay is 500ms
-- end
-- end
-- elseif refreshState == 1 then
-- physicalId, primId, dataId, value = telemetryPop()
-- if physicalId == 0x1A and primId == 0x32 and dataId == 0x0C30 then
-- fieldId = value % 256
-- if calibrationState == 2 then
-- if fieldId == 0x9D then
-- refreshState = 0
-- calibrationState = 0
-- calibrationStep = (calibrationStep + 1) % 6
-- end
-- else
-- field = fields[refreshIndex + 1]
-- if fieldId == field[3] then
-- value = math.floor(value / 256)
-- if field[3] >= 0x9E and field[3] <= 0xA0 then
-- b1 = value % 256
-- b2 = math.floor(value / 256)
-- value = b1*256 + b2
-- value = value - bit32.band(value, 0x8000) * 2
-- end
-- if field[2] == COMBO and #field == 6 then
-- for index = 1, #(field[6]), 1 do
-- if value == field[6][index] then
-- value = index - 1
-- break
-- end
-- end
-- elseif field[2] == VALUE and #field == 8 then
-- value = value - field[8] + field[5]
-- end
-- fields[refreshIndex + 1][4] = value
-- refreshIndex = refreshIndex + 1
-- refreshState = 0
-- end
-- end
-- elseif getTime() > telemetryPopTimeout then
-- refreshState = 0
-- calibrationState = 0
-- end
-- end
end
local function updateField(field)
value = field[4]
if field[2] == COMBO and #field == 6 then
value = field[6][1+value]
elseif field[2] == VALUE and #field == 8 then
value = value + field[8] - field[5]
end
telemetryWrite(field[3], value)
end end
-- Main -- Main
local function runFieldsPage(event) local function runNoDevicesPage(event)
if event == EVT_EXIT_BREAK then
return 2
end
lcd.clear()
lcd.drawText(24, 28, "Waiting for Crossfire devices...")
return 0
end
local function runDevicePage(index, event)
local page = pages[index]
if event == EVT_EXIT_BREAK then -- exit script if event == EVT_EXIT_BREAK then -- exit script
return 2 return 2
elseif event == EVT_ENTER_BREAK then -- toggle editing/selecting current field elseif event == EVT_ENTER_BREAK then -- toggle editing/selecting current field
if fields[current][4] ~= nil then local field = page.fields[fieldIndex]
if field.name ~= nil then
edit = not edit edit = not edit
if edit == false then if edit == false and field.functions.save ~= nil then
updateField(fields[current]) field.functions.save(fieldIndex)
end end
end end
elseif edit then elseif edit then
@@ -308,26 +245,16 @@ local function runFieldsPage(event)
selectField(-1) selectField(-1)
end end
end end
redrawFieldsPage()
return 0
end
local function runNoDevicesPage(event)
lcd.clear() lcd.clear()
lcd.drawText(24, 28, "Waiting for Crossfire devices...") lcd.drawScreenTitle(page.name, index, #pages)
return 0 drawDevicePage(page)
end
local function runDevicePage(index, event)
lcd.clear()
lcd.drawScreenTitle(pages[index].name, index, #pages)
drawDevicePage(pages[index])
return 0 return 0
end end
-- Init -- Init
local function init() local function init()
current, edit, refreshState, refreshIndex = 1, false, 0, 0 fieldIndex, edit = 1, false
end end
-- Main -- Main
@@ -340,14 +267,12 @@ local function run(event)
elseif event == EVT_PAGE_LONG then elseif event == EVT_PAGE_LONG then
killEvents(event); killEvents(event);
selectPage(-1) selectPage(-1)
elseif event == EVT_EXIT_BREAK then
return 2
end end
if #pages == 0 then if #pages == 0 then
result = runNoDevicesPage(event) result = runNoDevicesPage(event)
else else
result = runDevicePage(page, event) result = runDevicePage(pageIndex, event)
end end
refreshNext() refreshNext()