Step 12: Review the Application Code
Application File: app_sdcard_audio_task.h
1
Open the file app_sdcard_audio_task.h. This file defines application states, data and APIs.
2
Application states corresponding to the state machine described in the Overview section are as follows:
3
The audio player states are as follows:
4
The data structure for audio CODEC is as follows:
- handle refers to the instance of the opened CODEC AK4953 driver.
- Buffered Harmony driver APIs (e.g., DRV_CODEC_BufferAddWrite()) returns a handle for each data buffer submitted for processing (Transmission). writeBufHandle holds the buffer handle for the audio data buffers submitted for transmission using the CODEC API.
- The application can register an event handler to notify when the submitted transmission request is completed. bufferHandler holds the address of the function which is called when the audio data buffer has been completed by the CODEC driver.
- context holds an application specific value passed to the driver.
- txbufferObject holds the address of the audio data that needs to be received from the CODEC.
- bufferSize holds the size of the audio data buffer.
5
The SD Card Player data structure is as follows:
- The fileHandle points to the currently opened WAV file (either from SD Card or Flash Drive).
- The fileStatus contains the status (File name, date & time etc.) of the currently opened WAV file.
- The state variable contains the current state of the player.
- The buffer holds the decoded audio data that will be submitted to the CODEC driver. The buffer members are:
- buffer holds the decoded audio data.
- inUse, a buffer is marked to be in use when the buffer is queued for playing with the CODEC.
- decoded, a buffer is marked as decoded when the buffer contains the decoded audio data (output of decoder).
- writeHandler, holds the buffer handle returned by a call to DRV_CODEC_BufferAddWrite().
- bufferSize indicates the number of decoded audio data elements available in the buffer.
- The currentSongIdx variable contains the index of the current song being played, into the array of song lists found on the media.
- The nextSongIdx variable points to the index of the next song to be played, into the array of song lists found on the media.
- totalAudioFiles holds the total number of audio files found on the media.
- The isRandomSongSet indicates whether the user has selected a random song (through the GUI) for playing.
- The randomSongIdx indicates the index of the random song selected for playing (through the GUI).
- The isSongListAvailable variable indicates whether any WAV file is available on the media or not.
- The nBytesRead variable indicates the number of bytes last read from the currently opened WAV file.
- The readBytes variable requests the chunk of bytes to be read from the currently opened WAV file.
- The currentFilesize contains the size of the currently opened WAV file.
- The readIdx and writeIdx variables index into the decoded audio data buffer queue and manages the circular buffers.
- The decodeDataSize variable indicates the number of bytes decoded by the WAV decoder.
- The nextTrack indicates whether a next track is to be selected for playing or not.
- The MP3_decoder_enabled and WAV_decoder_enabled indicate if MP3/WAV decoders are enabled or not.
- The isLoopSingleTrack variable indicates if looping of a single track is selected by the user (through the GUI).
- The isLoopTrackList indicates if the track list is selected for playing in a loop (repeat) mode by the user (through the GUI).
- The isShuffleOn indicates if shuffling of tracks in the track list is turned on by the user (through the GUI).
- The isPlayerRunning variable indicates whether the player is currently in running state or pause state.
- The nTotalBytesPlayed variable keeps track of total number of bytes played so far for the currently opened WAV file.
- The playerTrackTotalTime variable indicates the total play time of the currently opened WAV file.
- The playerBitrate indicates the bit-rate for the currently opened WAV file.
- The eventNotify is a function pointer that points to an event handler registered by the APP_DISPLAY_Tasks to receive events generated by the APP_SDCARD_AUDIO_Tasks task.
6
The SD Card Audio data structure is as follows:
- The variable state represents the various states the task can be.
- The variable codec holds audio CODEC related information (Refer to Step 4).
- The variable player holds the audio player related data (Refer to Step 5).
- The isDeviceConnected variable indicates whether a media device (SD Card or Flash Drive) is connected and a file system is mounted on the media.
- The mountedDeviceType variable indicates the type of media device (SD Card or Flash Drive) the file system is currently mounted on.
- The isThumbDriveConnected variable indicates if the Flash drive is currently connected or dis-connected (removed).
- isFSMountedOnThumbDrive variable indicates if a file system is mounted on the Flash drive or not.
Application File: app_sdcard_audio_task.c
7
Open file app_sdcard_audio_task.c. This file contains the application state machine and implements the APIs..
The main tasks performed by the app_sdcard_audio_task.c include, initialization and opening media, initialization of CODEC driver, scanning the media for WAV audio files, playing the WAV files and managing the switching between the two media types (SD Card and Flash Drive).
8
The APP_SDCARD_AUDIO_Initialize() initializes the SDCARD Audio Task and it's state machine. The function calls the APP_SDCARD_AUDIO_Player_Initialize() routine that initializes the player related data structures.
9
The task state machine is implemented by the APP_SDCARD_AUDIO_Tasks() routine. The state machine is initialized to **APP_SDCARD_AUDIO_USB_BUS_ENABLE* state which starts the operation of USB bus in host mode before moving to the state where it waits for the USB bus to be enabled.
10
The task state APP_SDCARD_AUDIO_WAIT_FOR_USB_BUS_ENABLE_COMPLETE waits for the USB host bus to be enabled. Once enabled, the state machine moves to the APP_SDCARD_AUDIO_CARD_MOUNT state.
11
The task state APP_SDCARD_AUDIO_CARD_MOUNT waits for the file system to be mounted on the selected media type (SD Card or Flash Drive). Once the file system is mounted, the state machine moves to the APP_SDCARD_AUDIO_CARD_CURRENT_DRIVE_SET state.
12
The task state APP_SDCARD_AUDIO_CARD_CURRENT_DRIVE_SET sets the current drive based on the media type on which the file system is currently mounted. The state machine then moves to the APP_SDCARD_AUDIO_STATE_CODEC_OPEN state.
13
The task state APP_SDCARD_AUDIO_STATE_CODEC_OPEN opens the audio CODEC driver and then moves to the APP_SDCARD_AUDIO_STATE_CODEC_SET_BUFFER_HANDLER state.
14
The task state APP_SDCARD_AUDIO_STATE_CODEC_SET_BUFFER_HANDLER sets a buffer event handler with the CODEC driver. It also sets the default volume level for the CODEC. The state machine then moves to the APP_SDCARD_AUDIO_STATE_RUNNING state.
15
The APP_SDCARD_AUDIO_STATE_RUNNING runs the audio player state machine. In this state, the selected media is first scanned for WAV files. After this, the first WAV file is selected for playing and the player state machine is run.
- The player state machine state APP_SDCARD_AUDIO_PLAYER_STATE_RUNNING reads the WAV file, and passes the read WAV file data to the decoder for decoding. The decoded audio data is copied to the CODEC buffer queue. For the first time, it also schedules a CODEC buffer write, when all the CODCE buffers are empty. Subsequently (after the first CODEC buffer completion event is generated), the CODEC buffer writes will be scheduled from the CODEC buffer completion event handler (APP_SDCARD_AUDIO_BufferEventHandler).
- The player state machine enters the APP_SDCARD_AUDIO_PLAYER_STATE_TRACK_CHANGE either at the end of the current track or when a track change is requested from the GUI to select a new track for playing. This state is also entered when the player is paused by pressing the play/pause button on the GUI.
- The player state APP_SDCARD_AUDIO_PLAYER_STATE_WAIT_FOR_BUFFER_COMPLETION waits for the completion of the already scheduled audio buffers with the CODEC driver before selecting a new track for playing (or before entering the paused state - APP_SDCARD_AUDIO_PLAYER_STATE_STOP).
- In the player state APP_SDCARD_AUDIO_PLAYER_STATE_STOP the player state machine stays until the player is put into running state (by the user pressing the play/pause button on the GUI).
Application File: app_display_task.h
16
Open file app_display_task.h. This file defines states, data and APIs related to the display task.
17
The enumerator DISP_CMD enumerates various events generated by the GUI and the requests generated by the SD Card Audio task.
18
The MSG_QUEUE data structure defines a queue for holding the events generated by the GUI and the requests generated by the SD Card Audio task. The display task parses these events/requests and forwards it to the appropriate task/handler.
- The inPtr and outPtr are used for buffer management.
- The maxLen is set to the maximum length of the message buffer.
- The pBuffer points to the message buffer that will be managed by the MSG_QUEUE.
19
The DISPLAY_TASK_DATA structure contains:
- msgBuffer member is the buffer that will be managed by the MSG_QUEUE.
- msgQueue is the MSG_QUEUE instance that manages the msgBuffer.
Application File: app_display_task.c
20
Open file app_display_task.c. This file defines the state machine for the display task and implements the APIs.
21
The APP_DISPLAY_Initialize routine initializes the message queue and registers a callback with the APP_SDCARD_AUDIO_Tasks task (APP_SDCARD_AUDIO_RegisterEventHandler) and the GUI (registerGuiEventsHandler) to receive events/requests generated by these tasks.
Note: The GUI is implemented in the audio_playerDLG.c and settings_windowDLG.c files.
22
The APP_DISPLAY_Tasks implements the state machine for the Display Task. The state machine waits for a message in the message Queue. Once a message is available, the task parses the message and forwards it to the respective task for handling by calling appropriate APIs.
Application File: app_audio_player_gui.h
23
Open file app_audio_player_gui.h. This file defines the states, data and APIs related to the GUI. This file serves as a common header file for both the audio player dialog audio_playerDLG.c and the settings window dialog settings_windowDLG.c
The file defines the enumerations used by various widgets in the GUI.
Application File: audio_playerDLG.c
24
Open file audio_playerDLG.c. This file is generated by the SEGGER emWin GUIBuilder utility. The file also contains application logic for customizing widgets and handling widget events.
- The file handles creation of the audio_player window in the Createaudio_player() routine.
- The createAudioPlayerScreens() routine calls the creation routines for both the audio_player window and the settings_window. The createAudioPlayerScreens() is registered with the SEGGER emWin GUI wrapper which then calls this function when the screen is set using the emWin_GuiStartScreenSet() API.
- The _cbDialog() implements the callback for the audio_player window. This callback handles initialization of the audio_player child widgets and handles the various events generated by it's child widgets. These events are then propagated to the APP_DISPLAY_Tasks task, by passing the event to the callback function registered by the APP_DISPLAY_Tasks task.
- The following lists the widget callbacks that handle custom rendering of the respective widget.
- cbProgressBar - Custom rendering of the Progbar widget
- cbPlayButton - Custom rendering of the Play Button widget.
- cbSettingsButton - Custom rendering of the Settings Button widget
- cbVolMuteButton - Custom rendering of the Mute Button widget
- cbTrackButton - Custom rendering of the Next Button and Prev Button widgets.
- cbRepeatButton - Custom rendering of the Repeat Button widget.
- cbScrollBar - Custom rendering of the scroll bar (GUI_ID_VSCROLL) associated with the track list widget.
- cbShuffleButton - Custom rendering of the Shuffle Button widget.
- cbTrackSlider - Custom rendering of the Track Slider widget.
- cbVolSlider - Custom rendering of the Vol Slider widget
- cbListboxOwnerDraw - Implements custom rendering of an item in the list box widget.
- In addition to this, the audio_playerDLG.c file implements various helper functions used by the APP_DISPLAY_Tasks task.
Application File: settings_windowDLG.c
25
Open file settings_windowDLG.c. This file is generated by the SEGGER emWin GUIBuilder utility. The file also contains application logic for customizing widgets and handling widget events.
- The file handles creation of the settings_window window in the Createsettings_window() routine.
- The local _cbDialog() routine implements the callback for the settings_window window. This callback handles initialization of the settings_window child widgets and handles the various events generated by it's child widgets. These events are then propagated to the APP_DISPLAY_Tasks task, by passing the event to the callback function registered by the APP_DISPLAY_Tasks task.
Note: The _cbDialog() routine is declared static and hence the function is local to the file in which the routine is declared. This allows both the audio_player window and the settings_window to use same name for their callback routine.
- The following lists the widget callbacks that handle custom rendering of the respective widget.
- drawSettingsDialogItems - Custom rendering of the settings_window dialog.
- cbSwitchButton - Custom rendering of the Switch 1, Switch 2, and Switch 3 widgets.
- In addition to this, the settings_windowDLG.c file implements various helper functions used by the APP_DISPLAY_Tasks task.
System Configuration File: system_config.h
This file defines the application specific configuration macros.
26
- The macros DECODER_MAX_OUTPUT_BUFFER_SIZE and DECODER_MAX_INPUT_BUFFER_SIZE define the size of output and input buffers to the decoder respectively.
- The macro APP_SDCARD_AUDIO_CODEC_WRITE_QUEUE_SIZE contains the size of the CODEC driver write buffer queue.
- The macro APP_SDCARD_AUDIO_CARD_MAX_DIRS defines the maximum number of folders that will be scanned for the audio files.
- The macro APP_SDCARD_AUDIO_CARD_MAX_FILES defines the maximum number of files that will be saved.
- The macro APP_DISPLAY_VOLUME_MAX defines the maximum volume that can be set on the speaker.