[文章]【HarmonyOS HiSpark Wi-Fi IoT 套件试用连连载】三、红绿黄灯板的使用

阅读量0
0
0
wifiiot套件的板子很多,今天先来写一篇关于使用红绿灯板的帖子。主要完成红绿灯板,LED、蜂鸣器、按键的控制。
# 一、硬件介绍
通过查看红绿灯板的原理图,红绿灯板主要有红、绿、黄LED灯各一个,蜂鸣器一个,按键一个。
板子资源.jpg

经过查看原理图,它们使用主控的GPIO口分别为:
red         --》 GPIO10
green     --》 GPIO11
yellow    --》 GPIO12
beep       --》GPIO9

switch     --》GPIO8

# 二、新建文件
之前一直做的都是MCU这方面的开发,习惯了使用IDE,对一些编译工具链都没什么了解。对于怎么在原来的工程目录下,新建文件不是很熟悉。但是这些都不是什么大问题,可以看原来的工程目录结构是怎么样的,按照它的模板进行新建就行了。
好像就是一个文件夹下存放.c .h文件和BUILD.gn。
app目录.JPG

好的,我们就那样来开干。在app目录下新建SSL文件夹,下面再新建c文件和BUILD.gn文件。
SSL.JPG

## 1、light.c
该C文件任务主要是控制红绿黄灯亮灭。把原来的led例程复制过来进行更改。
新建一个任务,红绿灯一秒亮,一秒灭循环闪烁。
  1. /*
  2. * Copyright (c) 2020 Huawei Device Co., Ltd.
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. *     http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */

  15. #include <stdio.h>

  16. #include <unistd.h>

  17. #include "ohos_init.h"
  18. #include "cmsis_os2.h"
  19. #include "wifiiot_gpio.h"
  20. #include "wifiiot_gpio_ex.h"

  21. #define LIGHT_INTERVAL_TIME_US 1000000
  22. #define LIGHT_TASK_STACK_SIZE 512
  23. #define LIGHT_TASK_PRIO 26

  24. #define YELLOW_GPIO  WIFI_IOT_IO_NAME_GPIO_12
  25. #define GREEN_GPIO   WIFI_IOT_IO_NAME_GPIO_11
  26. #define RED_GPIO     WIFI_IOT_IO_NAME_GPIO_10



  27. static void *LightTask(const char *arg)
  28. {
  29.     (void)arg;
  30.     while (1) {
  31.                 GpioSetOutputVal(YELLOW_GPIO, 1);
  32.                 GpioSetOutputVal(RED_GPIO,1);
  33.                 GpioSetOutputVal(GREEN_GPIO,1);
  34.                 usleep(LIGHT_INTERVAL_TIME_US);
  35.                 GpioSetOutputVal(YELLOW_GPIO, 0);
  36.                 GpioSetOutputVal(RED_GPIO,0);
  37.                 GpioSetOutputVal(GREEN_GPIO,0);
  38.                 usleep(LIGHT_INTERVAL_TIME_US);
  39.         }

  40.     return NULL;
  41. }

  42. static void LightTaskEntry(void)
  43. {
  44.     osThreadAttr_t attr;

  45.     GpioInit();
  46.     IoSetFunc(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_IO_FUNC_GPIO_10_GPIO);
  47.     GpioSetDir(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_GPIO_DIR_OUT);
  48.    
  49.     IoSetFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_FUNC_GPIO_11_GPIO);
  50.     GpioSetDir(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_GPIO_DIR_OUT);
  51.    
  52.     IoSetFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_FUNC_GPIO_12_GPIO);
  53.     GpioSetDir(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_GPIO_DIR_OUT);

  54.     attr.name = "LightTask";
  55.     attr.attr_bits = 0U;
  56.     attr.cb_mem = NULL;
  57.     attr.cb_size = 0U;
  58.     attr.stack_mem = NULL;
  59.     attr.stack_size = LIGHT_TASK_STACK_SIZE;
  60.     attr.priority = LIGHT_TASK_PRIO;

  61.     if (osThreadNew((osThreadFunc_t)LightTask, NULL, &attr) == NULL) {
  62.         printf("[LightTask] Falied to create LightTask!n");
  63.     }
  64. }

  65. SYS_RUN(LightTaskEntry);
复制代码



