Updated Keyboard Led example

This commit is contained in:
Nico 2015-01-02 19:58:02 +01:00
parent 8efa9e116d
commit 8577a94b21
4 changed files with 172 additions and 71 deletions

View file

@ -5,7 +5,6 @@ Arduino HID Project 2.1
This project went through a lot of phases and has now reached a new Arduino USB-Core
with a lot of new functions like extended HID. It also supports HoodLoader1+2.
The idea is to enable enhanced USB functions to almost all 'standard' Arduino boards.
This is done with slightly different methods for different boards.
**Supported Arduinos (IDE 1.5.8 or higher!):**
* Uno (with HoodLoader1 or 2)
@ -18,13 +17,13 @@ This is done with slightly different methods for different boards.
* New HID devices(list below)
* HID reports are easier to modify now
* HID APIs are external usable now
* USB Wakeup
* Smaller flash/ram usage
* More Serial functions
* u2 Series support (HL2)
* HoodLoader1 compatible legacy example
**There are many more additions and fixes in this USB Core. See change log for more details.**
* u2 Series support (with [HoodLoader2](https://github.com/NicoHood/HoodLoader2))
* [HoodLoader1](https://github.com/NicoHood/HoodLoader) compatible legacy example
* See change log for more additions and fixes
**Supported HID devices:**
@ -34,26 +33,14 @@ This is done with slightly different methods for different boards.
* System Key (for PC standby/shutdown)
* Gamepad (32 buttons, 4 16bit axis, 2 8bit axis, 2 D-Pads)
The HID project contains HID APIs to generate HID reports and functions to send them to the USB Host.
The API (syntax/usage) of the HID Project is always the same for each solution, **you can port code from one device to another very easy**.
* On a Leonardo this function is extended and improved to get more HID devices + some improvements.
* [HoodLoader2](https://github.com/NicoHood/HoodLoader2) is a BootLoader for the 16u2 that let's you use it as standalone MCU with the same USB-Core.
* [HoodLoader1](https://github.com/NicoHood/HoodLoader) is a 16u2 firmware that filters special HW Serial signals from the main MCU and sends HID signals to the USB Host.
To make things more clear the HID Software is in a separate repository than the HoodLoader (1 & 2) sources and installing instructions.
**HoodLoader 1&2 is only used for an Uno/Mega to actually enable USB functions.** The 16u2 is normally used for USB-Serial programming of the main MCU but can do way more than that.
To use HoodLoader1&2 you also need the HID Project. For more information and installation instructions see the specific repository.
HoodLoader1 was the first solution to enable HID functions to the Uno/Mega but HoodLoader2 opens way more options because you can reprogram the whole MCU standalone.
See the repository for more infos about this great new opportunity. HoodLoader1 API is still usable in a specific example. HoodLoader1&2 are not needed/compatible with a Leonardo/Micro.
Installation
============
Put all files from *avr/* into *sketchbook/hardware/HID/avr*.
Make sure you use Arduino IDE 1.5.8 or newer.
For Arduino Uno/Mega first install [HoodLoader2](https://github.com/NicoHood/HoodLoader2) on your 16u2 + the needed software files.
[HoodLoader1](https://github.com/NicoHood/HoodLoader) is only supported for legacy but will get a new use soon!
Installation has changed over the time. Put all files from *avr/* into *sketchbook/hardware/HID/avr*.
Make sure you use Arduino IDE 1.5.8 or newer. You don't have to modify the original Arduino-Core any more.
At the moment you have to move the cores/hid folder into your Arduino installation
*arduino-1.6.0/hardware/arduino/avr/cores/hid* because of a bug in the IDE.
@ -66,7 +53,7 @@ How to use
### Micro/Leonardo + HoodLoader2
**You can compile all HID APIs but this doesnt mean that you can use them if no hid descriptor is set correctly.**
**You can compile all HID APIs but this doesn't mean that you can use them if no hid descriptor is set correctly.**
Edit the *sketchbook/hardware/HID/avr/variants/hid_descriptors/hid_descriptors.h* to use the extended HID core.
At the moment you have 3 options: Default, Gamepad or Extended. Extended should work for anything expect Gamepads.
See the bug section below to find out more about working hid reports. Not all of them are playing well together.
@ -88,6 +75,7 @@ Use the void HID_SendReport(uint8_t id, const void* data, int len); function to
See *Project/USB-Serial* for a fully usable USB-Serial bridge and how to use the new Serial functions.
In the CDC.h you can also see the new Control Line functions for advanced users.
Keep in mind that the USB_ENDPOINTs for the u2 Series are set to 16 bytes, so the Serial buffer is also smaller (normally 64b).
**Try the Basic HID examples for each HID device. They are pretty much self explaining.
You can also see the *Projects/HID_Test* for an all in one example.**
@ -128,13 +116,28 @@ How it works
For the Leonardo/Micro + HoodLoader2 its a modified version of the HID descriptors and USB-Core.
This changes were made to improve the functions, add more devices and add u2 series compatibility.
The HID project contains HID APIs to generate HID reports and functions to send them to the USB Host.
The API (syntax/usage) of the HID Project is always the same for each solution, **you can port code from one device to another very easy**.
* On a Leonardo this function is extended and improved to get more HID devices + some improvements.
* [HoodLoader2](https://github.com/NicoHood/HoodLoader2) is a BootLoader for the 16u2 that let's you use it as standalone MCU with the same USB-Core.
* [HoodLoader1](https://github.com/NicoHood/HoodLoader) is a 16u2 firmware that filters special HW Serial signals from the main MCU and sends HID signals to the USB Host.
To make things more clear the HID Software is in a separate repository than the HoodLoader (1 & 2) sources and installing instructions.
**HoodLoader 1&2 is only used for an Uno/Mega to actually enable USB functions.** The 16u2 is normally used for USB-Serial programming of the main MCU but can do way more than that.
To use HoodLoader1&2 you also need the HID Project. For more information and installation instructions see the specific repository.
HoodLoader1 was the first solution to enable HID functions to the Uno/Mega but HoodLoader2 opens way more options because you can reprogram the whole MCU standalone.
See the repository for more infos about this great new opportunity. HoodLoader1 API is still usable in a specific example. HoodLoader1&2 are not needed/compatible with a Leonardo/Micro.
HoodLoader1 only:
For the Uno/Mega you need a special Bootloader. Why? See [Hoodloader repository](https://github.com/NicoHood/Hoodloader).
For the Uno/Mega you need a special Bootloader(actually firmware in this case). Why? See [Hoodloader repository](https://github.com/NicoHood/Hoodloader).
To sum it up: Serial information is grabbed by the "man in the middle, 16u2" and you dont have to worry to get any wrong Serial stuff via USB.
Thatswhy you need a special baud (115200) that both sides can communicate with each other.
Every USB command is send via a special [NicoHood Protocol](https://github.com/NicoHood/NicoHoodProtocol)
that's filtered out by the 16u2. If you use Serial0 for extern devices it cannot filter the signal of course.
You can still use the NHP, just dont use the reserved Address 1.
You can still use the NHP, just don't use the reserved Address 1.
TODO
@ -151,12 +154,9 @@ Add/update Keywords.txt definitions
keycode/raw for keyboard
Check Keyboard keycode function again?
Generalize HID key definitions via HIDTables for example?
Add custom HID reports example
add keyboard led example
update Burning via ISP (advanced)
Test with Android phone
update no usb workaround notice (no longer needed for hoodloader2, but for leonardo)
Test with Android phone (HL1)
```

View file

@ -43,44 +43,51 @@ void loop() {
/*
Definitions:
KEY_LEFT_CTRL
KEY_LEFT_SHIFT
KEY_LEFT_ALT
KEY_LEFT_GUI
KEY_RIGHT_CTRL
KEY_RIGHT_SHIFT
KEY_RIGHT_ALT
KEY_RIGHT_GUI
KEY_UP_ARROW
KEY_DOWN_ARROW
KEY_LEFT_ARROW
KEY_RIGHT_ARROW
KEY_BACKSPACE
KEY_TAB
KEY_RETURN
KEY_ESC
KEY_INSERT
KEY_DELETE
KEY_PAGE_UP
KEY_PAGE_DOWN
KEY_HOME
KEY_END
KEY_CAPS_LOCK
KEY_F1
KEY_F2
KEY_F3
KEY_F4
KEY_F5
KEY_F6
KEY_F7
KEY_F8
KEY_F9
KEY_F10
KEY_F11
KEY_F12
KEY_PRINT
KEY_SCROLL_LOCK
KEY_PAUSE
*/
#define KEY_PRINT 0xCE
#define KEY_NUM_LOCK 0xDB
#define KEY_SCROLL_LOCK 0xCF
#define KEY_PAUSE 0xD0
#define KEY_LEFT_CTRL 0x80
#define KEY_LEFT_SHIFT 0x81
#define KEY_LEFT_ALT 0x82
#define KEY_LEFT_GUI 0x83
#define KEY_LEFT_WINDOWS KEY_LEFT_GUI
#define KEY_RIGHT_CTRL 0x84
#define KEY_RIGHT_SHIFT 0x85
#define KEY_RIGHT_ALT 0x86
#define KEY_RIGHT_GUI 0x87
#define KEY_RIGHT_WINDOWS KEY_RIGHT_GUI
#define KEY_UP_ARROW 0xDA
#define KEY_DOWN_ARROW 0xD9
#define KEY_LEFT_ARROW 0xD8
#define KEY_RIGHT_ARROW 0xD7
#define KEY_BACKSPACE 0xB2
#define KEY_TAB 0xB3
#define KEY_RETURN 0xB0
#define KEY_ESC 0xB1
#define KEY_INSERT 0xD1
#define KEY_DELETE 0xD4
#define KEY_PAGE_UP 0xD3
#define KEY_PAGE_DOWN 0xD6
#define KEY_HOME 0xD2
#define KEY_END 0xD5
#define KEY_CAPS_LOCK 0xC1
#define KEY_F1 0xC2
#define KEY_F2 0xC3
#define KEY_F3 0xC4
#define KEY_F4 0xC5
#define KEY_F5 0xC6
#define KEY_F6 0xC7
#define KEY_F7 0xC8
#define KEY_F8 0xC9
#define KEY_F9 0xCA
#define KEY_F10 0xCB
#define KEY_F11 0xCC
#define KEY_F12 0xCD
#define LED_NUM_LOCK 0x01
#define LED_CAPS_LOCK 0x02
#define LED_SCROLL_LOCK 0x04
*/

View file

@ -0,0 +1,94 @@
/*
Copyright (c) 2014 NicoHood
See the readme for credit to other people.
Keyboard Led example
Press a button to toogle caps lock.
Caps lock state is represented by the onboard led.
See official and HID Project documentation for more infos
Make sure the Keyboard report + LEDs! is set in:
sketchbook/hardware/HID/avr/variants/hid_descriptors/hid_descriptors.h
*/
const int pinLed = LED_BUILTIN;
const int pinButton = 2;
void setup() {
pinMode(pinLed, OUTPUT);
pinMode(pinButton, INPUT_PULLUP);
// Sends a clean report to the host. This is important on any Arduino type.
Keyboard.begin();
}
void loop() {
// update Led equal to the caps lock state
// keep in mind on HoodLoader2 high&low are inverted (like this)
if (Keyboard.getLeds()&LED_CAPS_LOCK)
digitalWrite(pinLed, LOW);
else
digitalWrite(pinLed, HIGH);
if (!digitalRead(pinButton)) {
// trigger caps lock
Keyboard.write(KEY_CAPS_LOCK);
// simple debounce
delay(500);
}
}
/*
Definitions:
#define KEY_PRINT 0xCE
#define KEY_NUM_LOCK 0xDB
#define KEY_SCROLL_LOCK 0xCF
#define KEY_PAUSE 0xD0
#define KEY_LEFT_CTRL 0x80
#define KEY_LEFT_SHIFT 0x81
#define KEY_LEFT_ALT 0x82
#define KEY_LEFT_GUI 0x83
#define KEY_LEFT_WINDOWS KEY_LEFT_GUI
#define KEY_RIGHT_CTRL 0x84
#define KEY_RIGHT_SHIFT 0x85
#define KEY_RIGHT_ALT 0x86
#define KEY_RIGHT_GUI 0x87
#define KEY_RIGHT_WINDOWS KEY_RIGHT_GUI
#define KEY_UP_ARROW 0xDA
#define KEY_DOWN_ARROW 0xD9
#define KEY_LEFT_ARROW 0xD8
#define KEY_RIGHT_ARROW 0xD7
#define KEY_BACKSPACE 0xB2
#define KEY_TAB 0xB3
#define KEY_RETURN 0xB0
#define KEY_ESC 0xB1
#define KEY_INSERT 0xD1
#define KEY_DELETE 0xD4
#define KEY_PAGE_UP 0xD3
#define KEY_PAGE_DOWN 0xD6
#define KEY_HOME 0xD2
#define KEY_END 0xD5
#define KEY_CAPS_LOCK 0xC1
#define KEY_F1 0xC2
#define KEY_F2 0xC3
#define KEY_F3 0xC4
#define KEY_F4 0xC5
#define KEY_F5 0xC6
#define KEY_F6 0xC7
#define KEY_F7 0xC8
#define KEY_F8 0xC9
#define KEY_F9 0xCA
#define KEY_F10 0xCB
#define KEY_F11 0xCC
#define KEY_F12 0xCD
#define LED_NUM_LOCK 0x01
#define LED_CAPS_LOCK 0x02
#define LED_SCROLL_LOCK 0x04
*/

View file

@ -115,7 +115,7 @@ void loop() {
case 'C':
Keyboard.write(KEY_CAPS_LOCK);
Serial.println("Leds");
Serial.println(Keyboard.getLEDs(), BIN);
Serial.println(Keyboard.getLeds(), BIN);
break;
case 'k':