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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
//*****************************************************************************
//
// keyboard_ui.c - The DK-TM4C129X USB keyboard application UI.
//
// 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 <string.h>
#include "grlib/grlib.h"
#include "usblib/usblib.h"
#include "usblib/usbhid.h"
#include "usblib/host/usbhost.h"
#include "drivers/frame.h"
#include "drivers/kentec320x240x16_ssd2119.h"
#include "keyboard_ui.h"
//*****************************************************************************
//
// These defines are used to define the screen constraints to the application.
//
//*****************************************************************************
#define DISPLAY_BANNER_HEIGHT 18
#define DISPLAY_TEXT_BORDER 8
#define DISPLAY_TEXT_BORDER_H 8
#define BUTTON_HEIGHT 18
//*****************************************************************************
//
// This is the number of characters that will fit on a line in the text area.
//
//*****************************************************************************
uint32_t g_ui32CharsPerLine;
//*****************************************************************************
//
// This is the number of lines that will fit in the text area.
//
//*****************************************************************************
uint32_t g_ui32LinesPerScreen;
//*****************************************************************************
//
// This is the current column for printing in the text area.
//
//*****************************************************************************
uint32_t g_ui32Column;
//*****************************************************************************
//
// Define the maximum number of lines and columns in the command windows.
//
//*****************************************************************************
#define MAX_LINES 23
#define MAX_COLUMNS 60
//*****************************************************************************
//
// The variables that are used to allow the screen to scroll.
//
//*****************************************************************************
static char g_ppcLines[MAX_LINES][MAX_COLUMNS];
static uint32_t g_ui32CurrentLine;
uint32_t g_ui32EntryLine;
//*****************************************************************************
//
// Graphics context used to show text on the CSTN display.
//
//*****************************************************************************
tContext g_sContext;
//*****************************************************************************
//
// Draws the prompt for each new line.
//
//*****************************************************************************
static void
DrawPrompt(void)
{
int32_t i32Idx;
g_ppcLines[g_ui32CurrentLine][0] = '>';
for(i32Idx = 1; i32Idx < MAX_COLUMNS - 1; i32Idx++)
{
g_ppcLines[g_ui32CurrentLine][i32Idx] = ' ';
}
g_ppcLines[g_ui32CurrentLine][i32Idx] = 0;
GrStringDraw(&g_sContext, g_ppcLines[g_ui32CurrentLine], MAX_COLUMNS,
DISPLAY_TEXT_BORDER_H +
(GrFontMaxWidthGet(g_psFontFixed6x8) * g_ui32Column),
DISPLAY_BANNER_HEIGHT + DISPLAY_TEXT_BORDER +
(g_ui32EntryLine * GrFontHeightGet(g_psFontFixed6x8)),
true);
g_ui32Column = 2;
}
//*****************************************************************************
//
// Initialize the application interface.
//
//*****************************************************************************
void
UIInit(uint32_t ui32SysClock)
{
//
// Initialize the display driver.
//
Kentec320x240x16_SSD2119Init(ui32SysClock);
//
// Initialize the graphics context.
//
GrContextInit(&g_sContext, &g_sKentec320x240x16_SSD2119);
//
// Draw the application frame.
//
FrameDraw(&g_sContext, "usb-host-keyboard");
//
// Set the font for the application.
//
GrContextFontSet(&g_sContext, g_psFontFixed6x8);
//
// Calculate the number of characters that will fit on a line.
// Make sure to leave a small border for the text box.
//
g_ui32CharsPerLine = (GrContextDpyWidthGet(&g_sContext) - 16) /
GrFontMaxWidthGet(g_psFontFixed6x8);
//
// Calculate the number of lines per usable text screen. This requires
// taking off space for the top and bottom banners and adding a small bit
// for a border.
//
g_ui32LinesPerScreen = (GrContextDpyHeightGet(&g_sContext) -
(2*(DISPLAY_BANNER_HEIGHT + 1)) -
BUTTON_HEIGHT) / GrFontHeightGet(g_psFontFixed6x8);
//
// Set up the text scrolling variables.
//
g_ui32CurrentLine = 0;
g_ui32EntryLine = 0;
//
// Draw the initial prompt on the screen.
//
DrawPrompt();
//
// Initial update of the screen.
//
UIUpdateStatus();
}
//*****************************************************************************
//
// Handles scrolling the text on the screen.
//
//*****************************************************************************
static void
ScrollText(void)
{
int32_t i32Idx;
uint32_t ui32Line, ui32Start;
ui32Line = 0;
//
// Skip the oldest entry in the circular list.
//
if(g_ui32CurrentLine == (MAX_LINES - 1))
{
//
// If at the end of the list wrap to entry 1.
//
ui32Start = 1;
}
else
{
//
// The oldest is 1 in front of the most recent.
//
ui32Start = g_ui32CurrentLine + 2;
}
//
// Print lines from the current position down first.
//
for(i32Idx = ui32Start; i32Idx < MAX_LINES; i32Idx++)
{
GrStringDraw(&g_sContext, g_ppcLines[i32Idx],
strlen(g_ppcLines[i32Idx]), DISPLAY_TEXT_BORDER_H,
DISPLAY_BANNER_HEIGHT + DISPLAY_TEXT_BORDER +
(ui32Line * GrFontHeightGet(g_psFontFixed6x8)), 1);
ui32Line++;
}
//
// If not the special case of the last line where everything has already
// printed, print the remaining lines.
//
if(g_ui32CurrentLine != (MAX_LINES - 1))
{
for(i32Idx = 0; i32Idx <= g_ui32CurrentLine; i32Idx++)
{
GrStringDraw(&g_sContext, g_ppcLines[i32Idx],
strlen(g_ppcLines[i32Idx]),
DISPLAY_TEXT_BORDER_H,
DISPLAY_BANNER_HEIGHT + DISPLAY_TEXT_BORDER +
(ui32Line * GrFontHeightGet(g_psFontFixed6x8)), 1);
ui32Line++;
}
}
//
// Reset the column to zero.
//
g_ui32Column = 0;
}
//*****************************************************************************
//
// This function prints the character out the screen and into the command
// buffer.
//
// ucChar is the character to print out.
//
// This function handles all of the detail of printing a character to the
// screen and into the command line buffer.
//
// No return value.
//
//*****************************************************************************
void
UIPrintChar(const char cChar)
{
bool bNewLine;
int32_t i32Idx;
GrContextForegroundSet(&g_sContext, ClrWhite);
bNewLine = true;
//
// Allow new lines to cause the column to go back to zero.
//
if(cChar != '\n')
{
//
// Handle when receiving a backspace character.
//
if(cChar != ASCII_BACKSPACE)
{
//
// This is not a backspace so print the character to the screen.
//
GrStringDraw(&g_sContext, &cChar, 1,
DISPLAY_TEXT_BORDER_H +
(GrFontMaxWidthGet(g_psFontFixed6x8) * g_ui32Column),
DISPLAY_BANNER_HEIGHT + DISPLAY_TEXT_BORDER +
(g_ui32EntryLine * GrFontHeightGet(g_psFontFixed6x8)),
1);
g_ppcLines[g_ui32CurrentLine][g_ui32Column] = cChar;
if(g_ui32Column < g_ui32CharsPerLine)
{
//
// No line wrap yet so move one column over.
//
g_ui32Column++;
bNewLine = false;
}
}
else
{
//
// We got a backspace. If we are at the top left of the screen,
// return since we don't need to do anything.
//
if(g_ui32Column || g_ui32EntryLine)
{
//
// Adjust the cursor position to erase the last character.
//
if(g_ui32Column > 2)
{
g_ui32Column--;
}
//
// Print a space at this position then return without fixing up
// the cursor again.
//
GrStringDraw(&g_sContext, " ", 1,
DISPLAY_TEXT_BORDER_H +
(GrFontMaxWidthGet(g_psFontFixed6x8) * g_ui32Column),
DISPLAY_BANNER_HEIGHT + DISPLAY_TEXT_BORDER +
(g_ui32EntryLine * GrFontHeightGet(g_psFontFixed6x8)),
true);
g_ppcLines[g_ui32CurrentLine][g_ui32Column] = ' ';
}
bNewLine = false;
}
}
//
// .
//
if(bNewLine)
{
g_ui32Column = 0;
if(g_ui32EntryLine < (MAX_LINES - 1))
{
g_ui32EntryLine++;
}
else
{
ScrollText();
}
g_ui32CurrentLine++;
//
// The line has gone past the end so go back to the first line.
//
if(g_ui32CurrentLine >= MAX_LINES)
{
g_ui32CurrentLine = 0;
}
//
// Add a prompt to the new line.
//
if(cChar == '\n')
{
DrawPrompt();
}
else
{
//
// Clear out the current line.
//
for(i32Idx = 0; i32Idx < MAX_COLUMNS - 1; i32Idx++)
{
g_ppcLines[g_ui32CurrentLine][i32Idx] = ' ';
}
g_ppcLines[g_ui32CurrentLine][i32Idx] = 0;
GrStringDraw(&g_sContext, g_ppcLines[g_ui32CurrentLine],
strlen(g_ppcLines[g_ui32CurrentLine]),
DISPLAY_TEXT_BORDER_H,
DISPLAY_BANNER_HEIGHT + DISPLAY_TEXT_BORDER +
(g_ui32EntryLine * GrFontHeightGet(g_psFontFixed6x8)),
1);
}
}
}
//*****************************************************************************
//
// Update one of the status boxes at the bottom of the screen.
//
//*****************************************************************************
static void
UpdateStatusBox(const tRectangle *psRect, const char *pcString, bool bActive)
{
uint32_t ui32TextColor;
//
// Change the status box to green for active devices.
//
if(bActive)
{
GrContextForegroundSet(&g_sContext, ClrOrange);
ui32TextColor = ClrBlack;
}
else
{
GrContextForegroundSet(&g_sContext, ClrBlack);
ui32TextColor = ClrWhite;
}
//
// Draw the background box.
//
GrRectFill(&g_sContext, psRect);
//
// Put a white box around the banner.
//
GrContextForegroundSet(&g_sContext, ClrWhite);
//
// Draw the box border.
//
GrRectDraw(&g_sContext, psRect);
//
// Put a white box around the banner.
//
GrContextForegroundSet(&g_sContext, ui32TextColor);
//
// Unknown device is currently connected.
//
GrStringDrawCentered(&g_sContext, pcString, -1,
psRect->i16XMin + ((psRect->i16XMax - psRect->i16XMin) / 2),
psRect->i16YMin + (BUTTON_HEIGHT / 2), false);
}
//*****************************************************************************
//
// This function updates the status area of the screen. It uses the current
// state of the application to print the status bar.
//
//*****************************************************************************
void
UIUpdateStatus(void)
{
uint8_t ui8DevClass, ui8DevProtocol;
static const tRectangle psRect[4] =
{
{
DISPLAY_TEXT_BORDER_H, 240 - 10 - BUTTON_HEIGHT,
DISPLAY_TEXT_BORDER_H + 124, 240 - 10
},
{
DISPLAY_TEXT_BORDER_H + 124, 240 - 10 - BUTTON_HEIGHT,
DISPLAY_TEXT_BORDER_H + 184, 240 - 10
},
{
DISPLAY_TEXT_BORDER_H + 184, 240 - 10 - BUTTON_HEIGHT,
DISPLAY_TEXT_BORDER_H + 244, 240 - 10
},
{
DISPLAY_TEXT_BORDER_H + 244, 240 - 10 - BUTTON_HEIGHT,
DISPLAY_TEXT_BORDER_H + 303, 240 - 10
},
};
//
// Put the application name in the middle of the banner.
//
GrContextFontSet(&g_sContext, g_psFontFixed6x8);
//
// Default the protocol to none so that non-HID devices do not update
// the keyboard modifiers.
//
ui8DevProtocol = USB_HID_PROTOCOL_NONE;
if(g_sStatus.bConnected)
{
ui8DevClass = USBHCDDevClass(g_sStatus.ui32Instance, 0);
ui8DevProtocol = USBHCDDevProtocol(g_sStatus.ui32Instance, 0);
if(ui8DevClass == USB_CLASS_HID)
{
if(ui8DevProtocol == USB_HID_PROTOCOL_MOUSE)
{
//
// Mouse is currently connected.
//
UpdateStatusBox(&psRect[0], "Mouse", true);
}
else if(ui8DevProtocol == USB_HID_PROTOCOL_KEYB)
{
//
// Keyboard is currently connected.
//
UpdateStatusBox(&psRect[0], "Keyboard", true);
}
else
{
//
// Unknown device is currently connected.
//
UpdateStatusBox(&psRect[0], "Unknown", true);
}
}
else if(ui8DevClass == USB_CLASS_MASS_STORAGE)
{
//
// MSC device is currently connected.
//
UpdateStatusBox(&psRect[0], "Mass Storage", true);
}
else if(ui8DevClass == USB_CLASS_HUB)
{
//
// MSC device is currently connected.
//
UpdateStatusBox(&psRect[0], "Hub", true);
}
else
{
//
// Unknown device is currently connected.
//
UpdateStatusBox(&psRect[0], "Unknown", true);
}
}
else
{
//
// Unknown device is currently connected.
//
UpdateStatusBox(&psRect[0], "No Device", false);
}
if(ui8DevProtocol == USB_HID_PROTOCOL_KEYB)
{
if(g_sStatus.ui32Modifiers & HID_KEYB_CAPS_LOCK)
{
UpdateStatusBox(&psRect[1], "CAPS", true);
}
else
{
UpdateStatusBox(&psRect[1], "caps", false);
}
if(g_sStatus.ui32Modifiers & HID_KEYB_SCROLL_LOCK)
{
UpdateStatusBox(&psRect[2], "SCROLL", true);
}
else
{
UpdateStatusBox(&psRect[2], "scroll", false);
}
if(g_sStatus.ui32Modifiers & HID_KEYB_NUM_LOCK)
{
UpdateStatusBox(&psRect[3], "NUM", true);
}
else
{
UpdateStatusBox(&psRect[3], "num", false);
}
}
else
{
UpdateStatusBox(&psRect[1], "caps", false);
UpdateStatusBox(&psRect[2], "scroll", false);
UpdateStatusBox(&psRect[3], "num", false);
}
}
|