Skip to content

Commit 98e6963

Browse files
committed
adding hid central
1 parent b58d2c1 commit 98e6963

File tree

8 files changed

+358
-0
lines changed

8 files changed

+358
-0
lines changed

changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Adafruit nRF52 Arduino Core Changelog
22

3+
## 0.8.3
4+
5+
- Enhance indicat API() to wait for confirm or timeout from peer.
6+
- Add BLEScanner filterService() for BLEService and BLEClientService
7+
38
## 0.8.2
49

510
- Fixed burning bootloader issue with MacOS
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*********************************************************************
2+
This is an example for our nRF52 based Bluefruit LE modules
3+
4+
Pick one up today in the adafruit shop!
5+
6+
Adafruit invests time and resources providing this open source code,
7+
please support Adafruit and open-source hardware by purchasing
8+
products from Adafruit!
9+
10+
MIT license, check LICENSE for more information
11+
All text above, and the splash screen below must be included in
12+
any redistribution
13+
*********************************************************************/
14+
15+
/*
16+
* This sketch demonstrate the central API(). A additional bluefruit
17+
* that has bleuart as peripheral is required for the demo.
18+
*/
19+
#include <bluefruit.h>
20+
21+
BLEClientHidAdafruit hid;
22+
23+
void setup()
24+
{
25+
Serial.begin(115200);
26+
27+
Serial.println("Bluefruit52 Central HID (Keyboard + Mouse) Example");
28+
Serial.println("--------------------------------------------------\n");
29+
30+
// Initialize Bluefruit with maximum connections as Peripheral = 0, Central = 1
31+
// SRAM usage required by SoftDevice will increase dramatically with number of connections
32+
Bluefruit.begin(0, 1);
33+
34+
Bluefruit.setName("Bluefruit52 Central");
35+
36+
// Init BLE Central Uart Serivce
37+
hid.begin();
38+
//clientUart.setRxCallback(bleuart_rx_callback);
39+
40+
// Increase Blink rate to different from PrPh advertising mode
41+
Bluefruit.setConnLedInterval(250);
42+
43+
// Callbacks for Central
44+
Bluefruit.Central.setConnectCallback(connect_callback);
45+
Bluefruit.Central.setDisconnectCallback(disconnect_callback);
46+
47+
/* Start Central Scanning
48+
* - Enable auto scan if disconnected
49+
* - Interval = 100 ms, window = 80 ms
50+
* - Don't use active scan
51+
* - Start(timeout) with timeout = 0 will scan forever (until connected)
52+
*/
53+
Bluefruit.Scanner.setRxCallback(scan_callback);
54+
Bluefruit.Scanner.restartOnDisconnect(true);
55+
Bluefruit.Scanner.setInterval(160, 80); // in unit of 0.625 ms
56+
Bluefruit.Scanner.useActiveScan(false);
57+
Bluefruit.Scanner.start(0); // 0 = Don't stop scanning after n seconds
58+
59+
Bluefruit.Scanner.filterService(hid); // only report HID service
60+
}
61+
62+
/**
63+
* Callback invoked when scanner pick up an advertising data
64+
* @param report Structural advertising data
65+
*/
66+
void scan_callback(ble_gap_evt_adv_report_t* report)
67+
{
68+
// Connect to device
69+
Bluefruit.Central.connect(report);
70+
}
71+
72+
/**
73+
* Callback invoked when an connection is established
74+
* @param conn_handle
75+
*/
76+
void connect_callback(uint16_t conn_handle)
77+
{
78+
Serial.println("Connected");
79+
80+
Serial.print("Discovering HID Service ... ");
81+
82+
if ( hid.discover(conn_handle) )
83+
{
84+
Serial.println("Found it");
85+
86+
// Serial.println("Enable TXD's notify");
87+
// clientUart.enableTXD();
88+
89+
Serial.println("Ready to receive from peripheral");
90+
}else
91+
{
92+
Serial.println("Found NONE");
93+
94+
// disconect since we couldn't find bleuart service
95+
Bluefruit.Central.disconnect(conn_handle);
96+
}
97+
}
98+
99+
/**
100+
* Callback invoked when a connection is dropped
101+
* @param conn_handle
102+
* @param reason
103+
*/
104+
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
105+
{
106+
(void) conn_handle;
107+
(void) reason;
108+
109+
Serial.println("Disconnected");
110+
}
111+
112+
/**
113+
* Callback invoked when uart received data
114+
* @param uart_svc Reference object to the service where the data
115+
* arrived. In this example it is clientUart
116+
*/
117+
//void bleuart_rx_callback(BLEClientUart& uart_svc)
118+
//{
119+
// Serial.print("[RX]: ");
120+
//
121+
// while ( uart_svc.available() )
122+
// {
123+
// Serial.print( (char) uart_svc.read() );
124+
// }
125+
//
126+
// Serial.println();
127+
//}
128+
129+
void loop()
130+
{
131+
// nothing to do
132+
if ( !hid.discovered() ) return;
133+
134+
}
135+

