tibbo_line/config_work.tc

274 lines
6.8 KiB
Plaintext

#include "global.th"
#include "config_work.th"
#include "common.th"
Configuration config;
string get_parameter(string param){
romfile.open("sdf.txt");
int str_start = 1;
unsigned long pos,pos2;
pos = romfile.find(str_start,param+"=",1);
pos +=len(param+"=");
romfile.pointer32=pos;
pos2 = romfile.find(pos,chr(13),1);
return romfile.getdata(pos2-pos);
}
string get_parameter_from_file(string param,string filename){
fd.filenum = 2;
fd.open(filename);
fd.setpointer(1);
unsigned long pos,pos2;
pos=fd.find(1,param+"=",1,FORWARD,1,PL_FD_FIND_EQUAL);
if (pos == 0){
fd.close();
return "";
}
fd.setpointer(pos+len(param+"="));
pos2 = fd.find(pos+len(param),chr(13),1,FORWARD,1,PL_FD_FIND_EQUAL)-len(param+"=");
string r = fd.getdata(pos2-pos);
//d("|<"+param+"='"+r+"'"+chr(13));
fd.close();
return r;
}
bool format_flash(int flash_size, int file_count){
return (fd.format(config.FLASH_SIZE,config.FLASH_FILES) == PL_FD_STATUS_OK);
}
void config_load(){
d("load config");
if (config.STORE_LOCATON == FLASH){
fd.filenum=1;
fd.open("config");
string s =fd.getdata(200);
while (len(s)>0){
d(s);
s=fd.getdata(200);
}
//d("config file size:" +str(fd.filesize));
fd.close();
config.IP= get_parameter_from_file("ip","config");
//config.STORE_LOCATON = get_parameter_from_file("STORE_LOCATON","config");
//config.FLASH_SIZE = get_parameter_from_file("FLASH_SIZE","config");
//config.FLASH_FILES = get_parameter_from_file("FLASH_FILES","config");
config.STORE_BRAKER = get_parameter_from_file("sb","config");
config.STORE_ENCODER = get_parameter_from_file("se","config");
if (config.STORE_ENCODER){
config.EncoderCfg.encoderPort = get_parameter_from_file("en.p","config");
config.EncoderCfg.encoderMode = get_parameter_from_file("en.m","config");
config.EncoderCfg.encoderInterval= get_parameter_from_file("en.int","config");
config.EncoderCfg.workedPorts = get_parameter_from_file("en.wp","config");
config.EncoderCfg.windowWidth = get_parameter_from_file("en.wn","config");
config.EncoderCfg.reinitInterval = get_parameter_from_file("en.ri","config");
}
//if (config.STORE_BRAKER){
for (int i = 0; i<BRAKER_COUNT;i++){
BrakerConfig c = config.BRAKERS[i];
if (len(get_parameter_from_file("br["+str(i)+"].e","config"))>0){
c.Enable = get_parameter_from_file("br["+str(i)+"].e","config");
c.SensPort= get_parameter_from_file("br["+str(i)+"].s","config");
c.DropPort=get_parameter_from_file("br["+str(i)+"].d","config");
c.Relay=get_parameter_from_file("br["+str(i)+"].r","config");
c.Timeout=get_parameter_from_file("br["+str(i)+"].t","config");
}
}
//}
char arr[1024];
get_config(arr);
}
}
char *get_config( char *array){
string sb="";
string r = chr(13)+chr(10);
sb += "ip="+config.IP+r;
string t[] = {"none","eeprom","flash"};
sb += "store="+ t[config.STORE_LOCATON]+r;
if (config.STORE_LOCATON == FLASH){
sb += "fs=" + str(config.FLASH_SIZE)+r;
sb += "ff=" + str(config.FLASH_FILES)+r;
}
//d("|>>>"+sb);
memcpy(array,sb,len(sb));
array+=len(sb);
sb="";
if (config.EncoderCfg.encoderPort>0){
config.STORE_ENCODER = true;
sb += "en.p="+str(config.EncoderCfg.encoderPort)+r;
sb += "en.m="+str(config.EncoderCfg.encoderMode)+r;
sb += "en.int="+str(config.EncoderCfg.encoderInterval)+r;
sb += "en.wp="+config.EncoderCfg.workedPorts+r;
sb += "en.wn="+str(config.EncoderCfg.windowWidth)+r;
sb += "en.ri="+str(config.EncoderCfg.reinitInterval)+r;
}
sb += "se=" + str(config.STORE_ENCODER)+r;
d(sb);
memcpy(array,sb,len(sb));
array+=len(sb);
sb="";
for (int i = 0; i<BRAKER_COUNT;i++){
string b = "";
if (config.BRAKERS[i].Enable){
config.STORE_BRAKER = true;
BrakerConfig c = config.BRAKERS[i];
b+="br["+str(i)+"].e=" +str(c.Enable)+r;
b+="br["+str(i)+"].s=" +str(c.SensPort)+r;
b+="br["+str(i)+"].d=" +str(c.DropPort)+r;
b+="br["+str(i)+"].r=" +str(c.Relay)+r;
b+="br["+str(i)+"].t=" +str(c.Timeout)+r;
//d("|>>>"+b);
}
sb+=b;
}
sb += "sb=" + str(config.STORE_BRAKER)+r;
d(sb);
memcpy(array,sb,len(sb));
array+=len(sb);
return array;
}
void config_write(){
if (config.STORE_LOCATON == NONE){
fd.filenum = 1;
fd.delete("config");
}
if (config.STORE_LOCATON == FLASH){
fd.mount();
fd.filenum = 1;
//d("flash size = "+ stri(fd.capacity));
if (fd.open("config") == PL_FD_STATUS_NOT_FOUND)
fd.create("config");
if (fd.open("config") == PL_FD_STATUS_OK){
//d("before write:file size:" +str(fd.filesize));
fd.setpointer(0);
char prep[1000];
char *ptr = get_config(prep);
char *out = prep;
int length = ptr - out;
while (out < ptr){
string s;
char *ss = s;
if (out+100 <ptr){
memcpy(ss,out,100);
*(ss-2)=100;
}else{
memcpy(ss,out,ptr-out);
*(ss-2)=ptr-out;
}
fd.setdata(s);
fd.flush();
out+=100;
}
//d("after write:file size:" +str(fd.filesize));
fd.close();
}else{
d("Error open file 'config' from flash");
}
}
}
#define DEF_IP "192.168.123.19"
//#define DEF_IP "10.0.50.232"
void config_init(){
config_create();
//unsigned char x=stor.base;
//stor.base = 5;
string in =stor.getdata(5,1);
unsigned char init= in[0];
// stor.base = x;
switch (init){
case 3:
config.STORE_LOCATON =FLASH;
config.FLASH_SIZE = 1024;
config.FLASH_FILES = 10;
break;
case 6:
config.STORE_LOCATON = EEPROM;
break;
default:
config.STORE_LOCATON = NONE;
break;
}
config.IP = ddstr(stor.getdata(0,4));
if (config.IP != validate_id(config.IP))
config.IP= DEF_IP;
// config.BRAKER_COUNT = 3;
// config.STORE_LOCATON = FLASH;
// config.FLASH_SIZE = 1024;
// config.FLASH_FILES = 10;
// config.STORE_BRAKER = false;
if (config.STORE_LOCATON == FLASH){
if (fd.mount() !=PL_FD_STATUS_OK){
d("formating");
if (format_flash(config.FLASH_SIZE,config.FLASH_FILES))
{
d("format complete");
fd.mount();
}else{
d("format error");
}
}
fd.filenum = 1;
int f = fd.open("config") ;
int s = fd.filesize;
d("f="+stri(f)+" ,size="+stri(s));
if (f>0 || s==0){
d("initial write config");
fd.create("config");
config_write();
}
config_load();
}
}
void config_save(){
config_write();
}
void config_create(){
config.IP= "0.0.0.0";
config.STORE_LOCATON = NONE;
config.FLASH_SIZE = 0;
config.FLASH_FILES = 0;
config.STORE_BRAKER = false;
BrakerConfig c;
c.Enable = false;
c.SensPort = 0;
c.DropPort = 0;
c.Relay = 0;
c.Timeout = 0;
config.BRAKERS[0] = c;
config.BRAKERS[1] = c;
config.BRAKERS[2] = c;
config.EncoderCfg.encoderPort = 0;
config.EncoderCfg.encoderMode = 0;
config.EncoderCfg.encoderInterval = 0;
config.EncoderCfg.windowWidth= 40;
config.EncoderCfg.reinitInterval = 3;
config.EncoderCfg.workedPorts = "";
config.debug.EncoderDebug=0;
config.debug.RelayShow=true;
}