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

Side by Side 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: Address review comments. 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chromeos/accelerometer/accelerometer_reader.h ('k') | chromeos/chromeos.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chromeos/accelerometer/accelerometer_reader.h"
6
7 #include "base/bind.h"
8 #include "base/file_util.h"
9 #include "base/location.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/task_runner_util.h"
15 #include "base/threading/sequenced_worker_pool.h"
16 #include "content/public/browser/browser_thread.h"
17
18 namespace chromeos {
19
20 namespace {
21
22 // Paths to access necessary data from the accelerometer device.
23 const base::FilePath::CharType kAccelerometerTriggerPath[] =
24 FILE_PATH_LITERAL("/sys/bus/iio/devices/trigger0/trigger_now");
25 const base::FilePath::CharType kAccelerometerDevicePath[] =
26 FILE_PATH_LITERAL("/dev/cros-ec-accel");
27 const base::FilePath::CharType kAccelerometerIioBasePath[] =
28 FILE_PATH_LITERAL("/sys/bus/iio/devices/");
29
30 // Files within the device in kAccelerometerIioBasePath containing the scales of
31 // the accelerometers.
32 const base::FilePath::CharType kAccelerometerBaseScaleName[] =
33 FILE_PATH_LITERAL("in_accel_base_scale");
34 const base::FilePath::CharType kAccelerometerLidScaleName[] =
35 FILE_PATH_LITERAL("in_accel_lid_scale");
36
37 // The filename giving the path to read the scan index of each accelerometer
38 // axis.
39 const char kAccelerometerScanIndexPath[] =
40 "scan_elements/in_accel_%s_%s_index";
41
42 // The names of the accelerometers and axes in the order we want to read them.
43 const char kAccelerometerNames[][5] = {"base", "lid"};
44 const char kAccelerometerAxes[][2] = {"x", "y", "z"};
45 const size_t kTriggerDataValues =
46 arraysize(kAccelerometerNames) * arraysize(kAccelerometerAxes);
47 const size_t kTriggerDataLength = kTriggerDataValues * 2;
48
49 // The length required to read uint values from configuration files.
50 const size_t kMaxAsciiUintLength = 21;
51
52 // The time to wait between reading the accelerometer.
53 const int kDelayBetweenReadsMs = 100;
54
55 // Reads |path| to the unsigned int pointed to by |value|. Returns true on
56 // success or false on failure.
57 bool ReadFileToUint(const base::FilePath& path, unsigned int* value) {
58 std::string s;
59 DCHECK(value);
60 if (!base::ReadFileToString(path, &s, kMaxAsciiUintLength)) {
61 LOG(ERROR) << "Failed to read " << path.value();
62 return false;
63 }
64 base::TrimWhitespaceASCII(s, base::TRIM_ALL, &s);
65 if (!base::StringToUint(s, value)) {
66 LOG(ERROR) << "Failed to parse \"" << s << "\" from " << path.value();
67 return false;
68 }
69 return true;
70 }
71
72 } // namespace
73
74 using content::BrowserThread;
75
76 AccelerometerReader::AccelerometerReader(
77 AccelerometerReader::Delegate* delegate)
78 : delegate_(delegate),
79 has_accelerometer_(false),
80 weak_factory_(this) {
81 // Asynchronously detect and initialize the accelerometer to avoid delaying
82 // startup.
83 BrowserThread::PostBlockingPoolTaskAndReply(FROM_HERE,
84 base::Bind(&AccelerometerReader::Initialize, weak_factory_.GetWeakPtr()),
85 base::Bind(&AccelerometerReader::OnInitialized,
86 weak_factory_.GetWeakPtr()));
87 }
88
89 AccelerometerReader::~AccelerometerReader() {
90 }
91
92 void AccelerometerReader::Initialize() {
93 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
94
95 // Check for accelerometer symlink which will be created by the udev rules
96 // file on detecting the device.
97 base::FilePath device;
98 if (!base::ReadSymbolicLink(base::FilePath(kAccelerometerDevicePath),
99 &device)) {
100 return;
101 }
102
103 if (!base::PathExists(base::FilePath(kAccelerometerTriggerPath))) {
104 LOG(ERROR) << "Accelerometer trigger does not exist at"
105 << kAccelerometerTriggerPath;
106 return;
107 }
108
109 base::FilePath iio_path(base::FilePath(kAccelerometerIioBasePath).Append(
110 device));
111 // Read accelerometer scales
112 if (!ReadFileToUint(iio_path.Append(kAccelerometerBaseScaleName),
113 &accelerometer_base_scale_)) {
114 return;
115 }
116 if (!ReadFileToUint(iio_path.Append(kAccelerometerLidScaleName),
117 &accelerometer_lid_scale_)) {
118 return;
119 }
120
121 // Read indices of each accelerometer axis from each accelerometer from
122 // /sys/bus/iio/devices/iio:deviceX/scan_elements/in_accel_{x,y,z}_%s_index
123 for (size_t i = 0; i < arraysize(kAccelerometerNames); ++i) {
124 for (size_t j = 0; j < arraysize(kAccelerometerAxes); ++j) {
125 std::string accelerometer_index_path = base::StringPrintf(
126 kAccelerometerScanIndexPath, kAccelerometerAxes[j],
127 kAccelerometerNames[i]);
128 unsigned int index = 0;
129 if (!ReadFileToUint(iio_path.Append(accelerometer_index_path.c_str()),
130 &index)) {
131 return;
132 }
133 CHECK(index < kTriggerDataValues);
Daniel Erat 2014/03/27 15:37:52 CHECK_LT() is better (as it'll log both values), b
flackr 2014/03/27 16:14:56 Done. Decided to go with bailing out.
134 accelerometer_index_.push_back(index);
135 }
136 }
137 has_accelerometer_ = true;
138 }
139
140 void AccelerometerReader::OnInitialized() {
141 if (has_accelerometer_)
142 TriggerRead();
143 }
144
145 void AccelerometerReader::TriggerRead() {
146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
147 read_success_ = false;
148 BrowserThread::PostBlockingPoolTaskAndReply(FROM_HERE,
149 base::Bind(&AccelerometerReader::ReadAccelerometer,
150 weak_factory_.GetWeakPtr()),
151 base::Bind(&AccelerometerReader::OnDataRead,
152 weak_factory_.GetWeakPtr()));
153 }
154
155 void AccelerometerReader::ReadAccelerometer() {
156 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
157
158 // Initiate the trigger to read accelerometers simultaneously
159 int bytes_written = base::WriteFile(
160 base::FilePath(kAccelerometerTriggerPath), "1\n", 2);
161 if (bytes_written < 2) {
162 PLOG(ERROR) << "Accelerometer trigger failure: " << bytes_written;
163 return;
164 }
165
166 // Read resulting sample from /dev/cros-ec-accel.
167 char data[kTriggerDataLength];
168 int bytes_read = base::ReadFile(base::FilePath(kAccelerometerDevicePath),
169 data, kTriggerDataLength);
170 if (bytes_read < static_cast<int>(kTriggerDataLength)) {
171 LOG(ERROR) << "Read " << bytes_read << " byte(s), expected "
172 << kTriggerDataLength << " bytes from accelerometer";
173 return;
174 }
175
176 int16* values = reinterpret_cast<int16*>(data);
177 base_reading_.set_x(values[accelerometer_index_[0]]);
178 base_reading_.set_y(values[accelerometer_index_[1]]);
179 base_reading_.set_z(values[accelerometer_index_[2]]);
180 base_reading_.Scale(1.0f / accelerometer_base_scale_);
181
182 lid_reading_.set_x(values[accelerometer_index_[3]]);
183 lid_reading_.set_y(values[accelerometer_index_[4]]);
184 lid_reading_.set_z(values[accelerometer_index_[5]]);
185 lid_reading_.Scale(1.0f / accelerometer_lid_scale_);
186 read_success_ = true;
187 }
188
189 void AccelerometerReader::OnDataRead() {
190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
191 // If successful, notify the delegate.
192 if (read_success_)
193 delegate_->HandleAccelerometerReading(base_reading_, lid_reading_);
194
195 // Trigger another read after the current sampling delay.
196 base::MessageLoop::current()->PostDelayedTask(
197 FROM_HERE,
198 base::Bind(&AccelerometerReader::TriggerRead,
199 weak_factory_.GetWeakPtr()),
200 base::TimeDelta::FromMilliseconds(kDelayBetweenReadsMs));
201 }
202
203 } // namespace chromeos
OLDNEW
« 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