static bool eeLoadGeneral() { theFile.openRd(FILE_GENERAL); memset(&g_eeGeneral, 0, sizeof(EEGeneral)); // uint8_t sz = theFile.readRlc((uint8_t*)&g_eeGeneral, sizeof(EEGeneral)); theFile.readRlc((uint8_t*)&g_eeGeneral, sizeof(EEGeneral)); validateName( g_eeGeneral.ownerName, sizeof(g_eeGeneral.ownerName) ) ; // for(uint8_t i=0; i<sizeof(g_eeGeneral.ownerName);i++) // makes sure name is valid // { // uint8_t idx = char2idx(g_eeGeneral.ownerName[i]); // g_eeGeneral.ownerName[i] = idx2char(idx); // } if(g_eeGeneral.myVers<MDVERS) { sysFlags |= sysFLAG_OLD_EEPROM; // if old EEPROM - Raise flag g_eeGeneral.myVers = MDVERS; // update myvers STORE_GENERALVARS; } // if(sz>(sizeof(EEGeneral)-20)) for(uint8_t i=0; i<12;i++) sum+=g_eeGeneral.calibMid[i]; return g_eeGeneral.chkSum == evalChkSum() ; }
bool eeDuplicateModel(uint8_t id) { uint8_t i; for( i=id+1; i<MAX_MODELS; i++) { if(! EFile::exists(FILE_MODEL(i))) break; } if(i==MAX_MODELS) return false; //no free space in directory left theFile.openRd(FILE_MODEL(id)); theFile2.create(FILE_MODEL(i),FILE_TYP_MODEL,600); uint8_t buf[15]; uint8_t l; while((l=theFile.read(buf,15))) { theFile2.write(buf,l); // if(theFile.write_errno()==ERR_TMO) // { // //wait for 10ms and try again // uint16_t tgtime = get_tmr10ms() + 100; // while (!=tgtime); // theFile2.write(buf,l); // } wdt_reset(); } theFile2.closeTrunc(); //todo error handling return true; }
void eeLoadModelName(uint8_t id,char*buf,uint8_t len) { if(id<MAX_MODELS) { //eeprom_read_block(buf,(void*)modelEeOfs(id),sizeof(g_model.name)); theFile.openRd(FILE_MODEL(id)); memset(buf,' ',len); if(theFile.readRlc2((uint8_t*)buf,sizeof(g_model.name)+1) == (sizeof(g_model.name)+1) ) { uint8_t vers = buf[sizeof(g_model.name)]; uint16_t sz = theFile.size(); buf[sizeof(g_model.name)]=' '; buf+=len; *--buf='0'+vers%10; vers/=10; *--buf='0'+vers%10; *--buf='V'; --buf; while(sz){ --buf; *buf='0'+sz%10; sz/=10;} } } }
bool eeDuplicateModel(uint8_t id) { uint8_t i; for( i=id+1; i<MAX_MODELS; i++) { if(! EFile::exists(FILE_MODEL(i))) break; } if(i==MAX_MODELS) return false; //no free space in directory left theFile.openRd(FILE_MODEL(id)); theFile2.create(FILE_MODEL(i),FILE_TYP_MODEL,200); uint8_t buf[15]; uint8_t l; while((l=theFile.read(buf,15))) { theFile2.write(buf,l); wdt_reset(); } theFile2.closeTrunc(); //todo error handling return true; }
void eeLoadModelName(uint8_t id,char*buf,uint8_t len) { if(id<MAX_MODELS) { //eeprom_read_block(buf,(void*)modelEeOfs(id),sizeof(g_model.name)); theFile.openRd(FILE_MODEL(id)); memset(buf,' ',len); if(theFile.readRlc((uint8_t*)buf,sizeof(g_model.name)) == sizeof(g_model.name) ) { uint16_t sz=theFile.size(); buf+=len; while(sz) { div_t qr ; qr = div( sz, 10 ) ; --buf; *buf='0'+qr.rem; sz = qr.quot; } } } }
bool eeLoadGeneral() { theFile.openRd(FILE_GENERAL); uint8_t sz = 0; if(theFile.readRlc2((uint8_t*)&g_eeGeneral, 1)==1){ theFile.openRd(FILE_GENERAL); if(g_eeGeneral.myVers < GENVERS192){ sz = theFile.readRlc1((uint8_t*)&g_eeGeneral, sizeof(g_eeGeneral)); }else{ sz = theFile.readRlc2((uint8_t*)&g_eeGeneral, sizeof(g_eeGeneral)); } } //uint16_t sum=0; if( sz == sizeof(EEGeneral_r0) && g_eeGeneral.myVers == GENVERS0 ){ printf("converting EEGeneral data from < 119\n"); char* pSrc = ((char*)&g_eeGeneral) + sizeof(EEGeneral_r0);//Pointers behind the end char* pDst = ((char*)&g_eeGeneral) + sizeof(EEGeneral_r119); fullCopy(sizeof(EEGeneral_r0)-offsetof(EEGeneral_r0,calibSpan)); //for(uint8_t i=0; i<12;i++) sum+=g_eeGeneral.calibMid[i]; //g_eeGeneral.chkSum = sum; sz = sizeof(EEGeneral_r119); EEGeneral_r119*p119= (EEGeneral_r119*)&g_eeGeneral; p119->myVers = GENVERS119; } if( sz == sizeof(EEGeneral_r119) && g_eeGeneral.myVers == GENVERS119){ printf("converting EEGeneral data from 119 to 119_3\n"); EEGeneral_r119*p119= (EEGeneral_r119*)&g_eeGeneral; p119->adcFilt = 2; p119->thr0pos = 1; //upper 6 bits of adc value p119->myVers = GENVERS119_3; } if( sz == sizeof(EEGeneral_r119) && g_eeGeneral.myVers == GENVERS119_3){ printf("converting EEGeneral data from 119_3 to 150\n"); char* pSrc = ((char*)&g_eeGeneral) + sizeof(EEGeneral_r119);//Pters behind the end char* pDst = ((char*)&g_eeGeneral) + sizeof(EEGeneral_r150); fullCopy(sizeof(EEGeneral_r119)-offsetof(EEGeneral_r119,chkSum)); partCopy(7*2,4*2); //calibSpanPos partCopy(7*2,4*2); //calibSpanNeg partCopy(7*2,4*2); //calibMid EEGeneral_r150*p150= (EEGeneral_r150*)&g_eeGeneral; p150->inactivityMin = 0; p150->resv = 0; for(uint8_t i=4; i<7;i++){ p150->calibMid[i] = 0x200; p150->calibSpanNeg[i] = 0x180; p150->calibSpanPos[i] = 0x180; } sz = sizeof(EEGeneral_r150); p150->myVers = GENVERS150; } if( sz == sizeof(EEGeneral_r150) && g_eeGeneral.myVers == GENVERS150){ EEGeneral_r150 *p150= (EEGeneral_r150*)&g_eeGeneral; if(p150->stickMode & 1) memswap(&p150->trainer.chanMix[1], &p150->trainer.chanMix[2],sizeof(p150->trainer.chanMix[1])); if(p150->stickMode & 2) memswap(&p150->trainer.chanMix[0], &p150->trainer.chanMix[3],sizeof(p150->trainer.chanMix[0])); p150->myVers = GENVERS150_5; } if( sz == sizeof(EEGeneral_r150) && g_eeGeneral.myVers == GENVERS150_5){ EEGeneral_r150 &p150= *(EEGeneral_r150*)&g_eeGeneral; EEGeneral_r192 &p192= *(EEGeneral_r192*)&g_eeGeneral; memmove(&p192.trainer.calib[4], &p150.trainer, sizeof(EEGeneral_r150)-offsetof(EEGeneral_r150,trainer)); sz+=8; p192.myVers = GENVERS192; } if( sz == sizeof(EEGeneral_TOP) && g_eeGeneral.myVers == GENVERS_TOP){ return true; } printf("bad g_eeGeneral sz=%d vers=%d\n",sz,g_eeGeneral.myVers); return false; }
void eeLoadModel(uint8_t id) { if(id>=MAX_MODELS) return; //paranoia theFile.openRd(FILE_MODEL(id)); uint16_t sz = 0; if(theFile.readRlc2((uint8_t*)&g_model,sizeof(g_model.name)+1) == (sizeof(g_model.name)+1)){ theFile.openRd(FILE_MODEL(id)); if(g_model.mdVers < MDVERS192){ sz = theFile.readRlc1((uint8_t*)&g_model, sizeof(g_model)); }else{ sz = theFile.readRlc2((uint8_t*)&g_model, sizeof(g_model)); } } // #if 0 // if( sz == sizeof(ModelData_r0) ){ // printf("converting model data t0 r84\n"); // char* pSrc = ((char*)&g_model) + sizeof(ModelData_r0); //Pointers behind the end // char* pDst = ((char*)&g_model) + sizeof(ModelData_r84); // ModelData_r84 *model84 = (ModelData_r84*)&g_model; // #define sizeof84(memb) sizeof(((ModelData_r84*)0)->memb) // fullCopy(sizeof84(trimData)+sizeof84(curves9)+sizeof84(curves5)); // // partCopy(sizeof84(mixData), sizeof(MixData_r0)*20); // // for(uint8_t i=0; i<DIM(model84->expoData); i++){ // partCopy(sizeof(ExpoData_r84), sizeof(ExpoData_r0)); // } // sz = sizeof(ModelData_r84); // model84->mdVers = MDVERS84; // } // #endif if( sz == sizeof(ModelData_r84) && g_model.mdVers == MDVERS84) { printf("converting model data from r84 to r143\n"); ModelData_r84 *model84 = (ModelData_r84*)&g_model; ModelData_r143 *model143 = (ModelData_r143*)&g_model; for(int8_t i=3; i>=0; i--){ int16_t val = trimExp2(model84->trimData[i].trim) + model84->trimData[i].trimDef_lt133; model143->trimData[i].itrim = trimRevert2(val); model143->trimData[i].tmode = 0; } memmove(&model143->curves5, &model84->curves5, sizeof(model84->curves5)+sizeof(model84->curves9)); memset(model143->curves3, 0, sizeof(model143->curves3)); model143->curves3[0][2] = 100; model143->curves3[2][0] = 100; model143->curves3[2][2] = 100; model143->curves3[1][0] = -100; sz = sizeof(ModelData_r143); model84->mdVers = MDVERS143; } if( sz == sizeof(ModelData_r143) && g_model.mdVers == MDVERS143) { printf("converting model data from r143 to r167\n"); ModelData_r143 *model143 = (ModelData_r143*)&g_model; ModelData_r167 *model167 = (ModelData_r167*)&g_model; for(int8_t i=0; i<NUM_CHNOUT; i++){ int8_t v = model143->limitData[i].min-100; model167->limitData[i].min = add7Bit(val2idx50_150(v),40); model167->limitData[i].scale = 0; v = model143->limitData[i].max+100; model167->limitData[i].max = add7Bit(val2idx50_150(v),-40); model167->limitData[i].binSwtch = 0; } model143->mdVers = MDVERS167; } if( sz == sizeof(ModelData_r167) && g_model.mdVers == MDVERS167) { printf("converting model data from r167 to r171\n"); ModelData_r167 *model167 = (ModelData_r167*)&g_model; ModelData_r171 *model171 = (ModelData_r171*)&g_model; ExpoData_r84 *hlpExp = (ExpoData_r84*) ((char*)&model171->expoTab +sizeof(model171->expoTab) -sizeof(model167->expoData)); // old:20B new:45B // move old to end // clr unused start 25B // interpret old to new <= 3*8=24B // clr old at end 20B // memmove(hlpExp, &model167->expoData, sizeof(ModelData_r167)-offsetof(ModelData_r167,expoData)); memset(model171->expoTab, 0, sizeof(model171->expoTab) -sizeof(model167->expoData)); for(int8_t i=0,j=0; i<4; i++){ if(hlpExp[i].expNorm || hlpExp[i].expNormWeight){ model171->expoTab[j].drSw = hlpExp[i].drSw ? -hlpExp[i].drSw : 0; model171->expoTab[j].chn = convertMode(i); model171->expoTab[j].mode3 = EM_BOTH; model171->expoTab[j].exp5 = val2idx15_100(hlpExp[i].expNorm); model171->expoTab[j].weight6 =val2idx30_100(hlpExp[i].expNormWeight+100); j++; } if(hlpExp[i].drSw && (hlpExp[i].expDr || hlpExp[i].expSwWeight)){ model171->expoTab[j].drSw = hlpExp[i].drSw; model171->expoTab[j].chn = convertMode(i); model171->expoTab[j].mode3 = EM_BOTH; model171->expoTab[j].exp5 = val2idx15_100(hlpExp[i].expDr); model171->expoTab[j].weight6 =val2idx30_100(hlpExp[i].expSwWeight+100); j++; } } memset(hlpExp, 0, sizeof(model167->expoData)); sz = sizeof(ModelData_r171); for(uint8_t i=0;i<MAX_MIXERS;i++){ MixData_r0 &md = model171->mixData[i]; if(md.destCh==0) break; md.srcRaw = convertMode(md.srcRaw-1)+1; } if(g_eeGeneral.stickMode & 1) memswap(&model171->trimData[1], &model171->trimData[2],sizeof(model171->trimData[1])); if(g_eeGeneral.stickMode & 2) memswap(&model171->trimData[0], &model171->trimData[3],sizeof(model171->trimData[0])); model167->mdVers = MDVERS171; } if( sz == sizeof(ModelData_r171) && g_model.mdVers == MDVERS171) { printf("converting model data from r171 to r192\n"); ModelData_r171 *model171 = (ModelData_r171*)&g_model; ModelData_r192 *model192 = (ModelData_r192*)&g_model; memmove(&model192->curves3, &model171->curves3, sizeof(ModelData_r171)-offsetof(ModelData_r171,curves3)); for(uint8_t i=MAX_MIXERS;i>0;){i--; MixData_r0 &md0 = model171->mixData[i]; MixData_r192 &md192 = model192->mixData[i]; uint8_t dc=md0.destCh; if(dc==0){ memset(&md192,0,5); continue; } memmove(&md192.weight,&md0.weight,3); // 0 1 2 3 4 5 6 7 8 9 uint8_t sr=md0.srcRaw; //0 RUD ELE THR AIL P1 P2 P3 MAX FUL X1-4 memset(&md192,0,2); md192.destCh =dc; if(sr>4){ if(sr<=9) md192.switchMode=1; //P1-3, FUL I=-1 if(sr==8) md192.switchMode=2; //MAX I=0 if(sr==9) sr=8; //FUL->MAX if(sr>9) sr+=8;//space for ch1-ch8 if(sr>7) sr+=3;//space for p1-3 } md192.srcRaw =sr-1; //now start with 0 if(md192.weight<0 && md192.curve){ md192.weight = -md192.weight; md192.curveNeg = 1; } } sz = sizeof(ModelData_r192); model171->mdVers = MDVERS192; } if( sz == sizeof(ModelData_r192) && g_model.mdVers == MDVERS192) { printf("converting model data from r192 to r204\n"); ModelData_r192 *model192 = (ModelData_r192*)&g_model; ModelData_r204 *model204 = (ModelData_r204*)&g_model; memmove(&model204->trimData,&model192->trimData,sizeof(model192->trimData)); memset(model204->switchTab,0,sizeof(model204->switchTab)); model204->trimData[0].tmode=0; model204->trimData[1].tmode=0; model204->trimData[2].tmode=0; model204->trimData[3].tmode=0; sz = sizeof(ModelData_r204); model204->mdVers = MDVERS204; } if( sz == sizeof(ModelData_TOP) && g_model.mdVers == MDVERS_TOP) { return; } printf("bad model%d data using default\n",id+1); modelDefault(id); }
void eeLoadModel(uint8_t id) { if(id<MAX_MODELS) { theFile.openRd(FILE_MODEL(id)); memset(&g_model, 0, sizeof(ModelData)); uint16_t sz = theFile.readRlc((uint8_t*)&g_model, sizeof(g_model)); if(sz<256) // if not loaded a fair amount { modelDefaultWrite(id); } validateName( g_model.name, sizeof(g_model.name) ) ; // for(uint8_t i=0; i<sizeof(g_model.name);i++) // makes sure name is valid // { // uint8_t idx = char2idx(g_model.name[i]); // g_model.name[i] = idx2char(idx); // } if ( g_model.numBlades == 0 ) { g_model.numBlades = g_model.xnumBlades + 2 ; } #ifdef FRSKY FrskyAlarmSendState |= 0x40 ; // Get RSSI Alarms FRSKY_setModelAlarms(); #endif #ifdef FIX_MODE // check for updating mix sources if ( g_model.modelVersion < 2 ) { for(uint8_t i=0;i<MAX_MIXERS;i++) { MixData *md = mixaddress( i ) ; if (md->srcRaw) { if (md->srcRaw <= 4) // Stick { md->srcRaw = modeFixValue( md->srcRaw-1 ) ; } } } for (uint8_t i = 0 ; i < NUM_CSW ; i += 1 ) { CSwData *cs = &g_model.customSw[i]; uint8_t cstate = CS_STATE(cs->func); uint8_t t = 0 ; if(cstate == CS_VOFS) { t = 1 ; } else if(cstate == CS_VCOMP) { t = 1 ; if (cs->v2) { if (cs->v2 <= 4) // Stick { cs->v2 = modeFixValue( cs->v2-1 ) ; } } } if ( t ) { if (cs->v1) { if (cs->v1 <= 4) // Stick { cs->v1 = modeFixValue( cs->v1-1 ) ; } } } } #if defined(CPUM128) || defined(CPUM2561) for (uint8_t i = NUM_CSW ; i < NUM_CSW+EXTRA_CSW ; i += 1 ) { CxSwData *cs = &g_model.xcustomSw[i-NUM_CSW]; uint8_t cstate = CS_STATE(cs->func); uint8_t t = 0 ; if(cstate == CS_VOFS) { t = 1 ; } else if(cstate == CS_VCOMP) { t = 1 ; if (cs->v2) { if (cs->v2 <= 4) // Stick { cs->v2 = modeFixValue( cs->v2-1 ) ; } } } if ( t ) { if (cs->v1) { if (cs->v1 <= 4) // Stick { cs->v1 = modeFixValue( cs->v1-1 ) ; } } } } #endif // CPUs memmove( &Xmem.texpoData, &g_model.expoData, sizeof(Xmem.texpoData) ) ; for (uint8_t i = 0 ; i < 4 ; i += 1 ) { uint8_t dest = modeFixValue( i ) - 1 ; memmove( &g_model.expoData[dest], &Xmem.texpoData[i], sizeof(Xmem.texpoData[0]) ) ; } // sort expo/dr here alert(PSTR("CHECK MIX/DR SOURCES")); g_model.modelVersion = 2 ; eeDirty( EE_MODEL ) ; eeWaitComplete() ; } #endif // FIX_MODE #ifdef VERSION3 if ( g_model.modelVersion < 3 ) { for (uint8_t i = 0 ; i < NUM_CSW ; i += 1 ) { CSwData *cs = &g_model.customSw[i]; if ( cs->func == CS_LATCH ) { cs->func = CS_GREATER ; } if ( cs->func == CS_FLIP ) { cs->func = CS_LESS ; } } #if defined(CPUM128) || defined(CPUM2561) for (uint8_t i = NUM_CSW ; i < NUM_CSW+EXTRA_CSW ; i += 1 ) { CxSwData *cs = &g_model.xcustomSw[i-NUM_CSW]; if ( cs->func == CS_LATCH ) { cs->func = CS_GREATER ; } if ( cs->func == CS_FLIP ) { cs->func = CS_LESS ; } } #endif // CPUs g_model.modelVersion = 3 ; eeDirty( EE_MODEL ) ; eeWaitComplete() ; } #endif #ifdef VERSION4 if ( g_model.modelVersion < 4 ) { g_model.switchWarningStates = g_eeGeneral.switchWarningStates ; alert(PSTR("CHECK MODEL TIMERS")); g_model.modelVersion = 4 ; eeDirty( EE_MODEL ) ; eeWaitComplete() ; } #endif // VERSION4 } TimerMode *ptConfig = TimerConfig ; FORCE_INDIRECT(ptConfig) ; ptConfig->tmrModeA = g_model.tmrMode ; ptConfig->tmrModeB = g_model.tmrModeB ; ptConfig->tmrVal = g_model.tmrVal ; ptConfig->tmrDir = g_model.tmrDir ; ptConfig += 1 ; ptConfig->tmrModeA = g_model.tmr2Mode ; ptConfig->tmrModeB = g_model.tmr2ModeB ; ptConfig->tmrVal = g_model.tmr2Val ; ptConfig->tmrDir = g_model.tmr2Dir ; resetTimer1() ; resetTimer2() ; asm("") ; }