summaryrefslogtreecommitdiff
path: root/boards/dk-tm4c129x/usb_dev_chid/usb_mouse.c
blob: fa46f3f765ede79b6ce458ec75bc30c009125791 (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
//*****************************************************************************
//
// usb_dev_mouse.c - Main routines for the mouse 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 "driverlib/interrupt.h"
#include "grlib/grlib.h"
#include "grlib/widget.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"

//*****************************************************************************
//
// The global state of the mouse.
//
//*****************************************************************************
static volatile struct
{
    int32_t i32X;
    int32_t i32Y;
    uint8_t ui8Buttons;

    enum
    {
        //
        // No pending or reports being sent.
        //
        MOUSE_IDLE,

        //
        // Sending a report with none pending.
        //
        MOUSE_SENDING,

        //
        // Sending a report and have one pending.
        //
        MOUSE_SENDING_PEND,

        //
        // Pending report but none currently sending.
        //
        MOUSE_PENDING,

        //
        // Disconnect occurred.
        //
        MOUSE_DISCONNECT
    }
    eState;
}
sMouseState;

//*****************************************************************************
//
// Initialize the global state of the mouse.
//
//*****************************************************************************
void
USBMouseInit(void)
{
    //
    // Initialize the mouse state..
    //
    sMouseState.i32X = 0;
    sMouseState.i32Y = 0;
    sMouseState.eState = MOUSE_IDLE;
    sMouseState.ui8Buttons = 0;
}

//*****************************************************************************
//
// This function is called by the UI to update the mouse movement and buttons.
//
// \param i32X is the delta in X movement for the mouse.
// \param i32Y is the delta in Y movement for the mouse.
// \param ui8Buttons is the bit mapped value for the buttons.
//
// \return None.
//
//*****************************************************************************
void
USBMouseUpdate(int32_t i32X, int32_t i32Y, uint8_t ui8Buttons)
{
    switch(sMouseState.eState)
    {
        case MOUSE_SENDING:
        {
            //
            // Go to the pending while sending state because there is a
            // transmit already being sent.  This
            //
            sMouseState.eState = MOUSE_SENDING_PEND;
        }
        case MOUSE_SENDING_PEND:
        case MOUSE_PENDING:
        {
            //
            // Accumulate changes in mouse position if there is already a
            // report being sent.
            //
            sMouseState.i32X += i32X;
            if(sMouseState.i32X > 127)
            {
                sMouseState.i32X = 127;
            }
            else if(sMouseState.i32X < -128)
            {
                sMouseState.i32X = -128;
            }

            sMouseState.i32Y += i32Y;
            if(sMouseState.i32X > 127)
            {
                sMouseState.i32X = 127;
            }
            else if(sMouseState.i32X < -128)
            {
                sMouseState.i32X = -128;
            }
            sMouseState.ui8Buttons |= ui8Buttons;

            break;
        }
        case MOUSE_IDLE:
        default:
        {
            //
            // If idle then just send the update.
            //
            sMouseState.i32X = i32X;
            sMouseState.i32Y = i32Y;

            sMouseState.ui8Buttons |= ui8Buttons;

            sMouseState.eState = MOUSE_PENDING;

            break;
        }
    }
}

//*****************************************************************************
//
// Event handler for the USB HID mouse callbacks.  This was passed into the
// USB library as the callback for USB HID mouse events.
//
//*****************************************************************************
uint32_t
USBMouseHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgParam,
                void *pvMsgData)
{
    //
    // The only event that is monitored is the transmit complete to know
    // that it safe to send another report.
    //
    if(ui32Event == USB_EVENT_TX_COMPLETE)
    {
        switch(sMouseState.eState)
        {
            //
            // Done sending return to idle.
            //
            case MOUSE_SENDING:
            {
                //
                // Clear the previous X,Y accumulated values.
                //
                sMouseState.i32X = 0;
                sMouseState.i32Y = 0;

                //
                // If done with sending then always send out a button update
                // if buttons were pressed.
                //
                if(sMouseState.ui8Buttons)
                {
                    sMouseState.ui8Buttons = 0;
                    sMouseState.eState = MOUSE_PENDING;
                }
                else
                {
                    sMouseState.eState = MOUSE_IDLE;
                }
                break;
            }

            //
            // While sending more data was ready to be sent so switch to the
            // pending state again.
            //
            case MOUSE_SENDING_PEND:
            {
                sMouseState.eState = MOUSE_PENDING;
                break;
            }

            //
            // Should not get here, but included for completeness.
            //
            case MOUSE_PENDING:
            case MOUSE_IDLE:
            default:
            {
                break;
            }
        }
    }
    else if(ui32Event == USB_EVENT_DISCONNECTED)
    {
        //
        // Stay in the disconnected state.
        //
        sMouseState.eState = MOUSE_DISCONNECT;
    }
    else if(ui32Event == USB_EVENT_CONNECTED)
    {
        //
        // This received even if the mouse is not active, but reset the state
        // in all cases.
        //
        sMouseState.i32X = 0;
        sMouseState.i32Y = 0;
        sMouseState.ui8Buttons = 0;
        sMouseState.eState = MOUSE_IDLE;
    }
    return(0);
}

//*****************************************************************************
//
// Main routine for the mouse.
//
//*****************************************************************************
void
USBMouseMain(void)
{
    //
    // Disable interrupts while changing the variables below.
    //
    IntMasterDisable();

    switch(sMouseState.eState)
    {
        case MOUSE_PENDING:
        {
            //
            // Send the report since there is one pending.
            //
            USBDHIDMouseStateChange((void *)&g_sMouseDevice,
                                    (uint8_t)sMouseState.i32X,
                                    (uint8_t)sMouseState.i32Y,
                                    sMouseState.ui8Buttons);
            //
            // Clear out the report data so that pending data does not
            // continually accumulate.
            //
            sMouseState.i32X = 0;
            sMouseState.i32Y = 0;

            if(sMouseState.ui8Buttons)
            {
                //
                // Need to always send out that all buttons are released.
                //
                sMouseState.ui8Buttons = 0;
                sMouseState.eState = MOUSE_SENDING_PEND;
            }
            else
            {
                //
                // Switch to sending state and wait for the transmit to be
                // complete.
                //
                sMouseState.eState = MOUSE_SENDING;
            }

            break;
        }
        case MOUSE_DISCONNECT:
        case MOUSE_SENDING:
        case MOUSE_SENDING_PEND:
        case MOUSE_IDLE:
        default:
        {
            break;
        }
    }

    //
    // Enable interrupts now that the main loop is complete.
    //
    IntMasterEnable();
}