betaflight / betaflight-tx-lua-scripts

Collection of scripts to configure Betaflight from your TX (currently only supported in OpenTx)
GNU General Public License v3.0
590 stars 142 forks source link

OSD Pos Page #462

Closed druckgott closed 1 year ago

druckgott commented 1 year ago

Hallo, I want to do a page to position the osd elements. I do a test page for one element. What I can get working, is reading out the values for osd_rssi_pos (my first test example), but I do not realy now what I need for save. At first I think I have to combine all again together to one value (Position, Profil 1, Profil 2, Profil 3) like here: https://github.com/betaflight/betaflight/blob/master/docs/OSD%20Profiles.md Thats also how I get the position and in which profil the OSD Element is aktiv. But on saveing I have to do a function (thats what I think) with the new value (Position, Profil 1, Profil 2, Profil 3) + any addr and maybe more things? I think here I have to start: https://github.com/betaflight/betaflight/blob/c78e14cc2766ddccf43826d7c5311e405e4c56b2/src/main/msp/msp.c#L4033 But then I need this "addr" = ?, "screen" > 1 and the pos value. If I read out the addr it´s always 1. Can somebody help me where I have to start, or what I all have to send to save exact one OSD Element?

Here is my current page code (not formated currently only for testing)


--[[
https://github.com/betaflight/betaflight-tx-lua-scripts/issues/448
https://github.com/betaflight/betaflight/blob/c78e14cc2766ddccf43826d7c5311e405e4c56b2/src/main/msp/msp.c
https://github.com/betaflight/betaflight-tx-lua-scripts/blob/2f733a4b45a9bcbecad059dd6d743f9dcc304ff1/src/SCRIPTS/BF/PAGES/filters2.lua
https://github.com/betaflight/betaflight/blob/c78e14cc2766ddccf43826d7c5311e405e4c56b2/src/main/msp/msp.c
https://github.com/betaflight/betaflight/blob/d01447c025508c1ba481ca3bdb33128bca99708b/src/main/cli/settings.c
https://github.com/betaflight/betaflight/blob/c78e14cc2766ddccf43826d7c5311e405e4c56b2/src/main/msp/msp.c
https://github.com/betaflight/betaflight/blob/c78e14cc2766ddccf43826d7c5311e405e4c56b2/src/main/osd/osd.h
https://github.com/betaflight/betaflight/blob/master/docs/OSD%20Profiles.md
--]]

local template = assert(loadScript(radio.template))()
local margin = template.margin
local indent = template.indent
local lineSpacing = template.lineSpacing
local tableSpacing = template.tableSpacing
local sp = template.listSpacing.field
local yMinLim = radio.yMinLimit
local x = margin
local y = yMinLim - lineSpacing
local inc = { x = function(val) x = x + val return x end, y = function(val) y = y + val return y end }
local labels = {}
local fields = {}

