From 2d09c07225eb44e8371563bca98217885d7cd7c7 Mon Sep 17 00:00:00 2001 From: Andrew Dodd Date: Mon, 30 Jul 2012 18:44:58 -0400 Subject: n7000: Add pressure sensor to libsensors Pressure sensor driver pulled from tuna Change-Id: I4da058a1c779f4ab73bc8c03fcf1ff836ed105f6 --- libsensors/Android.mk | 1 + libsensors/PressureSensor.cpp | 142 ++++++++++++++++++++++++++++++++++++++++++ libsensors/PressureSensor.h | 54 ++++++++++++++++ libsensors/sensors.cpp | 15 +++++ libsensors/sensors.h | 2 + 5 files changed, 214 insertions(+) create mode 100644 libsensors/PressureSensor.cpp create mode 100644 libsensors/PressureSensor.h diff --git a/libsensors/Android.mk b/libsensors/Android.mk index 15c29a2..9f0d998 100644 --- a/libsensors/Android.mk +++ b/libsensors/Android.mk @@ -35,6 +35,7 @@ LOCAL_SRC_FILES := \ ProximitySensor.cpp \ AkmSensor.cpp \ GyroSensor.cpp \ + PressureSensor.cpp \ InputEventReader.cpp LOCAL_SHARED_LIBRARIES := liblog libcutils libdl diff --git a/libsensors/PressureSensor.cpp b/libsensors/PressureSensor.cpp new file mode 100644 index 0000000..5e9f86e --- /dev/null +++ b/libsensors/PressureSensor.cpp @@ -0,0 +1,142 @@ +/* + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "PressureSensor.h" + +/* + * The BMP driver gives pascal values. + * It needs to be changed into hectoPascal + */ +#define PRESSURE_HECTO (1.0f/100.0f) + +/*****************************************************************************/ + +PressureSensor::PressureSensor() + : SensorBase(NULL, "barometer_sensor"), + mEnabled(0), + mInputReader(4), + mHasPendingEvent(false) +{ + mPendingEvent.version = sizeof(sensors_event_t); + mPendingEvent.sensor = ID_PR; + mPendingEvent.type = SENSOR_TYPE_PRESSURE; + memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data)); + + if (data_fd) { + strcpy(input_sysfs_path, "/sys/class/input/"); + strcat(input_sysfs_path, input_name); + strcat(input_sysfs_path, "/device/"); + input_sysfs_path_len = strlen(input_sysfs_path); + enable(0, 1); + } +} + +PressureSensor::~PressureSensor() { + if (mEnabled) { + enable(0, 0); + } +} + +int PressureSensor::setInitialState() { + struct input_absinfo absinfo; + if (!ioctl(data_fd, EVIOCGABS(EVENT_TYPE_PRESSURE), &absinfo)) { + // make sure to report an event immediately + mHasPendingEvent = true; + mPendingEvent.pressure = absinfo.value * PRESSURE_HECTO; + } + return 0; +} + +int PressureSensor::enable(int32_t, int en) { + int flags = en ? 1 : 0; + if (flags != mEnabled) { + int fd; + strcpy(&input_sysfs_path[input_sysfs_path_len], "enable"); + fd = open(input_sysfs_path, O_RDWR); + if (fd >= 0) { + char buf[2]; + buf[1] = 0; + if (flags) { + buf[0] = '1'; + } else { + buf[0] = '0'; + } + write(fd, buf, sizeof(buf)); + close(fd); + mEnabled = flags; + setInitialState(); + return 0; + } + return -1; + } + return 0; +} + +bool PressureSensor::hasPendingEvents() const { + return mHasPendingEvent; +} + +int PressureSensor::readEvents(sensors_event_t* data, int count) +{ + if (count < 1) + return -EINVAL; + + if (mHasPendingEvent) { + mHasPendingEvent = false; + mPendingEvent.timestamp = getTimestamp(); + *data = mPendingEvent; + return mEnabled ? 1 : 0; + } + + ssize_t n = mInputReader.fill(data_fd); + if (n < 0) + return n; + + int numEventReceived = 0; + input_event const* event; + + while (count && mInputReader.readEvent(&event)) { + int type = event->type; + if (type == EV_ABS) { + if (event->code == EVENT_TYPE_PRESSURE) { + mPendingEvent.pressure = event->value * PRESSURE_HECTO; + } + } else if (type == EV_SYN) { + mPendingEvent.timestamp = timevalToNano(event->time); + if (mEnabled) { + *data++ = mPendingEvent; + count--; + numEventReceived++; + } + } else { + LOGE("PressureSensor: unknown event (type=%d, code=%d)", + type, event->code); + } + mInputReader.next(); + } + + return numEventReceived; +} diff --git a/libsensors/PressureSensor.h b/libsensors/PressureSensor.h new file mode 100644 index 0000000..ddaaee2 --- /dev/null +++ b/libsensors/PressureSensor.h @@ -0,0 +1,54 @@ +/* + * 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. + */ + +#ifndef ANDROID_PRESSURE_SENSOR_H +#define ANDROID_PRESSURE_SENSOR_H + +#include +#include +#include +#include + +#include "sensors.h" +#include "SensorBase.h" +#include "InputEventReader.h" + +/*****************************************************************************/ + +struct input_event; + +class PressureSensor : public SensorBase { + int mEnabled; + InputEventCircularReader mInputReader; + sensors_event_t mPendingEvent; + bool mHasPendingEvent; + char input_sysfs_path[PATH_MAX]; + int input_sysfs_path_len; + + int setInitialState(); + float indexToValue(size_t index) const; + +public: + PressureSensor(); + virtual ~PressureSensor(); + virtual int readEvents(sensors_event_t* data, int count); + virtual bool hasPendingEvents() const; + virtual int enable(int32_t handle, int enabled); +}; + +/*****************************************************************************/ + +#endif // ANDROID_PRESSURE_SENSOR_H diff --git a/libsensors/sensors.cpp b/libsensors/sensors.cpp index 24ce66a..3ec1390 100644 --- a/libsensors/sensors.cpp +++ b/libsensors/sensors.cpp @@ -36,6 +36,7 @@ #include "ProximitySensor.h" #include "AkmSensor.h" #include "GyroSensor.h" +#include "PressureSensor.h" /*****************************************************************************/ @@ -50,6 +51,7 @@ #define SENSORS_LIGHT (1<getFd(); + mPollFds[pressure].events = POLLIN; + mPollFds[pressure].revents = 0; + int wakeFds[2]; int result = pipe(wakeFds); LOGE_IF(result<0, "error creating wake pipe (%s)", strerror(errno)); diff --git a/libsensors/sensors.h b/libsensors/sensors.h index 5eb6291..1cfca89 100644 --- a/libsensors/sensors.h +++ b/libsensors/sensors.h @@ -39,6 +39,7 @@ __BEGIN_DECLS #define ID_L (3) #define ID_P (4) #define ID_GY (5) +#define ID_PR (6) /*****************************************************************************/ @@ -72,6 +73,7 @@ __BEGIN_DECLS #define EVENT_TYPE_TEMPERATURE ABS_THROTTLE #define EVENT_TYPE_STEP_COUNT ABS_GAS #define EVENT_TYPE_PROXIMITY ABS_DISTANCE +#define EVENT_TYPE_PRESSURE ABS_PRESSURE #define EVENT_TYPE_LIGHT ABS_MISC #define EVENT_TYPE_GYRO_X REL_RY -- cgit v1.1