使用离线的IDE开发能够更灵活。本次实现了温湿度、光强、空气质量、电量和充电状态的采集,并通过屏幕显示出来。
在提供的模板例程中使用了操作系统。操作系统使用的是Freertos,但是其在rtos上又构建了一个抽象层。其入口是在
- void main_task(uint32_t param)
这个函数中。因此我也在这函数中新建了两个任务,获取数据的任务与显示任务。
- OSA_TASK_DEFINE(get_data_task, OSA_PRIORITY_NORMAL, 1, 1600, 0) ;
- OSA_TASK_DEFINE(display_task, OSA_PRIORITY_LOW, 1, 1600, 0) ;
获取数据的任务是没100ms获取一次各数据。代码没有进行优化,写的有点乱,大家先参考一下。
获取温湿度的代码:
- void get_temp_and_humity(int32_t *temp, int32_t *humity)
- {
- ens210_meas_data_t data;
- ENS210_Measure(mode_TH,&data);
- *temp = data.T_Celsius;
- *humity = data.H_Percent;
- }
获取空气质量,也就是获取CO2浓度的代码:
- void get_co2(int32_t *air)
- {
- uint8_t ready;
- uint16_t uCO2;
- CCS811_dataAvailable(&ready);
- if (ready == 1) {
- CCS811_readAlgorithmResults();
- uCO2 = CCS811_getCO2();
- *air = uCO2;
- }
- }
获取光强:
- void get_light(int32_t *light)
- {
- float temp_light = 0;
- TSL2572_ReadAmbientLight(&temp_light);
- TSL2572_ClearALSInterrupt();
- *light = (int32_t) temp_light;
- }
获取电量百分比与充电状态:
- void get_battery(int32_t *power_state,char *charge_state_string)
- {
- uint8_t bat_level,bat_state;
- BatterySensor_GetState(&bat_level, &bat_state);
- if(bat_state == 0){
- sprintf(charge_state_string," ");
- }else{
- sprintf(charge_state_string,"! ");
- }
- *power_state = bat_level;
- }
获取数据任务的代码:
- void get_data_task(uint32_t param)
- {
- while(1){
- get_temp_and_humity(&temp,&humity);
- get_light(&light);
- get_co2(&air);
- get_battery(&power,charge_state);
- OSA_TimeDelay(100);
- }
- }
显示任务的代码:
- int32_t temp,humity,light,air,power;
- char charge_state[10];
- void display_task(uint32_t param)
- {
- char dis_string[20];
- while(1){
- //GUI_Clear();
- GUI_GotoXY(0,0);
- GUI_DispString("temperature is: \n");
- sprintf(dis_string," %d \n",temp);
- GUI_DispString(dis_string);
- GUI_DispString("humity is: \n");
- sprintf(dis_string," %d \n",humity);
- GUI_DispString(dis_string);
- GUI_DispString("light is: \n");
- sprintf(dis_string," %d \n",light);
- GUI_DispString(dis_string);
- GUI_DispString("air is: \n");
- sprintf(dis_string," %d \n",air);
- GUI_DispString(dis_string);
- GUI_DispString("power is: \n");
- sprintf(dis_string," % 3d, %s\n",power,charge_state);
- GUI_DispString(dis_string);
- OSA_TimeDelay(100);
- }
- }
此内容由EEWORLD论坛网友manhuami2007原创,如需转载或用于商业用途需征得作者同意并注明出处