Example fixes

This commit is contained in:
Nico 2015-01-02 16:59:33 +01:00
parent 8997abe61b
commit eed723401b
13 changed files with 520 additions and 12 deletions

View file

@ -3,11 +3,11 @@
See the readme for credit to other people.
Advanced Gamepad example
Make sure the Gamepad report is set in:
sketchbook/hardware/HID/avr/variants/hid_descriptors/hid_descriptors.h
*/
// include HID library
#include <HID.h>
// see HID_Reports.h for all data structures
HID_GamepadReport_Data_t Gamepadreport;
@ -22,9 +22,8 @@ void setup() {
pinMode(pinButton, INPUT_PULLUP);
// Sends a clean report to the host. This is important on any Arduino type.
// Make sure all desired USB functions are activated in USBAPI.h!
memset(&Gamepadreport, 0, sizeof(Gamepadreport));
HID_SendReport(HID_REPORTID_Gamepad1Report, &Gamepadreport, sizeof(Gamepadreport));
HID_SendReport(HID_REPORTID_GAMEPAD, &Gamepadreport, sizeof(Gamepadreport));
}
void loop() {
@ -43,7 +42,7 @@ void loop() {
// functions before only set the values
// this writes the report to the host
HID_SendReport(HID_REPORTID_Gamepad1Report, &Gamepadreport, sizeof(Gamepadreport));
HID_SendReport(HID_REPORTID_GAMEPAD, &Gamepadreport, sizeof(Gamepadreport));
// simple debounce
delay(300);

View file

@ -17,7 +17,6 @@ void setup() {
pinMode(pinButton, INPUT_PULLUP);
// Sends a clean report to the host. This is important on any Arduino type.
// Make sure all desired USB functions are activated in USBAPI.h!
pressRawKeyboard(0, 0);
}
@ -45,7 +44,7 @@ void pressRawKeyboard(uint8_t modifiers, uint8_t key) {
uint8_t keys[8] = {
modifiers, 0, key, 0, 0, 0, 0, 0
}; //modifiers, reserved, key[0]
HID_SendReport(HID_REPORTID_KeyboardReport, keys, sizeof(keys));
HID_SendReport(HID_REPORTID_KEYBOARD, keys, sizeof(keys));
}
/*

View file

@ -2,7 +2,7 @@
Copyright (c) 2014 NicoHood
See the readme for credit to other people.
Advanced RawHID example
Advanced RawHID example (currently not available)
Shows how to send bytes via raw HID
Press a button to send some example values.
@ -23,7 +23,6 @@ void setup() {
pinMode(pinButton, INPUT_PULLUP);
// no begin function needed for RawHID
// Make sure all desired USB functions are activated in USBAPI.h!
}
void loop() {
@ -32,7 +31,7 @@ void loop() {
// direct without library. Always send RAWHID_RX_SIZE bytes!
uint8_t buff[RAWHID_RX_SIZE]; // unitialized, has random values
HID_SendReport(HID_REPORTID_RawKeyboardReport, buff, sizeof(buff));
HID_SendReport(HID_REPORTID_RAWHID, buff, sizeof(buff));
// with library
memset(&buff, 42, sizeof(buff));

View file

@ -0,0 +1,48 @@
/*
Copyright (c) 2014 NicoHood
See the readme for credit to other people.
Consumer example (former Media example)
Press a button to play/pause music player
See HID Project documentation for more Consumer keys.
Make sure the Consumer report is set in:
sketchbook/hardware/HID/avr/variants/hid_descriptors/hid_descriptors.h
// basic Media key definitions, see HID Project and official USB docs for more
#define MEDIA_FAST_FORWARD 0xB3
#define MEDIA_REWIND 0xB4
#define MEDIA_NEXT 0xB5
#define MEDIA_PREVIOUS 0xB6
#define MEDIA_STOP 0xB7
#define MEDIA_PLAY_PAUSE 0xCD
#define MEDIA_VOLUME_MUTE 0xE2
#define MEDIA_VOLUME_UP 0xE9
#define MEDIA_VOLUME_DOWN 0xEA
*/
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.
Consumer.begin();
}
void loop() {
if (!digitalRead(pinButton)) {
digitalWrite(pinLed, HIGH);
// See list above for more definitions or the official usb documentation
Consumer.write(MEDIA_PLAY_PAUSE);
// simple debounce
delay(300);
digitalWrite(pinLed, LOW);
}
}

View file

@ -0,0 +1,86 @@
/*
Copyright (c) 2014 NicoHood
See the readme for credit to other people.
Gamepad example
Press a button and demonstrate Gamepad actions
Make sure the Gamepad report is set in:
sketchbook/hardware/HID/avr/variants/hid_descriptors/hid_descriptors.h
Function prototypes:
void begin(void);
void end(void);
void write(void);
void press(uint8_t b);
void release(uint8_t b);
void releaseAll(void);
void buttons(uint32_t b);
void xAxis(int16_t a);
void yAxis(int16_t a);
void rxAxis(int16_t a);
void ryAxis(int16_t a);
void zAxis(int8_t a);
void rzAxis(int8_t a);
void dPad1(int8_t d);
void dPad2(int8_t d);
Definitions:
GAMEPAD_DPAD_CENTERED 0
GAMEPAD_DPAD_UP 1
GAMEPAD_DPAD_UP_RIGHT 2
GAMEPAD_DPAD_RIGHT 3
GAMEPAD_DPAD_DOWN_RIGHT 4
GAMEPAD_DPAD_DOWN 5
GAMEPAD_DPAD_DOWN_LEFT 6
GAMEPAD_DPAD_LEFT 7
GAMEPAD_DPAD_UP_LEFT 8
*/
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.
Gamepad.begin();
}
void loop() {
if (!digitalRead(pinButton)) {
digitalWrite(pinLed, HIGH);
// press button 1-32 and reset (34 becaue its written later)
static uint8_t count = 1;
Gamepad.press(count++);
if (count == 34) {
Gamepad.releaseAll();
count = 1;
}
// move x/y Axis to a new position (16bit)
Gamepad.xAxis(random(0xFFFF));
Gamepad.yAxis(random(0xFFFF));
// go through all dPad positions
// values: 0-8 (0==centred)
static uint8_t dpad1 = GAMEPAD_DPAD_CENTERED;
Gamepad.dPad1(dpad1++);
if(dpad1>GAMEPAD_DPAD_UP_LEFT) dpad1 = GAMEPAD_DPAD_CENTERED;
static int8_t dpad2 = GAMEPAD_DPAD_CENTERED;
Gamepad.dPad2(dpad2--);
if(dpad2<GAMEPAD_DPAD_CENTERED) dpad2 = GAMEPAD_DPAD_UP_LEFT;
// functions above only set the values
// this writes the report to the host
Gamepad.write();
// simple debounce
delay(300);
digitalWrite(pinLed, LOW);
}
}

