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
|
//*****************************************************************************
//
// mouse_ui.h - The DK-TM4C129X USB mouse 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 "mouse_ui.h"
#include "utils/ustdlib.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
#define BUTTON_WIDTH 30
#define STATUS_MIN_Y (240 - 10 - BUTTON_HEIGHT)
#define STATUS_MIDDLE_X 140
#define BUTTON_MIN_X (MOUSE_MAX_X - (BUTTON_WIDTH * 3) - 1)
//*****************************************************************************
//
// The cursor rectangle.
//
//*****************************************************************************
tRectangle g_sCursor;
//*****************************************************************************
//
// Graphics context used to show text on the CSTN display.
//
//*****************************************************************************
tContext g_sContext;
//*****************************************************************************
//
// Used to remember if the UI has updated the connection status yet.
//
//*****************************************************************************
bool g_bTypeUpdated;
//*****************************************************************************
//
// 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-mouse");
//
// Set the font for the application.
//
GrContextFontSet(&g_sContext, g_psFontFixed6x8);
//
// Default to device type not yet updated.
//
g_bTypeUpdated = false;
//
// Initial update of the screen.
//
UIUpdateStatus();
}
//*****************************************************************************
//
// 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 cursor position based on the current state
// information in the g_sStatus structure. This function takes the inputs and
// forces them to be constrained to the display area of the screen. If the
// left mouse button is pressed then the mouse will draw on the screen and if
// it is not it will move around normally. A side effect of not being able to
// read the current state of the screen is that the cursor will erase anything
// it moves over even while the left mouse button is not pressed.
//
// \return None.
//
//*****************************************************************************
static void
UpdateCursor(void)
{
//
// If the left button is not pressed then erase the previous cursor
// position.
//
if((g_sStatus.ui32Buttons & 1) == 0)
{
//
// Erase the previous cursor.
//
GrContextForegroundSet(&g_sContext, DISPLAY_MOUSE_BG);
GrRectFill(&g_sContext, &g_sCursor);
}
//
// Update the X position.
//
if(g_sStatus.ui32XPos >= (MOUSE_MAX_X - DISPLAY_MOUSE_SIZE))
{
g_sCursor.i16XMin = MOUSE_MAX_X - DISPLAY_MOUSE_SIZE - 1;
}
else if(g_sStatus.ui32XPos < MOUSE_MIN_X)
{
g_sCursor.i16XMin = MOUSE_MIN_X;
}
else
{
g_sCursor.i16XMin = g_sStatus.ui32XPos;
}
g_sCursor.i16XMax = g_sCursor.i16XMin + DISPLAY_MOUSE_SIZE;
//
// Update the Y position.
//
if(g_sStatus.ui32YPos >= (MOUSE_MAX_Y - DISPLAY_MOUSE_SIZE))
{
g_sCursor.i16YMin = MOUSE_MAX_Y - DISPLAY_MOUSE_SIZE - 1;
}
else if(g_sStatus.ui32YPos < MOUSE_MIN_Y)
{
g_sCursor.i16YMin = MOUSE_MIN_Y;
}
else
{
g_sCursor.i16YMin = g_sStatus.ui32YPos;
}
g_sCursor.i16YMax = g_sCursor.i16YMin + DISPLAY_MOUSE_SIZE;
//
// Draw the cursor at the new position.
//
GrContextForegroundSet(&g_sContext, DISPLAY_MOUSE_FG);
GrRectFill(&g_sContext, &g_sCursor);
}
//*****************************************************************************
//
// This function will update the mouse button indicators in the status
// bar area of the screen.
//
//*****************************************************************************
static void
UpdateButtons(void)
{
tRectangle sRect, sRectInner;
int iButton;
//
// Initialize the button indicator position.
//
sRect.i16XMin = BUTTON_MIN_X;
sRect.i16YMin = STATUS_MIN_Y;
sRect.i16XMax = sRect.i16XMin + BUTTON_WIDTH;
sRect.i16YMax = sRect.i16YMin + BUTTON_HEIGHT ;
sRectInner.i16XMin = sRect.i16XMin + 1;
sRectInner.i16YMin = sRect.i16YMin + 1;
sRectInner.i16XMax = sRect.i16XMax - 1;
sRectInner.i16YMax = sRect.i16YMax - 1;
//
// Check all three buttons.
//
for(iButton = 0; iButton < 3; iButton++)
{
//
// Draw the button indicator red if pressed and black if not pressed.
//
if(g_sStatus.ui32Buttons & (1 << iButton))
{
GrContextForegroundSet(&g_sContext, ClrRed);
}
else
{
GrContextForegroundSet(&g_sContext, ClrBlack);
}
//
// Draw the back of the button indicator.
//
GrRectFill(&g_sContext, &sRectInner);
//
// Draw the border on the button indicator.
//
GrContextForegroundSet(&g_sContext, ClrWhite);
GrRectDraw(&g_sContext, &sRect);
//
// Move to the next button indicator position.
//
sRect.i16XMin += BUTTON_WIDTH;
sRect.i16XMax += BUTTON_WIDTH;
sRectInner.i16XMin += BUTTON_WIDTH;
sRectInner.i16XMax += BUTTON_WIDTH;
}
}
//*****************************************************************************
//
// 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 char pcNoPos[] = "---,---";
static const tRectangle psRect[2] =
{
{
DISPLAY_TEXT_BORDER_H,
STATUS_MIN_Y,
DISPLAY_TEXT_BORDER_H + STATUS_MIDDLE_X,
STATUS_MIN_Y + BUTTON_HEIGHT
},
{
DISPLAY_TEXT_BORDER_H + STATUS_MIDDLE_X,
STATUS_MIN_Y,
BUTTON_MIN_X,
STATUS_MIN_Y + BUTTON_HEIGHT
},
};
char pcPos[8];
//
// Put the application name in the middle of the banner.
//
GrContextFontSet(&g_sContext, g_psFontFixed6x8);
//
// Default the protocol to none.
//
ui8DevProtocol = USB_HID_PROTOCOL_NONE;
if(g_sStatus.bConnected)
{
ui8DevClass = USBHCDDevClass(g_sStatus.ui32Instance, 0);
ui8DevProtocol = USBHCDDevProtocol(g_sStatus.ui32Instance, 0);
//
// If the class has not yet been recognized then print out the new
// class of device that has been connected.
//
if(g_bTypeUpdated == false)
{
//
// Now the class has been updated so do not update it again.
//
g_bTypeUpdated = true;
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);
//
// No device is present so allow the class to update when a new device
// is connected.
//
g_bTypeUpdated = false;
}
if(ui8DevProtocol == USB_HID_PROTOCOL_MOUSE)
{
//
// Update the current cursor position.
//
UpdateCursor();
usprintf(pcPos, "%3d,%3d", g_sCursor.i16XMin, g_sCursor.i16YMin);
UpdateStatusBox(&psRect[1], pcPos, false);
}
else
{
UpdateStatusBox(&psRect[1], pcNoPos, false);
}
UpdateButtons();
}
|