summaryrefslogtreecommitdiff
path: root/boards/dk-tm4c129x/usb_dev_chid/usb_keyboard.c
blob: bb8bf4623e4a4857c4f460af3782349b93bda585 (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//*****************************************************************************
//
// usb_dev_keyboard.c - Main routines for the keyboard portion of the composite
// device.
//
// Copyright (c) 2013-2014 Texas Instruments Incorporated.  All rights reserved.
// Software License Agreement
// 
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
// 
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
// 
// This is part of revision 2.1.0.12573 of the DK-TM4C129X Firmware Package.
//
//*****************************************************************************

#include <stdbool.h>
#include <stdint.h>
#include "driverlib/gpio.h"
#include "grlib/grlib.h"
#include "grlib/widget.h"
#include "grlib/keyboard.h"
#include "usblib/usblib.h"
#include "usblib/usbhid.h"
#include "usblib/device/usbdevice.h"
#include "usblib/device/usbdcomp.h"
#include "usblib/device/usbdhid.h"
#include "usblib/device/usbdhidmouse.h"
#include "usblib/device/usbdhidkeyb.h"
#include "usb_structs.h"
#include "ui.h"

//****************************************************************************
//
// Global USB keyboard state.
//
//****************************************************************************
struct
{
    //
    // Holds a pending special key press for the Caps Lock, Scroll Lock, or
    // Num Lock keys.
    //
    uint8_t ui8Special;
}
g_sKeyboardState;

//****************************************************************************
//
// The look up table entries for usage codes.
//
//****************************************************************************
typedef struct
{
    char cChar;
    char cUsage;
}
sUsageEntry;

//****************************************************************************
//
// The un-shifted HID usage codes used by the graphical keyboard.
//
//****************************************************************************
static const sUsageEntry g_pcUsageCodes[] =
{
    {'q', HID_KEYB_USAGE_Q}, {'w', HID_KEYB_USAGE_W},
    {'e', HID_KEYB_USAGE_E}, {'r', HID_KEYB_USAGE_R},
    {'t', HID_KEYB_USAGE_T}, {'y', HID_KEYB_USAGE_Y},
    {'u', HID_KEYB_USAGE_U}, {'i', HID_KEYB_USAGE_I},
    {'o', HID_KEYB_USAGE_O}, {'p', HID_KEYB_USAGE_P},
    {'a', HID_KEYB_USAGE_A}, {'s', HID_KEYB_USAGE_S},
    {'d', HID_KEYB_USAGE_D}, {'f', HID_KEYB_USAGE_F},
    {'g', HID_KEYB_USAGE_G}, {'h', HID_KEYB_USAGE_H},
    {'j', HID_KEYB_USAGE_J}, {'k', HID_KEYB_USAGE_K},
    {'l', HID_KEYB_USAGE_L}, {'z', HID_KEYB_USAGE_Z},
    {'x', HID_KEYB_USAGE_X}, {'c', HID_KEYB_USAGE_C},
    {'v', HID_KEYB_USAGE_V}, {'b', HID_KEYB_USAGE_B},
    {'n', HID_KEYB_USAGE_N}, {'m', HID_KEYB_USAGE_M},
    {'0', HID_KEYB_USAGE_0}, {'1', HID_KEYB_USAGE_1},
    {'2', HID_KEYB_USAGE_2}, {'3', HID_KEYB_USAGE_3},
    {'4', HID_KEYB_USAGE_4}, {'5', HID_KEYB_USAGE_5},
    {'6', HID_KEYB_USAGE_6}, {'7', HID_KEYB_USAGE_7},
    {'8', HID_KEYB_USAGE_8}, {'9', HID_KEYB_USAGE_9},
    {'-', HID_KEYB_USAGE_MINUS}, {'=', HID_KEYB_USAGE_EQUAL},
    {'\'', HID_KEYB_USAGE_FQUOTE}, {'[', HID_KEYB_USAGE_LBRACKET},
    {']', HID_KEYB_USAGE_RBRACKET}, {';', HID_KEYB_USAGE_SEMICOLON},
    {' ', HID_KEYB_USAGE_SPACE}, {'/', HID_KEYB_USAGE_FSLASH},
    {'\\', HID_KEYB_USAGE_BSLASH}, {'.', HID_KEYB_USAGE_PERIOD},
    {',', HID_KEYB_USAGE_COMMA},
    {UI_CAPS_LOCK, HID_KEYB_USAGE_CAPSLOCK},
    {UI_SCROLL_LOCK, HID_KEYB_USAGE_SCROLLOCK},
    {UI_NUM_LOCK, HID_KEYB_USAGE_NUMLOCK},
    {UNICODE_BACKSPACE, HID_KEYB_USAGE_BACKSPACE},
    {UNICODE_RETURN, HID_KEYB_USAGE_ENTER},
};

static const uint32_t ui32NumUsageCodes =
                                    sizeof(g_pcUsageCodes)/sizeof(sUsageEntry);

//****************************************************************************
//
// The shifted HID usage codes that are used by the graphical keyboard.
//
//****************************************************************************
static const sUsageEntry g_pcUsageCodesShift[] =
{
    {')', HID_KEYB_USAGE_0}, {'!', HID_KEYB_USAGE_1},
    {'@', HID_KEYB_USAGE_2}, {'#', HID_KEYB_USAGE_3},
    {'$', HID_KEYB_USAGE_4}, {'%', HID_KEYB_USAGE_5},
    {'^', HID_KEYB_USAGE_6}, {'&', HID_KEYB_USAGE_7},
    {'*', HID_KEYB_USAGE_8}, {'(', HID_KEYB_USAGE_9},
    {'?', HID_KEYB_USAGE_FSLASH}, {'+', HID_KEYB_USAGE_EQUAL},
    {':', HID_KEYB_USAGE_SEMICOLON}, {'_', HID_KEYB_USAGE_MINUS},
    {'~', HID_KEYB_USAGE_BQUOTE}, {'|', HID_KEYB_USAGE_BSLASH},
    {'\"', HID_KEYB_USAGE_FQUOTE},
};

static const uint32_t ui32NumUsageCodesShift =
                                    sizeof(g_pcUsageCodes)/sizeof(sUsageEntry);

//****************************************************************************
//
// Handle basic initialization of the USB keyboard.
//
//****************************************************************************
void
USBKeyboardInit(void)
{
    //
    // Clear out the special key variable.
    //
    g_sKeyboardState.ui8Special = 0;
}

//****************************************************************************
//
// Returns the Usage code for a ASCII character.
//
// \param cKey is the ASCII character to look up.
// \param bShifted determines if the look up is for the shifted value or not.
//
// This function is used to look up the USB HID usage code for a given
// ASCII character.  The \e bShifted value indicates if the key should be
// resported as shifted.
//
// \return Returns the usage code for the ASCII character or 0 if none found.
//
//****************************************************************************
static uint8_t
GetUsageCode(char cKey, bool bShifted)
{
    int32_t i32Idx, i32Entries;
    const sUsageEntry *pUsageTable;

    if(bShifted)
    {
        i32Entries = ui32NumUsageCodesShift;
        pUsageTable = g_pcUsageCodesShift;
    }
    else
    {
        i32Entries = ui32NumUsageCodes;
        pUsageTable = g_pcUsageCodes;
    }

    for(i32Idx = 0; i32Idx < i32Entries; i32Idx++)
    {
        if(pUsageTable[i32Idx].cChar == cKey)
        {
            return(pUsageTable[i32Idx].cUsage);
        }
    }
    return(0);
}

//****************************************************************************
//
// Called by the UI interface to update the USB keyboard.
//
// \param ui8Modifiers is the set of key modifiers.
// \param ui8Key is ASCII character to look up.
// \param bPressed indicates if this is a press or release event.
//
// This function is used to update a key that has been pressed based on the
// ASCII character that is passed in the \e ui8Key parameter.   The \e bPressed
// parameter is \b true if the key was pressed and \b false if the key was
// released.
//
// \return None.
//
//****************************************************************************
void
USBKeyboardUpdate(uint8_t ui8Modifiers, uint8_t ui8Key, bool bPressed)
{
    uint8_t ui8Usage;

    //
    // Move these to a-z because USB HID does not recognize unshifted values,
    // it uses the SHIFT modifier to change the case.
    //
    if((ui8Key >= 'A') && (ui8Key <= 'Z'))
    {
        ui8Key |= 0x20;

        if(bPressed)
        {
            ui8Modifiers |= HID_KEYB_LEFT_SHIFT;
        }
    }

    //
    // Get the usage code for this character.
    //
    ui8Usage = GetUsageCode(ui8Key, false);

    //
    // Check if this was a "special" key because USB HID handles these
    // separately.
    //
    if((ui8Usage == HID_KEYB_USAGE_CAPSLOCK) ||
       (ui8Usage == HID_KEYB_USAGE_SCROLLOCK) ||
       (ui8Usage == HID_KEYB_USAGE_NUMLOCK))
    {
        //
        // If there was already a special key pressed, then force it to be
        // released.
        //
        if(g_sKeyboardState.ui8Special)
        {
            USBDHIDKeyboardKeyStateChange(&g_sKeyboardDevice, ui8Modifiers,
                                          g_sKeyboardState.ui8Special, false);
        }

        //
        // Save the new special key.
        //
        g_sKeyboardState.ui8Special = ui8Usage;
    }

    //
    // If there was not an unshifted value for this character then look for
    // a shifted version of the character.
    //
    if(ui8Usage == 0)
    {
        //
        // Get the shifted value and set the shift modifier.
        //
        ui8Usage = GetUsageCode(ui8Key, true);

        if(bPressed)
        {
            ui8Modifiers |= HID_KEYB_LEFT_SHIFT;
        }
    }

    //
    // If a valid usage code was found then pass the key along to the
    // USB library.
    //
    if(ui8Usage)
    {
        USBDHIDKeyboardKeyStateChange(&g_sKeyboardDevice, ui8Modifiers,
                                      ui8Usage, bPressed);
    }
}

//****************************************************************************
//
// Handle the callbacks from the USB library's HID keyboard layer.
//
//****************************************************************************
uint32_t
USBKeyboardHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgParam,
                   void *pvMsgData)
{
    //
    // Handle LED set requests.  These are the various lock key requests.
    //
    if(ui32Event == USBD_HID_KEYB_EVENT_SET_LEDS)
    {
        //
        // Set the state of the lock keys in the UI.
        //
        UICapsLock(ui32MsgParam & HID_KEYB_CAPS_LOCK);
        UIScrollLock(ui32MsgParam & HID_KEYB_SCROLL_LOCK);
        UINumLock(ui32MsgParam & HID_KEYB_NUM_LOCK);
    }
    else if(ui32Event == USB_EVENT_TX_COMPLETE)
    {
        //
        // Any time a report is sent and there is a pending special key
        // pressed send a key release.
        //
        if(g_sKeyboardState.ui8Special)
        {
            USBDHIDKeyboardKeyStateChange(&g_sKeyboardDevice, 0,
                                          g_sKeyboardState.ui8Special, false);
            g_sKeyboardState.ui8Special = 0;
        }

    }
    return(0);
}