@@ -40,11 +40,29 @@ BLEClientHidAdafruit::BLEClientHidAdafruit(void)
40
40
: BLEClientService(UUID16_SVC_HUMAN_INTERFACE_DEVICE),
41
41
_protcol_mode(UUID16_CHR_PROTOCOL_MODE),
42
42
_kbd_boot_input(UUID16_CHR_BOOT_KEYBOARD_INPUT_REPORT), _kbd_boot_output(UUID16_CHR_BOOT_KEYBOARD_OUTPUT_REPORT),
43
- _mouse_boot_input (UUID16_CHR_BOOT_MOUSE_INPUT_REPORT),
43
+ _mse_boot_input (UUID16_CHR_BOOT_MOUSE_INPUT_REPORT),
44
44
_hid_info(UUID16_CHR_HID_INFORMATION), _hid_control(UUID16_CHR_HID_CONTROL_POINT)
45
45
{
46
+ _kbd_cb = NULL ;
47
+ _mse_cb = NULL ;
48
+ varclr (&_last_kbd_report);
49
+ varclr (&_last_mse_report);
46
50
}
47
51
52
+
53
+ void kbd_client_notify_cb (BLEClientCharacteristic* chr, uint8_t * data, uint16_t len)
54
+ {
55
+ BLEClientHidAdafruit& svc = (BLEClientHidAdafruit&) chr->parentService ();
56
+ svc._handle_kbd_input (data, len);
57
+ }
58
+
59
+ void mse_client_notify_cb (BLEClientCharacteristic* chr, uint8_t * data, uint16_t len)
60
+ {
61
+ BLEClientHidAdafruit& svc = (BLEClientHidAdafruit&) chr->parentService ();
62
+ svc._handle_mse_input (data, len);
63
+ }
64
+
65
+
48
66
bool BLEClientHidAdafruit::begin (void )
49
67
{
50
68
// Invoke base class begin()
@@ -57,12 +75,22 @@ bool BLEClientHidAdafruit::begin(void)
57
75
_kbd_boot_input.begin (this );
58
76
_kbd_boot_output.begin (this );
59
77
60
- _mouse_boot_input .begin (this );
78
+ _mse_boot_input .begin (this );
61
79
62
80
63
81
// set notify callback
64
- // _kbd_boot_input.setNotifyCallback(kbd_client_notify_cb);
65
- // _mouse_boot_input.setNotifyCallback(mouse_client_notify_cb);
82
+ _kbd_boot_input.setNotifyCallback (kbd_client_notify_cb);
83
+ _mse_boot_input.setNotifyCallback (mse_client_notify_cb);
84
+ }
85
+
86
+ void BLEClientHidAdafruit::setKeyboardReportCallback (kbd_callback_t fp)
87
+ {
88
+ _kbd_cb = fp;
89
+ }
90
+
91
+ void BLEClientHidAdafruit::setMouseReportCallback (mse_callback_t fp)
92
+ {
93
+ _mse_cb = fp;
66
94
}
67
95
68
96
bool BLEClientHidAdafruit::discover (uint16_t conn_handle)
@@ -72,7 +100,7 @@ bool BLEClientHidAdafruit::discover(uint16_t conn_handle)
72
100
_conn_hdl = BLE_CONN_HANDLE_INVALID; // make as invalid until we found all chars
73
101
74
102
// Discover all characteristics
75
- Bluefruit.Discovery .discoverCharacteristic (conn_handle, _protcol_mode, _kbd_boot_input, _kbd_boot_output, _mouse_boot_input , _hid_info, _hid_control);
103
+ Bluefruit.Discovery .discoverCharacteristic (conn_handle, _protcol_mode, _kbd_boot_input, _kbd_boot_output, _mse_boot_input , _hid_info, _hid_control);
76
104
77
105
VERIFY ( _protcol_mode.discovered () && _hid_info.discovered () && _hid_control.discovered () );
78
106
VERIFY ( keyboardPresent () || mousePresent () );
@@ -81,6 +109,9 @@ bool BLEClientHidAdafruit::discover(uint16_t conn_handle)
81
109
return true ;
82
110
}
83
111
112
+ /* ------------------------------------------------------------------*/
113
+ /* Info
114
+ *------------------------------------------------------------------*/
84
115
bool BLEClientHidAdafruit::getHidInfo (uint8_t info[4 ])
85
116
{
86
117
return 4 == _hid_info.read (info, 4 );
@@ -94,32 +125,70 @@ uint8_t BLEClientHidAdafruit::getCountryCode(void)
94
125
return info[2 ];
95
126
}
96
127
97
- bool BLEClientHidAdafruit::keyboardPresent ( void )
128
+ bool BLEClientHidAdafruit::setProtocolMode ( uint8_t mode )
98
129
{
99
- return _kbd_boot_input. discovered () && _kbd_boot_output. discovered ( );
130
+ return _protcol_mode. write8 (mode );
100
131
}
101
132
102
- bool BLEClientHidAdafruit::mousePresent (void )
133
+ /* ------------------------------------------------------------------*/
134
+ /* Keyboard
135
+ *------------------------------------------------------------------*/
136
+ bool BLEClientHidAdafruit::keyboardPresent (void )
103
137
{
104
- return _mouse_boot_input .discovered ();
138
+ return _kbd_boot_input. discovered () && _kbd_boot_output .discovered ();
105
139
}
106
140
107
141
bool BLEClientHidAdafruit::enableKeyboard (void )
108
142
{
109
143
_kbd_boot_input.enableNotify ();
110
144
}
111
145
112
- bool BLEClientHidAdafruit::enableMouse (void )
146
+ bool BLEClientHidAdafruit::disableKeyboard (void )
147
+ {
148
+ _kbd_boot_input.disableNotify ();
149
+ }
150
+
151
+ void BLEClientHidAdafruit::_handle_kbd_input (uint8_t * data, uint16_t len)
113
152
{
114
- _mouse_boot_input.enableNotify ();
153
+ varclr (&_last_kbd_report);
154
+ memcpy (&_last_kbd_report, data, len);
155
+
156
+ if ( _kbd_cb ) _kbd_cb (&_last_kbd_report);
115
157
}
116
158
117
- bool BLEClientHidAdafruit::disableKeyboard ( void )
159
+ void BLEClientHidAdafruit::getKeyboardReport ( hid_keyboard_report_t * report )
118
160
{
119
- _kbd_boot_input.disableNotify ();
161
+ memcpy (report, &_last_kbd_report, sizeof (hid_keyboard_report_t ));
162
+ }
163
+
164
+ /* ------------------------------------------------------------------*/
165
+ /* Mouse
166
+ *------------------------------------------------------------------*/
167
+ bool BLEClientHidAdafruit::mousePresent (void )
168
+ {
169
+ return _mse_boot_input.discovered ();
170
+ }
171
+
172
+ bool BLEClientHidAdafruit::enableMouse (void )
173
+ {
174
+ _mse_boot_input.enableNotify ();
120
175
}
121
176
122
177
bool BLEClientHidAdafruit::disableMouse (void )
123
178
{
124
- _mouse_boot_input .disableNotify ();
179
+ _mse_boot_input .disableNotify ();
125
180
}
181
+
182
+ void BLEClientHidAdafruit::_handle_mse_input (uint8_t * data, uint16_t len)
183
+ {
184
+ varclr (&_last_mse_report);
185
+ memcpy (&_last_mse_report, data, len);
186
+
187
+ if ( _mse_cb ) _mse_cb (&_last_mse_report);
188
+ }
189
+
190
+ void BLEClientHidAdafruit::getMouseReport (hid_mouse_report_t * report)
191
+ {
192
+ memcpy (report, &_last_mse_report, sizeof (hid_mouse_report_t ));
193
+ }
194
+
0 commit comments