forked from 0xPIT/encoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClickEncoder.h
118 lines (95 loc) · 2.68 KB
/
ClickEncoder.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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// ----------------------------------------------------------------------------
// Rotary Encoder Driver with Acceleration
// Supports Click, DoubleClick, Long Click
//
// (c) 2010 [email protected]
// (c) 2014 [email protected]
//
// Timer-based rotary encoder logic by Peter Dannegger
// http://www.mikrocontroller.net/articles/Drehgeber
// ----------------------------------------------------------------------------
#ifndef __have__ClickEncoder_h__
#define __have__ClickEncoder_h__
// ----------------------------------------------------------------------------
#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "Arduino.h"
// ----------------------------------------------------------------------------
#define ENC_NORMAL (1 << 1) // use Peter Danneger's decoder
#define ENC_FLAKY (1 << 2) // use Table-based decoder
// ----------------------------------------------------------------------------
#ifndef ENC_DECODER
# define ENC_DECODER ENC_NORMAL
#endif
#if ENC_DECODER == ENC_FLAKY
# ifndef ENC_HALFSTEP
# define ENC_HALFSTEP 1 // use table for half step per default
# endif
#endif
// ----------------------------------------------------------------------------
class ClickEncoder
{
public:
typedef enum Button_e {
Open = 0,
Closed,
Pressed,
Held,
Released,
Clicked,
DoubleClicked
} Button;
public:
ClickEncoder(uint8_t A, uint8_t B, uint8_t BTN = -1,
uint8_t stepsPerNotch = 1, bool active = LOW);
void service(void);
int16_t getValue(void);
#ifndef WITHOUT_BUTTON
public:
Button getButton(void);
#endif
#ifndef WITHOUT_BUTTON
public:
void setDoubleClickEnabled(const bool &d)
{
doubleClickEnabled = d;
}
const bool getDoubleClickEnabled()
{
return doubleClickEnabled;
}
#endif
public:
void setAccelerationEnabled(const bool &a)
{
accelerationEnabled = a;
if (accelerationEnabled == false) {
acceleration = 0;
}
}
const bool getAccelerationEnabled()
{
return accelerationEnabled;
}
private:
const uint8_t pinA;
const uint8_t pinB;
const uint8_t pinBTN;
const bool pinsActive;
volatile int16_t delta;
volatile int16_t last;
uint8_t steps;
volatile uint16_t acceleration;
#if ENC_DECODER != ENC_NORMAL
static const int8_t table[16];
#endif
#ifndef WITHOUT_BUTTON
volatile Button button;
bool doubleClickEnabled;
bool accelerationEnabled;
#endif
};
// ----------------------------------------------------------------------------
#endif // __have__ClickEncoder_h__