forked from shvass/qmd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquadrature.cpp
More file actions
82 lines (54 loc) · 2 KB
/
quadrature.cpp
File metadata and controls
82 lines (54 loc) · 2 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "quadrature.hpp"
#include <esp_log.h>
#include <esp_intr_types.h>
#define LOG "DECODER"
#define PCNT_COUNT_LIMIT_HIGH 1000
#define PCNT_COUNT_LIMIT_LOW -1000
// configuration to initialize all pcnt units
pcnt_unit_config_t pcnt_config = {
.low_limit = PCNT_COUNT_LIMIT_LOW,
.high_limit = PCNT_COUNT_LIMIT_HIGH,
.intr_priority = 0,
.flags = {true}
};
// configuration to initialize all pcnt channels
pcnt_chan_config_t chan_config = {
.edge_gpio_num = -1,
.level_gpio_num = -1,
.flags = {0, 0, 0, 0, 0}};
pcnt_glitch_filter_config_t filter_config = {
.max_glitch_ns = DECODER_GLITCH_NS,
};
decoder::decoder(int *phaseA, int *phaseB, int count){
if(count > DECODER_MAX_WHEEL_COUNT)
ESP_LOGD(LOG, "ERROR: unit count exceeded %d", count = DECODER_MAX_WHEEL_COUNT);
unitCount = count;
for(int i = 0; i <count; i++){
pcnt_new_unit(&pcnt_config, &pcnt_unit[i]);
pcnt_unit_handle_t& current = pcnt_unit[i];
chan_config.edge_gpio_num = phaseA[i];
chan_config.level_gpio_num = phaseB[i];
pcnt_new_channel(current, &chan_config, &pcnt_channels[i]);
pcnt_channel_set_edge_action(pcnt_channels[i], PCNT_CHANNEL_EDGE_ACTION_INCREASE, PCNT_CHANNEL_EDGE_ACTION_HOLD);
pcnt_channel_set_level_action(pcnt_channels[i], PCNT_CHANNEL_LEVEL_ACTION_KEEP, PCNT_CHANNEL_LEVEL_ACTION_INVERSE);
pcnt_unit_add_watch_point(current, PCNT_COUNT_LIMIT_HIGH);
pcnt_unit_add_watch_point(current, PCNT_COUNT_LIMIT_LOW);
pcnt_unit_set_glitch_filter(current, &filter_config);
pcnt_unit_enable(current);
pcnt_unit_start(current);
};
}
void decoder::update(){
int temp = 0;
for(int i = 0; i < unitCount; i++){
pcnt_unit_get_count(pcnt_unit[i], &temp);
count[i] = temp;
};
}
decoder::~decoder(){
for(int i = 0; i < unitCount; i++){
pcnt_unit_disable(pcnt_unit[i]);
pcnt_del_channel(pcnt_channels[i]);
pcnt_del_unit(pcnt_unit[i]);
}
}