First draft of the new global config system

Sorry if your display is mirrored again %)
This commit is contained in:
Stefan `Sec` Zehl 2011-07-27 00:57:36 +02:00
parent 833a6387fb
commit 535b635b0e
8 changed files with 152 additions and 94 deletions

View File

@ -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<CONFIGLEN;i++)
buf[i]=0;
res = f_read(&file, buf, 2, &readbytes);
lcdPrint("read:");
lcdPrintln(f_get_rc_string(res));
if(res){
return 1;
};
lcdPrint("r: ");
lcdPrintCharHex(buf[0]);
lcdPrintCharHex(buf[1]);
lcdNl();
if(buf[0] == '1')
lcdToggleFlag(LCD_INVERTED);
if(buf[1] == '1')
lcdToggleFlag(LCD_MIRRORX);
res=f_close(&file);
lcdPrint("close:");
lcdPrintln(f_get_rc_string(res));
if(res){
return 1;
};
return 0;
};
void main_default(void) {
systickInit(SYSTICKSPEED);
@ -101,13 +16,8 @@ void main_default(void) {
ISPandReset();
};
font=&Font_7x8;
if(lcdInitConfig()){
lcdDisplay();
getInputWait();
}else{
lcdDisplay();
};
readConfig();
applyConfig();
randomInit();
return;
@ -151,4 +61,3 @@ void tick_default(void) {
return;
};

View File

@ -0,0 +1,37 @@
#include <sysinit.h>
#include "basic/basic.h"
#include "lcd/print.h"
#include "filesystem/ff.h"
#include <string.h>
/**************************************************************************/
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;
};

View File

@ -18,6 +18,7 @@ OBJS += ecc.o
OBJS += byteorder.o
OBJS += random.o
OBJS += idle.o
OBJS += config.o
LIBNAME=basic

View File

@ -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

85
firmware/basic/config.c Normal file
View File

@ -0,0 +1,85 @@
#include <sysinit.h>
#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;
};

View File

@ -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
}

View File

@ -1,5 +1,7 @@
#include <ff.h>
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);
};

View File

@ -22,6 +22,8 @@ int main(void) {
// initialise basic badge functions
rbInit();
fsInit();
lcdInit(); // display