Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "waveplayer.h"
#include "waverecorder.h"
Global extern variables
MfxExtiReceived
state
Private variables
AudioDemo
PrevSelect
AudioSelectMode
AudioState
AUDIO_main_menu
Private function prototypes
AUDIO_MenuInit()
AUDIO_MenuProcess()
Joystick_AudioMenu()
HAL_GPIO_EXTI_Callback(uint16_t)
AUDIO_ChangeSelectMode(AUDIO_DEMO_SelectMode)
AUDIO_SelectItem(uint8_t **, uint8_t)
AUDIO_MenuProbeKey(JOYState_TypeDef)
LCD_ClearTextZone()
Files
loading...
CodeScopeSTM32 Libraries and SamplesAudio_playback_and_recordSrc/menu.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file Audio/Audio_playback_and_record/Src/menu.c * @author MCD Application Team * @brief This file implements Menu Functions ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* ... */ /* Includes ------------------------------------------------------------------*/ #include "waveplayer.h" #include "waverecorder.h" Includes /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Global extern variables ---------------------------------------------------*/ static volatile uint8_t MfxExtiReceived = 0; uint8_t state = 0; Global extern variables /* Private variables ---------------------------------------------------------*/ AUDIO_DEMO_StateMachine AudioDemo; uint8_t PrevSelect = 0; AUDIO_DEMO_SelectMode AudioSelectMode; AUDIO_PLAYBACK_StateTypeDef AudioState; uint8_t *AUDIO_main_menu[] = { (uint8_t *)" 1 - Explore Audio File ", (uint8_t *)" 2 - Start Audio Player ", (uint8_t *)" 3 - Start Audio Recorder ", ...}; Private variables /* Private function prototypes -----------------------------------------------*/ static void AUDIO_SelectItem(uint8_t **menu, uint8_t item); static void AUDIO_MenuProbeKey(JOYState_TypeDef state); static void AUDIO_ChangeSelectMode(AUDIO_DEMO_SelectMode select_mode); static void LCD_ClearTextZone(void); static void Joystick_AudioMenu(void); Private function prototypes /* Private functions ---------------------------------------------------------*/ /** * @brief Demo state machine. * @param None * @retval None *//* ... */ void AUDIO_MenuInit(void) { AudioDemo.state = AUDIO_DEMO_IDLE; AudioSelectMode = AUDIO_SELECT_MENU; AUDIO_SelectItem(AUDIO_main_menu, 0); }{ ... } /** * @brief Manages AUDIO Menu Process. * @param None * @retval None *//* ... */ void AUDIO_MenuProcess(void) { uint32_t ITstatus = 0; AUDIO_ErrorTypeDef status; static uint32_t debounce_time = 0; if(AppliState == APPLICATION_READY) { switch(AudioDemo.state) { case AUDIO_DEMO_IDLE: AudioDemo.state = AUDIO_DEMO_WAIT; BSP_LCD_SetTextColor(LCD_COLOR_GREEN); BSP_LCD_DisplayStringAtLine(24, (uint8_t *)"Use [Joystick Left/Right] to scroll up/down"); BSP_LCD_DisplayStringAtLine(25, (uint8_t *)"Use [Joystick Up/Down] to scroll Audio Playback and Record menu"); break; case AUDIO_DEMO_IDLE: case AUDIO_DEMO_WAIT: if(AudioDemo.select != PrevSelect) { PrevSelect = AudioDemo.select; AUDIO_SelectItem(AUDIO_main_menu, AudioDemo.select & 0x7F); /* Handle select item */ if(AudioDemo.select & 0x80) { switch(AudioDemo.select & 0x7F) { case 0: AudioDemo.state = AUDIO_DEMO_EXPLORE; break; case 0: case 1: /* Display HMI messages */ BSP_LCD_SetTextColor(LCD_COLOR_GREEN); BSP_LCD_DisplayStringAtLine(22 ,(uint8_t *)" "); BSP_LCD_DisplayStringAtLine(23 ,(uint8_t *)" "); BSP_LCD_DisplayStringAtLine(24 ,(uint8_t *)" "); BSP_LCD_DisplayStringAtLine(25 ,(uint8_t *)" "); BSP_LCD_DisplayStringAtLine(26 ,(uint8_t *)"Use [Tamper Key] To Stop and return from player/recorder "); BSP_LCD_SetTextColor(LCD_COLOR_WHITE); /* Set PLAYBACK state and start playing 1st file */ AudioState = AUDIO_STATE_IDLE; AudioDemo.state = AUDIO_DEMO_PLAYBACK; AUDIO_ChangeSelectMode(AUDIO_PLAYBACK_CONTROL); break; case 1: case 2: /* Display HMI messages */ BSP_LCD_SetTextColor(LCD_COLOR_GREEN); BSP_LCD_DisplayStringAtLine(22 ,(uint8_t *)" "); BSP_LCD_DisplayStringAtLine(23 ,(uint8_t *)" "); BSP_LCD_DisplayStringAtLine(24 ,(uint8_t *)" "); BSP_LCD_DisplayStringAtLine(25 ,(uint8_t *)" "); BSP_LCD_DisplayStringAtLine(26 ,(uint8_t *)"Use [Tamper Key] To Stop and return from player/recorder "); BSP_LCD_SetTextColor(LCD_COLOR_WHITE); /* Set PLAYBACK state and start playing 1st file */ AudioState = AUDIO_STATE_IDLE; AudioDemo.state = AUDIO_DEMO_IN; AUDIO_ChangeSelectMode(AUDIO_PLAYBACK_CONTROL); break; case 2: default: break;default }switch (AudioDemo.select & 0x7F) { ... } }if (AudioDemo.select & 0x80) { ... } }if (AudioDemo.select != PrevSelect) { ... } break; case AUDIO_DEMO_WAIT: case AUDIO_DEMO_EXPLORE: if(AppliState == APPLICATION_READY) { if(AUDIO_ShowWavFiles() > 0) { LCD_ErrLog("There is no WAV file on the USB Key.\n"); AUDIO_ChangeSelectMode(AUDIO_SELECT_MENU); AudioDemo.state = AUDIO_DEMO_IDLE; }if (AUDIO_ShowWavFiles() > 0) { ... } else { AudioDemo.state = AUDIO_DEMO_WAIT; }else { ... } }if (AppliState == APPLICATION_READY) { ... } else { AudioDemo.state = AUDIO_DEMO_WAIT; }else { ... } break; case AUDIO_DEMO_EXPLORE: case AUDIO_DEMO_PLAYBACK: if(AppliState == APPLICATION_READY) { if(AudioState == AUDIO_STATE_IDLE) { if(AUDIO_ShowWavFiles() > 0) { LCD_ErrLog("There is no WAV file on the USB Key.\n"); AUDIO_ChangeSelectMode(AUDIO_SELECT_MENU); AudioDemo.state = AUDIO_DEMO_IDLE; }if (AUDIO_ShowWavFiles() > 0) { ... } else { /* Start Playing */ AudioState = AUDIO_STATE_INIT; }else { ... } if(AUDIO_PLAYER_Start(0) == AUDIO_ERROR_IO) { AUDIO_ChangeSelectMode(AUDIO_SELECT_MENU); AudioDemo.state = AUDIO_DEMO_IDLE; }if (AUDIO_PLAYER_Start(0) == AUDIO_ERROR_IO) { ... } else { BSP_LCD_SetTextColor(LCD_COLOR_YELLOW); BSP_LCD_DisplayStringAtLine(21, (uint8_t *)"[ UP ] : Volume + "); BSP_LCD_DisplayStringAtLine(22, (uint8_t *)"[ DOWN ] : Volume - "); BSP_LCD_DisplayStringAtLine(23, (uint8_t *)"[ LEFT ] : Previous "); BSP_LCD_DisplayStringAtLine(24, (uint8_t *)"[ RIGHT ] : Next "); BSP_LCD_DisplayStringAtLine(25, (uint8_t *)"[ SEL ] : Pause/Resume "); BSP_LCD_SetTextColor(LCD_COLOR_WHITE); }else { ... } }if (AudioState == AUDIO_STATE_IDLE) { ... } else /* Not idle */ { if(AUDIO_PLAYER_Process() == AUDIO_ERROR_IO) { AUDIO_ChangeSelectMode(AUDIO_SELECT_MENU); AudioDemo.state = AUDIO_DEMO_IDLE; }if (AUDIO_PLAYER_Process() == AUDIO_ERROR_IO) { ... } }else { ... } }if (AppliState == APPLICATION_READY) { ... } else { AudioDemo.state = AUDIO_DEMO_WAIT; }else { ... } break; case AUDIO_DEMO_PLAYBACK: case AUDIO_DEMO_IN: if(AppliState == APPLICATION_READY) { if(AudioState == AUDIO_STATE_IDLE) { /* Start Playing */ AudioState = AUDIO_STATE_INIT; /* Init storage */ AUDIO_StorageInit(); /* Configure the audio recorder: sampling frequency, bits-depth, number of channels */ if(AUDIO_REC_Start() == AUDIO_ERROR_IO) { AUDIO_ChangeSelectMode(AUDIO_SELECT_MENU); AudioDemo.state = AUDIO_DEMO_IDLE; }if (AUDIO_REC_Start() == AUDIO_ERROR_IO) { ... } else { BSP_LCD_SetTextColor(LCD_COLOR_YELLOW); BSP_LCD_DisplayStringAtLine(23, (uint8_t *)"[ UP ] : Volume + "); BSP_LCD_DisplayStringAtLine(24, (uint8_t *)"[ DOWN ] : Volume - "); BSP_LCD_DisplayStringAtLine(25, (uint8_t *)"[ SEL ] : Pause/Resume "); BSP_LCD_SetTextColor(LCD_COLOR_WHITE); }else { ... } }if (AudioState == AUDIO_STATE_IDLE) { ... } else /* Not idle */ { status = AUDIO_REC_Process(); if((status == AUDIO_ERROR_IO) || (status == AUDIO_ERROR_EOF)) { AUDIO_ChangeSelectMode(AUDIO_SELECT_MENU); AudioDemo.state = AUDIO_DEMO_IDLE; }if ((status == AUDIO_ERROR_IO) || (status == AUDIO_ERROR_EOF)) { ... } }else { ... } }if (AppliState == APPLICATION_READY) { ... } else { AudioDemo.state = AUDIO_DEMO_WAIT; }else { ... } break; case AUDIO_DEMO_IN: default: break;default }switch (AudioDemo.state) { ... } }if (AppliState == APPLICATION_READY) { ... } if(AppliState == APPLICATION_DISCONNECT) { AppliState = APPLICATION_IDLE; AUDIO_ChangeSelectMode(AUDIO_SELECT_MENU); BSP_AUDIO_OUT_Stop(CODEC_PDWN_SW); }if (AppliState == APPLICATION_DISCONNECT) { ... } AudioDemo.select &= 0x7F; if (MfxExtiReceived == 1) { MfxExtiReceived = 0; ITstatus = BSP_IO_ITGetStatus(JOY_ALL_PINS); if (ITstatus) { /* Prevent debounce effect for Tamper key */ if((HAL_GetTick() - debounce_time) > 200) { debounce_time = HAL_GetTick(); Joystick_AudioMenu(); }if ((HAL_GetTick() - debounce_time) > 200) { ... } /* Clear joystick interrupt pending bits */ BSP_IO_ITClear(); }if (ITstatus) { ... } }if (MfxExtiReceived == 1) { ... } }{ ... } /** * @brief Joystick audio menu * @param None * @retval None *//* ... */ void Joystick_AudioMenu(void) { static JOYState_TypeDef JoyState = JOY_NONE; /* Get the Joystick State */ JoyState = BSP_JOY_GetState(); if(AudioSelectMode == AUDIO_SELECT_MENU) { AUDIO_MenuProbeKey(JoyState); switch(JoyState) { case JOY_LEFT: LCD_LOG_ScrollBack(); break; case JOY_LEFT: case JOY_RIGHT: LCD_LOG_ScrollForward(); break; case JOY_RIGHT: default: break; default }switch (JoyState) { ... } }if (AudioSelectMode == AUDIO_SELECT_MENU) { ... } else if(AudioSelectMode == AUDIO_PLAYBACK_CONTROL) { AUDIO_PlaybackProbeKey(JoyState); }else if (AudioSelectMode == AUDIO_PLAYBACK_CONTROL) { ... } }{ ... } /** * @brief EXTI line detection callbacks. * @param GPIO_Pin: Specifies the pins connected EXTI line * @retval None *//* ... */ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { static uint32_t debounce_time = 0; if(GPIO_Pin == MFX_IRQOUT_PIN) { /* The different functionalities of MFX (TS, Joystick, SD detection, etc. ) can be configured in exti mode to generate an IRQ on given events. The MFX IRQ_OUT pin is unique and common to all functionalities, so if several functionalities are configured in exit mode, the MCU has to enquire MFX about the IRQ source (see BSP_IO_ITGetStatus). Communication with Mfx is done by I2C. Often the sw requires ISRs (irq service routines) to be quick while communication with I2C can be considered relatively long (hundreds of usec depending on I2C clk). Considering that the features for human interaction like TS, Joystick, SD detection don�t need immediate reaction, it is suggested to use POLLING instead of EXTI mode, in order to avoid "blocking I2C communication" on interrupt service routines *//* ... */ /* Here an example of implementation is proposed: mix between pooling and exit: On ISR a flag is set (MfxIrqReceived), the main loop polls on the flag; Mcu communicates with Mfx only when the flag has been set. This is just an example: the users should choose they strategy depending on their application needs.*//* ... */ MfxExtiReceived = 1; }if (GPIO_Pin == MFX_IRQOUT_PIN) { ... } if(AudioDemo.state == AUDIO_DEMO_PLAYBACK) { if(GPIO_Pin == TAMPER_BUTTON_PIN) { /* Prevent debounce effect for Tamper key */ if((HAL_GetTick() - debounce_time) > 50) { debounce_time = HAL_GetTick(); }if ((HAL_GetTick() - debounce_time) > 50) { ... } else { return; }else { ... } /* Change the selection type */ if(AudioSelectMode == AUDIO_SELECT_MENU) { AUDIO_ChangeSelectMode(AUDIO_PLAYBACK_CONTROL); }if (AudioSelectMode == AUDIO_SELECT_MENU) { ... } else if(AudioSelectMode == AUDIO_PLAYBACK_CONTROL) { AUDIO_PLAYER_Stop(); }else if (AudioSelectMode == AUDIO_PLAYBACK_CONTROL) { ... } }if (GPIO_Pin == TAMPER_BUTTON_PIN) { ... } }if (AudioDemo.state == AUDIO_DEMO_PLAYBACK) { ... } if(AudioDemo.state == AUDIO_DEMO_IN) { if(GPIO_Pin == TAMPER_BUTTON_PIN) { /* Prevent debounce effect for Tamper key */ if((HAL_GetTick() - debounce_time) > 50) { debounce_time = HAL_GetTick(); }if ((HAL_GetTick() - debounce_time) > 50) { ... } else { return; }else { ... } AudioState = AUDIO_STATE_STOP; }if (GPIO_Pin == TAMPER_BUTTON_PIN) { ... } }if (AudioDemo.state == AUDIO_DEMO_IN) { ... } }{ ... } /******************************************************************************* Static Functions *******************************************************************************//* ... */ /** * @brief Changes the selection mode. * @param select_mode: Selection mode * @retval None *//* ... */ static void AUDIO_ChangeSelectMode(AUDIO_DEMO_SelectMode select_mode) { if(select_mode == AUDIO_SELECT_MENU) { AUDIO_SelectItem(AUDIO_main_menu, 0x00); LCD_LOG_UpdateDisplay(); AudioDemo.state = AUDIO_DEMO_IDLE; AUDIO_PLAYER_Stop(); }if (select_mode == AUDIO_SELECT_MENU) { ... } else if(select_mode == AUDIO_PLAYBACK_CONTROL) { LCD_ClearTextZone(); AUDIO_SelectItem(AUDIO_main_menu, 0xFF); }else if (select_mode == AUDIO_PLAYBACK_CONTROL) { ... } AudioSelectMode = select_mode; AudioDemo.select = 0; }{ ... } /** * @brief Manages the menu on the screen. * @param menu: Menu table * @param item: Selected item to be highlighted * @retval None *//* ... */ static void AUDIO_SelectItem(uint8_t **menu, uint8_t item) { BSP_LCD_SetTextColor(LCD_COLOR_WHITE); switch(item) { case 0: BSP_LCD_SetBackColor(LCD_COLOR_MAGENTA); BSP_LCD_DisplayStringAtLine(27, menu [0]); BSP_LCD_SetBackColor(LCD_COLOR_BLUE); BSP_LCD_DisplayStringAtLine(28, menu [1]); BSP_LCD_DisplayStringAtLine(29, menu [2]); break; case 0: case 1: BSP_LCD_SetBackColor(LCD_COLOR_BLUE); BSP_LCD_DisplayStringAtLine(27, menu [0]); BSP_LCD_SetBackColor(LCD_COLOR_MAGENTA); BSP_LCD_DisplayStringAtLine(28, menu [1]); BSP_LCD_SetBackColor(LCD_COLOR_BLUE); BSP_LCD_DisplayStringAtLine(29, menu [2]); break; case 1: case 2: BSP_LCD_SetBackColor(LCD_COLOR_BLUE); BSP_LCD_DisplayStringAtLine(27, menu [0]); BSP_LCD_DisplayStringAtLine(28, menu [1]); BSP_LCD_SetBackColor(LCD_COLOR_MAGENTA); BSP_LCD_DisplayStringAtLine(29, menu [2]); break; case 2: default: BSP_LCD_SetBackColor(LCD_COLOR_BLUE); BSP_LCD_DisplayStringAtLine(27, menu [0]); BSP_LCD_DisplayStringAtLine(28, menu [1]); BSP_LCD_DisplayStringAtLine(29, menu [2]); break; default }switch (item) { ... } BSP_LCD_SetBackColor(LCD_COLOR_BLACK); }{ ... } /** * @brief Probes the Audio joystick state. * @param state: Joystick state * @retval None *//* ... */ static void AUDIO_MenuProbeKey(JOYState_TypeDef state) { /* Handle Menu inputs */ if((state == JOY_UP) && (AudioDemo.select > 0)) { AudioDemo.select--; }if ((state == JOY_UP) && (AudioDemo.select > 0)) { ... } else if((state == JOY_DOWN) && (AudioDemo.select < 2)) { AudioDemo.select++; }else if ((state == JOY_DOWN) && (AudioDemo.select < 2)) { ... } else if(state == JOY_SEL) { AudioDemo.select |= 0x80; }else if (state == JOY_SEL) { ... } }{ ... } /** * @brief Clears the text zone. * @param None * @retval None *//* ... */ static void LCD_ClearTextZone(void) { uint8_t i = 0; for(i= 0; i < 13; i++) { BSP_LCD_ClearStringLine(i + 3); }for (i= 0; i < 13; i++) { ... } }{ ... }