Help > RITTLE FOR THE PIC32MZ MICROCONTROLLER > Specific Peripheral Modules > touch


touch (text_type/cmd [, … ] );

Enable support, initialise, and attach specified touch screen driver. If one with SPI is used, it occupies the same pins as the system SPI as shown in the pinout, plus two additional pins for TCS# and TIRQ# signals.


                                     SPI touch panel
--------------\                /------------------------\
           P4 |--- SYS_SCLK ---| SCLK (T_CLK in some)   |
           P5 |--- SYS_MISO ---| MISO (T_DO in some)    |
PIC32MZ    P6 |--- SYS_MOSI ---| MOSI (T_DIN in some)   |
          P46 |--- IRQ# -------| IRQ  (T_IRQ in some)   |
          P47 |--- CS# --------| CS   (T_CS in some)    |
--------------/                \------------------------/

The first parameter in the function specifies the model of the touch panel or a command to be executed by the touch panel. Commands should be sent only after the touch panel has been initialised.

Currently supported are:

The first text parameter for the function specifies the display controller model.

Currently supported display controllers are:

NULL”                      Null device. Any currently active touch panel is disconnected from the system and the CS# and TIRQ# pins are released.


 “RSPI*46”               Resistive panels served by controller such as XPT2046, TSC2046, ADS7846, ADS7843, or similar. These controllers use SPI interface and should be connected to the system SPI bus as shown above.


RRSPI*46”              Same as the above but with swapped X and Y axes on the touch panel. This is usually needed when the panel is used in landscape mode.


Once a touch panel has been initialised, the program can send commands to it by using the same function touch().


CALIBRATE”           Interactive calibration of the panel. This works only if a display driver is also initialised and active.

                                    The calibration shows sequentially four small white squares in every corner of the panel, and expects the user to touch in the square. Raw values are then taken from the touch controller and processed to produce two calibration values that are returned by the function.

                                    The specific format of the command is:

                                    int cx,cy = touch (“CALIBRATE”);


SETCAL”                  Set calibration values for touch panel. Once calibrated, the panel can be immediately restored to its calibrated state every time whenever that is needed (usually after system reset).

                                    The command takes two parameters and stores them in internal variables for further processing when the touch panel is used.

                                    The specific format of the command is:

                                    touch (“SETCAL”, int_cx, int_cy);


POINTS”                  Return the number of points currently being touched on the panel. Resistive panels can only have one point while capacitive panels can support one or more, sometimes as many as 10.

                                    The specific format of the command is:

                                    int p = touch (“POINTS”);                        ‘ return the number of

‘ currently touched points


READ”                     This is the main command that can read information from the touch panel, used in conjunction with “POINTS”.

Every time a “READ” command is executed it returns one currently touched point from the panel.

If the panel is calibrated, the returned coordinates are in pixels, otherwise they are raw values from the touch controller.

                                    The specific format of the command is:

                                    int x, y, p = touch (“READ”);


                                    Three integer values are returned – coordinate on the X-axis, coordinate on the Y-axis, and pressure. The pressure value is arbitrary and depends on the actual type of the touch panel.

                                    If there are no more active touch points to be read, the “READ” command will return values -1 for the X, Y, and pressure variables. At that point execution of another “POINTS” command will re-initialise the internal data stack with active points, if there are any.