-
Notifications
You must be signed in to change notification settings - Fork 0
/
das-control-panel.ino
122 lines (101 loc) · 2.58 KB
/
das-control-panel.ino
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
//http://www.pjrc.com/teensy/td_libs_Encoder.html
//https://github.com/PaulStoffregen/Encoder
#include <Encoder.h>
#include <Keyboard.h>
int ENCODER_BUTTON_PIN = 4;
int ACCELERATION = 4; //Accelerationeration factor for volume, use between 1 and 8
int K1 = 8;
int K2 = 7;
int K3 = 6;
int K4 = 5;
int DELAY_BEFORE_KEY_RELEASE = 100;
//Pins 2,3 are the interrupt pins on a Leonardo/Uno, which give best performance with a rotary encoder
//Use other pins if you wish, but performance may suffer. Avoid using pins that have LEDs attached
Encoder encoder(2, 3);
long lastPosition = 0;
void setup() {
pinMode(ENCODER_BUTTON_PIN, INPUT);
pinMode(K1, INPUT);
pinMode(K2, INPUT);
pinMode(K3, INPUT);
pinMode(K4, INPUT);
//Activate internal pull-up resistors
digitalWrite(ENCODER_BUTTON_PIN, HIGH);
digitalWrite(K1, HIGH);
digitalWrite(K2, HIGH);
digitalWrite(K3, HIGH);
digitalWrite(K4, HIGH);
Serial.begin(9600);
Keyboard.begin();
}
void loop() {
long currentPosition = encoder.read() / ACCELERATION;
if (currentPosition != lastPosition) {
Serial.println(currentPosition);
if (lastPosition < currentPosition)
volumeUp();
else
volumeDown();
lastPosition = currentPosition;
}
handleKeyPress(ENCODER_BUTTON_PIN, playOrPause, "PLAY_OR_PAUSE");
handleKeyPress(K1, lock, "LOCK");
handleKeyPress(K2, mute, "MUTE");
handleKeyPress(K3, next, "NEXT");
handleKeyPress(K4, prev, "PREV");
delay(50);
}
int handleKeyPress(int pin, void (*action)(), String actionName) {
int state = digitalRead(pin);
if (!state) {
Serial.println(actionName);
(*action)();
}
do { state = digitalRead(pin); } while (!state);
return state;
}
void mute() {
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_F7);
releaseAll();
}
void prev() {
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_F8);
releaseAll();
}
void playOrPause() {
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_F9);
releaseAll();
}
void next() {
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_F10);
releaseAll();
}
void volumeDown() {
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_F11);
releaseAll();
}
void volumeUp() {
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_F12);
releaseAll();
}
void lock() {
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press('l');
releaseAll();
}
void releaseAll() {
delay(DELAY_BEFORE_KEY_RELEASE);
Keyboard.releaseAll();
}