-
Notifications
You must be signed in to change notification settings - Fork 2
/
rotary.h
114 lines (91 loc) · 2.37 KB
/
rotary.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
// WriteLight library include file
#ifndef Rotary_h
#define Rotary_h
#include "Arduino.h"
#include "Metro.h"
// set these up to set the pins and interrupts
// you will also need to code for the variants if you use them
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define MEGA
//#else
// #define UNO
#endif
// The Bourns Rotary Optical Encoder
// Black, Pin 1 -- Ground
// Yellow, Pin 3 -- Channel A
// White, Pin 4 -- VCC (+5V)
// Purple, Pin 5 -- Channel B
#ifdef MEGA
const int MAX_ENCODER_COUNT = 3;
enum interruptPins {
pin1A = 2, // Gray (from encoder)
pin1B = 3, // Blue (from encoder)
pin2A = 21, // Gray (from encoder)
pin2B = 20, // Blue (from encoder)
pin3A = 19, // Gray (from encoder)
pin3B = 18}; // Blue (from encoder)
const interruptPins interruptAPins[MAX_ENCODER_COUNT] = {pin1A, pin2A, pin3A};
const interruptPins interruptBPins[MAX_ENCODER_COUNT] = {pin1B, pin2B, pin3B};
const int interruptAs[] = {0,2,4};
const int interruptBs[] = {1,3,5};
#endif
//const float ROTARY_INPUT_MAX = 1000;
//const float ROTARY_RANGE_MAX = 255;
class Rotary {
public:
Rotary(int inputMax, int rangeMax, bool fake);
void setup(Rotary& me);
void loop(void);
bool changed(void);
int value(void);
void InterruptA(void);
void InterruptB(void);
private:
const int _i; // encoder_index
int _pinA,_pinB;
bool _aSet,_bSet;
int _encoderPos;
int _lastPos;
int _lastValue;
bool _rotating;
int _inputMax;
int _rangeMax;
bool _fake;
static int installCount;
static bool _interrupt_ok;
public:
static void interruptA1(void) {
if(installCount >= 1 && encoders[0]) {
encoders[0]->InterruptA();
}
};
static void interruptB1(void){
if(installCount >= 1 && encoders[0]) {
encoders[0]->InterruptB();
}
};
static void interruptA2(void){
if(installCount >= 2 && encoders[1]) {
encoders[1]->InterruptA();
}
};
static void interruptB2(void){
if(installCount >= 2 && encoders[1]) {
encoders[1]->InterruptB();
}
};
static void interruptA3(void){
if(installCount >= 3 && encoders[2]) {
encoders[2]->InterruptA();
}
};
static void interruptB3(void){
if(installCount >= 3 && encoders[2]) {
encoders[2]->InterruptB();
}
};
static Rotary* encoders[MAX_ENCODER_COUNT];
static void (*A_interrupt_funs[MAX_ENCODER_COUNT])(void);
static void (*B_interrupt_funs[MAX_ENCODER_COUNT])(void);
};
#endif