forked from Candas1/Arduino-FOC
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStepDirListener.cpp
More file actions
41 lines (36 loc) · 1.06 KB
/
Copy pathStepDirListener.cpp
File metadata and controls
41 lines (36 loc) · 1.06 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
#include "StepDirListener.h"
StepDirListener::StepDirListener(int _pinStep, int _pinDir, float _counter_to_value){
pin_step = _pinStep;
pin_dir = _pinDir;
counter_to_value = _counter_to_value;
}
void StepDirListener::init(){
pinMode(pin_dir, INPUT);
pinMode(pin_step, INPUT_PULLUP);
count = 0;
}
void StepDirListener::enableInterrupt(void (*doA)()){
// Arduino-gd32 doesn't like this, need to be fixed
//attachInterrupt(digitalPinToInterrupt(pin_step), doA, polarity);
}
void StepDirListener::attach(float* variable){
attached_variable = variable;
}
void StepDirListener::handle(){
// read step status
//bool step = digitalRead(pin_step);
// update counter only on rising edge
//if(step && step != step_active){
if(digitalRead(pin_dir))
count++;
else
count--;
//}
//step_active = step;
// if attached variable update it
if(attached_variable) *attached_variable = getValue();
}
// calculate the position from counter
float StepDirListener::getValue(){
return (float) count * counter_to_value;
}