1. Bear pie Hello Word
1.1 add Hello Word source file
(1) open the VMware Workstation Pro virtual machine, then map the files of the virtual machine to Windows through RaiDrive, and open the Z:\home\bearpi\project\applications directory through VS code. Note that before memory mapping, you must open the terminal through the virtual machine, enter the ifconfig command to view the virtual machine ip, and fill the changed ip into the RaiDrive.
(2) , create a my_app folder, enter the folder and create a D1_ my_ The folder of helloword. Create helloword in the price asking folder C file, you can write applications in C file now.
#include <stdio.h> #include "ohos_init.h" void helloword(void) { printf("hello word!\r\n"); } APP_FEATURE_INIT(helloword);
The above is the complete code. Let's see why we do this.
①, stdio.h needless to say, it is the header file in the C library;
②,ohos_init.h refers to APP_FEATURE_INIT() is the header file of this function;
③,APP_ FEATURE_ The parameter of init() function is the address of a function passed in. It is used to link the function to the specified code segment. The specific code segment is left below.
The above simple program is written, so how can we let it participate in the compilation? Let's explain.
1.2 add the build file build gn
BUILD.gn is equivalent to Makefile, but its compilation speed is faster than makefile. BUILD.gn file is in the same level directory of C file.
Here is build GN file content:
static_library("helloword"){ sources = [ "./helloword.c" ] include_dirs = [ "//utils/native/lite/include" ] }
①,static_ The parameter in Library () is the file name generated by compilation;
② . sources is the C file path;
③,include_dirs is the path of the header file used by the C file, and "/ / utils/native/lite/include" is ohos_init.h) the path of the header file;
Note: in build The GN file cannot be indented with tab, only spaces can be used, so that the compilation will not report errors.
1.3 write build under applications gn
Add C file in features and compile the file path:
my_app/D1_my_helloword:helloword
Before the colon is the path of the compiled file, and after the colon is the compiled file. The file name must be the same as the innermost build GN compiled file names are the same.
1.4 compilation
Return the path to the upper level path of the applications folder, and then use
hpm dist
Command to compile.
After downloading:
2. Little bear pie lights up
2.1 add LED source code
Same in my_app create a new D2_my_led folder, add led in the folder C Documents:
Schematic diagram of LED part:
Code 1:
The function is to light the LED
#include <stdio.h> #include "ohos_init.h" #include "wifiiot_gpio.h" #include "wifiiot_gpio_ex.h" void led(void) { GpioInit(); IoSetFunc(WIFI_IOT_IO_NAME_GPIO_2,WIFI_IOT_IO_FUNC_GPIO_2_GPIO);//Set to normal GPIO function GpioSetDir(WIFI_IOT_IO_NAME_GPIO_2,WIFI_IOT_GPIO_DIR_OUT);//Set the direction of GPIO: set it to output mode GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_2,1);//Set output high level //GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_2,0);// Set output low level } APP_FEATURE_INIT(led);
①,wifiiot_gpio_ex.h and WiFi_ gpio. H header file contains functions of GpioInit(), IoSetFunc(), GpioSetDir(), and GpioSetOutputVal();
After downloading:
Code 2:
The function is to make the led light flash 10 times intermittently in 1s
#include <stdio.h> #include "ohos_init.h" #include "wifiiot_gpio.h" #include "wifiiot_gpio_ex.h" #include "unistd.h" void led(void) { GpioInit(); IoSetFunc(WIFI_IOT_IO_NAME_GPIO_2,WIFI_IOT_IO_FUNC_GPIO_2_GPIO);//Set to normal GPIO function GpioSetDir(WIFI_IOT_IO_NAME_GPIO_2,WIFI_IOT_GPIO_DIR_OUT);//Set the direction of GPIO: set it to output mode for(int i=0;i<10;i++) { GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_2,1);//Set output high level sleep(1);//Second order //usleep(1000000);// microsecond GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_2,0);//Set output low level sleep(1); //usleep(1000000);// microsecond } }
①,unistd.h is the header file of sleep() and usleep()()
Post download phenomenon
Little bear pie - Seasonal lighting
2.2 add the build file build gn
BUILD.gn file is in the same level directory of C file.
Here is build GN file content:
static_library("led"){ sources = [ "./led.c" ] include_dirs = [ "//utils/native/lite/include", "//base/iot_hardware/interfaces/kits/wifiiot_lite" ] }
①,static_ The parameter in Library () is the file name generated by compilation;
② . sources is the C file path;
③,include_dirs is the path of the header file used by the C file, and "/ / utils/native/lite/include" is ohos_init.h header file path, "/ / base / iot_hardware / interfaces / kits / wifi_lite" is WiFi_ gpio. h,wifiiot_ gpio_ Path of ex.h;
Note: in build The GN file cannot be indented with tab, only spaces can be used, so that the compilation will not report errors.
2.3 write build under applications gn
Add C file in features and compile the file path:
my_app/D2_my_led:led
Before the colon is the path of the compiled file, and after the colon is the compiled file. The file name must be the same as the innermost build GN compiled file names are the same.
2.4 compilation
Return the path to the upper level path of the applications folder, and then use
hpm dist
Command to compile.