diff options
author | Andrew Dodd <atd7@cornell.edu> | 2012-07-30 18:44:58 -0400 |
---|---|---|
committer | Andrew Dodd <atd7@cornell.edu> | 2012-08-15 23:11:21 -0400 |
commit | 2d09c07225eb44e8371563bca98217885d7cd7c7 (patch) | |
tree | 6d73980538491eb91d43a34f8040df7ec2751685 /libsensors/PressureSensor.cpp | |
parent | 90779d4af0d22c12ca3502b9e6a126d5db8d9e9a (diff) | |
download | device_samsung_n7000-2d09c07225eb44e8371563bca98217885d7cd7c7.zip device_samsung_n7000-2d09c07225eb44e8371563bca98217885d7cd7c7.tar.gz device_samsung_n7000-2d09c07225eb44e8371563bca98217885d7cd7c7.tar.bz2 |
n7000: Add pressure sensor to libsensors
Pressure sensor driver pulled from tuna
Change-Id: I4da058a1c779f4ab73bc8c03fcf1ff836ed105f6
Diffstat (limited to 'libsensors/PressureSensor.cpp')
-rw-r--r-- | libsensors/PressureSensor.cpp | 142 |
1 files changed, 142 insertions, 0 deletions
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 <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 { + LOGE("PressureSensor: unknown event (type=%d, code=%d)", + type, event->code); + } + mInputReader.next(); + } + + return numEventReceived; +} |