Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_HALL_EFFECT_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_HALL_EFFECT_H_ |
| 3 | |
| 4 | #include <stdint.h> |
| 5 | |
| 6 | #include "frc971/control_loops/control_loops.q.h" |
| 7 | |
| 8 | namespace frc971 { |
| 9 | |
| 10 | class HallEffectTracker { |
| 11 | public: |
| 12 | int32_t get_posedges() const { return posedges_.count(); } |
| 13 | int32_t get_negedges() const { return negedges_.count(); } |
| 14 | |
| 15 | bool either_count_changed() const { |
| 16 | return posedges_.count_changed() || negedges_.count_changed(); |
| 17 | } |
| 18 | bool posedge_count_changed() const { return posedges_.count_changed(); } |
| 19 | bool negedge_count_changed() const { return negedges_.count_changed(); } |
| 20 | |
| 21 | bool value() const { return value_; } |
| 22 | |
| 23 | void Update(const HallEffectStruct &position) { |
| 24 | value_ = position.current; |
| 25 | posedges_.update(position.posedge_count); |
| 26 | negedges_.update(position.negedge_count); |
| 27 | } |
| 28 | |
| 29 | private: |
| 30 | class { |
| 31 | public: |
| 32 | void update(int32_t count) { |
| 33 | previous_count_ = count_; |
| 34 | count_ = count; |
| 35 | } |
| 36 | |
| 37 | bool count_changed() const { |
| 38 | return previous_count_ != count_; |
| 39 | } |
| 40 | |
| 41 | int32_t count() const { return count_; } |
| 42 | |
| 43 | private: |
| 44 | int32_t count_ = 0; |
| 45 | int32_t previous_count_ = 0; |
| 46 | } posedges_, negedges_; |
| 47 | bool value_ = false; |
| 48 | }; |
| 49 | |
| 50 | } // namespace frc971 |
| 51 | |
| 52 | #endif // FRC971_CONTROL_LOOPS_HALL_EFFECT_H_ |