1 #include "lightmeter.h"
2 #include "drv_als_ps_ir_liteon_ap3216c.h"
3 #include "aos/kernel.h"
4 #include <stdio.h>
5 #include <stdlib.h>
6 
7 MENU_COVER_TYP lightmeter_cover      = {MENU_COVER_NONE};
8 MENU_TASK_TYP  lightmeter_tasks      = {lightmeter_init, lightmeter_uninit};
9 MENU_LIST_TYP  lightmeter_child_list = {NULL, 0};
10 MENU_TYP lightmeter = {"lightmeter", &lightmeter_cover, &lightmeter_tasks, NULL,
11                        &lightmeter_child_list};
12 
13 static aos_task_t lightmeter_task_handle;
14 
15 static int running = 1;
16 
lightmeter_init(void)17 int lightmeter_init(void)
18 {
19     LOGI(EDU_TAG, "lightmeter_init begin\n");
20     ap3216c_init();
21     LOGI(EDU_TAG, "lightmeter_init done\n");
22 
23     OLED_Clear();
24     OLED_Refresh_GRAM();
25     aos_task_new_ext(&lightmeter_task_handle, "lightmeter_task", lightmeter_task, NULL, 2048, AOS_DEFAULT_APP_PRI);
26     LOGI(EDU_TAG, "aos_task_new lightmeter_task\n");
27     return 0;
28 }
29 
lightmeter_task(void)30 void lightmeter_task(void)
31 {
32     uint16_t tmp[3];
33     uint8_t  als[20];
34     uint8_t  ps[20];
35     uint8_t  ir[20];
36 
37     while (running) {
38         tmp[0] = ap3216c_read_ambient_light();
39         tmp[1] = ap3216c_read_ir_data();
40         tmp[2] = ap3216c_read_ps_data();
41 
42         sprintf(als, "ALS: %d", tmp[0]);
43         sprintf(ir, "IR : %d", tmp[1]);
44 
45         OLED_Clear();
46         OLED_Icon_Draw(20, 14, &icon_lighter_32_32, 0);
47         OLED_Show_String(64, 6, als, 12, 1);
48         OLED_Show_String(64, 20, ir, 12, 1);
49 
50         if ((tmp[2] >> 15) & 1)
51             OLED_Show_String(64, 36, "near !", 16, 1);
52         else
53             OLED_Show_String(64, 40, "far !", 16, 1);
54 
55         OLED_Icon_Draw(2, 24, &icon_skip_left, 0);
56         OLED_Icon_Draw(122, 24, &icon_skip_right, 0);
57         OLED_Refresh_GRAM();
58         /* wait for 112.5ms at least according to ap3216c's datasheet */
59         aos_msleep(150);
60     }
61     running = 1;
62 }
63 
lightmeter_uninit(void)64 int lightmeter_uninit(void)
65 {
66     running = 0;
67 
68     while (!running) {
69         aos_msleep(50);
70     }
71     ap3216c_deinit();
72     aos_task_delete(&lightmeter_task_handle);
73     LOGI(EDU_TAG, "aos_task_delete lightmeter_task\n");
74     return 0;
75 }
76