aboutsummaryrefslogtreecommitdiffstats
path: root/demos
diff options
context:
space:
mode:
authorinmarket <inmarket@ugfx.io>2017-06-24 16:35:50 +1000
committerinmarket <inmarket@ugfx.io>2017-06-24 16:35:50 +1000
commitd6c31f404bf2d6b2520b42717072ad2d22a7ff87 (patch)
tree1529cb1876076580ea5d9c9eb53836c90a7a1e09 /demos
parent8561671cb8c8c81cca6407d47437a7238b111ada (diff)
downloaduGFX-d6c31f404bf2d6b2520b42717072ad2d22a7ff87.tar.gz
uGFX-d6c31f404bf2d6b2520b42717072ad2d22a7ff87.tar.bz2
uGFX-d6c31f404bf2d6b2520b42717072ad2d22a7ff87.zip
Add a demo for GFILE
Diffstat (limited to 'demos')
-rw-r--r--demos/modules/gfile/fatfs/demo.mk3
-rw-r--r--demos/modules/gfile/fatfs/gfxconf.h10
-rw-r--r--demos/modules/gfile/fatfs/main.c60
3 files changed, 73 insertions, 0 deletions
diff --git a/demos/modules/gfile/fatfs/demo.mk b/demos/modules/gfile/fatfs/demo.mk
new file mode 100644
index 00000000..6a2a9c52
--- /dev/null
+++ b/demos/modules/gfile/fatfs/demo.mk
@@ -0,0 +1,3 @@
+DEMODIR = $(GFXLIB)/demos/modules/gfile/fatfs
+GFXINC += $(DEMODIR)
+GFXSRC += $(DEMODIR)/main.c
diff --git a/demos/modules/gfile/fatfs/gfxconf.h b/demos/modules/gfile/fatfs/gfxconf.h
new file mode 100644
index 00000000..31738ec6
--- /dev/null
+++ b/demos/modules/gfile/fatfs/gfxconf.h
@@ -0,0 +1,10 @@
+#ifndef _GFXCONF_H
+#define _GFXCONF_H
+
+#define GFX_USE_GFILE TRUE
+
+#define GFILE_NEED_FATFS TRUE
+#define GFILE_NEED_NOAUTOMOUNT TRUE
+#define GFILE_NEED_PRINTG TRUE
+
+#endif /* _GFXCONF_H */ \ No newline at end of file
diff --git a/demos/modules/gfile/fatfs/main.c b/demos/modules/gfile/fatfs/main.c
new file mode 100644
index 00000000..c5b43927
--- /dev/null
+++ b/demos/modules/gfile/fatfs/main.c
@@ -0,0 +1,60 @@
+#include "gfx.h"
+#include <string.h>
+
+/* Function to log messages to a file. */
+void LogInfo(const char* msg) {
+ GFILE* logFile;
+
+ logFile = gfileOpen("info.txt", "a"); // Open the file for append
+ if (logfile) {
+ gfileWrite(logFile, msg, strlen(msg));
+ gfileClose(logFile); // Close the file again
+ }
+}
+
+int main(void) {
+ GFILE* file; // GFILE variable to store file info.
+ const char msg[] = "Hello file!"; // String to write to a file.
+
+ /* Call the µGFX init routine. */
+ gfxInit();
+
+ /* Mount the file system. */
+ if (gfileMount('F', "/"))
+ gfxHalt("Can't mount the FAT file system");
+
+ /* Check if a file exists. */
+ if (gfileExists("file.txt"))
+ LogInfo("[Info]: File exists already!");
+ else
+ LogInfo("[Info]: The file does not exist yet!");
+
+ /* Write a string to the file. */
+ file = gfileOpen("file.txt", "wx");
+ if(!file) {
+ LogInfo("[Error]: Something went wrong opening the file.");
+ gfxHalt("Can't open the file file.txt");;
+ }
+
+ /* A normal write */
+ gfileWrite(file, msg, strlen(msg));
+
+ /* Write the file size in the file using the uGFX equivalent of fprintf(). */
+ fnprintg(file, 30, "The file is currently %dkB", gfileGetSize(file));
+
+ /* Close the file */
+ gfileClose(file);
+
+ /* Rename te file. */
+ gfileRename("file.txt", "renamedFile.txt");
+
+ /* Unmount the file system again */
+ gfileUnmount('F', "/");
+
+ /* This line should not work as the file system is now unmounted */
+ LogInfo("[Info]: Entering enldess while loop.");
+
+ /* The program ends here. */
+ while(1)
+ gfxSleepMilliseconds(200);
+}