blob: 800c49b39d91c96614715432107689abc7d6f673 [file] [log] [blame]
Brian Silverman4da58072015-01-26 20:18:52 -05001#include "frc971/wpilib/encoder_and_potentiometer.h"
2
3#include "aos/linux_code/init.h"
4#include "aos/common/logging/logging.h"
5
6namespace frc971 {
7namespace wpilib {
8
9void DMAEncoderAndPotentiometer::UpdateFromSample(const DMASample &sample) {
10 if (index_last_value_) {
11 // It was already true last time, so check if it's reset back to false yet.
12 index_last_value_ = sample.Get(index_.get());
13 } else if (sample.Get(index_.get())) {
14 // This sample is posedge, so record all the values.
15 index_last_value_ = true;
16 ++index_posedge_count_;
17 last_encoder_value_ = sample.GetRaw(encoder_.get());
18 last_potentiometer_voltage_ = sample.GetVoltage(potentiometer_.get());
19 }
20}
21
22void InterruptEncoderAndPotentiometer::Start() {
23 CHECK_NE(nullptr, encoder_);
24 CHECK_NE(nullptr, index_);
25 CHECK_NE(nullptr, potentiometer_);
26 CHECK_NE(0, priority_);
27 thread_ = ::std::thread(::std::ref(*this));
28}
29
30void InterruptEncoderAndPotentiometer::operator()() {
31 ::aos::SetCurrentThreadName("IntEncPot_" +
32 ::std::to_string(potentiometer_->GetChannel()));
33
34 index_->RequestInterrupts();
35 index_->SetUpSourceEdge(true, false);
36
37 ::aos::SetCurrentThreadRealtimePriority(priority_);
38
39 InterruptableSensorBase::WaitResult result = InterruptableSensorBase::kBoth;
40 while (run_) {
41 result = index_->WaitForInterrupt(
42 0.1, result != InterruptableSensorBase::kTimeout);
43 if (result == InterruptableSensorBase::kTimeout) {
44 continue;
45 }
46
47 {
48 ::aos::MutexLocker locker(&mutex_);
49 last_potentiometer_voltage_ = potentiometer_->GetVoltage();
50 last_encoder_value_ = encoder_->GetRaw();
51 ++index_posedge_count_;
52 }
53 }
54}
55
56} // namespace wpilib
57} // namespace frc971