-
Notifications
You must be signed in to change notification settings - Fork 35
/
lv_swatch.c
1979 lines (1717 loc) · 61.7 KB
/
lv_swatch.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
/**
* @file lv_test_tileview.c
*
*/
/*********************
* INCLUDES
*********************/
#ifdef ESP32
#include <lvgl.h>
#include "struct_def.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "freertos/queue.h"
#include "esp_wifi.h"
extern xQueueHandle g_event_queue_handle;
extern const char *get_wifi_channel();
extern const char *get_wifi_rssi();
extern const char *get_wifi_ssid();
extern const char *get_wifi_address();
extern const char *get_wifi_mac();
extern int get_batt_percentage();
extern int get_ld1_status();
extern int get_ld2_status();
extern int get_ld3_status();
extern int get_ld4_status();
extern int get_dc2_status();
extern int get_dc3_status();
extern const char *get_s7xg_model();
extern const char *get_s7xg_ver();
extern const char *get_s7xg_join();
#else
#include <lv_examples/lv_apps/lv_swatch/lv_swatch.h>
const char *get_wifi_channel()
{
return "12";
}
const char *get_wifi_rssi()
{
return "-90";
}
const char *get_wifi_ssid()
{
return "Xiaomi";
}
const char *get_wifi_address()
{
return "192.168.1.1";
}
const char *get_wifi_mac()
{
return "ABC:DEF:GHI:JKL";
}
typedef struct {
float vbus_vol;
float vbus_cur;
float batt_vol;
float batt_cur;
float power;
} power_data_t;
int get_batt_percentage()
{
return 50;
}
int get_ld1_status()
{
return 1;
} int get_ld2_status()
{
return 1;
} int get_ld3_status()
{
return 0;
} int get_ld4_status()
{
return 1;
} int get_dc2_status()
{
return 0;
} int get_dc3_status()
{
return 1;
}
const char *get_s7xg_model()
{
return "s78G";
}
const char *get_s7xg_ver()
{
return "V1.08";
}
const char *get_s7xg_join()
{
return "unjoined";
}
// #define ENABLE_BLE
#endif
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
/**********************
* STATIC VARIABLES
**********************/
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/**
* Create a tileview to test their functionalities
*/
LV_IMG_DECLARE(image_location);
LV_IMG_DECLARE(img_folder);
LV_IMG_DECLARE(img_placeholder);
LV_IMG_DECLARE(img_setting);
LV_IMG_DECLARE(img_wifi);
LV_IMG_DECLARE(img_folder);
LV_IMG_DECLARE(img_menu);
LV_IMG_DECLARE(img_desktop);
LV_IMG_DECLARE(img_desktop1);
LV_IMG_DECLARE(img_desktop2);
LV_IMG_DECLARE(img_desktop3);
LV_IMG_DECLARE(img_bg0);
LV_IMG_DECLARE(img_bg1);
LV_IMG_DECLARE(img_directions);
LV_IMG_DECLARE(img_direction_up);
LV_IMG_DECLARE(img_direction_down);
LV_IMG_DECLARE(img_direction_right);
LV_IMG_DECLARE(img_direction_left);
LV_IMG_DECLARE(img_step_conut);
LV_FONT_DECLARE(font_miami);
LV_FONT_DECLARE(font_miami_32);
LV_FONT_DECLARE(font_sumptuous);
LV_FONT_DECLARE(font_sumptuous_24);
LV_IMG_DECLARE(img_power);
LV_IMG_DECLARE(img_batt1);
LV_IMG_DECLARE(img_batt2);
LV_IMG_DECLARE(img_batt3);
LV_IMG_DECLARE(img_batt4);
LV_IMG_DECLARE(img_ttgo);
LV_IMG_DECLARE(img_lora);
LV_IMG_DECLARE(img_bluetooth);
LV_IMG_DECLARE(img_alipay);
LV_IMG_DECLARE(img_wechatpay);
LV_IMG_DECLARE(img_qr);
typedef lv_res_t (*lv_menu_action_t) (lv_obj_t *obj);
typedef void (*lv_menu_destory_t) (void);
typedef struct {
const char *name;
lv_menu_action_t callback;
} lv_lora_struct_t;
typedef struct {
const char *name;
lv_menu_action_t callback;
lv_menu_destory_t destroy;
void *src_img;
lv_obj_t *cont;
} lv_menu_struct_t ;
typedef struct {
const char *name;
lv_obj_t *label;
} lv_gps_struct_t;
typedef struct {
const char *name;
lv_obj_t *label;
const char *(*get_val)(void);
} lv_wifi_struct_t;
typedef struct {
lv_obj_t *time_label;
lv_obj_t *step_count_label;
lv_obj_t *date_label;
lv_obj_t *temp_label;
} lv_main_struct_t;
static lv_obj_t *menu_cont = NULL;
static lv_obj_t *main_cont = NULL;
static lv_obj_t *g_menu_win = NULL;
static lv_obj_t *g_tileview = NULL;
static int g_menu_view_width;
static int g_menu_view_height;
static int curr_index = -1;
static bool g_menu_in = false;
static int prev = -1;
#ifdef ESP32
static wifi_auth_t auth;
#endif
static lv_main_struct_t main_data;
static char buff[256];
static lv_obj_t *img_batt = NULL;
static lv_task_t *chaging_handle = NULL;
static uint8_t changin_icons = 0;
lv_task_t *monitor_handle = NULL;
static bool gps_anim_started = false;
static lv_obj_t *gps_anim_cont = NULL;
/*****************************/
static lv_obj_t *gContainer = NULL;
static lv_obj_t *gObjecter = NULL;
static lv_res_t lv_setting_backlight_action(lv_obj_t *obj, const char *txt);
static lv_res_t lv_setting_th_action(lv_obj_t *obj);
static lv_res_t lv_tileview_action(lv_obj_t *obj, lv_coord_t x, lv_coord_t y);
static lv_res_t menubtn_action(lv_obj_t *btn);
static void lv_setWinMenuHeader(const char *title, const void *img_src, lv_action_t action);
static lv_res_t lv_file_setting(lv_obj_t *par);
static lv_res_t lv_setting(lv_obj_t *par);
static lv_res_t lv_gps_setting(lv_obj_t *par);
static lv_res_t lv_wifi_setting(lv_obj_t *par);
static lv_res_t lv_motion_setting(lv_obj_t *par);
static lv_res_t lv_power_setting(lv_obj_t *par);
static lv_res_t lv_lora_setting(lv_obj_t *par);
static lv_res_t lv_ble_setting(lv_obj_t *par);
static lv_res_t lv_pay(lv_obj_t *par);
static void lv_gps_setting_destroy();
static void lv_wifi_setting_destroy();
static void lv_file_setting_destroy();
static void lv_motion_setting_destroy();
static void lv_connect_wifi(const char *password);
static void lv_power_setting_destroy(void);
static void lv_lora_setting_destroy(void);
static void lv_setting_destroy(void);
static void lv_ble_setting_destroy();
static void lv_pay_destroy();
static lv_res_t lora_Sender(lv_obj_t *obj);
static lv_res_t lora_Receiver(lv_obj_t *obj);
static lv_res_t lora_LoRaWaln(lv_obj_t *obj);
static lv_res_t lora_HardwareInfo(lv_obj_t *obj);
static void lv_menu_del();
static const void *lv_get_batt_icon();
static lv_res_t win_btn_click(lv_obj_t *btn);
static void lv_setWinBtnInvaild(bool en);
lv_res_t lv_timer_start(void (*timer_callback)(void *), uint32_t period, void *param);
static void lv_setWinMenuHeader(const char *title, const void *img_src, lv_action_t action);
static void *motion_img_src[4] = {
&img_direction_up,
&img_direction_down,
&img_direction_left,
&img_direction_right,
};
static lv_gps_struct_t gps_data[] = {
{.name = "lat:"},
{.name = "lng:"},
{.name = "satellites:"},
{.name = "date:"},
{.name = "altitude:"}, //meters
{.name = "course:"},
{.name = "speed:"} //kmph
};
static lv_wifi_struct_t wifi_data[] = {
{.name = "SSID", .get_val = get_wifi_ssid},
{.name = "IP", .get_val = get_wifi_address},
{.name = "RSSI", .get_val = get_wifi_rssi},
{.name = "CHL", .get_val = get_wifi_channel},
{.name = "MAC", .get_val = get_wifi_mac},
};
static lv_menu_struct_t menu_data[] = {
#ifdef ENABLE_BLE
{.name = "Bluetooth", .callback = lv_ble_setting, .destroy = lv_ble_setting_destroy, .src_img = &img_bluetooth},
#endif
#if defined(ACSIP_S7XG_MODULE) && !defined(UBOX_GPS_MODULE)
{.name = "GPS", .callback = lv_gps_setting, .destroy = lv_gps_setting_destroy, .src_img = &img_placeholder},
{.name = "LoRa", .callback = lv_lora_setting, .destroy = lv_lora_setting_destroy, .src_img = &img_lora},
#elif defined(UBOX_GPS_MODULE)
{.name = "GPS", .callback = lv_gps_setting, .destroy = lv_gps_setting_destroy, .src_img = &img_placeholder},
#endif
{.name = "WiFi", .callback = lv_wifi_setting, .destroy = lv_wifi_setting_destroy, .src_img = &img_wifi},
{.name = "Power", .callback = lv_power_setting, .destroy = lv_power_setting_destroy, .src_img = &img_power},
{.name = "Setting", .callback = lv_setting, .destroy = lv_setting_destroy, .src_img = &img_setting},
{.name = "SD Card", .callback = lv_file_setting, .destroy = lv_file_setting_destroy, .src_img = &img_folder},
{.name = "Sensor", .callback = lv_motion_setting, .destroy = lv_motion_setting_destroy, .src_img = &img_directions},
//pay
{.name = "WechatPay", .callback = lv_pay, .destroy = lv_pay_destroy, .src_img = &img_wechatpay},
{.name = "AliPay", .callback = lv_pay, .destroy = lv_pay_destroy, .src_img = &img_alipay},
};
/*********************************************************************
*
* PAY
*
* ******************************************************************/
static lv_res_t lv_pay(lv_obj_t *par)
{
gContainer = lv_obj_create(par, NULL);
lv_obj_set_size(gContainer, g_menu_view_width, g_menu_view_height);
lv_obj_set_style(gContainer, &lv_style_transp_fit);
lv_obj_t *img = lv_img_create(gContainer, NULL);
lv_img_set_src(img, &img_qr);
lv_obj_align(img, NULL, LV_ALIGN_CENTER, 0, 0);
}
static void lv_pay_destroy()
{
lv_obj_del(gContainer);
gContainer = NULL;
gObjecter = NULL;
}
/*********************************************************************
*
* BLUETOOTH
*
* ******************************************************************/
typedef struct {
lv_obj_t *lmeter;
lv_obj_t *label;
} lv_soil_t;
lv_soil_t soil_data[3];
static bool connect = false;
extern void soil_led_control();
void lv_soil_btn_cb(lv_obj_t *obj)
{
#ifdef ESP32
soil_led_control();
#endif
}
void lv_soil_data_update(float humidity, float temperature, int soil)
{
if (!connect)return;
snprintf(buff, sizeof(buff), "%.2f", humidity);
lv_label_set_text(soil_data[0].label, buff);
lv_lmeter_set_value(soil_data[0].lmeter, (int)humidity);
snprintf(buff, sizeof(buff), "%.2f", temperature);
lv_label_set_text(soil_data[1].label, buff);
lv_lmeter_set_value(soil_data[1].lmeter, (int)temperature);
snprintf(buff, sizeof(buff), "%d%", soil);
lv_label_set_text(soil_data[2].label, buff);
lv_lmeter_set_value(soil_data[2].lmeter, soil);
}
void lv_soil_test_create()
{
if (gContainer)
lv_obj_clean(gContainer);
/*Create a simple style with ticker line width*/
static lv_style_t style_lmeter1;
lv_style_copy(&style_lmeter1, &lv_style_pretty_color);
style_lmeter1.line.width = 2;
style_lmeter1.line.color = LV_COLOR_SILVER;
style_lmeter1.body.main_color = LV_COLOR_HEX(0x91bfed); /*Light blue*/
style_lmeter1.body.grad_color = LV_COLOR_HEX(0x04386c); /*Dark blue*/
for (int i = 0; i < 3; i++) {
soil_data[i].lmeter = lv_lmeter_create(gContainer, NULL);
lv_lmeter_set_range(soil_data[i].lmeter, 0, 100); /*Set the range*/
lv_lmeter_set_value(soil_data[i].lmeter, 30); /*Set the current value*/
lv_lmeter_set_style(soil_data[i].lmeter, &style_lmeter1); /*Apply the new style*/
lv_obj_set_size(soil_data[i].lmeter, 50, 50);
soil_data[i].label = lv_label_create(soil_data[i].lmeter, NULL);
lv_label_set_text(soil_data[i].label, "N/A");
lv_label_set_style(soil_data[i].label, &lv_style_pretty);
lv_obj_align(soil_data[i].label, NULL, LV_ALIGN_CENTER, 0, 0);
if (i == 0)
lv_obj_align(soil_data[i].lmeter, NULL, LV_ALIGN_IN_TOP_LEFT, 20, 10);
else
lv_obj_align(soil_data[i].lmeter, soil_data[i - 1].lmeter, LV_ALIGN_OUT_RIGHT_MID, 20, 0);
}
lv_obj_t *scanbtn = lv_btn_create(gContainer, NULL);
lv_obj_align(scanbtn, gContainer, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
lv_obj_set_size(scanbtn, 100, 25);
lv_obj_t *label = lv_label_create(scanbtn, NULL);
lv_label_set_text(label, "LED");
lv_btn_set_action(scanbtn, LV_BTN_ACTION_PR, lv_soil_btn_cb);
connect = true;
}
static lv_res_t ble_list_action(lv_obj_t *obj)
{
#ifdef ESP32
lv_setWinBtnInvaild(true);
task_event_data_t event_data;
event_data.type = MESS_EVENT_BLE;
event_data.ble.event = LV_BLE_CONNECT;
event_data.ble.index = lv_list_get_btn_index(gObjecter, obj);
xQueueSend(g_event_queue_handle, &event_data, portMAX_DELAY);
#else
const char *dev = lv_list_get_btn_text(obj);
int32_t index = lv_list_get_btn_index(gObjecter, obj);
printf("connect device : %s index:%d\n", dev, index);
#endif
return LV_RES_OK;
}
void lv_ble_device_list_add(const char *name)
{
if (!gObjecter) {
lv_setWinBtnInvaild(true);
if (!name) {
lv_obj_t *obj = lv_obj_get_child_back(gContainer, NULL);
lv_label_set_text(obj, "No Search Device");
lv_obj_align(obj, NULL, LV_ALIGN_CENTER, 0, -10);
return;
}
lv_obj_clean(gContainer);
gObjecter = lv_list_create(gContainer, NULL);
lv_obj_set_size(gObjecter, g_menu_view_width, g_menu_view_height);
lv_obj_align(gObjecter, gContainer, LV_ALIGN_CENTER, 0, 0);
lv_obj_set_style(gObjecter, &lv_style_transp_fit);
}
lv_list_add(gObjecter, SYMBOL_BLUETOOTH, name, ble_list_action);
return LV_RES_OK;
}
static lv_res_t lv_mbox_btn_callback(lv_obj_t *obj, const char *txt)
{
lv_mbox_start_auto_close(lv_obj_get_parent(obj), 0);
}
void lv_ble_mbox_event(const char *event_txt)
{
printf("lv_ble_mbox_event : %s\n", event_txt);
if (!gContainer)return;
connect = false;
lv_obj_clean(gContainer);
/*Create a new background style*/
static lv_style_t style_bg;
lv_style_copy(&style_bg, &lv_style_pretty);
style_bg.body.main_color = LV_COLOR_MAKE(0xf5, 0x45, 0x2e);
style_bg.body.grad_color = LV_COLOR_MAKE(0xb9, 0x1d, 0x09);
style_bg.body.border.color = LV_COLOR_MAKE(0x3f, 0x0a, 0x03);
style_bg.text.color = LV_COLOR_WHITE;
style_bg.body.padding.hor = 12;
style_bg.body.padding.ver = 8;
style_bg.body.shadow.width = 8;
/*Create released and pressed button styles*/
static lv_style_t style_btn_rel;
static lv_style_t style_btn_pr;
lv_style_copy(&style_btn_rel, &lv_style_btn_rel);
style_btn_rel.body.empty = 1; /*Draw only the border*/
style_btn_rel.body.border.color = LV_COLOR_WHITE;
style_btn_rel.body.border.width = 2;
style_btn_rel.body.border.opa = LV_OPA_70;
style_btn_rel.body.padding.hor = 12;
style_btn_rel.body.padding.ver = 8;
lv_style_copy(&style_btn_pr, &style_btn_rel);
style_btn_pr.body.empty = 0;
style_btn_pr.body.main_color = LV_COLOR_MAKE(0x5d, 0x0f, 0x04);
style_btn_pr.body.grad_color = LV_COLOR_MAKE(0x5d, 0x0f, 0x04);
lv_obj_t *mbox1 = lv_mbox_create(gContainer, NULL);
lv_mbox_set_text(mbox1, event_txt); /*Set the text*/
static const char *btns[] = {"\221Apply", ""}; /*Button description. '\221' lv_btnm like control char*/
lv_mbox_add_btns(mbox1, btns, NULL);
lv_obj_set_width(mbox1, 180);
lv_obj_align(mbox1, NULL, LV_ALIGN_IN_TOP_LEFT, 10, 10); /*Align to the corner*/
lv_mbox_set_style(mbox1, LV_MBOX_STYLE_BG, &style_bg);
lv_mbox_set_style(mbox1, LV_MBOX_STYLE_BTN_REL, &style_btn_rel);
lv_mbox_set_style(mbox1, LV_MBOX_STYLE_BTN_PR, &style_btn_pr);
lv_mbox_set_action(mbox1, lv_mbox_btn_callback);
}
static void lv_ble_setting_destroy()
{
connect = false;
#ifdef ESP32
task_event_data_t event_data;
event_data.type = MESS_EVENT_BLE;
event_data.ble.event = LV_BLE_DISCONNECT;
xQueueSend(g_event_queue_handle, &event_data, portMAX_DELAY);
#endif
lv_obj_del(gContainer);
gContainer = NULL;
gObjecter = NULL;
}
static lv_res_t bluetooth_scan_btn_cb( lv_obj_t *obj)
{
#ifdef ESP32
lv_setWinBtnInvaild(false);
task_event_data_t event_data;
event_data.type = MESS_EVENT_BLE;
event_data.ble.event = LV_BLE_SCAN;
xQueueSend(g_event_queue_handle, &event_data, portMAX_DELAY);
lv_obj_clean(gContainer);
lv_obj_t *label = lv_label_create(gContainer, NULL);
lv_label_set_text(label, "Scaning...");
lv_obj_align(label, NULL, LV_ALIGN_CENTER, 0, -10);
#else
const char *ssid[] = {"SoilTest0", "SoilTest1"};
for (int i = 0; i < 2; i++) {
lv_ble_device_list_add(ssid[i]);
}
#endif
return LV_RES_OK;
}
static lv_res_t lv_ble_setting(lv_obj_t *par)
{
lv_obj_t *label = NULL;
gContainer = lv_obj_create(par, NULL);
lv_obj_set_size(gContainer, g_menu_view_width, g_menu_view_height);
lv_obj_set_style(gContainer, &lv_style_transp_fit);
// lv_soil_test_create();return;
// for (int i = 0; i < sizeof(wifi_data) / sizeof(wifi_data[0]); ++i) {
// wifi_data[i].label = lv_label_create(gContainer, NULL);
// snprintf(buff, sizeof(buff), "%s:%s", wifi_data[i].name, wifi_data[i].get_val());
// lv_label_set_text(wifi_data[i].label, buff);
// if (!i)
// lv_obj_align(wifi_data[i].label, gContainer, LV_ALIGN_IN_TOP_MID, 0, 0);
// else
// lv_obj_align(wifi_data[i].label, wifi_data[i - 1].label, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);
// }
lv_obj_t *scanbtn = lv_btn_create(gContainer, NULL);
lv_obj_align(scanbtn, gContainer, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
lv_obj_set_size(scanbtn, 100, 25);
label = lv_label_create(scanbtn, NULL);
lv_label_set_text(label, "Scan");
lv_btn_set_action(scanbtn, LV_BTN_ACTION_PR, bluetooth_scan_btn_cb);
return LV_RES_OK;
}
/*********************************************************************
*
* LORA
*
* ******************************************************************/
#define LV_LORA_TITLE_1 "Sender"
#define LV_LORA_TITLE_2 "Receiver"
#define LV_LORA_TITLE_3 "LoRaWaln"
#define LV_LORA_TITLE_4 "Hardware"
static const char *loraMap[] = {LV_LORA_TITLE_1, "\n",
LV_LORA_TITLE_2, "\n",
LV_LORA_TITLE_3, "\n",
LV_LORA_TITLE_4,
""
};
lv_lora_struct_t lora_data [] = {
{"Sender", lora_Sender},
{"Receiver", lora_Receiver},
{"LoRaWaln", lora_LoRaWaln},
{"Hardware", lora_HardwareInfo}
};
static lv_res_t lora_HardwareInfo(lv_obj_t *obj)
{
static lv_wifi_struct_t lora_data[] = {
{.name = "Model", .get_val = get_s7xg_model},
{.name = "Version", .get_val = get_s7xg_ver},
{.name = "Join", .get_val = get_s7xg_join},
};
lv_obj_t *label = NULL;
gObjecter = lv_obj_create(g_menu_win, NULL);
lv_obj_set_size(gObjecter, g_menu_view_width, g_menu_view_height);
lv_obj_set_style(gObjecter, &lv_style_transp_fit);
for (int i = 0; i < sizeof(lora_data) / sizeof(lora_data[0]); ++i) {
lora_data[i].label = lv_label_create(gObjecter, NULL);
snprintf(buff, sizeof(buff), "%s:%s", lora_data[i].name, lora_data[i].get_val());
lv_label_set_text(lora_data[i].label, buff);
if (!i)
lv_obj_align(lora_data[i].label, gObjecter, LV_ALIGN_IN_TOP_MID, 0, 0);
else
lv_obj_align(lora_data[i].label, lora_data[i - 1].label, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);
}
return LV_RES_OK;
}
static lv_obj_t *ta1 = NULL;
void timer1_callback(void *a)
{
static int i = 0;
char bf[128];
snprintf(bf, sizeof(bf), "lora send %d\n", ++i);
if (lv_txt_get_encoded_length(lv_ta_get_text(ta1)) >= lv_ta_get_max_length(ta1)) {
lv_ta_set_text(ta1, ""); /*Set an initial text*/
}
lv_ta_add_text(ta1, bf);
}
void lora_add_message(const char *txt)
{
if (!txt || !ta1)return;
if (lv_txt_get_encoded_length(lv_ta_get_text(ta1)) >= lv_ta_get_max_length(ta1)) {
lv_ta_set_text(ta1, "");
}
lv_ta_add_text(ta1, txt);
}
static void lora_create_windows()
{
static lv_style_t style_sb;
lv_style_copy(&style_sb, &lv_style_transp_fit);
gObjecter = lv_obj_create(g_menu_win, NULL);
lv_obj_set_size(gObjecter, g_menu_view_width, g_menu_view_height);
lv_obj_set_style(gObjecter, &lv_style_transp_fit);
/*Create a normal Text area*/
ta1 = lv_ta_create(gObjecter, NULL);
lv_obj_set_size(ta1, g_menu_view_width, g_menu_view_height);
lv_obj_align(ta1, NULL, LV_ALIGN_CENTER, 0, 0);
lv_ta_set_style(ta1, LV_TA_STYLE_BG, &style_sb); /*Apply the scroll bar style*/
lv_ta_set_cursor_type(ta1, LV_CURSOR_NONE);
lv_ta_set_text(ta1, ""); /*Set an initial text*/
lv_ta_set_max_length(ta1, 255);
}
static lv_res_t lora_Sender(lv_obj_t *obj)
{
lora_create_windows();
// lv_task_t *handle = lv_task_create(timer1_callback, 500, LV_TASK_PRIO_LOW, NULL);
#ifdef ESP32
task_event_data_t event_data;
event_data.type = MESS_EVENT_LORA;
event_data.lora.event = LVGL_S7XG_LORA_SEND;
xQueueSend(g_event_queue_handle, &event_data, portMAX_DELAY);
#endif
return LV_RES_OK;
}
static lv_res_t lora_Receiver(lv_obj_t *obj)
{
lora_create_windows();
#ifdef ESP32
task_event_data_t event_data;
event_data.type = MESS_EVENT_LORA;
event_data.lora.event = LVGL_S7XG_LORA_RECV;
xQueueSend(g_event_queue_handle, &event_data, portMAX_DELAY);
#endif
return LV_RES_OK;
}
static lv_res_t lora_LoRaWaln(lv_obj_t *obj)
{
lora_create_windows();
return LV_RES_OK;
}
lv_res_t lv_lora_action (struct _lv_obj_t *obj)
{
#ifdef ESP32
task_event_data_t event_data;
event_data.type = MESS_EVENT_LORA;
event_data.lora.event = LVGL_S7XG_LORA_STOP;
xQueueSend(g_event_queue_handle, &event_data, portMAX_DELAY);
#endif
if (gObjecter)
lv_obj_del(gObjecter);
gObjecter = NULL;
ta1 = NULL;
lv_obj_set_hidden(gContainer, false);
lv_setWinMenuHeader(NULL, SYMBOL_HOME, win_btn_click);
}
/*Called when a button is released ot long pressed*/
static lv_res_t lora_btnm_action(lv_obj_t *btnm, const char *txt)
{
for (int i = 0; i < sizeof(lora_data) / sizeof(lora_data[0]); ++i) {
if (strcmp(txt, lora_data[i].name) == 0) {
lv_obj_set_hidden(gContainer, true);
lv_setWinMenuHeader(NULL, SYMBOL_LEFT, lv_lora_action);
// printf("[%d] %s\n", i, lora_data[i].name);
if (lora_data[i].callback) {
lora_data[i].callback(NULL);
}
break;
}
}
return LV_RES_OK; /*Return OK because the button matrix is not deleted*/
}
static lv_res_t lv_lora_setting(lv_obj_t *par)
{
gContainer = lv_obj_create(par, NULL);
lv_obj_set_size(gContainer, g_menu_view_width, g_menu_view_height);
lv_obj_set_style(gContainer, &lv_style_transp_fit);
/*Create a default button matrix*/
lv_obj_t *btnm1 = lv_btnm_create(gContainer, NULL);
lv_btnm_set_map(btnm1, loraMap);
lv_btnm_set_action(btnm1, lora_btnm_action);
lv_obj_set_size(btnm1, 180, LV_VER_RES / 2);
lv_obj_align(btnm1, NULL, LV_ALIGN_CENTER, 0, -20);
return LV_RES_OK;
}
static void lv_lora_setting_destroy(void)
{
lv_obj_del(gContainer);
gContainer = NULL;
}
/*********************************************************************
*
* OHTER
*
* ******************************************************************/
static void lv_setWinBtnInvaild(bool en)
{
lv_win_ext_t *ext = lv_obj_get_ext_attr(g_menu_win);
lv_obj_t *obj = NULL;
obj = lv_obj_get_child_back(ext->header, NULL);
obj = lv_obj_get_child_back(ext->header, obj);
if (obj != NULL) {
lv_obj_set_click(obj, en);
}
}
static void lv_setWinMenuHeader(const char *title, const void *img_src, lv_action_t action)
{
lv_win_ext_t *ext = lv_obj_get_ext_attr(g_menu_win);
lv_obj_t *obj = NULL;
obj = lv_obj_get_child_back(ext->header, NULL);
if (!title)
lv_label_set_text(ext->title, title);
obj = lv_obj_get_child_back(ext->header, obj);
if (obj != NULL) {
lv_btn_set_action(obj, LV_BTN_ACTION_CLICK, action);
obj = lv_obj_get_child_back(obj, NULL);
lv_img_set_src(obj, img_src);
}
}
lv_res_t lv_timer_start(void (*timer_callback)(void *), uint32_t period, void *param)
{
lv_task_t *handle = lv_task_create(timer_callback, period, LV_TASK_PRIO_LOW, param);
lv_task_once(handle);
return LV_RES_OK;
}
static lv_point_t lv_font_get_size(lv_obj_t *obj)
{
lv_style_t *style = lv_obj_get_style(obj);
const lv_font_t *font = style->text.font;
lv_label_ext_t *ext = lv_obj_get_ext_attr(obj);
lv_point_t size;
lv_txt_flag_t flag = LV_TXT_FLAG_NONE;
if (ext->recolor != 0) flag |= LV_TXT_FLAG_RECOLOR;
if (ext->expand != 0) flag |= LV_TXT_FLAG_EXPAND;
lv_txt_get_size(&size, ext->text, font, style->text.letter_space, style->text.line_space, LV_COORD_MAX, flag);
return size;
}
/*********************************************************************
*
* BATTERY
*
* ******************************************************************/
void lv_update_battery_percent(int percent)
{
static void *img_src[4] = {
&img_batt4,
&img_batt3,
&img_batt2,
&img_batt1,
};
if (g_menu_in) {
int i = 0;
if (percent > 92) {
i = 0;
} else if (percent > 80) {
i = 1;
} else if (percent > 50) {
i = 2;
} else {
i = 3;
}
lv_img_set_src(img_batt, img_src[i]);
}
}
static const void *lv_get_batt_icon()
{
int percent = get_batt_percentage();
if (percent > 98) {
return &img_batt4;
}
if (percent > 80) {
return &img_batt3;
}
if (percent > 50) {
return &img_batt2;
} else {
return &img_batt1;
}
}
void charging_anim_callback()
{
static void *src_img[] = {
&img_batt1,
&img_batt2,
&img_batt3,
&img_batt4
};
if (g_menu_in) {
changin_icons = changin_icons + 1 >= sizeof(src_img) / sizeof(src_img[0]) ? 0 : changin_icons + 1;
lv_img_set_src(img_batt, src_img[changin_icons]);
}
}
void charging_anim_stop()
{
if (chaging_handle) {
lv_task_del(chaging_handle);
chaging_handle = NULL;
changin_icons = 0;
lv_img_set_src(img_batt, lv_get_batt_icon());
}
}
void charging_anim_start()
{
if (!chaging_handle) {
chaging_handle = lv_task_create(charging_anim_callback, 1000, LV_TASK_PRIO_LOW, NULL);
}
}
/*********************************************************************
*
* POWER
*
* ******************************************************************/
typedef struct {
int base;
int x;
int y;
lv_obj_t *label;
lv_align_t align;
const char *txt;
int (*get_func)(void);
} lv_power_list_t;
lv_power_list_t list[] = {
{-1, 10, 0, NULL, LV_ALIGN_IN_TOP_MID, "Batt"}, //0
{0, -50, 0, NULL, LV_ALIGN_OUT_LEFT_MID, "USB"}, //1
{0, 40, 0, NULL, LV_ALIGN_OUT_RIGHT_MID, "Uint"}, //2
{0, 0, 10, NULL, LV_ALIGN_OUT_BOTTOM_MID, "0000.00"}, //3
{1, 0, 10, NULL, LV_ALIGN_OUT_BOTTOM_MID, "0000.00"}, //4
{2, 0, 10, NULL, LV_ALIGN_OUT_BOTTOM_MID, "mV"}, //5
{3, 0, 10, NULL, LV_ALIGN_OUT_BOTTOM_MID, "0000.00"}, //6
{4, 0, 10, NULL, LV_ALIGN_OUT_BOTTOM_MID, "0000.00"}, //7
{5, 0, 10, NULL, LV_ALIGN_OUT_BOTTOM_MID, "mA"}, //8
};
enum {
LV_BATT_VOL_INDEX = 3,
LV_VBUS_VOL_INDEX = 4,
LV_BATT_CUR_INDEX = 6,
LV_VBUS_CUR_INDEX = 7,
};
void lv_update_power_info(power_data_t *data)
{
snprintf(buff, sizeof(buff), "%.2f", data->vbus_vol);
lv_label_set_text(list[LV_VBUS_VOL_INDEX].label, buff);
snprintf(buff, sizeof(buff), "%.2f", data->vbus_cur);
lv_label_set_text(list[LV_VBUS_CUR_INDEX].label, buff);
snprintf(buff, sizeof(buff), "%.2f", data->batt_vol);
lv_label_set_text(list[LV_BATT_VOL_INDEX].label, buff);
snprintf(buff, sizeof(buff), "%.2f", data->batt_cur);
lv_label_set_text(list[LV_BATT_CUR_INDEX].label, buff);
}
static lv_res_t lv_power_setting(lv_obj_t *par)
{
gContainer = lv_obj_create(g_menu_win, NULL);
lv_obj_set_size(gContainer, g_menu_view_width, g_menu_view_height);
lv_obj_set_style(gContainer, &lv_style_transp_fit);
static lv_style_t style_txt;
lv_style_copy(&style_txt, &lv_style_plain);
style_txt.text.font = &lv_font_dejavu_20;
style_txt.text.letter_space = 2;
style_txt.text.line_space = 1;
style_txt.text.color = LV_COLOR_HEX(0xffffff);
for (int i = 0; i < sizeof(list) / sizeof(list[0]); i++) {
list[i].label = lv_label_create(gContainer, NULL);
lv_label_set_text(list[i].label, list[i].txt);
if ( list[i].base != -1)
lv_obj_align( list[i].label, list[list[i].base].label, list[i].align, list[i].x, list[i].y);
else
lv_obj_align( list[i].label, gContainer, LV_ALIGN_IN_TOP_MID, list[i].x, list[i].y);
lv_obj_set_style(list[i].label, &style_txt);
}
lv_power_list_t state[] = {
{-1, 0, 85, NULL, LV_ALIGN_IN_TOP_MID, "LD2"},
{-2, 0, 5, NULL, LV_ALIGN_OUT_BOTTOM_MID, NULL, .get_func = get_ld2_status},
{0, -50, 0, NULL, LV_ALIGN_OUT_LEFT_MID, "LD1"},
{-2, 0, 5, NULL, LV_ALIGN_OUT_BOTTOM_MID, NULL, .get_func = get_ld1_status},
{0, 40, 0, NULL, LV_ALIGN_OUT_RIGHT_MID, "LD3"},
{-2, 0, 5, NULL, LV_ALIGN_OUT_BOTTOM_MID, NULL, .get_func = get_ld3_status},
{1, 0, 0, NULL, LV_ALIGN_OUT_BOTTOM_MID, "DC2"},
{-2, 0, 5, NULL, LV_ALIGN_OUT_BOTTOM_MID, NULL, .get_func = get_dc2_status},
{3, 0, 0, NULL, LV_ALIGN_OUT_BOTTOM_MID, "LD4"},
{-2, 0, 5, NULL, LV_ALIGN_OUT_BOTTOM_MID, NULL, .get_func = get_ld4_status},
{5, 0, 0, NULL, LV_ALIGN_OUT_BOTTOM_MID, "DC3"},
{-2, 0, 5, NULL, LV_ALIGN_OUT_BOTTOM_MID, NULL, .get_func = get_dc3_status},
};
static lv_style_t style_led;
lv_style_copy(&style_led, &lv_style_pretty_color);
style_led.body.radius = LV_RADIUS_CIRCLE;
style_led.body.main_color = LV_COLOR_GREEN;
style_led.body.grad_color = LV_COLOR_GREEN;
style_led.body.border.color = LV_COLOR_GREEN;
style_led.body.border.width = 3;
style_led.body.border.opa = LV_OPA_100;
style_led.body.shadow.color = LV_COLOR_GREEN;
style_led.body.shadow.width = 0;
for (int i = 0; i < sizeof(state) / sizeof(state[0]); i++) {
if (state[i].base == -2) {
state[i].label = lv_led_create(gContainer, NULL);
lv_obj_set_size(state[i].label, 10, 10);
lv_obj_set_style(state[i].label, &style_led);
lv_obj_align( state[i].label, state[i - 1].label, state[i].align, state[i].x, state[i].y);
if (state[i].get_func()) {
lv_led_on(state[i].label);
} else {