summaryrefslogtreecommitdiff
path: root/boards/dk-tm4c129x/usb_dev_chid/ui.c
blob: 688b6a1ed07cd21320552fc7e96cecc3faa1287c (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
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
//*****************************************************************************
//
// ui.c - User interface code for the USB composite HID keyboard/mouse example.
// This file seperates out the USB library accesses and the general hardware
// access and solely handles the user interface.
//
// 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 "grlib/grlib.h"
#include "grlib/widget.h"
#include "grlib/canvas.h"
#include "grlib/keyboard.h"
#include "grlib/pushbutton.h"
#include "drivers/frame.h"
#include "drivers/kentec320x240x16_ssd2119.h"
#include "ui.h"

//*****************************************************************************
//
// Global graphics context.
//
//*****************************************************************************
static tContext g_sContext;

static void ToggleMode(tWidget *psWidget);
static void Status1(tWidget *psWidget);
static void Status2(tWidget *psWidget);
static void Status3(tWidget *psWidget);

//*****************************************************************************
//
// Defines for the basic screen area used by the application.
//
//*****************************************************************************
#define STATUS_HEIGHT           40
#define BG_MIN_X                7
#define BG_MAX_X                (320 - 8)
#define BG_MIN_Y                24
#define BG_MAX_Y                (240 - 8)
#define BUTTON_HEIGHT           (STATUS_HEIGHT - 8)
#define BG_COLOR_SETTINGS       ClrGray
#define BG_COLOR_MAIN           ClrBlack

//*****************************************************************************
//
// The global UI state.
//
//*****************************************************************************
static struct
{
    uint32_t ui32Indicators;
}
g_sUIState;

//*****************************************************************************
//
// The defined values used with ui32Indicators.
//
//*****************************************************************************
#define UI_STATUS_MS_RIGHT      0x00000001
#define UI_STATUS_MS_MIDDLE     0x00000002
#define UI_STATUS_MS_LEFT       0x00000004

#define UI_STATUS_KEY_CAPS      0x00000001
#define UI_STATUS_KEY_SCROLL    0x00000002
#define UI_STATUS_KEY_NUM       0x00000004

#define UI_STATUS_UPDATE        0x80000000
#define UI_STATUS_KEYBOARD      0x00000008
#define UI_STATUS_MOUSE         0x00000000

//*****************************************************************************
//
// The graphical widgets that are used by this example.
//
//*****************************************************************************
extern tCanvasWidget g_sBackground;
extern tCanvasWidget g_sStatusPanel;

//
// The keyboard widget used by the application.
//
Keyboard(g_sKeyboard, &g_sBackground, 0, 0,
         &g_sKentec320x240x16_SSD2119, BG_MIN_X + 2, BG_MIN_Y + 4, 300, 160,
         KEYBOARD_STYLE_FILL | KEYBOARD_STYLE_AUTO_REPEAT |
         KEYBOARD_STYLE_PRESS_NOTIFY | KEYBOARD_STYLE_RELEASE_NOTIFY |
         KEYBOARD_STYLE_BG,
         ClrBlack, ClrGray, ClrDarkGray, ClrGray, ClrBlack, g_psFontCmtt14,
         100, 100, NUM_KEYBOARD_US_ENGLISH, g_psKeyboardUSEnglish, UIKeyEvent);

//
// The full background for the application.
//
Canvas(g_sBackground, WIDGET_ROOT, 0, &g_sStatusPanel,
       &g_sKentec320x240x16_SSD2119, BG_MIN_X, BG_MIN_Y,
       BG_MAX_X - BG_MIN_X, BG_MAX_Y - BG_MIN_Y - STATUS_HEIGHT,
       CANVAS_STYLE_FILL, ClrBlack, ClrWhite, ClrWhite, 0, 0, 0 ,0 );


//
// Right button in mouse mode and Num Lock in keyboard mode.
//
RectangularButton(g_sStatus3, &g_sStatusPanel, 0, 0,
       &g_sKentec320x240x16_SSD2119, BG_MIN_X + 250,
       BG_MAX_Y - STATUS_HEIGHT + 4, 50, BUTTON_HEIGHT,
       PB_STYLE_FILL | PB_STYLE_TEXT |
       PB_STYLE_RELEASE_NOTIFY, ClrLightGrey, ClrDarkGray, 0,
       ClrBlack, g_psFontCmss16, "Right", 0, 0, 0 ,0 , Status3);

//
// Middle button in mouse mode and Scroll Lock in keyboard mode.
//
RectangularButton(g_sStatus2, &g_sStatusPanel, &g_sStatus3, 0,
       &g_sKentec320x240x16_SSD2119, BG_MIN_X + 196,
       BG_MAX_Y - STATUS_HEIGHT + 4, 50, BUTTON_HEIGHT,
       PB_STYLE_FILL | PB_STYLE_TEXT |
       PB_STYLE_RELEASE_NOTIFY, ClrLightGrey, ClrDarkGray, 0,
       ClrBlack, g_psFontCmss16, "Middle", 0, 0, 0 ,0 , Status2);

//
// Left button in mouse mode and Caps Lock in keyboard mode.
//
RectangularButton(g_sStatus1, &g_sStatusPanel, &g_sStatus2, 0,
       &g_sKentec320x240x16_SSD2119, BG_MIN_X + 142,
       BG_MAX_Y - STATUS_HEIGHT + 4, 50, BUTTON_HEIGHT,
       PB_STYLE_FILL | PB_STYLE_TEXT |
       PB_STYLE_RELEASE_NOTIFY, ClrLightGrey, ClrDarkGray, 0,
       ClrBlack, g_psFontCmss16, "Left", 0, 0, 0 ,0 , Status1);

//
// The mode toggle button.
//
RectangularButton(g_sToggle, &g_sStatusPanel, &g_sStatus1, 0,
       &g_sKentec320x240x16_SSD2119, BG_MIN_X + 4,
       BG_MAX_Y - STATUS_HEIGHT + 4, 80, BUTTON_HEIGHT,
       PB_STYLE_FILL | PB_STYLE_TEXT | PB_STYLE_RELEASE_NOTIFY, ClrLightGrey,
       ClrDarkGray, 0, ClrBlack, g_psFontCmss16, "Mouse", 0, 0, 0 ,0 ,
       ToggleMode);

//
// Background of the status area behind the buttons.
//
Canvas(g_sStatusPanel, &g_sBackground, 0, &g_sToggle,
       &g_sKentec320x240x16_SSD2119, BG_MIN_X, BG_MAX_Y - STATUS_HEIGHT,
       BG_MAX_X - BG_MIN_X, STATUS_HEIGHT,
       CANVAS_STYLE_FILL | CANVAS_STYLE_TEXT_RIGHT |
       CANVAS_STYLE_TEXT_TOP, ClrGray, ClrWhite, ClrBlack, 0,
       0, 0, 0);

tUIState g_eState;

//****************************************************************************
//
// This is the callback from the graphical keyboard that is defined above as
// g_sKeyboard global.
//
//****************************************************************************
void
UIKeyEvent(tWidget *psWidget, uint32_t ui32Key, uint32_t ui32Event)
{
    if(ui32Event == KEYBOARD_EVENT_PRESS)
    {
        USBKeyboardUpdate(0, ui32Key, true);

    }
    else if(ui32Event == KEYBOARD_EVENT_RELEASE)
    {
        USBKeyboardUpdate(0, ui32Key, false);
    }
}

//****************************************************************************
//
// Handles updating the Caps Lock status when in keyboard mode.
//
//****************************************************************************
void
UIUpdateCapsLock(void)
{
    if(g_sUIState.ui32Indicators & UI_STATUS_KEY_CAPS)
    {
        PushButtonTextSet(&g_sStatus1, "CAPS");
        PushButtonTextColorSet(&g_sStatus1, ClrRed);
    }
    else
    {
        PushButtonTextSet(&g_sStatus1, "caps");
        PushButtonTextColorSet(&g_sStatus1, ClrBlack);
    }

    WidgetPaint((tWidget *)&g_sStatus1);
}

void
UICapsLock(bool bEnable)
{
    if(bEnable)
    {
        //
        // Enable caps lock.
        //
        g_sUIState.ui32Indicators |= UI_STATUS_KEY_CAPS;
    }
    else
    {
        //
        // Disable caps lock.
        //
        g_sUIState.ui32Indicators &= ~UI_STATUS_KEY_CAPS;
    }

    if(g_sUIState.ui32Indicators & UI_STATUS_KEYBOARD)
    {
        UIUpdateCapsLock();
    }
}

//****************************************************************************
//
// Handles updating the Scroll Lock status when in keyboard mode.
//
//****************************************************************************
void
UIUpdateScrollLock(void)
{
    if(g_sUIState.ui32Indicators & UI_STATUS_KEY_SCROLL)
    {
        PushButtonTextSet(&g_sStatus2, "SCROLL");
        PushButtonTextColorSet(&g_sStatus2, ClrRed);
    }
    else
    {
        PushButtonTextSet(&g_sStatus2, "scroll");
        PushButtonTextColorSet(&g_sStatus2, ClrBlack);
    }

    WidgetPaint((tWidget *)&g_sStatus2);
}

void
UIScrollLock(bool bEnable)
{
    if(bEnable)
    {
        //
        // Enable caps lock.
        //
        g_sUIState.ui32Indicators |= UI_STATUS_KEY_SCROLL;
    }
    else
    {
        //
        // Disable caps lock.
        //
        g_sUIState.ui32Indicators &= ~UI_STATUS_KEY_SCROLL;
    }

    if(g_sUIState.ui32Indicators & UI_STATUS_KEYBOARD)
    {
        UIUpdateScrollLock();
    }
}

//****************************************************************************
//
// Handles updating the Num Lock status when in keyboard mode.
//
//****************************************************************************
void
UIUpdateNumLock(void)
{
    if(g_sUIState.ui32Indicators & UI_STATUS_KEY_NUM)
    {
        PushButtonTextSet(&g_sStatus3, "NUM");
        PushButtonTextColorSet(&g_sStatus3, ClrRed);
    }
    else
    {
        PushButtonTextSet(&g_sStatus3, "num");
        PushButtonTextColorSet(&g_sStatus3, ClrBlack);
    }

    WidgetPaint((tWidget *)&g_sStatus3);
}

void
UINumLock(bool bEnable)
{
    if(bEnable)
    {
        //
        // Enable caps lock.
        //
        g_sUIState.ui32Indicators |= UI_STATUS_KEY_NUM;
    }
    else
    {
        //
        // Disable caps lock.
        //
        g_sUIState.ui32Indicators &= ~UI_STATUS_KEY_NUM;
    }

    if(g_sUIState.ui32Indicators & UI_STATUS_KEYBOARD)
    {
        UIUpdateNumLock();
    }
}

//****************************************************************************
//
// Handles updates to the current status.
//
//****************************************************************************
void
UIUpdateStatus(uint32_t ui32Indicators)
{
    if(g_eState == UI_NOT_CONNECTED)
    {
        PushButtonTextSet(&g_sToggle, "---");

        PushButtonTextColorSet(&g_sStatus1, ClrBlack);
        PushButtonTextSet(&g_sStatus1, "---");

        PushButtonTextColorSet(&g_sStatus2, ClrBlack);
        PushButtonTextSet(&g_sStatus2, "---");

        PushButtonTextColorSet(&g_sStatus3, ClrBlack);
        PushButtonTextSet(&g_sStatus3, "---");

        if(ui32Indicators & UI_STATUS_KEYBOARD)
        {
            WidgetRemove((tWidget *)&g_sKeyboard);
        }

        WidgetPaint((tWidget *)&g_sBackground);

        return;
    }

    //
    // See if there is a change to update.
    //
    if(ui32Indicators == g_sUIState.ui32Indicators)
    {
        return;
    }

    //
    // Was there a global change in the keyboard/mouse state.
    //
    if(((ui32Indicators ^ g_sUIState.ui32Indicators) & UI_STATUS_KEYBOARD) ||
       (ui32Indicators & UI_STATUS_UPDATE))
    {
        //
        // Update to keyboard mode or mouse mode for the UI.
        //
        if(ui32Indicators & UI_STATUS_KEYBOARD)
        {
            PushButtonTextSet(&g_sToggle, "Keyboard");
            WidgetPaint((tWidget *)&g_sToggle);
            UIUpdateCapsLock();
            UIUpdateScrollLock();
            UIUpdateNumLock();

            WidgetAdd(WIDGET_ROOT, (tWidget *)&g_sKeyboard);

            WidgetPaint((tWidget *)&g_sKeyboard);
        }
        else
        {
            //
            // Switch back to a mouse UI.
            //
            PushButtonTextSet(&g_sToggle, "Mouse");

            PushButtonTextColorSet(&g_sStatus1, ClrBlack);
            PushButtonTextSet(&g_sStatus1, "Left");

            PushButtonTextColorSet(&g_sStatus2, ClrBlack);
            PushButtonTextSet(&g_sStatus2, "Middle");

            PushButtonTextColorSet(&g_sStatus3, ClrBlack);
            PushButtonTextSet(&g_sStatus3, "Right");

            WidgetRemove((tWidget *)&g_sKeyboard);

            WidgetPaint((tWidget *)&g_sBackground);
        }
    }

    //
    // Update the new state of the indicators.
    //
    g_sUIState.ui32Indicators = ui32Indicators & ~UI_STATUS_UPDATE;
}


//****************************************************************************
//
// Called by the mode toggle button in the g_sToggle variable.
//
//****************************************************************************
static void
ToggleMode(tWidget *psWidget)
{
    if(g_eState == UI_CONNECTED)
    {
        UIUpdateStatus(g_sUIState.ui32Indicators ^ UI_STATUS_KEYBOARD);
    }
}

void
UIMode(tUIState eState)
{
    if(g_eState != UI_CONNECTED)
    {
        if(eState == UI_CONNECTED)
        {
            UIUpdateStatus(g_sUIState.ui32Indicators | UI_STATUS_UPDATE);
        }
    }
    else if(g_eState == UI_CONNECTED)
    {
        if(eState == UI_NOT_CONNECTED)
        {
            UIUpdateStatus(g_sUIState.ui32Indicators & ~UI_STATUS_UPDATE);
        }
    }

    g_eState = eState;
    UIUpdateStatus(g_sUIState.ui32Indicators);
}

//****************************************************************************
//
// Called by the button code controlled by the g_sStatus1 variable.
//
//****************************************************************************
static void
Status1(tWidget *psWidget)
{
    if(g_eState == UI_CONNECTED)
    {
        //
        // Toggle the state of the first status button.
        //
        if(g_sUIState.ui32Indicators & UI_STATUS_KEYBOARD)
        {
            //
            // Toggle the state of the caps lock.
            //
            g_sUIState.ui32Indicators ^= UI_STATUS_KEY_CAPS;

            UIUpdateCapsLock();

            USBKeyboardUpdate(0, UI_CAPS_LOCK, true);
        }
        else
        {
            USBMouseUpdate(0, 0, 0x01);
        }
    }
}

//****************************************************************************
//
// Called by the button code controlled by the g_sStatus2 variable.
//
//****************************************************************************
static void
Status2(tWidget *psWidget)
{
    if(g_eState == UI_CONNECTED)
    {
        //
        // Toggle the state of the first status button.
        //
        if(g_sUIState.ui32Indicators & UI_STATUS_KEYBOARD)
        {
            //
            // Toggle the state of the caps lock.
            //
            g_sUIState.ui32Indicators ^= UI_STATUS_KEY_SCROLL;

            UIUpdateScrollLock();

            USBKeyboardUpdate(0, UI_SCROLL_LOCK, true);
        }
        else
        {
            USBMouseUpdate(0, 0, 0x04);
        }
    }
}

//****************************************************************************
//
// Called by the button code controlled by the g_sStatus3 variable.
//
//****************************************************************************
static void
Status3(tWidget *psWidget)
{
    if(g_eState == UI_CONNECTED)
    {
        //
        // Toggle the state of the first status button.
        //
        if(g_sUIState.ui32Indicators & UI_STATUS_KEYBOARD)
        {
            //
            // Toggle the state of the caps lock.
            //
            g_sUIState.ui32Indicators ^= UI_STATUS_KEY_NUM;

            UIUpdateNumLock();
            USBKeyboardUpdate(0, UI_NUM_LOCK, true);
        }
        else
        {
            USBMouseUpdate(0, 0, 0x02);
        }
    }
}

static struct
{
    int32_t i32XLast;
    int32_t i32YLast;
    int8_t i8Buttons;
    bool bPressed;
}
sMouseState;

volatile uint32_t ui32PressCount;

//*****************************************************************************
//
// The callback function that is called by the touch screen driver to indicate
// activity on the touch screen.
//
//*****************************************************************************
int32_t
UITouchCallback(uint32_t ui32Message, int32_t i32X, int32_t i32Y)
{
    int32_t i32XDiff, i32YDiff;

    if(((g_sUIState.ui32Indicators & UI_STATUS_KEYBOARD) == 0) &&
       ((i32Y > BG_MIN_Y) && (i32Y < (BG_MAX_Y - STATUS_HEIGHT))))
    {

        switch(ui32Message)
        {
            //
            // .
            //
            case WIDGET_MSG_PTR_DOWN:
            {
                sMouseState.i32XLast = i32X;
                sMouseState.i32YLast = i32Y;

                sMouseState.bPressed = true;

                ui32PressCount = g_ui32SysTickCount;
                break;
            }


            // The touchscreen is no longer being pressed.
            //
            case WIDGET_MSG_PTR_UP:
            {
                sMouseState.bPressed = false;

                if(ui32PressCount > g_ui32SysTickCount)
                {
                    ui32PressCount = ui32PressCount - g_ui32SysTickCount;
                }
                else
                {
                    ui32PressCount = g_ui32SysTickCount - ui32PressCount;
                }

                if(ui32PressCount < 20)
                {
                    //
                    // Ensure that all buttons are not pressed.
                    //
                    sMouseState.i8Buttons = 0x01;
                }
                else
                {
                    //
                    // Ensure that all buttons are not pressed.
                    //
                    sMouseState.i8Buttons = 0x00;
                }

                //
                // Send the report back to the host.
                //
                USBMouseUpdate(0, 0, sMouseState.i8Buttons);

                break;
            }

            //
            // The touch position has moved.
            //
            case WIDGET_MSG_PTR_MOVE:
            {
                //
                // Send the difference not the absolute value.
                //
                i32XDiff = i32X - sMouseState.i32XLast;
                i32YDiff = i32Y - sMouseState.i32YLast;

                //
                // Send the report back to the host.
                //
                USBMouseUpdate(i32XDiff, i32YDiff, 0);

                //
                // Save these values to calculate the next move.
                //
                sMouseState.i32XLast = i32X;
                sMouseState.i32YLast = i32Y;

                break;
            }
            default:
            {
                break;
            }
        }
    }
    else
    {
        WidgetPointerMessage(ui32Message, i32X, i32Y);
    }

    return(0);
}


//****************************************************************************
//
// Application calls this once to initialize the UI.
//
//****************************************************************************
void
UIInit(void)
{
    //
    // Initialize the graphics context.
    //
    GrContextInit(&g_sContext, &g_sKentec320x240x16_SSD2119);

    //
    // Draw the application frame.
    //
    FrameDraw(&g_sContext, "usb-dev-chid");

    WidgetAdd(WIDGET_ROOT, (tWidget *)&g_sBackground);
    WidgetPaint((tWidget *)&g_sBackground);

    //
    // Initially not connected.
    //
    g_eState = UI_NOT_CONNECTED;

    //
    // Not initialized.
    //
    UIUpdateStatus(UI_STATUS_UPDATE);

}

//****************************************************************************
//
// Application should periodically call this function.
//
//****************************************************************************
void
UIMain(void)
{
    WidgetMessageQueueProcess();

    if(g_sUIState.ui32Indicators & UI_STATUS_KEYBOARD)
    {

    }
    else
    {
        USBMouseMain();
    }
}