forked from liuhm2019-crypto/RotorflightTelemeteringScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
927 lines (821 loc) · 36.3 KB
/
main.lua
File metadata and controls
927 lines (821 loc) · 36.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
------- 大贝壳 出品 V1.0
local NAME = "DBK_Dashboard"
local VERSION = "v1.0"
local TopValue = 10
local crsf_field = { "Vbat", "Curr", "Hspd", "Capa", "Bat%", "Tesc", "Tmcu", "1RSS", "2RSS", "RQly", "Thr", "Vbec", "ARM", "Gov", "Vcel" }
local TELE_ITEMS = #crsf_field
local LOG_INFO_LEN = 22
local LOG_DATA_LEN = 115
local value_min_max = {}
local field_id = {}
local bank_info = { current = 1, name = "Bank 1" }
local tg_pic_obj
local default_pic_obj
-- 缓存变量
local cached_model_name = ""
local cached_pic_path = ""
local last_audio_time = 0
local led_cache = { last_start_color = 0, last_end_color = 0 }
local log_write_timer = 0
local hold_locked = false
local button_pressed = false
local file_name = ""
local file_path = ""
local file_obj
local log_info = ""
local log_data = {}
local fly_number = 0
local sele_number = 0
local second = { 0, 0, 0 }
local total_second = 0
local hours = 0
local minutes = { 0, 0 }
local seconds = { 0, 0 }
local power_max = { 0, 0 }
local write_en_flag = false
local arm_flag = false
local second_save = 0
local display_log_flag = false
local display_mode = 0
local options = {
{ "SquareColor", COLOR, WHITE },
{ "BackgroundColor", COLOR, BLACK },
{ "ValueColor", COLOR, GREEN },
{ "DispLED", BOOL, 0 },
{ "BankSwitch", SOURCE, 0 },
{ "HoldSwitch", SWITCH, 0 },
{ "ArmSwitch", SWITCH, 0 }
}
local radioH = 0
local radio_name, version = getVersion()
if version and string.find(version, "tx15") then
radioH = 30
elseif version and string.find(version, "tx16s") then
radioH = 0
else
radioH = 0
end
-- Screen auto-scaling: base design is 480x272, scales up to any resolution
local BASE_W = 480
local BASE_H = 272
local scaleX = (LCD_W or BASE_W) / BASE_W
local scaleY = (LCD_H or BASE_H) / BASE_H
local function sx(v) return math.floor(v * scaleX + 0.5) end
local function sy(v) return math.floor(v * scaleY + 0.5) end
radioH = sy(radioH)
TopValue = sy(TopValue)
local function create(zone, options)
local widget = {
zone = zone,
options = options
}
for i = 1, TELE_ITEMS do
value_min_max[i] = { 0, 0, 0 }
field_id[i] = { 0, false }
end
sele_number = 1
display_log_flag = false
display_mode = 0
-- 初始化缓存的模型名称
cached_model_name = ""
for k, v in pairs(crsf_field) do
local field_info = getFieldInfo(v)
if field_info ~= nil then
field_id[k][1] = field_info.id
field_id[k][2] = true
else
field_id[k][1] = 0
field_id[k][2] = false
end
end
for i = 1, #second do
second[i] = 0
end
total_second = 0
write_en_flag = false
arm_flag = false
-- 先使用默认文件名,在refresh中动态更新
file_name = "[DBK_Dashboard]" ..
string.format("%d", getDateTime().year) ..
string.format("%02d", getDateTime().mon) ..
string.format("%02d", getDateTime().day) .. ".log"
file_path = "/WIDGETS/DBK_Dashboard/logs/" .. file_name
local file_info = fstat(file_path)
local read_count = 1
if file_info ~= nil then
if file_info.size > 0 then
file_obj = io.open(file_path, "r")
log_info = io.read(file_obj, LOG_INFO_LEN + 1)
while true do
log_data[read_count] = io.read(file_obj, LOG_DATA_LEN + 1)
if #log_data[read_count] == 0 then
break
else
read_count = read_count + 1
end
end
io.close(file_obj)
hours = string.sub(log_info, 12, 13)
minutes[2] = string.sub(log_info, 15, 16)
seconds[2] = string.sub(log_info, 18, 19)
total_second = tonumber(string.sub(log_info, 12, 13)) * 3600
total_second = total_second + tonumber(string.sub(log_info, 15, 16)) * 60
total_second = total_second + tonumber(string.sub(log_info, 18, 19))
end
else
-- 不创建默认文件,设置默认的log_info
log_info =
string.format("%d", getDateTime().year) .. '/' ..
string.format("%02d", getDateTime().mon) .. '/' ..
string.format("%02d", getDateTime().day) .. '|' ..
"00:00:00" .. '|' ..
"00\n"
end
local str_temp = string.sub(log_info, 21, 23)
if tonumber(str_temp) ~= nil then
fly_number = tonumber(str_temp)
end
if fly_number == 0 then
fly_number = 1
sele_number = 1
log_data[1] = "01|12:34:56|05:30|1850|025|2400|125.5|03500|25.2|22.8|+055|+025|+040|+020|-032|-072|-028|-065|100|095|080|12.6|11.8\n"
end
-- 只在create时加载默认图片
default_pic_obj = Bitmap.open("/WIDGETS/DBK_Dashboard/default.png")
return widget
end
local function update(widget, options)
widget.options = options
end
local function background(widget)
end
local function get_bank_info(widget)
if widget.options.BankSwitch ~= 0 then
local bank_value = getValue(widget.options.BankSwitch) or 0
local bank_num = 1
if bank_value < -300 then
bank_num = 1
elseif bank_value > 300 then
bank_num = 3
else
bank_num = 2
end
bank_info.current = bank_num
return
end
local fm_value = getValue("FM")
if fm_value ~= nil then
local bank_num = math.floor(fm_value) + 1
bank_info.current = math.max(1, math.min(6, bank_num))
return
end
bank_info.current = 1
end
local function draw_rounded_rectangle(xs, ys, w, h, r, color)
lcd.drawArc(xs + r, ys + r, r, 270, 360, color)
lcd.drawArc(xs + r, ys + h - r, r, 180, 270, color)
lcd.drawArc(xs + w - r, ys + r, r, 0, 90, color)
lcd.drawArc(xs + w - r, ys + h - r, r, 90, 180, color)
lcd.drawLine(xs + r, ys, xs + w - r, ys, SOLID, color)
lcd.drawLine(xs + r, ys + h, xs + w - r, ys + h, SOLID, color)
lcd.drawLine(xs, ys + r, xs, ys + h - r, SOLID, color)
lcd.drawLine(xs + w, ys + r, xs + w, ys + h - r, SOLID, color)
end
local function fuel_percentage(xs, ys, capa, number, text_color)
local color = lcd.RGB(255 - number * 2.55, number * 2.55, 0)
if number ~= 0 and number ~= 100 then
lcd.drawAnnulus(xs, ys, sx(45), sx(70), (100 - number) * 3.6, 360, color)
end
if number == 100 then
lcd.drawAnnulus(xs, ys, sx(45), sx(70), 1, 360, color)
lcd.drawAnnulus(xs, ys, sx(45), sx(70), -5, 5, color)
end
lcd.drawText(xs + sx(2), ys - sy(10), string.format("%d%%", number), CENTER + VCENTER + DBLSIZE + text_color)
lcd.drawText(xs, ys + sy(15), string.format("%dmAh", capa), CENTER + VCENTER + text_color)
end
local function throttle_percentage_bar(xs, ys, thr_value, border_color, text_color)
local bar_width = sx(150)
local bar_height = sy(20)
local fill_width = math.floor(bar_width * thr_value / 100)
thr_value = math.max(0, math.min(100, thr_value))
local color = lcd.RGB(thr_value * 2.55, 0, 255 - thr_value * 2.55)
draw_rounded_rectangle(xs, ys, bar_width, bar_height, sx(8), border_color)
if thr_value > 0 then
if fill_width > sx(6) then
lcd.drawFilledRectangle(xs + sx(2), ys + sy(2), fill_width - sx(4), bar_height - sy(4), color)
end
end
lcd.drawText(xs + bar_width / 2, ys + bar_height / 2, string.format("%d%%", thr_value), CENTER + VCENTER + SMLSIZE + text_color)
end
local function rqly_signal_bars(xs, ys, rqly_percent, default_color)
local block_size = sx(5)
local block_spacing = sx(7)
rqly_percent = math.max(0, math.min(100, rqly_percent))
local active_blocks = math.floor((rqly_percent + 19) / 20)
for i = 1, 5 do
local block_x = xs + (i - 1) * block_spacing
local block_y = ys
local block_color = default_color
if rqly_percent > 0 and i <= active_blocks then
if i == 1 then
block_color = RED
elseif i == 2 then
block_color = ORANGE
elseif i == 3 then
block_color = YELLOW
elseif i == 4 then
block_color = lcd.RGB(173, 255, 47)
else
block_color = GREEN
end
end
lcd.drawFilledRectangle(block_x, block_y, block_size, block_size, block_color)
end
end
local function draw_log_content(xs, ys, title, message, flags)
local extract = {}
local value
local index, length = 4, 8
extract[1] = string.sub(message, index, index + length - 1)
index = 13
length = 5
extract[2] = string.sub(message, index, index + length - 1)
for t = 1, 20 do
index = index + length + 1
if t == 2 or t == 16 or t == 17 or t == 18 then
length = 3
elseif t == 4 or t == 5 then
length = 5
else
length = 4
end
value = tonumber(string.sub(message, index, index + length - 1))
if t == 4 or t == 6 or t == 7 or t == 19 or t == 20 then
extract[t + 2] = string.format("%.1f", value)
else
extract[t + 2] = string.format("%d", value)
end
end
draw_rounded_rectangle(xs, ys, sx(400) - 1, sy(175) - 1, sx(2), flags)
lcd.drawLine(xs, ys + sy(28), xs + sx(400) - 2, ys + sy(28), SOLID, flags)
lcd.drawLine(xs + sx(200), ys + sy(28), xs + sx(200), ys + sy(175) - 2, SOLID, flags)
lcd.drawText(xs + sx(5), ys + sy(5), title, flags)
lcd.drawText(xs + sx(5), ys + sy(30),
"Time: " .. extract[2] .. '\n' ..
"Capa: " .. extract[3] .. "[mAh]\n" ..
"Fuel: " .. extract[4] .. "[%]\n" ..
"HSpd: " .. extract[5] .. "[rpm]\n" ..
"Throttle: " .. extract[20] .. "[%]\n" ..
"Current: " .. extract[6] .. "[A]\n" ..
"Power: " .. extract[7] .. "[W]"
, flags)
lcd.drawText(xs + sx(205), ys + sy(30),
"Battery: " .. extract[8] .. " -> " .. extract[9] .. "[V]\n" ..
"BEC: " .. extract[22] .. " -> " .. extract[21] .. "[V]\n" ..
"ESC: " .. extract[11] .. " -> " .. extract[10] .. "[°C]\n" ..
"MCU: " .. extract[13] .. " -> " .. extract[12] .. "[°C]\n" ..
crsf_field[8] .. ": " .. extract[14] .. " -> " .. extract[15] .. "[dB]\n" ..
crsf_field[9] .. ": " .. extract[16] .. " -> " .. extract[17] .. "[dB]\n" ..
crsf_field[10] .. ": " .. extract[18] .. " -> " .. extract[19] .. "[%]"
, flags)
end
-- 音频播放辅助函数
local function safe_play_tone(freq, length, pause, flags, freqIncr)
local current_time = getTime()
if current_time - last_audio_time > 10 then -- 防止音频重叠,至少间隔100ms
last_audio_time = current_time
playTone(freq, length, pause, flags or PLAY_NOW, freqIncr)
end
end
local function refresh(widget, event, touchState)
local screen_width = LCD_W or widget.zone.w
local screen_height = LCD_H or widget.zone.h
if event == nil then
elseif event ~= 0 then
if touchState then
if event == EVT_TOUCH_FIRST then
safe_play_tone(100, 50, 50)
elseif event == EVT_TOUCH_TAP then
safe_play_tone(200, 50, 50)
end
end
if event == EVT_VIRTUAL_NEXT_PAGE then
display_mode = (display_mode + 1) % 3
display_log_flag = (display_mode == 1)
safe_play_tone(200, 100, 100)
elseif event == EVT_VIRTUAL_PREV_PAGE then
if display_mode == 0 then
display_mode = 2
elseif display_mode == 1 then
display_mode = 0
elseif display_mode == 2 then
display_mode = 1
display_log_flag = false
end
safe_play_tone(200, 100, 100)
elseif event == EVT_VIRTUAL_NEXT then
if display_mode == 1 and not display_log_flag then
if fly_number > 0 then
if sele_number < fly_number then
sele_number = sele_number + 1
else
sele_number = 1
end
safe_play_tone(200, 50, 100)
end
end
elseif event == EVT_VIRTUAL_PREV then
if display_mode == 1 and not display_log_flag then
if fly_number > 0 then
if sele_number > 1 then
sele_number = sele_number - 1
else
sele_number = fly_number
end
safe_play_tone(200, 50, 100)
end
end
elseif event == EVT_VIRTUAL_ENTER then
if display_mode == 1 then
if fly_number > 0 and sele_number > 0 and sele_number <= fly_number and log_data[sele_number] then
display_log_flag = not display_log_flag
safe_play_tone(100, 200, 100, PLAY_NOW, 10)
else
safe_play_tone(3000, 100, 50)
end
else
safe_play_tone(600, 50, 50)
end
elseif event == EVT_VIRTUAL_EXIT then
display_mode = 0
display_log_flag = false
safe_play_tone(10000, 200, 100, PLAY_NOW, -60)
end
end
lcd.setColor(CUSTOM_COLOR, widget.options.BackgroundColor)
local bg_color = lcd.getColor(CUSTOM_COLOR)
lcd.setColor(CUSTOM_COLOR, widget.options.SquareColor)
local square_color = lcd.getColor(CUSTOM_COLOR)
lcd.setColor(CUSTOM_COLOR, widget.options.ValueColor)
local value_color = lcd.getColor(CUSTOM_COLOR)
-- LED控制逻辑:处理开启和关闭
if LED_STRIP_LENGTH and LED_STRIP_LENGTH > 0 then
if widget.options.DispLED == 1 then
-- LED开关开启
local start_color = widget.options.SquareColor
local end_color = widget.options.ValueColor
if led_cache.last_start_color ~= start_color or led_cache.last_end_color ~= end_color then
led_cache.last_start_color = start_color
led_cache.last_end_color = end_color
local start_rgb565 = math.floor(start_color / 65536)
local start_red = math.floor(start_rgb565 / 2048) * 8
local start_green = (math.floor(start_rgb565 / 32) % 64) * 4
local start_blue = (start_rgb565 % 32) * 8
local end_rgb565 = math.floor(end_color / 65536)
local end_red = math.floor(end_rgb565 / 2048) * 8
local end_green = (math.floor(end_rgb565 / 32) % 64) * 4
local end_blue = (end_rgb565 % 32) * 8
for i = 0, LED_STRIP_LENGTH - 1 do
local ratio = 0.5
local ratio = (i % 2) / 1
local red = start_red + (end_red - start_red) * ratio
local green = start_green + (end_green - start_green) * ratio
local blue = start_blue + (end_blue - start_blue) * ratio
setRGBLedColor(i, math.floor(red), math.floor(green), math.floor(blue))
end
applyRGBLedColors()
end
else
-- LED开关关闭,关闭所有LED
if led_cache.last_start_color ~= 0 or led_cache.last_end_color ~= 0 then
led_cache.last_start_color = 0
led_cache.last_end_color = 0
for i = 0, LED_STRIP_LENGTH - 1 do
setRGBLedColor(i, 0, 0, 0) -- 设置为黑色(关闭)
end
applyRGBLedColors()
end
end
end
lcd.drawFilledRectangle(0, 0, screen_width, screen_height, bg_color)
local title_height = sy(25)
local current_time = getDateTime()
local time_str = string.format("%02d:%02d:%02d", current_time.hour, current_time.min, current_time.sec)
lcd.drawText(sx(385), sy(250)+radioH, time_str, BOLD + square_color)
-- 检查模型名称是否改变,只在改变时重新加载图片和更新文件名
local current_model_name = model.getInfo().name
if cached_model_name ~= current_model_name then
cached_model_name = current_model_name
cached_pic_path = "/WIDGETS/DBK_Dashboard/"..string.sub(cached_model_name, 2)..".png"
-- 更新文件名为模型名称(只在模型名称变化时执行一次)
if current_model_name and current_model_name ~= "" then
-- 清理文件名中的无效字符
local safe_model_name = string.gsub(current_model_name, "[<>:\"/\\|?*]", "")
local new_file_name = "[".. safe_model_name .. "]" ..
string.format("%d", getDateTime().year) ..
string.format("%02d", getDateTime().mon) ..
string.format("%02d", getDateTime().day) .. ".log"
local new_file_path = "/WIDGETS/DBK_Dashboard/logs/" .. new_file_name
-- 只有文件名真的改变了才更新
if new_file_path ~= file_path then
file_name = new_file_name
file_path = new_file_path
end
end
if fstat(cached_pic_path) then
tg_pic_obj = Bitmap.open(cached_pic_path)
else
tg_pic_obj = nil
end
end
lcd.drawText(sx(255), sy(250)+radioH, cached_model_name, BOLD + value_color)
local tx_voltage = getValue("tx-voltage") or getValue("TxBt") or 0
local tx_battery_str = string.format("%.1fV", tx_voltage)
local tx_color = value_color
if tx_voltage < 6.5 then
tx_color = RED
elseif tx_voltage >= 6.5 and tx_voltage <= 7.0 then
tx_color = YELLOW
end
lcd.drawText(sx(400), sy(13), "Tx", BOLD + square_color)
lcd.drawText(sx(420), sy(13), tx_battery_str, BOLD + tx_color)
lcd.drawText(sx(30), title_height / 2 + TopValue, "Bank" , CENTER + VCENTER + square_color)
lcd.drawText(sx(100), title_height / 2 + TopValue, "HOLD" , CENTER + VCENTER + square_color)
lcd.drawText(sx(170), title_height / 2 + TopValue, "RSSI" , CENTER + VCENTER + square_color)
get_bank_info(widget)
local bank_color = square_color
if bank_info.current == 1 then
bank_color = lcd.RGB(0, 100, 255)
elseif bank_info.current == 2 then
bank_color = lcd.RGB(255, 165, 0)
elseif bank_info.current == 3 then
bank_color = lcd.RGB(255, 255, 0)
end
lcd.drawText(sx(20), sy(35)+TopValue, tostring(bank_info.current), CENTER + VCENTER + BOLD + MIDSIZE + bank_color)
local arm_status = (field_id[13][2] and value_min_max[13][1]) or 0
local gov_status = (field_id[14][2] and value_min_max[14][1]) or 0
local arm_switch_active = false
if widget.options.ArmSwitch ~= 0 then
local switch_value = getSwitchValue(widget.options.ArmSwitch)
if switch_value and switch_value ~= 0 and switch_value ~= false then
arm_switch_active = true
else
arm_switch_active = false
end
end
local is_armed = false
if field_id[13][2] then
is_armed = (arm_status == 1 or arm_status == 3)
else
is_armed = arm_switch_active
end
local gov_state_names = { "OFF", "IDLE", "SPOOLUP", "RECOVERY", "ACTIVE", "THR-OFF", "LOST-HS", "AUTOROT", "BAILOUT" }
if is_armed then
lcd.drawText(sx(280), sy(13)+TopValue, "GOV:", CENTER + VCENTER + square_color)
if field_id[14][2] then
local gov_name = gov_state_names[gov_status + 1] or "UNK"
if gov_name == "SPOOLUP" then
lcd.drawText(sx(340), sy(12)+TopValue, gov_name, CENTER + VCENTER + value_color)
else
lcd.drawText(sx(320), sy(12)+TopValue, gov_name, CENTER + VCENTER + value_color)
end
else
lcd.drawText(sx(340), sy(12)+TopValue, "ARMED", CENTER + VCENTER + GREEN)
end
else
lcd.drawText(sx(280), sy(13)+TopValue, "ARM:", CENTER + VCENTER + square_color)
if field_id[13][2] then
lcd.drawText(sx(345), sy(15)+TopValue, "DISARMED", CENTER + VCENTER + RED)
elseif widget.options.ArmSwitch ~= 0 then
lcd.drawText(sx(345), sy(15)+TopValue, "DISARMED", CENTER + VCENTER + RED)
else
lcd.drawText(sx(345), sy(15)+TopValue, "NO TELE", CENTER + VCENTER + BLINK + RED)
end
end
local hold_active = false
if widget.options.HoldSwitch ~= 0 then
local switch_value = getSwitchValue(widget.options.HoldSwitch)
if switch_value and switch_value ~= 0 and switch_value ~= false then
hold_active = true
else
hold_active = false
end
end
local hold_status = hold_active and "On" or "Off"
local hold_color = hold_active and GREEN or RED
lcd.drawText(sx(97), sy(35)+TopValue, hold_status, CENTER + VCENTER + MIDSIZE + hold_color)
for k = 1, TELE_ITEMS do
if field_id[k][2] then
local get_value = getValue(field_id[k][1])
value_min_max[k][1] = get_value
if not hold_active then
if get_value > value_min_max[k][2] then
value_min_max[k][2] = get_value
elseif get_value < value_min_max[k][3] then
value_min_max[k][3] = get_value
end
end
end
end
if is_armed then
if arm_flag == false then
arm_flag = true
for s = 1, TELE_ITEMS do
if field_id[s][2] then
value_min_max[s][2] = value_min_max[s][1]
value_min_max[s][3] = value_min_max[s][1]
end
end
second[1] = 0
power_max[1] = 0
power_max[2] = 0
write_en_flag = false
end
else
if arm_flag then
arm_flag = false
write_en_flag = true
end
end
power_max[2] = math.min(math.floor(value_min_max[1][1] * value_min_max[2][1]), 99999)
if power_max[1] < power_max[2] then
power_max[1] = power_max[2]
end
second[3] = getRtcTime()
if second[2] ~= second[3] then
second[2] = second[3]
if arm_flag then
second[1] = second[1] + 1
total_second = total_second + 1
end
end
minutes[1] = string.format("%02d", math.floor(second[1] % 3600 / 60))
seconds[1] = string.format("%02d", second[1] % 3600 % 60)
hours = string.format("%02d", math.floor(total_second / 3600))
minutes[2] = string.format("%02d", math.floor(total_second % 3600 / 60))
seconds[2] = string.format("%02d", total_second % 3600 % 60)
-- 优化日志写入:每5秒写入一次,避免频繁I/O
log_write_timer = log_write_timer + 1
if write_en_flag and fly_number < 57 and second[1] > 30 and log_write_timer >= 250 then -- 约5秒(假设50fps)
log_write_timer = 0
fly_number = fly_number + 1
-- 强制使用模型名称,如果获取不到就不写入
local current_model_name = model.getInfo().name
if not current_model_name or current_model_name == "" then
-- 如果没有模型名称,跳过写入
write_en_flag = false
return
end
-- 使用模型名称创建文件名
local safe_model_name = string.gsub(current_model_name, "[<>:\"/\\|?*]", "")
write_en_flag = true
-- 如果模型名称包含 BK_Dashboard,不写文件
if string.find(safe_model_name, "DBK_Dashboard") then
write_en_flag = false
end
if write_en_flag then
file_name = "[".. safe_model_name .."]" ..
string.format("%d", getDateTime().year) ..
string.format("%02d", getDateTime().mon) ..
string.format("%02d", getDateTime().day) .. ".log"
file_path = "/WIDGETS/DBK_Dashboard/logs/" .. file_name
-- 检查文件是否存在
local existing_file_info = fstat(file_path)
if existing_file_info and existing_file_info.size > 0 then
-- 文件存在,需要读取所有数据,更新记录数,然后重写整个文件
local temp_file_obj = io.open(file_path, "r")
local existing_log_data = {}
local existing_log_info = ""
local existing_fly_count = 0
if temp_file_obj then
existing_log_info = io.read(temp_file_obj, LOG_INFO_LEN + 1)
if existing_log_info and string.len(existing_log_info) >= 23 then
local str_temp = string.sub(existing_log_info, 21, 23)
if tonumber(str_temp) ~= nil then
existing_fly_count = tonumber(str_temp)
end
end
-- 读取所有现有记录
local read_count = 1
while true do
local data = io.read(temp_file_obj, LOG_DATA_LEN + 1)
if #data == 0 then
break
else
existing_log_data[read_count] = data
read_count = read_count + 1
end
end
io.close(temp_file_obj)
end
-- 更新飞行记录数
fly_number = existing_fly_count + 1
-- 重写整个文件
file_obj = io.open(file_path, "w")
if not file_obj then
write_en_flag = false
return
end
-- 写入更新后的头部信息
log_info =
string.format("%d", getDateTime().year) .. '/' ..
string.format("%02d", getDateTime().mon) .. '/' ..
string.format("%02d", getDateTime().day) .. '|' ..
hours .. ':' .. minutes[2] .. ':' .. seconds[2] .. '|' ..
string.format("%02d", fly_number) .. "\n"
io.write(file_obj, log_info)
-- 写入所有现有记录
for i = 1, existing_fly_count do
if existing_log_data[i] then
io.write(file_obj, existing_log_data[i])
end
end
-- 写入新记录
log_data[fly_number] =
string.format("%02d", fly_number) .. '|' ..
string.format("%02d", getDateTime().hour) .. ':' ..
string.format("%02d", getDateTime().min) .. ':' ..
string.format("%02d", getDateTime().sec) .. '|' ..
minutes[1] .. ':' .. seconds[1] .. '|' ..
string.format("%04d", math.max(0, value_min_max[4][1] - value_min_max[4][3])) .. '|' ..
string.format("%03d", math.max(0, value_min_max[5][2] - value_min_max[5][1])) .. '|' ..
string.format("%04d", value_min_max[3][2]) .. '|' ..
string.format("%05.1f", value_min_max[2][2]) .. '|' ..
string.format("%05d", power_max[1]) .. '|' ..
string.format("%04.1f", value_min_max[1][2]) .. '|' ..
string.format("%04.1f", value_min_max[1][3]) .. '|' ..
string.format("%+04d", value_min_max[6][2]) .. '|' ..
string.format("%+04d", value_min_max[6][3]) .. '|' ..
string.format("%+04d", value_min_max[7][2]) .. '|' ..
string.format("%+04d", value_min_max[7][3]) .. "|" ..
string.format("%+04d", value_min_max[8][2]) .. '|' ..
string.format("%+04d", value_min_max[8][3]) .. '|' ..
string.format("%+04d", value_min_max[9][2]) .. '|' ..
string.format("%+04d", value_min_max[9][3]) .. '|' ..
string.format("%03d", value_min_max[10][2]) .. '|' ..
string.format("%03d", value_min_max[10][3]) .. '|' ..
string.format("%03d", value_min_max[11][2]) .. '|' ..
string.format("%04.1f", value_min_max[12][2]) .. '|' ..
string.format("%04.1f", value_min_max[12][3]) .. "\n"
io.write(file_obj, log_data[fly_number])
else
-- 文件不存在,创建新文件
file_obj = io.open(file_path, "w")
if not file_obj then
write_en_flag = false
return
end
log_info =
string.format("%d", getDateTime().year) .. '/' ..
string.format("%02d", getDateTime().mon) .. '/' ..
string.format("%02d", getDateTime().day) .. '|' ..
hours .. ':' .. minutes[2] .. ':' .. seconds[2] .. '|' ..
string.format("%02d", fly_number) .. "\n"
io.write(file_obj, log_info)
for w = 1, fly_number - 1 do
io.write(file_obj, log_data[w])
end
log_data[fly_number] =
string.format("%02d", fly_number) .. '|' ..
string.format("%02d", getDateTime().hour) .. ':' ..
string.format("%02d", getDateTime().min) .. ':' ..
string.format("%02d", getDateTime().sec) .. '|' ..
minutes[1] .. ':' .. seconds[1] .. '|' ..
string.format("%04d", math.max(0, value_min_max[4][1] - value_min_max[4][3])) .. '|' ..
string.format("%03d", math.max(0, value_min_max[5][2] - value_min_max[5][1])) .. '|' ..
string.format("%04d", value_min_max[3][2]) .. '|' ..
string.format("%05.1f", value_min_max[2][2]) .. '|' ..
string.format("%05d", power_max[1]) .. '|' ..
string.format("%04.1f", value_min_max[1][2]) .. '|' ..
string.format("%04.1f", value_min_max[1][3]) .. '|' ..
string.format("%+04d", value_min_max[6][2]) .. '|' ..
string.format("%+04d", value_min_max[6][3]) .. '|' ..
string.format("%+04d", value_min_max[7][2]) .. '|' ..
string.format("%+04d", value_min_max[7][3]) .. "|" ..
string.format("%+04d", value_min_max[8][2]) .. '|' ..
string.format("%+04d", value_min_max[8][3]) .. '|' ..
string.format("%+04d", value_min_max[9][2]) .. '|' ..
string.format("%+04d", value_min_max[9][3]) .. '|' ..
string.format("%03d", value_min_max[10][2]) .. '|' ..
string.format("%03d", value_min_max[10][3]) .. '|' ..
string.format("%03d", value_min_max[11][2]) .. '|' ..
string.format("%04.1f", value_min_max[12][2]) .. '|' ..
string.format("%04.1f", value_min_max[12][3]) .. "\n"
io.write(file_obj, log_data[fly_number])
end
io.close(file_obj)
write_en_flag = false
end
end
local battery_voltage = (field_id[1][2] and value_min_max[1][1]) or 0
lcd.drawText(sx(179), sy(75)+TopValue, "Volt" , CENTER + VCENTER + square_color)
local battery_voltage_str = string.format("%.2fv", battery_voltage)
if battery_voltage == 0 then
lcd.drawText(sx(195), sy(95)+TopValue, battery_voltage_str, CENTER + VCENTER + MIDSIZE + value_color)
else
lcd.drawText(sx(200), sy(95)+TopValue, battery_voltage_str, CENTER + VCENTER + MIDSIZE + value_color)
end
lcd.drawText(sx(180), sy(125)+TopValue, "Vcel" , CENTER + VCENTER + square_color)
local vcel_voltage = (field_id[15][2] and value_min_max[15][1]) or 0
local vcel_voltage_str = "0.00v"
if vcel_voltage > 0 then
vcel_voltage_str = string.format("%.2fv", vcel_voltage)
end
lcd.drawText(sx(195), sy(145)+TopValue, vcel_voltage_str, CENTER + VCENTER + MIDSIZE + value_color)
lcd.drawText(sx(179), sy(175)+TopValue, "Bec" , CENTER + VCENTER + square_color)
local bec_voltage = (field_id[12][2] and value_min_max[12][1]) or 0
local bec_voltage_str = "0.00v"
if bec_voltage > 0 then
bec_voltage_str = string.format(bec_voltage < 10 and "%.2fv" or "%.1fv", bec_voltage)
end
lcd.drawText(sx(195), sy(198)+TopValue, bec_voltage_str, CENTER + VCENTER + MIDSIZE + value_color)
local battery_percent = (field_id[5][2] and value_min_max[5][1]) or 0
local battery_capacity = (field_id[4][2] and value_min_max[4][1]) or 0
fuel_percentage(sx(80), sy(150), battery_capacity, battery_percent, value_color)
lcd.drawText(sx(280), sy(128), "Time" , CENTER + VCENTER + square_color)
local flight_time_str = string.format("%s:%s", minutes[1], seconds[1])
lcd.drawText(sx(340), sy(128), flight_time_str, CENTER + VCENTER + MIDSIZE + value_color)
-- 统计当天这个模型的飞行记录数量
local flight_count = 0
local current_model_name = model.getInfo().name
if current_model_name and current_model_name ~= "" then
local safe_model_name = string.gsub(current_model_name, "[<>:\"/\\|?*]", "")
local target_file_name = "[".. safe_model_name .."]" ..
string.format("%d", getDateTime().year) ..
string.format("%02d", getDateTime().mon) ..
string.format("%02d", getDateTime().day) .. ".log"
local target_file_path = "/WIDGETS/DBK_Dashboard/logs/" .. target_file_name
local file_info = fstat(target_file_path)
if file_info ~= nil and file_info.size > 0 then
local temp_file_obj = io.open(target_file_path, "r")
if temp_file_obj then
local temp_log_info = io.read(temp_file_obj, LOG_INFO_LEN + 1)
if temp_log_info and string.len(temp_log_info) >= 23 then
local str_temp = string.sub(temp_log_info, 21, 23)
if tonumber(str_temp) ~= nil then
flight_count = tonumber(str_temp)
end
end
io.close(temp_file_obj)
end
end
end
lcd.drawText(sx(400), sy(128), flight_count, CENTER + VCENTER + MIDSIZE + value_color)
local throttle_percent = (field_id[11][2] and value_min_max[11][1]) or 0
throttle_percentage_bar(sx(80), sy(250)+radioH, throttle_percent, value_color, value_color)
lcd.drawText(sx(40), sy(260)+radioH, "Throttle" , CENTER + VCENTER + square_color)
local rpm_value = (field_id[3][2] and value_min_max[3][1]) or 0
local rpm_str = "0"
if rpm_value > 0 then
rpm_str = string.format("%d", rpm_value)
end
lcd.drawText(sx(335), sy(65)+TopValue, rpm_str, CENTER + VCENTER + DBLSIZE + BOLD + value_color)
lcd.drawText(sx(430), sy(78)+TopValue, "Rpm" , CENTER + VCENTER + square_color)
lcd.drawFilledRectangle(sx(260), sy(105), sx(190), 1, square_color)
local rqly_percent = (field_id[10][2] and value_min_max[10][1]) or 0
rqly_signal_bars(sx(200), sy(10)+TopValue, rqly_percent, WHITE)
if rqly_percent > 0 then
lcd.drawText(sx(175), sy(33)+TopValue, string.format("%ddB", rqly_percent), CENTER + VCENTER + value_color)
else
lcd.drawText(sx(165), sy(33)+TopValue, "---", CENTER + VCENTER + MIDSIZE + RED)
end
if tg_pic_obj then
if version and string.find(version, "tx15") then
lcd.drawBitmap(tg_pic_obj, sx(250), sy(135)+radioH)
else
lcd.drawBitmap(tg_pic_obj, sx(250), sy(150)+radioH)
end
else
if default_pic_obj then
lcd.drawBitmap(default_pic_obj, sx(250), sy(135)+radioH)
end
end
if version and string.find(version, "tx15") then
local current_value = (field_id[2][2] and value_min_max[2][1]) or 0
local current_str = "0.0A"
if current_value > 0 then
current_str = string.format("%.1fA", current_value)
end
lcd.drawText(sx(12), sy(240)+TopValue, "Current", BOLD + square_color)
lcd.drawText(sx(75), sy(240)+TopValue, current_str, BOLD + value_color)
local power_value = battery_voltage * current_value
local power_str = "0.0W"
if power_value > 0 then
if power_value >= 1000 then
power_str = string.format("%.1fkW", power_value / 1000)
else
power_str = string.format("%.0fW", power_value)
end
end
lcd.drawText(sx(125), sy(240)+TopValue, "Power", BOLD + square_color)
lcd.drawText(sx(180), sy(240)+TopValue, power_str, BOLD + value_color)
end
end
return {
name = NAME,
options = options,
create = create,
update = update,
refresh = refresh,
background = background
}