One 、 brief introduction
Intelligent metal detector is based on OpenAtom OpenHarmony( hereinafter referred to as “OpenHarmony”) operating system , Use the principle of electromagnetic induction to detect the surrounding metal objects . This example adopts the method of multi device collaboration , compatible OpenHarmony Equipment development and application development , The whole example reflects OpenHarmony Of NAPI、eTS UI、UI Management status @state And audio playback and other technical features .

This project consists of Geek_Lite_Board Development board and Runhe RK3568 Composition of development board ,Geek_Lite_Board The main control chip of the development board is STM32F427IIH6, As the equipment end, detect the change of magnetic field strength , It uses OpenHarmony 3.0 LTS edition . Runhe RK3568 The development board is developed by Runhe software , Using Ruixin micro RK3568 chip , As the application end, it displays metal detection information , It uses OpenHarmony 3.1 Release edition .
Two 、 Running effect
When there is metal around , The buzzer at the equipment end will give an alarm and pass the detection information Wi-Fi The module is sent to the application . After the application receives the detection information , The detection status will be displayed on the screen and the detection results will be announced by voice .

3、 ... and 、 Function realization
Geek_Lite_Board Development board through AK8963 The electronic compass chip obtains the strength of the earth's magnetic field . Usually the magnetic field strength of the earth is 0.4-0.6 gaussian , When the metal is close to the electronic compass chip , According to the principle of electromagnetic induction , The induction of metal in the magnetic field will cause the change of magnetic field signal .Geek_Lite_Board Development board through Wi-Fi And run he RK3568 Development board real-time communication , Upload the detected information in real time . Runhe RK3568 Development board as the application end , Adopt the ark development framework (ArkUI) be based on eTS The extended declarative development paradigm writes pages , adopt NAPI Interface receiving device end Geek_Lite_Board Detection information of development board , Use OpenHarmony The audio playback function in the media subsystem realizes voice broadcast of metal detection status .

Magnetic field data acquisition
AK8963 A three-axis electronic compass integrated chip with high sensitivity Hall sensor technology , The interior contains magnetic sensors , Can be in x Axis 、y Axis 、z The axis detects the strength of the earth's magnetic field .

● adopt Mpu_Read_Bytes() Function reads the value of the chip data register .
uint8_t Mpu_Read_Bytes(uint8_t const regAddr, uint8_t *pData, uint8_t len)
● obtain AK8963 Magnetic field raw data .
Mpu_Data.mag_x = (MPU_BUFF[16] << 8) | MPU_BUFF[15]; // x Axial magnetic field data
Mpu_Data.mag_y = (MPU_BUFF[18] << 8) | MPU_BUFF[17]; // y Axial magnetic field data
Mpu_Data.mag_z = (MPU_BUFF[20] << 8) | MPU_BUFF[19]; // z Axial magnetic field data
● Yes AK8963 Transform the original magnetic field data , Get the magnetic field strength , Unit Gauss .
Gauss_Mag_z = Mpu_Calc.mag_z * 0.15f * 0.01f;
Detect metal information
First collect 100 Group z Axis magnetic field strength reference data , Then average these data , From this we can get z Zero point data of axial magnetic field strength .
const uint16_t calibrateCount = 1000; // The maximum number of measurements is 1000
const uint16_t calibrateFrequency = 5; // Every measurement 5 Take one valid value at a time
const uint16_t calibrateAverageCount = 100; // take 100 Secondary valid value
if(i < calibrateCount){
i++;
if(i%calibrateFrequency == calibrateFrequency){
Mag_z_buff[j++] = Gauss_Mag_z;
if(j >= calibrateAverageCount){
i = calibrateCount;
Mag_z_Flag = true;
for(k=0;k<calibrateAverageCount;k++){
origin_mag_z += Mag_z_buff[k];
}
origin_mag_z = origin_mag_z / calibrateAverageCount;
// Calibration complete , The blue light is on , Send to the application "CalibrateOK"
BLUE_LED_ON();
memset(buff,0x00,sizeof(buff));
sprintf(buff,"angle:%s","CalibrateOK");
ESP8266_send_data(buff,strlen(buff));
}
}
}
With origin_mag_z As zero point , Compare the obtained magnetic field data with it , To judge whether metal is detected .
NAPI get data
NAPI(Native API) yes OpenHarmony A kind of standard system JS API Implementation mechanism , Suitable for packaging IO、CPU intensive 、OS The bottom layer and other capabilities are exposed JS Interface , adopt NAPI Can achieve JS And C/C++ Code accesses each other . Runhe RK3568 The application end passes through NAPI To receive the detection information sent by the device .
●
Bottom NAPI Module encapsulation
The package module is named tcpserverapi, Download to tcpservermodule Folder .
Download it and put it in 3.1Release Source root , And configure the compilation script ; After the first compilation, you need to burn the entire image , Later, modify the module source code , Just put the Library send Go inside the board . The order is as follows :
// First mount , Again send
hdc_std shell mount -oremount,rw /
hdc_std file send libtcpserverapi.z.so system/lib/module/libtcpserverapi.z.so
●
Application side import NAPI modular
import tcpserverapi from '@ohos.tcpserverapi'
●
Application end NAPI Interface call
// call initServer Interface initialization TCP The server
tcpserverapi.initServer()
// call recvMsg Get and parse Geek_Lite_Board The angle sent from the development board
tcpserverapi.recvMsg().then((result) => {
var resultAngle = result.angle;
})
UI Status display and management

