diff --git a/src/menus.cpp b/src/menus.cpp index 0f49c925e..d28a3b1be 100644 --- a/src/menus.cpp +++ b/src/menus.cpp @@ -677,34 +677,49 @@ int8_t switchMenuItem(uint8_t x, uint8_t y, int8_t value, LcdFlags attr, uint8_t } #if defined(GVARS) -int16_t gvarMenuItem(uint8_t x, uint8_t y, int16_t value, int16_t min, int16_t max, LcdFlags attr, uint8_t event) // @@@ open.20.fsguruh +int16_t gvarMenuItem(uint8_t x, uint8_t y, int16_t value, int16_t min, int16_t max, LcdFlags attr, uint8_t event) { - uint8_t delta = (max <= 100 ? GV1_SMALL-1 : GV1_LARGE-1); + uint16_t delta = GV_GET_GV1_VALUE(max); bool invers = (attr & INVERS); if (invers && event == EVT_KEY_LONG(KEY_ENTER)) { s_editMode = !s_editMode; - value = (value > max ? GET_GVAR(value, min, max, s_perout_flight_phase) : delta+1); + + value = ( GV_IS_GV_VALUE(value,min,max) ? GET_GVAR(value, min, max, s_perout_flight_phase) : delta); + eeDirty(EE_MODEL); } - if (value > max) { + if ( GV_IS_GV_VALUE(value,min,max) ) { if (attr & LEFT) attr -= LEFT; /* because of ZCHAR */ else x -= 2*FW+FWNUM; - int8_t idx = value - delta; + + int8_t idx = (int16_t) GV_INDEX_CALC_DELTA(value,delta); if (invers) { - CHECK_INCDEC_MODELVAR(event, idx, -4, +5); - value = (int16_t)idx + delta; + CHECK_INCDEC_MODELVAR(event, idx, -MAX_GVARS, MAX_GVARS-1); } - if (idx <= 0) { idx = 1-idx; lcd_putcAtt(x-6, y, '-', attr); } + +/* sometimes this implementation was smaller, but sometimes not.... + value=delta+(int16_t) idx; + if (idx < 0) {idx = -1-idx; lcd_putcAtt(x-6, y, '-', attr); } + else value|=~(delta-1); + idx++; */ + + if (idx < 0) { + value=(int16_t) GV_CALC_VALUE_IDX_NEG(idx,delta); idx=-idx; lcd_putcAtt(x-6, y, '-', attr); + } else { + value = (int16_t) GV_CALC_VALUE_IDX_POS(idx,delta); idx++; + } + putsStrIdx(x, y, STR_GV, idx, attr); } else { lcd_outdezAtt(x, y, value, attr); - if (invers) value = checkIncDec(event, value, min, max,EE_MODEL); // as proposed, no change to makro, but directly call this function + if (invers) value = checkIncDec(event, value, min, max,EE_MODEL); } return value; } + #else int8_t gvarMenuItem(uint8_t x, uint8_t y, int8_t value, int16_t min, int16_t max, LcdFlags attr, uint8_t event) { diff --git a/src/model_menus.cpp b/src/model_menus.cpp index 562ffee53..d75910522 100644 --- a/src/model_menus.cpp +++ b/src/model_menus.cpp @@ -1907,7 +1907,7 @@ void gvarWeightItem(xcoord_t x, uint8_t y, MixData *md, uint8_t attr, uint8_t ev // @@@ open.20.fsguruh u_int8int16_t weight; MD_GETWEIGHT(weight,md); - weight.word = gvarMenuItem(x, y, weight.word, -245, 245, attr, event); + weight.word = gvarMenuItem(x, y, weight.word, GV_RANGELARGE_NEG, GV_RANGELARGE, attr, event); MD_SETWEIGHT(weight,md); //#endif } @@ -1988,7 +1988,7 @@ void menuModelMixOne(uint8_t event) // @@@ open.20.fsguruh u_int8int16_t offset; MD_GETOFFSET(offset,md2); - offset.word = gvarMenuItem(COLUMN_X+MIXES_2ND_COLUMN, y, offset.word, -245, 245, attr|LEFT, event); + offset.word = gvarMenuItem(COLUMN_X+MIXES_2ND_COLUMN, y, offset.word, GV_RANGELARGE_NEG, GV_RANGELARGE, attr|LEFT, event); MD_SETOFFSET(offset,md2); // #else // md2->offset = gvarMenuItem(COLUMN_X+MIXES_2ND_COLUMN, y, md2->offset, -125, 125, attr|LEFT, event); @@ -2307,6 +2307,7 @@ void menuModelExpoMix(uint8_t expo, uint8_t event) else displayGVar(15*FW+2, y, md->curveParam, -100, 100); // open.20.fsguruh // displayGVar(15*FW+2, y, md->curveParam, -125, 125); // only -100 to +100 is allowed now + // could be increased now, but is it useful? differentiate with more the +-100% is a fault, correct? } if (md->swtch) putsSwitches(16*FW, y, md->swtch); diff --git a/src/myeeprom.h b/src/myeeprom.h index 2eac0d9bc..3a30457e1 100644 --- a/src/myeeprom.h +++ b/src/myeeprom.h @@ -65,6 +65,34 @@ #define PACK( __Declaration__ ) __Declaration__ __attribute__((__packed__)) #endif + +typedef int16_t gvar_t; + +#if !defined(CPUM64) +typedef char gvar_name_t[6]; +#define GVAR_MAX 1024 +#endif + +#if defined(CPUM64) && defined(GVARS) + #define MAX_GVARS 9 + #define MODEL_GVARS_DATA gvar_t gvars[MAX_GVARS]; + #define PHASE_GVARS_DATA + #define GVAR_VALUE(x, p) g_model.gvars[x] +#elif defined(CPUM64) + #define MAX_GVARS 0 + #define MODEL_GVARS_DATA + #define PHASE_GVARS_DATA +#elif defined(GVARS) + #define MAX_GVARS 9 + #define MODEL_GVARS_DATA gvar_name_t gvarsNames[MAX_GVARS]; + #define PHASE_GVARS_DATA gvar_t gvars[MAX_GVARS] + #define GVAR_VALUE(x, p) g_model.phaseData[p].gvars[x] +#else + #define MAX_GVARS 0 + #define MODEL_GVARS_DATA gvar_name_t gvarsNames[5]; + #define PHASE_GVARS_DATA gvar_t gvars[5] +#endif + PACK(typedef struct t_TrainerMix { uint8_t srcChn:6; // 0-7 = ch1-8 uint8_t mode:2; // off,add-mode,subst-mode @@ -427,7 +455,8 @@ enum Functions { #endif #if defined(GVARS) FUNC_ADJUST_GV1, - FUNC_ADJUST_GV5 = FUNC_ADJUST_GV1 + 4, + // FUNC_ADJUST_GV5 = FUNC_ADJUST_GV1 + 4, + FUNC_ADJUST_GVLAST = (FUNC_ADJUST_GV1 + (MAX_GVARS-1)), #endif #if defined(DEBUG) FUNC_TEST, // should remain the last before MAX as not added in companion9x @@ -693,33 +722,6 @@ PACK(typedef struct t_SwashRingData { // Swash Ring data #define TRIM_ARRAY int16_t trim[4] #endif -typedef int16_t gvar_t; - -#if !defined(CPUM64) -typedef char gvar_name_t[6]; -#define GVAR_MAX 1024 -#endif - -#if defined(CPUM64) && defined(GVARS) -#define MAX_GVARS 5 -#define MODEL_GVARS_DATA gvar_t gvars[MAX_GVARS]; -#define PHASE_GVARS_DATA -#define GVAR_VALUE(x, p) g_model.gvars[x] -#elif defined(CPUM64) -#define MAX_GVARS 0 -#define MODEL_GVARS_DATA -#define PHASE_GVARS_DATA -#elif defined(GVARS) -#define MAX_GVARS 5 -#define MODEL_GVARS_DATA gvar_name_t gvarsNames[MAX_GVARS]; -#define PHASE_GVARS_DATA gvar_t gvars[MAX_GVARS] -#define GVAR_VALUE(x, p) g_model.phaseData[p].gvars[x] -#else -#define MAX_GVARS 0 -#define MODEL_GVARS_DATA gvar_name_t gvarsNames[5]; -#define PHASE_GVARS_DATA gvar_t gvars[5] -#endif - PACK(typedef struct t_PhaseData { TRIM_ARRAY; int8_t swtch; // swtch of phase[0] is not used diff --git a/src/open9x.cpp b/src/open9x.cpp index 7ef1180da..d840dd176 100644 --- a/src/open9x.cpp +++ b/src/open9x.cpp @@ -757,12 +757,7 @@ int16_t applyLimits(uint8_t channel, int32_t value) // thanks to gbirkus, he motivated this change, which greatly reduces overruns // unfortunately the constants and 32bit compares generates about 50 bytes codes; didn't find a way to get it down. // But I think it is worth - value = limit(int32_t(-RESXl*256),value,int32_t(RESXl*256)); // saves 2 bytes compared to solution below - - // try also limit 0xFFFFF - // value = limit(int32_t(-0xFFFFF),value,int32_t(0xFFFFF)); // saves 2 bytes compared to solution below - // if (value>RESXl*256) value = RESXl*256; // no bigger values allowed, prevent early overruns - // if (value<-RESXl*256) value = -RESXl*256; // no smaller values allowed, prevent early overruns + value = limit(int32_t(-RESXl*256),value,int32_t(RESXl*256)); // saves 2 bytes compared to other solutions up to now #endif #if defined(PPM_LIMITS_SYMETRICAL) @@ -1251,8 +1246,8 @@ uint8_t s_gvar_last = 0; #if defined(CPUM64) int16_t getGVarValue(int16_t x, int16_t min, int16_t max) { - if ((x > (GV1_LARGE-6)) || (x>max)) { // @@@ open.20.fsguruh - int8_t idx = (max <= 100 ? x - GV1_SMALL : x - GV1_LARGE); + if (GV_IS_GV_VALUE(x,min,max)) { + int8_t idx = GV_INDEX_CALCULATION(x,max); int8_t mul = 1; if (idx < 0) { @@ -1291,12 +1286,12 @@ uint8_t getGVarFlightPhase(uint8_t phase, uint8_t idx) int16_t getGVarValue(int16_t x, int16_t min, int16_t max, int8_t phase) { - if (x > max) { - int8_t idx = (max <= 100 ? x - GV1_SMALL : x - GV1_LARGE); + if (GV_IS_GV_VALUE(x,min,max)) { + int8_t idx = GV_INDEX_CALCULATION(x,max); int8_t mul = 1; if (idx < 0) { - idx = 1-idx; + idx = -1-idx; mul = -1; } @@ -2717,7 +2712,7 @@ void perOut(uint8_t mode, uint8_t tick10ms) //========== OFFSET =============== if (apply_offset_and_curve) { - int16_t offset = GET_GVAR(MD_OFFSET(md), -245, 245, s_perout_flight_phase); // open.20.fsguruh + int16_t offset = GET_GVAR(MD_OFFSET(md), GV_RANGELARGE_NEG, GV_RANGELARGE, s_perout_flight_phase); // open.20.fsguruh if (offset) v += calc100toRESX_16Bits(offset); // @@@ open.20.fsguruh } @@ -2734,7 +2729,7 @@ void perOut(uint8_t mode, uint8_t tick10ms) } // saves 12 bytes code if done here and not together with weight; unknown reason - int16_t weight = GET_GVAR(MD_WEIGHT(md), -500, 500, s_perout_flight_phase); + int16_t weight = GET_GVAR(MD_WEIGHT(md), GV_RANGELARGE_NEG, GV_RANGELARGE, s_perout_flight_phase); weight=calc100to256_16Bits(weight); //========== SPEED =============== @@ -2820,6 +2815,15 @@ void perOut(uint8_t mode, uint8_t tick10ms) break; } //endswitch md->mltpx #ifdef PREVENT_ARITHMETIC_OVERFLOW +/* + // a lot of assumptions must be true, for this kind of check; not really worth for only 4 bytes flash savings + // this solution would save again 4 bytes flash + int8_t testVar=(*ptr<<1)>>24; + if ( (testVar!=-1) && (testVar!=0 ) ) { + // this devices by 64 which should give a good balance between still over 100% but lower then 32x100%; should be OK + *ptr >>= 6; // this is quite tricky, reduces the value a lot but should be still over 100% and reduces flash need + } */ + PACK( union u_int16int32_t { struct { @@ -2834,13 +2838,8 @@ void perOut(uint8_t mode, uint8_t tick10ms) if (tmp.dword<0) { if ((tmp.words_t.hi&0xFF80)!=0xFF80) tmp.words_t.hi=0xFF86; // set to min nearly - //tmp.bytes_t.h4=0xFF; - //tmp.bytes_t.h3|=0x80; } else { - if ((tmp.words_t.hi|0x007F)!=0x007F) tmp.words_t.hi=0x00079; // set to max nearly - // tmp.bytes_t.h4=0x00; - // tmp.bytes_t.h3&=0x7F; - // tmp.words_t.hi&=0x007F; + if ((tmp.words_t.hi|0x007F)!=0x007F) tmp.words_t.hi=0x0079; // set to max nearly } *ptr=tmp.dword; // this implementation saves 18bytes flash diff --git a/src/open9x.h b/src/open9x.h index 2c8027d5b..ddd1fa49f 100644 --- a/src/open9x.h +++ b/src/open9x.h @@ -812,21 +812,49 @@ void incRotaryEncoder(uint8_t idx, int8_t inc); void setGVarValue(uint8_t x, int8_t value); #define GET_GVAR(x, min, max, p) getGVarValue(x, min, max) #define SET_GVAR(idx, val, p) setGVarValue(idx, val) + #define GV1_SMALL 128 + #define GV1_LARGE 256 #else uint8_t getGVarFlightPhase(uint8_t phase, uint8_t idx); int16_t getGVarValue(int16_t x, int16_t min, int16_t max, int8_t phase); void setGVarValue(uint8_t x, int8_t value, int8_t phase); #define GET_GVAR(x, min, max, p) getGVarValue(x, min, max, p) #define SET_GVAR(idx, val, p) setGVarValue(idx, val, p) + // @@@ open.20.fsguruh may have different recalculation formula in future to allow higher ranges? + #define GV1_SMALL 128 + #define GV1_LARGE 256 #endif - #define GV1_SMALL 123 - // @@@ open.20.fsguruh - #define GV1_LARGE 251 + + // depends if we want free ranges for use, if no MAX_GVARS are smaller + // if not this would assume a max for GVARS instead using MAX_GVARS + #define GV_RANGESMALL (GV1_SMALL - (MAX_GVARS+1)) + #define GV_RANGESMALL_NEG (-GV1_SMALL + (MAX_GVARS+1)) + #define GV_RANGELARGE (GV1_LARGE - (MAX_GVARS+1)) + #define GV_RANGELARGE_NEG (-GV1_LARGE + (MAX_GVARS+1)) + + #define GV_IS_GV_VALUE(x,min,max) ( (x>max) || (x