|
【HarmonyOS HiSpark Wi-Fi IoT 套件試用連連載】三、紅綠黃燈板的使用, wifiiot套件的板子很多,今天先來寫一篇關(guān)于使用紅綠燈板的帖子。主要完成紅綠燈板,LED、蜂鳴器、按鍵的控制。
# 一、硬件介紹
通過查看紅綠燈板的原理圖,紅綠燈板主要有紅、綠、黃LED燈各一個(gè),蜂鳴器一個(gè),按鍵一個(gè)。
板子資源.jpg (38.93 KB, 下載次數(shù): 0)
下載附件 保存到相冊
2 小時(shí)前 上傳
經(jīng)過查看原理圖,它們使用主控的GPIO口分別為:
red --》 GPIO10
green --》 GPIO11
yellow --》 GPIO12
beep --》GPIO9
switch --》GPIO8
# 二、新建文件
之前一直做的都是MCU這方面的開發(fā),習(xí)慣了使用IDE,對一些編譯工具鏈都沒什么了解。對于怎么在原來的工程目錄下,新建文件不是很熟悉。但是這些都不是什么大問題,可以看原來的工程目錄結(jié)構(gòu)是怎么樣的,按照它的模板進(jìn)行新建就行了。
好像就是一個(gè)文件夾下存放.c .h文件和BUILD.gn。
app目錄.JPG (15.22 KB, 下載次數(shù): 0)
下載附件 保存到相冊
1 小時(shí)前 上傳
好的,我們就那樣來開干。在app目錄下新建SSL文件夾,下面再新建c文件和BUILD.gn文件。
SSL.JPG (10.83 KB, 下載次數(shù): 0)
下載附件 保存到相冊
1 小時(shí)前 上傳
## 1、light.c
該C文件任務(wù)主要是控制紅綠黃燈亮滅。把原來的led例程復(fù)制過來進(jìn)行更改。
新建一個(gè)任務(wù),紅綠燈一秒亮,一秒滅循環(huá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.
- */
- #include <stdio.h>
- #include <unistd.h>
- #include “ohos_init.h“
- #include “cmsis_os2.h“
- #include “wifiiot_gpio.h“
- #include “wifiiot_gpio_ex.h“
- #define LIGHT_INTERVAL_TIME_US 1000000
- #define LIGHT_TASK_STACK_SIZE 512
- #define LIGHT_TASK_PRIO 26
- #define YELLOW_GPIO WIFI_IOT_IO_NAME_GPIO_12
- #define GREEN_GPIO WIFI_IOT_IO_NAME_GPIO_11
- #define RED_GPIO WIFI_IOT_IO_NAME_GPIO_10
- static void *LightTask(const char *arg)
- {
- (void)arg;
- while (1) {
- GpioSetOutputVal(YELLOW_GPIO, 1);
- GpioSetOutputVal(RED_GPIO,1);
- GpioSetOutputVal(GREEN_GPIO,1);
- usleep(LIGHT_INTERVAL_TIME_US);
- GpioSetOutputVal(YELLOW_GPIO, 0);
- GpioSetOutputVal(RED_GPIO,0);
- GpioSetOutputVal(GREEN_GPIO,0);
- usleep(LIGHT_INTERVAL_TIME_US);
- }
- return NULL;
- }
- static void LightTaskEntry(void)
- {
- osThreadAttr_t attr;
- GpioInit();
- 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 = “LightTask“;
- attr.attr_bits = 0U;
- attr.cb_mem = NULL;
- attr.cb_size = 0U;
- attr.stack_mem = NULL;
- attr.stack_size = LIGHT_TASK_STACK_SIZE;
- attr.priority = LIGHT_TASK_PRIO;
- if (osThreadNew((osThreadFunc_t)LightTask, NULL, &attr) == NULL) {
- printf(“[LightTask] Falied to create LightTask!\n“);
- }
- }
- SYS_RUN(LightTaskEntry);
復(fù)制代碼
2、beep.c
該C文件任務(wù)主要任務(wù)是通過按下按鍵,控制蜂鳴器發(fā)聲。
新建任務(wù),按下按鍵,蜂鳴器發(fā)聲,松開按鍵,蜂鳴器停止發(fā)聲。
這里要注意一下,按鍵的引腳信號為輸入信號,初始化的時(shí)候,需要先進(jìn)行上拉模式設(shè)置,要不然會一直檢測到低電平。
- /*
- * 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.
- */
- #include <stdio.h>
- #include <unistd.h>
- #include “ohos_init.h“
- #include “cmsis_os2.h“
- #include “wifiiot_gpio.h“
- #include “wifiiot_gpio_ex.h“
- #define BEEP_INTERVAL_TIME_US 1000000
- #define BEEP_TASK_STACK_SIZE 512
- #define BEEP_TASK_PRIO 27
- #define BEEP_GPIO WIFI_IOT_IO_NAME_GPIO_9
- #define BUTTON_GPIO WIFI_IOT_IO_NAME_GPIO_8
- #define BUTTON_CHECK_DELAY_US 5000
- static void *BeepTask(const char *arg)
- {
- (void)arg;
- while (1) {
- WifiIotGpioValue buttonValue;
- GpioGetInputVal(BUTTON_GPIO ,&buttonValue);
- if(buttonValue == WIFI_IOT_GPIO_VALUE0){
- GpioSetOutputVal(BEEP_GPIO, 1);
- usleep(BUTTON_CHECK_DELAY_US);
- GpioSetOutputVal(BEEP_GPIO, 0);
- usleep(BUTTON_CHECK_DELAY_US);
- }
- usleep(BUTTON_CHECK_DELAY_US);
- }
- return NULL;
- }
- static void BeepTaskEntry(void)
- {
- osThreadAttr_t attr;
- GpioInit();
- IoSetFunc(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_IO_FUNC_GPIO_8_GPIO);
- GpioSetDir(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_GPIO_DIR_IN);
- IoSetPull(WIFI_IOT_IO_NAME_GPIO_8,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_OUT);
- attr.name = “BeepTask“;
- attr.attr_bits = 0U;
- attr.cb_mem = NULL;
- attr.cb_size = 0U;
- attr.stack_mem = NULL;
- attr.stack_size = BEEP_TASK_STACK_SIZE;
- attr.priority = BEEP_TASK_PRIO;
- if (osThreadNew((osThreadFunc_t)BeepTask, NULL, &attr) == NULL) {
- printf(“[BeepTask] Falied to create BeepTask!\n“);
- }
- }
- SYS_RUN(BeepTaskEntry);
復(fù)制代碼
3、BUILD.gn
對于我這種只習(xí)慣于IDE開發(fā),對這類文件都不熟悉,但是這些都些東西都是有規(guī)律的,看其他的文件夾下的BUILD.gn是怎么編寫的,然后復(fù)制更改。
- # 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.
- static_library(“ssl_example“) {
- sources = [
- “l(fā)ight.c“,
- “beep.c“
- ]
- include_dirs = [
- “//utils/native/lite/include“,
- “//kernel/liteos_m/components/cmsis/2.0“,
- “//base/iot_hardware/inteRFaces/kits/wifiiot_lite“,
- ]
- }
-
復(fù)制代碼
這樣還不行,還要修改app文件夾下的BUILD.gn。
app目錄1.jpg (15.26 KB, 下載次數(shù): 0)
下載附件 保存到相冊
1 小時(shí)前 上傳
- # 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.
- import(“//build/lite/config/component/lite_component.gni“)
- lite_component(“app“) {
- features = [
- “startup“,
- “SSL:ssl_example“,
- ]
- }
復(fù)制代碼
三、編譯下載
編譯.JPG (44.5 KB, 下載次數(shù): 0)
下載附件 保存到相冊
2 小時(shí)前 上傳
下載驗(yàn)證成功。 |
|