1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
21
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
50
51
52
53
54
55
56
57
62
63
64
130
131
132
133
134
142
143
144
145
146
147
148
149
150
151
153
154
155
177
178
179
180
181
186
202
203
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
230
231
232
233
234
235
236
237
238
239
240
241
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
269
270
274
275
276
281
304
305
310
/* ... */
#include "main.h"
Includes
#define TX_BUFF_SIZE (64 * 1024)
Private define
uint8_t *DEMO_SEND_menu[] = {
(uint8_t *)
" 1 - Send Dummy Data ",
(uint8_t *)
" 2 - Send File ",
(uint8_t *)
" 3 - Return ",
(uint8_t *)
" ",
...};
uint8_t BLANK_LINE[] =
" ";
Private typedef
FIL MyFile = { 0 };
uint8_t FilePos = 0;
uint8_t FileOffset = 0;
uint8_t PrevPos = 0;
uint8_t CDC_TX_Buffer[TX_BUFF_SIZE];
uint32_t EnableSendData = 0;
uint32_t use_file = 0;
Private variables
static void CDC_SendFile(uint8_t fileidx);
static void ReturnFromSendMenu(void);
static void CDC_ShowFiles(uint8_t offset, uint8_t select);
static void SendFileMenu_Init(void);
/* ... */
void CDC_Handle_Send_Menu(void)
{
if (CdcSelectMode == CDC_SELECT_MENU)
{
switch (CdcDemo.Send_state)
{
case CDC_SEND_IDLE:
CdcDemo.Send_state = CDC_SEND_WAIT;
CDC_SelectItem(DEMO_SEND_menu, 0);
CdcDemo.select = 0;
PrevPos = 0;
USBH_CDC_Stop(&hUSBHost);
break;
case CDC_SEND_IDLE:
case CDC_SEND_WAIT:
if (CdcDemo.select != PrevSelect)
{
PrevSelect = CdcDemo.select;
CDC_SelectItem(DEMO_SEND_menu, CdcDemo.select & 0x7F);
if (CdcDemo.select & 0x80)
{
switch (CdcDemo.select & 0x7F)
{
case 0:
memset(CDC_TX_Buffer, 0x5A, TX_BUFF_SIZE);
LCD_DbgLog("Sending data ...\n");
USBH_CDC_Transmit(&hUSBHost, CDC_TX_Buffer, TX_BUFF_SIZE);
use_file = 0;
break;
case 0:
case 1:
if (FileList.ptr > 0)
{
SendFileMenu_Init();
if (FilePos >= 9)
{
CDC_ShowFiles(FileOffset, 9);
}if (FilePos >= 9) { ... }
else
{
CDC_ShowFiles(FileOffset, FilePos);
}else { ... }
CdcDemo.Send_state = CDC_SEND_SELECT_FILE;
}if (FileList.ptr > 0) { ... }
else
{
LCD_ErrLog("No file on the microSD\n");
}else { ... }
break;
case 1:
case 2:
ReturnFromSendMenu();
break;case 2:
default:
break;default
}switch (CdcDemo.select & 0x7F) { ... }
}if (CdcDemo.select & 0x80) { ... }
}if (CdcDemo.select != PrevSelect) { ... }
break;
case CDC_SEND_WAIT:
default:
break;default
}switch (CdcDemo.Send_state) { ... }
}if (CdcSelectMode == CDC_SELECT_MENU) { ... }
else if (CdcSelectMode == CDC_SELECT_FILE)
{
switch (CdcDemo.Send_state)
{
case CDC_SEND_SELECT_FILE:
if (CdcDemo.select & 0x80)
{
CdcDemo.select &= 0x7F;
CdcDemo.Send_state = CDC_SEND_TRANSMIT_FILE;
}if (CdcDemo.select & 0x80) { ... }
break;
case CDC_SEND_SELECT_FILE:
case CDC_SEND_TRANSMIT_FILE:
LCD_DbgLog("Sending file ...\n");
use_file = 1;
CDC_SendFile(FilePos);
CDC_ChangeSelectMode(CDC_SELECT_MENU);
LCD_LOG_UpdateDisplay();
CdcDemo.Send_state = CDC_SEND_WAIT;
break;
case CDC_SEND_TRANSMIT_FILE:
default:
break;default
}switch (CdcDemo.Send_state) { ... }
if (FilePos != PrevPos)
{
if (((FilePos > 9) && (FilePos > PrevPos)) ||
((FilePos >= 9) && (FilePos < PrevPos)))
{
if (FilePos > PrevPos)
{
FileOffset++;
}if (FilePos > PrevPos) { ... }
else
{
FileOffset--;
}else { ... }
CDC_ShowFiles(FileOffset, 9);
}if (((FilePos > 9) && (FilePos > PrevPos)) || ((FilePos >= 9) && (FilePos < PrevPos))) { ... }
else
{
CDC_ShowFiles(0, FilePos);
}else { ... }
PrevPos = FilePos;
}if (FilePos != PrevPos) { ... }
}else if (CdcSelectMode == CDC_SELECT_FILE) { ... }
CdcDemo.select &= 0x7F;
}{ ... }
/* ... */
void CDC_SendFile_ProbeKey(JOYState_TypeDef state)
{
if ((state == JOY_UP) && (FilePos > 0))
{
FilePos--;
}if ((state == JOY_UP) && (FilePos > 0)) { ... }
else if ((state == JOY_DOWN) && (FilePos < (FileList.ptr - 1)))
{
FilePos++;
}else if ((state == JOY_DOWN) && (FilePos < (FileList.ptr - 1))) { ... }
else if (state == JOY_SEL)
{
CdcDemo.select |= 0x80;
}else if (state == JOY_SEL) { ... }
}{ ... }
/* ... */
static void SendFileMenu_Init(void)
{
BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
BSP_LCD_DisplayStringAtLine(14,
(uint8_t *)
" ");
BSP_LCD_DisplayStringAtLine(15,
(uint8_t *)
"[User Key] to switch menu ");
BSP_LCD_DisplayStringAtLine(16,
(uint8_t *)
"[Joystick Up/Down] to change settings items");
CDC_ChangeSelectMode(CDC_SELECT_FILE);
}{ ... }
/* ... */
static void ReturnFromSendMenu(void)
{
CdcDemo.state = CDC_DEMO_IDLE;
CdcDemo.select = 0;
LCD_ClearTextZone();
LCD_LOG_UpdateDisplay();
Menu_Init();
}{ ... }
/* ... */
static void CDC_SendFile(uint8_t fileidx)
{
uint32_t bytesread;
if (FileList.ptr > 0)
{
f_close(&MyFile);
if (f_open
(&MyFile, (char *)FileList.file[fileidx].name,
FA_OPEN_EXISTING | FA_READ) == FR_OK)
{
f_read(&MyFile, CDC_TX_Buffer, TX_BUFF_SIZE, (void *)&bytesread);
USBH_CDC_Transmit(&hUSBHost, CDC_TX_Buffer, bytesread);
}if (f_open (&MyFile, (char *)FileList.file[fileidx].name, FA_OPEN_EXISTING | FA_READ) == FR_OK) { ... }
else
{
LCD_ErrLog("Cannot open %s file\n", FileList.file[0].name);
}else { ... }
}if (FileList.ptr > 0) { ... }
else
{
LCD_ErrLog("No file on the microSD\n");
}else { ... }
}{ ... }
/* ... */
static void CDC_ShowFiles(uint8_t offset, uint8_t select)
{
uint8_t i = 0;
if (offset < FileList.ptr)
{
BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
for (i = 4; i < 14; i++)
{
BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
BSP_LCD_DisplayStringAtLine(i, BLANK_LINE);
if ((i - 4) < FileList.ptr - offset)
{
if (i == (select + 4))
{
BSP_LCD_SetBackColor(LCD_COLOR_MAGENTA);
}if (i == (select + 4)) { ... }
BSP_LCD_DisplayStringAtLine(i, FileList.file[i - 4 + offset].name);
}if ((i - 4) < FileList.ptr - offset) { ... }
}for (i = 4; i < 14; i++) { ... }
}if (offset < FileList.ptr) { ... }
}{ ... }
/* ... */
void USBH_CDC_TransmitCallback(USBH_HandleTypeDef * phost)
{
uint32_t bytesread;
if (use_file == 1)
{
if (MyFile.fptr >= f_size(& MyFile))
{
f_close(&MyFile);
LCD_DbgLog(">> File sent\n");
use_file = 0;
}if (MyFile.fptr >= f_size(& MyFile)) { ... }
else
{
f_read(&MyFile, CDC_TX_Buffer, TX_BUFF_SIZE, (void *)&bytesread);
USBH_CDC_Transmit(&hUSBHost, CDC_TX_Buffer, bytesread);
}else { ... }
}if (use_file == 1) { ... }
else
{
LCD_DbgLog(">> Data sent\n");
}else { ... }
}{ ... }