Step 5: Review the Application Code
Application File: app_tone_textfile_sdcard.h
Application states corresponding to the state machine, described in the Overview section of this lab, are as follows. Note the newly added states highlighted in the red box.
The application data structure is shown in the accompanying image.
- The variable state and CODEC are retained from audio_player_lab1. They hold the state of application and codec-related variables respectively.
- audio_player_lab1 had the audio data saved in a global buffer, whereas audio_player_lab2 will read ASCII audio text data from a file saved in the SD card. This ASCII data needs to be parsed. The variable textParser holds the ASCII text data to be parsed (see APP_TONE_TEXTFILE_AUDIO_DATA_PARSER in the following figure).
- fileHandle holds the handle to a file opened by using the File System API call SYS_FS_FileOpen.
- fileSize holds the size of the opened file.
- currentFilePosition holds the current file position.
- buffer holds the ASCII data read from the tone.txt file. The buffer serves as an input to the audio data parser routine. It converts the comma-separated ASCII audio values to binary integer values.
- nElements holds the number of ASCII values present in the buffer that need to be parsed.
- The CODEC data structure will remain the same. It is included here for completeness.
Application File: app_tone_textfile_sdcard.c
The function APP_TONE_TEXTFILE_SDCARD_Tasks implements the task’s state machine as shown in the figure in the Overview section of this lab.
As with audio_player_lab1, it starts in APP_TONE_TEXTFILE_SDCARD_STATE_CODEC_OPEN state and remains in it until it opens the CODEC driver and gets a valid driver handle.
After the file system is successfully mounted, the state machine enters APP_TONE_TEXTFILE_SDCARD_STATE_CARD_CURRENT_DRIVE_SET state where it sets the current drive of the file system to the specified path and then opens the tone.txt audio file.
In the APP_TONE_TEXTFILE_SDCARD_STATE_READ_FILE_SIZE, the size of the opened file (tone.txt) is read.
The state APP_TONE_TEXTFILE_SDCARD_STATE_CARD_READ reads the ASCII audio data contained in the tone.txt file and passes the read buffer to the APP_TONE_TEXTFILE_SDCARD_Parse_Audio_Text_Data routine, which converts it to binary data and saves it in the buffer pointed by the CODEC variable.
The entire tone.txt file is read and converted in one shot, and the audio data buffer appToneTextFileSdcardToneBuffer is updated.
In the event handler APP_TONE_TEXTFILE_SDCARD_BufferEventHandler, the state of the task is changed from waiting to complete.