From a292fabb02dfae6a4c3f474f7d2e8f4c885832be Mon Sep 17 00:00:00 2001 From: Bertrand Songis Date: Sun, 12 Jan 2014 12:57:14 +0100 Subject: [PATCH] [Taranis] 6Pos switch support is now ok. Calibration is automatic. More tests needed --- radio/src/gui/menu_general.cpp | 59 ++++++++++++++++++++++++---------- radio/src/gui/menus.cpp | 10 ++++-- radio/src/gui/view_main.cpp | 6 ++-- radio/src/myeeprom.h | 5 +-- radio/src/opentx.cpp | 53 +++++++++++++++++------------- radio/src/opentx.h | 7 ++++ 6 files changed, 94 insertions(+), 46 deletions(-) diff --git a/radio/src/gui/menu_general.cpp b/radio/src/gui/menu_general.cpp index 78d56bc2c..766d05d8b 100644 --- a/radio/src/gui/menu_general.cpp +++ b/radio/src/gui/menu_general.cpp @@ -1250,13 +1250,13 @@ void menuCommonCalib(uint8_t event) #if defined(PCBTARANIS) uint8_t idx = i - POT1; int count = reusableBuffer.calib.xpotsPositionsCount[idx]; - if (count <= 6) { + if (count <= POTS_POS_COUNT) { bool found = false; for (int j=0; j reusableBuffer.calib.xpotsPositions[idx][j][0]-20 && vt < reusableBuffer.calib.xpotsPositions[idx][j][0]+20) { + if (vt > reusableBuffer.calib.xpotsPositions[idx][j][0]-10 && vt < reusableBuffer.calib.xpotsPositions[idx][j][0]+10) { reusableBuffer.calib.xpotsPositions[idx][j][0] = min(vt, reusableBuffer.calib.xpotsPositions[idx][j][0]); reusableBuffer.calib.xpotsPositions[idx][j][1] = max(vt, reusableBuffer.calib.xpotsPositions[idx][j][1]); - if (reusableBuffer.calib.xpotsPositions[idx][j][1] - reusableBuffer.calib.xpotsPositions[idx][j][0] > 50) { + if (reusableBuffer.calib.xpotsPositions[idx][j][1] - reusableBuffer.calib.xpotsPositions[idx][j][0] > 20) { reusableBuffer.calib.xpotsPositionsCount[idx] = 255; } found = true; @@ -1301,6 +1301,10 @@ void menuCommonCalib(uint8_t event) lcd_putsAtt(0*FW, 2*FH, STR_SETMIDPOINT, s_noScroll ? INVERS : 0); lcd_putsLeft(3*FH, STR_MENUWHENDONE); +#if defined(PCBTARANIS) + g_eeGeneral.potsType = 0; +#endif + for (uint8_t i=0; i=POT1 && i<=POT_LAST && reusableBuffer.calib.xpotsPositionsCount[i-POT1] > 1) { - g_eeGeneral.potsType &= ~(1 << (i-POT1)); - if (reusableBuffer.calib.xpotsPositionsCount[i-POT1] <= 6) { - StepsCalibData * calib = (StepsCalibData *) &g_eeGeneral.calib[i]; - calib->count = reusableBuffer.calib.xpotsPositionsCount[i-POT1]-1; - for (int j=0; jcount-1; j++) { - calib->steps[i] = (reusableBuffer.calib.xpotsPositions[i-POT1][j+1][0] + reusableBuffer.calib.xpotsPositions[i-POT1][j][1]) / 2; - } - g_eeGeneral.potsType |= (1 << (i-POT1)); - } - } - else -#endif if (abs(reusableBuffer.calib.loVals[i]-reusableBuffer.calib.hiVals[i]) > 50) { g_eeGeneral.calib[i].mid = reusableBuffer.calib.midVals[i]; int16_t v = reusableBuffer.calib.midVals[i] - reusableBuffer.calib.loVals[i]; @@ -1345,6 +1335,28 @@ void menuCommonCalib(uint8_t event) break; case 3: +#if defined(PCBTARANIS) + for (uint8_t i=POT1; i<=POT_LAST; i++) { + int idx = i - POT1; + int count = reusableBuffer.calib.xpotsPositionsCount[idx]; + if (count > 1 && count <= POTS_POS_COUNT) { + for (int j=0; jcount = count - 1; + for (int j=0; jcount; j++) { + calib->steps[j] = (reusableBuffer.calib.xpotsPositions[idx][j+1][0] + reusableBuffer.calib.xpotsPositions[idx][j][1]) >> 5; + } + } + } +#endif g_eeGeneral.chkSum = evalChkSum(); eeDirty(EE_GENERAL); reusableBuffer.calib.state = 4; @@ -1359,6 +1371,19 @@ void menuCommonCalib(uint8_t event) #if defined(PCBTARANIS) drawPotsBars(); + for (int i=POT1; i<=POT_LAST; i++) { + uint8_t steps = 0; + if (reusableBuffer.calib.state == 2) { + steps = reusableBuffer.calib.xpotsPositionsCount[i-POT1]; + } + else if (IS_MULTIPOS_POT(i)) { + StepsCalibData * calib = (StepsCalibData *) &g_eeGeneral.calib[i]; + steps = calib->count + 1; + } + if (steps > 0 && steps <= POTS_POS_COUNT) { + lcd_outdezAtt(LCD_W/2-2+(i-POT1)*5, LCD_H-6, steps, TINSIZE); + } + } #endif } diff --git a/radio/src/gui/menus.cpp b/radio/src/gui/menus.cpp index 9feed0435..9277f05b3 100644 --- a/radio/src/gui/menus.cpp +++ b/radio/src/gui/menus.cpp @@ -1265,8 +1265,14 @@ bool isSwitchAvailable(int16_t swtch) #if defined(PCBTARANIS) if (swtch >= SWSRC_P11 && swtch <= SWSRC_P26) { - int index = (swtch - SWSRC_P11) / 6; - return (g_eeGeneral.potsType & (1<count >= ((swtch - SWSRC_P11) % POTS_POS_COUNT)); + } + else { + return false; + } } #endif diff --git a/radio/src/gui/view_main.cpp b/radio/src/gui/view_main.cpp index 51465631d..930119fbd 100644 --- a/radio/src/gui/view_main.cpp +++ b/radio/src/gui/view_main.cpp @@ -120,9 +120,9 @@ void drawPotsBars() { // Optimization by Mike Blandford - uint8_t x, y, len ; // declare temporary variables - for (x=LCD_W/2-5, y=NUM_STICKS; ycount>0 && calib->count<6) { - uint8_t pos = calib->count; - int v = (int16_t)anaIn(POT1+i); - for (int j=0; jcount; j++) { - if (v < calib->steps[j]) { - pos = j; - break; - } - } + if (calib->count>0 && calib->countcount); uint8_t previousPos = potsPos[i] >> 4; if (pos != previousPos) { potsLastposStart[i] = get_tmr10ms(); @@ -1360,9 +1354,10 @@ void getSwitchesPosition() } } } +#endif } #define SWITCH_POSITION(sw) (switchesPos & (1<<(sw))) -#define POT_POSITION(sw) ((potsPos[(sw)/6] & 0x0f) == ((sw) % 6)) +#define POT_POSITION(sw) ((potsPos[(sw)/POTS_POS_COUNT] & 0x0f) == ((sw) % POTS_POS_COUNT)) #else #define getSwitchesPosition() #define SWITCH_POSITION(idx) switchState((EnumKeys)(SW_BASE+(idx))) @@ -2502,6 +2497,15 @@ void getADC() for (uint32_t i=0; i<4; i++) { adcRead(); for (uint32_t x=0; x=POT1 && x<=POT2 && temp[x]!=Analog_values[x]) { + i = 0; + } + temp[x] = Analog_values[x]; + } + else +#endif temp[x] += Analog_values[x]; } } @@ -2509,14 +2513,14 @@ void getADC() for (uint32_t x=0; x> 3; #if defined(PCBTARANIS) + if (s_noScroll) v = temp[x] >> 1; StepsCalibData * calib = (StepsCalibData *) &g_eeGeneral.calib[x]; - if (x >= POT1 && x <= POT_LAST && (g_eeGeneral.potsType & (1<<(x-POT1))) && calib->count>0 && calib->count<6) { - for (int i=0; i<=calib->count; i++) { - if (i == calib->count) { - s_anaFilt[x] = RESX; - } - else if (v < calib->steps[i]) { - s_anaFilt[x] = -RESX + i*2*RESX/calib->count; + if (IS_MULTIPOS_POT(x) && calib->count>0 && calib->count> 4); + s_anaFilt[x] = 2*RESX; + for (int i=0; icount; i++) { + if (vShifted < calib->steps[i]) { + s_anaFilt[x] = i*2*RESX/calib->count; break; } } @@ -2771,9 +2775,14 @@ void evalInputs(uint8_t mode) #ifndef SIMU if (i < NUM_STICKS+NUM_POTS) { - CalibData * calib = &g_eeGeneral.calib[i]; - v -= calib->mid; - v = v * (int32_t)RESX / (max((int16_t)100, (v>0 ? calib->spanPos : calib->spanNeg))); + if (IS_MULTIPOS_POT(i)) { + v -= RESX; + } + else { + CalibData * calib = &g_eeGeneral.calib[i]; + v -= calib->mid; + v = v * (int32_t)RESX / (max((int16_t)100, (v>0 ? calib->spanPos : calib->spanNeg))); + } } #endif @@ -3730,10 +3739,10 @@ void doMixerCalculations() // therefore forget the exact calculation and use only 1 instead; good compromise lastTMR = tmr10ms; - getSwitchesPosition(); - getADC(); + getSwitchesPosition(); + #if defined(PCBSKY9X) && !defined(REVA) && !defined(SIMU) Current_analogue = (Current_analogue*31 + s_anaFilt[8] ) >> 5 ; if (Current_analogue > Current_max) diff --git a/radio/src/opentx.h b/radio/src/opentx.h index d12bf7edd..ead6c7fc2 100644 --- a/radio/src/opentx.h +++ b/radio/src/opentx.h @@ -412,6 +412,12 @@ enum EnumKeys { #include "myeeprom.h" +#if defined(PCBTARANIS) + #define IS_MULTIPOS_POT(x) ((x)>=POT1 && (x)<=POT_LAST && (g_eeGeneral.potsType & (1 << ((x)-POT1)))) +#else + #define IS_MULTIPOS_POT(x) (false) +#endif + #if ROTARY_ENCODERS > 0 #define IF_ROTARY_ENCODERS(x) x, #else @@ -1098,6 +1104,7 @@ template FORCEINLINE t min(t a, t b) { return a FORCEINLINE t max(t a, t b) { return a>b?a:b; } template FORCEINLINE t sgn(t a) { return a>0 ? 1 : (a < 0 ? -1 : 0); } template FORCEINLINE t limit(t mi, t x, t ma) { return min(max(mi,x),ma); } +template void swap(t & a, t & b) { t tmp = b; b = a; a = tmp; } #if defined(HELI) || defined(FRSKY_HUB) uint16_t isqrt32(uint32_t n);