Matrix Designs

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.

How It Works

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.

matrix.JPG

Sensors in a Matrix Configuration

Proximity Sensor Configuration

  1. Configure the matrix'd sensors to be scanned as normal sensors in the mTouch_config.h file. We will be defining index ranges for the rows and columns, so when configuring the MTOUCH_SENSORx list, make sure to place the matrix sensors as a continuous group starting at 0 or place at the very end with no 'normal' sensors after it. Within the matrix list, the rows should be grouped together and the columns should be grouped together.
     // 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
    
  2. At the bottom of mTouch_config.h is the 'Decoding' section which contains the matrix configuration options.
    1. Uncomment MTOUCH_MATRIX_ENABLED to enable matrix decoding.
    2. Define MTOUCH_MATRIX_ROW_START and MTOUCH_MATRIX_ROW_END to the MTOUCH_SENSORx index values for the row sensors. In the first example, above, the start would be '0' and the end would be '1'. In the second example, the start would be '5' and the end would be '6'.
    3. Define MTOUCH_MATRIX_COLUMN_START and MTOUCH_MATRIX_COLUMN_END to the MTOUCH_SENSORx index values for the column sensors. In the first example, above, the start would be '2' and the end would be '4'. In the second example, the start would be '2' and the end would be '4'.
    4. Define one MTOUCH_SENSORx_IS_PROX definition per enabled proximity sensor. The value of the definition must be its 'proximity index' value. The 'Proximity index' is used as the index to proximity variable arrays. In total, the indexes should start at 0 and end with MTOUCH_NUMBER_PROXIMITY - 1. The order is arbitrary.
    5. If desired, set the value of MTOUCH_MOST_PRESSED_THRESHOLD to the number of counts that the most-pressed row or column must be greater than the second-most-pressed row/column to qualify it as the 'most pressed'.

Example

 #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.

Matrix Output

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.

Example Matrix API

 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;
     }
 }