if apiVersion >= 1.041 then

    val_1 = 11
    val_2 = 12

    x = margin
    y = yMinLim - tableSpacing.header

    labels[#labels + 1] = { t = "",         x = x, y = inc.y(tableSpacing.header) }
    labels[#labels + 1] = { t = "rssi", x = x, y = inc.y(tableSpacing.row) }

    x = x + tableSpacing.col*0.7
    y = yMinLim - tableSpacing.header

    labels[#labels + 1] = { t = "POS",      x = x, y = inc.y(tableSpacing.header) }
    fields[#fields + 1] = {                 x = x, y = inc.y(tableSpacing.row), min = 0, max = 200, vals = { val_1, val_2 } }
    fields[#fields + 1] = {                 x = x, y = inc.y(tableSpacing.row), min = 0, max = 200, vals = { val_1, val_2 } }
    fields[#fields + 1] = { t = "adre",                 x = x, y = inc.y(lineSpacing), sp = x + sp, min = 1, max = 999999, vals = { 1 } }
    fields[#fields + 1] = { t = "OSD_ITEM_COUNT",       x = x, y = inc.y(lineSpacing), sp = x + sp, min = 0, max = 20000, vals = { 8 } }

    x = x + tableSpacing.col
    y = yMinLim - tableSpacing.header

    labels[#labels + 1] = { t = "OP1",      x = x, y = inc.y(tableSpacing.header) }
    fields[#fields + 1] = {                 x = x, y = inc.y(tableSpacing.row), min = 0, max = 1, vals = { val_1, val_2 }, table = { [0] = "OFF", "ON" } }

    x = x + tableSpacing.col
    y = yMinLim - tableSpacing.header

    labels[#labels + 1] = { t = "OP2",      x = x, y = inc.y(tableSpacing.header) }
    fields[#fields + 1] = {                 x = x, y = inc.y(tableSpacing.row), min = 0, max = 1, vals = { val_1, val_2 }, table = { [0] = "OFF", "ON" } }

    x = x + tableSpacing.col
    y = yMinLim - tableSpacing.header

    labels[#labels + 1] = { t = "OP3",      x = x, y = inc.y(tableSpacing.header) }
    fields[#fields + 1] = {                 x = x, y = inc.y(tableSpacing.row), min = 0, max = 1, vals = { val_1, val_2 }, table = { [0] = "OFF", "ON" } }

end

return {
    read        = 84, -- MSP_OSD_CONFIG
    write       = 85, -- MSP_SET_OSD_CONFIG
    title       = "OSD Test",
    reboot      = false,
    eepromWrite = true,
    minBytes    = 2,
    labels      = labels,
    fields      = fields,
    postLoad = function(self)
        self.fields[1].value, self.fields[5].value, self.fields[6].value, self.fields[7].value = self.check_hex(self, self.values[val_1], self.values[val_2])
    end,
    check_hex = function(self, value_1, value_2)
        local temp=bit32.lshift(value_1, (1-1)*8)+bit32.lshift(value_2, (2-1)*8)
        local field_1
        local field_2
        local field_3
        local field_4
        if temp > 14336 then        
            field_1 = math.floor(temp-14336)
            field_4 = math.floor(1)
            field_3 = math.floor(1)
            field_2 = math.floor(1)
        elseif temp > 12288 then
            field_1 = math.floor(temp-12288)
            field_4 = math.floor(1)
            field_3 = math.floor(1)
            field_2 = math.floor(0)
        elseif temp > 10240 then
            field_1 = math.floor(temp-10240)
            field_4 = math.floor(1)
            field_3 = math.floor(0)
            field_2 = math.floor(1)
        elseif temp > 8192 then
            field_1 = math.floor(temp-8192)
            field_4 = math.floor(1)
            field_3 = math.floor(0)
            field_2 = math.floor(0)
        elseif temp > 6144 then
            field_1 = math.floor(temp-6144)
            field_4 = math.floor(0)
            field_3 = math.floor(1)
            field_2 = math.floor(1)
        elseif temp > 4096 then
            field_1 = math.floor(temp-4096)
            field_4 = math.floor(0)
            field_3 = math.floor(1)
            field_2 = math.floor(0) 
        elseif temp > 2048 then
            field_1 = math.floor(temp-2048)
            field_4 = math.floor(0)
            field_3 = math.floor(0)
            field_2 = math.floor(1)
        else
            field_1 = math.floor(temp)
            field_4 = math.floor(0)
            field_3 = math.floor(0)
            field_2 = math.floor(0) 
        end 
        return field_1, field_2, field_3, field_4
    end 
}

BR druckgott

druckgott commented 1 year ago

Hello, I get one big step forward, I can now also save the value:

What I do not understand what the screen value contains, maybe somebody knows this? https://github.com/betaflight/betaflight/blob/c78e14cc2766ddccf43826d7c5311e405e4c56b2/src/main/msp/msp.c#L4037

--[[
https://github.com/betaflight/betaflight-tx-lua-scripts/issues/448
https://github.com/betaflight/betaflight/blob/c78e14cc2766ddccf43826d7c5311e405e4c56b2/src/main/msp/msp.c
https://github.com/betaflight/betaflight-tx-lua-scripts/blob/2f733a4b45a9bcbecad059dd6d743f9dcc304ff1/src/SCRIPTS/BF/PAGES/filters2.lua
https://github.com/betaflight/betaflight/blob/d01447c025508c1ba481ca3bdb33128bca99708b/src/main/cli/settings.c
https://github.com/betaflight/betaflight/blob/c78e14cc2766ddccf43826d7c5311e405e4c56b2/src/main/osd/osd.h
https://github.com/betaflight/betaflight/blob/master/docs/OSD%20Profiles.md
--]]

local template = assert(loadScript(radio.template))()
local margin = template.margin
local indent = template.indent
local lineSpacing = template.lineSpacing
local tableSpacing = template.tableSpacing
local sp = template.listSpacing.field
local yMinLim = radio.yMinLimit
local x = margin
local y = yMinLim - lineSpacing
local inc = { x = function(val) x = x + val return x end, y = function(val) y = y + val return y end }
local labels = {}
local fields = {}

if apiVersion >= 1.041 then

    val_1 = 11
    val_2 = 12

    x = margin
    y = yMinLim - tableSpacing.header

    labels[#labels + 1] = { t = "",         x = x, y = inc.y(tableSpacing.header) }
    labels[#labels + 1] = { t = "",         x = x, y = inc.y(tableSpacing.header) }
    labels[#labels + 1] = { t = "rssi", x = x, y = inc.y(tableSpacing.row) }

    x = x + tableSpacing.col*0.7
    y = yMinLim - tableSpacing.header

    fields[#fields + 1] = { t = "adre",     x = x, y = inc.y(lineSpacing), sp = x + sp, min = 0, max = 999999, vals = { 1 } }
    labels[#labels + 1] = { t = "POS",      x = x, y = inc.y(tableSpacing.header) }
    fields[#fields + 1] = {                 x = x, y = inc.y(tableSpacing.row), min = 0, max = 999999, vals = { 2, 3 } }
    fields[#fields + 1] = {                 x = x, y = inc.y(tableSpacing.row), min = 0, max = 999999, vals = { val_1, val_2 } }

    --fields[#fields + 1] = { t = "OSD_ITEM_COUNT",       x = x, y = inc.y(lineSpacing), sp = x + sp, min = 0, max = 20000, vals = { 8 } }
    fields[#fields + 1] = { t = "Screen",       x = x, y = inc.y(lineSpacing), sp = x + sp, min = 0, max = 20000, vals = { 4 } }

    x = x + tableSpacing.col
    y = yMinLim - tableSpacing.header

    labels[#labels + 1] = { t = "",      x = x, y = inc.y(tableSpacing.header) }
    labels[#labels + 1] = { t = "OP1",      x = x, y = inc.y(tableSpacing.header) }
    fields[#fields + 1] = {                 x = x, y = inc.y(tableSpacing.row), min = 0, max = 1, vals = { val_1, val_2 }, table = { [0] = "OFF", "ON" } }

    x = x + tableSpacing.col
    y = yMinLim - tableSpacing.header

    labels[#labels + 1] = { t = "",      x = x, y = inc.y(tableSpacing.header) }
    labels[#labels + 1] = { t = "OP2",      x = x, y = inc.y(tableSpacing.header) }
    fields[#fields + 1] = {                 x = x, y = inc.y(tableSpacing.row), min = 0, max = 1, vals = { val_1, val_2 }, table = { [0] = "OFF", "ON" } }

    x = x + tableSpacing.col
    y = yMinLim - tableSpacing.header

    labels[#labels + 1] = { t = "",      x = x, y = inc.y(tableSpacing.header) }
    labels[#labels + 1] = { t = "OP3",      x = x, y = inc.y(tableSpacing.header) }
    fields[#fields + 1] = {                 x = x, y = inc.y(tableSpacing.row), min = 0, max = 1, vals = { val_1, val_2 }, table = { [0] = "OFF", "ON" } }

end

return {
    read        = 84, -- MSP_OSD_CONFIG
    write       = 85, -- MSP_SET_OSD_CONFIG
    title       = "OSD Test",
    reboot      = false,
    eepromWrite = true,
    minBytes    = 2,
    labels      = labels,
    fields      = fields,
    postLoad = function(self)
    -- adresse
        self.fields[1].value = 0
    -- 2 und weitere is pos wert + Profil on/off
        self.fields[2].value, self.fields[5].value, self.fields[6].value, self.fields[7].value = self.check_hex(self, self.values[val_1], self.values[val_2])
    -- screen 1 
        self.fields[4].value = 1
    end,
    preSave = function(self)
        local writetemp
    -- adresse
        self.values[1] = self.fields[1].value
    -- dieser Wert wird gespeichert als OSD Pos 
        writetemp = self.check_profil(self, self.fields[2].value, self.fields[5].value, self.fields[6].value, self.fields[7].value)
        self.values[2] = bit32.lshift(writetemp, (1-1)*8)
        self.values[3] = bit32.rshift(writetemp, (2-1)*8)
    -- screen value ??? k/a fuer was diese ist
        self.values[4] = self.fields[4].value
        return self.values
    end,
    -- value und Profil in Gesamtwert umrechnen
    check_profil = function(self, value, pro_1, pro_2, pro_3)
        local output
        if (pro_3 == 1 and pro_2 == 1 and pro_1 == 1) then      
            output = math.floor(value+14336)
        elseif (pro_3 == 1 and pro_2 == 1 and pro_1 == 0) then
            output = math.floor(value+12288)
        elseif (pro_3 == 1 and pro_2 == 0 and pro_1 == 1) then
            output = math.floor(value+10240)
        elseif (pro_3 == 1 and pro_2 == 0 and pro_1 == 0) then
            output = math.floor(value+8192)
        elseif (pro_3 == 0 and pro_2 == 1 and pro_1 == 1) then
            output = math.floor(value+6144)
        elseif (pro_3 == 0 and pro_2 == 1 and pro_1 == 0) then
            output = math.floor(value+4096)
        elseif (pro_3 == 0 and pro_2 == 0 and pro_1 == 1) then
            output = math.floor(value+2048)
        else
            output = math.floor(value)  
        end 
        return output
    end,
    -- Gesamtwert in Pos und Profile zerlegen
    check_hex = function(self, value_1, value_2)
        local temp=bit32.lshift(value_1, (1-1)*8)+bit32.lshift(value_2, (2-1)*8)
        local field_1
        local field_2
        local field_3
        local field_4
        if temp > 14336 then        
            field_1 = math.floor(temp-14336)
            field_4 = math.floor(1)
            field_3 = math.floor(1)
            field_2 = math.floor(1)
        elseif temp > 12288 then
            field_1 = math.floor(temp-12288)
            field_4 = math.floor(1)
            field_3 = math.floor(1)
            field_2 = math.floor(0)
        elseif temp > 10240 then
            field_1 = math.floor(temp-10240)
            field_4 = math.floor(1)
            field_3 = math.floor(0)
            field_2 = math.floor(1)
        elseif temp > 8192 then
            field_1 = math.floor(temp-8192)
            field_4 = math.floor(1)
            field_3 = math.floor(0)
            field_2 = math.floor(0)
        elseif temp > 6144 then
            field_1 = math.floor(temp-6144)
            field_4 = math.floor(0)
            field_3 = math.floor(1)
            field_2 = math.floor(1)
        elseif temp > 4096 then
            field_1 = math.floor(temp-4096)
            field_4 = math.floor(0)
            field_3 = math.floor(1)
            field_2 = math.floor(0) 
        elseif temp > 2048 then
            field_1 = math.floor(temp-2048)
            field_4 = math.floor(0)
            field_3 = math.floor(0)
            field_2 = math.floor(1)
        else
            field_1 = math.floor(temp)
            field_4 = math.floor(0)
            field_3 = math.floor(0)
            field_2 = math.floor(0) 
        end 
        return field_1, field_2, field_3, field_4
    end 
}
druckgott commented 1 year ago

I add a comit this can be closed