libraries/Bluefruit52Lib/src/BLEDiscovery.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ class BLEDiscovery
9595
return discoverCharacteristic(conn_handle, chr_arr, arrcount(chr_arr));
9696
}
9797

98+
uint8_t discoverCharacteristic(uint16_t conn_handle, BLEClientCharacteristic& chr1, BLEClientCharacteristic& chr2, BLEClientCharacteristic& chr3, BLEClientCharacteristic& chr4, BLEClientCharacteristic& chr5, BLEClientCharacteristic& chr6)
99+
{
100+
BLEClientCharacteristic* chr_arr[] = {&chr1, &chr2, &chr3, &chr4, &chr5, &chr6};
101+
return discoverCharacteristic(conn_handle, chr_arr, arrcount(chr_arr));
102+
}
103+
98104
/*------------------------------------------------------------------*/
99105
/* INTERNAL USAGE ONLY
100106
* Although declare as public, it is meant to be invoked by internal

libraries/Bluefruit52Lib/src/BLEScanner.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,16 @@ void BLEScanner::filterUuid(BLEUuid ble_uuid[], uint8_t count)
277277
for(uint8_t i=0; i<count; i++) _filter_uuid[i] = ble_uuid[i];
278278
}
279279

280+
void BLEScanner::filterService(BLEService& svc)
281+
{
282+
filterUuid(svc.uuid);
283+
}
284+
285+
void BLEScanner::filterService(BLEClientService& cli)
286+
{
287+
filterUuid(cli.uuid);
288+
}
289+
280290
void BLEScanner::filterMSD(uint16_t manuf_id)
281291
{
282292
_filter_msd_en = true;

libraries/Bluefruit52Lib/src/BLEScanner.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ class BLEScanner
6868
void filterUuid(BLEUuid ble_uuid1, BLEUuid ble_uuid2, BLEUuid ble_uuid3, BLEUuid ble_uuid4);
6969
void filterUuid(BLEUuid ble_uuid[], uint8_t count);
7070

71+
void filterService(BLEService& svc);
72+
void filterService(BLEClientService& cli);
73+
7174
void clearFilters(void);
7275

7376
bool start(uint16_t timeout = 0);

libraries/Bluefruit52Lib/src/bluefruit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
#include "clients/BLEClientUart.h"
8888
#include "clients/BLEClientDis.h"
8989
#include "clients/BLEClientCts.h"
90+
#include "clients/BLEClientHidAdafruit.h"
9091

9192
#include "utility/AdaCallback.h"
9293

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**************************************************************************/
2+
/*!
3+
@file BLEClientHidAdafruit.cpp
4+
@author hathach (tinyusb.org)
5+
6+
@section LICENSE
7+
8+
Software License Agreement (BSD License)
9+
10+
Copyright (c) 2018, Adafruit Industries (adafruit.com)
11+
All rights reserved.
12+
13+
Redistribution and use in source and binary forms, with or without
14+
modification, are permitted provided that the following conditions are met:
15+
1. Redistributions of source code must retain the above copyright
16+
notice, this list of conditions and the following disclaimer.
17+
2. Redistributions in binary form must reproduce the above copyright
18+
notice, this list of conditions and the following disclaimer in the
19+
documentation and/or other materials provided with the distribution.
20+
3. Neither the name of the copyright holders nor the
21+
names of its contributors may be used to endorse or promote products
22+
derived from this software without specific prior written permission.
23+
24+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
25+
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
28+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34+
*/
35+
/**************************************************************************/
36+
37+
#include "bluefruit.h"
38+
39+
void kbd_client_notify_cb(BLEClientCharacteristic* chr, uint8_t* data, uint16_t len)
40+
{
41+
42+
}
43+
44+
void mouse_client_notify_cb(BLEClientCharacteristic* chr, uint8_t* data, uint16_t len)
45+
{
46+
47+
}
48+
49+
BLEClientHidAdafruit::BLEClientHidAdafruit(void)
50+
: BLEClientService(UUID16_SVC_HUMAN_INTERFACE_DEVICE),
51+
_protcol_mode(UUID16_CHR_PROTOCOL_MODE),
52+
_kbd_boot_input(UUID16_CHR_BOOT_KEYBOARD_INPUT_REPORT), _kbd_boot_output(UUID16_CHR_BOOT_KEYBOARD_OUTPUT_REPORT),
53+
_mouse_boot_input(UUID16_CHR_BOOT_MOUSE_INPUT_REPORT),
54+
_hid_info(UUID16_CHR_HID_INFORMATION), _hid_control(UUID16_CHR_HID_CONTROL_POINT)
55+
{
56+
_country = 0;
57+
}
58+
59+
bool BLEClientHidAdafruit::begin(void)
60+
{
61+
// Invoke base class begin()
62+
BLEClientService::begin();
63+
64+
_kbd_boot_input.begin(this);
65+
_kbd_boot_output.begin(this);
66+
67+
_mouse_boot_input.begin(this);
68+
69+
// set notify callback
70+
_kbd_boot_input.setNotifyCallback(kbd_client_notify_cb);
71+
_mouse_boot_input.setNotifyCallback(mouse_client_notify_cb);
72+
73+
}
74+
75+
bool BLEClientHidAdafruit::discover(uint16_t conn_handle)
76+
{
77+
// Call Base class discover
78+
VERIFY( BLEClientService::discover(conn_handle) );
79+
_conn_hdl = BLE_CONN_HANDLE_INVALID; // make as invalid until we found all chars
80+
81+
// Discover all characteristics
82+
Bluefruit.Discovery.discoverCharacteristic(conn_handle, _protcol_mode, _kbd_boot_input, _kbd_boot_output, _mouse_boot_input, _hid_info, _hid_control);
83+
84+
VERIFY( _protcol_mode.discovered() && _hid_info.discovered() && _hid_control.discovered() );
85+
VERIFY ( keyboardPresent() || mousePresent() );
86+
87+
_conn_hdl = conn_handle;
88+
return true;
89+
}
90+
91+
bool BLEClientHidAdafruit::keyboardPresent(void)
92+
{
93+
return _kbd_boot_input.discovered() && _kbd_boot_output.discovered();
94+
}
95+
96+
bool BLEClientHidAdafruit::mousePresent(void)
97+
{
98+
return _mouse_boot_input.discovered();
99+
}
100+
101+
bool BLEClientHidAdafruit::enableKeyboard(void)
102+
{
103+
_kbd_boot_input.enableNotify();
104+
}
105+
106+
bool BLEClientHidAdafruit::enableMouse(void)
107+
{
108+
_mouse_boot_input.enableNotify();
109+
}
110+
111+
bool BLEClientHidAdafruit::disableKeyboard(void)
112+
{
113+
_kbd_boot_input.disableNotify();
114+
}
115+
116+
bool BLEClientHidAdafruit::disableMouse(void)
117+
{
118+
_mouse_boot_input.disableNotify();
119+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**************************************************************************/
2+
/*!
3+
@file BLEClientHidAdafruit.h
4+
@author hathach (tinyusb.org)
5+
6+
@section LICENSE
7+
8+
Software License Agreement (BSD License)
9+
10+
Copyright (c) 2018, Adafruit Industries (adafruit.com)
11+
All rights reserved.
12+
13+
Redistribution and use in source and binary forms, with or without
14+
modification, are permitted provided that the following conditions are met:
15+
1. Redistributions of source code must retain the above copyright
16+
notice, this list of conditions and the following disclaimer.
17+
2. Redistributions in binary form must reproduce the above copyright
18+
notice, this list of conditions and the following disclaimer in the
19+
documentation and/or other materials provided with the distribution.
20+
3. Neither the name of the copyright holders nor the
21+
names of its contributors may be used to endorse or promote products
22+
derived from this software without specific prior written permission.
23+
24+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
25+
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
28+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34+
*/
35+
/**************************************************************************/
36+
#ifndef BLECLIENTHIDADAFRUIT_H_
37+
#define BLECLIENTHIDADAFRUIT_H_
38+
39+
#include "bluefruit_common.h"
40+
#include "BLEClientCharacteristic.h"
41+
#include "BLEClientService.h"
42+
43+
#include "services/BLEHidGeneric.h"
44+
45+
// Only support Boot Keyboard and/or Boot Mouse, there is no Consumer Control support
46+
class BLEClientHidAdafruit : public BLEClientService
47+
{
48+
public:
49+
BLEClientHidAdafruit(void);
50+
51+
virtual bool begin(void);
52+
virtual bool discover(uint16_t conn_handle);
53+
54+
bool keyboardPresent(void);
55+
bool mousePresent(void);
56+
57+
bool enableKeyboard(void);
58+
bool disableKeyboard(void);
59+
60+
bool enableMouse(void);
61+
bool disableMouse(void);
62+
63+
private:
64+
uint8_t _country;
65+
66+
// Only support Boot protocol for keyboard and Mouse
67+
BLEClientCharacteristic _protcol_mode;
68+
BLEClientCharacteristic _hid_info;
69+
BLEClientCharacteristic _hid_control;
70+
71+
BLEClientCharacteristic _kbd_boot_input;
72+
BLEClientCharacteristic _kbd_boot_output;
73+
74+
BLEClientCharacteristic _mouse_boot_input;
75+
};
76+
77+
78+
79+
#endif /* BLECLIENTHIDADAFRUIT_H_ */

0 commit comments

Comments
 (0)