2015-08-29 16:12:51 +00:00
|
|
|
/*
|
2015-08-30 17:06:56 +00:00
|
|
|
Copyright (c) 2014-2015 NicoHood
|
|
|
|
|
See the readme for credit to other people.
|
2015-08-29 16:12:51 +00:00
|
|
|
|
2015-08-30 17:06:56 +00:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
furnished to do so, subject to the following conditions:
|
2015-08-29 16:12:51 +00:00
|
|
|
|
2015-08-30 17:06:56 +00:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
|
all copies or substantial portions of the Software.
|
2015-08-29 16:12:51 +00:00
|
|
|
|
2015-08-30 17:06:56 +00:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
THE SOFTWARE.
|
2015-08-29 16:12:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "ImprovedKeyboard.h"
|
|
|
|
|
|
|
|
|
|
#if defined(_USING_HID)
|
|
|
|
|
|
|
|
|
|
//================================================================================
|
|
|
|
|
//================================================================================
|
|
|
|
|
// Keyboard
|
|
|
|
|
|
|
|
|
|
static const u8 _hidReportDescriptor[] PROGMEM = {
|
|
|
|
|
|
|
|
|
|
// Keyboard
|
|
|
|
|
0x05, 0x01, // USAGE_PAGE (Generic Desktop) // 47
|
|
|
|
|
0x09, 0x06, // USAGE (Keyboard)
|
|
|
|
|
0xa1, 0x01, // COLLECTION (Application)
|
2015-09-08 15:31:29 +00:00
|
|
|
0x85, HID_REPORTID_KEYBOARD, // REPORT_ID
|
2015-08-29 16:12:51 +00:00
|
|
|
0x05, 0x07, // USAGE_PAGE (Keyboard)
|
|
|
|
|
|
|
|
|
|
0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
|
|
|
|
|
0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
|
|
|
|
|
0x15, 0x00, // LOGICAL_MINIMUM (0)
|
|
|
|
|
0x25, 0x01, // LOGICAL_MAXIMUM (1)
|
|
|
|
|
0x75, 0x01, // REPORT_SIZE (1)
|
|
|
|
|
|
|
|
|
|
0x95, 0x08, // REPORT_COUNT (8)
|
|
|
|
|
0x81, 0x02, // INPUT (Data,Var,Abs)
|
|
|
|
|
0x95, 0x01, // REPORT_COUNT (1)
|
|
|
|
|
0x75, 0x08, // REPORT_SIZE (8)
|
|
|
|
|
0x81, 0x03, // INPUT (Cnst,Var,Abs)
|
|
|
|
|
|
|
|
|
|
0x95, 0x06, // REPORT_COUNT (6)
|
|
|
|
|
0x75, 0x08, // REPORT_SIZE (8)
|
|
|
|
|
0x15, 0x00, // LOGICAL_MINIMUM (0)
|
|
|
|
|
0x25, 0x65, // LOGICAL_MAXIMUM (101)
|
|
|
|
|
0x05, 0x07, // USAGE_PAGE (Keyboard)
|
|
|
|
|
|
|
|
|
|
0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
|
|
|
|
|
0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
|
|
|
|
|
0x81, 0x00, // INPUT (Data,Ary,Abs)
|
|
|
|
|
0xc0, // END_COLLECTION
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Keyboard_::Keyboard_(void)
|
|
|
|
|
{
|
|
|
|
|
static HID_Descriptor cb = {
|
|
|
|
|
.length = sizeof(_hidReportDescriptor),
|
|
|
|
|
.descriptor = _hidReportDescriptor,
|
|
|
|
|
};
|
|
|
|
|
static HIDDescriptorListNode node(&cb);
|
|
|
|
|
HID.AppendDescriptor(&node);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Keyboard_::begin(void)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Keyboard_::end(void)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Keyboard_::sendReport(KeyReport* keys)
|
|
|
|
|
{
|
2015-09-06 17:51:30 +00:00
|
|
|
HID.SendReport(HID_REPORTID_KEYBOARD,keys,sizeof(KeyReport));
|
2015-08-29 16:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// press() adds the specified key (printing, non-printing, or modifier)
|
|
|
|
|
// to the persistent key report and sends the report. Because of the way
|
|
|
|
|
// USB HID works, the host acts like the key remains pressed until we
|
|
|
|
|
// call release(), releaseAll(), or otherwise clear the report and resend.
|
|
|
|
|
size_t Keyboard_::press(uint8_t k)
|
|
|
|
|
{
|
|
|
|
|
uint8_t i;
|
|
|
|
|
if (k >= 136) { // it's a non-printing key (not a modifier)
|
|
|
|
|
k = k - 136;
|
|
|
|
|
} else if (k >= 128) { // it's a modifier key
|
|
|
|
|
_keyReport.modifiers |= (1<<(k-128));
|
|
|
|
|
k = 0;
|
|
|
|
|
} else { // it's a printing key
|
2015-09-08 15:57:04 +00:00
|
|
|
k = pgm_read_byte(_asciimap + k);
|
2015-08-29 16:12:51 +00:00
|
|
|
if (!k) {
|
|
|
|
|
setWriteError();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (k & 0x80) { // it's a capital letter or other character reached with shift
|
|
|
|
|
_keyReport.modifiers |= 0x02; // the left shift modifier
|
|
|
|
|
k &= 0x7F;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add k to the key report only if it's not already present
|
|
|
|
|
// and if there is an empty slot.
|
|
|
|
|
if (_keyReport.keys[0] != k && _keyReport.keys[1] != k &&
|
|
|
|
|
_keyReport.keys[2] != k && _keyReport.keys[3] != k &&
|
|
|
|
|
_keyReport.keys[4] != k && _keyReport.keys[5] != k) {
|
|
|
|
|
|
|
|
|
|
for (i=0; i<6; i++) {
|
|
|
|
|
if (_keyReport.keys[i] == 0x00) {
|
|
|
|
|
_keyReport.keys[i] = k;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (i == 6) {
|
|
|
|
|
setWriteError();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sendReport(&_keyReport);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// release() takes the specified key out of the persistent key report and
|
|
|
|
|
// sends the report. This tells the OS the key is no longer pressed and that
|
|
|
|
|
// it shouldn't be repeated any more.
|
|
|
|
|
size_t Keyboard_::release(uint8_t k)
|
|
|
|
|
{
|
|
|
|
|
uint8_t i;
|
|
|
|
|
if (k >= 136) { // it's a non-printing key (not a modifier)
|
|
|
|
|
k = k - 136;
|
|
|
|
|
} else if (k >= 128) { // it's a modifier key
|
|
|
|
|
_keyReport.modifiers &= ~(1<<(k-128));
|
|
|
|
|
k = 0;
|
|
|
|
|
} else { // it's a printing key
|
2015-09-08 15:57:04 +00:00
|
|
|
k = pgm_read_byte(_asciimap + k);
|
2015-08-29 16:12:51 +00:00
|
|
|
if (!k) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (k & 0x80) { // it's a capital letter or other character reached with shift
|
|
|
|
|
_keyReport.modifiers &= ~(0x02); // the left shift modifier
|
|
|
|
|
k &= 0x7F;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test the key report to see if k is present. Clear it if it exists.
|
|
|
|
|
// Check all positions in case the key is present more than once (which it shouldn't be)
|
|
|
|
|
for (i=0; i<6; i++) {
|
|
|
|
|
if (0 != k && _keyReport.keys[i] == k) {
|
|
|
|
|
_keyReport.keys[i] = 0x00;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendReport(&_keyReport);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Keyboard_::releaseAll(void)
|
|
|
|
|
{
|
|
|
|
|
_keyReport.keys[0] = 0;
|
|
|
|
|
_keyReport.keys[1] = 0;
|
|
|
|
|
_keyReport.keys[2] = 0;
|
|
|
|
|
_keyReport.keys[3] = 0;
|
|
|
|
|
_keyReport.keys[4] = 0;
|
|
|
|
|
_keyReport.keys[5] = 0;
|
|
|
|
|
_keyReport.modifiers = 0;
|
|
|
|
|
sendReport(&_keyReport);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t Keyboard_::write(uint8_t c)
|
|
|
|
|
{
|
|
|
|
|
uint8_t p = press(c); // Keydown
|
|
|
|
|
release(c); // Keyup
|
|
|
|
|
return p; // just return the result of press() since release() almost always returns 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Keyboard_ Keyboard;
|
|
|
|
|
|
|
|
|
|
#endif
|