-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncoderMenuSwitch.cpp
67 lines (62 loc) · 1.5 KB
/
EncoderMenuSwitch.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
60
61
62
63
64
65
66
67
#include <Arduino.h>
#include "EncoderMenuSwitch.h"
//----------------------------------------------
// Constructor
//----------------------------------------------
EncoderMenuSwitch::EncoderMenuSwitch() { }
//----------------------------------------------
// Initialize the instance
//----------------------------------------------
void EncoderMenuSwitch::Initialize(byte pinA, byte pinB, byte pinSwitch)
{
pinOutA = pinA;
pinOutB = pinB;
btnPin = pinSwitch;
pinMode(pinOutA, INPUT_PULLUP);
pinMode(pinOutB, INPUT_PULLUP);
pinMode(btnPin, INPUT_PULLUP);
}
//------------------------------------------------------
// Encoder dispatcher
//------------------------------------------------------
void EncoderMenuSwitch::Dispatch()
{
// Encoder read
valuePinAcurrent = digitalRead(pinOutA);
if ((valuePinA == LOW) && (valuePinAcurrent == HIGH))
{
if (init)
{
if (digitalRead(pinOutB) == LOW)
{
if (OnEncoderMoved) OnEncoderMoved(EncoderDirection::ENCODER_UP);
}
else
{
if (OnEncoderMoved) OnEncoderMoved(EncoderDirection::ENCODER_DOWN);
}
}
else
{
init = true;
}
}
valuePinA = valuePinAcurrent;
// Push button read
if (digitalRead(btnPin) == 0)
{
if (!btnFirstPress)
{
btnFirstPress = true;
btnElapsedMs = millis();
}
else
{
if ((millis() - btnElapsedMs) > 200)
{
btnFirstPress = false;
if (OnEncoderClick) OnEncoderClick();
}
}
}
}