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
65
69
70
71
76
77
78
79
80
87
88
89
90
91
92
93
94
95
96
97
98
99
103
104
105
106
107
108
109
110
111
113
114
115
116
117
118
124
126
127
128
129
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
161
168
169
175
176
177
178
179
180
181
182
183
184
185
189
193
194
198
202
203
204
205
206
207
208
209
210
211
212
213
214
219
220
221
222
223
224
225
226
246
247
248
253
254
255
256
257
258
259
260
280
/* ... */
#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 7
#define MOUSE_WINDOW_Y 52
#define MOUSE_WINDOW_X_MAX 10
#define MOUSE_WINDOW_Y_MIN 50
#define MOUSE_WINDOW_HEIGHT 40
#define MOUSE_WINDOW_WIDTH 110
#define HID_MOUSE_BUTTON_HEIGHT 10
#define HID_MOUSE_BUTTON_WIDTH 30
#define HID_MOUSE_BUTTON1_XCHORD 8
#define HID_MOUSE_BUTTON2_XCHORD 48
#define HID_MOUSE_BUTTON3_XCHORD 88
#define HID_MOUSE_BUTTON_YCHORD 100
#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
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(hid_demo.mouse_state)
{
case HID_MOUSE_IDLE:
hid_demo.mouse_state = HID_MOUSE_START;
HID_SelectItem(DEMO_MOUSE_menu, 0);
hid_demo.select = 0;
prev_select = 0;
break;
case HID_MOUSE_IDLE:
case HID_MOUSE_WAIT:
if(hid_demo.select != prev_select)
{
prev_select = hid_demo.select ;
HID_SelectItem(DEMO_MOUSE_menu, hid_demo.select & 0x7F);
if(hid_demo.select & 0x80)
{
hid_demo.select &= 0x7F;
switch(hid_demo.select)
{
case 0:
hid_demo.mouse_state = HID_MOUSE_START;
break;
case 0:
case 1:
LCD_LOG_ClearTextZone();
BSP_LCD_ClearStringLine(12);
BSP_LCD_ClearStringLine(13);
hid_demo.state = HID_DEMO_REENUMERATE;
hid_demo.select = 0;
break;
case 1:
default:
break;default
}switch (hid_demo.select) { ... }
}if (hid_demo.select & 0x80) { ... }
}if (hid_demo.select != prev_select) { ... }
break;
case HID_MOUSE_WAIT:
case HID_MOUSE_START:
USR_MOUSE_Init();
hid_demo.mouse_state = HID_MOUSE_WAIT;
HID_MOUSE_UpdatePosition(0,0);
break;
case HID_MOUSE_START:
default:
break;default
}switch (hid_demo.mouse_state) { ... }
}{ ... }
/* ... */
static void USR_MOUSE_Init(void)
{
LCD_LOG_ClearTextZone();
BSP_LCD_ClearStringLine(12);
BSP_LCD_ClearStringLine(13);
BSP_LCD_SetTextColor(LCD_COLOR_YELLOW);
BSP_LCD_DisplayStringAtLine(5, (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/HID_MOUSE_RES_X;
y_loc += y/HID_MOUSE_RES_Y;
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_RED);
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) { ... }
}{ ... }