Skip to content

Commit 423b026

Browse files
authored
Merge pull request #310 from klutvott123/memory-saving
Memory saving + ui.lua size reduction
2 parents f5e98a6 + edd84df commit 423b026

File tree

10 files changed

+109
-175
lines changed

10 files changed

+109
-175
lines changed

src/SCRIPTS/BF/MSP/common.lua

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ local MSP_STARTFLAG = bit32.lshift(1,4)
66
local mspSeq = 0
77
local mspRemoteSeq = 0
88
local mspRxBuf = {}
9-
local mspRxIdx = 1
109
local mspRxCRC = 0
1110
local mspRxReq = 0
1211
local mspStarted = false
1312
local mspLastReq = 0
1413
local mspTxBuf = {}
1514
local mspTxIdx = 1
1615
local mspTxCRC = 0
17-
local mspTxPk = 0
1816

1917
function mspProcessTxQ()
2018
if (#(mspTxBuf) == 0) then
@@ -31,10 +29,7 @@ function mspProcessTxQ()
3129
payload[1] = payload[1] + MSP_STARTFLAG
3230
end
3331
local i = 2
34-
while (i <= protocol.maxTxBufferSize) do
35-
if mspTxIdx > #(mspTxBuf) then
36-
break
37-
end
32+
while (i <= protocol.maxTxBufferSize) and mspTxIdx <= #mspTxBuf do
3833
payload[i] = mspTxBuf[mspTxIdx]
3934
mspTxIdx = mspTxIdx + 1
4035
mspTxCRC = bit32.bxor(mspTxCRC,payload[i])
@@ -48,17 +43,13 @@ function mspProcessTxQ()
4843
payload[i] = 0
4944
i = i + 1
5045
end
51-
if protocol.mspSend(payload) then
52-
mspTxPk = mspTxPk + 1
53-
end
46+
protocol.mspSend(payload)
5447
mspTxBuf = {}
5548
mspTxIdx = 1
56-
mspTxCRC = 0
49+
mspTxCRC = 0
5750
return false
5851
end
59-
if protocol.mspSend(payload) then
60-
mspTxPk = mspTxPk + 1
61-
end
52+
protocol.mspSend(payload)
6253
return true
6354
end
6455

@@ -90,7 +81,6 @@ function mspReceivedReply(payload)
9081
local seq = bit32.band(head,0x0F)
9182
if start then
9283
-- start flag set
93-
mspRxIdx = 1
9484
mspRxBuf = {}
9585
mspRxSize = payload[idx]
9686
mspRxCRC = bit32.bxor(mspRxSize,mspLastReq)
@@ -103,28 +93,26 @@ function mspReceivedReply(payload)
10393
mspStarted = false
10494
return nil
10595
end
106-
while (idx <= protocol.maxRxBufferSize) and (mspRxIdx <= mspRxSize) do
107-
mspRxBuf[mspRxIdx] = payload[idx]
96+
while (idx <= protocol.maxRxBufferSize) and (#mspRxBuf < mspRxSize) do
97+
mspRxBuf[#mspRxBuf + 1] = payload[idx]
10898
mspRxCRC = bit32.bxor(mspRxCRC,payload[idx])
109-
mspRxIdx = mspRxIdx + 1
11099
idx = idx + 1
111100
end
112101
if idx > protocol.maxRxBufferSize then
113102
mspRemoteSeq = seq
114103
return true
115104
end
105+
mspStarted = false
116106
-- check CRC
117107
if mspRxCRC ~= payload[idx] then
118-
mspStarted = false
119108
return nil
120109
end
121-
mspStarted = false
122110
return mspRxBuf
123111
end
124112

125113
function mspPollReply()
126114
while true do
127-
ret = protocol.mspPoll()
115+
local ret = protocol.mspPoll()
128116
if type(ret) == "table" then
129117
mspLastReq = 0
130118
return mspRxReq, ret

src/SCRIPTS/BF/MSP/crsf.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ protocol.mspSend = function(payload)
1313
for i=1, #(payload) do
1414
payloadOut[i+2] = payload[i]
1515
end
16-
return crossfireTelemetryPush(crsfMspCmd, payloadOut)
16+
return protocol.push(crsfMspCmd, payloadOut)
1717
end
1818

1919
protocol.mspRead = function(cmd)

src/SCRIPTS/BF/MSP/sp.lua

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ local REPLY_FRAME_ID = 0x32
77
local lastSensorId, lastFrameId, lastDataId, lastValue
88

99
protocol.mspSend = function(payload)
10-
local dataId = 0
11-
dataId = payload[1] + bit32.lshift(payload[2],8)
12-
local value = 0
13-
value = payload[3] + bit32.lshift(payload[4],8)
10+
local dataId = payload[1] + bit32.lshift(payload[2],8)
11+
local value = payload[3] + bit32.lshift(payload[4],8)
1412
+ bit32.lshift(payload[5],16) + bit32.lshift(payload[6],24)
15-
return sportTelemetryPush(LOCAL_SENSOR_ID, REQUEST_FRAME_ID, dataId, value)
13+
return protocol.push(LOCAL_SENSOR_ID, REQUEST_FRAME_ID, dataId, value)
1614
end
1715

1816
protocol.mspRead = function(cmd)
@@ -23,15 +21,14 @@ protocol.mspWrite = function(cmd, payload)
2321
return mspSendRequest(cmd, payload)
2422
end
2523

26-
--Discards duplicate data from lua input buffer
24+
-- Discards duplicate data from lua input buffer
2725
local function smartPortTelemetryPop()
28-
local sensorId, frameId, dataId, value
2926
while true do
30-
sensorId, frameId, dataId, value = sportTelemetryPop()
31-
if sensorId == nil then
27+
local sensorId, frameId, dataId, value = sportTelemetryPop()
28+
if not sensorId then
3229
return nil
3330
elseif (lastSensorId == sensorId) and (lastFrameId == frameId) and (lastDataId == dataId) and (lastValue == value) then
34-
--Keep checking
31+
-- Keep checking
3532
else
3633
lastSensorId = sensorId
3734
lastFrameId = frameId

src/SCRIPTS/BF/background.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ local function run_bg()
2525
-- assuming when sensor value higher than 0 there is an telemetry connection
2626
if not dataInitialised then
2727
if not data_init then
28-
data_init = assert(loadScript(SCRIPT_HOME .. "/data_init.lua"))()
28+
data_init = assert(loadScript("data_init.lua"))()
2929
end
3030

3131
dataInitialised = data_init()
@@ -37,7 +37,7 @@ local function run_bg()
3737
end
3838
elseif rssiEnabled and apiVersion >= 1.037 then
3939
if not rssiTask then
40-
rssiTask = assert(loadScript(SCRIPT_HOME.."/rssi.lua"))()
40+
rssiTask = assert(loadScript("rssi.lua"))()
4141
end
4242

4343
rssiEnabled = rssiTask()

src/SCRIPTS/BF/protocols.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ local supportedProtocols =
22
{
33
smartPort =
44
{
5-
transport = SCRIPT_HOME.."/MSP/sp.lua",
5+
transport = "MSP/sp.lua",
66
rssi = function() return getValue("RSSI") end,
77
stateSensor = "Tmp1",
88
push = sportTelemetryPush,
@@ -13,7 +13,7 @@ local supportedProtocols =
1313
},
1414
crsf =
1515
{
16-
transport = SCRIPT_HOME.."/MSP/crsf.lua",
16+
transport = "MSP/crsf.lua",
1717
rssi = function() return getValue("TQly") end,
1818
stateSensor = "1RSS",
1919
push = crossfireTelemetryPush,

src/SCRIPTS/BF/radios.lua

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
lcdResolution =
2-
{
3-
low = 0,
4-
high = 1
5-
}
6-
71
local supportedRadios =
82
{
93
["128x64"] =
104
{
11-
templateHome = SCRIPT_HOME.."/TEMPLATES/128x64/",
12-
resolution = lcdResolution.low,
5+
templateHome = "TEMPLATES/128x64/",
136
MenuBox = { x=15, y=12, w=100, x_offset=36, h_line=8, h_offset=3 },
147
SaveBox = { x=15, y=12, w=100, x_offset=4, h=30, h_offset=5 },
158
NoTelem = { 30, 55, "No Telemetry", BLINK },
@@ -19,8 +12,7 @@ local supportedRadios =
1912
},
2013
["212x64"] =
2114
{
22-
templateHome = SCRIPT_HOME.."/TEMPLATES/212x64/",
23-
resolution = lcdResolution.low,
15+
templateHome = "TEMPLATES/212x64/",
2416
MenuBox = { x=40, y=12, w=120, x_offset=36, h_line=8, h_offset=3 },
2517
SaveBox = { x=40, y=12, w=120, x_offset=4, h=30, h_offset=5 },
2618
NoTelem = { 70, 55, "No Telemetry", BLINK },
@@ -30,8 +22,8 @@ local supportedRadios =
3022
},
3123
["480x272"] =
3224
{
33-
templateHome = SCRIPT_HOME.."/TEMPLATES/480x272/",
34-
resolution = lcdResolution.high,
25+
templateHome = "TEMPLATES/480x272/",
26+
highRes = true,
3527
MenuBox = { x=120, y=100, w=200, x_offset=68, h_line=20, h_offset=6 },
3628
SaveBox = { x=120, y=100, w=180, x_offset=12, h=60, h_offset=12 },
3729
NoTelem = { 192, LCD_H - 28, "No Telemetry", (TEXT_COLOR or 0) + INVERS + BLINK },
@@ -41,8 +33,8 @@ local supportedRadios =
4133
},
4234
["320x480"] =
4335
{
44-
templateHome = SCRIPT_HOME.."/TEMPLATES/320x480/",
45-
resolution = lcdResolution.high,
36+
templateHome = "TEMPLATES/320x480/",
37+
highRes = true,
4638
MenuBox = { x= (LCD_W -200)/2, y=LCD_H/2, w=200, x_offset=68, h_line=20, h_offset=6 },
4739
SaveBox = { x= (LCD_W -200)/2, y=LCD_H/2, w=180, x_offset=12, h=60, h_offset=12 },
4840
NoTelem = { LCD_W/2 - 50, LCD_H - 28, "No Telemetry", (TEXT_COLOR or 0) + INVERS + BLINK },
@@ -52,7 +44,6 @@ local supportedRadios =
5244
},
5345
}
5446

55-
local ver, rad, maj, min, rev = getVersion()
5647
local radio = supportedRadios[tostring(LCD_W) .. "x" .. tostring(LCD_H)]
5748

5849
if not radio then

0 commit comments

Comments
 (0)