1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2011-02-24     Bernard      the first version
9  */
10 
11 /**
12  * @addtogroup FM3
13  */
14 
15 /*@{*/
16 
17 #include <rtthread.h>
18 #include "board.h"
19 #include "led.h"
20 #include "key.h"
21 #include "adc.h"
22 #include "lcd.h"
23 #include "cpuusage.h"
24 
25 #ifdef RT_USING_RTGUI
26 #include <rtgui/rtgui.h>
27 #include <rtgui/driver.h>
28 extern void rtgui_startup();
29 #endif
30 
31 struct rt_messagequeue mq;
32 static char msg_pool[2048];
33 
rt_init_thread_entry(void * parameter)34 void rt_init_thread_entry(void *parameter)
35 {
36     rt_device_t lcd;
37 
38     rt_hw_led_init();
39     rt_hw_key_init();
40     rt_hw_adc_init();
41     rt_hw_lcd_init();
42     rt_hw_cpu_init();
43 
44 #ifdef RT_USING_RTGUI
45     extern void rtgui_system_server_init(void);
46 
47     /* find lcd device */
48     lcd = rt_device_find("lcd");
49 
50     /* set lcd device as rtgui graphic driver */
51     rtgui_graphic_set_device(lcd);
52 
53     /* init rtgui system server */
54     rtgui_system_server_init();
55 
56     /* startup rtgui */
57     rtgui_startup();
58 #else
59     {
60     char buf[20] = {'\0'};
61     struct lcd_msg msg;
62     rt_device_t device;
63     device = rt_device_find("lcd");
64     rt_device_control(device, RT_DEVICE_CTRL_LCD_CLEAR_SCR, RT_NULL);
65     x = 1;
66     y = 1;
67     rt_device_control(device, RT_DEVICE_CTRL_LCD_PUT_STRING, "ADC");
68     x = 1;
69     y = 20;
70     rt_device_control(device, RT_DEVICE_CTRL_LCD_PUT_STRING, "CPU");
71     x = 1;
72     y = 40;
73     rt_device_control(device, RT_DEVICE_CTRL_LCD_PUT_STRING, "KEY");
74 
75     while(1)
76     {
77         if (rt_mq_recv(&mq, &msg, sizeof(msg), RT_WAITING_FOREVER) >= 0)
78         {
79             switch(msg.type)
80             {
81                 case ADC_MSG:
82                     x = 40;
83                     y = 1;
84                     rt_memset(buf, 0, sizeof(buf));
85                     rt_sprintf(buf, "%04d", msg.adc_value);
86                     rt_device_control(device, RT_DEVICE_CTRL_LCD_PUT_STRING, buf);
87                     break;
88                 case CPU_MSG:
89                     x = 40;
90                     y = 20;
91                     rt_memset(buf, 0, sizeof(buf));
92                     rt_sprintf(buf, "%03d %03d", msg.major, msg.minor);
93                     rt_device_control(device, RT_DEVICE_CTRL_LCD_PUT_STRING, buf);
94                     break;
95                 case KEY_MSG:
96                     x = 40;
97                     y = 40;
98                     rt_memset(buf, 0, sizeof(buf));
99                     switch(msg.key)
100                     {
101                         case KEY_DOWN:
102                             rt_sprintf(buf, "DOWN KEY ");
103                             break;
104                         case KEY_UP:
105                             rt_sprintf(buf, "UP KEY   ");
106                             break;
107                         case KEY_RIGHT:
108                             rt_sprintf(buf, "RIGHT KEY");
109                             break;
110                         case KEY_LEFT:
111                             rt_sprintf(buf, "LEFT KEY ");
112                             break;
113                         case KEY_ENTER:
114                             rt_sprintf(buf, "ENTER KEY");
115                             break;
116                         default:
117                             rt_sprintf(buf, "NO KEY   ");
118                             break;
119                     }
120                     rt_device_control(device, RT_DEVICE_CTRL_LCD_PUT_STRING, buf);
121                     break;
122             }
123         }
124     }
125     }
126 #endif
127 }
128 
rt_application_init(void)129 int rt_application_init(void)
130 {
131     rt_thread_t init_thread;
132 
133     rt_mq_init(&mq, "mqt", &msg_pool[0], 128 - sizeof(void*), sizeof(msg_pool), RT_IPC_FLAG_FIFO);
134 
135     init_thread = rt_thread_create("init", rt_init_thread_entry, RT_NULL, 1024, 21, 20);
136     if(init_thread != RT_NULL)
137         rt_thread_startup(init_thread);
138 
139     return 0;
140 }
141 
142 /*@}*/
143