diff options
author | Andrew Dodd <atd7@cornell.edu> | 2012-07-30 18:44:58 -0400 |
---|---|---|
committer | Andrew Dodd <atd7@cornell.edu> | 2012-07-30 20:10:42 -0400 |
commit | 0db94d7207d4cd60a733a1a0036d2a7a1f8754ed (patch) | |
tree | d30677fb7766967bc7e8333e6e09f4db31dd85cf /libsensors | |
parent | 76fd79d748ed4241b356d44122efcd644facfff6 (diff) | |
download | device_samsung_n7000-0db94d7207d4cd60a733a1a0036d2a7a1f8754ed.zip device_samsung_n7000-0db94d7207d4cd60a733a1a0036d2a7a1f8754ed.tar.gz device_samsung_n7000-0db94d7207d4cd60a733a1a0036d2a7a1f8754ed.tar.bz2 |
n7000: Add pressure sensor to libsensors
Pressure sensor driver pulled from tuna
Diffstat (limited to 'libsensors')
-rw-r--r-- | libsensors/Android.mk | 1 | ||||
-rw-r--r-- | libsensors/PressureSensor.cpp | 142 | ||||
-rw-r--r-- | libsensors/PressureSensor.h | 54 | ||||
-rw-r--r-- | libsensors/sensors.cpp | 15 | ||||
-rw-r--r-- | libsensors/sensors.h | 2 |
5 files changed, 214 insertions, 0 deletions
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..9b57654 --- /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 <fcntl.h> +#include <errno.h> +#include <math.h> +#include <poll.h> +#include <unistd.h> +#include <dirent.h> +#include <sys/select.h> + +#include <cutils/log.h> + +#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 { + ALOGE("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 <stdint.h> +#include <errno.h> +#include <sys/cdefs.h> +#include <sys/types.h> + +#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 0a058f9..4e89a9d 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<<ID_L) #define SENSORS_PROXIMITY (1<<ID_P) #define SENSORS_GYROSCOPE (1<<ID_GY) +#define SENSORS_PRESSURE (1<<ID_PR) #define SENSORS_ACCELERATION_HANDLE 0 #define SENSORS_MAGNETIC_FIELD_HANDLE 1 @@ -57,6 +59,7 @@ #define SENSORS_LIGHT_HANDLE 3 #define SENSORS_PROXIMITY_HANDLE 4 #define SENSORS_GYROSCOPE_HANDLE 5 +#define SENSORS_PRESSURE_HANDLE 6 #define AKM_FTRACE 0 #define AKM_DEBUG 0 @@ -90,6 +93,10 @@ static const struct sensor_t sSensorList[] = { "STMicroelectronics", 1, SENSORS_GYROSCOPE_HANDLE, SENSOR_TYPE_GYROSCOPE, RANGE_GYRO, CONVERT_GYRO, 6.1f, 1190, { } }, + { "BMP180 Pressure sensor", + "Bosch", + 1, SENSORS_PRESSURE_HANDLE, + SENSOR_TYPE_PRESSURE, 1100.0f, 0.01f, 0.67f, 20000, { } }, }; @@ -136,6 +143,7 @@ private: proximity = 1, akm = 2, gyro = 3, + pressure = 4, numSensorDrivers, numFds, }; @@ -158,6 +166,8 @@ private: return light; case ID_GY: return gyro; + case ID_PR: + return pressure; } return -EINVAL; } @@ -187,6 +197,11 @@ sensors_poll_context_t::sensors_poll_context_t() mPollFds[gyro].events = POLLIN; mPollFds[gyro].revents = 0; + mSensors[pressure] = new PressureSensor(); + mPollFds[pressure].fd = mSensors[pressure]->getFd(); + mPollFds[pressure].events = POLLIN; + mPollFds[pressure].revents = 0; + int wakeFds[2]; int result = pipe(wakeFds); ALOGE_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 |