基于Bearpi开发板鸿蒙OS之Hello world

news/2024/7/21 10:30:10 标签: Bearpi开发板, HarmonyOS

Bearpi开发环境搭建及代码获取请参考以下链接

开发环境搭建

用VS code打开获取到的源码后,在sample目录下新建一个文件夹my_app,在my_app下新建hello_world.c和BUILD.gn两个文件

在这里插入图片描述

在hello_world.c中加入以下内容

#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"

void Hello_World(void)
{
    for(int i=0; i<1000; i++)  // 打印1000次,间隔1秒打印一次
    {
        printf("Hello World!\r\n"); // 打印Hello World!
        usleep(1000000); // 延时1秒
    }
    
}
APP_FEATURE_INIT(Hello_World);   // 注册初时化模块
  • 在my_app下的BUILD.gn中加入以下内容
static_library("hello_world") {  //编译成静态库文件libhello_world.a
sources = [
"hello_world.c"       // 源文件名
]
include_dirs = [    //头文件路径
  
  "//utils/native/lite/include",  
  "//kernel/liteos_m/components/cmsis/2.0",
 
]
}
  • 在sample下的BUILD.gn中新增"my_app:hello_world",并将原来的"B2_basic_button:button_example"注释掉

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

lite_component("app") {
    features = [
        #"A1_kernal_thread:thread_example",
        #"A2_kernel_timer:timer_example",
        #"A3_kernel_event:event_example",
        #"A4_kernel_mutex:mutex_example",
        #"A5_kernel_semaphore:semaphore_example",
        #"A6_kernel_message:message_example",

        #"B1_basic_led_blink:led_example",
        #"B2_basic_button:button_example",
        #"B3_basic_pwm_led:pwm_example",
        #"B4_basic_adc:adc_example",
        #"B5_basic_i2c_nfc:i2c_example",
        #"B6_basic_uart:uart_example",
        
        #"C1_e53_sf1_mq2:e53_sf1_example",
        #"C2_e53_ia1_temp_humi_pls:e53_ia1_example",
        #"C3_e53_sc1_pls:e53_sc1_example",
        #"C4_e53_sc2_axis:e53_sc2_example",
        #"C5_e53_is1_infrared:e53_is1_example",

        #"D1_iot_wifi_ap:wifi_ap",
        #"D2_iot_wifi_sta_connect:wifi_sta_connect",        
        #"D3_iot_udp_client:udp_client",
        #"D4_iot_tcp_server:tcp_server",
        #"D5_iot_mqtt:iot_mqtt",        
        #"D6_iot_cloud_oc:oc_mqtt",
        #"D7_iot_cloud_onenet:onenet_mqtt",
        #"D8_iot_cloud_oc_smoke:cloud_oc_smoke",
        #"D9_iot_cloud_oc_light:cloud_oc_light",
        #"D10_iot_cloud_oc_manhole_cover:cloud_oc_manhole_cover",
        #"D11_iot_cloud_oc_infrared:cloud_oc_infrared",
        #"D12_iot_cloud_oc_agriculture:cloud_oc_agriculture",
        #"D13_iot_cloud_oc_gps:cloud_oc_gps",
        
        "my_app:hello_world",
        
    ]
}

  • 使用MobaXterm工具编译后并烧录代码到开发板,运行效果图
    在这里插入图片描述

注册入口函数

在这里插入图片描述

  • APP_FEATURE_INIT宏原型
#define APP_FEATURE_INIT(func) LAYER_INITCALL_DEF(func, app_feature, "app.feature")
  • LAYER_INITCALL_DEF宏原型
// Default priority is 2, priority range is [0, 4]
#define LAYER_INITCALL_DEF(func, layer, clayer) \
    LAYER_INITCALL(func, layer, clayer, 2)
  • LAYER_INITCALL宏原型
#define LAYER_INITCALL(func, layer, clayer, priority)            \
    static const InitCall USED_ATTR __zinitcall_##layer##_##func \
        __attribute__((section(".zinitcall." clayer #priority ".init"))) = func
  • InitCall定义原型
typedef void (*InitCall)(void);
  • USED_ATTR 宏定义原型
#define USED_ATTR __attribute__((used))

-那么APP_FEATURE_INIT(my_led_example)宏依次展开如下

  1. APP_FEATURE_INIT(my_led_example)
LAYER_INITCALL_DEF(my_led_example, app_feature, "app.feature")
LAYER_INITCALL(my_led_example, app_feature, "app.feature", 2)
static const InitCall USED_ATTR __zinitcall_app_feature_my_led_example \
        __attribute__((section(".zinitcall.app_feature2 .init"))) = my_led_example

http://www.niftyadmin.cn/n/1226543.html

相关文章

EPS8266移动端网络控制家庭设备

本文&#xff0c;我们将讨论如何构建网络服务器&#xff0c;以借助库来获取Wi-Fi网络凭据&#xff0c;将其保存到SPIFFS文件中&#xff0c;紧接着&#xff0c;我们将构建一个模块&#xff0c;使您可以使用红外线控制电视。 库 到目前为止&#xff0c;Sketch中已使用以下代码对…

基于Bearpi开发板之LED闪烁

在源码工程sample目录下新建my_led文件夹&#xff0c;在my_led目录下新建myled.c和BUILD.gn两个文件 在myled.c中加入以下内容 #include <stdio.h> #include <unistd.h> #include "ohos_init.h" #include "cmsis_os2.h" #include "wifii…

ESP8266红外检测安全系统

我们每个人都想知道不在家时&#xff0c;家中所发生的事情。这与安全性或天然气&#xff0c;火或水等关键要素有关。 知道发生了什么事情的瞬间对于将最终损坏降至最低至关重要。 我们将重点介绍PIR传感器&#xff0c;但是相同的原理也适用于其他传感器&#xff0c;例如湿度&am…

Bearpi开发板之Visual studio code终端编译

使用Visual studio code打开bearpi工程 VMware Workstation 16 Player并登入到bearpi&#xff0c;右键选择打开终端&#xff0c;虚拟机及服务器搭建请参考此链接介绍 在终端输入ifconfig,得到服务器地址&#xff0c;等下Visual studio code要使用 在Visual studio code的Term…

ESP8266云端数据加密保护

启用mosquitto上加密 要在mosquitto上启用加密&#xff0c;您首先需要拥有证书。 您可以从发行证书的公司购买它们&#xff0c;也可以将它们自己生成为自签名证书。 安装OpenSSL包 首先&#xff0c;确认您已安装openssl软件包&#xff0c;并且它具有较新的版本&#xff08;1…

ESP8266与网络服务器实时通讯

目前&#xff0c;所有已呈现的通信都是基于请求响应方法的&#xff0c;其中一个实体正在发送请求&#xff0c;另一实体正在发送回响应。 但是在某些情况下&#xff0c;您需要ESP8266模块与服务器之间的实时通信&#xff0c;而不仅仅是事务。 实时通讯协议 ESP8266流数据 要从…

bearpi开发板之HarmonyOS编译框架介绍

编译控制的Json文件 业务gn文件&#xff0c;这里以my_led目录下的为例 编译后生的libmyled.a 模块gn文件,这里选择 "my_led:myled"模块参与编译 # Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd. # Licensed under the Apache Licens…

ESP8266使用Docker容器化移动端控制

容器映像是一个软件的轻量级独立可执行软件包&#xff0c;包括运行该映像所需的一切&#xff1a;代码&#xff0c;运行时&#xff0c;系统工具&#xff0c;系统库和设置。 这样&#xff0c;您无需安装大量的库和软件&#xff0c;就可以使用现有映像并从中启动一个容器。 我们将…