You will probably hate me, but commit new, global config framework

hopefully it will be stable now. Sorry for breaking your stuff
This commit is contained in:
Stefan `Sec` Zehl 2011-07-31 18:15:44 +02:00
parent 2c62859a60
commit cfe6e328e5
8 changed files with 117 additions and 51 deletions

View File

@ -38,10 +38,17 @@ void tick_default(void) {
};
};
if(isNight())
backlightSetBrightness(globalconfig.backlightvalue);
else
backlightSetBrightness(0);
if(ctr>100/SYSTICKSPEED){
if(isNight()){
backlightSetBrightness(GLOBAL(lcdbacklight));
if(GLOBAL(nightinvert))
lcdSetInvert(0);
} else {
backlightSetBrightness(0);
if(GLOBAL(nightinvert))
lcdSetInvert(1);
}
}
if(ctr%(50/SYSTICKSPEED)==0){

View File

@ -187,21 +187,31 @@ void handleMenu(const struct MENU *the_menu);
// 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);
void applyConfig(void);
struct CDESC {
char *name;
char value;
char min;
char max;
};
extern struct CDESC the_config[];
#define GLOBALversion (the_config[0].value)
#define GLOBALprivacy (the_config[1].value)
#define GLOBALnighttrigger (the_config[2].value)
#define GLOBALnightinvert (the_config[3].value)
#define GLOBALlcdbacklight (the_config[4].value)
#define GLOBALlcdmirror (the_config[5].value)
#define GLOBALlcdinvert (the_config[6].value)
#define GLOBALlcdcontrast (the_config[7].value)
#define GLOBAL(x) GLOBAL ## x
#define SYSTICKSPEED 10

View File

@ -1,21 +1,38 @@
#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 CFGVER 23
struct CDESC the_config[]= {
{"version", CFGVER, CFGVER, CFGVER},
{"privacy", 3, 0, 2 },
{"nighttrigger", 310/2, 0, 255},
{"nightinvert", 1, 0, 1 },
{"lcdbacklight", 50, 0, 100},
{"lcdmirror", 0, 0, 1 },
{"lcdinvert", 0, 0, 1 },
{"lcdcontrast", 3, 1, 6 },
{ NULL, 0, 0, 0 },
};
#define CONFFILE "r0ket.cfg"
#define CONF_ITER for(int i=0;the_config[i].name!=NULL;i++)
/**************************************************************************/
void applyConfig(){
lcdSetContrast(GLOBAL(lcdcontrast));
return 0;
};
int saveConfig(void){
FIL file; /* File object */
UINT writebytes;
UINT allwrite=0;
int res;
res=f_open(&file, CONFFILE, FA_OPEN_ALWAYS|FA_WRITE);
@ -25,16 +42,20 @@ int saveConfig(void){
return 1;
};
res = f_write(&file, &globalconfig, sizeof(CONFIG), &writebytes);
CONF_ITER{
res = f_write(&file, &the_config[i].value, sizeof(uint8_t), &writebytes);
allwrite+=writebytes;
if(res){
lcdPrint("write:");
lcdPrintln(f_get_rc_string(res));
return 1;
};
};
lcdPrint("write:");
lcdPrintln(f_get_rc_string(res));
if(res){
return 1;
};
lcdPrint("wrote:");
lcdPrintInt(writebytes);
lcdPrintln("b");
lcdPrint(" (");
lcdPrintInt(allwrite);
lcdPrintln("b)");
res=f_close(&file);
lcdPrint("close:");
@ -48,34 +69,32 @@ int saveConfig(void){
int readConfig(void){
FIL file; /* File object */
UINT readbytes;
UINT allread;
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;
CONF_ITER{
res = f_read(&file, &the_config[i].value, sizeof(uint8_t), &readbytes);
allread+=readbytes;
if(GLOBAL(version) != CFGVER){
GLOBAL(version) =CFGVER;
return 1;
};
if(res || GLOBAL(version) != CFGVER)
return 1;
};
res=f_close(&file);
lcdPrint("close:");
lcdPrintln(f_get_rc_string(res));
if(res){
return 1;
};
applyConfig();
return 0;
};
int applyConfig(){
return 0;
};

View File

@ -7,7 +7,7 @@
uint32_t light=300*HYST;
char _isnight=1;
#define threshold globalconfig.backlighttrigger
#define threshold GLOBAL(nighttrigger)
void LightCheck(void){
int iocon;
@ -22,7 +22,7 @@ void LightCheck(void){
gpioSetDir(RB_LED3, gpioDirection_Input);
IOCON_PIO1_11 = IOCON_PIO1_11_FUNC_AD7|IOCON_PIO1_11_ADMODE_ANALOG;
light-=light/HYST;
light += adcRead(7);
light += (adcRead(7)/2);
gpioSetDir(RB_LED3, iodir);
IOCON_PIO1_11=iocon;

View File

@ -13,7 +13,6 @@
/**************************************************************************/
uint8_t lcdBuffer[RESX*RESY_B];
#define lcd_layout globalconfig.lcdstate
uint32_t intstatus; // Caches USB interrupt state
// (need to disable MSC while displaying)
@ -153,12 +152,12 @@ void lcdDisplay(void) {
uint16_t i,page;
for(page=0; page<RESY_B;page++) {
for(i=0; i<RESX; i++) {
if (lcd_layout & LCD_MIRRORX)
if (GLOBAL(lcdmirror))
byte=lcdBuffer[page*RESX+RESX-1-(i)];
else
byte=lcdBuffer[page*RESX+(i)];
if (lcd_layout & LCD_INVERTED)
if (GLOBAL(lcdinvert))
byte=~byte;
lcdWrite(TYPE_DATA,byte);
@ -169,13 +168,36 @@ void lcdDisplay(void) {
}
inline void lcdInvert(void) {
lcdToggleFlag(LCD_INVERTED);
GLOBAL(lcdinvert)=!GLOBAL(lcdinvert);
}
void lcdToggleFlag(int flag) {
lcd_layout=lcd_layout ^ flag;
}
void lcdSetContrast(int c) {
c+=0x20;
if(c>0x2e) c=0x24;
lcd_select();
lcdWrite(TYPE_CMD,c);
lcd_deselect();
};
void lcdSetInvert(int c) {
if(c>1)
c=1;
if(c<0)
c=1;
c+=0xa6;
lcd_select();
lcdWrite(TYPE_CMD,c);
lcd_deselect();
};
/* deprecated */
void __attribute__((__deprecated__)) lcdToggleFlag(int flag) {
if(flag==LCD_MIRRORX)
GLOBAL(lcdmirror)=!GLOBAL(lcdmirror);
if(flag==LCD_INVERTED)
GLOBAL(lcdinvert)=!GLOBAL(lcdinvert);
}
void lcdShiftH(bool right, bool wrap) {

View File

@ -29,4 +29,5 @@ void lcdSetPixel(char x, char y, bool f);
void lcdSafeSetPixel(char x, char y, bool f);
bool lcdGetPixel(char x, char y);
void lcdShift(int x, int y, bool wrap);
void lcdSetContrast(int c);
#endif

View File

@ -34,6 +34,12 @@ void setExtFont(const char *fname){
font=NULL;
};
int getFontHeight(void){
if(font)
return font->u8Height;
return 8; // XXX: Should be done right.
};
int _getFontData(int type, int offset){
UINT readbytes;

View File

@ -36,6 +36,7 @@ int DoCharX(int sx, int sy, unsigned char num);
int DoShortX(int sx, int sy, uint16_t num);
void setIntFont(const struct FONT_DEF * font);
void setExtFont(const char *file);
int getFontHeight(void);
#define START_FONT 0
#define SEEK_EXTRAS 1