-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
osd.c
2242 lines (1806 loc) · 86.3 KB
/
osd.c
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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdbool.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <libgen.h> // For dirname()
#include "osd/msp/msp.h"
#include "osd/msp/msp_displayport.h"
#ifdef _x86
// #include <cairo/cairo.h>
// #include <cairo/cairo-xlib.h>
// #include <X11/Xlib.h>
// #include <X11/Xutil.h>
// #include <X11/Xatom.h>
// #include <stdio.h>
// #include <stdbool.h>
#include "osd/util/Render_x86.c"
#else
#include "bmp/region.h"
#include "bmp/common.h"
#endif
#include "bmp/region.h"
#include "bmp/bitmap.h"
#include "bmp/text.h"
#include "libpng/lodepng.h"
#define X_OFFSET 0
#define CLOCK_MONOTONIC 1
/*------------------------------------------------------------------------------------------------------*/
/*------------MSP PROTOCOL MSG PROCESSING ----------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
//#include "osd/msp/msp.h"
//#include "osd/msp/msp_displayport.h"
#include "osd/util/debug.h"
#include "osd/util/time_util.h"
#include "osd/util/fs_util.h"
#include "osd/util/settings.h"
#include "osd/util/interface.h"
#define CPU_TEMP_PATH "/sys/devices/platform/soc/f0a00000.apb/f0a71000.omc/temp1"
#define AU_VOLTAGE_PATH "/sys/devices/platform/soc/f0a00000.apb/f0a71000.omc/voltage4"
#define FAST_SERIAL_KEY "fast_serial"
#define CACHE_SERIAL_KEY "cache_serial"
#define COMPRESS_KEY "compress_osd"
#define UPDATE_RATE_KEY "osd_update_rate_hz"
#define NO_BTFL_HD_KEY "disable_betaflight_hd"
// The MSP_PORT is used to send MSP passthrough messages.
// The DATA_PORT is used to send arbitrary data - for example, bitrate and temperature data.
#define MSP_PORT 7654
#define DATA_PORT 7655
#define COMPRESSED_DATA_PORT 7656
#define COMPRESSED_DATA_VERSION 1
enum {
MAX_DISPLAY_X = 60,
MAX_DISPLAY_Y = 22
};
// The Betaflight MSP minor version in which MSP DisplayPort sizing is supported.
#define MSP_DISPLAY_SIZE_VERSION 45
extern FrequencyChannel fc_list[MAX_ENTRIES]; // Array to store frequency-channel pairs
typedef struct msp_cache_entry_s {
struct timespec time;
msp_msg_t message;
} msp_cache_entry_t;
static msp_cache_entry_t *msp_message_cache[256]; // make a slot for all possible messages
static uint8_t frame_buffer[8192]; // buffer a whole frame of MSP commands until we get a draw command
static uint32_t fb_cursor = 0;
static uint8_t message_buffer[256]; // only needs to be the maximum size of an MSP packet, we only care to fwd MSP
static char current_fc_identifier[4];
static char current_fc_identifier_end_of_string=0x00;
/* For compressed full-frame transmission */
static uint16_t msp_character_map_buffer[MAX_DISPLAY_X][MAX_DISPLAY_Y];
static uint16_t msp_character_map_draw[MAX_DISPLAY_X][MAX_DISPLAY_Y];
static msp_hd_options_e msp_hd_option = 0;
static displayport_vtable_t *display_driver = NULL;
uint8_t update_rate_hz = 2;
static unsigned long long LastDrawn=0;
static unsigned long long LastPcktSent=0;
static int MinTimeBetweenScreenRefresh;
int pty_fd;
int serial_fd;
int socket_fd;
int compressed_fd;
/// @brief Will skip char on the left to allow for smaller matrix and overlay area
int SkipXChar=0;
int font_pages=2;
static uint8_t serial_passthrough = 1;
static uint8_t compress = 0;
static uint8_t no_btfl_hd = 0;
static int16_t last_pitch = 0;
static int16_t last_roll = 0;
static int16_t last_heading=0;
static int16_t last_directionToHome=0;
static int16_t last_distanceToHome=0;
int AHI_TiltY = 50;
// https://github.com/betaflight/betaflight/blob/master/src/main/msp/msp.c#L1949
typedef struct
{
uint8_t vtxType;
uint8_t band;
uint8_t channel;
uint8_t power;
uint8_t pitmode;
// uint16_t freq; // This doesnt work and bytes are missing after memcpy.
uint8_t freqLSB;
uint8_t freqMSB;
uint8_t deviceIsReady;
uint8_t lowPowerDisarm;
// uint16_t pitModeFreq; // This doesnt work and bytes are missing after memcpy.
uint8_t pitModeFreqLSB;
uint8_t pitModeFreqMSB;
uint8_t vtxTableAvailable;
uint8_t bands;
uint8_t channels;
uint8_t powerLevels;
} mspVtxConfigStruct;
extern bool vtxMenuActive;
extern bool vtxMenuEnabled;
extern bool armed;
extern bool vtxInitDone;
extern bool DrawOSD;
static void send_display_size(int serial_fd) {
uint8_t buffer[8];
uint8_t payload[2] = {MAX_DISPLAY_X, MAX_DISPLAY_Y};
construct_msp_command(buffer, MSP_CMD_SET_OSD_CANVAS, payload, 2, MSP_OUTBOUND);
write(serial_fd, &buffer, sizeof(buffer));
}
static void send_variant_request(int serial_fd) {
uint8_t buffer[6];
construct_msp_command(buffer, MSP_CMD_FC_VARIANT, NULL, 0, MSP_OUTBOUND);
write(serial_fd, &buffer, sizeof(buffer));
}
static void send_version_request(int serial_fd) {
uint8_t buffer[6];
construct_msp_command(buffer, MSP_CMD_API_VERSION, NULL, 0, MSP_OUTBOUND);
write(serial_fd, &buffer, sizeof(buffer));
}
static void copy_to_msp_frame_buffer(void *buffer, uint16_t size) {
memcpy(&frame_buffer[fb_cursor], buffer, size);
fb_cursor += size;
}
//int displayport_process_message(displayport_vtable_t *display_driver, msp_msg_t *msg) {
//}
static int stat_msp_msgs=0;
static int stat_msp_msg_attitude=0;
static int stat_screen_refresh_count=0;
static int stat_skipped_frames=0;
static int stat_draw_overlay_1=0, stat_draw_overlay_2=0, stat_draw_overlay_3=0;
static int stat_MSPBytesSent=0;
static int stat_MSP_draw_complete_count=0;
static int stat_UDP_MSPframes=0;
static uint64_t last_MSP_ATTITUDE=0;
static int stat_attitudeDelay=0;
int RCWidgetX=1620;
int RCWidgetY=820;
char air_unit_info_msg[255];
extern bool AbortNow;
extern bool verbose;
extern struct sockaddr_in sin_out;//= {.sin_family = AF_INET,};
extern int out_sock;
extern int AHI_Enabled;
extern void showchannels(int count);
extern void ProcessChannels();
extern uint16_t channels[18];
extern int matrix_size;
extern int GetTempSigmaStar();
extern int Get8812EU2Temp();
extern int SendWfbLogToGround();
extern bool monitor_wfb;
extern int last_board_temp;
uint64_t get_time_ms() // in milliseconds
{
struct timespec ts;
int rc = clock_gettime(1 /*CLOCK_MONOTONIC*/, &ts);
//if (rc < 0)
// return get_current_time_ms_Old();
return ts.tv_sec * 1000LL + ts.tv_nsec / 1000000;
}
/* MSP DisplayPort handlers for compressed mode */
static void msp_draw_character(uint32_t x, uint32_t y, uint16_t c) {
DEBUG_PRINT("drawing char %d at x %d y %d\n", c, x, y);
msp_character_map_buffer[x][y] = c;
}
static void msp_clear_screen() {
memset(msp_character_map_buffer, 0, sizeof(msp_character_map_buffer));
}
static void msp_draw_complete() {
memcpy(msp_character_map_draw, msp_character_map_buffer, sizeof(msp_character_map_buffer));
}
static void msp_set_options(uint8_t font_num, msp_hd_options_e is_hd) {
DEBUG_PRINT("Got options!\n");
msp_clear_screen();
msp_hd_option = is_hd;
}
/*----------------------------------------------------------------------------------------------------*/
/*------------CONFIGURE SWITCHES ----------------------------------------------------------------*/
static int enable_fast_layout = 0;
void *io_map;
struct osd *osds;
char timefmt[32] = DEF_TIMEFMT;
int PIXEL_FORMAT_BitsPerPixel = 8;
typedef struct display_info_s {
uint8_t char_width;
uint8_t char_height;
uint8_t font_width;
uint8_t font_height;
uint16_t num_chars;
} display_info_t;
#define SD_DISPLAY_INFO {.char_width = 31, .char_height = 15, .font_width = 36, .font_height = 54, .num_chars = 256}
static const display_info_t sd_display_info = SD_DISPLAY_INFO;
static const display_info_t hd_display_info = {
.char_width = 50,
.char_height = 18,
.font_width = 24,
.font_height = 36,
.num_chars = 512,
};
static const display_info_t fhd_display_info = {
.char_width = 50,
.char_height = 18,
.font_width = 36,
.font_height = 54,
.num_chars = 512,
};
/*
36*50=1800
54*18=972
*/
#define MAX_OSD_WIDTH 54
#define MAX_OSD_HEIGHT 20
// for x86 preview only
#define TRANSPARENT_COLOR 0xFBDE
//Not implemented, draw the center part of the screen with much faster rate to keep CPU load low
//overlays 1 to 8 are taken by OSD tool, but they are limited to 8 in some systems like Goke
#ifdef __SIGMASTAR__
#define FULL_OVERLAY_ID 9
#define FAST_OVERLAY_ID 8
#else
#define FULL_OVERLAY_ID 6
#define FAST_OVERLAY_ID 7
#endif
char font_2_name[256];
uint16_t OVERLAY_WIDTH =1800;
uint16_t OVERLAY_HEIGHT =1000;
static displayport_vtable_t *display_driver;
static display_info_t current_display_info = SD_DISPLAY_INFO;
//bounderies as characters of fast area. Fast Character
int fcX= 12; int fcW=10; int fcY= 5 ; int fcH=8;
static bool InjectChars(char* payload){
char* str= payload + 4;
//string starts at 4 payload[0]==MSP_subtype
//set Position to Widget that draws Stick Positions - only on ground side, so do not clear it on camera side
if (DrawOSD && str[0]=='!' && str[1]=='R' && str[2]=='C' && str[3]=='!'){
// uint8_t row = payload[0]; //uint8_t col = payload[1];
RCWidgetX = payload[2] *current_display_info.font_width;
RCWidgetY = payload[1] * current_display_info.font_height;
memset (&payload[4],0,4);
return true;
}
int cnt=0;
//may have several in one text message
while (str[0]!=0 && cnt<20){
//set extra temp on screen
if ( str[0]=='!' && str[1]=='T' && str[2]=='M' && str[3]=='P'&& str[4]=='!'){
int temp = 103;
#if __SIGMASTAR__
temp = 102;
temp = GetTempSigmaStar();
#else
temp = last_board_temp ;
if (temp==-100)
temp=101;
#endif
if (temp<0)
temp=104;
if (font_pages==2){//inav symbols
str[0] = 199;//10748/54=199
str[3] = 11;//degree
}else{//betaflight symbols
str[0] = 122;//10748/54=199
str[3] = 14;//degree
}
if (temp>100)
str[1]=48 + 'A';
else
str[1]=48 + temp/10;
str[2]=48 + temp%10;
str[4] = 32;//end of string, no need, we are only replacing! place space
str = str + 4;
//return true;
}
//set extra temp on screen
if ( str[0]=='!' && str[1]=='T' && str[2]=='M' && str[3]=='W'&& str[4]=='!'){
int temp = 99;
temp = Get8812EU2Temp();
if (temp<0)
last_board_temp=99;
if (last_board_temp==-100)
last_board_temp=99;
if (last_board_temp<0)
last_board_temp=99;
if (font_pages==2){//inav symbols
str[0] = 196;//10748/54=199
str[3] = 11;//degree
}else{//betaflight symbols
str[0] = 122;//10748/54=199
str[3] = 14;//degree
}
str[1]=48 + temp/10;
str[2]=48 + temp%10;
str[4] = 32;//end of string, no need, we are only replacing! place space
str = str + 4;//move the pointer
//return true;
}
str=str + 1;
cnt++;
}//while
return false;
}
static void rx_msp_callback(msp_msg_t *msp_message)
{
// Process a received MSP message from FC and decide whether to send it to the PTY (DJI) or UDP port (MSP-OSD on Goggles)
DEBUG_PRINT("FC->AU MSP msg %d with data len %d \n", msp_message->cmd, msp_message->size);
stat_msp_msgs++;
//We will forward ALL MSP traffic, not only DisplayPort
if(fb_cursor > sizeof(frame_buffer)) {
if (out_sock>0){
printf("Exhausted frame buffer! Flushing...\n");
sendto(out_sock, frame_buffer, fb_cursor, 0, (struct sockaddr *)&sin_out, sizeof(sin_out));
}
fb_cursor = 0;
//return;
}
//Here it will replace custom text messages for configurator screen
if (msp_message->cmd==MSP_CMD_DISPLAYPORT && msp_message->direction == MSP_INBOUND && msp_message->cmd == MSP_CMD_DISPLAYPORT && msp_message->payload[0] == MSP_DISPLAYPORT_DRAW_STRING)
InjectChars(&msp_message->payload[0]);
//if (out_sock>0){//No need to cache MSP if we won't send it later
uint16_t size = msp_data_from_msg(message_buffer, msp_message);
copy_to_msp_frame_buffer(message_buffer, size);
//}
switch(msp_message->cmd) {
case MSP_CMD_STATUS: {
// we need the armed state
armed = (msp_message->payload[6] & 0x01);
if (armed) vtxMenuActive = false;
break;
}
case MSP_ATTITUDE: {
last_pitch = *(int16_t*)&msp_message->payload[2];
last_roll = *(int16_t*)&msp_message->payload[0];
last_heading = *(int16_t*)&msp_message->payload[4];
stat_msp_msg_attitude++;
stat_attitudeDelay=get_time_ms() - last_MSP_ATTITUDE;
//printf("\n Got MSG_ATTITUDE pitch:%d roll:%d\n", pitch, roll);
break;
}
case MSP_COMP_GPS: {
/*
GPS_distanceToHome UINT 16 unit: meter
GPS_directionToHome UINT 16 unit: degree (range [-180;+180])
GPS_update UINT 8 a flag to indicate when a new GPS frame is received (the GPS fix is not dependent of this)
*/
last_distanceToHome = *(int16_t*)&msp_message->payload[0];
last_directionToHome = *(int16_t*)&msp_message->payload[2];
//stat_msp_msg_attitude++;
//printf("\n Got MSG_ATTITUDE pitch:%d roll:%d\n", pitch, roll);
break;
}
case MSP_RC: {
//printf("Got MSP_RC \n");
//memcpy(&channels[0], &msp_message->payload[0],32);
memcpy(&channels[0], &msp_message->payload[0], 16 * sizeof(uint16_t));
//showchannels(18);
ProcessChannels();
if (vtxMenuEnabled && vtxMenuActive) {
print_current_state(display_driver);
}
break;
}
case MSP_CMD_DISPLAYPORT: {
if(msp_message->payload[0] == MSP_DISPLAYPORT_INFO_MSG) {
msp_message->payload[80]=0;//just in case
strcpy(air_unit_info_msg,&msp_message->payload[1]);
fill(air_unit_info_msg);
}
if ( ! vtxMenuActive ) {
displayport_process_message(display_driver, msp_message);
} else
if (!DrawOSD){//if we only resend to ground station and menu is active, stop regular OSD
LastPcktSent= get_time_ms();//clear buffer and prevent from sending MSP to ground
fb_cursor = 0;
printf("D");
}
if (out_sock>0 && fb_cursor>0){//if there is data to send
if(msp_message->payload[0] == MSP_DISPLAYPORT_DRAW_SCREEN) {
//Try to aggregate several MSP packets into one UDP packets
if ( (!DrawOSD) && MinTimeBetweenScreenRefresh>20 && (get_time_ms() - LastPcktSent ) < MinTimeBetweenScreenRefresh)
{}//Do not send the frame but keep it in the buffer
else{
stat_UDP_MSPframes++;
sendto(out_sock, frame_buffer, fb_cursor, 0, (struct sockaddr *)&sin_out, sizeof(sin_out));
LastPcktSent= get_time_ms();
stat_MSPBytesSent+=fb_cursor;
fb_cursor = 0;
}
}
}
break;
}
case MSP_CMD_FC_VARIANT: {
// This is an FC Variant response, so we want to use it to set our FC variant.
DEBUG_PRINT("Got FC Variant response!\n");
if(strncmp(current_fc_identifier, msp_message->payload, 4) != 0) {
// FC variant changed or was updated. Update the current FC identifier and send an MSP version request.
memcpy(current_fc_identifier, msp_message->payload, 4);
//Seems only BetaFlight needs this
send_version_request(serial_fd);
printf("Flight Controller detected: %s\r\n",current_fc_identifier);
}
break;
}
case MSP_CMD_API_VERSION: {
// Got an MSP API version response. Compare the version if we have Betaflight in order to see if we should send the new display size message.
if(strncmp(current_fc_identifier, "BTFL", 4) == 0) {
uint8_t msp_minor_version = msp_message->payload[2];
DEBUG_PRINT("Got Betaflight minor MSP version %d\n", msp_minor_version);
if(msp_minor_version >= MSP_DISPLAY_SIZE_VERSION) {
if(!no_btfl_hd) {
if(!compress) {
// If compression is disabled, we need to manually inject a canvas-change command into the command stream.
uint8_t displayport_set_size[3] = {MSP_DISPLAYPORT_SET_OPTIONS, 0, MSP_HD_OPTION_60_22};
construct_msp_command(message_buffer, MSP_CMD_DISPLAYPORT, displayport_set_size, 3, MSP_INBOUND);
copy_to_msp_frame_buffer(message_buffer, 9);
DEBUG_PRINT("Sent display size to goggles\n");
}
// Betaflight with HD support. Send our display size and set 60x22.
send_display_size(serial_fd);
msp_hd_option = MSP_HD_OPTION_60_22;
DEBUG_PRINT("Sent display size to FC\n");
}
}
}
break;
}
case MSP_GET_VTX_CONFIG: {
if (vtxInitDone) {
mspVtxConfigStruct *in_mspVtxConfigStruct = (mspVtxConfigStruct *) msp_message->payload;
uint16_t frequency = (in_mspVtxConfigStruct->freqMSB << 8) | in_mspVtxConfigStruct->freqLSB;
double current_frequency = read_current_freq_from_interface(read_setting("/etc/wfb.conf","wlan"));
if (verbose) printf("mspVTX Band: %i, Channel: %i, wanted Frequency: %u, set Frequency: %.0f\n",in_mspVtxConfigStruct->band, in_mspVtxConfigStruct->channel, frequency, current_frequency);
if (frequency != (uint16_t)current_frequency) {
int channel = 0;
for (int i =0 ; i < MAX_ENTRIES; i++) {
if (fc_list[i].frequency == frequency)
channel = fc_list[i].channel;
}
if (channel > 0) {
if (verbose) printf("mspVTX executing channel change to channel %d\n",channel);
set_frequency(read_setting("/etc/wfb.conf","wlan"), channel);
//store new channel to wfb.conf
char cha_str[32];
sprintf(cha_str, "%i", channel);
write_setting("/etc/wfb.conf", "channel", cha_str);
} else {
printf("Never change to channel 0, we should not reach here, check vtx table in your fc\n");
}
}
} else {
if (verbose)
printf("vtxInitDone not finished, should never happen\n");
}
break;
}
default: {
if (verbose) printf("Received a uncatched MSP_COMMAND: %i\n", msp_message->cmd);
uint16_t size = msp_data_from_msg(message_buffer, msp_message);
if(serial_passthrough /*|| cache_msp_message(msp_message)*/) {
// Either serial passthrough was on, or the cache was enabled but missed (a response was not available).
// Either way, this means we need to send the message through to DJI.
write(pty_fd, message_buffer, size);
}
break;
}
}
}
static int load_fontFromBMP(const char *filename, BITMAP *bitmap){
if (access(filename, F_OK))
return -1;
//White will be the transparant color
return prepare_bitmap(filename, bitmap, 2, TRANSPARENT_COLOR, PIXEL_FORMAT_1555);
}
// Function to move the cursor to a specific position
void move_cursor(int row, int col) {
printf("\033[%d;%dH", row, col);
}
// Function to draw a character at a specific position
void draw_character_on_console(int row, int col, char ch) {
move_cursor(row, col);
printf("%c", ch);
}
/// @brief We keep the main font glyphs here
BITMAP bitmapFnt;
/// @brief Extra font glyphs with smaller size
BITMAP bmpFntSmall;
uint16_t character_map[MAX_OSD_WIDTH][MAX_OSD_HEIGHT];
struct osd *osds;//regions over the overlay
static long cntr=-10;
int center_refresh=0;
/// @brief Where to place the message, 0 upper left, 1 -upper middle , 2 - upper right, 3 - upper row moving
int msg_layout=0;
/// @brief Color type to use to render the font. 0 - White, 1 - Black edges on white font
int msg_colour=0;
static unsigned long long LastCleared=0;
static bool osd_msg_enabled = false;
//This will be where we will copy font icons and then pass to Display API to render over video.
BITMAP bmpBuff;
//Buffer to hold converted RGBA data to work with Cairo
unsigned char* bmp_x86=NULL;
/// @brief Pointer to the canvas memory with bmp data
void* directBmp;
bool useDirectBMPBuffer=false;
int x_start = 600;
int y_start = 500;
int x_end = 1300;
int y_end = 500;
uint32_t getcolor(uint8_t index){
MI_RGN_PaletteElement_t element = g_stPaletteTable.astElement[index];
uint32_t rgba = (element.u8Red << 24) | (element.u8Green << 16) | (element.u8Blue<< 8) | element.u8Alpha;
}
void LineDirect(uint8_t* bmpData, uint32_t width, uint32_t height, int x0, int y0, int x1, int y1, uint8_t color, int thickness) {
#ifdef _x86
drawLine_x86(x0, y0, x1, y1, getcolor(color), thickness, false);
#else
drawLineI4( bmpData, width, height, x0, y0, x1, y1, color, thickness);
#endif
}
void LineTranspose(uint8_t* bmpData, int posX0, int posY0, int posX1, int posY1, uint8_t color, int thickness) {
#ifdef _x86
drawLine_x86(posX0, posY0, posX1, posY1, getcolor(color), thickness, true);
#else
drawLine( bmpData, posX0, posY0, posX1, posY1, color, thickness);
#endif
}
static void draw_AHI(){
int OffsY= sin((last_pitch/10) * (M_PI / 180.0))*400;
int img_width = x_end-x_start;
int img_height = y_end-y_start;
double angle_degrees = -last_roll/10; // Rotate by 45 degrees
Transform_OVERLAY_WIDTH=OVERLAY_WIDTH;
Transform_OVERLAY_HEIGHT=OVERLAY_HEIGHT;
Transform_Pitch=last_pitch/10;
Transform_Roll=-last_roll/10;
Point img_center = {OVERLAY_WIDTH/2, OVERLAY_HEIGHT/2}; // Center of the image (example)
Point original_point = {600, 500}; // Example point
Point original_point2 = {1300, 500}; // Example point
// drawLineI4Ex(bmpBuff.pData, bmpBuff.u32Width, bmpBuff.u32Height, original_point,original_point2, 7);
int linewidth=400;
int linethickness=2;
if (OVERLAY_WIDTH>1500){
linewidth=500;
linethickness=3;
}
LineTranspose(bmpBuff.pData, img_center.x-linewidth/2 , img_center.y, img_center.x+linewidth/2, img_center.y, COLOR_WHITE, linethickness);
//drawRectangleI4(bmpBuff.pData, 600 , 400 , 700 , 6, COLOR_GREEN, 1);
}
/// @brief Ugly implementation. To do : clear from bmp format dependant code
static void draw_Ladder(){
//if (PIXEL_FORMAT_DEFAULT!=PIXEL_FORMAT_I4)
// return;
Transform_OVERLAY_WIDTH=OVERLAY_WIDTH;
Transform_OVERLAY_HEIGHT=OVERLAY_HEIGHT;
Transform_Pitch=last_pitch/10;
Transform_Roll=-last_roll/10;
int TiltY= - AHI_TiltY;//pixels offset on the vertical, negative value means up, this is camera nose-down angle in pixels
//to do, make this in degrees
const bool horizonInvertPitch = false;
const bool horizonInvertRoll = false;
double horizonWidth = 3;
const int horizonSpacing = 165;//????//pixels per degree 165
const bool horizonShowLadder = true;
int horizonRange = 80; //total vertical range in degrees
const int horizonStep = 10;//????//degrees per line
const bool show_center_indicator = false;////m_show_center_indicator;
const double ladder_stroke_faktor=0.1;
const int subline_thickness=2;
if (OVERLAY_HEIGHT<900){//720p mode
horizonWidth = 2;
horizonRange = 50;
}
double roll_degree = ((double)last_roll)/10;
double pitch_degree = ((double)last_pitch)/10;
if (horizonInvertRoll == true){
roll_degree=roll_degree*-1;
}
if (horizonInvertPitch == false){
pitch_degree=pitch_degree*-1;
}
const int pos_x= OVERLAY_WIDTH/2;
const int pos_y= (OVERLAY_HEIGHT/2) + TiltY;
const int width_ladder= 100*horizonWidth;
int px = pos_x - width_ladder / 2;
if(/*show_center_indicator*/true){
// Line always drawn in the center to have some orientation where the center is
int line_w = 100 * horizonWidth * 0.2;
LineDirect(bmpBuff.pData, OVERLAY_WIDTH, OVERLAY_HEIGHT, px-20,pos_y-3,px+20,pos_y-3, COLOR_GRAY_Light,2);
LineDirect(bmpBuff.pData, OVERLAY_WIDTH, OVERLAY_HEIGHT, px+width_ladder-20,pos_y-3,px+width_ladder+20,pos_y-3, COLOR_GRAY_Light,2);
}
int ratio = horizonSpacing; //pixels per degree
int vrange = horizonRange; //total vertical range in degrees
int step = horizonStep; //degrees per line
if (step == 0) step = 10; // avoid div by 0
if (ratio == 0) ratio = 1; // avoid div by 0
int m_color=7;
int i;
int k;
int y;
int n;
int startH;
int stopH;
startH = pitch_degree - vrange/2;
stopH = pitch_degree + vrange/2;
if (startH<-90) startH = -90;
if (stopH>90) stopH = 90;
//painter->setPen(m_color);
int m_color_def=m_color;
for (i = startH/step; i <= stopH/step; i++) {
m_color = m_color_def;
if (i>0 && i*ratio<30 && /*showHorizonHeadingLadder*/ true ) i=i+ 30/ratio;
k = i*step;
y = pos_y - (i - 1.0*pitch_degree/step)*ratio;
if (horizonShowLadder == true) {
if (i != 0) {
//fix pitch line wrap around at extreme nose up/down
n=k;//this is the line index number relative to the main one.
if (n>90){
n=180-k;
}
if (n<-90){
n=-k-180;
}
if (abs(n)>20)//pitch higher than 30 degree.
m_color=COLOR_YELLOW;
if (abs(n)>40)//pitch higher than 30 degree.
m_color=COLOR_RED;
//left numbers
// painter->setPen(m_color);
// painter->drawText(px-30, y+6, QString::number(n));
//right numbers
//painter->drawText((px + width_ladder)+8, y+6, QString::number(n));
// painter->setPen(m_color);
if ((i > 0)) {
//Upper ladders
// default to stroke strength of 2 (I think it is pixels)
int stroke_s = 2 * ladder_stroke_faktor;
//left upper cap
//drawRectangleI4(bmpBuff.pData, px , y , stroke_s , width_ladder/24, m_color,subline_thickness);
LineTranspose(bmpBuff.pData, px, y, px, y+width_ladder/24 , m_color, subline_thickness); // Top side
//left upper line
//drawRectangleI4(bmpBuff.pData, px , y , width_ladder/3 , stroke_s, m_color,subline_thickness);
LineTranspose(bmpBuff.pData, px, y, px + width_ladder/3, y , m_color, subline_thickness); // Top side
//right upper cap
//drawRectangleI4(bmpBuff.pData, px+width_ladder-2 , y , px+width_ladder-2 + stroke_s , width_ladder/24, m_color,subline_thickness);
LineTranspose(bmpBuff.pData, px+width_ladder-2 , y , px+width_ladder-2, y+width_ladder/24 , m_color, subline_thickness); // Top side
//right upper line
//drawRectangleI4(bmpBuff.pData, px+width_ladder*2/3 , y , width_ladder/3 , stroke_s, m_color,subline_thickness);
LineTranspose(bmpBuff.pData, px+width_ladder*2/3 , y , (px+width_ladder*2/3) + width_ladder/3 , y , m_color, subline_thickness); // Top side
} else if (i < 0) {
// Lower ladders
// default to stroke strength of 2 (I think it is pixels)
int stroke_s = 2 * ladder_stroke_faktor;
//left to right
//left lower cap
//drawRectangleI4(bmpBuff.pData, px, y-(width_ladder/24)+2 , stroke_s , width_ladder/24, m_color,subline_thickness);
LineTranspose(bmpBuff.pData, px, y-(width_ladder/24)+2 , px , y-(width_ladder/24)+1 + width_ladder/24 , m_color, subline_thickness); // Top side
//1l
//drawRectangleI4(bmpBuff.pData, px , y , width_ladder/12 , stroke_s, m_color,subline_thickness);
LineTranspose(bmpBuff.pData, px , y , px + width_ladder/12 , y , m_color, subline_thickness); // Top side
//2l
//drawRectangleI4(bmpBuff.pData, px+(width_ladder/12)*1.5 , y , width_ladder/12 , stroke_s, m_color,subline_thickness);
LineTranspose(bmpBuff.pData, px+(width_ladder/12)*1.5 , y , px+(width_ladder/12)*1.5 + width_ladder/12 , y , m_color, subline_thickness); // Top side
//3l
//drawRectangleI4(bmpBuff.pData, px+(width_ladder/12)*3 , y , width_ladder/12 , stroke_s, m_color,subline_thickness);
LineTranspose(bmpBuff.pData, px+(width_ladder/12)*3 , y ,px+(width_ladder/12)*3 + width_ladder/12 , y , m_color, subline_thickness); // Top side
//right lower cap
//drawRectangleI4(bmpBuff.pData, px+width_ladder-2 , y-(width_ladder/24)+2 , stroke_s , width_ladder/24, m_color,subline_thickness);
LineTranspose(bmpBuff.pData, px+width_ladder-2 , y-(width_ladder/24)+2 , px+width_ladder-2 , y-(width_ladder/24)+1 + width_ladder/24 , m_color, subline_thickness); // Top side
//1r ///spacing on these might be a bit off
//drawRectangleI4(bmpBuff.pData, px+(width_ladder/12)*8 , y , width_ladder/12 , stroke_s, m_color,subline_thickness);
LineTranspose(bmpBuff.pData, px+(width_ladder/12)*8 , y , px+(width_ladder/12)*8 + width_ladder/12 , y , m_color, subline_thickness); // Top side
//2r ///spacing on these might be a bit off
//drawRectangleI4(bmpBuff.pData, px+(width_ladder/12)*9.5 , y , width_ladder/12 , stroke_s, m_color,subline_thickness);
LineTranspose(bmpBuff.pData, px+(width_ladder/12)*9.5 , y , px+(width_ladder/12)*9.5 + width_ladder/12 , y , m_color, subline_thickness); // Top side
//3r ///spacing on these might be a bit off tried a decimal here
//drawRectangleI4(bmpBuff.pData, px+(width_ladder*.9166) , y , width_ladder/12 , stroke_s, m_color,subline_thickness);
LineTranspose(bmpBuff.pData, px+(width_ladder*.9166) , y , px+(width_ladder*.9166) + width_ladder/12 , y , m_color, subline_thickness); // Top side
}
} else { // i==0
//Main AHI Line
// default to stroke strength of 3 - a bit bigger than the non center lines
int stroke_s = 4 * ladder_stroke_faktor;
int rect_height = 5; // Height of the rectangles (as per your original code)
int fragments=6;
float SpacingK=0.6;
//int rect_width = (width_ladder*2.5/(fragments + (float)SpacingK*fragments)); // Width of a single rectangle
int rect_width = width_ladder*2.5/(fragments); // Width of a single semiline+spacing
int spacing = rect_width*SpacingK ; // Spacing between rectangles
rect_width = rect_width - spacing;//Length of a small line
// Calculate the starting X position for the first rectangle
int start_x = pos_x-width_ladder*2.5/2;
// Draw 6 rectangles in a line
for (int i = 0; i < fragments; i++) {
// Calculate the X position for the current rectangle
int rect_x = start_x + i * (rect_width + spacing) + spacing/2;
bool isPlaneLevel= (-2 < last_pitch/10 && last_pitch/10 <2)&&(i==2 || i==3);
LineTranspose(bmpBuff.pData, rect_x, y, rect_x + rect_width, y, isPlaneLevel? COLOR_GREEN : COLOR_WHITE, 3);
}
int LAST_ROLL = -5; // Example integer value (change as needed)
char buffer[6]; // Enough space for "+/-", two digits, the degree symbol, and the null terminator
// Format the LAST_ROLL value into the buffer with the specified format
// %+02d: '+' sign for positive numbers, '0' for leading zero, '2' for width.
// \xB0: ASCII code for the degree symbol.
if (-10 < last_pitch && last_pitch <10)
sprintf(buffer, "%+0.1f°", -last_pitch / 10.0);
else
sprintf(buffer, "%+02d°", -last_pitch/10);
int osd_font_size=18;
#ifdef _x86
uint32_t color = getcolor(COLOR_YELLOW);
if ((-50 < last_pitch) && (last_pitch <50))
color = getcolor(COLOR_WHITE);
drawText_x86(buffer, start_x + width_ladder*2.5 - spacing/3, y + osd_font_size/2 - 4, color, osd_font_size, true,1);
#endif
if (AHI_Enabled==3){//Draw home
uint32_t xHome, yHome;
int home_offset = last_directionToHome - last_heading;
// Normalize to range [-180, 180]
if (home_offset > 180) {
home_offset -= 360;
} else if (home_offset < -180) {
home_offset += 360;
}
// home is out of AHI range
if (abs(home_offset)>90){
if (home_offset<0){ //(left < right){
xHome=start_x;
} else{
xHome=start_x+width_ladder*2.5;//Don't go over the pitch digits
}
}else{
double K = (double)((double)home_offset+90) /180;//this is from 0 to 1
if (K==0)//just in case
K=1;
xHome = (start_x + (K*((double)width_ladder)*2.5));
}
if (xHome>(start_x+width_ladder*2.5-40))//Don't go over the pitch digits
xHome=start_x+width_ladder*2.5-40;
int c=10;//Home(small house) symbol to be shown for INAV
if (font_pages>2)//tha same symbol for betflight
c=17;
//Find the coordinates if the the rectangle in the Font Bitmap
u_int16_t s_left = /*page*/0*current_display_info.font_width;
u_int16_t s_top = current_display_info.font_height * c;
u_int16_t s_width = current_display_info.font_width;
u_int16_t s_height = current_display_info.font_height;
BITMAP btmp=bitmapFnt;
if (matrix_size>10 && bmpFntSmall.u32Width>0){
btmp=bmpFntSmall;
s_width=24;
s_height=36;
s_left = 0*s_width;
s_top = s_height * c;
btmp=bmpFntSmall;
}
uint32_t xR, yR;
ApplyTransform(xHome - (s_width/2) , y - s_height, &xR, &yR);
//xR = (xR + 7) & ~7;//Round up to 8
xR = (xR + 3) & ~3;//Round up to 4, otherwise I4 bitmap image copy distorts the glyph !!!
if (xR>0 && xR<OVERLAY_WIDTH && yR>0 && yR<OVERLAY_HEIGHT){
if (PIXEL_FORMAT_DEFAULT==PIXEL_FORMAT_I4)