2010-07-09 01:44:54 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-12-16 23:17:44 +01:00
|
|
|
#include <inttypes.h>
|
2010-08-25 02:57:25 +02:00
|
|
|
#include <string.h>
|
2010-07-09 01:44:54 +02:00
|
|
|
#include <stdint.h>
|
2017-04-11 17:56:09 +02:00
|
|
|
#include <stdio.h>
|
2010-08-19 19:21:01 +02:00
|
|
|
#include <string.h>
|
2010-07-09 01:44:54 +02:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2017-04-11 17:56:09 +02:00
|
|
|
#include <log/log.h>
|
|
|
|
#include <utils/Timers.h>
|
2010-07-09 01:44:54 +02:00
|
|
|
|
|
|
|
#include <hardware/sensors.h>
|
|
|
|
|
|
|
|
char const* getSensorName(int type) {
|
|
|
|
switch(type) {
|
|
|
|
case SENSOR_TYPE_ACCELEROMETER:
|
|
|
|
return "Acc";
|
|
|
|
case SENSOR_TYPE_MAGNETIC_FIELD:
|
|
|
|
return "Mag";
|
|
|
|
case SENSOR_TYPE_ORIENTATION:
|
|
|
|
return "Ori";
|
2011-09-19 00:21:33 +02:00
|
|
|
case SENSOR_TYPE_GYROSCOPE:
|
|
|
|
return "Gyr";
|
2010-07-09 01:44:54 +02:00
|
|
|
case SENSOR_TYPE_LIGHT:
|
|
|
|
return "Lux";
|
2011-09-19 00:21:33 +02:00
|
|
|
case SENSOR_TYPE_PRESSURE:
|
|
|
|
return "Bar";
|
|
|
|
case SENSOR_TYPE_TEMPERATURE:
|
|
|
|
return "Tmp";
|
|
|
|
case SENSOR_TYPE_PROXIMITY:
|
|
|
|
return "Prx";
|
|
|
|
case SENSOR_TYPE_GRAVITY:
|
|
|
|
return "Grv";
|
|
|
|
case SENSOR_TYPE_LINEAR_ACCELERATION:
|
|
|
|
return "Lac";
|
|
|
|
case SENSOR_TYPE_ROTATION_VECTOR:
|
|
|
|
return "Rot";
|
|
|
|
case SENSOR_TYPE_RELATIVE_HUMIDITY:
|
|
|
|
return "Hum";
|
|
|
|
case SENSOR_TYPE_AMBIENT_TEMPERATURE:
|
|
|
|
return "Tam";
|
2010-07-09 01:44:54 +02:00
|
|
|
}
|
|
|
|
return "ukn";
|
|
|
|
}
|
|
|
|
|
2015-12-16 23:17:44 +01:00
|
|
|
int main(int /* argc */, char** /* argv */)
|
2010-07-09 01:44:54 +02:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct sensors_poll_device_t* device;
|
|
|
|
struct sensors_module_t* module;
|
|
|
|
|
|
|
|
err = hw_get_module(SENSORS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
|
|
|
|
if (err != 0) {
|
|
|
|
printf("hw_get_module() failed (%s)\n", strerror(-err));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-19 00:21:33 +02:00
|
|
|
err = sensors_open(&module->common, &device);
|
|
|
|
if (err != 0) {
|
|
|
|
printf("sensors_open() failed (%s)\n", strerror(-err));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-09 01:44:54 +02:00
|
|
|
struct sensor_t const* list;
|
|
|
|
int count = module->get_sensors_list(module, &list);
|
2011-09-19 00:21:33 +02:00
|
|
|
printf("%d sensors found:\n", count);
|
2010-07-09 01:44:54 +02:00
|
|
|
for (int i=0 ; i<count ; i++) {
|
|
|
|
printf("%s\n"
|
|
|
|
"\tvendor: %s\n"
|
|
|
|
"\tversion: %d\n"
|
|
|
|
"\thandle: %d\n"
|
|
|
|
"\ttype: %d\n"
|
|
|
|
"\tmaxRange: %f\n"
|
|
|
|
"\tresolution: %f\n"
|
|
|
|
"\tpower: %f mA\n",
|
|
|
|
list[i].name,
|
|
|
|
list[i].vendor,
|
|
|
|
list[i].version,
|
|
|
|
list[i].handle,
|
|
|
|
list[i].type,
|
|
|
|
list[i].maxRange,
|
|
|
|
list[i].resolution,
|
|
|
|
list[i].power);
|
|
|
|
}
|
|
|
|
|
2011-09-19 00:21:33 +02:00
|
|
|
static const size_t numEvents = 16;
|
|
|
|
sensors_event_t buffer[numEvents];
|
2010-07-09 01:44:54 +02:00
|
|
|
|
2010-07-21 03:01:19 +02:00
|
|
|
for (int i=0 ; i<count ; i++) {
|
|
|
|
err = device->activate(device, list[i].handle, 0);
|
|
|
|
if (err != 0) {
|
|
|
|
printf("deactivate() for '%s'failed (%s)\n",
|
|
|
|
list[i].name, strerror(-err));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-09 01:44:54 +02:00
|
|
|
for (int i=0 ; i<count ; i++) {
|
|
|
|
err = device->activate(device, list[i].handle, 1);
|
|
|
|
if (err != 0) {
|
|
|
|
printf("activate() for '%s'failed (%s)\n",
|
|
|
|
list[i].name, strerror(-err));
|
|
|
|
return 0;
|
|
|
|
}
|
2011-09-19 00:21:33 +02:00
|
|
|
device->setDelay(device, list[i].handle, ms2ns(10));
|
2010-07-09 01:44:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2011-09-19 00:21:33 +02:00
|
|
|
int n = device->poll(device, buffer, numEvents);
|
2010-07-09 01:44:54 +02:00
|
|
|
if (n < 0) {
|
|
|
|
printf("poll() failed (%s)\n", strerror(-err));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("read %d events:\n", n);
|
|
|
|
for (int i=0 ; i<n ; i++) {
|
2010-07-16 03:29:03 +02:00
|
|
|
const sensors_event_t& data = buffer[i];
|
|
|
|
|
|
|
|
if (data.version != sizeof(sensors_event_t)) {
|
2015-12-16 23:17:44 +01:00
|
|
|
printf("incorrect event version (version=%d, expected=%zu",
|
2010-07-16 03:29:03 +02:00
|
|
|
data.version, sizeof(sensors_event_t));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-07-21 03:01:19 +02:00
|
|
|
switch(data.type) {
|
2010-07-09 01:44:54 +02:00
|
|
|
case SENSOR_TYPE_ACCELEROMETER:
|
|
|
|
case SENSOR_TYPE_MAGNETIC_FIELD:
|
|
|
|
case SENSOR_TYPE_ORIENTATION:
|
2011-09-19 00:21:33 +02:00
|
|
|
case SENSOR_TYPE_GYROSCOPE:
|
|
|
|
case SENSOR_TYPE_GRAVITY:
|
|
|
|
case SENSOR_TYPE_LINEAR_ACCELERATION:
|
|
|
|
case SENSOR_TYPE_ROTATION_VECTOR:
|
2015-12-16 23:17:44 +01:00
|
|
|
printf("sensor=%s, time=%" PRId64 ", value=<%5.1f,%5.1f,%5.1f>\n",
|
2010-07-21 03:01:19 +02:00
|
|
|
getSensorName(data.type),
|
2010-07-16 03:29:03 +02:00
|
|
|
data.timestamp,
|
2011-09-19 00:21:33 +02:00
|
|
|
data.data[0],
|
|
|
|
data.data[1],
|
|
|
|
data.data[2]);
|
2010-07-09 01:44:54 +02:00
|
|
|
break;
|
2011-09-19 00:21:33 +02:00
|
|
|
|
2010-07-09 01:44:54 +02:00
|
|
|
case SENSOR_TYPE_LIGHT:
|
2011-09-19 00:21:33 +02:00
|
|
|
case SENSOR_TYPE_PRESSURE:
|
|
|
|
case SENSOR_TYPE_TEMPERATURE:
|
|
|
|
case SENSOR_TYPE_PROXIMITY:
|
|
|
|
case SENSOR_TYPE_RELATIVE_HUMIDITY:
|
|
|
|
case SENSOR_TYPE_AMBIENT_TEMPERATURE:
|
2015-12-16 23:17:44 +01:00
|
|
|
printf("sensor=%s, time=%" PRId64 ", value=%f\n",
|
2010-07-21 03:01:19 +02:00
|
|
|
getSensorName(data.type),
|
2010-07-16 03:29:03 +02:00
|
|
|
data.timestamp,
|
2011-09-19 00:21:33 +02:00
|
|
|
data.data[0]);
|
2010-07-09 01:44:54 +02:00
|
|
|
break;
|
2011-09-19 00:21:33 +02:00
|
|
|
|
2010-07-09 01:44:54 +02:00
|
|
|
default:
|
2015-12-16 23:17:44 +01:00
|
|
|
printf("sensor=%d, time=% " PRId64 ", value=<%f,%f,%f, ...>\n",
|
2010-07-21 03:01:19 +02:00
|
|
|
data.type,
|
2010-07-16 03:29:03 +02:00
|
|
|
data.timestamp,
|
2011-09-19 00:21:33 +02:00
|
|
|
data.data[0],
|
|
|
|
data.data[1],
|
|
|
|
data.data[2]);
|
2010-07-09 01:44:54 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (1); // fix that
|
|
|
|
|
|
|
|
|
|
|
|
for (int i=0 ; i<count ; i++) {
|
|
|
|
err = device->activate(device, list[i].handle, 0);
|
|
|
|
if (err != 0) {
|
|
|
|
printf("deactivate() for '%s'failed (%s)\n",
|
|
|
|
list[i].name, strerror(-err));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = sensors_close(device);
|
|
|
|
if (err != 0) {
|
|
|
|
printf("sensors_close() failed (%s)\n", strerror(-err));
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|