Forgot usb support files

This commit is contained in:
William Toohey 2015-12-28 01:26:19 +10:00
parent 9880107af5
commit 9425b66779
3 changed files with 284 additions and 0 deletions

View File

@ -0,0 +1,145 @@
// https://github.com/SFE-Chris/UNO-HIDKeyboard-Library/blob/master/HIDKeyboard.h
#include <avr/pgmspace.h>
// HID Values of Function Keys
#define F1 0x3a
#define F2 0x3b
#define F3 0x3c
#define F4 0x3d
#define F5 0x3e
#define F6 0x3f
#define F7 0x40
#define F8 0x41
#define F9 0x42
#define F10 0x43
#define F11 0x44
#define F12 0x45
// HID Values of Special Keys
#define ENTER 0x28
#define ESCAPE 0x29
#define BACKSPACE 0x2a
#define TAB 0x2b
#define SPACEBAR 0x2c
#define CAPSLOCK 0x39
#define PRINTSCREEN 0x46
#define SCROLLLOCK 0x47
#define PAUSE 0x48
#define INSERT 0x49
#define HOME 0x4a
#define PAGEUP 0x4b
#define DELETE 0x4c
#define END 0x4d
#define PAGEDOWN 0x4e
#define RIGHTARROW 0x4f
#define LEFTARROW 0x50
#define DOWNARROW 0x51
#define UPARROW 0x52
// HID Values of Keypad Keys
#define NUMLOCK 0x53
#define KEYPADSLASH 0x54
#define KEYPADSTAR 0x55
#define KEYPADMINUS 0x56
#define KEYPADPLUS 0x57
#define KEYPADENTER 0x58
#define KEYPAD1 0x59
#define KEYPAD2 0x5a
#define KEYPAD3 0x5b
#define KEYPAD4 0x5c
#define KEYPAD5 0x5d
#define KEYPAD6 0x5e
#define KEYPAD7 0x5f
#define KEYPAD8 0x60
#define KEYPAD9 0x61
#define KEYPAD0 0x62
#define KEYPADPERIOD 0x63
// HID Values of System Keys
#define KEYBOARDAPPLICATION 0x65
#define KEYBOARDPOWER 0x66
#define VOLUMEMUTE 0x7f
#define VOLUMEUP 0x80
#define VOLUMEDOWN 0x81
// Common-use modifiers
#define CTRL 0x01
#define SHIFT 0x02
#define ALT 0x04
#define GUI 0x08
/****************************************************************************
*
* ASCII->HID LOOKUP TABLE
*
* Taken from the HID Table definition at
* http://www.usb.org/developers/devclass_docs/Hut1_11.pdf
*
* This array maps the ASCII value of a type-able character to its
* corresponding HID value.
*
* Example:
* 'a' = ASCII value 97 = HID value 0x04
* HIDTable['a'] = HIDTable[97] = 0x04
*
* NOTE:
* "Shift Modified" HID values are the same as the non Shift-Modified values
* for any given character, e.g. the HID value for '2' is equal to the
* HID value for '@'. The Shift-Modified value is sent by setting the
* modifier value (buf[0]) to the corresponding modifier value in the
* modifier table.
*
****************************************************************************/
PROGMEM uint8_t HIDTable[] = {
0x00, // 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2A, 0x00, 0x28, // 10
0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 20
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00, // 30
0x00, 0x2c, 0x1e, 0x34, 0x20, 0x21, 0x22, 0x24, 0x34, 0x26, // 40
0x27, 0x25, 0x2e, 0x36, 0x2d, 0x37, 0x38, 0x27, 0x1e, 0x1f, // 50
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x33, 0x33, 0x36, // 60
0x2e, 0x37, 0x38, 0x1f, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, // 70
0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, // 80
0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, // 90
0x2f, 0x31, 0x30, 0x23, 0x2d, 0x35, 0x04, 0x05, 0x06, 0x07, // 100
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, // 110
0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, // 120
0x1c, 0x1d, 0x2f, 0x31, 0x30, 0x35, 127 // 127
};
/****************************************************************************
*
* ASCII->MODIFIER LOOKUP TABLE
*
* Looks up whether or not the HID report should use the SHIFT modifier.
*
* Example:
* The character '2' and the character '@' have different ASCII values but
* the same HID value. This table uses the ASCII value to determine if
* we should hold shift while sending the key. e.g.:
*
* HIDTable['2'] = 0x1f and modifierTable['2'] = 0
* HIDTable['@'] = 0x1f and modifierTable['@'] = SHIFT
*
* There's probaly a better way to do this, but it's functional.
*
****************************************************************************/
PROGMEM uint8_t modifierTable[] = {
0x00, // 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 10
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 20
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 30
0x00, 0x00, SHIFT, SHIFT, SHIFT, SHIFT, SHIFT, SHIFT, SHIFT, SHIFT, // 40
SHIFT, 0x00, SHIFT, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 50
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, SHIFT, 0x00, SHIFT, // 60
0x00, SHIFT, SHIFT, SHIFT, SHIFT, SHIFT, SHIFT, SHIFT, SHIFT, SHIFT, // 70
SHIFT, SHIFT, SHIFT, SHIFT, SHIFT, SHIFT, SHIFT, SHIFT, SHIFT, SHIFT, // 80
SHIFT, SHIFT, SHIFT, SHIFT, SHIFT, SHIFT, SHIFT, SHIFT, SHIFT, SHIFT, // 90
0x00, 0x00, 0x00, SHIFT, SHIFT, 0x00, 0x00, 0x00, 0x00, 0x00, // 100
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 110
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 120
0x00, 0x00, SHIFT, SHIFT, SHIFT, SHIFT, 0x00 // 127
};

