-
Notifications
You must be signed in to change notification settings - Fork 21
/
USBKeyboard.h
118 lines (88 loc) · 4.31 KB
/
USBKeyboard.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**********************************************************************************************
*
* This library for the Arduino IDE adds HID USB keyboard functionality to your projects, it
* allows Arduinos and other AVRs to act as an HID USB Device. This way no drivers have to be
* installed, the keyboard will work with every PC and OS.
* It is based the V-USB code by Objective Developement (https://www.obdev.at/products/vusb/)
* and under the same license (GNU GPLv3, see LICENSE.txt).
*
*********************************************************************************************/
#ifndef __USBKeyboard_h__
#define __USBKeyboard_h__
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <string.h>
#include <usbdrv.h>
#include <Arduino.h>
#include <keycodes.h>
/* constants */
#define MODIFIER_CONTROL (1<<0)
#define MODIFIER_SHIFT (1<<1)
#define MODIFIER_ALT (1<<2)
#define MODIFIER_GUI (1<<3)
#ifndef LAYOUT
#define LAYOUT LAYOUT_US
#endif
/* makros */
#define conc(a, b) (a ## b)
#define concat(a, b) conc(a, b)
/* global variables */
static uint8_t idle_rate = 500 / 4; /* see HID1_11.pdf sect 7.2.4 */
static uint8_t protocol_version = 0; /* see HID1_11.pdf sect 7.2.6 */
volatile static uint8_t LED_states = 0; /* see HID1_11.pdf appendix B section 1 */
volatile static uint8_t toggle_counter = 0; /* keep track of how many times caps lock have toggled */
static uint8_t keyboard_layout = 0; /* keyboard layout, US layout by standard */
class USBKeyboard : public Print {
public: /*#################### PUBLIC FUNCTIONS ####################*/
USBKeyboard() = default;
/*******************************************************
* call it when initializing the library
******************************************************/
void begin(uint8_t layout = 0);
/*******************************************************
* Call this function at least every 20ms,
* otherwise an USB timeout will occur
******************************************************/
void update();
/*******************************************************
* Type a single char to the USB host
******************************************************/
virtual size_t write(uint8_t ascii);
/*******************************************************
* Send a single key to the USB host
******************************************************/
void sendKey(uint8_t keycode) {sendKey(keycode, 0);}
void sendKey(uint8_t keycode, uint8_t modifiers);
/*******************************************************
* Send up to 6 keys to the USB host
******************************************************/
void sendKeys(uint8_t keycode1, uint8_t keycode2, uint8_t keycode3, uint8_t keycode4, uint8_t keycode5, uint8_t keycode6) {sendKeys(keycode1, keycode2, keycode3, keycode4, keycode5, keycode6, 0);}
void sendKeys(uint8_t keycode1, uint8_t keycode2, uint8_t keycode3, uint8_t keycode4, uint8_t keycode5, uint8_t keycode6, uint8_t modifiers);
/*******************************************************
* Translate ASCII char to keycode
******************************************************/
uint8_t asciiToKeycode(char ascii);
/*******************************************************
* Translate ASCII char to Shift state, taking into
* consideration the status of Caps Lock
******************************************************/
uint8_t asciiToShiftState(char ascii);
/*******************************************************
* Get the state of Caps Lock
******************************************************/
bool isCapsLockActivated();
/*******************************************************
* Get how often Caps Lock was toggled
******************************************************/
uint8_t getCapsLockToggleCount();
/*******************************************************
* Reset the Caps Lock toggle counter
******************************************************/
void resetCapsLockToggleCount();
private: /*################### PRIVATE FUNCTIONS ###################*/
/*******************************************************
* Send the keyboard report
******************************************************/
void sendReport(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2, uint8_t keycode3, uint8_t keycode4, uint8_t keycode5, uint8_t keycode6);
};
#endif /* __USBKeyboard_h__ */