-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotaryEncoder.cpp
60 lines (48 loc) · 1.11 KB
/
RotaryEncoder.cpp
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
#include "RotaryEncoder.h"
RotaryEncoder::RotaryEncoder(int pinAId, int pinBId, int switchId) : StateMachine(), m_clockA(pinAId), m_clockB(pinBId), m_Last_ClockB_Status(0), m_Current_ClockB_Status(0)
{
m_button = new MicroSwitch(switchId);
pinMode(m_clockA, INPUT);
pinMode(m_clockB, INPUT);
}
RotaryEncoder::~RotaryEncoder()
{
delete m_button;
}
void RotaryEncoder::Update(long updateInterval)
{
int flag = 0;
m_button->Update(updateInterval);
if (m_button->GetState())
{
Reset();
}
m_Last_ClockB_Status = digitalRead(m_clockB);
while(!digitalRead(m_clockA)){
m_Current_ClockB_Status = digitalRead(m_clockB);
flag = 1;
}
if(flag == 1){
flag = 0;
if((m_Last_ClockB_Status == 0)&&(m_Current_ClockB_Status == 1)){
m_currentChange++;
}
if((m_Last_ClockB_Status == 1)&&(m_Current_ClockB_Status == 0)){
m_currentChange--;
}
}
};
void RotaryEncoder::Reset()
{
m_currentChange = 0;
};
int RotaryEncoder::GetChange()
{
int change = m_currentChange;
Reset();
return change;
}
bool RotaryEncoder::GetPressed()
{
return m_button->JustPressed();
}