📄 main.c
字号:
/*
* File: main.c
* Purpose: Main loop
* Author: Peter Ivanov
* Modified by:
* Created: 2007-05-19 11:31:29
* Last modify: 2007-10-21 11:45:17 ivanovp {Time-stamp}
* Copyright: (C) Peter Ivanov, 2007
* Licence: GPL
*/
/**
* \file main.c
* \brief Main loop
* \author Peter Ivanov
*/
#include <msp430xG461x.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include "system.h"
#include "mma.h"
#include "lcd.h"
#include "font.h"
#include "ball.h"
#include "bits.h"
#include "periphery.h"
#include "at_flash.h"
#include "irda.h"
#include "tff.h"
#include "diskio.h"
#include "time.h"
#include "common.h"
#include "menu.h"
#include "mmc.h"
#include "nrf24l01.h"
// File handling variables
FATFS fatfs;
bool_t fatfsOk = FALSE;
/**
* User Provided Timer Function for FatFs module
* This is a real time clock service to be called from
* FatFs module. Any valid time must be returned even if
* the system does not support a real time clock.
*/
DWORD get_fattime ()
{
DWORD tmr;
TIME_date_t date;
TIME_getDate (&date);
tmr = (((DWORD) date.year - 1980) << 25)
| ((DWORD) date.month << 21)
| ((DWORD) date.day << 16)
| (WORD) (date.hour << 11)
| (WORD) (date.min << 5)
| (WORD) (date.sec >> 1);
return tmr;
}
#define ERROR_STR_NUMBER 13
/**
* Converts error number to string.
*
* @param fres File operation result.
* @return Error string.
*/
const char *FF_getErrorStr (FRESULT fres)
{
static const char *errorStr[ERROR_STR_NUMBER] =
{
"FR_OK", /* 0 */
"FR_NOT_READY", /* 1 */
"FR_NO_FILE", /* 2 */
"FR_NO_PATH", /* 3 */
"FR_INVALID_NAME", /* 4 */
"FR_INVALID_DRIVE", /* 5 */
"FR_DENIED", /* 6 */
"FR_EXIST", /* 7 */
"FR_RW_ERROR", /* 8 */
"FR_WRITE_PROTECTED", /* 9 */
"FR_NOT_ENABLED", /* 10 */
"FR_NO_FILESYSTEM", /* 11 */
"FR_INVALID_OBJECT" /* 12 */
};
static const char unknownErrorStr[] = "Unknown error";
if (fres >= 0 && fres <= ERROR_STR_NUMBER)
{
return errorStr[fres];
}
else
{
return unknownErrorStr;
}
}
#if 0
uint32_t acc_dirs = 0;
uint32_t acc_files = 0;
FRESULT scan_files (char* path, size_t size)
{
DIR dir;
FRESULT res;
BYTE i;
FILINFO finfo;
LCD_printf ("p:%s\n", path);
if ((res = f_opendir (&dir, path)) == FR_OK)
{
i = strlen (path);
while (((res = f_readdir (&dir, &finfo)) == FR_OK) && finfo.fname[0])
{
if (finfo.fattrib & AM_DIR)
{
LCD_printf ("%s/\n", finfo.fname);
acc_dirs++;
*(path + i) = '/';
strncpy (path + i + 1, &finfo.fname[0], size);
res = scan_files (path, size);
*(path + i) = '\0';
if (res != FR_OK)
{
LCD_printf ("Err1: %s\n", FF_getErrorStr (res));
break;
}
}
else
{
LCD_printf ("%s\n", finfo.fname);
acc_files++;
//acc_size += finfo.fsize;
}
}
if (res != FR_OK)
{
LCD_printf ("Err2: %s\n", FF_getErrorStr (res));
}
}
else
{
LCD_printf ("Err3: %s\n", FF_getErrorStr (res));
}
return res;
}
#endif
/**
* Menu point which initializes FAT filesystem (mount).
*/
void FF_init ()
{
uint8_t res, max_tries;
FRESULT fres;
// Initialize FAT filesystem on MMC/SD card
max_tries = 5;
do
{
res = disk_initialize (0);
mdelay (100); // wait 100 ms
} while ((res & STA_NOINIT) && max_tries-- > 0);
if (!(res & STA_NOINIT) && max_tries)
{
// FAT filesystem is OK
LCD_printf ("disk_init OK\n");
// Mounting filesystem
if ((fres = f_mount(0, &fatfs)) == FR_OK)
{
LCD_printf ("mount OK\n");
fatfsOk = TRUE;
}
else
{
LCD_printf ("mount ERR:\n%s\n", FF_getErrorStr (fres));
}
}
else
{
LCD_printf ("disk_init ERR:\n0x%X\n", res);
}
mdelay (500);
}
/**
* Menu point which disconnects FAT filesystem (unmount).
*/
void FF_done ()
{
uint8_t res, max_tries;
FRESULT fres;
// Unmounting filesystem
if ((fres = f_mount(0, NULL)) == FR_OK)
{
LCD_printf ("umount OK\n");
fatfsOk = FALSE;
}
else
{
LCD_printf ("umount ERR:\n%s\n", FF_getErrorStr (fres));
}
mdelay (500);
}
/**
* Menu point which shows status of filesystem and SD card.
*/
void FF_showStatus ()
{
KBD_buttonPressed_t buttonPressed;
LCD_printf ("Card pres.: %s\n", MMC_cardPresent () ? "Y" : "N");
LCD_printf ("Write prot.: %s\n", MMC_cardWriteProtected () ? "Y" : "N");
LCD_printf ("FatFs: %s\n", fatfsOk ? "OK" : "Err");
LCD_printf ("\nPress OK/Quit");
do
{
buttonPressed = KBD_getButtonPressed ();
mdelay (20);
} while (!(buttonPressed & KBD_QUIT) && !(buttonPressed & KBD_OK));
}
menuPoint_t rootMenu[];
/**
* Menu point which displays the filenames of root directory.
*/
void FF_listRootDir ()
{
DIR dir;
FILINFO finfo;
FRESULT fres;
const uint16_t maxFileNumber = 128;
uint16_t fileNumber = 0;
menuPoint_t fileList[maxFileNumber];
KBD_buttonPressed_t buttonPressed;
uint8_t menuTitleLength = 0;
//char path[512];
#if 0
//strncpy (path, "/", sizeof (path));
path[0] = 0;
scan_files (path, sizeof (path));
LCD_printf ("%i files\n", acc_files);
LCD_printf ("%i dirs\n", acc_dirs);
#endif
#if 0
LCD_clear ();
if ((fres = f_opendir (&dir, "/")) == FR_OK)
{
while (((fres = f_readdir (&dir, &finfo)) == FR_OK) && finfo.fname[0])
{
if (finfo.fattrib & AM_DIR)
{
LCD_printf ("%s/\n", finfo.fname);
}
else
{
LCD_printf ("%s\n", finfo.fname);
}
}
}
else
{
LCD_printf ("opendir ERR:\n%s\n", FF_getErrorStr (fres));
}
#endif
LCD_clear ();
fileList[0].menuTitle = NULL;
if ((fres = f_opendir (&dir, "/")) == FR_OK)
{
while (((fres = f_readdir (&dir, &finfo)) == FR_OK) && finfo.fname[0] && fileNumber < maxFileNumber)
{
menuTitleLength = strlen (finfo.fname) + 1; // +1 is for "/"
fileList[fileNumber].menuTitle = malloc (menuTitleLength);
if (fileList[fileNumber].menuTitle != NULL)
{
strcpy (fileList[fileNumber].menuTitle, finfo.fname);
if (finfo.fattrib & AM_DIR)
{
strncat (fileList[fileNumber].menuTitle, "/", menuTitleLength);
}
fileList[fileNumber].subMenu = NULL;
fileList[fileNumber].menuHandlerCallback = NULL;
fileNumber++;
}
}
fileList[fileNumber].menuTitle = NULL;
MENU_init (fileList);
do
{
buttonPressed = KBD_getButtonPressed ();
MENU_handler (buttonPressed);
mdelay (20);
} while (!(buttonPressed & KBD_QUIT));
MENU_init (rootMenu);
}
else
{
LCD_printf ("opendir ERR:\n%s\n", FF_getErrorStr (fres));
mdelay (500);
}
}
/**
* Menu point which displays a bitmap file from the SD cards. You should copy the test.rgb to
* your SD card.
*/
void FF_showBitmap ()
{
bool_t ok = FALSE;
FIL F;
FILINFO finfo;
FRESULT fres;
WORD allBytesRead = 0;
WORD bytesRead;
const char* filename = "test.rgb";
uint8_t buf[512];
uint16_t i;
KBD_buttonPressed_t buttonPressed;
if (fatfsOk)
{
fres = f_open (&F, filename, FA_READ);
if (fres == FR_OK)
{
LCD_write130x130bmpStart ();
do
{
fres = f_read (&F, buf, sizeof (buf), &bytesRead);
allBytesRead += bytesRead;
if (fres == FR_OK)
{
for (i = 0; i < bytesRead; i++)
{
LCD_write130x130bmpData8 (&(buf[i]));
}
}
else
{
LCD_write130x130bmpEnd ();
LCD_clear ();
LCD_printf ("read ERR:\n%s\n", FF_getErrorStr (fres));
}
} while (fres == FR_OK && bytesRead);
f_close (&F);
LCD_write130x130bmpEnd ();
//ok = TRUE;
}
else
{
LCD_printf ("open ERR:\n%s\n", FF_getErrorStr (fres));
}
}
else
{
LCD_printf ("FatFs not OK!\n");
LCD_printf ("Press OK/Quit");
}
//return ok;
do
{
buttonPressed = KBD_getButtonPressed ();
mdelay (20);
} while (!(buttonPressed & KBD_QUIT) && !(buttonPressed & KBD_OK));
}
/**
* Menu point to test file creating and reading on SD cards. Creates a text file
* (fftest.txt), writes some text to it and reads back, then compares the two
* buffers. The result will be displayed.
*/
void FF_fileTest ()
{
FIL F;
FRESULT fres;
WORD bytesWritten;
WORD bytesRead;
const char filename[] = "fftest.txt";
const char buf1[] = "Hello world!\nIf you can read this text, the filesystem module is working!\n";
char buf2[80];
KBD_buttonPressed_t buttonPressed;
if (fatfsOk)
{
LCD_printf ("Writing... ");
fres = f_open (&F, filename, FA_READ | FA_WRITE | FA_CREATE_ALWAYS);
if (fres == FR_OK)
{
fres = f_write (&F, buf1, sizeof (buf1) - 1, &bytesWritten);
if (fres != FR_OK)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -