Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00064
00065
00066
00067 #include "mTouch.h"
00068
00069
00070
00071
00072
00083
00084 mTouch_State mTouch_state;
00085 mTouch_StateVariables mTouch_stateVars;
00086 uint16_t mTouch_average [MTOUCH_NUMBER_SENSORS];
00087
00088 #if defined(MCOMM_ENABLED)
00089 #if (MTOUCH_NUMBER_SENSORS <= 8)
00090 uint8_t mTouch_stateMask;
00091 #elif (MTOUCH_NUMBER_SENSORS <= 16)
00092 uint16_t mTouch_stateMask;
00093 #elif (MTOUCH_NUMBER_SENSORS <= 24)
00094 uint24_t mTouch_stateMask;
00095 #elif (MTOUCH_NUMBER_SENSORS <= 32)
00096 uint32_t mTouch_stateMask;
00097 #endif
00098 #endif
00099
00100 #if defined(MTOUCH_SLIDERS_ENABLED) || defined(MTOUCH_MOST_PRESSED_ONLY) || defined(MTOUCH_MATRIX_ENABLED)
00101 uint16_t mTouch_deltas [MTOUCH_NUMBER_SENSORS];
00102 #endif
00103
00104 #if defined(MTOUCH_TOGGLE_ENABLED)
00105 #if (MTOUCH_NUMBER_SENSORS <= 8)
00106 uint8_t mTouch_toggle;
00107 #elif (MTOUCH_NUMBER_SENSORS <= 16)
00108 uint16_t mTouch_toggle;
00109 #elif (MTOUCH_NUMBER_SENSORS <= 24)
00110 uint24_t mTouch_toggle;
00111 #elif (MTOUCH_NUMBER_SENSORS <= 32)
00112 uint32_t mTouch_toggle;
00113 #endif
00114 #endif
00115
00116 #if defined(MTOUCH_MATRIX_ENABLED) && defined(MTOUCH_MOST_PRESSED_ONLY)
00117
00118 #define MTOUCH_MOST_PRESSED_INDEX 0
00119 #define MTOUCH_MATRIX_ROW_INDEX 1
00120 #define MTOUCH_MATRIX_COL_INDEX 2
00121
00122 #define MTOUCH_MOSTPRESSED_ARRAYSIZE 3
00123
00124 uint8_t mTouch_matrix;
00125 uint8_t mTouch_matrix_latch;
00126 mTouch_MostPressedData* mTouch_mostPressed;
00127 mTouch_MostPressedData mTouch_mostPressedData [3];
00128 const mTouch_MostPressedIndex mTouch_mostPressedIndex[3] = {
00129 { MTOUCH_SENSOR_START, MTOUCH_SENSOR_END },
00130 { MTOUCH_MATRIX_ROW_START, MTOUCH_MATRIX_ROW_END },
00131 { MTOUCH_MATRIX_COLUMN_START, MTOUCH_MATRIX_COLUMN_END }
00132 };
00133 #elif defined(MTOUCH_MATRIX_ENABLED)
00134
00135 #define MTOUCH_MATRIX_ROW_INDEX 0
00136 #define MTOUCH_MATRIX_COL_INDEX 1
00137 #define MTOUCH_DUMMY_MOST_PRESSED_INDEX 2
00138
00139 #define MTOUCH_MOSTPRESSED_ARRAYSIZE 2
00140 uint8_t mTouch_matrix;
00141 uint8_t mTouch_matrix_latch;
00142 mTouch_MostPressedData* mTouch_mostPressed;
00143 mTouch_MostPressedData mTouch_mostPressedData [3];
00144
00145
00146
00147
00148
00149 const mTouch_MostPressedIndex mTouch_mostPressedIndex[2] = {
00150 { MTOUCH_MATRIX_ROW_START, MTOUCH_MATRIX_ROW_END },
00151 { MTOUCH_MATRIX_COLUMN_START, MTOUCH_MATRIX_COLUMN_END }
00152 };
00153
00154 #elif defined(MTOUCH_MOST_PRESSED_ONLY)
00155
00156 #define MTOUCH_MOST_PRESSED_INDEX 0
00157 #define MTOUCH_MOSTPRESSED_ARRAYSIZE 1
00158
00159 mTouch_MostPressedData* mTouch_mostPressed;
00160 mTouch_MostPressedData mTouch_mostPressedData [1];
00161 const mTouch_MostPressedIndex mTouch_mostPressedIndex[1] = { { 0, MTOUCH_NUMBER_SENSORS-1 } };
00162
00163 #endif
00164
00165
00167
00168
00169
00170
00180
00181 #if !defined(MTOUCH_EEPROM_ENABLED)
00182
00183
00184 const uint16_t mTouch_pressThreshold [MTOUCH_NUMBER_SENSORS] = PRESS_THRESHOLD_INIT;
00185 const uint16_t mTouch_releaseThreshold [MTOUCH_NUMBER_SENSORS] = RELEASE_THRESHOLD_INIT;
00186 #endif
00187
00189
00190
00191
00192
00193 void mTouch_Init (void);
00194 void mTouch_Decode (void);
00195 void mTouch_UpdateBaseline (void);
00196 void mTouch_ChangeState (void);
00197
00198 uint8_t mTouch_ButtonStateMachine (void);
00199
00200 #if defined(MTOUCH_MOST_PRESSED_ONLY) || defined(MTOUCH_MATRIX_ENABLED)
00201 void mTouch_FindMostPressed (uint8_t);
00202 #endif
00203 #if defined(MTOUCH_MATRIX_ENABLED)
00204 void mTouch_UpdateMatrixOutput (void);
00205 #endif
00206 #if defined(MTOUCH_ERROR_DETECTION_ENABLED)
00207 void mTouch_ErrorDetect (void);
00208 #endif
00209
00210
00211 #if (MTOUCH_NUMBER_SENSORS > 1) && ((defined(MTOUCH_PROXIMITY_ENABLED) && (MTOUCH_NUMBER_SENSORS != MTOUCH_NUMBER_PROXIMITY)))
00212 uint8_t (*const mTouch_StateMachine [MTOUCH_NUMBER_SENSORS])(void) = {
00213 MTOUCH_STATEMACHINE_0
00214 MTOUCH_STATEMACHINE_1
00215 MTOUCH_STATEMACHINE_2
00216 MTOUCH_STATEMACHINE_3
00217 MTOUCH_STATEMACHINE_4
00218 MTOUCH_STATEMACHINE_5
00219 MTOUCH_STATEMACHINE_6
00220 MTOUCH_STATEMACHINE_7
00221 MTOUCH_STATEMACHINE_8
00222 MTOUCH_STATEMACHINE_9
00223 MTOUCH_STATEMACHINE_10
00224 MTOUCH_STATEMACHINE_11
00225 MTOUCH_STATEMACHINE_12
00226 MTOUCH_STATEMACHINE_13
00227 MTOUCH_STATEMACHINE_14
00228 MTOUCH_STATEMACHINE_15
00229 MTOUCH_STATEMACHINE_16
00230 MTOUCH_STATEMACHINE_17
00231 MTOUCH_STATEMACHINE_18
00232 MTOUCH_STATEMACHINE_19
00233 MTOUCH_STATEMACHINE_20
00234 MTOUCH_STATEMACHINE_21
00235 MTOUCH_STATEMACHINE_22
00236 MTOUCH_STATEMACHINE_23
00237 MTOUCH_STATEMACHINE_24
00238 MTOUCH_STATEMACHINE_25
00239 MTOUCH_STATEMACHINE_26
00240 MTOUCH_STATEMACHINE_27
00241 MTOUCH_STATEMACHINE_28
00242 MTOUCH_STATEMACHINE_29
00243 };
00244 #endif
00245
00246
00247
00248
00249
00250
00251
00252
00253
00258 void mTouch_Init(void)
00259 {
00260 #if defined(MTOUCH_EEPROM_ENABLED)
00261 mTouch_EEPROM_Init();
00262 #endif
00263
00267 #if (MTOUCH_NUMBER_SENSORS == 1)
00268 #define sensorIndex 0 // If only one sensor is implemented, don't implement
00269
00270
00271 #else
00272
00273
00274
00275
00276
00277 for (int8_t sensorIndex = MTOUCH_NUMBER_SENSORS-1; sensorIndex >= 0; sensorIndex--)
00278 {
00279 #endif
00280
00281 mTouch_AcquisitionData* acqData = &mTouch_acqData[sensorIndex];
00282 (*acqData).result.v &= 0x000F;
00283 (*acqData).result.v |= (uint16_t)(0x47F << 4);
00284
00285 mTouch_stateVars.sensor[sensorIndex].state = MTOUCH_INITIALIZING;
00286 #if defined(MTOUCH_BUTTON_TIMEOUT) && (MTOUCH_BUTTON_TIMEOUT > 0) // (Only if the press timer has been enabled)
00287 mTouch_stateVars.sensor[sensorIndex].timeout = 0;
00288 #endif
00289 mTouch_stateVars.sensor[sensorIndex].timer = MTOUCH_POWER_UP_SAMPLES;
00290 mTouch_stateVars.sensor[sensorIndex].baselineCount = MTOUCH_BASELINE_RATE_VALUE;
00291 #if defined(MTOUCH_STATE_DEBOUNCE)
00292 mTouch_stateVars.sensor[sensorIndex].debounce = 0;
00293 #endif
00294
00295 mTouch_average [sensorIndex] = 0;
00296 #if defined(MTOUCH_SLIDERS_ENABLED) || defined(MTOUCH_MOST_PRESSED_ONLY) || defined(MTOUCH_MATRIX_ENABLED)
00297 mTouch_deltas [sensorIndex] = 0;
00298 #endif
00299
00300 #if (MTOUCH_NUMBER_SENSORS == 1)
00301 #undef sensorIndex // Turn off the text replacement on 'sensorIndex'
00302 #else
00303 }
00304 #endif
00305
00306 #if defined(MTOUCH_TOGGLE_ENABLED)
00307 mTouch_toggle = 0;
00308 #endif
00309
00311 #if defined(MTOUCH_SLIDERS_ENABLED)
00312 #if (MTOUCH_NUMBER_OF_SLIDERS == 1)
00313 mTouch_slider[0] = 0;
00314 #elif (MTOUCH_NUMBER_OF_SLIDERS == 2)
00315 mTouch_slider[0] = 0;
00316 mTouch_slider[1] = 0;
00317 #else
00318 for (int8_t i = MTOUCH_NUMBER_OF_SLIDERS-1; i >= 0; i--)
00319 {
00320 mTouch_slider [i] = 0;
00321 }
00322 #endif
00323 #endif
00324
00326 #if MTOUCH_NUM_MODES > 1
00327 mTouch_state.skippedDecode = 0;
00328 mTouch_state.allReleased = 0;
00329 mTouch_state.justChanged = 0;
00330 #endif
00331
00332 mTouch_state.dataReady = 0;
00333 mTouch_state.areInitialized = 0;
00334
00336 #if defined(MCOMM_ENABLED)
00337 mTouch_stateMask = 0;
00338 #endif
00339
00340 #if defined(MTOUCH_ACTIVE_RUNS_AS_MAINLOOP)
00341 mTouch_state.isrServiced = 0;
00342 mTouch_state.isRepeatScan = 0;
00343 #endif
00344
00346 #if defined(CVD_GUARD_DACOUT_ENABLED)
00347 PIC_DACOUT_LAT_C = 0;
00348 PIC_DACOUT_TRIS_C = 0;
00349 #endif
00350
00351 #if defined(MTOUCH_PROXIMITY_ENABLED)
00352 mTouch_ProxInit();
00353 #endif
00354
00355 #if MTOUCH_NUM_MODES > 1
00356 mTouch_ChangeMode(0);
00357 #else
00358 mTouch_ChangeMode();
00359 #endif
00360
00362 MTOUCH_ADC_INIT();
00363
00365 #if MTOUCH_ISR_TIMER != 0
00366 PEIE = 1;
00367 #endif
00368 mTouch_EnableScanning();
00370
00371 }
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00391 void mTouch_Decode(void)
00392 {
00393
00394
00395
00396
00397 mTouch_DisableScanning();
00398
00399
00400 mTouch_state.areInitialized = 1;
00401 mTouch_state.buttonStateChange = 0;
00402 #if MTOUCH_NUM_MODES > 1
00403 mTouch_state.allReleased = 1;
00404 #endif
00405
00406 #if MTOUCH_NUM_MODES > 1
00407 if (mTouch_state.justChanged == 1)
00408 {
00409 mTouch_state.justChanged = 0;
00410 mTouch_state.skippedDecode = 1;
00411 mTouch_EnableScanning();
00412 return;
00413 }
00414 mTouch_state.skippedDecode = 0;
00415 #endif
00416
00417
00418 #if MTOUCH_NUM_MODES > 1
00419 mTouch_currentScan = mTouch_mode[mTouch_modeIndex];
00420 #else
00421 #if MTOUCH_NUMBER_SENSORS > 1
00422 mTouch_currentScan = 0;
00423 #endif
00424 #endif
00425
00426
00427 #if defined(MTOUCH_REQUIRES_DELTA_ARRAY)
00428 do
00429 {
00436 int16_t delta = mTouch_GetSensor(MTOUCH_CURRENTSCAN_VALUE) - mTouch_GetAverage(MTOUCH_CURRENTSCAN_VALUE);
00437
00438 if (delta < 0)
00439 {
00440 delta = 0;
00441 }
00442 mTouch_deltas[MTOUCH_CURRENTSCAN_VALUE] = (uint16_t) delta;
00443
00444 mTouch_currentScan++;
00445 } while (MTOUCH_CURRENTSCAN_VALUE != MTOUCH_NUMBER_SENSORS);
00446 #endif
00447
00448
00449 #if defined(MTOUCH_MATRIX_ENABLED)
00450
00451 #if MTOUCH_NUM_MODES > 1
00452
00453
00454 #endif
00455
00456 mTouch_FindMostPressed(MTOUCH_MATRIX_ROW_INDEX);
00457 mTouch_FindMostPressed(MTOUCH_MATRIX_COL_INDEX);
00458 #endif
00459
00460
00461 #if defined(MTOUCH_MOST_PRESSED_ONLY) && !defined(MTOUCH_MATRIX_SENSORS_ONLY)
00462
00463 #if MTOUCH_NUM_MODES > 1
00464
00465
00466 #endif
00467
00468 mTouch_FindMostPressed(MTOUCH_MOST_PRESSED_INDEX);
00469 #endif
00470
00471
00472
00473
00474 #if MTOUCH_NUM_MODES > 1
00475 mTouch_currentScan = mTouch_mode[mTouch_modeIndex];
00476 #else
00477 #if MTOUCH_NUMBER_SENSORS > 1
00478 mTouch_currentScan = 0;
00479 #endif
00480 #endif
00481
00482 #if MTOUCH_NUMBER_SENSORS > 1
00483 do
00484 {
00485 #if defined(MTOUCH_PROXIMITY_ENABLED) && (MTOUCH_NUMBER_SENSORS != MTOUCH_NUMBER_PROXIMITY)
00486 if (mTouch_StateMachine[MTOUCH_CURRENTSCAN_VALUE]())
00487 #elif defined(MTOUCH_PROXIMITY_ENABLED)
00488 if (mTouch_ProxStateMachine())
00489 #else
00490 if (mTouch_ButtonStateMachine())
00491 #endif
00492 #else
00493 if ((*MTOUCH_STATEMACHINE_0)())
00494 #endif
00495 {
00496 mTouch_UpdateBaseline();
00497 }
00498
00499 #if MTOUCH_NUMBER_SENSORS > 1
00500 mTouch_currentScan++;
00501 } while (MTOUCH_CURRENTSCAN_VALUE != MTOUCH_NUMBER_SENSORS);
00502 #endif
00503
00504
00505
00506
00507
00512 #if defined(MTOUCH_SLIDERS_ENABLED)
00513 mTouch_SliderDecode();
00514 #endif
00515
00516 #if defined(MTOUCH_MATRIX_ENABLED)
00517 mTouch_UpdateMatrixOutput();
00518 #endif
00519
00520 #if defined(MCOMM_ENABLED)
00521 if (mTouch_state.buttonStateChange)
00522 {
00523 mTouch_stateMask = 0;
00524
00525 #if MTOUCH_NUMBER_SENSORS > 1
00526 for (int8_t i = MTOUCH_NUMBER_SENSORS-1; i >= 0; i--)
00527 {
00528 if (mTouch_GetButtonState(i) == MTOUCH_PRESSED)
00529 {
00530 mTouch_stateMask |= 1 << i;
00531 }
00532 }
00533 #else
00534 if (mTouch_GetButtonState(0) == MTOUCH_PRESSED)
00535 mTouch_stateMask = 1;
00536 #endif
00537 }
00538 #endif
00539
00544 #if defined(MCOMM_ENABLED) && defined(MCOMM_UART_IMPLEMENTED)
00545 if (mTouch_state.areInitialized)
00546 {
00547 #if MTOUCH_NUM_MODES > 1
00548 if (mTouch_state.change == 0)
00549 {
00550 #endif
00551
00552 #if defined(MCOMM_TWO_WAY_ENABLED) && defined(MCOMM_ENABLE_STREAM)
00553 mComm_streamConfig.go = 1;
00554 #endif
00555
00556 mComm_Service();
00557
00558 #if MTOUCH_NUM_MODES > 1
00559 }
00560 #endif
00561 }
00562 #endif
00563
00564 #if MTOUCH_NUM_MODES > 1
00565 mTouch_ChangeMode(mTouch_modeIndex);
00566 #else
00567 mTouch_ChangeMode();
00568 #endif
00569
00573 mTouch_EnableScanning();
00574
00575 }
00576
00577
00578
00579 uint8_t mTouch_ButtonStateMachine(void)
00580 {
00581 #if MTOUCH_NUMBER_SENSORS == 1
00582
00583 #define MTOUCH_SENSOR_state mTouch_stateVars.sensor[0].state
00584 #define MTOUCH_SENSOR_timer mTouch_stateVars.sensor[0].timer
00585 #define MTOUCH_SENSOR_debounce mTouch_stateVars.sensor[0].debounce
00586 #define MTOUCH_SENSOR_timeout mTouch_stateVars.sensor[0].timeout
00587
00588 #else
00589
00590 #define MTOUCH_SENSOR_state (*sensor).state
00591 #define MTOUCH_SENSOR_timer (*sensor).timer
00592 #define MTOUCH_SENSOR_debounce (*sensor).debounce
00593 #define MTOUCH_SENSOR_timeout (*sensor).timeout
00594
00595 mTouch_SensorVariables* sensor = &mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE];
00596
00597 #endif
00598
00599
00600 #if defined(MTOUCH_REQUIRES_DELTA_ARRAY)
00601 uint16_t delta = mTouch_deltas[MTOUCH_CURRENTSCAN_VALUE];
00602 #else
00603 uint16_t delta = mTouch_GetSensor(MTOUCH_CURRENTSCAN_VALUE) - mTouch_GetAverage(MTOUCH_CURRENTSCAN_VALUE);
00604
00605 if ((int16_t) delta < 0)
00606 {
00607 delta = 0;
00608 }
00609 #endif
00610
00611
00612 #if defined(MTOUCH_MOST_PRESSED_ONLY) || defined(MTOUCH_MATRIX_ENABLED)
00613 #if defined(MTOUCH_MATRIX_ENABLED) // If matrix is enabled,
00614 #if !defined(MTOUCH_MATRIX_SENSORS_ONLY) // and there are more than just row and column sensors,
00615 #if defined(MTOUCH_MATRIX_DEFS_AT_START) // and the matrix row/columns come BEFORE normal sensors...
00616 if (MTOUCH_CURRENTSCAN_VALUE < MTOUCH_SENSOR_START)
00617 #else // and the matrix row/columns come AFTER normal sensors...
00618 if (MTOUCH_CURRENTSCAN_VALUE > MTOUCH_SENSOR_END)
00619 #endif
00620 {
00621 #endif // If this sensor is part of the matrix,
00622 #if defined(MTOUCH_MATRIX_ROWS_FIRST) // and rows are defined first...
00623 if (MTOUCH_CURRENTSCAN_VALUE < MTOUCH_MATRIX_COLUMN_START)
00624 #else // and columns are defined first...
00625 if (MTOUCH_CURRENTSCAN_VALUE > MTOUCH_MATRIX_COLUMN_END)
00626 #endif
00627 {
00628 mTouch_mostPressed = &mTouch_mostPressedData[MTOUCH_MATRIX_ROW_INDEX];
00629 }
00630 else
00631 {
00632 mTouch_mostPressed = &mTouch_mostPressedData[MTOUCH_MATRIX_COL_INDEX];
00633 }
00634 #if !defined(MTOUCH_MATRIX_SENSORS_ONLY) // If there are other sensors besides those implementing
00635 }
00636 else
00637 {
00638 #if defined(MTOUCH_MOST_PRESSED_ONLY)
00639 mTouch_mostPressed = &mTouch_mostPressedData[MTOUCH_MOST_PRESSED_INDEX];
00640 #else
00641
00642
00643
00644
00645
00646 mTouch_mostPressed = &mTouch_mostPressedData[MTOUCH_DUMMY_MOST_PRESSED_INDEX];
00647 (*mTouch_mostPressed).maxIndex = MTOUCH_CURRENTSCAN_VALUE;
00648 (*mTouch_mostPressed).maxValue = delta;
00649 #endif
00650 }
00651 #endif
00652 #else // If there isn't a matrix being implemented, most-pressed must be.
00653 mTouch_mostPressed = &mTouch_mostPressedData[MTOUCH_MOST_PRESSED_INDEX];
00654 #endif
00655 #endif
00656
00657 switch(MTOUCH_SENSOR_state)
00658 {
00671 case MTOUCH_INITIALIZING:
00672 {
00673 if (--(MTOUCH_SENSOR_timer) <= 0)
00674 {
00675
00676
00677
00678
00679 MTOUCH_SENSOR_state = MTOUCH_RELEASED;
00680
00681 mTouch_average[MTOUCH_CURRENTSCAN_VALUE] = mTouch_GetSensor(MTOUCH_CURRENTSCAN_VALUE);
00682
00683 #if defined(MTOUCH_STATE_DEBOUNCE) // Initialize sensor's debounce counter
00684 MTOUCH_SENSOR_debounce = MTOUCH_pDEBOUNCE_VALUE;
00685 #endif
00686
00687 }
00688 else
00689 {
00690
00691 mTouch_state.areInitialized = 0;
00692
00693 #if MTOUCH_NUM_MODES > 1
00694 mTouch_state.allReleased = 0;
00695 #endif
00696
00697 }
00698
00699 return 0;
00700 }
00701
00722 case MTOUCH_RELEASED:
00723 {
00724
00725 if(delta > (int16_t)mTouch_GetPressThreshold(MTOUCH_CURRENTSCAN_VALUE))
00726 {
00727
00728 #if defined(MTOUCH_MOST_PRESSED_ONLY) || defined(MTOUCH_MATRIX_ENABLED)
00729
00730
00731
00732 if ((MTOUCH_CURRENTSCAN_VALUE == (*mTouch_mostPressed).maxIndex) &&
00733 ((*mTouch_mostPressed).maxValue > MTOUCH_MOST_PRESSED_THRESHOLD))
00734 {
00735 #endif
00736
00737 #if defined(MTOUCH_STATE_DEBOUNCE)
00738 if (MTOUCH_SENSOR_debounce == 0)
00739 {
00740 MTOUCH_SENSOR_debounce = MTOUCH_rDEBOUNCE_VALUE;
00741 #endif
00742
00743
00744
00745
00746 MTOUCH_SENSOR_state = MTOUCH_PRESSED;
00747
00748 #if defined(MTOUCH_BUTTON_TIMEOUT) // If the press timer is enabled
00749 #if MTOUCH_BUTTON_TIMEOUT > 0
00750 MTOUCH_SENSOR_timer = MTOUCH_pTIMEOUT_VALUE;
00751 #endif // NOTE: This delay determined by
00752 #endif // MTOUCH_BUTTON_TIMEOUT
00753
00754 #if MTOUCH_NUM_MODES > 1
00755 mTouch_state.allReleased = 0;
00756 #endif
00757
00758 #if defined(MTOUCH_TOGGLE_ENABLED)
00759 mTouch_ToggleToggle(MTOUCH_CURRENTSCAN_VALUE);
00760 #endif
00761
00762 mTouch_state.buttonStateChange = 1;
00763
00764
00765 #if defined(MTOUCH_STATE_DEBOUNCE)
00766 }
00767 else
00768 {
00769 MTOUCH_SENSOR_debounce--;
00770 }
00771 #endif
00772
00773 #if defined(MTOUCH_MOST_PRESSED_ONLY) || defined(MTOUCH_MATRIX_ENABLED)
00774 }
00775 else
00776 {
00777 #if defined(MTOUCH_STATE_DEBOUNCE)
00778 MTOUCH_SENSOR_debounce = MTOUCH_pDEBOUNCE_VALUE;
00779 #endif
00780 }
00781 #endif
00782
00783 }
00784 else
00785 {
00786 #if defined(MTOUCH_STATE_DEBOUNCE)
00787 MTOUCH_SENSOR_debounce = MTOUCH_pDEBOUNCE_VALUE;
00788 #endif
00789
00790
00791
00792
00793
00794
00795 return 1;
00796 }
00797 return 0;
00798 }
00799
00831 case MTOUCH_PRESSED:
00832 {
00833
00834 #if defined(MTOUCH_BUTTON_TIMEOUT) // (Only if the press timer has been enabled)
00835 #if MTOUCH_BUTTON_TIMEOUT > 0
00836
00837 if (--(MTOUCH_SENSOR_timer) <= 0)
00838 {
00839
00840
00841
00842 MTOUCH_SENSOR_state = MTOUCH_RELEASED;
00843 MTOUCH_SENSOR_timeout = 1;
00844
00845 mTouch_average[MTOUCH_CURRENTSCAN_VALUE] = mTouch_GetSensor(MTOUCH_CURRENTSCAN_VALUE);
00846
00847 #if defined(MTOUCH_STATE_DEBOUNCE)
00848 MTOUCH_SENSOR_debounce = MTOUCH_pDEBOUNCE_VALUE;
00849 #endif // and check if it has reached 0.
00850
00851 mTouch_state.buttonStateChange = 1;
00852
00853
00854 break;
00855 }
00856
00857 #endif
00858 #endif
00859
00860 #if !defined(MTOUCH_MOST_PRESSED_ONLY) && !defined(MTOUCH_MATRIX_ENABLED)
00861
00862 if (delta < (int16_t)mTouch_GetReleaseThreshold(MTOUCH_CURRENTSCAN_VALUE))
00863 #else // Otherwise,
00864 if ((delta < (int16_t)mTouch_GetReleaseThreshold(MTOUCH_CURRENTSCAN_VALUE)) ||
00865 (MTOUCH_CURRENTSCAN_VALUE != (*mTouch_mostPressed).maxIndex) ||
00866 ((*mTouch_mostPressed).maxValue < MTOUCH_MOST_PRESSED_THRESHOLD) )
00867 #endif
00868 {
00869
00870 #if defined(MTOUCH_STATE_DEBOUNCE)
00871 if (MTOUCH_SENSOR_debounce == 0)
00872 {
00873 MTOUCH_SENSOR_debounce = MTOUCH_pDEBOUNCE_VALUE;
00874 #endif
00875
00876
00877
00878
00879 MTOUCH_SENSOR_state = MTOUCH_RELEASED;
00880
00881 mTouch_state.buttonStateChange = 1;
00882
00883
00884 #if defined(MTOUCH_STATE_DEBOUNCE)
00885 } else {
00886
00887 MTOUCH_SENSOR_debounce--;
00888
00889 #if MTOUCH_NUM_MODES > 1
00890 mTouch_state.allReleased = 0;
00891 #endif
00892
00893 }
00894 #endif
00895
00896 }
00897 else
00898 {
00899
00900 #if defined(MTOUCH_STATE_DEBOUNCE)
00901 MTOUCH_SENSOR_debounce = MTOUCH_rDEBOUNCE_VALUE;
00902 #endif
00903 #if MTOUCH_NUM_MODES > 1
00904 mTouch_state.allReleased = 0;
00905 #endif
00906
00907 }
00908 return 0;
00909 }
00910
00911 default: break;
00912
00913 }
00914 return 0;
00915 }
00916
00917 #if defined(MTOUCH_MOST_PRESSED_ONLY) || defined(MTOUCH_MATRIX_ENABLED)
00918
00919
00920
00921
00922
00923
00924
00925
00926 void mTouch_FindMostPressed(uint8_t index)
00927 {
00928
00929
00930
00931 mTouch_mostPressed = &mTouch_mostPressedData[index];
00932 (*mTouch_mostPressed).maxIndex = 0;
00933 (*mTouch_mostPressed).maxValue = 0;
00934 uint16_t secValue = 0;
00935
00936
00937 for(int8_t i = mTouch_mostPressedIndex[index].end; i >= mTouch_mostPressedIndex[index].start; i--)
00938 {
00939 if (mTouch_deltas[i] > (*mTouch_mostPressed).maxValue)
00940 {
00941 (*mTouch_mostPressed).maxValue = mTouch_deltas[i];
00942 (*mTouch_mostPressed).maxIndex = i;
00943 }
00944 }
00945
00946
00947 for(int8_t i = mTouch_mostPressedIndex[index].end; i >= mTouch_mostPressedIndex[index].start; i--)
00948 {
00949 if (i != (*mTouch_mostPressed).maxIndex)
00950 {
00951 if (mTouch_deltas[i] > secValue)
00952 {
00953 secValue = mTouch_deltas[i];
00954 }
00955 }
00956 }
00957
00958
00959
00960 (*mTouch_mostPressed).maxValue = (*mTouch_mostPressed).maxValue - secValue;
00961 }
00962 #endif
00963
00964 #if defined(MTOUCH_MATRIX_ENABLED)
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974 void mTouch_UpdateMatrixOutput(void)
00975 {
00976 mTouch_matrix = 0xFF;
00977
00978 for(int8_t rowIndex = MTOUCH_MATRIX_ROW_END;
00979 rowIndex >= MTOUCH_MATRIX_ROW_START;
00980 rowIndex--)
00981 {
00982 if (mTouch_GetButtonState(rowIndex) == MTOUCH_PRESSED)
00983 {
00984 for(int8_t colIndex = MTOUCH_MATRIX_COLUMN_END;
00985 colIndex >= MTOUCH_MATRIX_COLUMN_START;
00986 colIndex--)
00987 {
00988 if (mTouch_GetButtonState(colIndex) == MTOUCH_PRESSED)
00989 {
00990 mTouch_matrix = colIndex - MTOUCH_MATRIX_COLUMN_START;
00991 mTouch_matrix <<= MTOUCH_MATRIX_ROW_OFFSET;
00992 mTouch_matrix += rowIndex - MTOUCH_MATRIX_ROW_START;
00993 break;
00994 }
00995 }
00996 break;
00997 }
00998 }
00999 }
01000 #endif
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01021 void mTouch_UpdateBaseline(void)
01022 {
01023 uint16_t reading = mTouch_GetSensor(MTOUCH_CURRENTSCAN_VALUE);
01038 #if (MTOUCH_BUTTON_TIMEOUT > 0)
01039 if (mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE].timeout)
01040 {
01041 if ((int16_t)(mTouch_average[MTOUCH_CURRENTSCAN_VALUE] - reading) > (int16_t)(mTouch_GetPressThreshold(MTOUCH_CURRENTSCAN_VALUE) >> 1))
01042 {
01043 mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE].baselineCorrection = 1;
01044 mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE].timeout = 0;
01045 }
01046 }
01047 #endif
01048
01049 if (mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE].baselineCorrection)
01050 {
01051 mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE].baselineCount = 1;
01052
01053 if ((int16_t)(mTouch_average[MTOUCH_CURRENTSCAN_VALUE] - reading) < (int16_t)(mTouch_GetReleaseThreshold(MTOUCH_CURRENTSCAN_VALUE)))
01054 {
01055 mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE].baselineCorrection = 0;
01056 }
01057 }
01058
01059 #if defined(MTOUCH_BLCOUNT_16BITS_REQUIRED) // If the baseline counter has been exceeded
01060 if ((uint16_t)(--(mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE].baselineCount)) == 0)
01061 #else
01062 if ((uint8_t)(--(mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE].baselineCount)) == 0)
01063 #endif
01064 {
01065 mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE].baselineCount = MTOUCH_BASELINE_RATE_VALUE;
01066
01067 #if defined(MTOUCH_EEPROM_ENABLED)
01068 if (MTOUCH_BASELINE_WEIGHT_VALUE == 1)
01069 {
01070 #endif
01071
01072 #if defined(MTOUCH_EEPROM_ENABLED) || (MTOUCH_BASELINE_WEIGHT == 1)
01073 mTouch_average[MTOUCH_CURRENTSCAN_VALUE] += reading;
01074 mTouch_average[MTOUCH_CURRENTSCAN_VALUE] >>= 1;
01075 #endif
01076
01077 #if defined(MTOUCH_EEPROM_ENABLED)
01078 }
01079 else
01080 {
01081 #endif
01082
01083 #if defined(MTOUCH_EEPROM_ENABLED) || (MTOUCH_BASELINE_WEIGHT != 1)
01084 mTouch_average[MTOUCH_CURRENTSCAN_VALUE] -= (uint16_t)(mTouch_average[MTOUCH_CURRENTSCAN_VALUE] >> MTOUCH_BASELINE_WEIGHT_VALUE);
01085 mTouch_average[MTOUCH_CURRENTSCAN_VALUE] += (uint16_t)(reading >> MTOUCH_BASELINE_WEIGHT_VALUE);
01086 #endif
01087
01088 #if defined(MTOUCH_EEPROM_ENABLED)
01089 }
01090 #endif
01091
01092 }
01094 }
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105 #if defined(MTOUCH_ERROR_DETECTION_ENABLED)
01106 void mTouch_ErrorDetect(void)
01107 {
01108 mTouch_DisableScanning();
01109 MTOUCH_GENERATE_SHORTCHECKS();
01110 mTouch_EnableScanning();
01111 }
01112 #endif