Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added initial CMS framework (CRSF Only) #339

Merged
merged 1 commit into from
Jun 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions src/SCRIPTS/BF/CMS/common.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
local CONST = {
bitmask = {
firstChunk = 0x80,
lastChunk = 0x40,
batchId = 0x3F,
rleCharValueMask = 0x7F,
rleCharRepeatedMask = 0x80
},
offset = {
meta = 1,
sequence = 2,
data = 3
}
}

local function cRleDecode(buf)
local dest = {}
local rpt = false
local c = nil
for i = 1, #buf do
if (rpt == false) then
c = bit32.band(buf[i], CONST.bitmask.rleCharValueMask)
if (bit32.band(buf[i], CONST.bitmask.rleCharRepeatedMask) > 0) then
rpt = true
else
dest[#dest + 1] = c
end
else
for j = 1, buf[i] do
dest[#dest + 1] = c
end
rpt = false
end
end
return dest
end

screen = {
config = nil,
buffer = {},
data = {},
batchId = 0,
sequence = 0,
reset = function()
screen.buffer = {}
screen.data = {}
screen.batchId = 0
screen.sequence = 0
end,
draw = function()
if (screen.buffer ~= nil and screen.config ~= nil and #screen.buffer > 0) then
lcd.clear()
for char = 1, #screen.buffer do
if (screen.buffer[char] ~= 32) then -- skip spaces to avoid CPU spikes
c = string.char(screen.buffer[char])
row = math.ceil(char / screen.config.cols)
col = char - ((row - 1) * screen.config.cols)
xPos = ((col - 1) * screen.config.pixelsPerChar) + screen.config.xIndent + 1
yPos = ((row - 1) * screen.config.pixelsPerRow) + screen.config.yOffset + 1
lcd.drawText(xPos, yPos, c, screen.config.textSize)
end
end
lcd.drawText(screen.config.refresh.left, screen.config.refresh.top, screen.config.refresh.text, screen.config.textSize)
end
end
}

cms = {
menuOpen = false,
init = function(cmsConfig)
screen.config = assert(cmsConfig, "Resolution not supported")
screen.reset()
protocol.cms.close()
cms.menuOpen = false
end,
open = function()
protocol.cms.open(screen.config.rows, screen.config.cols)
end,
refresh = function()
protocol.cms.refresh()
end,
close = function()
protocol.cms.close()
cms.menuOpen = false
end,
update = function()
local command, data = protocol.cms.poll()
if (command == "update") then
local firstChunk = bit32.band(data[CONST.offset.meta], CONST.bitmask.firstChunk)
local lastChunk = bit32.band(data[CONST.offset.meta], CONST.bitmask.lastChunk)
local batchId = bit32.band(data[CONST.offset.meta], CONST.bitmask.batchId)
local sequence = data[CONST.offset.sequence]
local frameData = {}
for i = CONST.offset.data, #data do
frameData[#frameData + 1] = data[i]
end
if (firstChunk ~= 0) then
screen.reset()
screen.batchId = batchId
screen.sequence = 0
end
if (screen.batchId == batchId) and (screen.sequence == sequence) then
screen.sequence = sequence + 1
for i = 1, #frameData do
screen.data[#screen.data + 1] = frameData[i]
end
if (lastChunk ~= 0) then
screen.buffer = cRleDecode(screen.data)
screen.draw()
screen.reset()
end
else
protocol.cms.refresh()
end
cms.menuOpen = true
elseif (command == "clear") then
screen.reset()
end
end
}
64 changes: 64 additions & 0 deletions src/SCRIPTS/BF/CMS/crsf.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
local CONST = {
frame = {
destination = 1,
source = 2,
command = 3,
content = 4
},
address = {
transmitter = 0xEA,
betaflight = 0xC8
},
frameType = {
displayPort = 0x7D
},
command = {
update = 0x01,
clear = 0x02,
open = 0x03,
close = 0x04,
refresh = 0x05
}
}

local function crsfDisplayPortCmd(cmd, data)
local payloadOut = { CONST.address.betaflight, CONST.address.transmitter, cmd }
if data ~= nil then
for i = 1, #(data) do
payloadOut[3 + i] = data[i]
end
end
return crossfireTelemetryPush(CONST.frameType.displayPort, payloadOut)
end

protocol.cms.poll = function()
local command = nil
local dataOut = {}
local frameType, data = crossfireTelemetryPop()
if (data ~= nil) and (#data > 2) then
if (frameType == CONST.frameType.displayPort) and (data[CONST.frame.destination] == CONST.address.transmitter) and (data[CONST.frame.source] == CONST.address.betaflight) then
for k,v in pairs(CONST.command) do
if (v == data[CONST.frame.command]) then
command = k
end
end
for i = CONST.frame.content, #data do
dataOut[#dataOut + 1] = data[i]
end
end
end
return command, dataOut
end

protocol.cms.open = function(rows, cols)
return crsfDisplayPortCmd(CONST.command.open, { rows, cols })
end

protocol.cms.close = function()
return crsfDisplayPortCmd(CONST.command.close, nil)
end

protocol.cms.refresh = function()
return crsfDisplayPortCmd(CONST.command.refresh, nil)
end

23 changes: 23 additions & 0 deletions src/SCRIPTS/BF/cms.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
lastMenuEventTime = 0

local function init()
cms.init(radio)
end

local function run(event)
lastMenuEventTime = getTime()
cms.update()
if (cms.menuOpen == false) then
cms.open()
end
if (event == radio.refresh.event) then
cms.refresh()
end
if (event == EVT_VIRTUAL_EXIT) then
cms.close()
return 1
end
return 0
end

return { init=init, run=run }
11 changes: 7 additions & 4 deletions src/SCRIPTS/BF/protocols.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@ local supportedProtocols =
{
smartPort =
{
transport = "MSP/sp.lua",
mspTransport = "MSP/sp.lua",
rssi = function() return getValue("RSSI") end,
stateSensor = "Tmp1",
push = sportTelemetryPush,
maxTxBufferSize = 6,
maxRxBufferSize = 6,
saveMaxRetries = 2,
saveTimeout = 500
saveTimeout = 500,
cms = {},
},
crsf =
{
transport = "MSP/crsf.lua",
mspTransport = "MSP/crsf.lua",
cmsTransport = "CMS/crsf.lua",
rssi = function() return getValue("TQly") end,
stateSensor = "1RSS",
push = crossfireTelemetryPush,
maxTxBufferSize = 8,
maxRxBufferSize = 58,
saveMaxRetries = 2,
saveTimeout = 150
saveTimeout = 150,
cms = {},
}
}

Expand Down
114 changes: 84 additions & 30 deletions src/SCRIPTS/BF/radios.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,99 @@ local supportedRadios =
{
["128x64"] =
{
templateHome = "TEMPLATES/128x64/",
MenuBox = { x=15, y=12, w=100, x_offset=36, h_line=8, h_offset=3 },
SaveBox = { x=15, y=12, w=100, x_offset=4, h=30, h_offset=5 },
NoTelem = { 30, 55, "No Telemetry", BLINK },
textSize = SMLSIZE,
yMinLimit = 12,
yMaxLimit = 52,
msp = {
templateHome = "TEMPLATES/128x64/",
MenuBox = { x=15, y=12, w=100, x_offset=36, h_line=8, h_offset=3 },
SaveBox = { x=15, y=12, w=100, x_offset=4, h=30, h_offset=5 },
NoTelem = { 30, 55, "No Telemetry", BLINK },
textSize = SMLSIZE,
yMinLimit = 12,
yMaxLimit = 52,
},
cms = {
rows = 8,
cols = 26,
pixelsPerRow = 8,
pixelsPerChar = 5,
xIndent = 0,
yOffset = 0,
textSize = SMLSIZE,
refresh = {
event = EVT_VIRTUAL_ENTER,
text = "Refresh: [ENT]",
top = 1,
left = 64,
},
},
},
["212x64"] =
{
templateHome = "TEMPLATES/212x64/",
MenuBox = { x=40, y=12, w=120, x_offset=36, h_line=8, h_offset=3 },
SaveBox = { x=40, y=12, w=120, x_offset=4, h=30, h_offset=5 },
NoTelem = { 70, 55, "No Telemetry", BLINK },
textSize = SMLSIZE,
yMinLimit = 12,
yMaxLimit = 52,
msp = {
templateHome = "TEMPLATES/212x64/",
MenuBox = { x=40, y=12, w=120, x_offset=36, h_line=8, h_offset=3 },
SaveBox = { x=40, y=12, w=120, x_offset=4, h=30, h_offset=5 },
NoTelem = { 70, 55, "No Telemetry", BLINK },
textSize = SMLSIZE,
yMinLimit = 12,
yMaxLimit = 52,
},
cms = {
rows = 8,
cols = 32,
pixelsPerRow = 8,
pixelsPerChar = 6,
xIndent = 0,
yOffset = 0,
textSize = SMLSIZE,
refresh = {
event = EVT_VIRTUAL_INC,
text = "Refresh: [+]",
top = 1,
left = 156,
}
},
},
["480x272"] =
{
templateHome = "TEMPLATES/480x272/",
highRes = true,
MenuBox = { x=120, y=100, w=200, x_offset=68, h_line=20, h_offset=6 },
SaveBox = { x=120, y=100, w=180, x_offset=12, h=60, h_offset=12 },
NoTelem = { 192, LCD_H - 28, "No Telemetry", (TEXT_COLOR or 0) + INVERS + BLINK },
textSize = 0,
yMinLimit = 35,
yMaxLimit = 235,
msp = {
templateHome = "TEMPLATES/480x272/",
highRes = true,
MenuBox = { x=120, y=100, w=200, x_offset=68, h_line=20, h_offset=6 },
SaveBox = { x=120, y=100, w=180, x_offset=12, h=60, h_offset=12 },
NoTelem = { 192, LCD_H - 28, "No Telemetry", (TEXT_COLOR or 0) + INVERS + BLINK },
textSize = 0,
yMinLimit = 35,
yMaxLimit = 235,
},
cms = {
rows = 9,
cols = 32,
pixelsPerRow = 24,
pixelsPerChar = 14,
xIndent = 14,
yOffset = 32,
textSize = MIDSIZE,
refresh = {
event = EVT_VIRTUAL_ENTER,
text = "Refresh: [ENT]",
top = 1,
left = 300,
}
},
},
["320x480"] =
{
templateHome = "TEMPLATES/320x480/",
highRes = true,
MenuBox = { x= (LCD_W -200)/2, y=LCD_H/2, w=200, x_offset=68, h_line=20, h_offset=6 },
SaveBox = { x= (LCD_W -200)/2, y=LCD_H/2, w=180, x_offset=12, h=60, h_offset=12 },
NoTelem = { LCD_W/2 - 50, LCD_H - 28, "No Telemetry", (TEXT_COLOR or 0) + INVERS + BLINK },
textSize = 0,
yMinLimit = 35,
yMaxLimit = 435,
msp = {
templateHome = "TEMPLATES/320x480/",
highRes = true,
MenuBox = { x= (LCD_W -200)/2, y=LCD_H/2, w=200, x_offset=68, h_line=20, h_offset=6 },
SaveBox = { x= (LCD_W -200)/2, y=LCD_H/2, w=180, x_offset=12, h=60, h_offset=12 },
NoTelem = { LCD_W/2 - 50, LCD_H - 28, "No Telemetry", (TEXT_COLOR or 0) + INVERS + BLINK },
textSize = 0,
yMinLimit = 35,
yMaxLimit = 435,
},
cms = nil,
},
}

Expand Down
2 changes: 1 addition & 1 deletion src/SCRIPTS/FUNCTIONS/bfbkgd.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
chdir("/SCRIPTS/BF")
apiVersion = 0
protocol = assert(loadScript("protocols.lua"))()
assert(loadScript(protocol.transport))()
assert(loadScript(protocol.mspTransport))()
assert(loadScript("MSP/common.lua"))()
local background = assert(loadScript("background.lua"))()

Expand Down
4 changes: 2 additions & 2 deletions src/SCRIPTS/TOOLS/bf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ local scriptsCompiled = assert(loadScript("COMPILE/scripts_compiled.lua"))()

if scriptsCompiled then
protocol = assert(loadScript("protocols.lua"))()
radio = assert(loadScript("radios.lua"))()
assert(loadScript(protocol.transport))()
radio = assert(loadScript("radios.lua"))().msp
assert(loadScript(protocol.mspTransport))()
assert(loadScript("MSP/common.lua"))()
run = assert(loadScript("ui.lua"))()
else
Expand Down
Loading