forked from unhuman-io/motorlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhall.h
39 lines (35 loc) · 1.11 KB
/
hall.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#ifndef UNHUMAN_MOTORLIB_HALL_H_
#define UNHUMAN_MOTORLIB_HALL_H_
#include "encoder.h"
class GPIO;
#include "gpio.h"
class HallEncoder : public EncoderBase {
public:
HallEncoder(GPIO &gpio_a, GPIO &gpio_b, GPIO &gpio_c) :
gpio_a_(gpio_a), gpio_b_(gpio_b), gpio_c_(gpio_c) {}
int32_t read() {
uint8_t hall_read = gpio_a_.get_value() | (gpio_b_.get_value() << 1) | (gpio_c_.get_value() << 2);
uint8_t hall_count = hall_table_[hall_read];
if (hall_count != 0) {
int8_t diff = hall_count - last_hall_count_;
last_hall_count_ = hall_count;
if (diff < -3) {
diff += 6;
} else if (diff > 3) {
diff -= 6;
}
count_ += diff;
}
return count_;
}
int32_t get_value() const { return count_; }
void trigger() {}
int32_t get_index_pos() { return 0; }
bool index_received() { return true; }
private:
GPIO &gpio_a_, &gpio_b_, &gpio_c_;
int32_t count_ = 0;
uint8_t last_hall_count_ = 0;
static uint8_t hall_table_[8];
};
#endif // UNHUMAN_MOTORLIB_HALL_H_