|
Hi3861 炫彩燈板(DCL)開發(fā), 一、前言
HarmonyOS HiSpark Wi-Fi IoT 套件開發(fā)之
炫彩燈板 二、場景描述 場景設(shè)定:樓道 三、功能描述 感應(yīng)照明燈,在光線比較暗的地方,感應(yīng)到人體就會打開照明燈 四、功能實現(xiàn) 首先,通過人體紅外感應(yīng)檢測人體運動 然后,通過光敏電阻檢測光線亮度 最后,若滿足有人且天黑的條件則亮燈 五、代碼實現(xiàn)
- /*
- * Copyright (c) 2020 Huawei Device Co., Ltd.
- * Licensed under the Apache License, Version 2.0 (the “License“);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an “AS IS“ BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /*
- * HiSpark_WiFi_IoT_DCL_VER.A板
- * GPIO_07 人體紅外感應(yīng)
- * GPIO_09 光敏電阻
- * GPIO_10 紅色LED,高電平點亮
- * GPIO_11 綠色LED,高電平點亮
- * GPIO_12 藍色LED,高電平點亮
- */
- #include <stdio.h>
- #include <unistd.h>
- #include “wifiiot_adc.h“
- #include “ohos_init.h“
- #include “cmsis_os2.h“
- #include “wifiiot_gpio.h“
- #include “wifiiot_gpio_ex.h“
- #define LED_INTERVAL_TIME_US 300000
- #define LED_TASK_STACK_SIZE 512
- #define LED_TASK_PRIO 25
- enum LedState {
- LED_ON = 0,
- LED_OFF,
- LED_SPARK,
- };
- enum LedState g_ledState = LED_OFF;
- static void *LedTask(const char *arg)
- {
- (void)arg;
- while (1) {
- WifiIotGpioValue rel=0;
- GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_7, &rel);
- unsigned short data = 0;
- AdcRead(WIFI_IOT_ADC_CHANNEL_4, &data, WIFI_IOT_ADC_EQU_MODEL_4, WIFI_IOT_ADC_CUR_BAIS_DEFAULT, 0);
- if(rel != 0 && data > 1500){//當感應(yīng)到人體活動且天色較暗時,三色LED會閃爍
- g_ledState = LED_SPARK;
- // printf(“GPIO_7=%d, GPIO_9=%d\n“,(int)rel,data);
- }
- else{
- g_ledState = LED_OFF;
- }
- switch (g_ledState) {
- case LED_ON:
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_10, 1);
- usleep(LED_INTERVAL_TIME_US);
- break;
- case LED_OFF:
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_10, 0);
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_11, 0);
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_12, 0);
- usleep(LED_INTERVAL_TIME_US);
- break;
- case LED_SPARK:
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_10, 0);
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_11, 1);
- usleep(LED_INTERVAL_TIME_US);
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_11, 0);
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_12, 1);
- usleep(LED_INTERVAL_TIME_US);
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_12, 0);
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_10, 1);
- usleep(LED_INTERVAL_TIME_US);
- break;
- default:
- usleep(LED_INTERVAL_TIME_US);
- break;
- }
- }
- return NULL;
- }
- static void LedExampleEntry(void)
- {
- osThreadAttr_t attr;
- GpioInit();
- //設(shè)置IO為輸入狀態(tài)
- IoSetFunc(WIFI_IOT_IO_NAME_GPIO_7, WIFI_IOT_IO_FUNC_GPIO_7_GPIO);
- GpioSetDir(WIFI_IOT_IO_NAME_GPIO_7, WIFI_IOT_GPIO_DIR_IN);
- IoSetPull(WIFI_IOT_IO_NAME_GPIO_7, WIFI_IOT_IO_PULL_UP);
- IoSetFunc(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_IO_FUNC_GPIO_9_GPIO);
- GpioSetDir(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_GPIO_DIR_IN);
- //設(shè)置紅色,綠色,藍色 LED IO為輸出狀態(tài)
- 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);
- 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);
- 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 = “LedTask“;
- attr.attr_bits = 0U;
- attr.cb_mem = NULL;
- attr.cb_size = 0U;
- attr.stack_mem = NULL;
- attr.stack_size = LED_TASK_STACK_SIZE;
- attr.priority = LED_TASK_PRIO;
- if (osThreadNew((osThreadFunc_t)LedTask, NULL, &attr) == NULL) {
- printf(“[LedExample] Falied to create LedTask!\n“);
- }
- }
- SYS_RUN(LedExampleEntry);
-
復(fù)制代碼 六、編譯
Screenshot from 2020-10-24 22-39-38.png (119.84 KB, 下載次數(shù): 0)
下載附件 保存到相冊
昨天 22:25 上傳 七、燒錄
Screenshot from 2020-10-24 22-41-03.png (50.65 KB, 下載次數(shù): 0)
下載附件 保存到相冊
昨天 22:25 上傳 八、運行
WeChat_20201030205535.gif (914.79 KB, 下載次數(shù): 0)
下載附件 保存到相冊
昨天 22:26 上傳 九、總結(jié) 在編程過程中需要參考API文檔,了解每個函數(shù)的定義,還需要參考硬件電路圖,了解每個管腳的定義。感謝社區(qū)的大佬們提供幫助 |
|