OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "tools/battor_agent/battor_agent.h" | 5 #include "tools/battor_agent/battor_agent.h" |
6 | 6 |
| 7 #include <iomanip> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
9 #include "tools/battor_agent/battor_connection_impl.h" | 11 #include "tools/battor_agent/battor_connection_impl.h" |
| 12 #include "tools/battor_agent/battor_sample_converter.h" |
10 | 13 |
11 using std::vector; | 14 using std::vector; |
12 | 15 |
13 namespace battor { | 16 namespace battor { |
14 | 17 |
15 namespace { | 18 namespace { |
16 | 19 |
17 // The number of seconds that it takes a BattOr to reset. | 20 // The number of seconds that it takes a BattOr to reset. |
18 const uint8_t kBattOrResetTimeSeconds = 2; | 21 const uint8_t kBattOrResetTimeSeconds = 2; |
19 | 22 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 size_t remaining_bytes = msg.size() - sizeof(BattOrFrameHeader); | 83 size_t remaining_bytes = msg.size() - sizeof(BattOrFrameHeader); |
81 if (remaining_bytes != frame_header->length) | 84 if (remaining_bytes != frame_header->length) |
82 return false; | 85 return false; |
83 | 86 |
84 samples->resize(remaining_bytes / sizeof(RawBattOrSample)); | 87 samples->resize(remaining_bytes / sizeof(RawBattOrSample)); |
85 memcpy(samples->data(), frame_ptr, remaining_bytes); | 88 memcpy(samples->data(), frame_ptr, remaining_bytes); |
86 | 89 |
87 return true; | 90 return true; |
88 } | 91 } |
89 | 92 |
90 std::string SamplesToString(const vector<RawBattOrSample>& samples) { | |
91 // TODO(charliea): Print the samples in a better trace format. | |
92 std::stringstream trace_stream; | |
93 for (auto sample : samples) | |
94 trace_stream << sample.voltage_raw << "/" << sample.current_raw | |
95 << std::endl; | |
96 | |
97 return trace_stream.str(); | |
98 } | |
99 | |
100 } // namespace | 93 } // namespace |
101 | 94 |
102 BattOrAgent::BattOrAgent( | 95 BattOrAgent::BattOrAgent( |
103 const std::string& path, | 96 const std::string& path, |
104 Listener* listener, | 97 Listener* listener, |
105 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, | 98 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, |
106 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner) | 99 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner) |
107 : connection_(new BattOrConnectionImpl(path, | 100 : connection_(new BattOrConnectionImpl(path, |
108 this, | 101 this, |
109 file_thread_task_runner, | 102 file_thread_task_runner, |
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 BattOrControlMessage msg{type, param1, param2}; | 385 BattOrControlMessage msg{type, param1, param2}; |
393 connection_->SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, &msg, sizeof(msg)); | 386 connection_->SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, &msg, sizeof(msg)); |
394 } | 387 } |
395 | 388 |
396 void BattOrAgent::CompleteCommand(BattOrError error) { | 389 void BattOrAgent::CompleteCommand(BattOrError error) { |
397 switch (command_) { | 390 switch (command_) { |
398 case Command::START_TRACING: | 391 case Command::START_TRACING: |
399 listener_->OnStartTracingComplete(error); | 392 listener_->OnStartTracingComplete(error); |
400 break; | 393 break; |
401 case Command::STOP_TRACING: { | 394 case Command::STOP_TRACING: { |
402 listener_->OnStopTracingComplete(SamplesToString(samples_), error); | 395 listener_->OnStopTracingComplete(SamplesToString(), error); |
403 break; | 396 break; |
404 } | 397 } |
405 case Command::INVALID: | 398 case Command::INVALID: |
406 NOTREACHED(); | 399 NOTREACHED(); |
407 } | 400 } |
408 | 401 |
409 last_action_ = Action::INVALID; | 402 last_action_ = Action::INVALID; |
410 command_ = Command::INVALID; | 403 command_ = Command::INVALID; |
411 battor_eeprom_.reset(); | 404 battor_eeprom_.reset(); |
412 calibration_frame_.clear(); | 405 calibration_frame_.clear(); |
413 samples_.clear(); | 406 samples_.clear(); |
414 } | 407 } |
415 | 408 |
| 409 std::string BattOrAgent::SamplesToString() { |
| 410 if (calibration_frame_.empty() || samples_.empty() || !battor_eeprom_) |
| 411 return ""; |
| 412 |
| 413 BattOrSampleConverter converter(*battor_eeprom_, calibration_frame_); |
| 414 |
| 415 std::stringstream trace_stream; |
| 416 trace_stream << std::fixed; |
| 417 for (size_t i = 0; i < samples_.size(); i++) { |
| 418 BattOrSample sample = converter.ToSample(samples_[i], i); |
| 419 trace_stream << std::setprecision(2) << sample.time_ms << " " |
| 420 << std::setprecision(1) << sample.current_mA << " " |
| 421 << sample.voltage_mV << std::endl; |
| 422 } |
| 423 |
| 424 return trace_stream.str(); |
| 425 } |
| 426 |
416 } // namespace battor | 427 } // namespace battor |
OLD | NEW |