diff --git a/firmware/applications/default.c b/firmware/applications/default.c index 022b896..4b25ee3 100644 --- a/firmware/applications/default.c +++ b/firmware/applications/default.c @@ -9,91 +9,6 @@ /**************************************************************************/ -FATFS FatFs[_VOLUMES]; /* File system object for logical drive */ - -#define CONFIGLEN 2 -int lcdInitConfig(){ - FIL file; /* File objects */ - BYTE buf[CONFIGLEN]; - UINT readbytes; - int res; - - lcdFill(0); // clear display buffer - res=f_mount(0, &FatFs[0]); - lcdPrint("mount:"); - lcdPrintln(f_get_rc_string(res)); - if(res){ - return 1; - }; - - res=f_open(&file, "r0ket.cfg", FA_OPEN_EXISTING|FA_READ); - lcdPrint("open:"); - lcdPrintln(f_get_rc_string(res)); - if(res){ - if(res==FR_NO_FILESYSTEM) - return 1; - - lcdPrintln("new r0ket.cfg..."); - - res=f_open(&file, "r0ket.cfg", FA_OPEN_ALWAYS|FA_WRITE); - lcdPrint("create:"); - lcdPrintln(f_get_rc_string(res)); - if(res){ - return 1; - }; - - buf[0]='0'; - buf[1]='0'; - res = f_write(&file, buf, 2, &readbytes); - lcdPrint("write:"); - lcdPrintln(f_get_rc_string(res)); - if(res){ - return 1; - }; - - lcdPrint("wrote:"); - lcdPrintInt(readbytes); - lcdPrintln("b"); - - res=f_close(&file); - lcdPrint("close:"); - lcdPrintln(f_get_rc_string(res)); - if(res){ - return 1; - }; - return 2; // created. Still show screen - }; - - for(int i=0;i + +#include "basic/basic.h" + +#include "lcd/print.h" + +#include "filesystem/ff.h" + +#include + +/**************************************************************************/ + +void readcfg(void) { + readConfig(); +}; + +void savecfg(void){ + saveConfig(); +}; + +void applycfg(void){ + applyConfig(); +}; + +void show(void){ + lcdClear(); + lcdPrint("time:"); lcdPrintInt(globalconfig.time); lcdNl(); + lcdPrint("btrig:"); lcdPrintInt(globalconfig.backlighttrigger); lcdNl(); + lcdPrint("bval:"); lcdPrintInt(globalconfig.backlightvalue); lcdNl(); + lcdPrint("lcd:"); lcdPrintInt(globalconfig.lcdstate); lcdNl(); + lcdPrint("priv:"); lcdPrintInt(globalconfig.privacy); lcdNl(); + lcdRefresh(); +}; + +void lcdmirror(void){ + globalconfig.lcdstate^=2; +}; diff --git a/firmware/basic/Makefile b/firmware/basic/Makefile index cc372be..e831d6d 100644 --- a/firmware/basic/Makefile +++ b/firmware/basic/Makefile @@ -18,6 +18,7 @@ OBJS += ecc.o OBJS += byteorder.o OBJS += random.o OBJS += idle.o +OBJS += config.o LIBNAME=basic diff --git a/firmware/basic/basic.h b/firmware/basic/basic.h index be165b4..b311f81 100644 --- a/firmware/basic/basic.h +++ b/firmware/basic/basic.h @@ -182,7 +182,24 @@ void handleMenu(const struct MENU *the_menu); #include "basic/idle.h" +// config.c + +struct config_t { + time_t time; + uint16_t backlighttrigger; + char backlightvalue; + char lcdstate; + char privacy; +} __attribute__((__packed__)); + +typedef struct config_t CONFIG; + +extern CONFIG globalconfig; + +int readConfig(void); +int saveConfig(void); +int applyConfig(void); + #define SYSTICKSPEED 10 #endif - diff --git a/firmware/basic/config.c b/firmware/basic/config.c new file mode 100644 index 0000000..0a9284c --- /dev/null +++ b/firmware/basic/config.c @@ -0,0 +1,85 @@ +#include +#include "basic/basic.h" + +#include "lcd/lcd.h" +#include "lcd/fonts/smallfonts.h" +#include "lcd/print.h" +#include "filesystem/ff.h" +#include "basic/random.h" + +CONFIG globalconfig = { 0,310,50,0,0}; + +#define CONFFILE "r0ket.cfg" + +/**************************************************************************/ + +int saveConfig(void){ + FIL file; /* File object */ + UINT writebytes; + int res; + + res=f_open(&file, CONFFILE, FA_OPEN_ALWAYS|FA_WRITE); + lcdPrint("create:"); + lcdPrintln(f_get_rc_string(res)); + if(res){ + return 1; + }; + + res = f_write(&file, &globalconfig, sizeof(CONFIG), &writebytes); + lcdPrint("write:"); + lcdPrintln(f_get_rc_string(res)); + if(res){ + return 1; + }; + + lcdPrint("wrote:"); + lcdPrintInt(writebytes); + lcdPrintln("b"); + + res=f_close(&file); + lcdPrint("close:"); + lcdPrintln(f_get_rc_string(res)); + if(res){ + return 1; + }; + return 0; +}; + +int readConfig(void){ + FIL file; /* File object */ + UINT readbytes; + int res; + + lcdFill(0); // clear display buffer + + res=f_open(&file, CONFFILE, FA_OPEN_EXISTING|FA_READ); + lcdPrint("open:"); + lcdPrintln(f_get_rc_string(res)); + if(res){ + return 1; + }; + + res = f_read(&file, &globalconfig, sizeof(CONFIG), &readbytes); + lcdPrint("read:"); + lcdPrintln(f_get_rc_string(res)); + if(res){ + return 1; + }; + + + res=f_close(&file); + lcdPrint("close:"); + lcdPrintln(f_get_rc_string(res)); + if(res){ + return 1; + }; + return 0; +}; + +int applyConfig(){ + if(globalconfig.lcdstate & LCD_INVERTED) + lcdToggleFlag(LCD_INVERTED); + if(globalconfig.lcdstate & LCD_MIRRORX) + lcdToggleFlag(LCD_MIRRORX); + return 0; +}; diff --git a/firmware/filesystem/ff.h b/firmware/filesystem/ff.h index 9a5eb20..07eb8ad 100644 --- a/firmware/filesystem/ff.h +++ b/firmware/filesystem/ff.h @@ -330,6 +330,7 @@ int ff_del_syncobj (_SYNC_t); /* Delete a sync object */ /* Utility functions */ const char* f_get_rc_string (FRESULT rc); +void fsInit(); #ifdef __cplusplus } diff --git a/firmware/filesystem/util.c b/firmware/filesystem/util.c index f183b1a..7e635ad 100644 --- a/firmware/filesystem/util.c +++ b/firmware/filesystem/util.c @@ -1,5 +1,7 @@ #include +FATFS FatFs; /* File system object for logical drive */ + const TCHAR *rcstrings = _T("OK\0DISK_ERR\0INT_ERR\0NOT_READY\0NO_FILE\0NO_PATH\0INVALID_NAME\0") _T("DENIED\0EXIST\0INVALID_OBJECT\0WRITE_PROTECTED\0INVALID_DRIVE\0") @@ -16,3 +18,7 @@ const char* f_get_rc_string (FRESULT rc) { return p; } + +void fsInit(){ + f_mount(0, &FatFs); +}; diff --git a/firmware/main.c b/firmware/main.c index 94c5a13..bcef02d 100644 --- a/firmware/main.c +++ b/firmware/main.c @@ -22,6 +22,8 @@ int main(void) { // initialise basic badge functions rbInit(); + + fsInit(); lcdInit(); // display