fix i2c error, add senser interface.

This commit is contained in:
TXuian
2022-09-01 02:52:44 -07:00
parent 0a629fc69d
commit c014727e2c
34 changed files with 3290 additions and 133 deletions
@@ -1,3 +1,4 @@
SRC_FILES := lv_init.c lv_demo.c lv_demo_calendar.c
SRC_FILES += lv_sensor_info.c lv_sensor_update_info.c
include $(KERNEL_ROOT)/compiler.mk
@@ -0,0 +1,31 @@
## lv_sensor_update_info
用于在触摸屏上显示传感器的各项数据
更新显示数据可在线程中使用`bool sensor_update_val(double val, enum sensor_type st)`(保障线程安全)
- `val`: 传感器数值;
- `st`:检测值类型(如氧气、臭氧);
- `st`类型为`enum sensor_type`,该枚举类型在lv_sensor_info.h中给出;
- 该函数的使用方法可参考lv_sensor_info_update_demo.c
- 请勿直接修改lvgl table
``` C
enum sensor_type {
O3 = 0, // 臭氧
CO2, // 二氧化碳
SO2, // 二氧化硫
NO2, // 二氧化氮
N2, // 氨气
TVOC,
FORMALDEHYDE, // 甲醛
ALCOHOL, // 乙醇
METHANE, // 甲烷
O2, // 氧气
AQS,
PM, // PM1.0/2.5
TEMPERATURE, // 温度
HUMIDITY, // 湿度
WIND_SPEED, // 风速
WIND_DIRECTION, //风向
PRESURE, // 压力
NOISE // 噪音
};
```
+8 -7
View File
@@ -3,19 +3,20 @@
#include "lv_demo_calendar.h"
#include <transform.h>
extern void lv_example_chart_2(void);
extern void lv_example_img_1(void);
extern void lv_example_img_2(void);
extern void lv_example_img_3(void);
extern void lv_example_img_4(void);
extern void lv_example_line_1(void);
extern void lv_example_aoteman(void);
// extern void lv_example_chart_2(void);
// extern void lv_example_img_1(void);
// extern void lv_example_img_2(void);
// extern void lv_example_img_3(void);
// extern void lv_example_img_4(void);
// extern void lv_example_line_1(void);
// extern void lv_example_aoteman(void);
void* lvgl_thread(void *parameter)
{
/* display demo; you may replace with your LVGL application at here */
lv_demo_calendar();
// lv_example_img_1();
// lv_example_chart_2();
// lv_example_table_1();
// lv_example_line_1();
// lv_example_aoteman();
/* handle the tasks of LVGL */
@@ -0,0 +1,126 @@
#include "lv_sensor_info.h"
static void draw_part_event_cb(lv_event_t* e) {
lv_obj_t* obj = lv_event_get_target(e);
lv_obj_draw_part_dsc_t* dsc = lv_event_get_param(e);
/*If the cells are drawn...*/
if(dsc->part == LV_PART_ITEMS) {
uint32_t row = dsc->id / lv_table_get_col_cnt(obj);
uint32_t col = dsc->id - row * lv_table_get_col_cnt(obj);
/*Make the texts in the first cell center aligned*/
if(row == 0) {
dsc->label_dsc->align = LV_TEXT_ALIGN_CENTER;
dsc->rect_dsc->bg_color = lv_color_mix(lv_palette_main(LV_PALETTE_BLUE), dsc->rect_dsc->bg_color, LV_OPA_20);
dsc->rect_dsc->bg_opa = LV_OPA_COVER;
}
/*In the first column align the texts to the right*/
else if(col == 0) {
dsc->label_dsc->flag = LV_TEXT_ALIGN_CENTER;
}
/*Make every 2nd row grayish*/
if((row != 0 && row % 2) == 0) {
dsc->rect_dsc->bg_color = lv_color_mix(lv_palette_main(LV_PALETTE_GREY), dsc->rect_dsc->bg_color, LV_OPA_10);
dsc->rect_dsc->bg_opa = LV_OPA_COVER;
}
}
}
char* Double2Str(char* buf, double value) {
sprintf(buf,"%.8f",value);//保留8位小数,不够补0
int index = 0;
int len = strlen(buf);
for(int i = len-1;i>0;i--)
{
if(buf[i] == '0')
continue;
else {
if (buf[i] == '.') index = i;
else index = i + 1;
break;
}
}
buf[index] = '\0';
return buf;
}
lv_obj_t* lv_ssr_tb;
void lv_sensor_info(void) {
lv_ssr_tb = lv_table_create(lv_scr_act());
// lv_obj_remove_style(lv_ssr_tb, NULL, LV_PART_ITEMS | LV_STATE_PRESSED);
for (uint32_t i = 0; i < NR_VAL_PERLINE; ++i) {
lv_table_set_cell_value(lv_ssr_tb, 0, 2 * i, "检测量");
lv_table_set_cell_value(lv_ssr_tb, 0, 2 * i + 1, "数值");
}
// fill name
uint32_t filled_pos = 0;
uint32_t cur_line_tmp = 0;
LV_FONT_DECLARE(lvgl_font_chinese);
while (filled_pos < nr_sensors) {
cur_line_tmp = 1 + (filled_pos / NR_VAL_PERLINE);
for (uint32_t i = 0; i < NR_VAL_PERLINE; ++i) {
if (filled_pos >= nr_sensors) { break; }
lv_table_set_cell_value(lv_ssr_tb, cur_line_tmp, 2 * i, sensor_names[filled_pos++]);
}
}
lv_obj_set_style_text_font(lv_ssr_tb, &lvgl_font_chinese, 0);
for (uint32_t i = 0; i < 2 * NR_VAL_PERLINE; ++i) {
if (i % 2 == 0) {
lv_table_set_col_width(lv_ssr_tb, i, 75);
}
else {
lv_table_set_col_width(lv_ssr_tb, i, 85);
}
}
// fill val
filled_pos = 0;
// init val
for (uint32_t i = 0; i < nr_sensors; ++i) { lv_sensor_vals[i] = 0; }
char buf[10];
snprintf(buf, 9, "%.1f", 0);
while (filled_pos < nr_sensors) {
for (uint32_t i = 0; i < NR_VAL_PERLINE; ++i) {
if (filled_pos >= nr_sensors) { break; }
lv_table_set_cell_value_fmt(lv_ssr_tb, 1 + (filled_pos / NR_VAL_PERLINE),
1 + 2 * i, "%s %s", buf, seneor_denominations[filled_pos++]);
}
}
lv_obj_set_size(lv_ssr_tb, 480, 272);
lv_obj_set_height(lv_ssr_tb, 272);
lv_obj_center(lv_ssr_tb);
/*Add an event callback to to apply some custom drawing*/
lv_obj_add_event_cb(lv_ssr_tb, draw_part_event_cb, LV_EVENT_DRAW_PART_BEGIN, NULL);
}
void* lvgl_thd_show_sensor_info(void *parameter)
{
lv_sensor_info();
PrivMutexCreate(&ssr_val_lock, 0);
while (1)
{
lv_task_handler();
sensor_update_table();
}
}
pthread_t lvgl_task;
static int lvgl_show_sensor_info(void)
{
pthread_attr_t attr;
attr.schedparam.sched_priority = 25;
attr.stacksize = 4096;
PrivTaskCreate(&lvgl_task, &attr, lvgl_thd_show_sensor_info, NULL);
return 0;
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0),lvgl_show_sensor_info, lvgl_show_sensor_info, lvgl_show_sensor_info );
@@ -0,0 +1,54 @@
#ifndef __LVGL_SENSOR_INFO_H__
#define __LVGL_SENSOR_INFO_H__
#include <lvgl.h>
#include <string.h>
#define nr_sensors 18
extern lv_obj_t* lv_ssr_tb;
pthread_mutex_t ssr_val_lock;
#define NR_VAL_PERLINE 3
static char* sensor_names[nr_sensors] = {
"臭氧", "二氧化碳", "二氧化硫", "二氧化氮", "氨气",
"TVOC", "甲醛", "乙醇", "甲烷", "氧气", "AQS", "PM1.0/2.5/10",
"温度", "湿度",
"风速", "风向", "气压", "噪音"
};
static char* seneor_denominations[nr_sensors] = {
"ppb", "ppm", "ppb", "ppb", "ppm",
"ppm", "ppm", "ppm", "%VOL", "%VOL", "ug/m³", "ug/m³",
"°C", "%RH", "m/s", "m/s", "mbar", "dB(A)"
};
static double lv_sensor_vals[nr_sensors];
enum sensor_type {
O3 = 0, // 臭氧
CO2, // 二氧化碳
SO2, // 二氧化硫
NO2, // 二氧化氮
N2, // 氨气
TVOC,
FORMALDEHYDE, // 甲醛
ALCOHOL, // 乙醇
METHANE, // 甲烷
O2, // 氧气
AQS,
PM, // PM1.0/2.5
TEMPERATURE, // 温度
HUMIDITY, // 湿度
WIND_SPEED, // 风速
WIND_DIRECTION, //风向
PRESURE, // 压力
NOISE // 噪音
};
void lv_sensor_info(void);
bool sensor_update_val(double, enum sensor_type);
char* Double2Str(char* buf, double value);
void sensor_update_table();
#endif
@@ -0,0 +1,32 @@
#include "lv_sensor_info.h"
void* lvgl_thd_sensor_info_update_demo(void *parameter)
{
double val = 0;
while (1)
{
sensor_update_val(val + 0.1, O3);
sensor_update_val(val, CO2);
sensor_update_val(val + 0.2, NO2);
sensor_update_val(val - 0.1, SO2);
sensor_update_val(val + 0.3, AQS);
sensor_update_val(val - 0.3, O2);
sensor_update_val(val + 0.3, TEMPERATURE);
val += 0.3;
PrivTaskDelay(10);
}
}
pthread_t lvgl_task;
static int lvgl_sensor_info_update_demo(void)
{
pthread_attr_t attr;
attr.schedparam.sched_priority = 25;
attr.stacksize = 4096;
PrivTaskCreate(&lvgl_task, &attr, lvgl_thd_sensor_info_update_demo, NULL);
return 0;
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0),lvgl_sensor_info_update_demo, lvgl_sensor_info_update_demo, lvgl_sensor_info_update_demo );
@@ -0,0 +1,57 @@
#include "lv_sensor_info.h"
uint32_t lv_ssr_map_idx(enum sensor_type st) {
uint32_t idx;
switch (st) {
case O3: {idx = 0; break;}
case CO2: {idx = 1; break;}
case SO2: {idx = 2; break;}
case NO2: {idx = 3; break;}
case N2: {idx = 4; break;}
case TVOC: {idx = 5; break;}
case FORMALDEHYDE: {idx = 6; break;}
case ALCOHOL: {idx = 7; break;}
case METHANE: {idx = 8; break;}
case O2: {idx = 9; break;}
case AQS: {idx = 10; break;}
case PM: {idx = 11; break;}
case TEMPERATURE: {idx = 12; break;}
case HUMIDITY: {idx = 13; break;}
case WIND_SPEED: {idx = 14; break;}
case WIND_DIRECTION: {idx = 15; break;}
case PRESURE: {idx = 16; break;}
case NOISE: {idx = 17; break;}
default: {idx = -1; break;}
}
return idx;
}
bool sensor_update_val(double val, enum sensor_type st) {
uint32_t idx = lv_ssr_map_idx(st);
if (idx >= nr_sensors || lv_ssr_tb == NULL) { return false; }
PrivMutexObtain(&ssr_val_lock);
lv_sensor_vals[idx] = val;
PrivMutexAbandon(&ssr_val_lock);
return true;
}
/**
*@brief update cell vals in lv_ssr_tb,
* note that this function can only be called in lv_ssr_tb control thread
*
*/
void sensor_update_table() {
uint32_t filled_pos = 0;
char buf[10] = { 0 };
PrivMutexObtain(&ssr_val_lock);
while (filled_pos < nr_sensors) {
for (uint32_t i = 0; i < NR_VAL_PERLINE; ++i) {
if (filled_pos >= nr_sensors) { break; }
snprintf(buf, 9, "%.1f", lv_sensor_vals[filled_pos]);
lv_table_set_cell_value_fmt(lv_ssr_tb, 1 + (filled_pos / NR_VAL_PERLINE),
1 + 2 * i, "%s %s", buf, seneor_denominations[filled_pos++]);
}
}
PrivMutexAbandon(&ssr_val_lock);
}
+8 -1
View File
@@ -15,6 +15,8 @@
// #include <user_api.h>
#include <transform.h>
extern int FrameworkInit();
extern void ApplicationOtaTaskInit(void);
int main(void)
@@ -24,7 +26,12 @@ int main(void)
#ifdef APPLICATION_OTA
ApplicationOtaTaskInit();
#endif
return 0;
// while (1) {
// ShowTask();
// ShowMemory();
// PrivTaskDelay(1500);
// }
return 0;
}
// int cppmain(void);