/* * 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 #include "AkmSensor.h" #define AKMD_DEFAULT_INTERVAL 200000000 /*****************************************************************************/ AkmSensor::AkmSensor() : SensorBase(NULL, "compass"), mPendingMask(0), mInputReader(32) { for (int i=0; i 32767) mEnabled[id] = 32767; } else { (mEnabled[id])--; if (mEnabled[id] < 0) mEnabled[id] = 0; } ALOGD("AkmSensor: mEnabled[%d] = %d", id, mEnabled[id]); return err; } int AkmSensor::setDelay(int32_t handle, int64_t ns) { int id = handle2id(handle); int err = 0; char buffer[32]; int bytes; if (ns < -1 || 2147483647 < ns) { ALOGE("AkmSensor: invalid delay (%lld)", ns); return -EINVAL; } switch (id) { case Accelerometer: strcpy(&input_sysfs_path[input_sysfs_path_len], "delay_acc"); break; case MagneticField: strcpy(&input_sysfs_path[input_sysfs_path_len], "delay_mag"); break; case Orientation: strcpy(&input_sysfs_path[input_sysfs_path_len], "delay_ori"); break; default: ALOGE("AkmSensor: unknown handle (%d)", handle); return -EINVAL; } if (ns != mDelay[id]) { bytes = sprintf(buffer, "%lld", ns); err = write_sys_attribute(input_sysfs_path, buffer, bytes); if (err == 0) { mDelay[id] = ns; ALOGD("AkmSensor: set %s to %f ms.", &input_sysfs_path[input_sysfs_path_len], ns/1000000.0f); } } return err; } int64_t AkmSensor::getDelay(int32_t handle) { int id = handle2id(handle); if (id > 0) { return mDelay[id]; } else { return 0; } } int AkmSensor::getEnable(int32_t handle) { int id = handle2id(handle); if (id >= 0) { return mEnabled[id]; } else { return 0; } } int AkmSensor::readEvents(sensors_event_t* data, int count) { if (count < 1) return -EINVAL; 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) { processEvent(event->code, event->value); mInputReader.next(); } else if (type == EV_SYN) { int64_t time = timevalToNano(event->time); for (int j=0 ; count && mPendingMask && jcode); mInputReader.next(); } } return numEventReceived; } int AkmSensor::setAccel(sensors_event_t* data) { int err; int16_t acc[3]; acc[0] = (int16_t)(data->acceleration.x / GRAVITY_EARTH * AKSC_LSG); acc[1] = (int16_t)(data->acceleration.y / GRAVITY_EARTH * AKSC_LSG); acc[2] = (int16_t)(data->acceleration.z / GRAVITY_EARTH * AKSC_LSG); strcpy(&input_sysfs_path[input_sysfs_path_len], "accel"); err = write_sys_attribute(input_sysfs_path, (char*)acc, 6); if (err < 0) { ALOGD("AkmSensor: %s write failed.", &input_sysfs_path[input_sysfs_path_len]); } return err; } int AkmSensor::handle2id(int32_t handle) { switch (handle) { case ID_A: return Accelerometer; case ID_M: return MagneticField; case ID_O: return Orientation; default: ALOGE("AkmSensor: unknown handle (%d)", handle); return -EINVAL; } } void AkmSensor::processEvent(int code, int value) { switch (code) { case EVENT_TYPE_ACCEL_X: mPendingMask |= 1<