自动旋转问题 sensor底层分析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lf12345678910/article/details/54603725

hardware/xxx/sensors/NativeSensormanager.cpp

const struct sensor_t NativeSensorManager::virtualSensorList [VIRTUAL_SENSOR_COUNT] = {
    [ORIENTATION] = {
        .name = virtualSensorName[ORIENTATION],
        .vendor = "oem",
        .version = 1,
        .handle = '_dmy',
        .type = SENSOR_TYPE_ORIENTATION,
        .maxRange = 360.0f,
        .resolution = 1.0f/256.0f,
        .power = 1,
        .minDelay = 20000,
        .fifoReservedEventCount = 0,
        .fifoMaxEventCount = 0,

    [ROTATION_VECTOR] = {
        .name = virtualSensorName[ROTATION_VECTOR],
        .vendor = "oem",
        .version = 1,
        .handle = '_dmy',
        .type = SENSOR_TYPE_ROTATION_VECTOR,
        .maxRange = 1,
        .resolution = 1.0f / (1<<24),
        .power = 1,
        .minDelay = 20000,

    [GAME_ROTATION_VECTOR] = {
        .name = "xxx-game-rotation-vector",
        .vendor = "xxx",
        .version = 1,
        .handle = '_dmy',
        .type = SENSOR_TYPE_GAME_ROTATION_VECTOR,
        .maxRange = 1,
        .resolution = 1.0f / (1<<24),
        .power = 1,
        .minDelay = 20000,
        .fifoReservedEventCount = 0,

xxx.c

//#include <linux/xxx.h>
#include "xxx.h"
#include <linux/sensors.h>

#define xxx_DRIVER_VERSION     "1.0.0"

#define DELAY_TIME_DEFAULT         50   //ms, 20Hz
#define DELAY_TIME_MIN             20   //ms, 100Hz
#define DELAY_TIME_MAX            1000   //ms, 10Hz
/*
static float transfer_table[31]=

static int xxx_mag_get_data(struct xxx_data *mag)
{
    int err;
    unsigned char  data[6];
    int i;
    int xyz[3];
    ktime_t timestamp;
    static ktime_t last_ts;
    data[0] = 0x03;
    err = VTC_i2c_Rx(data, 6);
    if(err < 0) goto err_i2c_fail;

    timestamp = ktime_get_boottime();

    //next read
    data[0] = 0x0A;
    data[1] = 0x01;
    err = VTC_i2c_Tx(data, 2);
    if(err < 0) goto err_i2c_fail;
    if(mag->pdata->poll_interval == 20)
    {
        if((ktime_to_ns(timestamp) - ktime_to_ns(last_ts) > 36000000L) && (ktime_to_ns(timestamp) - ktime_to_ns(last_ts) < 100000000L))
        {
            //printk("new %lx, old %lx\n", ktime_to_ns(ts), ktime_to_ns(last_ts) );
            ktime_to_ns(timestamp) = ktime_to_ns(last_ts) + 36000000L;
        }
    }
    
    ktime_to_ns(last_ts) = ktime_to_ns(timestamp);
    input_report_abs(mag->input, ABS_X, xyz[0]);
    input_report_abs(mag->input, ABS_Y, xyz[1]);
    input_report_abs(mag->input, ABS_Z, xyz[2]);
    input_event(mag->input, EV_SYN, SYN_TIME_SEC, ktime_to_timespec(timestamp).tv_sec);
    input_event(mag->input, EV_SYN, SYN_TIME_NSEC, ktime_to_timespec(timestamp).tv_nsec);
    input_sync(mag->input);

    return 0;

xxx.c

#define XXX_PCODE_RESERVE_10    0x99

//=============================================================================
#define XXX_I2C_NAME            SENSOR_NAME
#define SENSOR_DEV_COUNT           1
#define SENSOR_DURATION_MAX        1000
#define SENSOR_DURATION_MIN        10
#define SENSOR_DURATION_DEFAULT    100

#define INPUT_FUZZ    0
#define INPUT_FLAT    0

//=============================================================================
static void xxx_work_func(struct work_struct *work)
{
    struct xxx_data *data = container_of(work, struct xxx_data, work);
    struct acceleration accel = { 0 };
    static ktime_t last_ts;
    static struct acceleration last_accel = {0};
    ktime_t ts;
    xxx_measure(data->client, &accel);
    if(last_accel.x == accel.x && last_accel.y == accel.y && last_accel.z == accel.z)
    {
        accel.x ++ ;
        accel.y ++ ;
        accel.z ++ ;
    }

    last_accel.x = accel.x;
    last_accel.y = accel.y;
    last_accel.z = accel.z;

    ts = ktime_get_boottime();
    
    if(sensor_duration == 10)
    {
        if((ktime_to_ns(ts) - ktime_to_ns(last_ts) > 18000000L) && (ktime_to_ns(ts) - ktime_to_ns(last_ts) < 100000000L))
        {
            //printk("new %lx, old %lx\n", ktime_to_ns(ts), ktime_to_ns(last_ts) );
            ktime_to_ns(ts) = ktime_to_ns(last_ts) + 18000000L;
        }
    }
    
    ktime_to_ns(last_ts) = ktime_to_ns(ts);
    input_report_abs(data->input_dev, ABS_X, accel.x);
    input_report_abs(data->input_dev, ABS_Y, accel.y);
    input_report_abs(data->input_dev, ABS_Z, accel.z);
    input_event(data->input_dev, EV_SYN, SYN_TIME_SEC,
            ktime_to_timespec(ts).tv_sec);
    input_event(data->input_dev, EV_SYN, SYN_TIME_NSEC,
            ktime_to_timespec(ts).tv_nsec);
    input_sync(data->input_dev);
}

猜你喜欢

转载自blog.csdn.net/lf12345678910/article/details/54603725
今日推荐