blob: b63a34b31bd656794a4b8f0540574450d97f4665 [file] [log] [blame]
Brian Silvermane0a95462014-02-17 00:41:09 -08001#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
8namespace frc971 {
9
Austin Schuhf84a1302014-02-19 00:23:30 -080010// TODO(brians): Have a Reset() for when the cape resets.
Brian Silvermane0a95462014-02-17 00:41:09 -080011class HallEffectTracker {
12 public:
13 int32_t get_posedges() const { return posedges_.count(); }
14 int32_t get_negedges() const { return negedges_.count(); }
15
16 bool either_count_changed() const {
17 return posedges_.count_changed() || negedges_.count_changed();
18 }
19 bool posedge_count_changed() const { return posedges_.count_changed(); }
20 bool negedge_count_changed() const { return negedges_.count_changed(); }
21
22 bool value() const { return value_; }
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +000023 bool last_value() const { return last_value_; }
Brian Silvermane0a95462014-02-17 00:41:09 -080024
25 void Update(const HallEffectStruct &position) {
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +000026 last_value_ = value_;
Brian Silvermane0a95462014-02-17 00:41:09 -080027 value_ = position.current;
28 posedges_.update(position.posedge_count);
29 negedges_.update(position.negedge_count);
30 }
31
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +000032 void Reset(const HallEffectStruct &position) {
33 posedges_.Reset(position.posedge_count);
34 negedges_.Reset(position.negedge_count);
35 value_ = position.current;
36 last_value_ = position.current;
Austin Schuhf84a1302014-02-19 00:23:30 -080037 }
38
Brian Silvermane0a95462014-02-17 00:41:09 -080039 private:
40 class {
41 public:
42 void update(int32_t count) {
43 previous_count_ = count_;
44 count_ = count;
45 }
46
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +000047 void Reset(int32_t count) { count_ = count; }
Austin Schuhf84a1302014-02-19 00:23:30 -080048
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +000049 bool count_changed() const { return previous_count_ != count_; }
Brian Silvermane0a95462014-02-17 00:41:09 -080050
51 int32_t count() const { return count_; }
52
53 private:
54 int32_t count_ = 0;
55 int32_t previous_count_ = 0;
Brian Silvermane0a95462014-02-17 00:41:09 -080056 } posedges_, negedges_;
57 bool value_ = false;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +000058 bool last_value_ = false;
Brian Silvermane0a95462014-02-17 00:41:09 -080059};
60
61} // namespace frc971
62
63#endif // FRC971_CONTROL_LOOPS_HALL_EFFECT_H_