-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVolumeKnob.ino
83 lines (70 loc) · 2.07 KB
/
VolumeKnob.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
// Based on the Oversized Media Control Volume Knob project from Prusa Research.
// https://blog.prusaprinters.org/3d-print-an-oversized-media-control-volume-knob-arduino-basics_30184/
#include <ClickEncoder.h>
#include <TimerOne.h>
#include <HID-Project.h>
#define ENCODER_CLK A0
#define ENCODER_DT A1
#define ENCODER_SW A2
volatile ClickEncoder encoder = ClickEncoder(ENCODER_DT, ENCODER_CLK, ENCODER_SW);
void timerISR() {
encoder.service();
}
void setup() {
encoder.setAccelerationEnabled(true);
Serial.begin(115200);
Consumer.begin();
Mouse.begin();
Timer1.initialize(1000);
Timer1.attachInterrupt(timerISR);
}
const bool MODE_VOLUME = false;
const bool MODE_SCROLL = true;
void loop() {
static bool mode = MODE_VOLUME;
static bool isHeld = false;
static int16_t realValue = 0;
realValue += encoder.getValue();
switch (encoder.getButton()) {
case ClickEncoder::Clicked:
Serial.println("clicked");
if (mode == MODE_VOLUME) Consumer.write(MEDIA_PLAY_PAUSE);
break;
case ClickEncoder::DoubleClicked:
Serial.println("double clicked");
if (mode == MODE_VOLUME) Consumer.write(MEDIA_NEXT);
break;
case ClickEncoder::Held:
Serial.println("held");
isHeld = true;
break;
case ClickEncoder::Open:
if (isHeld) {
Serial.println("long clicked");
// Consumer.write(MEDIA_PREV);
isHeld = false;
mode = !mode;
}
int16_t fullStepValue = realValue / 4;
static int16_t fullStepLast = 42;
if (mode == MODE_VOLUME) {
if (fullStepValue != fullStepLast) {
if (fullStepLast < fullStepValue){
Consumer.write(MEDIA_VOLUME_UP);
} else {
Consumer.write(MEDIA_VOLUME_DOWN);
}
}
} else if (mode == MODE_SCROLL) {
if (fullStepValue != fullStepLast) {
if (fullStepValue < fullStepLast) {
Mouse.move(0, 0, 1);
} else {
Mouse.move(0, 0, -1);
}
}
}
fullStepLast = fullStepValue;
break;
}
}