By dave | May 15, 2021

Touch screen support that works on all Arduino and mbed boards

IoAbstraction includes support for touch screens. The support is built in layers to allow for different drivers to be added over time. However, at the moment there is support for resistive touch screens on analog pins. This library can work with such touch screens regardless of analog input resolution, as long as the device is supported by IoAbstraction.

As with switches and rotary encoder support, this component deals with debouncing the user input and also working out if the user has selected or held, therefore supporting drag operations.


Adding touch screen support to your project

In order to add touch screen support, first use your library manager to install IoAbstraction. Once this is done, you can either start with the touch screen example or just follow the instructions below.

Add the ResistiveTouchScreen include and create an instance

#include <ResistiveTouchScreen.h>

// This is the most basic extension of the touch screen class, it just allows
// you to read it's present values, there is also an event driven version.
ValueStoringResistiveTouchScreen touchScreen(XPOS_PIN, XNEG_PIN, YPOS_PIN, YNEG_PIN,
                                             TouchInterrogator::PORTRAIT);

Notes:

  • XNEG_PIN and YPOS_PIN must be on an ADC pin
  • Last parameter is one of PORTRAIT, PORTRAIT_INVERTED, LANDSCAPE, LANDSCAPE_INVERTED, RAW

During your app setup, provide calibration data and start the touch screen

// If calibration is needed, then you can provide the calibration in terms of
// screen edges here. Then we start
touchScreen.calibrateMinMaxValues(0.15F, 0.75F, 0.06F, 0.91F);
touchScreen.start();

Then you can read back the settings at any time later

You can read back the values at any time, it is split into three methods, one to determine the current touch status, another to get the X and Y coordinates.

// tell the encoder it is being used for scrolling and needs to be responsive
void setUsedForScrolling(bool scrolling)

// returns one of NOT_TOUCHED, TOUCHED, HELD, TOUCH_DEBOUNCE
TouchState getTouchState()

// return a float value between 0 and 1 to indicate screen position
getLastX() and getLastY()

Deal with touches in an event driven way

To do this instead of using the above ValueStoringTouchScreenManager you create your own class that extends from TouchScreenManager. See the Touch interface reference docs for more information.

Creating a new touch interrogator for new hardware

To do this you create an object that extends from TouchInterrogator, it must indicate touche status in the following method:

    virtual TouchState internalProcessTouch(
            float* ptrX, float* ptrY, TouchRotation rotation, 
            const CalibrationHandler& calib)=0;

See the Touch interface reference docs for more information.

Go back to the IoAbstraction page

Other pages within this category

comments powered by Disqus

This site uses cookies to analyse traffic, serve ads by Google AdSense (non-personalized in EEA/UK), and to record consent. We also embed Twitter, Youtube and Disqus content on some pages, these companies have their own privacy policies.

Our privacy policy applies to all pages on our site

Should you need further guidance on how to proceed: External link for information about cookie management.