| OLD | NEW |
| 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 Loading... |
| 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 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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; | 199 gfx::Vector3dF base_reading, lid_reading; |
| 197 int16* values = reinterpret_cast<int16*>(reading->data); | 200 int16* values = reinterpret_cast<int16*>(reading->data); |
| 198 base_reading.set_x(values[configuration_->data.index[0]]); | 201 float lid_scale = kMeanGravity / configuration_->data.lid_scale; |
| 199 base_reading.set_y(values[configuration_->data.index[1]]); | 202 update_.Set(ui::ACCELEROMETER_SOURCE_SCREEN, |
| 200 base_reading.set_z(values[configuration_->data.index[2]]); | 203 -values[configuration_->data.index[4]] * lid_scale, |
| 201 base_reading.Scale(1.0f / configuration_->data.base_scale); | 204 values[configuration_->data.index[3]] * lid_scale, |
| 202 | 205 values[configuration_->data.index[5]] * lid_scale); |
| 203 lid_reading.set_x(values[configuration_->data.index[3]]); | 206 float base_scale = kMeanGravity / configuration_->data.base_scale; |
| 204 lid_reading.set_y(values[configuration_->data.index[4]]); | 207 update_.Set(ui::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD, |
| 205 lid_reading.set_z(values[configuration_->data.index[5]]); | 208 -values[configuration_->data.index[1]] * base_scale, |
| 206 lid_reading.Scale(1.0f / configuration_->data.lid_scale); | 209 -values[configuration_->data.index[0]] * base_scale, |
| 207 delegate_->HandleAccelerometerReading(base_reading, lid_reading); | 210 -values[configuration_->data.index[2]] * base_scale); |
| 211 delegate_->HandleAccelerometerUpdate(update_); |
| 208 } | 212 } |
| 209 | 213 |
| 210 // Trigger another read after the current sampling delay. | 214 // Trigger another read after the current sampling delay. |
| 211 base::MessageLoop::current()->PostDelayedTask( | 215 base::MessageLoop::current()->PostDelayedTask( |
| 212 FROM_HERE, | 216 FROM_HERE, |
| 213 base::Bind(&AccelerometerReader::TriggerRead, | 217 base::Bind(&AccelerometerReader::TriggerRead, |
| 214 weak_factory_.GetWeakPtr()), | 218 weak_factory_.GetWeakPtr()), |
| 215 base::TimeDelta::FromMilliseconds(kDelayBetweenReadsMs)); | 219 base::TimeDelta::FromMilliseconds(kDelayBetweenReadsMs)); |
| 216 } | 220 } |
| 217 | 221 |
| 218 } // namespace chromeos | 222 } // namespace chromeos |
| OLD | NEW |