📄 main.c
字号:
{
LCD_printf ("write ERR:\n%s\n", FF_getErrorStr (fres));
}
f_sync (&F);
f_close (&F);
LCD_printf ("OK\n");
}
else
{
LCD_printf ("open ERR:\n%s\n", FF_getErrorStr (fres));
}
LCD_printf ("Reading... ");
fres = f_open (&F, filename, FA_READ);
if (fres == FR_OK)
{
fres = f_read (&F, buf2, sizeof (buf1) - 1, &bytesRead);
if (fres != FR_OK)
{
LCD_printf ("read ERR:\n%s\n", FF_getErrorStr (fres));
}
f_close (&F);
LCD_printf ("OK\n");
}
else
{
LCD_printf ("open ERR:\n%s\n", FF_getErrorStr (fres));
}
LCD_printf ("Comparing... ");
if (memcmp (buf1, buf2, sizeof (buf1) - 1) == 0)
{
LCD_printf ("OK\n");
}
else
{
LCD_printf ("Failed!\n");
}
}
else
{
LCD_printf ("FatFs not OK!\n");
}
LCD_printf ("\nPress OK/Quit");
do
{
buttonPressed = KBD_getButtonPressed ();
mdelay (20);
} while (!(buttonPressed & KBD_QUIT) && !(buttonPressed & KBD_OK));
}
//#define MMA
#define POS_COL_START 0x3d
#define POS_COL_END 0x44
#define POS_ROW_START 0x00
#define POS_ROW_END 0x0f
#define ROW_EXCEED_LEFT 180 // stupid, but we work with unsigned variable
#define ROW_EXCEED_RIGHT 110
#define COL_EXCEED_LEFT 180 // stupid, but we work with unsigned variable
#define COL_EXCEED_RIGHT 110
#define KFACTOR 100 // for sensibility of ball :-)
/**
* Menu point to test MMA. Virtual ball test.
*/
void mmaBallTest ()
{
uint16_t i;
// base cordinate of row and column
int row_base;
int col_base;
// variables
uint8_t x = LCD_WIDTH / 2 - BALL_WIDTH / 2,
y = LCD_HEIGHT / 2 - BALL_HEIGHT / 2;
uint8_t ox = 0, oy = 0; // coordinates in previous cycle
KBD_buttonPressed_t buttonPressed;
LCD_printf ("calibrate MMA:");
// calibrate
for (i = 0; i < 16; i++)
{
MMA_getCoordinates();
}
row_base = MMA_coordinates[1];
col_base = MMA_coordinates[0];
LCD_printf ("OK");
mdelay (500);
LCD_clear ();
do
{
// mma and ball ------------------------------------------------------------
MMA_getCoordinates();
// with calibrate when start
y = y + (MMA_coordinates[1] - row_base)/KFACTOR;
x = x + (MMA_coordinates[0] - col_base)/KFACTOR;
// check to exceed LCD area
if(y>ROW_EXCEED_LEFT) y=1;
if(y>ROW_EXCEED_RIGHT) y=110;
if(x>COL_EXCEED_LEFT) x=1;
if(x>COL_EXCEED_RIGHT) x=110;
if (oy != y || ox != x)
{
//ClearBall (ox, oy);
LCD_writeBall (x, y);
oy = y;
ox = x;
}
// -------------------------------------------------------------------------
// press center joystick button --------------------------------------------
if (P2IN & BIT6)
{
// Clear LCD
LCD_clear ();
// calibrate
MMA_getCoordinates();
// Set new position
row_base = MMA_coordinates[1];
col_base = MMA_coordinates[0];
}
buttonPressed = KBD_getButtonPressed ();
} while (!(buttonPressed & KBD_QUIT));
}
/**
* Menu point which displays raw XYZ values of MMA.
*/
void mmaRawValues ()
{
KBD_buttonPressed_t buttonPressed;
do
{
MMA_getCoordinates();
LCD_setXY (0, 0);
LCD_printf ("x: %04i\n", MMA_coordinates[MMA_COORD_X]);
LCD_printf ("y: %04i\n", MMA_coordinates[MMA_COORD_Y]);
LCD_printf ("z: %04i", MMA_coordinates[MMA_COORD_Z]);
buttonPressed = KBD_getButtonPressed ();
} while (!(buttonPressed & KBD_QUIT));
}
/**
* Helper function to the menu.
* Example:
<pre>
year = setValue ("Year: ", date.year, 2000, 2100, 1, 5);
</pre>
*
* @param title Text to display. Example: "Number".
* @param value The default value.
* @param min Minimum value.
* @param max Maximum value.
* @param smallStep Increases/decreases the value if you press the joystick up
* and down.
* @param bigStep Increases/decreases the value if you press the joystick left
* and right.
*/
uint32_t setValue (const char* title, uint32_t value, uint32_t min, uint32_t max, uint16_t smallStep, uint16_t bigStep)
{
KBD_buttonPressed_t buttonPressed;
uint32_t val = value;
do
{
LCD_setXY (0, 0);
LCD_printf ("%s %5li", title, val);
buttonPressed = KBD_getButtonPressed ();
if (buttonPressed & KBD_UP)
{
if ((val + smallStep) <= max)
{
val += smallStep;
}
else
{
val = max;
}
}
if (buttonPressed & KBD_DOWN)
{
if ((val - smallStep) >= min)
{
val -= smallStep;
}
else
{
val = min;
}
}
if (buttonPressed & KBD_RIGHT)
{
if ((val + bigStep) <= max)
{
val += bigStep;
}
else
{
val = max;
}
}
if (buttonPressed & KBD_LEFT)
{
if ((val - bigStep) >= min)
{
val -= bigStep;
}
else
{
val = min;
}
}
mdelay (20);
} while (!(buttonPressed & KBD_QUIT) && !(buttonPressed & KBD_OK));
if (buttonPressed & KBD_QUIT)
{
val = value;
}
return val;
}
/**
* Menu point to set date and time.
*/
void setDateTime ()
{
TIME_date_t date;
TIME_getDate (&date);
date.year = setValue ("Year: ", date.year, 2000, 2100, 1, 5);
date.month = setValue ("Month:", date.month, 1, 12, 1, 5);
// TODO: 31 nem mindig jo!!!
date.day = setValue ("Day: ", date.day, 1, 31, 1, 10);
date.hour = setValue ("Hour: ", date.hour, 0, 24, 1, 6);
date.min = setValue ("Min: ", date.min, 0, 59, 1, 10);
date.sec = 0;
TIME_setDate (&date);
}
/**
* Menu point to show date and time.
*/
void showDateTime ()
{
TIME_date_t date;
uint8_t oldSec = 0xFF;
KBD_buttonPressed_t buttonPressed;
LCD_setColor (LCD_COLOR_BLACK, LCD_COLOR_WHITE);
do
{
TIME_getDate (&date);
if (date.sec != oldSec)
{
// show current date
LCD_setXY (0, 0);
LCD_printf ("Time: %02i.%02i.%02i\nDate: %04i-%02i-%02i",
date.hour, date.min, date.sec,
date.year, date.month, date.day);
oldSec = date.sec;
}
buttonPressed = KBD_getButtonPressed ();
mdelay (20);
} while (!(buttonPressed & KBD_QUIT) && !(buttonPressed & KBD_OK));
}
/**
* Append text to a file.
*
* @param filename Filename to open.
* @param buf Buffer to append.
* @param size Size of the buffer.
*/
bool_t appendToFile (const char* filename, const char* buf, size_t size)
{
bool_t ok = FALSE;
FIL F;
FRESULT fres;
WORD bytesWritten;
if (fatfsOk)
{
fres = f_open (&F, filename, FA_READ | FA_WRITE | FA_OPEN_ALWAYS);
if (fres == FR_OK)
{
// seek to the end of file
fres = f_lseek (&F, F.fsize);
if (fres != FR_OK)
{
LCD_printf ("seek ERR:\n%s\n", FF_getErrorStr (fres));
}
fres = f_write (&F, buf, size, &bytesWritten);
if (fres != FR_OK)
{
LCD_printf ("write ERR:\n%s\n", FF_getErrorStr (fres));
}
f_sync (&F);
f_close (&F);
ok = TRUE;
}
else
{
LCD_printf ("open ERR:\n%s\n", FF_getErrorStr (fres));
}
}
return ok;
}
/**
* Menu point which displays the actual voltage and saves the result to a file
* if an SD cards is inserted.
*/
void showVcc ()
{
//KBD_buttonPressed_t buttonPressed;
float VCC = 0;
char buf[64];
TIME_date_t date;
const char* filename = "vresult.txt";
// get current date
TIME_getDate (&date);
// measure and calculate power supply's voltage
VCC = ADC12_getVCC ();
snprintf (buf, sizeof (buf),
"Potential\n"
"---------\n"
"%04i-%02i-%02i\n"
"%02i.%02i.%02i\n"
"VCC: %i.%02i V\n"
"\n"
"\n",
date.year, date.month, date.day, date.hour, date.min, date.sec,
(int) VCC, (int) ((VCC - floor (VCC)) * 100.0));
LCD_printf (buf);
if (fatfsOk)
{
LCD_printf ("Save... ");
appendToFile (filename, buf, strlen (buf));
LCD_printf ("OK");
}
delay (2);
}
/**
* Menu point which displays the actual temperature and saves the result to a file
* if an SD cards is inserted.
*/
void showTemp ()
{
float tempCelsius = 0;
char buf[64];
TIME_date_t date;
const char* filename = "tresult.txt";
// get current date
TIME_getDate (&date);
// measure and calculate temperature
tempCelsius = ADC12_getTempCelsius ();
snprintf (buf, sizeof (buf),
"Temperature\n"
"-----------\n"
"%04i-%02i-%02i\n"
"%02i.%02i.%02i\n"
"T: %i.%i \xB0""C\n"
"\n"
"\n",
date.year, date.month, date.day, date.hour, date.min, date.sec,
(int) tempCelsius, (int) ((tempCelsius - floor (tempCelsius)) * 10.0));
LCD_printf (buf);
if (fatfsOk)
{
LCD_printf ("Save... ");
if (appendToFile (filename, buf, strlen (buf)))
{
LCD_printf ("OK");
}
}
delay (2);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -