H753_CPP_SPI_Resistive_Touch_Screen_Controller 1.0
STM32H753 SPI resistive touchscreen (Adafruit 333 overlay + 5767 TSC2046 controller) driver and tutorial
Loading...
Searching...
No Matches
TSC2046.hpp
1/*
2 * TSC2046.hpp
3 *
4 * STM32 HAL port of Adafruit's TSC2046 resistive touchscreen controller
5 * driver -- reads raw touch X/Y coordinates and pressure over SPI.
6 *
7 * Adapted from Adafruit_TSC2046
8 * (https://github.com/adafruit/Adafruit_TSC2046, MIT License, Copyright (c)
9 * 2023 Adafruit Industries, Copyright (c) 2023 Mikaela Szekely) as a design
10 * reference for the command byte layout, differential/single-ended read
11 * modes, and pressure formula -- this is an independent STM32 HAL
12 * implementation built on STM32BusIO, not a direct port of Adafruit's
13 * Arduino source.
14 *
15 * Created on: Sep 15, 2026
16 * Author: johng
17 */
18
19#ifndef TSC2046_TSC2046_HPP_
20#define TSC2046_TSC2046_HPP_
21
22#include "STM32SPIDevice.hpp"
23
38
68class TSC2046 {
69public:
77 TSC2046(SPI_HandleTypeDef *hspi, GPIO_TypeDef *csPort, uint16_t csPin);
78
87 void begin(uint32_t xPlateResistance = 400);
88
97 void setVRef(float vref);
98
105 void setTouchedThreshold(float touchedThresholdOhms);
106
113
120 bool isTouched();
121
128 void enableInterrupts(bool enable);
129
153 float readTemperatureC();
154
161 float readTemperatureF();
162
167 float readBatteryVoltage();
168
173 float readAuxiliaryVoltage();
174
175private:
176 uint16_t readCoord(uint8_t channelSelect);
177 uint16_t readExtra(uint8_t channelSelect);
178 float effectiveVRef();
179 float readTemperatureKelvin();
180 static uint16_t extractTwelveBitValue(uint32_t registerValue);
181
182 STM32SPIDevice _spiDevice;
183 uint32_t _xPlateResistance;
184 float _vref;
185 float _touchedThreshold;
186 bool _interruptsEnabled;
187};
188
189#endif /* TSC2046_TSC2046_HPP_ */
Wraps one SPI peripheral + chip-select pin into read/write/transfer operations, mirroring the hardwar...
STM32 HAL driver for a TI/Adafruit TSC2046 resistive touchscreen controller, reached over SPI via an ...
Definition TSC2046.hpp:68
float readTemperatureF()
Same reading as readTemperatureC(), converted to degrees Fahrenheit.
Definition TSC2046.cpp:156
void setVRef(float vref)
Sets the voltage connected to the TSC2046's VRef pin, if any.
Definition TSC2046.cpp:63
TSC2046(SPI_HandleTypeDef *hspi, GPIO_TypeDef *csPort, uint16_t csPin)
Constructs the driver and its underlying STM32SPIDevice. Does not touch hardware – call begin() befor...
Definition TSC2046.cpp:53
float readAuxiliaryVoltage()
Definition TSC2046.cpp:176
float readBatteryVoltage()
Definition TSC2046.cpp:170
void enableInterrupts(bool enable)
Enables or disables the TSC2046's own touch interrupt (IRQ pin pulled low on touch)....
Definition TSC2046.cpp:144
TSC2046TouchPoint getPoint()
Reads the current touch position and pressure.
Definition TSC2046.cpp:114
bool isTouched()
Determines if the touchscreen is currently being touched, based on the pressure value from getPoint()...
Definition TSC2046.cpp:139
void setTouchedThreshold(float touchedThresholdOhms)
Sets the pressure threshold used by isTouched().
Definition TSC2046.cpp:67
float readTemperatureC()
Reads the TSC2046's on-die temperature sensor, in degrees Celsius.
Definition TSC2046.cpp:152
void begin(uint32_t xPlateResistance=400)
Initializes the driver. Call once before any other method.
Definition TSC2046.cpp:58
One touch reading: raw X/Y position and a pressure-proportional resistance value.
Definition TSC2046.hpp:32
int16_t x
Raw 12-bit X coordinate (0-4095). Meaningless if not touched.
Definition TSC2046.hpp:33
int16_t y
Raw 12-bit Y coordinate (0-4095). Meaningless if not touched.
Definition TSC2046.hpp:34