1 #include "homepage.h"
2 #include "../menu.h"
3 #include "aos/kernel.h"
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <time.h>
7 #include "posix/timer.h"
8 #include "../../build_version.h"
9 #include "netmgr.h"
10 #include "aos/vfs.h"
11 
12 #include <drivers/u_ld.h>
13 #include <vfsdev/adc_dev.h>
14 #include <drivers/char/u_device.h>
15 
16 static int battery_level = 0;
17 extern int bt_connected;
18 extern int ip_got_finished;
19 extern char eduk1_ip_addr[IPADDR_STR_LEN];
20 
21 MENU_COVER_TYP homepage_cover = {MENU_COVER_NONE};
22 MENU_TASK_TYP  homepage_tasks = {homepage_init, homepage_uninit};
23 MENU_TYP homepage = {"homepage", &homepage_cover, &homepage_tasks, NULL, NULL};
24 
25 static aos_task_t homepage_task_handle;
26 
homepage_init(void)27 int homepage_init(void)
28 {
29     OLED_Clear();
30     OLED_Refresh_GRAM();
31     aos_task_new_ext(&homepage_task_handle, "homepage_task", homepage_task, NULL, 1024, AOS_DEFAULT_APP_PRI);
32     LOGI(EDU_TAG, "aos_task_new homepage_task\n");
33     return 0;
34 }
35 
get_battery(int * level,uint32_t * volage)36 static int get_battery(int *level, uint32_t *volage)
37 {
38     int32_t  ret       = 0;
39     uint32_t test_sum  = 0;
40     uint32_t test_avrg = 0;
41     uint32_t test_min  = 3300;
42     uint32_t test_max  = 0;
43     int32_t  fd        = 0;
44     int32_t index      = 1;
45     int sampling_cycle = 100;
46     char name[16] = {0};
47     io_adc_arg_t adc_arg;
48 
49     snprintf(name, sizeof(name), "/dev/adc%d", index);
50     fd = open(name, 0);
51 
52     if (fd >= 0) {
53         ret = ioctl(fd, IOC_ADC_START, sampling_cycle);
54         usleep(1000);
55         adc_arg.value = 0;
56         adc_arg.timeout = 500000; // in unit of us
57 
58         for (int32_t i = 0; i < 10; i++) {
59             ret = ioctl(fd, IOC_ADC_GET_VALUE, (unsigned long)&adc_arg);
60             test_sum += adc_arg.value;
61 
62             /* the min sampling voltage */
63             if (test_min >= adc_arg.value) {
64                 test_min = adc_arg.value;
65             }
66             /* the max sampling voltage */
67             if (test_max <= adc_arg.value) {
68                 test_max = adc_arg.value;
69             }
70         }
71         usleep(1000);
72         ret = ioctl(fd, IOC_ADC_STOP, 0);
73         close(fd);
74 
75         test_avrg = (test_sum - test_min - test_max) >> 3;
76         LOGD(EDU_TAG, "the samping volage is:%dmv\n", test_avrg);
77         test_avrg *= 3.208;
78         *volage = test_avrg;
79 
80         if (test_avrg > 4100) {
81             *level = 4;
82         } else if ((test_avrg > 3980) && (test_avrg < 4100)) {
83             *level = 3;
84         } else if ((test_avrg > 3850) && (test_avrg < 3980)) {
85             *level = 2;
86         } else if ((test_avrg > 3700) && (test_avrg < 3850)) {
87             *level = 1;
88         } else if (test_avrg < 3700) {
89             *level = 0;
90         }
91     }
92     return 0;
93 
94 }
95 
homepage_task(void)96 void homepage_task(void)
97 {
98     unsigned char   c = 0;
99     struct tm *info;
100     struct timespec tv;
101     uint8_t         image_version[22];
102     uint8_t         tmp[22];
103     netmgr_ifconfig_info_t ifconfig;
104     netmgr_hdl_t hdl;
105     uint32_t volage;
106     int ret = 0;
107 
108     while (1) {
109         OLED_Clear();
110         /* 获取 GMT 时间 */
111         clock_gettime(CLOCK_REALTIME, &tv);
112         info = gmtime(&tv);
113 
114         snprintf(tmp, 20, "%2d:%02d", (info->tm_hour + 8) % 24, info->tm_min);
115         OLED_Show_String(2, 12 * 0, tmp, 12, 1);
116         if (ip_got_finished)
117         {
118             OLED_Icon_Draw(86, 0, &icon_wifi_on_12_12, 0);
119             snprintf(image_version, 20, "IP: %s", eduk1_ip_addr);
120             OLED_Show_String(20, (12 + 4) * 3, image_version, 12, 1);
121         }else
122         {
123             //snprintf(image_version, 20, "IP: ?.?.?.?", ifconfig.ip_addr);
124             //OLED_Show_String(20, (12 + 4) * 3, image_version, 12, 1);
125         }
126 
127         if (bt_connected)
128         {
129             OLED_Icon_Draw(97, 0, &icon_bt_on_12_12, 0);
130         }
131 
132         OLED_Show_String(40, (12 + 4) * 1, "HaaS EDU", 12, 1);
133         snprintf(image_version, 21, "VER: %s", BUILD_VERSION);
134         OLED_Show_String(33, (12 + 4) * 2, image_version, 12, 1);
135 
136         OLED_Icon_Draw(2, 24, &icon_skip_left, 0);
137         OLED_Icon_Draw(122, 24, &icon_skip_right, 0);
138 
139         if (0 == get_battery(&battery_level, &volage)) {
140             LOGD(EDU_TAG, "get_battery success:%d mv,level :%d \n", volage, battery_level);
141             OLED_Icon_Draw(110, 0, &icon_battery_20_12[battery_level], 0);
142         } else {
143             LOGE(EDU_TAG, "get_battery fail\n");
144         }
145 
146         OLED_Refresh_GRAM();
147         aos_msleep(1000);
148     }
149 }
150 
homepage_uninit(void)151 int homepage_uninit(void)
152 {
153     aos_task_delete(&homepage_task_handle);
154     LOGI(EDU_TAG, "aos_task_delete homepage_task\n");
155     return 0;
156 }
157