-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathHID_volume.ino
120 lines (88 loc) · 2.73 KB
/
HID_volume.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
// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// Import libraries (BLEPeripheral depends on SPI)
#include <SPI.h>
#include <BLEHIDPeripheral.h>
#include <BLEMultimedia.h>
// http://www.pjrc.com/teensy/td_libs_Encoder.html
#include <Encoder.h>
#define BUTTON_PIN 5
#define ENC_RIGHT_PIN 3
#define ENC_LEFT_PIN 4
#define INPUT_POLL_INTERVAL 100
//#define ANDROID_CENTRAL
//custom boards may override default pin definitions with BLEHIDPeripheral(PIN_REQ, PIN_RDY, PIN_RST)
//but you will also need to set the pinmode for the output pins PIN_REQ and PIN_RST. See setup() below.
BLEHIDPeripheral bleHIDPeripheral = BLEHIDPeripheral();
BLEMultimedia bleMultimedia;
Encoder encoder(ENC_RIGHT_PIN, ENC_LEFT_PIN);
int buttonState;
unsigned long lastInputPollTime = 0;
void setup() {
//set pinMode, if you are using a custom board with different pin assignments
//pinMode(PIN_REQ, OUTPUT);
//pinMode(PIN_RST, OUTPUT);
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
buttonState = digitalRead(BUTTON_PIN);
if (buttonState == LOW) {
Serial.println(F("BLE HID Peripheral - clearing bond data"));
// clear bond store data
bleHIDPeripheral.clearBondStoreData();
}
encoder.write(0);
#ifdef ANDROID_CENTRAL
bleHIDPeripheral.setReportIdOffset(1);
#endif
bleHIDPeripheral.setLocalName("HID Volume");
bleHIDPeripheral.addHID(bleMultimedia);
bleHIDPeripheral.begin();
Serial.println(F("BLE HID Volume Knob"));
}
void loop() {
BLECentral central = bleHIDPeripheral.central();
if (central) {
// central connected to peripheral
Serial.print(F("Connected to central: "));
Serial.println(central.address());
while (bleHIDPeripheral.connected()) {
pollInputs();
}
// central disconnected
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
}
void pollInputs() {
// only poll the input every 100ms
if (millis() - lastInputPollTime > INPUT_POLL_INTERVAL) {
pollButton();
pollEncoder();
lastInputPollTime = millis();
}
}
void pollButton() {
// check the button
int tempButtonState = digitalRead(BUTTON_PIN);
if (tempButtonState != buttonState) {
buttonState = tempButtonState;
if (buttonState == LOW) {
Serial.println(F("Mute"));
bleMultimedia.write(MMKEY_MUTE);
}
}
}
void pollEncoder() {
// check the encoder
int encoderState = encoder.read();
if (encoderState != 0) {
if (encoderState > 0) {
Serial.println(F("Volume up"));
bleMultimedia.write(MMKEY_VOL_UP);
} else {
Serial.println(F("Volume down"));
bleMultimedia.write(MMKEY_VOL_DOWN);
}
encoder.write(0);
}
}