RK Android7.1 G-sensor 调试

G-sensor,全称为Accelerometer-sensor,是一种加速度传感器
G-sensor能够感知到加速力的变化,加速力就是当物体在加速过程中作用在物体上的力,
比如晃动、跌落、上升、下降等各种移动变化都能被G-sensor转化为电信号

一. OS Android5.1  Kernel 3.10

dts 配置

	sensor@18 {
			compatible = "stk8baxx_acc";
			reg = <0x18>;
			type = <SENSOR_TYPE_ACCEL>;
			irq-gpio = <&gpio0 GPIO_A1 IRQ_TYPE_EDGE_FALLING>;
			irq_enable = <0>;
			poll_delay_ms = <30>;
			layout = <1>;//4
	};  

1.1.正常加载

正常的加载 sensor
<4>[    2.978067] sensor_chip_init:stk8baxx_acc:devid=0x0,ops=0xc0e00474
<6>[    2.998169] input: stk8baxx_attr as /devices/virtual/input/input2
<6>[    2.998320] input: gsensor as /devices/ff160000.i2c/i2c-4/4-0018/input/input3
<4>[    2.998441] sensor_misc_device_register:miscdevice: mma8452_daemon
<4>[    2.998449] sensor_probe:initialized ok,sensor name:stk8baxx_acc,type:2,id=27
<4>[    2.998449]
<6>[    2.998478] sensor_init: Probe name sensors

错误的加载 sensor
<4>[    2.646424] sensor_chip_init:gs_stk8312:devid=0x0,ops=0xc0bf44b0
<4>[    2.646944] sensor_initial:fail to init sensor
<4>[    2.646952] sensor_chip_init:fail to init sensor
<3>[    2.646960] i2c i2c-4: sensor_probe failed -11
<3>[    2.646960]
<4>[    2.646975] sensors: probe of 4-003d failed with error -11
<6>[    2.646994] sensor_init: Probe name sensors
<4>[    2.647010] sensor-dev.c v1.4 add angle calculation support between two gsensors 2013-09-01

1.2.getevent  查看数据读取

getevent -p
add device 1: /dev/input/event5
  name:     "PixArt USB Optical Mouse"
  events:
    KEY (0001): 0110  0111  0112
    REL (0002): 0000  0001  0008
    MSC (0004): 0004
  input props:
    <none>
add device 2: /dev/input/event2
  name:     "stk8baxx_attr"
  events:
  input props:
    <none>
add device 3: /dev/input/event3
  name:     "gsensor"
  events:
    ABS (0003): 0000  : value 62496, min -2000000, max 2000000, fuzz 0, flat 0, resolution 0
                0001  : value -7812, min -2000000, max 2000000, fuzz 0, flat 0, resolution 0
                0002  : value 941346, min -2000000, max 2000000, fuzz 0, flat 0, resolution 0
  input props:
    <none>
add device 4: /dev/input/event1
  name:     "rk29-keypad"
  events:
    KEY (0001): 0072  0073  0074  008f
  input props:
    <none>
add device 5: /dev/input/event4
  name:     "rockchip_headset.32"
  events:
    KEY (0001): 00e2
  input props:
    <none>
add device 6: /dev/input/event0
  name:     "rkxx-remotectl"
  events:
    KEY (0001): 0002  0003  0004  0005  0006  0007  0008  0009
                000a  000b  001c  0040  0042  0058  0066  0067
                0069  006a  006c  0071  0072  0073  0074  008b
                008f  0092  009e  00a3  00a5  00ac  00b7  00b9
                00ba  00bb  00bc  00d9  00e8  00f8  0175  0183
                0184  0190  0191  0192
  input props:
    <none>

1.3.android   SensorManager onSensorChanged

https://www.runoob.com/w3cnote/android-tutorial-sensor3.html 

 

二.OS Android7.1.2  Kernel 4.4.143

