|
【HarmonyOS HiSpark Wi-Fi IoT 套件試用連載】感應燈Demo, 分享一個比較簡單的Demo
硬件模塊:
HiSpark_WiFi_IoT_3861_CH340G_主板
HiSpark_WiFi_IoT_智能(炫彩)燈板
實現(xiàn)功能:
1.模擬紅外人體感應燈,人到亮起紅燈,人走紅燈滅。
2.光敏電阻,暗環(huán)境亮綠燈,亮環(huán)境滅燈。
代碼分享如下
1.在 applications\sample\wifi-iot\app\BUILD.gn 修改如下
- import(“//build/lite/config/component/lite_component.gni“)
- lite_component(“app“) {
- features = [
- “startup“,
- “sensor_light:sensor_light“,
-
- ]
- }
復制代碼
2.新建applications\sample\wifi-iot\app\sensor_light 目錄并添加以下兩個文件
applications\sample\wifi-iot\app\sensor_light\BUILD.gn
applications\sample\wifi-iot\app\sensor_light\sensor_light.c
3.applications\sample\wifi-iot\app\sensor_light\BUILD.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.applications\sample\wifi-iot\app\sensor_light\sensor_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 =%d\n“,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 =%d\n“,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);
復制代碼 |
|