|
- #include <stdio.h>
- #include "my_includes.h"
- #include "my_types.h"
- #include "FatFileSystem.h" // FAT16 lib
- #include "SDCardDriver.h" // SDCard HAL
- #include "bitmap.h" // bmp lib
- #define WAITING_SEC 5
- // bmp file list parameter bmp文件列表参数
- #define MAX_FILE_NUM 128 // maximum file number in file list
- #define FILENAME_LEN 32 // length of file name
- typedef struct {
- int filenum;
- char filename[MAX_FILE_NUM][FILENAME_LEN];
- } BmpFileList;
- static BmpFileList bmp_file_list;
- // bmp parameter bmp参数
- #define BMP_WIDTH 800
- #define BMP_HEIGHT 480
- #define BMP_RGB_OFST 54
- #define NUM_RGB 3
- #define OFST_R 2
- #define OFST_G 1
- #define OFST_B 0
- // wait sdcard insert into socket
- void wait_sdcard_insert(void) {
- bool bFirstTime2Detect = TRUE;
-
- while(!SD_card_init()) {
- if (bFirstTime2Detect){
- printf("Please insert SD card.\n");
- bFirstTime2Detect = FALSE;
- }
- }
-
- printf("Find SD card.\n");
- }
- // build bmp file list
- int build_bmp_play_list(void) {
- int filecnt = 0; // number of bmp
- FAT_BROWSE_HANDLE hFileBrowse; // FAT browse handle
- FAT_DIRECTORY Directory; // FAT directory
- FAT_FILE_HANDLE hFile; // FAT file handle
- alt_u8 header[BMP_RGB_OFST];
- char filename[FILENAME_LEN];
- bmp_file_list.filenum = 0;
-
- // FatFileSystem.h
- if (!Fat_FileBrowseBegin(&hFileBrowse)){
- printf("browse file fail.\n");
- return 0;
- }
- // FatFileSystem.h
- while (Fat_FileBrowseNext(&hFileBrowse, &Directory)) {
- // only bmp in file list
- if (strncmpi(Directory.Extension, "BMP", 3))
- continue;
-
- // compose filename
- Fat_ComposeFilename(&Directory, filename);
- // fopen() (FatFileSystem.h)
- if (!Fat_FileOpen(&hFile, filename)) {
- printf("bmp file open fail.\n");
- continue;
- }
- // fread() (FatFileSystem.h)
- if (!Fat_FileRead(&hFile, header, sizeof(header))) {
- printf("bmp file read fail.\n");
- continue;
- }
- // fclose() (FatFileSystem.h)
- Fat_FileClose(&hFile);
-
- // convert to bmp header (bitmap.h)
- BitmapHeader *bmp_header = get_bmp_header(header);
- // check valid bmp format
- if (!chk_valid_bmp(bmp_header)) {
- printf("%s is invalid bmp for DE2-70 LTM.\n", filename);
- continue;
- }
- // copy filename into file list
- strcpy(bmp_file_list.filename[filecnt], filename);
- filecnt++;
- } // while
-
- bmp_file_list.filenum = filecnt;
-
- return filecnt;
- }
- // play bmp by filename
- bool play_bmp(char *filename){
- FAT_FILE_HANDLE hFile; // FAT file handle
-
- // fopen() (FatFileSystem.h)
- if (!Fat_FileOpen(&hFile, filename)){
- printf("Fat_FileOpen fail.\n");
- return FALSE;
- }
-
- printf("BMP file name is %s.\n", filename);
-
- if (!Fat_FileSeek(&hFile, FILE_SEEK_BEGIN, BMP_RGB_OFST)) {
- printf("Fat_FileSeek fail.\n");
- return FALSE;
- }
-
- // bmp RGB array
- alt_u8 buff_bmp[BMP_WIDTH * BMP_HEIGHT * NUM_RGB];
- // fread() (FatFileSystem.h)
- if (!Fat_FileRead(&hFile, buff_bmp, sizeof(buff_bmp))) {
- printf("Fat_FileRead fail.\n");
- return FALSE;
- }
-
- // move to sdram frame buffer//
- int x, y;
- for(x=0; x < BMP_WIDTH; x++) {
- for(y=0; y < BMP_HEIGHT; y++) {
- // get RGB
- int r = buff_bmp[(y * BMP_WIDTH + x) * NUM_RGB + OFST_R];
- int g = buff_bmp[(y * BMP_WIDTH + x) * NUM_RGB + OFST_G];
- int b = buff_bmp[(y * BMP_WIDTH + x) * NUM_RGB + OFST_B];
- }
- }
-
- printf("Finsh reading FAT16\n");
- // file close
- Fat_FileClose(&hFile);
-
- return TRUE;
- }
- int main() {
- int play_index = 0;
- alt_u8 filename[FILENAME_LEN];
-
- // check SD card
- wait_sdcard_insert();
-
- // Mount SD-CARD
- if (!Fat_Mount(FAT_SD_CARD)) {
- printf("SD card mount fail.\n");
- return -1;
- }
-
- // build wave list in gWavePlayList
- if (build_bmp_play_list() == 0) {
- printf("There is no bmp file in the root directory of SD card.\n");
- return -1;
- }
-
- while(1) {
- strcpy(filename, bmp_file_list.filename[play_index]);
-
- if (!play_bmp(filename)){
- printf("bmp play fail.\n");
- return -1;
- }
-
- play_index++;
-
- // repeat
- if (play_index >= bmp_file_list.filenum)
- play_index = 0;
-
- // waiting 5 sec between bmp
- printf("Waiting %d sec...\n", WAITING_SEC);
- usleep(WAITING_SEC * 1000 * 1000);
- }
- return 0;
- }
复制代码 |
|