-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathInfinityUSBKnob_0.9.ino
More file actions
324 lines (237 loc) · 7.73 KB
/
InfinityUSBKnob_0.9.ino
File metadata and controls
324 lines (237 loc) · 7.73 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//Author:@vincentmakes
//Instructions:https://hackaday.io/project/164043-the-infinity-usb-knob
//GNU Licence
#include "HID-Project.h"
#include "Adafruit_NeoPixel.h"
#define PIN 4//to change to DI
#define NUMPIXELS 16 //adjust number of pixels in the ring
int MaxPixelID=NUMPIXELS-1; //because first pixel is 0
volatile byte mybrightness = 36; //max brightness when turning the knob
volatile byte lowbrightness = 2; //initial brightness
int MaxMode=5; //defines how many modes we want. The code further down is pre-written for 5 modes (0 to 4) but this integer allows to trim off while not having to delete functions.
Adafruit_NeoPixel pixels=Adafruit_NeoPixel(NUMPIXELS,PIN,NEO_GRB+NEO_KHZ800);
int encoderPinA = 1;
int encoderPinB = 2;
int buttonPin = 3; //Pin C (push)
volatile int lastEncoded = 0;
volatile long encoderValue = 0;
long lastencoderValue = 0;
long OldValue = 0;
long NewValue =0;
int lastMSB = 0;
int lastLSB = 0;
int Mode =0, Red[5] = { 0, 0, 255, 255,255 }, Green[5]={145,255,119,0,255}, Blue[5]={255,30,0,239,0} ; //default mode on startup + define colors for each mode
/*Mode 0->Blue (0,145,255) ; 1-> Green (0,255,30) ; 2->Orange ; 3-> Purple ; 5-> Yellow*/
long readEncoderValue(void){
return encoderValue/4;
}
boolean isButtonPushDown(void){
if(!digitalRead(buttonPin)){
delay(50);
if(!digitalRead(buttonPin))
return true;
}
return false;
}
void setup() {
Serial.begin (9600);
pinMode(encoderPinA, INPUT);
pinMode(encoderPinB, INPUT);
pinMode(buttonPin, INPUT);
digitalWrite(encoderPinA, HIGH); //turn pullup resistor on
digitalWrite(encoderPinB, HIGH); //turn pullup resistor on
//call updateEncoder() when any high/low changed seen on Pin A or B
attachInterrupt(digitalPinToInterrupt(encoderPinA), updateEncoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoderPinB), updateEncoder, CHANGE);
Consumer.begin(); //initialize HID
Keyboard.begin(); // initialize keyboard
//Initialize Neopixel
pixels.setBrightness(lowbrightness);
pixels.begin();
//lit all pixels with lower brightness
for( int i = 0; i<NUMPIXELS; i++){
pixels.setPixelColor(i, Red[Mode],Green[Mode],Blue[Mode]);
}
pixels.show();
} //end setup
void loop(){
if(isButtonPushDown()){
Serial.println("you push button down!!!");
Mode++;
encoderValue = 0;
if(Mode==MaxMode){Mode=0;} //cycle through Modes
delay(100);
pixels.setBrightness(lowbrightness);
for( int i = 0; i<NUMPIXELS; i++){
pixels.setPixelColor(i, Red[Mode],Green[Mode],Blue[Mode]);
}
//pixels.setPixelColor(0, Red[Mode],Green[Mode],Blue[Mode]); //display first led with new color and lower brightness
pixels.show();
}
delay(50);
}//end loop
//2 types Led Animation. The pixels in the neoring 16 are ordered in counterclokwise way while the 8 one is counterclockwise. The code below is for counterclockwise setup.
void AnimationStop(signed int KnobValue, int Red, int Green, int Blue) //Animation in one direction only with pixel 0 as starting point |----->
{
for(int i=0;i<NUMPIXELS;i++) { //turn all off
pixels.setPixelColor(i, 0,0,0);
}
if(KnobValue<0)
{
encoderValue=0;
}
if(KnobValue>0)
{
int MinValue=min(abs(MaxPixelID-KnobValue),MaxPixelID);
for(int i=MaxPixelID;i>MinValue;i--) {
pixels.setPixelColor(i, Red,Green,Blue);
}
}
pixels.setPixelColor(0, Red,Green,Blue); //pixel 0 always on
pixels.setBrightness(mybrightness);
pixels.show();
}
void AnimationContinuous(signed int KnobValue, int Red, int Green, int Blue) //Animation bidirectional centered on pixel 0 <------|------>
{
for(int i=0;i<NUMPIXELS;i++) { //turn all off
pixels.setPixelColor(i, 0,0,0);
}
if(KnobValue<0) {
int MaxValue=abs(KnobValue);
for(int i=0;i<MaxValue;i++){
pixels.setPixelColor(i, Red,Green,Blue);
}
}
if(KnobValue>0)
{
int MinValue=min(abs(MaxPixelID-KnobValue),MaxPixelID);
for(int i=MaxPixelID;i>MinValue;i--) {
pixels.setPixelColor(i, Red,Green,Blue);
}
}
pixels.setPixelColor(0, Red,Green,Blue); //pixel 0 always on
pixels.setBrightness(mybrightness);
pixels.show();
}
//Here's all the shortcuts pre-defined. CW=clockwise; CCW=counterclockwise. Do NOT put any delay() in those functions as it is incompatible with the interrupt
void ShortcutActionCW0(){//Undo
//Keyboard.press(KEY_LEFT_GUI);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.write('z');
Keyboard.releaseAll();//release
}
void ShortcutActionCCW0(){//Redo
//Keyboard.press(KEY_LEFT_GUI);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_SHIFT);//SHIFT+CMD+z
Keyboard.write('z');
Keyboard.releaseAll();//release
}
void ShortcutActionCW1(){
Consumer.write(MEDIA_VOLUME_UP);
}
void ShortcutActionCCW1(){
Consumer.write(MEDIA_VOLUME_DOWN);
}
void ShortcutActionCW2(){
Keyboard.press(KEY_LEFT_GUI);
//Keyboard.write('w');
Keyboard.releaseAll();//release
}
void ShortcutActionCCW2(){
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press(KEY_LEFT_SHIFT);//SHIFT+CMD+z
//Keyboard.write('t');
Keyboard.releaseAll();//release
}
void ShortcutActionCW3(){
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press(KEY_LEFT_ALT);//SHIFT+CMD+z
Keyboard.press(KEY_RIGHT_ARROW);
Keyboard.releaseAll();//release
}
void ShortcutActionCCW3(){
Keyboard.begin();
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press(KEY_LEFT_ALT);//SHIFT+CMD+z
Keyboard.press(KEY_LEFT_ARROW);
Keyboard.releaseAll();//release
}
void ShortcutActionCW4(){
}
void ShortcutActionCCW4(){
}
///////End of Shortcuts definition
void updateEncoder(){ //this function is called on interrupt
OldValue=readEncoderValue();
int MSB = digitalRead(encoderPinA); //MSB = most significant bit
int LSB = digitalRead(encoderPinB); //LSB = least significant bit
int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++;
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;
NewValue=readEncoderValue();
if(NewValue!=OldValue)
{
Serial.println(NewValue);
}
if(NewValue>OldValue) //Clockwise turn
{
Serial.println("Forward");
if(Mode==0)
{
ShortcutActionCW0();
AnimationStop(NewValue, Red[Mode], Green[Mode], Blue[Mode]);
}
if(Mode==1)
{
ShortcutActionCW1();
AnimationContinuous(NewValue, Red[Mode], Green[Mode], Blue[Mode]);
}
if(Mode==2)
{
ShortcutActionCW2();
AnimationContinuous(NewValue, Red[Mode], Green[Mode], Blue[Mode]);
}
if(Mode==3)
{
ShortcutActionCW3();
AnimationContinuous(NewValue, Red[Mode], Green[Mode], Blue[Mode]);
}
if(Mode==4)
{
ShortcutActionCW4();
AnimationContinuous(NewValue, Red[Mode], Green[Mode], Blue[Mode]);
}
}
if(NewValue<OldValue) //AntiClockwise turn
{
Serial.println("Backward");
if(Mode==0)
{
ShortcutActionCCW0();
AnimationStop(NewValue, Red[Mode], Green[Mode], Blue[Mode]);
}
if(Mode==1)
{
ShortcutActionCCW1();
AnimationContinuous(NewValue, Red[Mode], Green[Mode], Blue[Mode]);
}
if(Mode==2)
{
ShortcutActionCCW2();
AnimationContinuous(NewValue, Red[Mode], Green[Mode], Blue[Mode]);
}
if(Mode==3)
{
ShortcutActionCCW3();
AnimationContinuous(NewValue, Red[Mode], Green[Mode], Blue[Mode]);
}
if(Mode==4)
{
ShortcutActionCCW4();
AnimationContinuous(NewValue, Red[Mode], Green[Mode], Blue[Mode]);
}
}
lastEncoded = encoded; //store this value for next time
}