tibbo_line/config_work.tc

199 lines
4.9 KiB
Plaintext

#include "global.th"
#include "config_work.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");
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("STORE_BRAKER","config");
for (int i = 0; i<BRAKER_COUNT;i++){
BrakerConfig c = config.BRAKERS[i];
c.Enable = get_parameter_from_file("BRAKER["+str(i)+"].ENABLE","config");
c.SensPort= get_parameter_from_file("BRAKER["+str(i)+"].SENSPORT","config");
c.DropPort=get_parameter_from_file("BRAKER["+str(i)+"].DROPPORT","config");
c.Relay=get_parameter_from_file("BRAKER["+str(i)+"].RELAY","config");
c.Timeout=get_parameter_from_file("BRAKER["+str(i)+"].TIMEOUT","config");
}
}
}
void config_write(Configuration conf){
if (conf.STORE_LOCATON == FLASH){
fd.filenum = 1;
fd.open("config");
d("before write:file size:" +str(fd.filesize));
fd.setpointer(1);
string sb="";
string r = chr(13);
sb += "IP="+conf.IP+r;
string t[] = {"NONE","EEPROM","FLASH"};
sb += "STORE_LOCATON="+ t[conf.STORE_LOCATON]+r;
sb += "FLASH_SIZE=" + str(conf.FLASH_SIZE)+r;
sb += "FLASH_FILES=" + str(conf.FLASH_FILES)+r;
sb += "STORE_BRAKER=" + str(conf.STORE_BRAKER)+r;
fd.filenum = 1;
fd.setdata(sb);
d("|>>>"+sb);
for (int i = 0; i<BRAKER_COUNT;i++){
string b = "";
if (conf.BRAKERS[i].SensPort>0){
BrakerConfig c = conf.BRAKERS[i];
b+="BRAKER["+str(i)+"].ENABLE=" +str(c.Enable)+r;
b+="BRAKER["+str(i)+"].SENSPORT=" +str(c.SensPort)+r;
b+="BRAKER["+str(i)+"].DROPPORT=" +str(c.DropPort)+r;
b+="BRAKER["+str(i)+"].RELAY=" +str(c.Relay)+r;
b+="BRAKER["+str(i)+"].TIMEOUT=" +str(c.Timeout)+r;
fd.filenum = 1;
fd.setdata(b);
d("|>>>"+b);
}
}
for (int i=0;i<SENSORS;i++){
// d("write bounce" + chr(i));
string b = "";
if (conf.BOUNCE[i].Port>0){
b+="BOUNCE["+str(i)+"].ENABLE="+ str(conf.BOUNCE[i].Enable);
b+="BOUNCE["+str(i)+"].PORT="+ str(conf.BOUNCE[i].Port);
b+="BOUNCE["+str(i)+"].TIMEOUT="+ str(conf.BOUNCE[i].Timeout);
fd.filenum = 1;
fd.setdata(b);
d("|>>>"+b);
}
}
d("after write:file size:" +str(fd.filesize));
fd.close();
}
}
#define DEF_IP "192.168.123.19"
//#define DEF_IP "10.0.50.232"
void config_init(){
config_create();
int init= stor.getdata(5,1);
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;
if (!fd.open("config") || fd.filesize==0){
d("initial write config");
fd.create("config");
config_write(config);
}
config_load();
}
}
void config_save(){
config_write(config);
}
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 = "";
}