
??想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??
??51CTO 開(kāi)源基礎(chǔ)軟件社區(qū)??
??https://ost.51cto.com??
前言
前面兩篇文章已經(jīng)實(shí)現(xiàn)了ADC的HDF框架接入,現(xiàn)在已經(jīng)可以正常調(diào)用HDF提供的ADC統(tǒng)一驅(qū)動(dòng)接口進(jìn)行應(yīng)用開(kāi)發(fā)。結(jié)合之前學(xué)的一些知識(shí),設(shè)計(jì)一個(gè)基于NAPI框架和HDF框架讀取溫度傳感器數(shù)據(jù)的程序應(yīng)用。
![#創(chuàng)作者激勵(lì)# [FFH]標(biāo)準(zhǔn)系統(tǒng)HDF平臺(tái)驅(qū)動(dòng)(三)——ADC應(yīng)用實(shí)現(xiàn)-開(kāi)源基礎(chǔ)軟件社區(qū) #創(chuàng)作者激勵(lì)# [FFH]標(biāo)準(zhǔn)系統(tǒng)HDF平臺(tái)驅(qū)動(dòng)(三)——ADC應(yīng)用實(shí)現(xiàn)-開(kāi)源基礎(chǔ)軟件社區(qū)](https://dl-harmonyos.51cto.com/images/202303/32c624a30c1c0069dd3257acc58c6957abb4ed.png?x-oss-process=image/resize,w_820,h_1280)
參考
??平臺(tái)驅(qū)動(dòng)使用??標(biāo)準(zhǔn)系統(tǒng)HDF平臺(tái)驅(qū)動(dòng)(一)——ADC驅(qū)動(dòng)適配
標(biāo)準(zhǔn)系統(tǒng)HDF平臺(tái)驅(qū)動(dòng)(二)——ADC平臺(tái)驅(qū)動(dòng)使用
環(huán)境
- OpenHarmony-3.2-Beta5
- 九聯(lián)UnionPi-Tiger開(kāi)發(fā)板
- Visual Studio Code(版本需1.62.0及以上)
- USB_Burning_Tool燒錄工具
- napi_generator工具可執(zhí)行文件或vs code插件
- Deveco Studio(API 9 )
- LM35線(xiàn)性模擬溫度傳感器
概述
開(kāi)發(fā)步驟
一、編譯構(gòu)建實(shí)現(xiàn)
添加子系統(tǒng)。
"napisubsys":{
"path":"vendor/unionman/unionpi_tiger/sample/napi/napisubsys",
"name":"napisubsys"
}
添加組件,打開(kāi)unionpi_tiger/sample/napi/napisubsys/ohos.build文件,在"parts":中添加下列語(yǔ)句。
"adc_hdf": {
"variants": [
"phone"
],
"module_list": [
"http://vendor/unionman/unionpi_tiger/sample/napi/napisubsys/adc_hdf:adc_hdf"
]
}
添加產(chǎn)品定義,打開(kāi)vendor/unionman/unionpi_tiger/config.json文件,在"subsystems":中添加下列語(yǔ)句。
{
"subsystem": "napisubsys",
"components": [
{
"component": "adc_hdf",
"features": []
}
]
},
二、NAPI接口設(shè)計(jì)及NAPI框架生成
編寫(xiě)ts文件,新建文件@ohos.adc_hdf.d.ts于vendor/unionman/unionpi_tiger/sample/napi/napisubsys/adc_hdf目錄下,聲明應(yīng)用接口函數(shù)get_adc_value,傳入?yún)?shù)為通道號(hào),返回值為ADC采樣值,北向應(yīng)用通過(guò)調(diào)用該接口獲取的ADC采樣值計(jì)算溫度。
declare namespace adc_hdf {
function get_adc_value(channel: number): number;
}
export default adc_hdf;
生成NAPI框架,使用napi_generator可執(zhí)行程序或者vscode插件生成NAPI框架。
生成框架路徑也選擇當(dāng)前路徑,number類(lèi)型選擇uint32_t。
三、NAPI接口實(shí)現(xiàn)
實(shí)現(xiàn)adc_hdf.cpp接口,文件生成結(jié)束后,我們定義的北向應(yīng)用接口需要在adc_hdf.cpp中實(shí)現(xiàn),具體代碼如下:
#include "adc_hdf.h"
#include "adc_if.h"
#include "hdf_log.h"
#include <cstdio>
namespace adc_hdf {
bool get_adc_value(NUMBER_TYPE_1& channel, NUMBER_TYPE_2& out)
{
int32_t ret;
DevHandle adcHandle = NULL;
uint32_t read_val = 0;
/* 打開(kāi)ADC設(shè)備 */
adcHandle = AdcOpen(ADC_DEVICE_NUM);
if (adcHandle == NULL) {
printf("%s: Open ADC%u fail!", __func__, ADC_DEVICE_NUM);
return false;
}
/* 讀取ADC采樣值 */
ret = AdcRead(adcHandle, (uint32_t)channel, &read_val);
if (ret != HDF_SUCCESS) {
printf("%s: ADC read fail!:%d", __func__, ret);
AdcClose(adcHandle);
return false;
}
/* 結(jié)果返回值 */
out = (NUMBER_TYPE_2)read_val;
printf("ADC read:%d\r\n",read_val);
/* 訪問(wèn)完畢關(guān)閉ADC設(shè)備 */
AdcClose(adcHandle);
return true;
}
}
修改BUILD.gn文件,主要添加了ADC平臺(tái)驅(qū)動(dòng)所需依賴(lài)及頭文件路徑,并且修改目標(biāo)子系統(tǒng)及所屬部件,編譯后會(huì)生成相應(yīng)的.so共享庫(kù)文件,存放在/system/lib/module目錄下。
import("http://build/ohos.gni")
ohos_shared_library("adc_hdf")
{
sources = [
"adc_hdf_middle.cpp",
"adc_hdf.cpp",
"tool_utility.cpp",
]
include_dirs = [
".",
"http://third_party/node/src",
"http://drivers/hdf_core/framework/include/platform"
]
deps=[
"http://foundation/arkui/napi:ace_napi",
"http://drivers/hdf_core/adapter/uhdf2/platform:libhdf_platform"
]
external_deps = [
"hdf_core:libhdf_utils",
"hiviewdfx_hilog_native:libhilog",
]
remove_configs = [ "http://build/config/compiler:no_rtti" ]
cflags=[
]
cflags_cc=[
"-frtti",
]
ldflags = [
]
relative_install_dir = "module"
part_name = "adc_hdf"
subsystem_name = "napisubsys"
}
四、編譯打包燒錄
進(jìn)入源碼根目錄,執(zhí)行如下命令進(jìn)行編譯:
./build.sh --product-name unionpi_tiger
編譯完成后需要打包成可以給開(kāi)發(fā)板燒錄的鏡像,執(zhí)行一下命令:
./device/board/unionman/unionpi_tiger/common/tools/packer-unionpi.sh
固件打包完成,生成路徑為編譯根目錄下的out/unionpi_tiger/packages/phone/images/OpenHarmony.img,下載或映射到Windows上進(jìn)行燒錄。
打開(kāi)燒錄工具,連接PC與開(kāi)發(fā)板OTG口并接通電源,導(dǎo)入燒錄包進(jìn)行燒錄。
![#創(chuàng)作者激勵(lì)# [FFH]標(biāo)準(zhǔn)系統(tǒng)HDF平臺(tái)驅(qū)動(dòng)(三)——ADC應(yīng)用實(shí)現(xiàn)-開(kāi)源基礎(chǔ)軟件社區(qū) #創(chuàng)作者激勵(lì)# [FFH]標(biāo)準(zhǔn)系統(tǒng)HDF平臺(tái)驅(qū)動(dòng)(三)——ADC應(yīng)用實(shí)現(xiàn)-開(kāi)源基礎(chǔ)軟件社區(qū)](https://dl-harmonyos.51cto.com/images/202303/09d7f9943e6181db6534684c440ec79027677f.png?x-oss-process=image/resize,w_820,h_683)
或者使用hdc_std工具:
hdc_std shell mount -o remount,rw /
hdc_std file send libadc_hdf.z.so /system/lib/module/
五、NAPI應(yīng)用實(shí)現(xiàn)
新建OpenHarmony工程(stage+ArkTs)。

導(dǎo)入外部接口,adc_hdf為上述定義NAPI接口生成的動(dòng)態(tài)庫(kù)文件名字一致,直接導(dǎo)入會(huì)報(bào)找不到包,忽略即可,prompt為彈窗組件接口。
// @ts-ignore
import adc_hdf from '@ohos.adc_hdf';
import prompt from '@system.prompt'
ADC通道選擇組件(使用Swiper滑塊視圖容器實(shí)現(xiàn))。新建滑塊視圖數(shù)據(jù)類(lèi)。
class MyDataSource implements IDataSource {
private list: string[] = []
private listener: DataChangeListener
constructor(list: string[]) {
this.list = list
}
totalCount(): number {
return this.list.length
}
getData(index: number): any {
return this.list[index]
}
registerDataChangeListener(listener: DataChangeListener): void {
this.listener = listener
}
unregisterDataChangeListener() {
}
}
Swiper滑塊視圖容器組件,滑動(dòng)或點(diǎn)擊可以切換ADC通道,點(diǎn)擊select進(jìn)行數(shù)據(jù)采集。
//通道選擇組件實(shí)現(xiàn)
@Component
struct channel_chose {
private swiperController: SwiperController = new SwiperController()
private data: MyDataSource = new MyDataSource([])
private channelNum: number = 2
private tmp: number = 1
@Link channel: number
//導(dǎo)入輪播內(nèi)容
aboutToAppear(): void {
let list = []
for (var i = 1; i <= this.channelNum; i++) {
list.push('ADC_' + i.toString());
}
this.data = new MyDataSource(list)
}
build() {
Column({ space: 5 }) {
Swiper(this.swiperController) {
LazyForEach(this.data, (item: string) => {
Text(item)
.borderRadius(10)
.width('80%')
.height(70)
.fontColor('#ff1a5ea4')
.fontWeight(FontWeight.Bolder)
.textAlign(TextAlign.Center)
.fontSize(30)
}, item => item)
}
.cachedCount(2)
.interval(4000)
.indicator(false)
.duration(1000)
.itemSpace(10)
.vertical(true)
.curve(Curve.Linear)
.onChange((index: number) => {
this.tmp = index + 1
})
Row({ space: 12 }) {
Button('Change').backgroundColor('#ff366fb1').fontSize(20).height(50).width(120)
.onClick(() => {
this.swiperController.showNext()
})
Button('Select').backgroundColor('#ff366fb1').fontSize(20).height(50).width(120)
.onClick(() => {
this.channel = this.tmp;
prompt.showToast({ message: 'Select data From ADC_'+this.channel.toString() })
console.info('Select data From Channel_'+this.channel.toString())
})
}.margin(20)
}.width('50%')
}
}
文本組件顯示溫度值。
@Component
struct show_temperature{
@Link temperature: Number
build(){
Text(this.temperature.toFixed(1)+'°C')
.width('50%')
.fontSize(60)
.fontColor(Color.White)
.fontStyle(FontStyle.Italic)
.textAlign(TextAlign.Center)
.fontWeight(FontWeight.Bold)
}
}
溫度計(jì)算及首頁(yè)內(nèi)容組件,程序啟動(dòng)時(shí)開(kāi)啟定時(shí)器,每隔1s獲取一次ADC溫度值。
@Entry
@Component
struct Index {
@State channel: number = 1
@State adc_val: number = 0
@State temperature: number = 0
private timerId: number = -1
//獲取溫度傳感器ADC采樣值并計(jì)算溫度
private get_adc_value() {
let get_value = adc_hdf.get_adc_value(this.channel-1);
if (get_value <= 1500) {
this.adc_val = get_value;
this.temperature = (this.adc_val / 4096) * 1.8 * 100;
}
else {
this.temperature = 0
prompt.showToast({
message: "獲取失敗,請(qǐng)檢查連線(xiàn)", // 顯示文本
duration: 1000, // 顯示時(shí)長(zhǎng)
})
}
}
//程序運(yùn)行時(shí)開(kāi)啟定時(shí)器Interval,每隔一秒調(diào)用get_adc_value更新溫度值
aboutToAppear(): void {
this.timerId = setInterval(() => {
this.get_adc_value()
}, 1000)
}
//銷(xiāo)毀定時(shí)器
aboutToDisappear() {
if (this.timerId > 0) {
clearTimeout(this.timerId)
this.timerId = -1
}
}
build() {
Row() {
show_temperature({temperature: $temperature})
Column({space:0}){
channel_chose({channel:$channel})
}.height('80%')
}.height('100%')
.backgroundImage($r('app.media.bg')).backgroundImageSize(ImageSize.Cover)
}
}
應(yīng)用簽名。
安裝應(yīng)用到開(kāi)發(fā)板。
結(jié)果演示
https://ost.51cto.com/show/22186
文章相關(guān)附件可以點(diǎn)擊下面的原文鏈接前往下載:
https://ost.51cto.com/resource/2661
??想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??
??51CTO 開(kāi)源基礎(chǔ)軟件社區(qū)??
??https://ost.51cto.com??