Since | Origin / Contributor | Maintainer | Source |
---|---|---|---|
2017-03-09 | Starofall/Frank Exoo | Frank Exoo | xpt2046.c |
XPT2046 is a touch controller used by several cheap displays - often in combination with the ILI9341 display controller. The module is built based on the libraries of spapadim and PaulStoffregen.
Initiates the XPT2046 module to read touch values from the display. It is required to call spi.setup()
before calling xpt2046.init
(see example).
As the ucg lib also requires spi.setup()
to be called before it is important to only call it once in total and to activate spi.FULLDUPLEX
.
The clock_div
used in spi.setup()
should be 16 or higher, as lower values might produces inaccurate results.
xpt2046.init(cs_pin, irq_pin, height, width)
cs_pin
GPIO pin for csirq_pin
GPIO pin for irqheight
display height in pixelwidth
display width in pixel
nil
-- Setup spi with `clock_div` of 16 and spi.FULLDUPLEX
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 16,spi.FULLDUPLEX)
-- SETTING UP DISPLAY (using ucg module)
local disp = ucg.ili9341_18x240x320_hw_spi(8, 4, 0)
disp:begin(0)
-- SETTING UP TOUCH
xpt2046.init(2,1,320,240)
xpt2046.setCalibration(198, 1776, 1762, 273)
Sets the calibration of the display. Calibration values can be optained by using xpt2046.getRaw()
and read the values in the edges.
xpt2046.setCalibration(x1, y1, x2, y2)
x1
raw x value at top lefty1
raw y value at top leftx2
raw x value at bottom righty2
raw y value at bottom right
nil
Checks if the touch panel is touched.
xpt2046.isTouched()
true
if the display is touched, else false
if(xpt2046.isTouched()) then
local x, y = xpt2046.getPosition()
print(x .. "-" .. y)
end
Returns the position the display is touched using the calibration values and given width and height. Can be used in an interrupt pin callback to return the coordinates when the touch screen is touched.
xpt2046.getPosition()
returns both the x and the y position.
-- Setup spi with `clock_div` of 16 and spi.FULLDUPLEX
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 16,spi.FULLDUPLEX)
-- SETTING UP TOUCH
cs_pin = 2 -- GPIO4
irq_pin = 3 -- GPIO0
height = 240
width = 320
xpt2046.init(cs_pin, irq_pin, width, height)
xpt2046.setCalibration(198, 1776, 1762, 273)
gpio.mode(irq_pin,gpio.INT,gpio.PULLUP)
gpio.trig(irq_pin, "down", function()
print(xpt2046.getPosition())
end)
To create better measurements this function reads the position three times and averages the two positions with the least distance.
xpt2046.getPositionAvg()
returns both the x and the y position.
local x, y = xpt2046.getPositionAvg()
print(x .. "-" .. y)
Reads the raw value from the display. Useful for debugging and custom conversions.
xpt2046.getRaw()
returns both the x and the y position as a raw value.
local rawX, rawY = xpt2046.getRaw()
print(rawX .. "-" .. rawY)