Repository navigation
Expand file tree
/
Copy pathClickEncoderStream.h
More file actions
116 lines (91 loc) · 2.69 KB
/
Copy pathClickEncoderStream.h
File metadata and controls
116 lines (91 loc) · 2.69 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
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
/**************
ClickEncoderStream.h
Jun. 2016
Modified by Christophe Persoz and Rui Azevedo.
Based on keyStream.h developed by Rui Azevado.
and ClickEncoder library by Peter Dannegger.
https://github.com/christophepersoz/encoder
Sept. 2014 Rui Azevedo - ruihfazevedo(@rrob@)gmail.com
creative commons license 3.0: Attribution-ShareAlike CC BY-SA
This software is furnished "as is", without technical support, and with no
warranty, express or implied, as to its usefulness for any purpose.
Thread Safe: No
Extendable: Yes
quick and dirty keyboard driver
metaprog keyboard driver where N is the number of keys
all keys are expected to be a pin (buttons)
we can have reverse logic (pull-ups) by entering negative pin numbers
ex: -A0 means: pin A0 normally high, low when button pushed (reverse logic)
***/
#ifndef __ClickEncoderStream_h__
#define __ClickEncoderStream_h__
// Arduino specific libraries
#ifdef defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega328P__)
#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#endif
#include <Arduino.h>
#include <ClickEncoder.h>
/* emulate a stream based on clickEncoder movement returning +/- for every 'sensivity' steps
buffer not needer because we have an accumulator
*/
class ClickEncoderStream:public Stream {
public:
ClickEncoder &enc; //associated hardware clickEncoder
int8_t sensivity;
int oldPos;
int pos;
ClickEncoder::Button btn;
inline void update() {
pos += enc.getValue();
if (btn == ClickEncoder::Open)
btn = enc.getButton();
}
ClickEncoderStream(ClickEncoder &enc,int sensivity)
:enc(enc),
pos(0),
oldPos(0),
btn(ClickEncoder::Open),
sensivity(sensivity) {
pos = enc.getValue();
}
inline void setSensivity(int s) {
sensivity = s;
}
int available(void) {
return peek() != -1;
}
int peek(void) {
update();
if (btn == ClickEncoder::Clicked)
return menu::enterCode;
if (btn == ClickEncoder::DoubleClicked)
return menu::escCode;
int d = pos - oldPos;
if (d <= -sensivity)
return menu::downCode;
if (d >= sensivity)
return menu::upCode;
return -1;
}
int read()
{
int ch = peek();
btn = ClickEncoder::Open;
if (ch == menu::upCode)
oldPos += sensivity;
else if (ch == menu::downCode)
oldPos -= sensivity;
return ch;
}
void flush() {
update();
oldPos = pos;
}
size_t write(uint8_t v) {
oldPos = v;
return 1;
}
};
#endif /* ClickEncoderStream_h */