forked from sandeepmistry/arduino-BLEPeripheral
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHID_keypad.ino
More file actions
72 lines (56 loc) · 1.84 KB
/
HID_keypad.ino
File metadata and controls
72 lines (56 loc) · 1.84 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
// 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 (BLEPeripheralObserver depends on SPI)
#include <SPI.h>
#include <BLEHIDPeripheral.h>
#include <BLEKeyboard.h>
// From: http://playground.arduino.cc/Code/Keypad
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = { A0, A1, A2, A3 }; //connect to the row pinouts of the keypad
byte colPins[COLS] = { 7, 6, 5 }; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
//custom boards may override default pin definitions with BLEHIDPeripheral(PIN_REQ, PIN_RDY, PIN_RST)
BLEHIDPeripheral bleHIDPeripheral = BLEHIDPeripheral();
BLEKeyboard bleKeyboard;
void setup() {
Serial.begin(9600);
#if defined (__AVR_ATmega32U4__)
while(!Serial);
#endif
char c = keypad.getKey();
if (c == '#') {
Serial.println(F("BLE HID Peripheral - clearing bond data"));
// clear bond store data
bleHIDPeripheral.clearBondStoreData();
}
bleHIDPeripheral.setLocalName("HID Keypad");
bleHIDPeripheral.addHID(bleKeyboard);
bleHIDPeripheral.begin();
Serial.println(F("BLE HID Keypad"));
}
void loop() {
BLECentral central = bleHIDPeripheral.central();
if (central) {
// central connected to peripheral
Serial.print(F("Connected to central: "));
Serial.println(central.address());
while (central.connected()) {
char c = keypad.getKey();
if (c) {
Serial.println(c);
bleKeyboard.print(c);
}
}
// central disconnected
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
}