The mTouch Framework scans all sensors in the same basic manner. All sensors (whether normal 'button', part of a slider or wheel, a single row in a matrix, or a proximity sensor) start by being scanned using the mTouch CVD waveform. Once all enabled sensors have a 'final' reading, the data ready flag is set and the decoding routine is called from the main loop.
#define MTOUCH_SENSOR0 AN0 // Sensor0 is connected to AN4
#define THRESHOLD_PRESS_SENSOR0 100 // Reading must be 100 counts > the baseline
First, we will need to set up the application to scan mTouch. To do this, we need to create a check in the main loop to service the mTouch decode function. If we are using mTouch as one of several ISRs, we also need to add the scanning call to our ISR logic.
void main(void) { myApp_Init(); // Your application's initialization (examples provided in main.c) mTouch_Init(); // mTouch initialization #if defined(MCOMM_ENABLED) mComm_Init(); // mComm initialization #endif INTCONbits.GIE = 1; // Initialization complete. Begin servicing interrupts. while(1) { if (mTouch_isDataReady()) // Is new information ready? { mTouch_Service(); // Decode the newly captured data and transmit new data updates. // Application sensor-state logic checks go here. } } }
void interrupt ISR(void) { SAVE_STATE(); // mTouch Framework-supplied general ISR save state macro. // Not required, but convenient. #if (MTOUCH_INTEGRATION_TYPE == MTOUCH_CALLED_FROM_ISR) if (mTouch_checkInterrupt()) // Checks if the TMRxIE and TMRxIF flags are both equal to 1. { mTouch_Scan(); // Required if running as ISR slave. The mTouch timer interrupt // flag is cleared inside the mTouch_Scan() function. } #elif (MTOUCH_INTEGRATION_TYPE == MTOUCH_CALLED_FROM_MAINLOOP) mTouch_state.isrServiced = 1; // Alerts the mTouch scanning routine that an interrupt may // have disrupted a scan. This is cleared at the start of a // new scan and is checked at the end of the scan. // Bad data can affect the readings if this flag is not set. #endif RESTORE_STATE(); // mTouch Framework-supplied general ISR restore state macro. // Not required, but convienent. }
The API for accessing button states is mTouch_GetButtonState(i). The states have been enumerated with the labels: MTOUCH_INITIALIZING, MTOUCH_RELEASED, and MTOUCH_PRESSED.
if (mTouch_GetButtonState(0) < MTOUCH_PRESSED) { LED0 = LED_OFF; // Sensor0 not pressed. Turn off LED. } else { LED0 = LED_ON; // Sensor0 pressed. Turn on LED. }