2、beep.c
该C文件任务主要任务是通过按下按键,控制蜂鸣器发声。
新建任务,按下按键,蜂鸣器发声,松开按键,蜂鸣器停止发声。
这里要注意一下,按键的引脚信号为输入信号,初始化的时候,需要先进行上拉模式设置,要不然会一直检测到低电平。
  1. /*
  2. * Copyright (c) 2020 Huawei Device Co., Ltd.
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. *     http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */

  15. #include <stdio.h>

  16. #include <unistd.h>

  17. #include "ohos_init.h"
  18. #include "cmsis_os2.h"
  19. #include "wifiiot_gpio.h"
  20. #include "wifiiot_gpio_ex.h"

  21. #define BEEP_INTERVAL_TIME_US 1000000
  22. #define BEEP_TASK_STACK_SIZE 512
  23. #define BEEP_TASK_PRIO 27

  24. #define BEEP_GPIO  WIFI_IOT_IO_NAME_GPIO_9
  25. #define BUTTON_GPIO   WIFI_IOT_IO_NAME_GPIO_8

  26. #define BUTTON_CHECK_DELAY_US   5000


  27. static void *BeepTask(const char *arg)
  28. {
  29.     (void)arg;
  30.     while (1) {

  31.             WifiIotGpioValue buttonValue;
  32.             GpioGetInputVal(BUTTON_GPIO ,&buttonValue);
  33.             if(buttonValue == WIFI_IOT_GPIO_VALUE0){
  34.                 GpioSetOutputVal(BEEP_GPIO, 1);
  35.                 usleep(BUTTON_CHECK_DELAY_US);
  36.                 GpioSetOutputVal(BEEP_GPIO, 0);
  37.                 usleep(BUTTON_CHECK_DELAY_US);
  38.             }
  39.             usleep(BUTTON_CHECK_DELAY_US);

  40.         }

  41.     return NULL;
  42. }


  43. static void BeepTaskEntry(void)
  44. {

  45.     osThreadAttr_t attr;

  46.     GpioInit();
  47.     IoSetFunc(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_IO_FUNC_GPIO_8_GPIO);
  48.     GpioSetDir(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_GPIO_DIR_IN);
  49.     IoSetPull(WIFI_IOT_IO_NAME_GPIO_8,WIFI_IOT_IO_PULL_UP);

  50.     IoSetFunc(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_IO_FUNC_GPIO_9_GPIO);
  51.     GpioSetDir(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_GPIO_DIR_OUT);

  52.     attr.name = "BeepTask";
  53.     attr.attr_bits = 0U;
  54.     attr.cb_mem = NULL;
  55.     attr.cb_size = 0U;
  56.     attr.stack_mem = NULL;
  57.     attr.stack_size = BEEP_TASK_STACK_SIZE;
  58.     attr.priority = BEEP_TASK_PRIO;

  59.     if (osThreadNew((osThreadFunc_t)BeepTask, NULL, &attr) == NULL) {
  60.         printf("[BeepTask] Falied to create BeepTask!n");
  61.     }
  62. }

  63. SYS_RUN(BeepTaskEntry);
复制代码



3、BUILD.gn
对于我这种只习惯于IDE开发,对这类文件都不熟悉,但是这些都些东西都是有规律的,看其他的文件夹下的BUILD.gn是怎么编写的,然后复制更改。
  1. # Copyright (c) 2020 Huawei Device Co., Ltd.
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. #
  6. #     http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS,
  10. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. # See the License for the specific language governing permissions and
  12. # limitations under the License.

  13. static_library("ssl_example") {
  14.     sources = [
  15.         "light.c",
  16.         "beep.c"
  17.     ]

  18.     include_dirs = [
  19.         "//utils/native/lite/include",
  20.         "//kernel/liteos_m/components/cmsis/2.0",
  21.         "//base/iot_hardware/interfaces/kits/wifiiot_lite",
  22.     ]
  23. }
复制代码


这样还不行,还要修改app文件夹下的BUILD.gn。
app目录1.jpg

  1. # Copyright (c) 2020 Huawei Device Co., Ltd.
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. #
  6. #     http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS,
  10. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. # See the License for the specific language governing permissions and
  12. # limitations under the License.

  13. import("//build/lite/config/component/lite_component.gni")

  14. lite_component("app") {
  15.     features = [
  16.         "startup",
  17.         "SSL:ssl_example",
  18.     ]
  19. }
复制代码


三、编译下载
编译.JPG

下载验证成功。


回帖

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容图片侵权或者其他问题,请联系本站作侵删。 侵权投诉
链接复制成功,分享给好友