Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: demo.cpp
- Revision:
- 26:c6a803706a42
- Parent:
- 25:015631f9e875
- Child:
- 27:dbf79d116497
diff -r 015631f9e875 -r c6a803706a42 demo.cpp
--- a/demo.cpp Tue Sep 27 11:49:34 2016 +0000
+++ b/demo.cpp Wed Sep 28 12:41:32 2016 +0000
@@ -6,13 +6,14 @@
#define LONG_WAIT 1000
// local declarations
-void clearBuf(char *);
+void clearBuf(char*, int);
//**************************************************************************
// Demo program that goes through file system based functions
//**************************************************************************
void PICASO_4DGL :: fileSystemDemo() {
+ short h1;
int i;
cls();
screenMode(landscape); // set orientation to landscape
@@ -23,8 +24,9 @@
if (file_Mount()) puts("\rFAT16 Mount: OK");
else puts("\rFAT16 Mount: FAIL");
char buf[100];
- sprintf(buf, "\n\rFAT16 Error = %i", file_Error());
+ sprintf(buf, "\n\rFAT16 Error = %i\n", file_Error());
puts(buf);
+ /*
sprintf(buf, "\n\rFile count on disc = %i\n\r", file_Count("*.*"));
puts(buf);
sprintf(buf, "\n\rFound %i files maching 1*.*", file_Dir("1*.*"));
@@ -49,6 +51,102 @@
else puts("\n\r3.TXT : DOES NOT EXIST");
if (file_Exists("4.TXT")) puts("\n\r4.TXT : EXISTS");
else puts("\n\r4.TXT : DOES NOT EXIST");
+ wait_ms(LONG_WAIT);
+ cls();
+ */
+
+ h1 = file_Open("file.txt", OPEN_READ);
+ short err = file_Error();
+ if (err == 0) sprintf(buf, "\r\file.txt handle = %i", h1);
+ else sprintf(buf, "\rFile Open error = %i", err);
+ puts(buf);
+ short count = file_Read(31, h1);
+ if (count == 31) sprintf(buf, "\r\nRead %i bytes from %i", count, h1);
+ else sprintf(buf, "\r\nError = %i\n\rMax data count = %i", file_Error(), count);
+ puts(buf);
+ sprintf(buf, "\r\nData: %s", fileReadBuf);
+ puts(buf);
+
+ clearBuf(fileReadBuf, BUFFER_SIZE);
+ short hi = 0, lo = 5;
+ sprintf(buf, "\r\nSetting pointer to %i = %s", lo, file_Seek(h1, hi, lo) ? "true" : "false");
+ puts(buf);
+ file_Read(1, h1);
+ sprintf(buf, "\r\nRead char = %c", fileReadBuf[0]);
+ puts(buf);
+
+ clearBuf(fileReadBuf, BUFFER_SIZE);
+ short recNum = 2;
+ sprintf(buf, "\r\nIndex pointer to %i = %s", lo, file_Index(h1, hi, lo, recNum) ? "true" : "false");
+ puts(buf);
+ file_Read(1, h1);
+ sprintf(buf, "\r\nRead char = %c", fileReadBuf[0]);
+ puts(buf);
+
+ int pointer = file_Tell(h1);
+ if (pointer >= 0) sprintf(buf, "\r\nPointer = %i", pointer);
+ else sprintf(buf, "\r\nError = %i", file_Error());
+ puts(buf);
+
+ sprintf(buf, "\r\nFile %i Close = %s", h1, file_Close(h1) ? "true" : "false");
+ puts(buf);
+
+ h1 = file_Open("newfile.txt", OPEN_READ);
+ err = file_Error();
+ if (err == 0) sprintf(buf, "\r\nnewfile.txt handle = %i", h1);
+ else sprintf(buf, "\rFile Open error = %i", err);
+ puts(buf);
+
+ clearBuf(fileReadBuf, BUFFER_SIZE);
+ count = file_Read(18, h1);
+ if (count == 18) sprintf(buf, "\r\nRead %i bytes from %i", count, h1);
+ else sprintf(buf, "\r\nError = %i\n\rMax data count = %i", file_Error(), count);
+ puts(buf);
+ sprintf(buf, "\r\nData: %s", fileReadBuf);
+ puts(buf);
+
+ sprintf(buf, "\r\nFile %i size = %i", h1, file_Size(h1));
+ puts(buf);
+
+ sprintf(buf, "\r\nFile %i Close = %s", h1, file_Close(h1) ? "true" : "false");
+ puts(buf);
+
+ short h2 = file_Open("scr.png", OPEN_APPEND);
+ file_ScreenCapture(10, 10, 200, 100, h2);
+ file_Close(h2);
+
+ wait_ms(LONG_WAIT);
+ cls();
+
+ h2 = file_Open("pic.bmp", OPEN_READ);
+ file_Image(5, 5, h2);
+
+ /*
+ h1 = file_Open("newfile.txt", OPEN_APPEND); // try appending to file
+ err = file_Error();
+ if (err == 0) sprintf(buf, "\r\nnewfile.txt handle = %i", h1);
+ else sprintf(buf, "\rFile Open error = %i", err);
+ puts(buf);
+
+ count = file_Write(13, " My Precious!", h1); // add data
+ file_Close(h1); // close file
+ h1 = file_Open("newfile.txt", OPEN_READ); // open in read mode again
+ err = file_Error();
+ if (err != 0) {
+ sprintf(buf, "\rFile Open error = %i", err);
+ puts(buf);
+ }
+
+ clearBuf(fileReadBuf, BUFFER_SIZE); // clear buffer
+ count = file_Read(18, h1); // read bytes
+ if (count != 18) {
+ sprintf(buf, "\r\nError = %i\n\rMax data count = %i", file_Error(), count);
+ puts(buf);
+ }
+ sprintf(buf, "\r\nData: %s", fileReadBuf);
+ puts(buf);
+ */
+
}
//**************************************************************************
@@ -745,8 +843,9 @@
}
-void clearBuf(char *str) {
+void clearBuf(char *str, int size) {
int i;
- for (i = 0; i < strlen(str); i++)
+ for (i = 0; i < size; i++) {
str[i] = 0;
+ }
}
\ No newline at end of file