1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
31
32
33
34
35
36
37
38
39
40
45
46
47
48
49
50
51
52
53
54
61
62
76
77
78
83
84
85
86
87
88
89
90
91
92
99
100
104
105
106
107
108
109
110
111
112
113
119
120
121
126
127
128
132
133
134
135
136
141
142
143
144
145
146
147
148
149
150
151
157
163
169
175
177
178
179
180
181
182
188
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
212
213
214
215
216
217
218
219
220
221
222
223
227
231
232
233
234
235
236
237
238
239
240
241
244
245
/* ... */
#include "main.h"
/* ... */
/* ... */
Includes
static JOYState_TypeDef JoyState = JOY_NONE;
static void Joystick_SetHint(uint8_t exti_vs_polling);
/* ... */
void Joystick_polling_demo (void)
{
uint8_t status = 0;
Joystick_SetHint(0);
status = BSP_JOY_Init(JOY_MODE_GPIO);
if (status != IO_OK)
{
BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize()- 95, (uint8_t *)"ERROR", CENTER_MODE);
BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize()- 80, (uint8_t *)"Joystick cannot be initialized", CENTER_MODE);
}if (status != IO_OK) { ... }
while (1)
{
if (status == IO_OK)
{
JoyState = BSP_JOY_GetState();
Joystick_SetCursorPosition();
}if (status == IO_OK) { ... }
if(CheckForUserInput() > 0)
{
return;
}if (CheckForUserInput() > 0) { ... }
HAL_Delay(6);
}while (1) { ... }
}{ ... }
/* ... */
void Joystick_exti_demo (void)
{
uint8_t status = 0;
uint32_t ITstatus = 0;
Joystick_SetHint(1);
status = BSP_JOY_Init(JOY_MODE_EXTI);
if (status != IO_OK)
{
BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize()- 95, (uint8_t *)"ERROR", CENTER_MODE);
BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize()- 80, (uint8_t *)"Joystick cannot be initialized", CENTER_MODE);
}if (status != IO_OK) { ... }
if (status == IO_OK)
{
Joystick_SetCursorPosition();
}if (status == IO_OK) { ... }
while (1)
{
if (status == IO_OK)
{
if (MfxExtiReceived == 1)
{
MfxExtiReceived = 0;
ITstatus = BSP_IO_ITGetStatus(JOY_ALL_PINS);
if (ITstatus)
{
JoyState = BSP_JOY_GetState();
Joystick_SetCursorPosition();
}if (ITstatus) { ... }
BSP_IO_ITClear();
while ( BSP_JOY_GetState() != JOY_NONE)
{
Joystick_SetCursorPosition();
HAL_Delay(5);
}while (BSP_JOY_GetState() != JOY_NONE) { ... }
}if (MfxExtiReceived == 1) { ... }
}if (status == IO_OK) { ... }
if(CheckForUserInput() > 0)
{
return;
}if (CheckForUserInput() > 0) { ... }
HAL_Delay(5);
...}
}{ ... }
/* ... */
void Joystick_SetCursorPosition(void)
{
static uint16_t xPtr = 12;
static uint16_t yPtr = 92;
static uint16_t old_xPtr = 12;
static uint16_t old_yPtr = 92;
switch(JoyState)
{
case JOY_UP:
if(yPtr > 92)
{
yPtr--;
}if (yPtr > 92) { ... }
break; case JOY_UP:
case JOY_DOWN:
if(yPtr < (BSP_LCD_GetYSize() - 12 - 11))
{
yPtr++;
}if (yPtr < (BSP_LCD_GetYSize() - 12 - 11)) { ... }
break; case JOY_DOWN:
case JOY_LEFT:
if(xPtr > 12)
{
xPtr--;
}if (xPtr > 12) { ... }
break; case JOY_LEFT:
case JOY_RIGHT:
if(xPtr < (BSP_LCD_GetXSize() - 8 - 11))
{
xPtr++;
}if (xPtr < (BSP_LCD_GetXSize() - 8 - 11)) { ... }
break; case JOY_RIGHT:
default:
break; default
}switch (JoyState) { ... }
BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
if(JoyState == JOY_SEL)
{
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayChar(xPtr, yPtr, 'X');
}if (JoyState == JOY_SEL) { ... }
else if(JoyState == JOY_NONE)
{
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
BSP_LCD_DisplayChar(xPtr, yPtr, 'X');
}else if (JoyState == JOY_NONE) { ... }
else
{
BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
BSP_LCD_DisplayChar(old_xPtr, old_yPtr, 'X');
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
BSP_LCD_DisplayChar(xPtr, yPtr, 'X');
old_xPtr = xPtr;
old_yPtr = yPtr;
}else { ... }
}{ ... }
/* ... */
static void Joystick_SetHint(uint8_t exti_vs_polling)
{
BSP_LCD_Clear(LCD_COLOR_WHITE);
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
BSP_LCD_FillRect(0, 0, BSP_LCD_GetXSize(), 80);
BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
BSP_LCD_SetFont(&Font24);
if (exti_vs_polling == 0)
{
BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"MFX Joystick", CENTER_MODE);
}if (exti_vs_polling == 0) { ... }
else
{
BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"MFX Joystick Exti", CENTER_MODE);
}else { ... }
BSP_LCD_SetFont(&Font12);
BSP_LCD_DisplayStringAt(0, 30, (uint8_t *)"Please use the joystick to", CENTER_MODE);
BSP_LCD_DisplayStringAt(0, 45, (uint8_t *)"move the pointer inside the", CENTER_MODE);
BSP_LCD_DisplayStringAt(0, 60, (uint8_t *)"rectangle", CENTER_MODE);
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
BSP_LCD_DrawRect(10, 90, BSP_LCD_GetXSize() - 20, BSP_LCD_GetYSize()- 100);
BSP_LCD_DrawRect(11, 91, BSP_LCD_GetXSize() - 22, BSP_LCD_GetYSize()- 102);
}{ ... }
/* ... */
/* ... */