-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknob.ino
103 lines (82 loc) · 1.98 KB
/
knob.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
// KY-040 volume controller
// Wiring map
// CLK <-> D11
// DT <-> D9
// SW <-> D10
// + <-> 5v
// GND <-> GND
// https://github.com/NicoHood/HID
// needed to control media keys
#include "HID-Project.h"
int CLK_PIN = 11; // CLK pin
int DT_PIN = 9; // DT pin
int SW_PIN = 10; // SW pin (click)
int clkPinLastValue;
bool wasKnobDown;
void setup()
{
// init pins, use pullup for internal resistors, seems to stabilize it a bit
pinMode (CLK_PIN, INPUT_PULLUP);
pinMode (DT_PIN, INPUT_PULLUP);
pinMode (SW_PIN, INPUT_PULLUP);
// init pin last values
clkPinLastValue = digitalRead(CLK_PIN);
wasKnobDown = digitalRead(SW_PIN) == 0;
Consumer.begin();
Serial.begin (9600);
}
void loop()
{
handleRotation();
handleKnobPress();
}
void handleRotation()
{
int clkPinCurrValue = digitalRead(CLK_PIN);
if (clkPinCurrValue == clkPinLastValue) // no rotation happening, nothing to do
return;
if(digitalRead(DT_PIN) == clkPinCurrValue) // if DT and CLK are the same, rotation is CCW
onRotateCCW();
else
onRotateCW();
clkPinLastValue = clkPinCurrValue;
}
void handleKnobPress()
{
bool isKnobDown = digitalRead(SW_PIN) == 0; // LOW when knob pressed
if(isKnobDown && !wasKnobDown)
onKnobDown();
else if(!isKnobDown && wasKnobDown)
onKnobUp();
else if(isKnobDown && wasKnobDown)
onKnobHeld();
//else if (!isKnobDown && !wasKnobDown)
// onKnobReleased();
wasKnobDown = isKnobDown;
}
// ############# CALLBACKS #############
void onRotateCW()
{
Consumer.write(MEDIA_VOLUME_UP);
}
void onRotateCCW()
{
Consumer.write(MEDIA_VOLUME_DOWN);
}
void onKnobDown() // called one time on press
{
Consumer.write(MEDIA_VOLUME_MUTE);
}
void onKnobHeld() // called each tick knob is held after initial down
{
//Serial.println("held");
}
void onKnobUp() // called one time on release
{
//Serial.println("up");
}
// off by default
void onKnobReleased () // called each tick knob is not held after initial up
{
//Serial.println("released");
}