Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(216)

Unified Diff: chromeos/accelerometer/accelerometer_reader.cc

Issue 200643005: Read and expose accelerometer values from cros-ec-accel trigger. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Virtual dtor in observer, ASH_EXPORT for tests in dependent CLs. Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chromeos/accelerometer/accelerometer_reader.h ('k') | chromeos/chromeos.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chromeos/accelerometer/accelerometer_reader.cc
diff --git a/chromeos/accelerometer/accelerometer_reader.cc b/chromeos/accelerometer/accelerometer_reader.cc
new file mode 100644
index 0000000000000000000000000000000000000000..73948b7319e0d2508559a9cced28ce11b331cefd
--- /dev/null
+++ b/chromeos/accelerometer/accelerometer_reader.cc
@@ -0,0 +1,178 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chromeos/accelerometer/accelerometer_reader.h"
+
+#include "base/bind.h"
+#include "base/file_util.h"
+#include "base/location.h"
+#include "base/message_loop/message_loop.h"
+#include "base/strings/string_number_conversions.h"
+#include "base/strings/string_util.h"
+#include "base/strings/stringprintf.h"
+#include "ui/gfx/geometry/vector3d_f.h"
+
+namespace chromeos {
+
+namespace {
+
+// Paths to access necessary data from the accelerometer device.
+const base::FilePath::CharType kAccelerometerTriggerPath[] =
+ FILE_PATH_LITERAL("/sys/bus/iio/devices/trigger0/trigger_now");
+const base::FilePath::CharType kAccelerometerDevicePath[] =
+ FILE_PATH_LITERAL("/dev/cros-ec-accel");
+const base::FilePath::CharType kAccelerometerIioBasePath[] =
+ FILE_PATH_LITERAL("/sys/bus/iio/devices/");
+
+// Files within the device in kAccelerometerIioBasePath containing the scales of
+// the accelerometers.
+const base::FilePath::CharType kAccelerometerBaseScaleName[] =
+ FILE_PATH_LITERAL("in_accel_base_scale");
+const base::FilePath::CharType kAccelerometerLidScaleName[] =
+ FILE_PATH_LITERAL("in_accel_lid_scale");
+
+// The filename giving the path to read the scan index of each accelerometer
+// axis.
+const char kAccelerometerScanIndexPath[] =
+ "scan_elements/in_accel_%s_%s_index";
+
+// The names of the accelerometers and axes in the order we want to read them.
+const char kAccelerometerNames[][5] = {"base", "lid"};
+const char kAccelerometerAxes[][2] = {"x", "y", "z"};
+const size_t kTriggerDataValues =
+ arraysize(kAccelerometerNames) * arraysize(kAccelerometerAxes);
+const size_t kTriggerDataLength = kTriggerDataValues * 2;
+
+// The length required to read uint values from configuration files.
+const size_t kMaxAsciiUintLength = 21;
+
+// The time to wait between reading the accelerometer.
+const int kDelayBetweenReadsMs = 100;
sadrul 2014/04/01 16:33:55 Would it make sense to use a base::FilePathWatcher
flackr 2014/04/01 17:10:55 It shouldn't trigger too frequently as it posts th
+
+// Reads |path| to the unsigned int pointed to by |value|. Returns true on
+// success or false on failure.
+bool ReadFileToUint(const base::FilePath& path, unsigned int* value) {
+ std::string s;
+ DCHECK(value);
+ if (!base::ReadFileToString(path, &s, kMaxAsciiUintLength)) {
+ LOG(ERROR) << "Failed to read " << path.value();
+ return false;
+ }
+ base::TrimWhitespaceASCII(s, base::TRIM_ALL, &s);
+ if (!base::StringToUint(s, value)) {
+ LOG(ERROR) << "Failed to parse \"" << s << "\" from " << path.value();
+ return false;
+ }
+ return true;
+}
+
+} // namespace
+
+AccelerometerReader::AccelerometerReader(
+ AccelerometerReader::Delegate* delegate)
+ : delegate_(delegate),
+ weak_factory_(this) {
+ DCHECK(delegate_);
+ if (Initialize())
+ TriggerRead();
+}
+
+AccelerometerReader::~AccelerometerReader() {
+}
+
+bool AccelerometerReader::Initialize() {
+ // Check for accelerometer symlink which will be created by the udev rules
+ // file on detecting the device.
+ base::FilePath device;
+ if (!base::ReadSymbolicLink(base::FilePath(kAccelerometerDevicePath),
+ &device)) {
+ return false;
+ }
+
+ if (!base::PathExists(base::FilePath(kAccelerometerTriggerPath))) {
+ LOG(ERROR) << "Accelerometer trigger does not exist at"
+ << kAccelerometerTriggerPath;
+ return false;
+ }
+
+ base::FilePath iio_path(base::FilePath(kAccelerometerIioBasePath).Append(
+ device));
+ // Read accelerometer scales
+ if (!ReadFileToUint(iio_path.Append(kAccelerometerBaseScaleName),
+ &accelerometer_base_scale_)) {
+ return false;
+ }
+ if (!ReadFileToUint(iio_path.Append(kAccelerometerLidScaleName),
+ &accelerometer_lid_scale_)) {
+ return false;
+ }
+
+ // Read indices of each accelerometer axis from each accelerometer from
+ // /sys/bus/iio/devices/iio:deviceX/scan_elements/in_accel_{x,y,z}_%s_index
+ for (size_t i = 0; i < arraysize(kAccelerometerNames); ++i) {
+ for (size_t j = 0; j < arraysize(kAccelerometerAxes); ++j) {
+ std::string accelerometer_index_path = base::StringPrintf(
+ kAccelerometerScanIndexPath, kAccelerometerAxes[j],
+ kAccelerometerNames[i]);
+ unsigned int index = 0;
+ if (!ReadFileToUint(iio_path.Append(accelerometer_index_path.c_str()),
+ &index)) {
+ return false;
+ }
+ if (index >= kTriggerDataValues) {
+ LOG(ERROR) << "Field index from " << accelerometer_index_path
+ << " out of bounds: " << index;
+ return false;
+ }
+ accelerometer_index_.push_back(index);
+ }
+ }
+ return true;
+}
+
+void AccelerometerReader::TriggerRead() {
+ ReadAccelerometer();
+
+ // Trigger another read after the current sampling delay.
+ base::MessageLoop::current()->PostDelayedTask(
+ FROM_HERE,
+ base::Bind(&AccelerometerReader::TriggerRead,
+ weak_factory_.GetWeakPtr()),
+ base::TimeDelta::FromMilliseconds(kDelayBetweenReadsMs));
+}
+
+void AccelerometerReader::ReadAccelerometer() {
+ // Initiate the trigger to read accelerometers simultaneously
+ int bytes_written = base::WriteFile(
+ base::FilePath(kAccelerometerTriggerPath), "1\n", 2);
+ if (bytes_written < 2) {
+ PLOG(ERROR) << "Accelerometer trigger failure: " << bytes_written;
+ return;
+ }
+
+ // Read resulting sample from /dev/cros-ec-accel.
+ char data[kTriggerDataLength];
+ int bytes_read = base::ReadFile(base::FilePath(kAccelerometerDevicePath),
+ data, kTriggerDataLength);
+ if (bytes_read < static_cast<int>(kTriggerDataLength)) {
+ LOG(ERROR) << "Read " << bytes_read << " byte(s), expected "
+ << kTriggerDataLength << " bytes from accelerometer";
+ return;
+ }
+
+ int16* values = reinterpret_cast<int16*>(data);
+ gfx::Vector3dF base_reading, lid_reading;
+ base_reading.set_x(values[accelerometer_index_[0]]);
+ base_reading.set_y(values[accelerometer_index_[1]]);
+ base_reading.set_z(values[accelerometer_index_[2]]);
+ base_reading.Scale(1.0f / accelerometer_base_scale_);
+
+ lid_reading.set_x(values[accelerometer_index_[3]]);
+ lid_reading.set_y(values[accelerometer_index_[4]]);
+ lid_reading.set_z(values[accelerometer_index_[5]]);
+ lid_reading.Scale(1.0f / accelerometer_lid_scale_);
+ delegate_->HandleAccelerometerReading(base_reading, lid_reading);
+}
+
+} // namespace chromeos
« no previous file with comments | « chromeos/accelerometer/accelerometer_reader.h ('k') | chromeos/chromeos.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698