As shown in the above figure, the page under detection , The overall layout is divided into text title and detection status diagram .
The text title is composed of Flex Layout container text Component implementation , Used to present a piece of information , The following for text Interface related properties .

●
The detection state diagram includes the page background diagram , Detection state diagram , Test result description , The specific functions are Flex Flexible layout component implementation . The interface prototype is shown below :
Flex(options?: { direction?: FlexDirection, wrap?: FlexWrap, justifyContent?: FlexAlign, alignItems?: ItemAlign, alignContent?: FlexAlign })
●
By calling recvMsg The interface picks up the message of the metal detector , And modify the display status of the home page , The key implementation code is described as follows :
aboutToAppear() {
var intervalID = setInterval(() => {
tcpserverapi.recvMsg().then((result) => {
}
}
}
among “ var intervalID = setInterval(() => ” Timer used , Timed call “recvMsg” Method , adopt NAPI Obtain metal test results .
if (resultMetal.match("metal:")) {
this.metal = resultMetal.slice(6);
console.info('=======' + this.metal)
if (this.metal === 'Detected') {
this.detectionState = ' Find metal ';
this.detection = $r("app.media.img_detected");
this.isDisplay = false;
}
else if (this.metal === 'UnDetected' || this.metal ==='CalibrateOK') {
this.detectionState = ' Testing ';
this.detection = $r("app.media.img_detecting");
this.isDisplay = true;
}
}
When passed NAPI The detection information obtained by the method is “ metal:Detected ” when , Set up detectionState To discover the state of metal , The screen shows that a metal page has been detected ; When passed NAPI The detection information obtained by the method is “ metal:UnDetected ” when , No metal information is detected at this time , Set up detectionState Is in the detection state , The screen displays the detection status page .
Voice broadcast detection status
When metal is detected ,RK3568 The voice message that metal has been detected will be broadcast , Achieve the purpose of notifying users , adopt OpenHarmony The audio playing function of the media subsystem is realized .
OpenHarmony The media subsystem provides developers with a simple and easy to understand interface , So that developers can easily access the system and use the media resources of the system . The media subsystem includes audio playback 、 Video playback 、 Common functions such as audio recording and video recording .
The main work of audio playback is to transcode audio data into audible audio analog signals and play them through output devices , At the same time, manage the playback task . This example mainly imports media modular 、 Create an audio playback instance 、 Realize the audio playback interface and the pause playback interface .
●
Import media modular
import media from '@ohos.multimedia.media';
●
Create an audio playback instance
// OH media object
private player = media.createAudioPlayer();
●
Realize the audio playback interface
Use play() Call to start playing audio resources , After the audio data is loaded , namely src Property setting is completed before calling .
play():void
Four 、 summary
This article briefly describes how to use OpenHarmony Develop multiple devices , Demonstrated NAPI、eTS UI、UI Management status @state And the application of technical features such as audio playback , Through these diversified functions , We finally implemented a sample metal detector . Rich and varied OpenHarmony Developing samples is inseparable from the contributions of partners and developers , If you want to share your own examples , Welcome to submit the sample to OpenHarmony Knowledge system SIG Canglai , Work together to build development samples .
5、 ... and 、 Reference link
RK3568 Develop the silky experience on the board OpenHarmony Standard system
https://gitee.com/openharmony-sig/knowledge_demo_smart_home/tree/master/dev/docs/rk3568_quick_start
stay Windows compile OpenHarmony engineering
https://gitee.com/Cruise2019/team_x/blob/master/homework/ohos_build_win/readme.md
OpenHarmony NAPI Learning documents
https://gitee.com/javen678/hello-ohos-napi/tree/master/doc#/javen678/hello-ohos-napi/blob/master/doc/1.HelloNAPI.md
OpenHarmony be based on eTS Extended declarative development paradigm
https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/arkui-ts/Readme-CN.md
Audio playback development guide
https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/media/audio-playback.md
Develop metal detector applications from zero
https://gitee.com/openharmony-sig/knowledge_demo_temp/blob/master/docs/metal_detection/quick_develop.md
GEEKROS Official website

原网站版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051043484735.html