adding hid central

This commit is contained in:
hathach committed 2018-02-12 15:17:23 +07:00
1 parent b58d2c1f20
commit 98e69635ee
8 files changed
+358

No files matched your search

+5
View File
@@ -1,5 +1,10 @@
# Adafruit nRF52 Arduino Core Changelog
## 0.8.3
- Enhance indicat API() to wait for confirm or timeout from peer.
- Add BLEScanner filterService() for BLEService and BLEClientService
## 0.8.2
- Fixed burning bootloader issue with MacOS
@@ -0,0 +1,135 @@
/*********************************************************************
This is an example for our nRF52 based Bluefruit LE modules
Pick one up today in the adafruit shop!
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in
any redistribution
*********************************************************************/
/*
* This sketch demonstrate the central API(). A additional bluefruit
* that has bleuart as peripheral is required for the demo.
*/
#include <bluefruit.h>
BLEClientHidAdafruit hid;
void setup()
{
Serial.begin(115200);
Serial.println("Bluefruit52 Central HID (Keyboard + Mouse) Example");
Serial.println("--------------------------------------------------\n");
// Initialize Bluefruit with maximum connections as Peripheral = 0, Central = 1
// SRAM usage required by SoftDevice will increase dramatically with number of connections
Bluefruit.begin(0, 1);
Bluefruit.setName("Bluefruit52 Central");
// Init BLE Central Uart Serivce
hid.begin();
//clientUart.setRxCallback(bleuart_rx_callback);
// Increase Blink rate to different from PrPh advertising mode
Bluefruit.setConnLedInterval(250);
// Callbacks for Central
Bluefruit.Central.setConnectCallback(connect_callback);
Bluefruit.Central.setDisconnectCallback(disconnect_callback);
/* Start Central Scanning
* - Enable auto scan if disconnected
* - Interval = 100 ms, window = 80 ms
* - Don't use active scan
* - Start(timeout) with timeout = 0 will scan forever (until connected)
*/
Bluefruit.Scanner.setRxCallback(scan_callback);
Bluefruit.Scanner.restartOnDisconnect(true);
Bluefruit.Scanner.setInterval(160, 80); // in unit of 0.625 ms
Bluefruit.Scanner.useActiveScan(false);
Bluefruit.Scanner.start(0); // 0 = Don't stop scanning after n seconds
Bluefruit.Scanner.filterService(hid); // only report HID service
}
/**
* Callback invoked when scanner pick up an advertising data
* @param report Structural advertising data
*/
void scan_callback(ble_gap_evt_adv_report_t* report)
{
// Connect to device
Bluefruit.Central.connect(report);
}
/**
* Callback invoked when an connection is established
* @param conn_handle
*/
void connect_callback(uint16_t conn_handle)
{
Serial.println("Connected");
Serial.print("Discovering HID Service ... ");
if ( hid.discover(conn_handle) )
{
Serial.println("Found it");
// Serial.println("Enable TXD's notify");
// clientUart.enableTXD();
Serial.println("Ready to receive from peripheral");
}else
{
Serial.println("Found NONE");
// disconect since we couldn't find bleuart service
Bluefruit.Central.disconnect(conn_handle);
}
}
/**
* Callback invoked when a connection is dropped
* @param conn_handle
* @param reason
*/
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
(void) conn_handle;
(void) reason;
Serial.println("Disconnected");
}
/**
* Callback invoked when uart received data
* @param uart_svc Reference object to the service where the data
* arrived. In this example it is clientUart
*/
//void bleuart_rx_callback(BLEClientUart& uart_svc)
//{
// Serial.print("[RX]: ");
//
// while ( uart_svc.available() )
// {
// Serial.print( (char) uart_svc.read() );
// }
//
// Serial.println();
//}
void loop()
{
// nothing to do
if ( !hid.discovered() ) return;
}
@@ -95,6 +95,12 @@ class BLEDiscovery
return discoverCharacteristic(conn_handle, chr_arr, arrcount(chr_arr));
}
uint8_t discoverCharacteristic(uint16_t conn_handle, BLEClientCharacteristic& chr1, BLEClientCharacteristic& chr2, BLEClientCharacteristic& chr3, BLEClientCharacteristic& chr4, BLEClientCharacteristic& chr5, BLEClientCharacteristic& chr6)
{
BLEClientCharacteristic* chr_arr[] = {&chr1, &chr2, &chr3, &chr4, &chr5, &chr6};
return discoverCharacteristic(conn_handle, chr_arr, arrcount(chr_arr));
}
/*------------------------------------------------------------------*/
/* INTERNAL USAGE ONLY
* Although declare as public, it is meant to be invoked by internal
@@ -277,6 +277,16 @@ void BLEScanner::filterUuid(BLEUuid ble_uuid[], uint8_t count)
for(uint8_t i=0; i<count; i++) _filter_uuid[i] = ble_uuid[i];
}
void BLEScanner::filterService(BLEService& svc)
{
filterUuid(svc.uuid);
}
void BLEScanner::filterService(BLEClientService& cli)
{
filterUuid(cli.uuid);
}
void BLEScanner::filterMSD(uint16_t manuf_id)
{
_filter_msd_en = true;
@@ -68,6 +68,9 @@ public:
void filterUuid(BLEUuid ble_uuid1, BLEUuid ble_uuid2, BLEUuid ble_uuid3, BLEUuid ble_uuid4);
void filterUuid(BLEUuid ble_uuid[], uint8_t count);
void filterService(BLEService& svc);
void filterService(BLEClientService& cli);
void clearFilters(void);
bool start(uint16_t timeout = 0);
+1
View File
@@ -87,6 +87,7 @@
#include "clients/BLEClientUart.h"
#include "clients/BLEClientDis.h"
#include "clients/BLEClientCts.h"
#include "clients/BLEClientHidAdafruit.h"
#include "utility/AdaCallback.h"
@@ -0,0 +1,119 @@
/**************************************************************************/
/*!
@file BLEClientHidAdafruit.cpp
@author hathach (tinyusb.org)
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2018, Adafruit Industries (adafruit.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**************************************************************************/
#include "bluefruit.h"
void kbd_client_notify_cb(BLEClientCharacteristic* chr, uint8_t* data, uint16_t len)
{
}
void mouse_client_notify_cb(BLEClientCharacteristic* chr, uint8_t* data, uint16_t len)
{
}
BLEClientHidAdafruit::BLEClientHidAdafruit(void)
: BLEClientService(UUID16_SVC_HUMAN_INTERFACE_DEVICE),
_protcol_mode(UUID16_CHR_PROTOCOL_MODE),
_kbd_boot_input(UUID16_CHR_BOOT_KEYBOARD_INPUT_REPORT), _kbd_boot_output(UUID16_CHR_BOOT_KEYBOARD_OUTPUT_REPORT),
_mouse_boot_input(UUID16_CHR_BOOT_MOUSE_INPUT_REPORT),
_hid_info(UUID16_CHR_HID_INFORMATION), _hid_control(UUID16_CHR_HID_CONTROL_POINT)
{
_country = 0;
}
bool BLEClientHidAdafruit::begin(void)
{
// Invoke base class begin()
BLEClientService::begin();
_kbd_boot_input.begin(this);
_kbd_boot_output.begin(this);
_mouse_boot_input.begin(this);
// set notify callback
_kbd_boot_input.setNotifyCallback(kbd_client_notify_cb);
_mouse_boot_input.setNotifyCallback(mouse_client_notify_cb);
}
bool BLEClientHidAdafruit::discover(uint16_t conn_handle)
{
// Call Base class discover
VERIFY( BLEClientService::discover(conn_handle) );
_conn_hdl = BLE_CONN_HANDLE_INVALID; // make as invalid until we found all chars
// Discover all characteristics
Bluefruit.Discovery.discoverCharacteristic(conn_handle, _protcol_mode, _kbd_boot_input, _kbd_boot_output, _mouse_boot_input, _hid_info, _hid_control);
VERIFY( _protcol_mode.discovered() && _hid_info.discovered() && _hid_control.discovered() );
VERIFY ( keyboardPresent() || mousePresent() );
_conn_hdl = conn_handle;
return true;
}
bool BLEClientHidAdafruit::keyboardPresent(void)
{
return _kbd_boot_input.discovered() && _kbd_boot_output.discovered();
}
bool BLEClientHidAdafruit::mousePresent(void)
{
return _mouse_boot_input.discovered();
}
bool BLEClientHidAdafruit::enableKeyboard(void)
{
_kbd_boot_input.enableNotify();
}
bool BLEClientHidAdafruit::enableMouse(void)
{
_mouse_boot_input.enableNotify();
}
bool BLEClientHidAdafruit::disableKeyboard(void)
{
_kbd_boot_input.disableNotify();
}
bool BLEClientHidAdafruit::disableMouse(void)
{
_mouse_boot_input.disableNotify();
}
@@ -0,0 +1,79 @@
/**************************************************************************/
/*!
@file BLEClientHidAdafruit.h
@author hathach (tinyusb.org)
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2018, Adafruit Industries (adafruit.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**************************************************************************/
#ifndef BLECLIENTHIDADAFRUIT_H_
#define BLECLIENTHIDADAFRUIT_H_
#include "bluefruit_common.h"
#include "BLEClientCharacteristic.h"
#include "BLEClientService.h"
#include "services/BLEHidGeneric.h"
// Only support Boot Keyboard and/or Boot Mouse, there is no Consumer Control support
class BLEClientHidAdafruit : public BLEClientService
{
public:
BLEClientHidAdafruit(void);
virtual bool begin(void);
virtual bool discover(uint16_t conn_handle);
bool keyboardPresent(void);
bool mousePresent(void);
bool enableKeyboard(void);
bool disableKeyboard(void);
bool enableMouse(void);
bool disableMouse(void);
private:
uint8_t _country;
// Only support Boot protocol for keyboard and Mouse
BLEClientCharacteristic _protcol_mode;
BLEClientCharacteristic _hid_info;
BLEClientCharacteristic _hid_control;
BLEClientCharacteristic _kbd_boot_input;
BLEClientCharacteristic _kbd_boot_output;
BLEClientCharacteristic _mouse_boot_input;
};
#endif /* BLECLIENTHIDADAFRUIT_H_ */