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

Side by Side Diff: chromeos/accelerometer/accelerometer_reader.cc

Issue 500613003: Use standardized and extendable accelerometer update type. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merge and remove unused Vector3dF reference. Created 6 years, 3 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chromeos/accelerometer/accelerometer_reader.h" 5 #include "chromeos/accelerometer/accelerometer_reader.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 const size_t kTriggerDataValues = 45 const size_t kTriggerDataValues =
46 arraysize(kAccelerometerNames) * arraysize(kAccelerometerAxes); 46 arraysize(kAccelerometerNames) * arraysize(kAccelerometerAxes);
47 const size_t kTriggerDataLength = kTriggerDataValues * 2; 47 const size_t kTriggerDataLength = kTriggerDataValues * 2;
48 48
49 // The length required to read uint values from configuration files. 49 // The length required to read uint values from configuration files.
50 const size_t kMaxAsciiUintLength = 21; 50 const size_t kMaxAsciiUintLength = 21;
51 51
52 // The time to wait between reading the accelerometer. 52 // The time to wait between reading the accelerometer.
53 const int kDelayBetweenReadsMs = 100; 53 const int kDelayBetweenReadsMs = 100;
54 54
55 // The mean acceleration due to gravity on Earth in m/s^2.
56 const float kMeanGravity = 9.80665f;
57
55 // Reads |path| to the unsigned int pointed to by |value|. Returns true on 58 // Reads |path| to the unsigned int pointed to by |value|. Returns true on
56 // success or false on failure. 59 // success or false on failure.
57 bool ReadFileToUint(const base::FilePath& path, unsigned int* value) { 60 bool ReadFileToUint(const base::FilePath& path, unsigned int* value) {
58 std::string s; 61 std::string s;
59 DCHECK(value); 62 DCHECK(value);
60 if (!base::ReadFileToString(path, &s, kMaxAsciiUintLength)) { 63 if (!base::ReadFileToString(path, &s, kMaxAsciiUintLength)) {
61 LOG(ERROR) << "Failed to read " << path.value(); 64 LOG(ERROR) << "Failed to read " << path.value();
62 return false; 65 return false;
63 } 66 }
64 base::TrimWhitespaceASCII(s, base::TRIM_ALL, &s); 67 base::TrimWhitespaceASCII(s, base::TRIM_ALL, &s);
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 base::Bind(&AccelerometerReader::OnDataRead, 189 base::Bind(&AccelerometerReader::OnDataRead,
187 weak_factory_.GetWeakPtr(), reading)); 190 weak_factory_.GetWeakPtr(), reading));
188 } 191 }
189 192
190 void AccelerometerReader::OnDataRead( 193 void AccelerometerReader::OnDataRead(
191 scoped_refptr<AccelerometerReader::Reading> reading, 194 scoped_refptr<AccelerometerReader::Reading> reading,
192 bool success) { 195 bool success) {
193 DCHECK(!task_runner_->RunsTasksOnCurrentThread()); 196 DCHECK(!task_runner_->RunsTasksOnCurrentThread());
194 197
195 if (success) { 198 if (success) {
196 gfx::Vector3dF base_reading, lid_reading;
197 int16* values = reinterpret_cast<int16*>(reading->data); 199 int16* values = reinterpret_cast<int16*>(reading->data);
198 base_reading.set_x(values[configuration_->data.index[0]]); 200 float lid_scale = kMeanGravity / configuration_->data.lid_scale;
199 base_reading.set_y(values[configuration_->data.index[1]]); 201 update_.Set(ui::ACCELEROMETER_SOURCE_SCREEN,
200 base_reading.set_z(values[configuration_->data.index[2]]); 202 -values[configuration_->data.index[4]] * lid_scale,
201 base_reading.Scale(1.0f / configuration_->data.base_scale); 203 values[configuration_->data.index[3]] * lid_scale,
202 204 values[configuration_->data.index[5]] * lid_scale);
203 lid_reading.set_x(values[configuration_->data.index[3]]); 205 float base_scale = kMeanGravity / configuration_->data.base_scale;
204 lid_reading.set_y(values[configuration_->data.index[4]]); 206 update_.Set(ui::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD,
205 lid_reading.set_z(values[configuration_->data.index[5]]); 207 -values[configuration_->data.index[1]] * base_scale,
206 lid_reading.Scale(1.0f / configuration_->data.lid_scale); 208 -values[configuration_->data.index[0]] * base_scale,
207 delegate_->HandleAccelerometerReading(base_reading, lid_reading); 209 -values[configuration_->data.index[2]] * base_scale);
210 delegate_->HandleAccelerometerUpdate(update_);
208 } 211 }
209 212
210 // Trigger another read after the current sampling delay. 213 // Trigger another read after the current sampling delay.
211 base::MessageLoop::current()->PostDelayedTask( 214 base::MessageLoop::current()->PostDelayedTask(
212 FROM_HERE, 215 FROM_HERE,
213 base::Bind(&AccelerometerReader::TriggerRead, 216 base::Bind(&AccelerometerReader::TriggerRead,
214 weak_factory_.GetWeakPtr()), 217 weak_factory_.GetWeakPtr()),
215 base::TimeDelta::FromMilliseconds(kDelayBetweenReadsMs)); 218 base::TimeDelta::FromMilliseconds(kDelayBetweenReadsMs));
216 } 219 }
217 220
218 } // namespace chromeos 221 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698