1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
22
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
70
71
72
77
82
83
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
113
120
121
127
128
129
130
131
132
133
134
135
136
137
141
145
146
150
154
155
156
157
158
159
160
161
162
163
164
165
166
171
172
173
174
175
176
177
178
198
199
200
205
206
207
208
209
210
211
212
232
/* ... */
#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 120
#define MOUSE_WINDOW_Y 120
#define MOUSE_WINDOW_X_MAX 181
#define MOUSE_WINDOW_Y_MIN 101
#define MOUSE_WINDOW_HEIGHT 200
#define MOUSE_WINDOW_WIDTH 500
#define HID_MOUSE_BUTTON_HEIGHT 30
#define HID_MOUSE_BUTTON_WIDTH 50
#define HID_MOUSE_BUTTON1_XCHORD 120
#define HID_MOUSE_BUTTON2_XCHORD 340
#define HID_MOUSE_BUTTON3_XCHORD 570
#define HID_MOUSE_BUTTON_YCHORD 340
#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)
{
USR_MOUSE_Init();
HID_MOUSE_UpdatePosition(0,0);
}{ ... }
/* ... */
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/2;
if(y_loc > (MOUSE_WINDOW_HEIGHT - 16))
{
y_loc = MOUSE_WINDOW_HEIGHT - 16;
}if (y_loc > (MOUSE_WINDOW_HEIGHT - 16)) { ... }
if(x_loc > (MOUSE_WINDOW_WIDTH - 12))
{
x_loc = MOUSE_WINDOW_WIDTH - 12;
}if (x_loc > (MOUSE_WINDOW_WIDTH - 12)) { ... }
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) { ... }
}{ ... }