S6R Lua script fixes

This commit is contained in:
Bertrand Songis
2016-05-18 21:50:17 +02:00
parent 633af68fa9
commit 5d42c437a8
2 changed files with 58 additions and 44 deletions
+58 -44
View File
@@ -8,8 +8,9 @@ local page = 1
local current = 1 local current = 1
local refreshState = 0 local refreshState = 0
local refreshIndex = 0 local refreshIndex = 0
local calibrationState = 0
local pageOffset = 0 local pageOffset = 0
local calibrationStep = 1 local calibrationStep = 0
local pages = { } local pages = { }
local configFields = { local configFields = {
@@ -66,7 +67,7 @@ end
local function selectPage(step) local function selectPage(step)
page = 1 + ((page + step - 1 + #pages) % #pages) page = 1 + ((page + step - 1 + #pages) % #pages)
refreshIndex = 0 refreshIndex = 0
calibrationStep = 1 calibrationStep = 0
end end
-- Select the next or previous editable field -- Select the next or previous editable field
@@ -118,60 +119,72 @@ local function redrawFieldsPage()
end end
end end
local function telemetryRead(field)
return telemetryPush(0x1A, 0x30, 0x0C30, field)
end
local function telemetryWrite(field, value)
return telemetryPush(0x1A, 0x31, 0x0C30, field + value*256)
end
local telemetryPopTimeout = 0 local telemetryPopTimeout = 0
local function refreshNext() local function refreshNext()
if refreshState == 0 then if refreshState == 0 then
if refreshIndex < #fields 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] field = fields[refreshIndex + 1]
if telemetryPush(0x1A, 0x30, 0x0C30, field[3]) == true then if telemetryRead(field[3]) == true then
refreshState = 1 refreshState = 1
telemetryPopTimeout = getTime() + 80 -- normal delay is 500ms telemetryPopTimeout = getTime() + 80 -- normal delay is 500ms
end end
end end
elseif refreshState == 1 then elseif refreshState == 1 then
field = fields[refreshIndex + 1]
physicalId, primId, dataId, value = telemetryPop() physicalId, primId, dataId, value = telemetryPop()
if physicalId == 0x1A and primId == 0x32 and dataId == 0x0C30 and value % 256 == field[3] then if physicalId == 0x1A and primId == 0x32 and dataId == 0x0C30 then
value = math.floor(value / 256) fieldId = value % 256
if field[3] >= 0x9E and field[3] <= 0xA0 then if calibrationState == 2 then
b1 = value % 256 if fieldId == 0x9D then
b2 = math.floor(value / 256) refreshState = 0
value = toInt16(b1*256 + b2) calibrationState = 0
end calibrationStep = (calibrationStep + 1) % 6
if field[2] == COMBO and #field == 6 then end
for index = 1, #(field[6]), 1 do else
if value == field[6][index] then field = fields[refreshIndex + 1]
value = index - 1 if fieldId == field[3] then
break 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 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 field[2] == VALUE and #field == 8 then
value = value - field[8] + field[5]
end end
fields[refreshIndex + 1][4] = value
refreshIndex = refreshIndex + 1
refreshState = 0
elseif getTime() > telemetryPopTimeout then elseif getTime() > telemetryPopTimeout then
refreshState = 0 refreshState = 0
calibrationState = 0
end end
elseif refreshState == 2 then
physicalId, primId, dataId, value = telemetryPop()
if physicalId == 0x1A and primId == 0x32 and dataId == 0x0C30 and value % 256 == 0x9D then
refreshState = 0
calibrationStep = calibrationStep + 1
end
end end
end end
local function telemetryWrite(field, value)
telemetryPush(0x1A, 0x31, 0x0C30, field + value*256)
end
local function calibrationStart()
refreshState = 2
telemetryWrite(0x9D, calibrationStep)
end
local function updateField(field) local function updateField(field)
value = field[4] value = field[4]
if field[2] == COMBO and #field == 6 then if field[2] == COMBO and #field == 6 then
@@ -235,11 +248,11 @@ local calibrationPositions = { "up", "down", "left", "right", "forward", "back"
local function runCalibrationPage(event) local function runCalibrationPage(event)
fields = calibrationFields fields = calibrationFields
if refreshIndex == #fields then if refreshIndex == #fields then
-- refreshIndex = 0 refreshIndex = 0
end end
lcd.clear() lcd.clear()
lcd.drawScreenTitle("S6R", page, #pages) lcd.drawScreenTitle("S6R", page, #pages)
local position = calibrationPositions[calibrationStep] local position = calibrationPositions[1 + calibrationStep]
lcd.drawText(0, 9, "Turn the receiver " .. position, 0) lcd.drawText(0, 9, "Turn the receiver " .. position, 0)
lcd.drawPixmap(10, 19, position .. ".bmp") lcd.drawPixmap(10, 19, position .. ".bmp")
for index = 1, 3, 1 do for index = 1, 3, 1 do
@@ -248,13 +261,13 @@ local function runCalibrationPage(event)
lcd.drawNumber(90, 12+10*index, field[4]/10, LEFT+PREC2) lcd.drawNumber(90, 12+10*index, field[4]/10, LEFT+PREC2)
end end
attr = refreshState == 2 and 0 or INVERS attr = calibrationState == 0 and INVERS or 0
lcd.drawText(0, 56, "Press [Enter] when ready", attr) lcd.drawText(0, 56, "Press [Enter] when ready", attr)
if refreshState == 0 and event == EVT_ENTER_BREAK then if event == EVT_ENTER_BREAK then
calibrationStart() calibrationState = 1
elseif event == EVT_EXIT_BREAK then elseif event == EVT_EXIT_BREAK then
if calibrationStep > 1 then if calibrationStep > 0 then
calibrationStep = 1 calibrationStep = 0
else else
return 2 return 2
end end
@@ -280,6 +293,7 @@ local function run(event)
elseif event == EVT_PAGE_BREAK then elseif event == EVT_PAGE_BREAK then
selectPage(1) selectPage(1)
elseif event == EVT_PAGE_LONG then elseif event == EVT_PAGE_LONG then
killEvents(event);
selectPage(-1) selectPage(-1)
end end