-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_main.c
1231 lines (1073 loc) · 33.4 KB
/
app_main.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
/**
* Copyright (c) 2020 Rockchip Electronics Co., Ltd
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <rtthread.h>
#include <rtdef.h>
#include <stdio.h>
#include <math.h>
#include <dfs_posix.h>
#include "drv_heap.h"
#include "drv_display.h"
#include "drv_wdt.h"
#include "image_info.h"
#include "display.h"
#include "auto_version.h"
#if defined(RT_USING_TOUCH_DRIVERS)
#include "touch.h"
#include "touchpanel.h"
#endif
#include "app_main.h"
#include "board.h"
#include "hal_bsp.h"
#ifdef RT_USING_USB_DEVICE
#include "drv_usbd.h"
#endif
/*
**************************************************************************************************
*
* Macro define
*
**************************************************************************************************
*/
#define APP_TIMER_UPDATE_TICKS RT_TICK_PER_SECOND
#define APP_MAGIC_SUM 0x21082108
/*
**************************************************************************************************
*
* Declaration
*
**************************************************************************************************
*/
/*
**************************************************************************************************
*
* Global static struct & data define
*
**************************************************************************************************
*/
struct app_page_data_t *app_cur_page = RT_NULL;
struct app_main_data_t *app_main_data = RT_NULL;
RT_UNUSED static const rt_uint8_t month_days[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
#if (APP_PSRAM_END_RESERVE > 0)
extern uint8_t xPsramDataBase[];
extern uint8_t xPsramBase[];
extern uint8_t xPsramSize[];
struct app_info *g_app_info;
#endif
/*
**************************************************************************************************
*
* Clock tick funcitons
*
**************************************************************************************************
*/
rt_err_t app_load_img(img_load_info_t *info, rt_uint8_t *pbuf, rt_uint16_t w, rt_uint16_t h, rt_uint16_t offset, rt_uint8_t bitsize)
{
FILE *fd;
rt_uint32_t flen;
fd = fopen(info->name, "r");
if (fd <= 0)
{
rt_kprintf("open %s failed\n", info->name);
return -RT_ERROR;
}
fseek(fd, 0, SEEK_SET);
rt_uint32_t xoffset = offset;
rt_uint32_t yoffset = (h - info->h) / 2;
for (rt_uint16_t j = yoffset; j < yoffset + info->h; j++)
{
flen = fread(pbuf + (j * w + xoffset) * bitsize, 1, info->w * bitsize, fd);
if (!flen)
{
break;
}
}
fclose(fd);
return RT_EOK;
}
rt_uint32_t app_str2num(const char *str, uint8_t len)
{
rt_uint32_t num = 0;
for (rt_uint8_t i = 0; i < len; i++)
{
num += (str[i] - '0') * pow(10, (len - i - 1));
}
return num;
}
RT_UNUSED static rt_uint8_t get_day_of_week(uint16_t year, uint8_t month, uint8_t day)
{
rt_uint32_t a = month < 3 ? 1 : 0;
rt_uint32_t b = year - a;
rt_uint32_t day_of_week = (day + (31 * (month - 2 + 12 * a) / 12) + b + (b / 4) - (b / 100) + (b / 400)) % 7;
return day_of_week;
}
void app_main_get_time(struct tm **tm_time)
{
time_t now;
now = time(RT_NULL);
*tm_time = localtime(&now);
}
void app_main_set_time(struct tm *time)
{
#ifdef RT_USING_RTC
set_date(time->tm_year + 1900, time->tm_mon + 1, time->tm_mday);
set_time(time->tm_hour, time->tm_min, time->tm_sec);
#endif
}
static void app_main_init_variable(struct tm **time)
{
#ifdef RT_USING_RTC
const char *str = FIRMWARE_AUTO_VERSION;
set_date(app_str2num(&str[0], 4), app_str2num(&str[5], 2), app_str2num(&str[8], 2));
set_time(app_str2num(&str[11], 2), app_str2num(&str[14], 2), app_str2num(&str[17], 2));
#endif
app_main_get_time(time);
}
static app_clock_cb_t g_timer_cb = {NULL, NULL, 0};
static app_clock_cb_t g_timer_cb_bak = {NULL, NULL, 0};
static app_clock_cb_t g_refr_cb = {NULL, NULL, 0};
static void app_job_handler(void *parameter)
{
if (g_timer_cb.cb)
g_timer_cb.cb(g_timer_cb.param);
if (g_timer_cb_bak.cb)
{
g_timer_cb.cb = g_timer_cb_bak.cb;
g_timer_cb.param = g_timer_cb_bak.param;
g_timer_cb.timeout = g_timer_cb_bak.timeout;
g_timer_cb_bak.cb = NULL;
g_timer_cb_bak.param = NULL;
g_timer_cb_bak.timeout = 0;
rt_timer_control(app_main_data->job_timer, RT_TIMER_CTRL_SET_TIME, &g_timer_cb.timeout);
rt_timer_start(app_main_data->job_timer);
}
else
{
g_timer_cb.cb = NULL;
g_timer_cb.param = NULL;
g_timer_cb.timeout = 0;
}
}
void app_main_register_timeout_cb(rt_err_t (*cb)(void *param), void *param, uint32_t ms)
{
if (g_timer_cb.cb)
{
g_timer_cb_bak.cb = g_timer_cb.cb;
g_timer_cb_bak.param = g_timer_cb.param;
g_timer_cb_bak.timeout = g_timer_cb.timeout;
rt_timer_stop(app_main_data->job_timer);
}
g_timer_cb.cb = cb;
g_timer_cb.param = param;
g_timer_cb.timeout = ms * RT_TICK_PER_SECOND / 1000;
rt_timer_control(app_main_data->job_timer, RT_TIMER_CTRL_SET_TIME, &g_timer_cb.timeout);
rt_timer_start(app_main_data->job_timer);
}
void app_main_unregister_timeout_cb(void)
{
rt_timer_stop(app_main_data->job_timer);
g_timer_cb.cb = NULL;
g_timer_cb.param = NULL;
g_timer_cb.timeout = 0;
}
void app_main_unregister_timeout_cb_if_is(rt_err_t (*cb)(void *param))
{
if (g_timer_cb.cb == cb)
{
rt_timer_stop(app_main_data->job_timer);
g_timer_cb.cb = NULL;
g_timer_cb.param = NULL;
g_timer_cb.timeout = 0;
if (g_timer_cb_bak.cb)
{
g_timer_cb.cb = g_timer_cb_bak.cb;
g_timer_cb.param = g_timer_cb_bak.param;
g_timer_cb.timeout = g_timer_cb_bak.timeout;
g_timer_cb_bak.cb = NULL;
g_timer_cb_bak.param = NULL;
g_timer_cb_bak.timeout = 0;
rt_timer_control(app_main_data->job_timer, RT_TIMER_CTRL_SET_TIME, &g_timer_cb.timeout);
rt_timer_start(app_main_data->job_timer);
}
}
}
static void app_main_timer_tick(void *parameter)
{
struct app_main_data_t *pdata = (struct app_main_data_t *)parameter;
#if APP_WDT_ENABLE
rt_device_control(pdata->wdt_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
#endif
if (pdata->timer_cb && pdata->bl_en)
{
pdata->timer_cb();
}
}
void app_main_timer_cb_register(void (*cb)(void), uint32_t ms)
{
app_main_data->timer_cb = cb;
app_main_data->timer_cycle = ms * RT_TICK_PER_SECOND / 1000;
rt_timer_control(app_main_data->clk_timer, RT_TIMER_CTRL_SET_TIME, &app_main_data->timer_cycle);
}
void app_main_timer_cb_unregister(void)
{
app_main_data->timer_cb = RT_NULL;
app_main_data->timer_cycle = RT_TICK_PER_SECOND;
rt_timer_control(app_main_data->clk_timer, RT_TIMER_CTRL_SET_TIME, &app_main_data->timer_cycle);
}
/*
**************************************************************************************************
*
* Declaration
*
**************************************************************************************************
*/
rt_err_t app_main_touch_smooth_design(void *param)
{
struct app_main_data_t *pdata = app_main_data;
page_refrsh_request_param_t *par = (page_refrsh_request_param_t *)param;
uint16_t mov_fix;
uint32_t tick;
if (pdata->dir_mode && pdata->mov_speed)
{
tick = HAL_GetTick() - pdata->down_timestamp;
pdata->down_timestamp = HAL_GetTick();
mov_fix = ((int32_t)tick * pdata->mov_speed) / 1000;
if (par->cb)
par->cb(mov_fix);
app_refresh_request(par);
}
pdata->smooth_design = 0;
return RT_EOK;
}
void app_slide_refresh(page_refrsh_request_param_t *param)
{
#ifdef APP_USING_SMOOTH_REFRESH
if (app_main_data->smooth_design != 0)
return;
app_main_data->smooth_design = 1;
app_registe_refresh_done_cb(app_main_touch_smooth_design, param);
#endif
app_refresh_request(param);
}
void app_slide_refresh_undo(void)
{
app_main_data->smooth_design = 0;
app_registe_refresh_done_cb(NULL, NULL);
}
void app_main_keep_screen_on(void)
{
#if APP_TIMING_LIGHTOFF
if (app_main_data->bl_time && app_main_data->bl_timer)
rt_timer_start(app_main_data->bl_timer);
#endif
}
static rt_uint8_t wait_event = RT_TOUCH_EVENT_NONE;
void app_main_touch_skip(rt_uint8_t event)
{
wait_event = event;
}
rt_err_t app_main_touch_process(struct rt_touch_data *point, rt_uint8_t num)
{
struct app_main_data_t *pdata = app_main_data;
struct rt_touch_data *p = &point[0];
struct rt_touch_data *pre_p = &pdata->pre_point[0];
struct rt_touch_data *cur_p = &pdata->cur_point[0];
struct rt_touch_data *down_p = &pdata->down_point[0];
struct rt_touchpanel_block *b = &pdata->touch_block;
rt_err_t ret;
if (RT_EOK != rt_touchpoint_is_valid(p, b))
{
return -RT_ERROR;
}
struct rt_touch_data cur_point;
rt_memcpy(&cur_point, p, sizeof(struct rt_touch_data));
p = &cur_point;
p->x_coordinate -= b->x;
p->y_coordinate -= b->y;
if (app_main_data->bl_en == 0)
{
#ifndef POWER_KEY_BANK_PIN
clock_app_mq_t mq;
mq.cmd = MQ_BACKLIGHT_SWITCH;
mq.param = (void *)(uint32_t)app_main_data->bl;
rt_mq_send(app_main_data->mq, &mq, sizeof(clock_app_mq_t));
app_main_touch_skip(RT_TOUCH_EVENT_UP);
#endif
return RT_EOK;
}
app_main_keep_screen_on();
if (wait_event)
{
if (b->event == wait_event)
app_main_touch_skip(RT_TOUCH_EVENT_NONE);
return RT_EOK;
}
switch (b->event)
{
case RT_TOUCH_EVENT_DOWN:
rt_memcpy(pre_p, p, sizeof(struct rt_touch_data));
rt_memcpy(cur_p, p, sizeof(struct rt_touch_data));
rt_memcpy(down_p, p, sizeof(struct rt_touch_data));
pdata->tb_flag = 0;
pdata->dir_mode = 0;
pdata->xdir = 0;
pdata->ydir = 0;
pdata->xoffset = 0;
pdata->yoffset = 0;
pdata->touch_event = RT_TOUCH_EVENT_DOWN;
if (pdata->touch_cb.tp_touch_down)
{
ret = pdata->touch_cb.tp_touch_down(pdata);
RT_ASSERT(ret == RT_EOK);
}
break;
case RT_TOUCH_EVENT_MOVE:
pdata->touch_event = RT_TOUCH_EVENT_MOVE;
if (pdata->dir_mode == TOUCH_DIR_MODE_NULL)
{
if ((ABS(p->x_coordinate - pre_p->x_coordinate) >= TOUCH_START_THRESHOLD) ||
(ABS(p->y_coordinate - pre_p->y_coordinate) >= TOUCH_START_THRESHOLD))
{
if (ABS(p->x_coordinate - down_p->x_coordinate) > ABS(p->y_coordinate - down_p->y_coordinate))
{
if (pdata->touch_cb.tp_move_lr_start)
{
ret = pdata->touch_cb.tp_move_lr_start(pdata);
if (ret == RT_EOK)
pdata->dir_mode = TOUCH_DIR_MODE_LR;
}
else
{
pdata->dir_mode = TOUCH_DIR_MODE_LR;
}
}
else
{
if (pdata->touch_cb.tp_move_updn_start)
{
ret = pdata->touch_cb.tp_move_updn_start(pdata);
if (ret == RT_EOK)
pdata->dir_mode = TOUCH_DIR_MODE_UPDN;
}
else
{
pdata->dir_mode = TOUCH_DIR_MODE_UPDN;
}
}
}
}
if (pdata->dir_mode == TOUCH_DIR_MODE_LR)
{
if ((pdata->tb_flag) || (ABS(p->x_coordinate - pre_p->x_coordinate) >= TOUCH_MOVE_THRESHOLD))
{
if (pdata->tb_flag == 0)
{
pdata->xdir = -1;
if (p->x_coordinate > pre_p->x_coordinate)
{
pdata->xdir = 1;
}
}
pdata->xoffset += (rt_int16_t)(p->x_coordinate - pre_p->x_coordinate);
if (pdata->xoffset > TOUCH_REGION_W)
pdata->xoffset = TOUCH_REGION_W;
if (pdata->xoffset < -TOUCH_REGION_W)
pdata->xoffset = -TOUCH_REGION_W;
pdata->mov_speed = ((int32_t)p->x_coordinate - (int32_t)pre_p->x_coordinate) * 1000 / (int32_t)(p->timestamp - pre_p->timestamp);
pdata->down_timestamp = HAL_GetTick();
rt_memcpy(cur_p, p, sizeof(struct rt_touch_data));
if (pdata->touch_cb.tp_move_lr)
{
pdata->tb_flag = 0;
ret = pdata->touch_cb.tp_move_lr(pdata);
if (RT_EOK != ret)
{
pdata->tb_flag = 1;
}
}
rt_memcpy(pre_p, p, sizeof(struct rt_touch_data));
}
}
else if (pdata->dir_mode == TOUCH_DIR_MODE_UPDN)
{
if ((pdata->tb_flag) || (ABS(p->y_coordinate - pre_p->y_coordinate) >= TOUCH_MOVE_THRESHOLD))
{
if (pdata->tb_flag == 0)
{
pdata->ydir = -1;
if (p->y_coordinate > pre_p->y_coordinate)
{
pdata->ydir = 1;
}
}
pdata->yoffset += (rt_int16_t)(p->y_coordinate - pre_p->y_coordinate);
if (pdata->yoffset > TOUCH_REGION_H)
pdata->yoffset = TOUCH_REGION_H;
if (pdata->yoffset < -TOUCH_REGION_H)
pdata->yoffset = -TOUCH_REGION_H;
pdata->mov_speed = ((int32_t)p->y_coordinate - (int32_t)pre_p->y_coordinate) * 1000 / (int32_t)(p->timestamp - pre_p->timestamp);
pdata->down_timestamp = HAL_GetTick();
rt_memcpy(cur_p, p, sizeof(struct rt_touch_data));
if (pdata->touch_cb.tp_move_updn)
{
pdata->tb_flag = 0;
ret = pdata->touch_cb.tp_move_updn(pdata);
if (RT_EOK != ret)
{
pdata->tb_flag = 1;
}
}
rt_memcpy(pre_p, p, sizeof(struct rt_touch_data));
}
}
break;
case RT_TOUCH_EVENT_UP:
if (pdata->dir_mode != TOUCH_DIR_MODE_NULL)
{
if (pdata->touch_cb.tp_move_up)
{
ret = pdata->touch_cb.tp_move_up(pdata);
RT_ASSERT(ret == RT_EOK);
}
}
else
{
if (pdata->touch_cb.tp_touch_up)
{
ret = pdata->touch_cb.tp_touch_up(pdata);
RT_ASSERT(ret == RT_EOK);
}
}
pdata->touch_event = RT_TOUCH_EVENT_UP;
pdata->smooth_design = 0;
app_main_touch_value_reset();
break;
default:
break;
}
return RT_EOK;
}
static void app_main_touch_init(struct rt_touchpanel_block *block)
{
struct rt_device_graphic_info *info = &app_main_data->disp->info;
rt_memset(block, 0, sizeof(struct rt_touchpanel_block));
block->x = TOUCH_REGION_X + ((info->width - DISP_XRES) / 2);
block->y = TOUCH_REGION_Y + ((info->height - DISP_YRES) / 2);
block->w = TOUCH_REGION_W;
block->h = TOUCH_REGION_H;
block->name = "wearable";
#ifdef APP_WEARABLE_ANIM_AUTO_PLAY
block->callback = NULL;
#else
block->callback = app_main_touch_process;
#endif
}
void app_main_touch_value_reset(void)
{
struct app_main_data_t *pdata = app_main_data;
pdata->tb_flag = 0;
pdata->dir_mode = 0;
pdata->xdir = 0;
pdata->ydir = 0;
pdata->xoffset = 0;
pdata->yoffset = 0;
}
void app_main_touch_unregister(void)
{
struct app_main_data_t *pdata = app_main_data;
pdata->touch_cb.tp_touch_down = RT_NULL;
pdata->touch_cb.tp_move_lr_start = RT_NULL;
pdata->touch_cb.tp_move_updn_start = RT_NULL;
pdata->touch_cb.tp_move_lr = RT_NULL;
pdata->touch_cb.tp_move_updn = RT_NULL;
pdata->touch_cb.tp_move_up = RT_NULL;
pdata->touch_cb.tp_touch_up = RT_NULL;
}
void app_main_touch_register(struct app_touch_cb_t *tcb)
{
struct app_main_data_t *pdata = app_main_data;
pdata->touch_cb.tp_touch_down = tcb->tp_touch_down;
pdata->touch_cb.tp_move_lr_start = tcb->tp_move_lr_start;
pdata->touch_cb.tp_move_updn_start = tcb->tp_move_updn_start;
pdata->touch_cb.tp_move_lr = tcb->tp_move_lr;
pdata->touch_cb.tp_move_updn = tcb->tp_move_updn;
pdata->touch_cb.tp_move_up = tcb->tp_move_up;
pdata->touch_cb.tp_touch_up = tcb->tp_touch_up;
}
/*
**************************************************************************************************
*
* Design subroutines
*
**************************************************************************************************
*/
/**
* Design request.
*/
void app_design_request(rt_uint8_t urgent, design_cb_t *design, void *param)
{
register rt_base_t level;
int retry = 5;
rt_err_t ret;
struct app_main_data_t *pdata = app_main_data;
rt_slist_t *head = &pdata->design_list;
level = rt_hw_interrupt_disable();
if (urgent)
{
rt_slist_insert(head, &design->list);
}
else
{
rt_slist_append(head, &design->list);
}
rt_hw_interrupt_enable(level);
clock_app_mq_t mq = {MQ_DESIGN_UPDATE, param};
SEND_RETRY:
ret = rt_mq_send(pdata->mq, &mq, sizeof(clock_app_mq_t));
if ((ret == -RT_EFULL) && (retry > 0))
{
rt_kprintf("WARNING: Queue full %d\n", retry);
rt_thread_mdelay(10);
retry--;
goto SEND_RETRY;
}
RT_ASSERT(ret != -RT_ERROR);
}
/**
* Design task.
*/
static void app_main_design(struct app_main_data_t *pdata, void *param)
{
rt_err_t ret;
rt_slist_t *head = &pdata->design_list;
rt_slist_t *list = rt_slist_first(head);
register rt_base_t level;
if (list != RT_NULL)
{
level = rt_hw_interrupt_disable();
design_cb_t *design = rt_slist_entry(list, design_cb_t, list);
rt_slist_remove(head, list);
rt_hw_interrupt_enable(level);
ret = design->cb(param);
if (ret != RT_EOK)
{
rt_kprintf("Design failed %p %d\n", design->cb, ret);
}
}
}
/*
**************************************************************************************************
*
* Display refresh subroutines
*
**************************************************************************************************
*/
/**
* Refresh callback register.
*/
void app_refresh_register(rt_uint8_t winid, void *cb, void *param)
{
app_main_data->refr[winid].cb = cb;
app_main_data->refr[winid].param = param;
}
/**
* Refresh callback unregister.
*/
void app_refresh_unregister(rt_uint8_t winid)
{
app_main_data->refr[winid].cb = RT_NULL;
app_main_data->refr[winid].param = RT_NULL;
}
/**
* Get refresh callback.
*/
void app_get_refresh_callback(rt_uint8_t winid, void **cb, void **param)
{
*cb = app_main_data->refr[winid].cb;
*param = app_main_data->refr[winid].param;
}
/**
* Refresh callback request.
*/
void app_refresh_request(void *param)
{
clock_app_mq_t mq = {MQ_REFR_UPDATE, param};
rt_err_t ret = rt_mq_send(app_main_data->mq, &mq, sizeof(clock_app_mq_t));
RT_ASSERT(ret != -RT_ERROR);
}
/**
* app clock display refresh request: request send data to LCD pannel.
*/
void app_registe_refresh_done_cb(rt_err_t (*cb)(void *param), void *param)
{
g_refr_cb.cb = cb;
g_refr_cb.param = param;
g_refr_cb.timeout = 0;
}
static rt_err_t app_main_refresh_done(void)
{
if (g_refr_cb.cb)
{
g_refr_cb.cb(g_refr_cb.param);
app_registe_refresh_done_cb(NULL, NULL);
}
return (rt_event_send(app_main_data->event, EVENT_REFR_DONE));
}
rt_err_t app_main_refresh_request(struct rt_display_mq_t *disp_mq, rt_int32_t wait)
{
rt_err_t ret;
//wait refresh done
rt_uint32_t event;
ret = rt_event_recv(app_main_data->event, EVENT_REFR_DONE,
RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
RT_WAITING_FOREVER, &event);
if (ret != RT_EOK)
{
return ret;
}
//request refresh display data to Pannel
disp_mq->disp_finish = app_main_refresh_done;
ret = rt_mq_send(app_main_data->disp->disp_mq, disp_mq, sizeof(struct rt_display_mq_t));
RT_ASSERT(ret != -RT_ERROR);
if (ret != RT_EOK)
{
rt_kprintf("mq send failed %d\n", ret);
return RT_EOK;
}
return RT_EOK;
}
/**
* app clock display task.
*/
static rt_err_t app_main_refresh(struct app_main_data_t *pdata, void *param)
{
page_refrsh_request_param_t *par = (page_refrsh_request_param_t *)param;
return app_page_refresh(par->page, par->page_num, par->auto_resize);
}
void app_asr_cb(void)
{
rt_kprintf("Xiao Rui Wakeup\n");
app_asr_set_callback(NULL);
app_play_notice(NOTICE_IAMHERE);
}
void app_play_cb(void)
{
app_asr_set_callback(app_asr_cb);
}
/*
**************************************************************************************************
*
* app init & thread
*
**************************************************************************************************
*/
void app_enter_page(struct app_page_data_t *page)
{
app_cur_page = page;
app_page_refresh(page, 1, 0);
}
void app_update_page(struct app_page_data_t *page)
{
app_cur_page = page;
}
void app_wake_up(void)
{
#if APP_SUSPEND_RESUME_ENABLE
if (app_main_data->pm_status == APP_PM_SUSPEND)
{
rt_pm_request(1);
app_main_data->pm_status = APP_PM_RESUME;
#if APP_WDT_ENABLE
rt_device_control(app_main_data->wdt_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
#endif
rk_imagelib_init();
rt_pin_write(TOUCH_RST_PIN, PIN_HIGH);
}
#endif
clock_app_mq_t mq;
mq.cmd = MQ_BACKLIGHT_SWITCH;
mq.param = (void *)(uint32_t)app_main_data->bl;
rt_err_t ret = rt_mq_send(app_main_data->mq, &mq, sizeof(clock_app_mq_t));
RT_ASSERT(ret != -RT_ERROR);
}
#ifdef RT_USING_BMI270
extern rt_err_t bmi270_isr_sethook(void (*hook)(void));
void app_bmi_isr(void)
{
if (app_main_data->bl_en == 0)
{
app_wake_up();
}
}
#endif
void app_main_set_panel_bl(void *param)
{
uint16_t en = !!(uint16_t)(uint32_t)param;
uint16_t bl;
rt_err_t ret;
if (app_main_data->bl_en == en)
return;
if (en)
{
bl = app_main_data->bl;
app_main_get_time(&app_main_data->tmr_data);
app_main_page_clock_design(NULL);
}
else
bl = 0;
ret = rt_display_backlight_set(bl);
if (ret == RT_EOK)
{
app_main_data->bl_en = en;
if ((en == 1) && (app_dialog_check() == RT_FALSE))
app_enter_page(app_cur_page);
#if APP_TIMING_LIGHTOFF
if (app_main_data->bl_time)
{
if (app_main_data->bl_en == 1)
rt_timer_start(app_main_data->bl_timer);
else
rt_timer_stop(app_main_data->bl_timer);
}
#endif
}
}
void app_bl_switch(void)
{
clock_app_mq_t mq;
mq.cmd = MQ_BACKLIGHT_SWITCH;
mq.param = (void *)(app_main_data->bl_en == 1 ? 0 : app_main_data->bl);
rt_err_t ret = rt_mq_send(app_main_data->mq, &mq, sizeof(clock_app_mq_t));
RT_ASSERT(ret != -RT_ERROR);
}
MSH_CMD_EXPORT_ALIAS(app_bl_switch, switch, switch panel backlight);
#ifdef POWER_KEY_PIN
rt_err_t app_reboot_loader(void *param)
{
if (rt_pin_read(POWER_KEY_BANK_PIN) == PIN_HIGH)
{
BSP_SetLoaderFlag();
rt_hw_cpu_reset();
}
return RT_EOK;
}
static uint32_t last_int = 0;
static void app_gpio_isr(void *arg)
{
if ((HAL_GetTick() - last_int) < 300)
return;
last_int = HAL_GetTick();
if (rt_pin_read(POWER_KEY_BANK_PIN) == PIN_HIGH)
{
if (app_main_data->bl_en == 0)
{
app_wake_up();
}
else
{
app_main_keep_screen_on();
}
app_main_register_timeout_cb(app_reboot_loader, NULL, 3000);
}
else
{
if (app_main_data->pm_status != APP_PM_SUSPEND)
app_main_unregister_timeout_cb_if_is(app_reboot_loader);
}
}
#endif
#if APP_TIMING_LIGHTOFF
static void app_main_backlight_timer(void *arg)
{
if (app_main_data->bl_en != 0)
{
clock_app_mq_t mq = {MQ_BACKLIGHT_SWITCH, (void *)0};
rt_err_t ret = rt_mq_send(app_main_data->mq, &mq, sizeof(clock_app_mq_t));
RT_ASSERT(ret != -RT_ERROR);
}
}
#endif
void app_main_set_bl_timeout(uint32_t set_time)
{
app_main_data->bl_time = set_time;
save_app_info(app_main_data);
if (app_main_data->bl_timer)
{
if (set_time)
{
set_time *= RT_TICK_PER_SECOND;
rt_timer_control(app_main_data->bl_timer, RT_TIMER_CTRL_SET_TIME, &set_time);
rt_timer_control(app_main_data->bl_timer, RT_TIMER_CTRL_GET_TIME, &set_time);
rt_timer_start(app_main_data->bl_timer);
rt_kprintf("Set bl timeout %dms", set_time);
}
else
{
rt_timer_stop(app_main_data->bl_timer);
}
}
}
void set_bl(int argc, char *argv[])
{
uint8_t bl = 0xFF;
int to = -1;
if (argc < 3)
return;
argc--;
argv++;
while (argc)
{
if (*argv && (strcmp(*argv, "-t") == 0))
{
argc--;
argv++;
if (*argv)
to = atoi(*argv);
}
else if (argv && (strcmp(*argv, "-b") == 0))
{
argc--;
argv++;
if (*argv)
bl = (uint8_t)atoi(*argv);
}
argc--;
argv++;
}
if (bl != 0xFF)
{
if (bl > 100)
bl = 100;
rt_kprintf("Set backlight level %d\n", bl);
app_main_data->bl = bl;
app_main_set_panel_bl((void *)1);
rt_display_backlight_set(bl);
}
if (to != -1)
{
rt_kprintf("Set timeout %d\n", to);
app_main_set_bl_timeout(to);
}
}
MSH_CMD_EXPORT(set_bl, set backlight);
static void app_main_thread(void *p)
{
rt_err_t ret;
clock_app_mq_t mq;
struct app_main_data_t *pdata;
//test ...
// rt_thread_mdelay(3000);
#if (APP_PSRAM_END_RESERVE > 0)
g_app_info = (struct app_info *)((uint32_t)xPsramBase + (uint32_t)xPsramSize - RT_PSRAM_END_RESERVE);
if (g_app_info->magic == APP_MAGIC_SUM)
{
g_app_info->cold_boot = 0;
}
else
{
g_app_info->cold_boot = 1;
}