|
发表于 2022-1-20 13:48:53
1657 浏览 1 回复
SC60 SDM450 Android9.0 HP5806驱动
需求:
实现在SC60 Android9.0的平台上,驱动HP5806,是现在APP端获取到温度和气压高度值!
解决方案:
JNI部分代码如下(hp5806_driver.tar.gz 为在Android9.0下的驱动移植包;):
- typedef struct
- {
- int16_t C0;
- int16_t C1;
- int32_t C00;
- int32_t C10;
- int16_t C01;
- int16_t C11;
- int16_t C20;
- int16_t C21;
- int16_t C30;
- uint32_t tmp_osr_scale_coeff;
- uint32_t prs_osr_scale_coeff;
- }hp5806_calib_data;
- typedef struct{
- uint8_t pressure[3];
- uint8_t temperature[3];
- hp5806_calib_data calib_coeffs;
- }hp5806_report_s;
- static int hp5806_fd = -1;
- #define KT 524288
- #define KP 253952
- void hp5806_convert(hp5806_report_s * rp,float * pressure,float *temp)
- {
- long press_raw;
- long temp_raw;
- long int temputer_valu = 0;
- long int pressure_valu = 0;
- long Traw;
- long Praw;
- double Traw_sc;
- double Praw_sc;
- double temp_scaled;
- float temp_final;
- double press_scaled;
- float press_final;
- LOGE("pressure[0]=0x%x,pressure[1]=0x%x,pressure[2]=0x%x,temperature[0]=0x%x,temperature[1]=0x%x,temperature[2]=0x%x\r\n",rp->pressure[0],rp->pressure[1],rp->pressure[2],rp->temperature[0],rp->temperature[1],rp->temperature[2]);
- LOGE("calib.C0 =%d,C1 =%d,C00 =%d,C10 =%d,C01 =%d,C11 =%d,C20 =%d,C21 =%d,C30 =%d\r\n",rp->calib_coeffs.C0,rp->calib_coeffs.C1,rp->calib_coeffs.C00,rp->calib_coeffs.C10,rp->calib_coeffs.C01,rp->calib_coeffs.C11,rp->calib_coeffs.C20,rp->calib_coeffs.C21,rp->calib_coeffs.C30);
- press_raw = (rp->pressure[2]) + (rp->pressure[1]<<8) + (rp->pressure[0] <<16);
- temp_raw = (rp->temperature[2]) + (rp->temperature[1]<<8) + (rp->temperature[0] <<16);
- LOGE("press_raw %ld temp_raw %ld\r\n",press_raw,temp_raw);
- if(press_raw>8388607)
- {
- press_raw=press_raw-16777216;
- }
- Traw_sc=(double)temp_raw/KT;
- Praw_sc=(double)press_raw/KP;
- LOGE("Praw_sc %lf Traw_sc %lf\r\n",Praw_sc,Traw_sc);
- temp_final=(float)rp->calib_coeffs.C0*0.5f + (float)rp->calib_coeffs.C1*Traw_sc;
- LOGE("temp_final %f\r\n",temp_final);
- press_final=rp->calib_coeffs.C00 + Praw_sc *(rp->calib_coeffs.C10 + Praw_sc *(rp->calib_coeffs.C20+ Praw_sc *rp->calib_coeffs.C30)) + Traw_sc*rp->calib_coeffs.C01 + Traw_sc *Praw_sc *(rp->calib_coeffs.C11+Praw_sc *rp->calib_coeffs.C21);
- LOGE("press_final %f\r\n",press_final);
- temputer_valu = (long int)(temp_final*1000);
- pressure_valu = (long int)(press_final*1000);
- LOGE("temputer_valu %ld pressure_valu %ld \r\n",temputer_valu,pressure_valu);
- }
- bool hp5806_open()
- {
- hp5806_fd = open("/dev/HP5806", O_RDONLY);
- usleep(10000);
- if(hp5806_fd < 0){
- LOGE("Can not open dev %s!\n", "/dev/hp5806");
- return false;
- }
- LOGE("open dev %s! success!\n", "/dev/hp5806");
- return true;
- }
- bool hp5806_close()
- {
- int ret = 0;
- ret = close(hp5806_fd);
- return ret;
- }
- long nmh21_get_temp(void)
- {
- int ret;
- long temputer_valu = 0,temp_raw;
- double Traw_sc;
- float temp_final;
- hp5806_report_s report_hp5806;
- hp5806_open();
- ret = read(hp5806_fd,&report_hp5806,sizeof(report_hp5806));
- if(ret < 0)
- LOGE("read hp5806_fd fail!\n");
- else{
- //LOGE("report_hp5806.pressure 0x%x 0x%x 0x%x!\n",report_hp5806.pressure[0],report_hp5806.pressure[1],report_hp5806.pressure[2]);
- LOGE("report_hp5806.temperature 0x%x 0x%x 0x%x!\n",report_hp5806.temperature[0],report_hp5806.temperature[1],report_hp5806.temperature[2]);
- temp_raw = (report_hp5806.temperature[2]) + (report_hp5806.temperature[1]<<8) + (report_hp5806.temperature[0] <<16);
- if(temp_raw>8388607)
- {
- temp_raw=temp_raw-16777216;
- }
- Traw_sc=(double)temp_raw/KT;
- temp_final=(float)report_hp5806.calib_coeffs.C0*0.5f + report_hp5806.calib_coeffs.C1*Traw_sc;
- temputer_valu = (long)(temp_final * 1000);
- LOGE("temp_raw=%ld , Traw_sc=%lf, temp_final=%f temputer_valu=%ld\r\n",temp_raw,Traw_sc,temp_final,temputer_valu);
- }
- hp5806_close();
- return temputer_valu;
- }
- long nmh21_get_pressure(void)
- {
- int ret;
- long pressure_valu = 0,press_raw,temp_raw;
- double Praw_sc,Traw_sc;
- float press_final;
- hp5806_report_s report_hp5806;
- hp5806_open();
- ret = read(hp5806_fd,&report_hp5806,sizeof(report_hp5806));
- if(ret < 0)
- LOGE("read hp5806_fd fail!\n");
- else{
- press_raw = (report_hp5806.pressure[2]) + (report_hp5806.pressure[1]<<8) + (report_hp5806.pressure[0] <<16);
- LOGE("report_hp5806.pressure 0x%x 0x%x 0x%x!\n",report_hp5806.pressure[0],report_hp5806.pressure[1],report_hp5806.pressure[2]);
- temp_raw = (report_hp5806.temperature[2]) + (report_hp5806.temperature[1]<<8) + (report_hp5806.temperature[0] <<16);
- LOGE("report_hp5806.temperature 0x%x 0x%x 0x%x!\n",report_hp5806.temperature[0],report_hp5806.temperature[1],report_hp5806.temperature[2]);
- if(temp_raw>8388607)
- {
- temp_raw=temp_raw-16777216;
- }
- Traw_sc=(double)temp_raw/KT;
- if(press_raw>8388607)
- {
- press_raw=press_raw-16777216;
- }
- Praw_sc=(double)press_raw/KP;
- press_final=report_hp5806.calib_coeffs.C00 + Praw_sc *(report_hp5806.calib_coeffs.C10 + Praw_sc *(report_hp5806.calib_coeffs.C20+ Praw_sc *report_hp5806.calib_coeffs.C30)) + Traw_sc*report_hp5806.calib_coeffs.C01 + Traw_sc *Praw_sc *(report_hp5806.calib_coeffs.C11+Praw_sc *report_hp5806.calib_coeffs.C21);
- pressure_valu = (long)(press_final * 1000);
- LOGE("temp_raw=%ld , Traw_sc=%lf, press_final=%f pressure_valu=%ld\r\n",temp_raw,Traw_sc,press_final,pressure_valu);
- }
- hp5806_close();
- return pressure_valu;
- }
- float hp5806_altitude_convert(float Press, float Ref_P)
- {
- return 44330 * (1 - powf(((float)Press / (float)Ref_P),(1/5.255)));
- }
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|