分享一个比较简单的Demo
硬件模块:
HiSpark_WiFi_IoT_3861_CH340G_主板
HiSpark_WiFi_IoT_智能(炫彩)灯板
实现功能:
1.模拟红外人体感应灯,人到亮起红灯,人走红灯灭。
2.光敏电阻,暗环境亮绿灯,亮环境灭灯。
代码分享如下
1.在 applicationssamplewifi-iotappBUILD.gn 修改如下
- import("//build/lite/config/component/lite_component.gni")
- lite_component("app") {
- features = [
- "startup",
- "sensor_light:sensor_light",
-
- ]
- }
复制代码
2.新建applicationssamplewifi-iotappsensor_light 目录并添加以下两个文件
applicationssamplewifi-iotappsensor_lightBUILD.gn
applicationssamplewifi-iotappsensor_lightsensor_light.c
3.applicationssamplewifi-iotappsensor_lightBUILD.gn 修改如下
- static_library("sensor_light") {
- sources = [
- "sensor_light.c"
- ]
- include_dirs = [
- "//utils/native/lite/include",
- "//kernel/liteos_m/components/cmsis/2.0",
- "//base/iot_hardware/interfaces/kits/wifiiot_lite",
- ]
- }
复制代码
4.applicationssamplewifi-iotappsensor_lightsensor_light.c 修改如下
- #include <stdio.h>
- #include <unistd.h>
- #include "ohos_init.h"
- #include "cmsis_os2.h"
- #include "wifiiot_gpio.h"
- #include "wifiiot_gpio_ex.h"
- #include "wifiiot_adc.h"
- #include "wifiiot_errno.h"
- static void SensorLightTask(void *arg)
- {
- (void)arg;
- //人体感应ADC 值
- unsigned short humandata = 0;
- //光敏电阻ADC值
- unsigned short lightdata = 0;
- while(1){
- if (AdcRead(WIFI_IOT_ADC_CHANNEL_3, &humandata, WIFI_IOT_ADC_EQU_MODEL_4, WIFI_IOT_ADC_CUR_BAIS_DEFAULT, 0)
- == WIFI_IOT_SUCCESS) {
- printf("[SensorLight] humandata =%dn",humandata);
- }
- if (AdcRead(WIFI_IOT_ADC_CHANNEL_4, &lightdata, WIFI_IOT_ADC_EQU_MODEL_4, WIFI_IOT_ADC_CUR_BAIS_DEFAULT, 0)
- == WIFI_IOT_SUCCESS) {
- printf("[SensorLight] lightdata =%dn",lightdata);
- }
- //从串口log中来看ADC值感应到是18xx,没有感应到是12x,所以这里用500做一个阈值
- if(humandata > 500){
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_GPIO_VALUE1);//R on
- }else{
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_GPIO_VALUE0);//R off
- }
- if(lightdata > 500){
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_GPIO_VALUE1);//G on
- }else{
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_GPIO_VALUE0);//G off
- }
- usleep(10000);
- }
- }
- static void SensorLight(void)
- {
- osThreadAttr_t attr;
- GpioInit();
- //RLED init
- IoSetFunc(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_IO_FUNC_GPIO_10_GPIO);
- GpioSetDir(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_GPIO_DIR_OUT);
- //GLED init
- IoSetFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_FUNC_GPIO_11_GPIO);
- GpioSetDir(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_GPIO_DIR_OUT);
- //BLED init
- IoSetFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_FUNC_GPIO_12_GPIO);
- GpioSetDir(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_GPIO_DIR_OUT);
- attr.name = "SensorLightTask";
- attr.attr_bits = 0U;
- attr.cb_mem = NULL;
- attr.cb_size = 0U;
- attr.stack_mem = NULL;
- attr.stack_size = 4096;
- attr.priority = osPriorityNormal;
- if (osThreadNew(SensorLightTask, NULL, &attr) == NULL) {
- printf("[SensorLight] Falied to create SensorLightTask!n");
- }
- }
- SYS_RUN(SensorLight);
复制代码