119
Firmware/Keyboard/usbio.c Normal file
View File

@ -0,0 +1,119 @@
/*
* FILE: serialio.c
*
* Written by Peter Sutton.
*
* Module to allow standard output routines to be used via
* USB keyboard typing. The init_serial_stdio() method must be called before
* any standard IO methods (e.g. printf). We use a circular buffer to
* store output messages. (This allows us
* to print many characters at once to the buffer and have them
* output by the UART as speed permits.)
*/
#include <usbio.h>
#include <stdio.h>
#include <stdint.h>
#include <asciihid.h>
/* Global variables */
/* Circular buffer to hold outgoing characters. The insert_pos variable
* keeps track of the position (0 to OUTPUT_BUFFER_SIZE-1) that the next
* outgoing character should be written to. bytes_in_buffer keeps
* count of the number of characters currently stored in the buffer
* (ranging from 0 to OUTPUT_BUFFER_SIZE). This number of bytes immediately
* prior to the current insert_pos are the bytes waiting to be output.
* If the insert_pos reaches the end of the buffer it will wrap around
* to the beginning (assuming those bytes have been output).
* NOTE - OUTPUT_BUFFER_SIZE can not be larger than 255 without changing
* the type of the variables below.
*/
#define OUTPUT_BUFFER_SIZE 255
char out_buffer[OUTPUT_BUFFER_SIZE];
uint8_t out_insert_pos;
uint8_t bytes_in_out_buffer;
// Whether we should be depressing or releasing a key
uint8_t liftoff = 0;
static int usb_put_char(char, FILE*);
/* Setup a stream that uses the uart get and put functions. We will
* make standard input and output use this stream below.
*/
static FILE myStream = FDEV_SETUP_STREAM(usb_put_char, NULL,
_FDEV_SETUP_WRITE);
void init_usb_stdio(void) {
/*
* Initialise our buffers
*/
out_insert_pos = 0;
bytes_in_out_buffer = 0;
/* Set up our stream so the put function below is used
* to write characters via the USB port when we use
* stdio functions
*/
stdout = &myStream;
}
static int usb_put_char(char c, FILE* stream) {
/* Add the character to the buffer for transmission if there
* is space to do so. We advance the insert_pos to the next
* character position. If this is beyond the end of the buffer
* we wrap around back to the beginning of the buffer
* NOTE: we disable interrupts before modifying the buffer. This
* prevents the ISR from modifying the buffer at the same time.
* We reenable them if they were enabled when we entered the
* function.
*/
// Drop overrun
if(bytes_in_out_buffer >= OUTPUT_BUFFER_SIZE) {
return 0;
}
out_buffer[out_insert_pos++] = c;
bytes_in_out_buffer++;
if(out_insert_pos == OUTPUT_BUFFER_SIZE) {
/* Wrap around buffer pointer if necessary */
out_insert_pos = 0;
}
return 0;
}
int make_report(USB_KeyboardReport_Data_t *KeyboardReport) {
if(liftoff) {
liftoff = 0;
return 1;
} else {
/* Check if we have data in our buffer */
if(bytes_in_out_buffer > 0) {
/* Yes we do - remove the pending byte and output it
* via the USB. The pending byte (character) is the
* one which is "bytes_in_buffer" characters before the
* insert_pos (taking into account that we may
* need to wrap around to the end of the buffer).
*/
char c;
if(out_insert_pos - bytes_in_out_buffer < 0) {
/* Need to wrap around */
c = out_buffer[out_insert_pos - bytes_in_out_buffer
+ OUTPUT_BUFFER_SIZE];
} else {
c = out_buffer[out_insert_pos - bytes_in_out_buffer];
}
/* Decrement our count of the number of bytes in the
* buffer
*/
bytes_in_out_buffer--;
/* Output the character via USB, converting to scancode */
KeyboardReport->KeyCode[0] = pgm_read_byte(&HIDTable[c]);
KeyboardReport->Modifier = pgm_read_byte(&modifierTable[c]);
liftoff = 1;
return 1;
} else {
return 0;
}
}
}

20
Firmware/Keyboard/usbio.h Normal file
View File

@ -0,0 +1,20 @@
/*
* serialio.h
*
* Author: Peter Sutton
*
* Modified by William Toohey
*/
#ifndef SERIALIO_H_
#define SERIALIO_H_
#include <stdint.h>
#include <LUFA/Drivers/USB/USB.h>
/* Initialise IO using USB.
*/
void init_usb_stdio(void);
int make_report(USB_KeyboardReport_Data_t*);
#endif /* SERIALIO_H_ */