View file

@ -0,0 +1,86 @@
/*
Copyright (c) 2014 NicoHood
See the readme for credit to other people.
Keyboard example
Press a button to write some text to your pc.
See official and HID Project documentation for more infos
Make sure the Keyboard report is set in (by default it is):
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);
// Starts Serial debug output
Serial.begin(115200);
// Sends a clean report to the host. This is important on any Arduino type.
Keyboard.begin();
}
void loop() {
if (!digitalRead(pinButton)) {
digitalWrite(pinLed, HIGH);
// Same use as the official library, pretty much self explaining
Keyboard.println("This message was sent with my Arduino.");
Serial.println("Serial port is still working and not glitching out");
// simple debounce
delay(300);
digitalWrite(pinLed, LOW);
}
}
/*
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
*/

View file

@ -0,0 +1,72 @@
/*
Copyright (c) 2014 NicoHood
See the readme for credit to other people.
Mouse example
Press a button to click, move, moveTo the mouse.
See official documentation for more infos
Make sure the Absolute Mouse report is set in:
sketchbook/hardware/HID/avr/variants/hid_descriptors/hid_descriptors.h
Mouse Key definitions:
MOUSE_LEFT
MOUSE_RIGHT
MOUSE_MIDDLE
MOUSE_PREV
MOUSE_NEXT
*/
const int pinLed = LED_BUILTIN;
const int pinButtonC = 2;
const int pinButtonR = 3;
const int pinButtonT = 4;
void setup() {
// prepare led + buttons
pinMode(pinLed, OUTPUT);
pinMode(pinButtonC, INPUT_PULLUP);
pinMode(pinButtonR, INPUT_PULLUP);
pinMode(pinButtonT, INPUT_PULLUP);
// Sends a clean report to the host. This is important on any Arduino type.
Mouse.begin();
}
void loop() {
if (!digitalRead(pinButtonC)) {
digitalWrite(pinLed, HIGH);
// Same use as the official library, pretty much self explaining
Mouse.click();
//Mouse.click(MOUSE_RIGHT);
// simple debounce
delay(300);
digitalWrite(pinLed, LOW);
}
if (!digitalRead(pinButtonR)) {
digitalWrite(pinLed, HIGH);
// Same use as the official library, pretty much self explaining
Mouse.move(100, 0);
// simple debounce
delay(300);
digitalWrite(pinLed, LOW);
}
if (!digitalRead(pinButtonT)) {
digitalWrite(pinLed, HIGH);
// Moves Mouse to an absolute position(0- 32767).
// Make sure the Absolute Mouse report is set in hid_descriptors.h
Mouse.moveTo(16384, 16384);
// simple debounce
delay(300);
digitalWrite(pinLed, LOW);
}
}

View file

