optimise mavlink SS packet size (#3029)
[ExpressLRS.git] / src / basic / ELRS.bas
blob2a49efab4298b34cb2c992c0644d48aab63db05e
1 rem -- Change ExpressLRS parameters
3 rem -- License https://www.gnu.org/licenses/gpl-3.0.en.html
5 rem -- basic script for radios running ErskTx R223A6 or later, based roughly on ELRS.lua
7 rem -- Original author: spinorkit
9 rem -- See https://openrcforums.com/forum/viewtopic.php?f=7&t=4676&sid=25e7416b867f2c94a6382fbff922d082
10 rem -- Description of the ErskyTx Basic dialect: https://openrcforums.com/forum/viewtopic.php?t=10325
11 rem -- N.B. ErskyTx Basic uses 0 based indexing into arrays in contrast to lua's 1 based indexing
13 rem -- Note: ErskyTx uses 400000 baud to talk to crossfire modules, and therefore ELRS.
14 rem -- If you have a 2018 R9M module (with the ACCST logo on the back), you need the
15 rem -- resistor mod for reliable communuication:
16 rem -- https://www.expresslrs.org/hardware/inverter-mod/
18 array byte rxBuf[64]
19 array byte transmitBuffer[64]
20 array byte commitSha[8]
21 array byte intToHex[17]
23 array byte paramNames[96]
24 array params[16]
25 array paramMaxs[16]
26 array paramMins[16]
28 array validAirRates127[8]
29 array validAirRates128[8]
30 array validAirRates[8]
33 const kAirRateParamIdx 0
34 const kTLMintervalParamIdx 1
35 const kMaxPowerParamIdx 2
36 const kRFfreqParamIdx 3
37 const kWifiUpdateParamIdx 4
38 const kNParams 5
39 const kNSX127Rates 4
40 const kNSX128Rates 5
42 rem -- states
43 const kInitializing 0
44 const kReceivedParams 1
45 const kWaitForUpdate 2
47 rem -- max param name len is 11 = 10 + null
48 const kMaxParamNameLen 11
49 const kMinCommandInterval10ms 100
50 const PARAMETER_WRITE 0x2D
51 const kQuestionMark 63
53 if init = 0
54 init = 1
56 strtoarray(intToHex,"0123456789abcdef")
58 rem -- try to detect e.g. Taranis style Tx
59 isTaranisStyle = sysflags() & 0x20
61 eventLeft = EVT_LEFT_FIRST
62 eventRight = EVT_RIGHT_FIRST
63 eventUp = EVT_UP_FIRST
64 eventDown = EVT_DOWN_FIRST
66 eventLeftRept = EVT_LEFT_REPT
67 eventRightRept = EVT_RIGHT_REPT
68 eventUpRept = EVT_UP_REPT
69 eventDownRept = EVT_DOWN_REPT
71 activateBtn = EVT_MENU_BREAK
73 if isTaranisStyle # 0
74 eventLeft = EVT_UP_FIRST
75 eventRight = EVT_DOWN_FIRST
76 eventUp = EVT_MENU_BREAK
77 eventDown = EVT_LEFT_FIRST
79 eventLeftRept = EVT_UP_REPT
80 eventRightRept = EVT_DOWN_REPT
81 eventUpRept = eventUp
82 eventDownRept = eventDown
84 activateBtn = EVT_BTN_BREAK
85 end
87 pktsRead = 0
88 pktsSent = 0
89 pktsGood = 0
90 elrsPkts = 0
92 lastChangeTime = gettime()
94 rem -- Note: list indexes must match to param handling in tx_main!
95 rem -- list = {AirRate, TLMinterval, MaxPower, RFfreq, Bind, WebServer},
97 strtoarray(commitSha,"??????")
99 strtoarray(paramNames,"Pkt Rate:\0\0TLM Ratio:\0Power:\0\0\0\0\0RF freq:")
101 paramMins[kAirRateParamIdx] = 0
102 paramMaxs[kAirRateParamIdx] = 3
103 paramMins[kTLMintervalParamIdx] = 0
104 paramMaxs[kTLMintervalParamIdx] = 7
105 paramMins[kMaxPowerParamIdx] = 0
106 paramMaxs[kMaxPowerParamIdx] = 7
107 paramMins[kRFfreqParamIdx] = 0
108 paramMaxs[kRFfreqParamIdx] = 0
109 paramMins[kWifiUpdateParamIdx] = 0
110 paramMaxs[kWifiUpdateParamIdx] = 1
113 rem -- 433/868/915 (SX127x) '25 Hz', '50 Hz', '100 Hz', '200 Hz'
114 validAirRates127[0] = 6
115 validAirRates127[1] = 5
116 validAirRates127[2] = 4
117 validAirRates127[3] = 2
119 rem -- 2.4 GHz (SX128x) '50 Hz', '150 Hz', '250 Hz', '500 Hz'
120 validAirRates128[0] = 6
121 validAirRates128[1] = 5
122 validAirRates128[2] = 3
123 validAirRates128[3] = 1
124 validAirRates128[4] = 0
126 band24GHz = 0
127 nValidAirRates = kNSX127Rates
129 i = 0
130 while i < kNParams
131 params[i] = -1
132 i += 1
136 bindMode = 0
137 wifiupdatemode = 0
139 UartGoodPkts = 0
140 UartBadPkts = 0
142 StopUpdate = 0
144 kNTotalParams = 5
145 kNumEditParams = 3
147 selectedParam = 0
149 incDecParam = 0
151 state = kInitializing
153 rem -- purge the crossfire packet fifo
154 result = crossfirereceive(count, command, rxBuf)
155 while result = 1
156 result = crossfirereceive(count, command, rxBuf)
159 rem gosub requestDeviceData
161 goto main
163 sendPacket:
164 result = crossfiresend(sendCommand, sendLength, transmitBuffer)
165 if result = 1
166 pktsSent += 1
168 return
170 sendPingPacket:
171 rem -- crossfireTelemetryPush(0x2D, {0xEE, 0xEA, 0x00, 0x00}) ping until we get a resp
172 sendCommand = PARAMETER_WRITE
173 sendLength = 4
174 transmitBuffer[0]=0xEE
175 transmitBuffer[1]=0xEA
176 transmitBuffer[2]=0x00
177 transmitBuffer[3]=0x00
179 gosub sendPacket
180 return
182 sendWifiUpdatePacket:
183 sendCommand = PARAMETER_WRITE
184 sendLength = 4
185 transmitBuffer[0]=0xEE
186 transmitBuffer[1]=0xEA
187 transmitBuffer[2]=0xFE
188 transmitBuffer[3]=0x01
189 gosub sendPacket
190 return
193 sendParamIncDecPacket:
194 if state # kReceivedParams then return
195 rem -- if gettime() - lastChangeTime < kMinCommandInterval10ms then return
197 origValue = params[selectedParam]
198 if selectedParam = kAirRateParamIdx
199 rem -- find the index of the current value
200 AirRateIdx = params[kAirRateParamIdx]
201 i = 0
202 if band24GHz = 1
203 nValidAirRates = kNSX128Rates
204 while i < nValidAirRates
205 validAirRates[i] = validAirRates128[i]
206 i += 1
208 else
209 nValidAirRates = kNSX127Rates
210 while i < nValidAirRates
211 validAirRates[i] = validAirRates127[i]
212 i += 1
215 i = 0
216 while i < nValidAirRates
217 if validAirRates[i] = AirRateIdx then break
218 i += 1
220 i += incDecParam
221 if i >= nValidAirRates
222 i = nValidAirRates-1
223 elseif i < 0
224 i = 0
226 value = validAirRates[i]
227 else
228 value = params[selectedParam]
229 value += incDecParam
230 if value > paramMaxs[selectedParam]
231 value = paramMaxs[selectedParam]
232 elseif value < paramMins[selectedParam]
233 value = paramMins[selectedParam]
237 if value = origValue then return
239 sendCommand = PARAMETER_WRITE
240 sendLength = 4
241 transmitBuffer[0]=0xEE
242 transmitBuffer[1]=0xEA
243 transmitBuffer[2]=selectedParam+1
244 transmitBuffer[3]=value
246 state = kWaitForUpdate
247 lastChangeTime = gettime()
248 elrsPkts += 1
250 gosub sendPacket
251 return
253 checkForPackets:
254 command = 0
255 count = 0
257 rem -- N/B. rxBuf[0] maps to data[1] in ELRS.lua
258 result = crossfirereceive(count, command, rxBuf)
259 while result = 1
260 if (command = 0x2D) & (rxBuf[0] = 0xEA) & (rxBuf[1] = 0xEE)
261 pktsRead += 1
263 if ((rxBuf[2] = 0xFF) & (count = 12))
264 bindMode = (rxBuf[3] & 1)
265 wifiupdatemode = (rxBuf[3] & 2)
267 if StopUpdate = 0
268 params[kAirRateParamIdx] = rxBuf[4]
269 params[kTLMintervalParamIdx] = rxBuf[5]
270 params[kMaxPowerParamIdx] = rxBuf[6]
271 params[kRFfreqParamIdx] = rxBuf[7]-1
273 band24GHz = 0
274 if rxBuf[7] = 6
275 band24GHz = 1
278 state = kReceivedParams
279 rem -- elrsPkts += 1
282 UartBadPkts = rxBuf[8]
283 UartGoodPkts = rxBuf[9] * 256 + rxBuf[10]
285 elseif ((rxBuf[2] = 0xFE) & (count = 9))
286 rem -- First half of commit sha - 6 hexadecimal digits
287 i = 0
288 while i < 6
289 j = rxBuf[i+3]
290 if j >= 0 & j < 16
291 commitSha[i] = intToHex[j]
292 else
293 commitSha[i] = kQuestionMark
295 i += 1
300 result = crossfirereceive(count, command, rxBuf)
302 return
304 nextParameter:
305 selectedParam += 1
306 if selectedParam >= kNTotalParams then selectedParam = 0
307 if selectedParam = kRFfreqParamIdx then selectedParam += 1
308 return
310 previousParameter:
311 selectedParam -= 1
312 if selectedParam < 0 then selectedParam = kNTotalParams - 1
313 if selectedParam = kRFfreqParamIdx then selectedParam -= 1
314 return
316 decrementParameter:
317 incDecParam = -1;
318 gosub sendParamIncDecPacket
319 return
321 incrementParameter:
322 incDecParam = 1;
323 gosub sendParamIncDecPacket
324 return
326 main:
327 gosub checkForPackets
329 if state = kInitializing
330 rem -- crossfireTelemetryPush(0x2D, {0xEE, 0xEA, 0x00, 0x00}) -- ping until we get a resp
331 gosub sendPingPacket
334 if (Event = eventDown) | (Event = eventDownRept)
335 gosub nextParameter
336 elseif (Event = eventUp) | (Event = eventUpRept)
337 gosub previousParameter
338 elseif (Event = eventLeft) | (Event = eventLeftRept)
339 gosub decrementParameter
340 elseif (Event = eventRight) | (Event = eventRightRept)
341 gosub incrementParameter
342 elseif Event = activateBtn
343 if selectedParam = kWifiUpdateParamIdx then gosub sendWifiUpdatePacket
346 drawclear()
347 valueXPos = 60
348 ygap = 10
349 yPos = 0
350 drawtext(0, yPos, "ELRS", INVERS)
351 if wifiupdatemode = 2
352 drawtext(30, yPos, "http://10.0.0.1")
353 elseif bindMode = 1
354 drawtext(30, yPos, "Bind not needed")
355 else
356 rem UartBadPkts = 250
357 rem UartGoodPkts = 250
358 drawnumber(86,yPos, UartBadPkts)
359 drawtext(86, yPos, ":")
360 drawnumber(106,yPos, UartGoodPkts)
361 drawtext(28, yPos, commitSha)
363 rem if param = selectedParam then attr = INVERS
365 param = 0
366 idx = params[param]+1
367 yPos = (param+1)*ygap
368 drawtext(0, yPos, "Pkt Rate:")
369 rem -- drawtext(valueXPos,yPos,"------\0AUTO \0 500Hz\0 250Hz\0 200Hz\0 150Hz\0 100Hz\0 50Hz\0 25Hz\0 4Hz"[AirRateIdx*7],(param=selectedParam)*INVERS)
370 drawtext(valueXPos,yPos,"------\0 500Hz\0 250Hz\0 200Hz\0 150Hz\0 100Hz\0 50Hz\0 25Hz\0 4Hz"[idx*7],(param=selectedParam)*INVERS)
372 param += 1
373 idx = params[param]+1
374 yPos = (param+1)*ygap
375 drawtext(0, yPos, "TLM Ratio:")
376 drawtext(valueXPos,yPos,"------\0 Off\0 1:128\0 1:64\0 1:32\0 1:16\0 1:8\0 1:4\0 1:2"[idx*7],(param=selectedParam)*INVERS)
378 param += 1
379 idx = params[param]+1
380 yPos = (param+1)*ygap
381 drawtext(0, yPos, "Power:")
382 drawtext(valueXPos,yPos,"------ \0 10 mW\0 25 mW\0 50 mW\0 100 mW\0 250 mW\0 500 mW\0 1 W\0 2 W"[idx*8],(param=selectedParam)*INVERS)
384 param += 1
385 idx = params[param]+1
386 yPos = (param+1)*ygap
387 drawtext(0, yPos, "RF freq:")
388 drawtext(valueXPos,yPos,"------ \0 915 AU \0 915 FCC\0 868 EU \0 433 AU \0 433 EU \0 2G4 ISM"[idx*9],(param=selectedParam)*INVERS)
390 param += 1
391 idx = params[param]+1
392 yPos = (param+1)*ygap
393 drawtext(0, yPos, "Wifi:")
394 drawtext(valueXPos,yPos,"Update\0Update\0"[idx*7],(param=selectedParam)*INVERS)
396 rem param += 1
397 rem yPos = (param+1)*ygap
398 rem drawtext(0,yPos,"elrsPkts:")
399 rem drawnumber(valueXPos+15,yPos,elrsPkts)
401 stop
403 exit:
404 finish