[文章]【HarmonyOS HiSpark Wi-Fi IoT 套件试用连载】第三篇 跑马灯

阅读量0
0
0
`弄了半天,VSCode中还是识别不了串口,只好放弃,采用Ubuntu中编译,Windows中使用HiBurn来烧写。

拿到开发板通常第一件事儿都是写个helloWorld的程序,点个灯
我也不例外,搞了个跑马灯。
代码如下:
led_demo.c

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include "ohos_init.h"
  4. #include "cmsis_os2.h"
  5. #include "wifiiot_gpio.h"
  6. #include "wifiiot_gpio_ex.h"

  7. #define LED_TASK_STACK_SIZE 512
  8. #define LED_TASK_PRIO 25

  9. enum LedState {
  10.     LED_ON = 0,
  11.     LED_OFF,
  12.     LED_SPARK,
  13. };
  14. static void *LedTask(const char *arg)
  15. {
  16.     (void)arg;
  17.     while (1) {
  18.         GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_10, 1);
  19.         GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_11, 0);
  20.         GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_12, 0);
  21.         usleep(300000);
  22.         GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_10, 0);
  23.         GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_11, 0);
  24.         GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_12, 1);
  25.         usleep(300000);
  26.         GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_10, 0);
  27.         GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_11, 1);
  28.         GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_12, 0);
  29.         usleep(300000);
  30.     }
  31.     return NULL;
  32. }

  33. static void led_demo(void)
  34. {
  35.     osThreadAttr_t attr;
  36.     GpioInit();
  37.     IoSetFunc(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_IO_FUNC_GPIO_10_GPIO);
  38.     IoSetFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_FUNC_GPIO_11_GPIO);
  39.     IoSetFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_FUNC_GPIO_12_GPIO);
  40.     GpioSetDir(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_GPIO_DIR_OUT);
  41.     GpioSetDir(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_GPIO_DIR_OUT);
  42.     GpioSetDir(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_GPIO_DIR_OUT);

  43.     attr.name = "LedTask";
  44.     attr.attr_bits = 0U;
  45.     attr.cb_mem = NULL;
  46.     attr.cb_size = 0U;
  47.     attr.stack_mem = NULL;
  48.     attr.stack_size = LED_TASK_STACK_SIZE;
  49.     attr.priority = LED_TASK_PRIO;

  50.     if (osThreadNew((osThreadFunc_t)LedTask, NULL, &attr) == NULL) {
  51.         printf("[LedExample] Falied to create LedTask!
  52. ");
  53.     }
  54. }

  55. SYS_RUN(led_demo);
复制代码
led_demo文件夹中

BIULD.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("led_demo") {
  14.     sources = [
  15.         "led_demo.c"
  16.     ]

  17.     include_dirs = [
  18.         "//utils/native/lite/include",
  19.         "//kernel/liteos_m/components/cmsis/2.0",
  20.         "//base/iot_hardware/interfaces/kits/wifiiot_lite",
  21.     ]
  22. }
复制代码
同时,也要将app文件夹中的编译脚本文件BUILD.gn修改一下,在features中添加以下内容:
"led_demo:led_demo",
  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.         "led_demo:led_demo",
  18.     ]
  19. }
复制代码
添加完之后,返回CODE-1.0目录,在终端中输入以下命令:python build.py wifiiot
编译即可;
然后,返回Windows,用hiburn烧录即可。
第三篇完结,下一步,OLED,未完待续……








`[attach]976037[/attach]

回帖

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