| 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 <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 | 45 |
| 46 // The axes on each accelerometer. | 46 // The axes on each accelerometer. |
| 47 const char kAccelerometerAxes[][2] = {"y", "x", "z"}; | 47 const char kAccelerometerAxes[][2] = {"y", "x", "z"}; |
| 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 size of individual values. | 52 // The size of individual values. |
| 53 const size_t kDataSize = 2; | 53 const size_t kDataSize = 2; |
| 54 | 54 |
| 55 // The time to wait between reading the accelerometer. | |
| 56 const int kDelayBetweenReadsMs = 100; | |
| 57 | |
| 58 // The mean acceleration due to gravity on Earth in m/s^2. | 55 // The mean acceleration due to gravity on Earth in m/s^2. |
| 59 const float kMeanGravity = 9.80665f; | 56 const float kMeanGravity = 9.80665f; |
| 60 | 57 |
| 61 // 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 |
| 62 // success or false on failure. | 59 // success or false on failure. |
| 63 bool ReadFileToInt(const base::FilePath& path, int* value) { | 60 bool ReadFileToInt(const base::FilePath& path, int* value) { |
| 64 std::string s; | 61 std::string s; |
| 65 DCHECK(value); | 62 DCHECK(value); |
| 66 if (!base::ReadFileToString(path, &s, kMaxAsciiUintLength)) { | 63 if (!base::ReadFileToString(path, &s, kMaxAsciiUintLength)) { |
| 67 return false; | 64 return false; |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 if (bytes_read < static_cast<int>(length)) { | 159 if (bytes_read < static_cast<int>(length)) { |
| 163 LOG(ERROR) << "Read " << bytes_read << " byte(s), expected " | 160 LOG(ERROR) << "Read " << bytes_read << " byte(s), expected " |
| 164 << length << " bytes from accelerometer"; | 161 << length << " bytes from accelerometer"; |
| 165 return false; | 162 return false; |
| 166 } | 163 } |
| 167 return true; | 164 return true; |
| 168 } | 165 } |
| 169 | 166 |
| 170 } // namespace | 167 } // namespace |
| 171 | 168 |
| 169 const int AccelerometerReader::kDelayBetweenReadsMs = 100; |
| 170 |
| 172 AccelerometerReader::ConfigurationData::ConfigurationData() | 171 AccelerometerReader::ConfigurationData::ConfigurationData() |
| 173 : count(0) { | 172 : count(0) { |
| 174 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) { | 173 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) { |
| 175 has[i] = false; | 174 has[i] = false; |
| 176 for (int j = 0; j < 3; ++j) { | 175 for (int j = 0; j < 3; ++j) { |
| 177 scale[i][j] = 0; | 176 scale[i][j] = 0; |
| 178 index[i][j] = -1; | 177 index[i][j] = -1; |
| 179 } | 178 } |
| 180 } | 179 } |
| 181 } | 180 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 195 // Asynchronously detect and initialize the accelerometer to avoid delaying | 194 // Asynchronously detect and initialize the accelerometer to avoid delaying |
| 196 // startup. | 195 // startup. |
| 197 base::PostTaskAndReplyWithResult( | 196 base::PostTaskAndReplyWithResult( |
| 198 task_runner_.get(), FROM_HERE, | 197 task_runner_.get(), FROM_HERE, |
| 199 base::Bind(&DetectAndReadAccelerometerConfiguration, configuration_), | 198 base::Bind(&DetectAndReadAccelerometerConfiguration, configuration_), |
| 200 base::Bind(&AccelerometerReader::OnInitialized, | 199 base::Bind(&AccelerometerReader::OnInitialized, |
| 201 weak_factory_.GetWeakPtr(), configuration_)); | 200 weak_factory_.GetWeakPtr(), configuration_)); |
| 202 } | 201 } |
| 203 | 202 |
| 204 void AccelerometerReader::AddObserver(Observer* observer) { | 203 void AccelerometerReader::AddObserver(Observer* observer) { |
| 205 observers_.AddObserver(observer); | 204 observers_->AddObserver(observer); |
| 206 if (has_update_) | 205 if (update_) |
| 207 observer->OnAccelerometerUpdated(update_); | 206 observer->OnAccelerometerUpdated(update_.get()); |
| 208 } | 207 } |
| 209 | 208 |
| 210 void AccelerometerReader::RemoveObserver(Observer* observer) { | 209 void AccelerometerReader::RemoveObserver(Observer* observer) { |
| 211 observers_.RemoveObserver(observer); | 210 observers_->RemoveObserver(observer); |
| 212 } | 211 } |
| 213 | 212 |
| 214 AccelerometerReader::AccelerometerReader() | 213 AccelerometerReader::AccelerometerReader() |
| 215 : has_update_(false), | 214 : configuration_(new AccelerometerReader::Configuration()), |
| 216 configuration_(new AccelerometerReader::Configuration()), | 215 observers_(new ObserverListThreadSafe<Observer>()), |
| 217 weak_factory_(this) { | 216 weak_factory_(this) { |
| 218 } | 217 } |
| 219 | 218 |
| 220 AccelerometerReader::~AccelerometerReader() { | 219 AccelerometerReader::~AccelerometerReader() { |
| 221 } | 220 } |
| 222 | 221 |
| 223 void AccelerometerReader::OnInitialized( | 222 void AccelerometerReader::OnInitialized( |
| 224 scoped_refptr<AccelerometerReader::Configuration> configuration, | 223 scoped_refptr<AccelerometerReader::Configuration> configuration, |
| 225 bool success) { | 224 bool success) { |
| 226 if (success) | 225 if (success) |
| (...skipping 13 matching lines...) Expand all Loading... |
| 240 weak_factory_.GetWeakPtr(), | 239 weak_factory_.GetWeakPtr(), |
| 241 reading)); | 240 reading)); |
| 242 } | 241 } |
| 243 | 242 |
| 244 void AccelerometerReader::OnDataRead( | 243 void AccelerometerReader::OnDataRead( |
| 245 scoped_refptr<AccelerometerReader::Reading> reading, | 244 scoped_refptr<AccelerometerReader::Reading> reading, |
| 246 bool success) { | 245 bool success) { |
| 247 DCHECK(!task_runner_->RunsTasksOnCurrentThread()); | 246 DCHECK(!task_runner_->RunsTasksOnCurrentThread()); |
| 248 | 247 |
| 249 if (success) { | 248 if (success) { |
| 250 has_update_ = true; | 249 update_ = new AccelerometerUpdate(); |
| 251 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) { | 250 for (int i = 0; i < ACCELEROMETER_SOURCE_COUNT; ++i) { |
| 252 if (!configuration_->data.has[i]) | 251 if (!configuration_->data.has[i]) |
| 253 continue; | 252 continue; |
| 254 | 253 |
| 255 int16* values = reinterpret_cast<int16*>(reading->data); | 254 int16* values = reinterpret_cast<int16*>(reading->data); |
| 256 update_.Set(static_cast<AccelerometerSource>(i), | 255 update_->Set(static_cast<AccelerometerSource>(i), |
| 257 values[configuration_->data.index[i][0]] * | 256 values[configuration_->data.index[i][0]] * |
| 258 configuration_->data.scale[i][0], | 257 configuration_->data.scale[i][0], |
| 259 values[configuration_->data.index[i][1]] * | 258 values[configuration_->data.index[i][1]] * |
| 260 configuration_->data.scale[i][1], | 259 configuration_->data.scale[i][1], |
| 261 values[configuration_->data.index[i][2]] * | 260 values[configuration_->data.index[i][2]] * |
| 262 configuration_->data.scale[i][2]); | 261 configuration_->data.scale[i][2]); |
| 263 } | 262 } |
| 264 FOR_EACH_OBSERVER(Observer, observers_, OnAccelerometerUpdated(update_)); | 263 // TODO(jonross): move this to the blocking thread (crbug.com/461433) |
| 264 observers_->Notify(FROM_HERE, &Observer::OnAccelerometerUpdated, update_); |
| 265 } | 265 } |
| 266 | 266 |
| 267 // Trigger another read after the current sampling delay. | 267 // Trigger another read after the current sampling delay. |
| 268 base::MessageLoop::current()->PostDelayedTask( | 268 base::MessageLoop::current()->PostDelayedTask( |
| 269 FROM_HERE, | 269 FROM_HERE, |
| 270 base::Bind(&AccelerometerReader::TriggerRead, | 270 base::Bind(&AccelerometerReader::TriggerRead, |
| 271 weak_factory_.GetWeakPtr()), | 271 weak_factory_.GetWeakPtr()), |
| 272 base::TimeDelta::FromMilliseconds(kDelayBetweenReadsMs)); | 272 base::TimeDelta::FromMilliseconds(kDelayBetweenReadsMs)); |
| 273 } | 273 } |
| 274 | 274 |
| 275 } // namespace chromeos | 275 } // namespace chromeos |
| OLD | NEW |