The mTouch Framework supports decoding of matrix'd sensors to simplify the user's design process. This method allows for a larger number of possible unique press locations by decoding the state of two sensors at once: the row and the column.
The mTouch Framework will scan all sensors as normal. When a new set of readings is available, the framework will first perform some logic to determine which row and which column is the most pressed of the matrix. If both a row and column are activated, the state machine for the sensors will begin debouncing the result. After the required number of repeated results, the matrix coordinate-output will be updated.
Sensors in a Matrix Configuration
// Example sensor configuration for matrix #define MTOUCH_SENSOR0 AN0 // Matrix Row0 #define MTOUCH_SENSOR1 AN1 // Matrix Row1 #define MTOUCH_SENSOR2 AN2 // Matrix Col0 #define MTOUCH_SENSOR3 AN3 // Matrix Col1 #define MTOUCH_SENSOR4 AN4 // Matrix Col2 #define MTOUCH_SENSOR5 AN5 // Normal mTouch Sensor #define MTOUCH_SENSOR6 AN6 // Normal mTouch Sensor
// Example sensor configuration for matrix #define MTOUCH_SENSOR0 AN0 // Normal mTouch Sensor #define MTOUCH_SENSOR1 AN1 // Normal mTouch Sensor #define MTOUCH_SENSOR2 AN2 // Matrix Col0 #define MTOUCH_SENSOR3 AN3 // Matrix Col1 #define MTOUCH_SENSOR4 AN4 // Matrix Col2 #define MTOUCH_SENSOR5 AN5 // Matrix Row0 #define MTOUCH_SENSOR6 AN6 // Matrix Row1
#define MTOUCH_MATRIX_ENABLED // <-- Matrix decoding is enabled #define MTOUCH_MATRIX_ROW_START 0 // <-- The sensor index of the first row #define MTOUCH_MATRIX_ROW_END 1 // <-- The sensor index of the last row #define MTOUCH_MATRIX_COLUMN_START 2 // <-- The sensor index of the first column #define MTOUCH_MATRIX_COLUMN_END 4 // <-- The sensor index of the last column #define MTOUCH_MOST_PRESSED_THRESHOLD 10 // <-- The most pressed column/row must be at least 10 counts // greater than the second-most-pressed column/row.
#define MTOUCH_MATRIX_ENABLED // <-- Matrix decoding is enabled #define MTOUCH_MATRIX_ROW_START 2 // <-- The sensor index of the first row #define MTOUCH_MATRIX_ROW_END 3 // <-- The sensor index of the last row #define MTOUCH_MATRIX_COLUMN_START 4 // <-- The sensor index of the first column #define MTOUCH_MATRIX_COLUMN_END 6 // <-- The sensor index of the last column #define MTOUCH_MOST_PRESSED_THRESHOLD 10 // <-- The most pressed column/row must be at least 10 counts // greater than the second-most-pressed column/row.
The mTouch Framework stores the column and row output coordinates in an 8-bit value to save on RAM requirements. To easily decode which row and column is pressed, use the mTouch_Matrix_getRow() and mTouch_Matrix_getColumn() API functions. They will return the row and column indexes, respectively.
Note: The index they will be returning is relative to the matrix rows and matrix columns - not on the MTOUCH_SENSORx index value. For example, in the two example MTOUCH_SENSORx configurations above, pressing on Row0's sensor will make mTouch_Matrix_getRow() return '0' - regardless of whether the row is defined as MTOUCH_SENSOR0 or MTOUCH_SENSOR5.
mTouch_Matrix_hasChanged() will return a '0' if there was no change in the matrix output value since the last decode. mTouch_Matrix_latchNewValue() will save the current value of the matrix for later comparison. mTouch_Matrix_isPressed() will return a '0' if there are no pressed nodes on the matrix.
if (mTouch_Matrix_hasChanged()) // If the matrix value has changed since last time { mTouch_Matrix_latchNewValue(); // Save the new value of the matrix LED1 = LED_OFF; // Reset all matrix outputs LED2 = LED_OFF; LED3 = LED_OFF; LED4 = LED_OFF; LED5 = LED_OFF; LED6 = LED_OFF; LED7 = LED_OFF; LED8 = LED_OFF; LED9 = LED_OFF; LED10 = LED_OFF; LED11 = LED_OFF; LED12 = LED_OFF; } if (mTouch_Matrix_isPressed()) // If the matrix is being pressed { switch(mTouch_Matrix_getRow()) // Find the row/column being pressed and output accordingly. { case 0: switch(mTouch_Matrix_getColumn()) { case 0: LED1 = LED_ON; break; // (0, 0) case 1: LED2 = LED_ON; break; // (0, 1) case 2: LED3 = LED_ON; break; // (0, 2) case 3: LED4 = LED_ON; break; // (0, 3) } break; case 1: switch(mTouch_Matrix_getColumn()) { case 0: LED5 = LED_ON; break; // (1, 0) case 1: LED6 = LED_ON; break; // (1, 1) case 2: LED7 = LED_ON; break; // (1, 2) case 3: LED8 = LED_ON; break; // (1, 3) } break; case 2: switch(mTouch_Matrix_getColumn()) { case 0: LED9 = LED_ON; break; // (2, 0) case 1: LED10 = LED_ON; break; // (2, 1) case 2: LED11 = LED_ON; break; // (2, 2) case 3: LED12 = LED_ON; break; // (2, 3) } break; } }