1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
21
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
67
71
72
73
78
128
129
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
159
166
167
173
174
175
176
177
178
179
180
181
182
183
187
191
192
196
200
201
202
203
204
205
206
207
208
209
210
211
212
217
218
219
220
221
222
223
224
244
245
246
251
252
253
254
255
256
257
258
278
/* ... */
#include "main.h"
Includes
extern USBH_HandleTypeDef hUSBHost;
Private typedef
#define HID_MOUSE_BUTTON1 0x01
#define HID_MOUSE_BUTTON2 0x02
#define HID_MOUSE_BUTTON3 0x04
#define MOUSE_TOP_DIR 0x80
#define MOUSE_BOTTOM_DIR 0x00
#define MOUSE_LEFT_DIR 0x80
#define MOUSE_RIGHT_DIR 0x00
#define MOUSE_WINDOW_X 80
#define MOUSE_WINDOW_Y 70
#define MOUSE_WINDOW_X_MAX 181
#define MOUSE_WINDOW_Y_MIN 101
#define MOUSE_WINDOW_HEIGHT 70
#define MOUSE_WINDOW_WIDTH 145
#define HID_MOUSE_BUTTON_HEIGHT 10
#define HID_MOUSE_BUTTON_WIDTH 24
#define HID_MOUSE_BUTTON1_XCHORD 80
#define HID_MOUSE_BUTTON2_XCHORD 140
#define HID_MOUSE_BUTTON3_XCHORD 200
#define HID_MOUSE_BUTTON_YCHORD 150
#define MOUSE_LEFT_MOVE 1
#define MOUSE_RIGHT_MOVE 2
#define MOUSE_UP_MOVE 3
#define MOUSE_DOWN_MOVE 4
#define HID_MOUSE_HEIGHTLSB 2
#define HID_MOUSE_WIDTHLSB 2
#define HID_MOUSE_RES_X 4
#define HID_MOUSE_RES_Y 4
27 defines
Private define
extern DEMO_StateMachine demo;
Private variables
static void USR_MOUSE_Init(void);
static void HID_MOUSE_UpdatePosition(int8_t x, int8_t y);
Private function prototypes
/* ... */
void HID_MouseMenuProcess(void)
{
switch(demo.mouse_state)
{
case HID_MOUSE_IDLE:
demo.mouse_state = HID_MOUSE_START;
Demo_SelectItem(DEMO_MOUSE_menu, 0);
demo.select = 0;
break;
case HID_MOUSE_IDLE:
case HID_MOUSE_WAIT:
if(demo.select != prev_select)
{
prev_select = demo.select;
Demo_SelectItem(DEMO_MOUSE_menu, demo.select & 0x7F);
if(demo.select & 0x80)
{
demo.select &= 0x7F;
switch(demo.select)
{
case 0:
demo.mouse_state = HID_MOUSE_START;
break;
case 0:
case 1:
LCD_LOG_ClearTextZone();
demo.state = DEMO_IDLE;
LCD_UsrLogY("> HID application closed.\n");
demo.select = 0;
break;
case 1:
default:
break;default
}switch (demo.select) { ... }
}if (demo.select & 0x80) { ... }
}if (demo.select != prev_select) { ... }
break;
case HID_MOUSE_WAIT:
case HID_MOUSE_START:
USR_MOUSE_Init();
demo.mouse_state = HID_MOUSE_WAIT;
HID_MOUSE_UpdatePosition(0,0);
break;
case HID_MOUSE_START:
default:
break;default
}switch (demo.mouse_state) { ... }
}{ ... }
/* ... */
static void USR_MOUSE_Init(void)
{
LCD_LOG_ClearTextZone();
BSP_LCD_SetTextColor(LCD_COLOR_YELLOW);
BSP_LCD_DisplayStringAtLine(4, (uint8_t *)"USB HID Host Mouse Demo... ");
BSP_LCD_SetTextColor(LCD_LOG_DEFAULT_COLOR);
BSP_LCD_DrawRect(MOUSE_WINDOW_X, MOUSE_WINDOW_Y, MOUSE_WINDOW_WIDTH, MOUSE_WINDOW_HEIGHT);
HID_MOUSE_ButtonReleased(0);
HID_MOUSE_ButtonReleased(1);
HID_MOUSE_ButtonReleased(2);
BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
HID_MOUSE_UpdatePosition(0,0);
}{ ... }
/* ... */
void USR_MOUSE_ProcessData(HID_MOUSE_Info_TypeDef *data)
{
if((data->x != 0) && (data->y != 0))
{
HID_MOUSE_UpdatePosition(data->x , data->y);
}if ((data->x != 0) && (data->y != 0)) { ... }
}{ ... }
/* ... */
static void HID_MOUSE_UpdatePosition(int8_t x, int8_t y)
{
static int16_t x_loc = 0, y_loc = 0;
static int16_t prev_x = 5, prev_y = 1;
if((x != 0) || (y != 0))
{
x_loc += x/2;
y_loc += y/10;
if(y_loc > MOUSE_WINDOW_HEIGHT - 12)
{
y_loc = MOUSE_WINDOW_HEIGHT - 12;
}if (y_loc > MOUSE_WINDOW_HEIGHT - 12) { ... }
if(x_loc > MOUSE_WINDOW_WIDTH - 10)
{
x_loc = MOUSE_WINDOW_WIDTH - 10;
}if (x_loc > MOUSE_WINDOW_WIDTH - 10) { ... }
if(y_loc < 2)
{
y_loc = 2;
}if (y_loc < 2) { ... }
if(x_loc < 2)
{
x_loc = 2;
}if (x_loc < 2) { ... }
BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
BSP_LCD_DisplayChar(MOUSE_WINDOW_X + prev_x, MOUSE_WINDOW_Y + prev_y, 'x');
BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
BSP_LCD_DisplayChar(MOUSE_WINDOW_X + x_loc, MOUSE_WINDOW_Y + y_loc, 'x');
prev_x = x_loc;
prev_y = y_loc;
}if ((x != 0) || (y != 0)) { ... }
}{ ... }
/* ... */
void HID_MOUSE_ButtonPressed(uint8_t button_idx)
{
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
switch(button_idx)
{
case 0:
BSP_LCD_FillRect(HID_MOUSE_BUTTON1_XCHORD, HID_MOUSE_BUTTON_YCHORD,\
HID_MOUSE_BUTTON_WIDTH, HID_MOUSE_BUTTON_HEIGHT );
break;
case 0:
case 1:
BSP_LCD_FillRect(HID_MOUSE_BUTTON2_XCHORD,HID_MOUSE_BUTTON_YCHORD,\
HID_MOUSE_BUTTON_WIDTH,HID_MOUSE_BUTTON_HEIGHT);
break;
case 1:
case 2:
BSP_LCD_FillRect(HID_MOUSE_BUTTON3_XCHORD,HID_MOUSE_BUTTON_YCHORD,\
HID_MOUSE_BUTTON_WIDTH,HID_MOUSE_BUTTON_HEIGHT);
break;case 2:
}switch (button_idx) { ... }
}{ ... }
/* ... */
void HID_MOUSE_ButtonReleased(uint8_t button_idx)
{
BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
switch(button_idx)
{
case 0:
BSP_LCD_FillRect(HID_MOUSE_BUTTON1_XCHORD, HID_MOUSE_BUTTON_YCHORD,\
HID_MOUSE_BUTTON_WIDTH, HID_MOUSE_BUTTON_HEIGHT);
break;
case 0:
case 1:
BSP_LCD_FillRect(HID_MOUSE_BUTTON2_XCHORD, HID_MOUSE_BUTTON_YCHORD,\
HID_MOUSE_BUTTON_WIDTH, HID_MOUSE_BUTTON_HEIGHT);
break;
case 1:
case 2:
BSP_LCD_FillRect(HID_MOUSE_BUTTON3_XCHORD, HID_MOUSE_BUTTON_YCHORD,\
HID_MOUSE_BUTTON_WIDTH, HID_MOUSE_BUTTON_HEIGHT);
break;case 2:
}switch (button_idx) { ... }
}{ ... }