• Lv0
    粉丝5

积分196 / 贡献0

提问31答案被采纳6文章2

传感器sensor驱动用户端息屏后停止输出

shixiaocheng 显示全部楼层 发表于 2024-1-18 09:15:12

【问题描述】

  1. 介绍问题现象和发生的背景 移植了一个光亮度传感器,出现以下状况:(1)亮屏时开启用户端程序,可以打印当前环境亮度,但是息屏后就会停止输出,点击电源键亮屏后又会恢复;(2)息屏时开启用户端程序,可以打印当前环境亮度,亮屏后打印速度加快,息屏后又停止输出;请问这是什么原因呢?

  2. 相关的代码(请勿使用截图)

    #include <stdio.h>
    #include "hilog/log.h"  // use HILOG_INFO to print log should include this .h
    #include "sensor_if.h"
    #include "sensor_type.h"
    #include <unistd.h>
    #include <stdlib.h>
    using OHOS::HiviewDFX::HiLog;
    
    /* Create a callback. */
    int SensorDataCallback(const struct SensorEvents *event)
    {
        if(event == NULL){
            return -1;
        }
        int sensorId= event->sensorId;
        if(sensorId==5){
            uint8_t *sensorData=(uint8_t *)event->data;
            printf("als sensor sensorId[%d] data[%d], data len:%d\n",sensorId, *sensorData, event->dataLen);
        }
        if(sensorId==12){
            uint16_t *sensorData=(uint16_t *)event->data;
            printf("ps sensor sensorId[%d] data[%d], data len:%d\n",sensorId, *sensorData, event->dataLen);
            if(*sensorData > 150) system("power-shell wakeup");
        }
        if(sensorId==13){
            uint16_t *sensorData=(uint16_t *)event->data;
            printf("humidity sensor sensorId[%d] data[%d], data len:%d\n",sensorId, *sensorData, event->dataLen);
        }
        return 0;
    }
    
    int main(int argc, char* argv[])
    {
        const OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0xD001800, "URI"};
        HiLog::Info(LABEL, "sxc test success");
        int32_t ret;
        struct SensorInformation *sensorInfo = NULL;
        int32_t count = 0;
        int32_t sensorInterval = 200000000; /* Set the data sampling rate to 200000000, in the unit of nanoseconds (200 ms). */
        int sensorId = 0;
        if(argc==1)
            sensorId = 5;
        else
            sensorId = atoi(argv[1]);  // 将第一个参数转换为整数
        printf("Do not enter the default light sensor, otherwise it is a distance sensor\n");
        printf("sensorId = %d\n", sensorId);
        /* 1. Create a SensorInterface instance. */
        const struct SensorInterface *sensorDev = NewSensorInterfaceInstance();
        if (sensorDev == NULL) {
            return -1;
        }
        printf("NewSensorInterfaceInstance success\n");
         /* 2. Register a sensor data callback. */
        ret = sensorDev->Register(0, SensorDataCallback);
        if (ret != 0) {
            return -1;
        }
        printf("Register success\n");
        /* 3. Obtain the list of sensors supported by the device. */
        ret = sensorDev->GetAllSensors(&sensorInfo, &count);
        if (ret != 0) {
            return -1;
        }
        printf("GetAllSensors success\n");
        for (int i = 0; i < count; i++) {
            printf("get sensorId[%d], info name[%s]\n\r", sensorInfo[i].sensorId, sensorInfo[i].sensorName);
        }
        /* 4. Set the sensor sampling rate. */
        ret = sensorDev->SetBatch(sensorId, sensorInterval, 0);
        if (ret != 0) {
            printf("SetBatch failed!\n");
            return -1;
        }
        printf("SetBatch success,sensorId[%d]\n",sensorId);
        /* 5. Enable the sensor. */
        ret = sensorDev->Enable(sensorId);
        if (ret != 0) {
            printf("Enable failed!\n");
            return -1;
        }
        printf("Enable success,sensorId[%d]\n",sensorId);
        usleep(60*1000 * 1000);
          /* 6. Disable the sensor. */
        ret = sensorDev->Disable(sensorId);
        if (ret != 0) {
            printf("Disable failed!\n");
            return -1;
        }
        printf("Disable success,sensorId[%d]\n",sensorId);
        /* 7. Unregister the sensor data callback. */
        ret = sensorDev->Unregister(0, SensorDataCallback);
        if (ret != 0) {
            printf("Unregister failed!\n");
            return -1;
        }
        printf("Unregister success,sensorId[%d]\n",sensorId);
        /* 8. Release the SensorInterface instance.*/
        ret = FreeSensorInterfaceInstance();
        if (ret != 0) {
            return -1;
        }
        printf("FreeSensorInterfaceInstance success\n");
        return 0;
    }
  3. 运行结果、错误截图

  4. 我尝试过的解决方法和结果

  5. 我想要达到的结果 息屏或亮屏时都可以正常输出。

【运行环境】

硬件:rk3568 ROM版本:4.0 DevEvoStudio版本: SDK版本:

您尚未登录,无法参与评论,登录后可以:
参与开源共建问题交流
认同或收藏高质量问答
获取积分成为开源共建先驱

精彩评论2

深开鸿_王石

沙发 发表于 2024-1-18 11:44:20
抓个系统log看看呢,是不是电源管理那个回调线程停了

shixiaocheng

板凳 发表于 2024-1-19 09:04:22
知道原因了,文件‘//base/powermgr/display_manager/service/native/src/display_power_mgr_service.cpp’里面会对ambient sensor进行判断,息屏时关闭以省电

Copyright   ©2023  OpenHarmony开发者论坛  京ICP备2020036654号-3 |技术支持 Discuz!

返回顶部