@ -0,0 +1,51 @@
/*
Copyright (c) 2014 NicoHood
See the readme for credit to other people.
System example
Press a button to put pc into sleep/shut it down or wake it up again.
Make sure the System report is set in:
sketchbook/hardware/HID/avr/variants/hid_descriptors/hid_descriptors.h
*/
const int pinLed = LED_BUILTIN;
const int pinButtonS = 2;
const int pinButtonW = 3;
void setup() {
// prepare led + buttons
pinMode(pinLed, OUTPUT);
pinMode(pinButtonS, INPUT_PULLUP);
pinMode(pinButtonW, INPUT_PULLUP);
// Sends a clean report to the host. This is important on any Arduino type.
System.begin();
}
void loop() {
if (!digitalRead(pinButtonS)) {
digitalWrite(pinLed, HIGH);
// puts pc into sleep mode/shuts it down
System.write(SYSTEM_SLEEP);
//System.write(SYSTEM_POWER_DOWN);
// simple debounce
delay(300);
digitalWrite(pinLed, LOW);
}
if (!digitalRead(pinButtonW)) {
digitalWrite(pinLed, HIGH);
// tries to wake up the PC
// this might fail on some PCs where USB wakeup isnt supported
USBDevice.wakeupHost();
// simple debounce
delay(300);
digitalWrite(pinLed, LOW);
}
}

View file

@ -41,7 +41,6 @@ void setup() {
pinMode(pinButton12, INPUT_PULLUP);
// Sends a clean report to the host. This is important on any Arduino type.
// Make sure all desired USB functions are activated in USBAPI.h!
Gamepad.begin();
}

View file

@ -0,0 +1,169 @@
void setup() {
// put your setup code here, to run once:
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
// 7052 279
Keyboard.begin();
Gamepad.begin();
hid_keyboard_leds = 0; //test to access it from here
}
uint32_t eventBaud = 0;
void loop() {
if (!digitalRead(7)) {
digitalWrite(13, 1);
Mouse.moveTo(1000, 1000);
delay(300);
digitalWrite(13, 0);
}
if (!digitalRead(8)) {
digitalWrite(13, 1);
Mouse.move(100, 0);
delay(300);
digitalWrite(13, 0);
}
if (!digitalRead(9)) {
digitalWrite(13, 1);
Mouse.move(-100, 0);
delay(300);
digitalWrite(13, 0);
}
if (!digitalRead(10)) {
uint8_t k[8] = {0};
k[1] = 1;
k[2] = 4;
k[3] = 5;
HID_SendReport(HID_REPORTID_KEYBOARD, &k, sizeof(k));
Keyboard.releaseAll();
delay(300);
}
if (!digitalRead(2)) {
digitalWrite(13, 1);
System.write(SYSTEM_SLEEP);
delay(300);
digitalWrite(13, 0);
}
if (!digitalRead(3)) {
digitalWrite(13, 1);
USBDevice.wakeupHost();
delay(300);
digitalWrite(13, 0);
}
if (Serial.available()) {
// let the Serial receive all bytes and discard the first bytes
// this is to ensure you only input a single char and no string
char c;
delay(300);
int length = Serial.available();
while (Serial.available())
c = Serial.read();
if (length > 1) {
Serial.println("Please only input a single character or deactivate linefeed!");
return;
}
if (c != -1) {
Serial.println(c);
switch (c) {
case 'a':
Keyboard.write('b');
break;
case 'p':
Consumer.write(MEDIA_PLAY_PAUSE);
break;
case 'o':
{
// uint8_t k[8] = {0};
// k[1] = 1 << 4;
// //k[2] = 4;
// HID_SendReport(HID_REPORTID_KEYBOARD, &k, sizeof(k));
// Keyboard.releaseAll();
break;
}
case 's':
System.write(SYSTEM_SLEEP);
break;
case 'r':
Mouse.move(100, 0);
break;
case 'l':
Mouse.move(-100, 0);
break;
case 't':
Mouse.moveTo(1000, 1000);
break;
case 'c':
case 'C':
Keyboard.write(KEY_CAPS_LOCK);
Serial.println("Leds");
Serial.println(Keyboard.getLEDs(), BIN);
break;
case 'k':
Keyboard.print("Testing USB functions xyz");
break;
case '\r':
case '\n':
Serial.println("Please only input a single character!");
break;
case 'g': {
// press button 1-32 and reset (34 becaue its written later)
static uint8_t count = 1;
Gamepad.press(count++);
if (count == 34) {
Gamepad.releaseAll();
count = 1;
}
Gamepad.write();
}
break;
case 'd':
Serial.println("Serial");
Serial.println(Serial.dtr());
Serial.println(Serial.rts());
Serial.println(Serial.baud());
Serial.println(Serial.stopbits());
Serial.println(Serial.paritytype());
Serial.println(Serial.numbits());
break;
default:
Serial.println("unknown");
}
}
}
if (eventBaud) {
Serial.println("Event");
Serial.println(eventBaud);
eventBaud = 0;
}
}
void CDC_LineEncodingEvent(void)
{
eventBaud = Serial.baud();
}