2.1.driver/input/sensors/accel/目录下增加相应驱动
struct sensor_operate gsensor_mpu6500_ops = {
	.name				= "mpu6500_acc",//与 sensor-dev.c 中定义的名称一致
	.type				= SENSOR_TYPE_ACCEL,//sensor 类型是 gsensor
	.id_i2c				= ACCEL_ID_MPU6500,//定义在include/linux/sensor-dev.h 中
	.read_reg				= MPU6500_ACCEL_XOUT_H,//读取 gsensor 数据的起始寄存器地址
	.read_len				= 6,//需要读取的 gsensor 数据的字节数
	.id_reg				= MPU6500_WHOAMI,//芯片唯一 ID 寄存器
	.id_data 				= MPU6500_DEVICE_ID,//芯片 ID 值
	.precision				= MPU6500_PRECISION,//采样 gsensor 数据的adc 位数
	.ctrl_reg 				= MPU6500_PWR_MGMT_2,//用于使能 gsensor 的寄存器地址
	.int_status_reg 		= MPU6500_INT_STATUS,//中断状态寄存器地址
	.range				= {-32768, 32768},//量程,这里表示上报的量程为+-2G
	.trig					= IRQF_TRIGGER_HIGH |IRQF_ONESHOT,//中断类型
	.active				= sensor_active,//用于开关 gsensor
	.init					= sensor_init,//用于初始化 gsensor
	.report 				= sensor_report_value,//用于上报gsensor数据
};

2.2.Android 中的 sensor 相关宏配置

BoardConfig.mk 中
# Sensors
BOARD_SENSOR_ST := true //采用 RK 的 sensors Hal,也就是本文介绍的
BOARD_SENSOR_MPU_PAD := false //仅适用 MPU6500、mpu6050 等芯片
支持哪些类型的 sensor,如果没有,要配置成 false,否则 vts 和 cts 测试会失败:
BOARD_GRAVITY_SENSOR_SUPPORT := true
BOARD_COMPASS_SENSOR_SUPPORT := false
BOARD_GYROSCOPE_SENSOR_SUPPORT := false
BOARD_PROXIMITY_SENSOR_SUPPORT := false
BOARD_LIGHT_SENSOR_SUPPORT := true
BOARD_PRESSURE_SENSOR_SUPPORT := false
BOARD_TEMPERATURE_SENSOR_SUPPORT := false

2.3.Rockchip sensors hal 

代码路径:hardware/rockchip/sensor/st 支持最新的 SENSORS_DEVICE_API_VERSION_1_3 android sensors hal API 接口 
其实现的接口包括:
get_sensors_list - 返回所有传感器的列表
activate - 启动或停止传感器
batch - 设置传感器的参数,如采样率和最大报告延迟
setDelay - 仅用于 1.0 版本的 HAL,设置指定传感器的采样率
flush - 刷写指定传感器的 FIFO ,并在完成后报告刷写完成事件
poll - 返回可用的传感器事件

有意思的是 需要
mmm hardware/rockchip/sensor/st/
单独烧制内核getevnt没有出现数据的同学试试这个 还有一件事记得把 无障碍 中的自动旋转屏幕打开

 

二.G光感测试Demo

package com.gatsby.light;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.DataOutputStream;

public class MainActivity extends AppCompatActivity {
    private SensorManager sm;
    private TextView light_tv;
    private Sensor ligthSensor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        light_tv = (TextView) findViewById(R.id.light_tv);

        RootCommand("settings put system screen_brightness_mode 1");

        sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        ligthSensor = sm.getDefaultSensor(Sensor.TYPE_LIGHT);
        sm.registerListener(listener, ligthSensor, SensorManager.SENSOR_DELAY_NORMAL);

    }

    private SensorEventListener listener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent sensorEvent) {
            //获取当前光照强度
            //Toast.makeText(MainActivity.this,"当前光照强度:" + sensorEvent.values[0] + "勒克斯", Toast.LENGTH_LONG).show();
            float lux = sensorEvent.values[0];
            showData("光线强度--->" + lux, light_tv);
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };

    private void showData(final String str, TextView tv) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tv.append(str + "\n");
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //传感器使用完毕,释放资源
        if (sm != null) {
            sm.unregisterListener(listener);
        }
    }
}

  

 

 那一日我遇到了自己 开始流泪
 假若他日相逢 我将何以贺你 以流泪 以沉默     

                1640859234000

  

posted @ 2021-03-13 11:59  CrushGirl  阅读(1822)  评论(0编辑  收藏  举报