Adafruit TSC2046 Touchscreen Arduino Library
Adafruit_TSC2046.h
Go to the documentation of this file.
1 
150 #ifndef ADA_TSC2046_H
151 #define ADA_TSC2046_H
152 
153 #include "Arduino.h"
154 #include "Print.h"
155 #include <Adafruit_BusIO_Register.h>
156 #include <Adafruit_SPIDevice.h>
157 #include <stdint.h>
158 
164 class TSPoint {
165 public:
174  TSPoint(int16_t x, int16_t y, float z);
175 
180  int16_t x;
181 
186  int16_t y;
187 
194  float z;
195 
203  float xPercent();
204 
212  float yPercent();
213 };
214 
241 public:
242  ~Adafruit_TSC2046();
243 
275  void begin(int spiChipSelect, SPIClass *the_spi = &SPI,
276  uint32_t xResistance = 400,
277  uint32_t spiFrequency = 2L * 1000L * 1000L);
278 
297  void setVRef(float vRef);
298 
315  void setTouchedThreshold(float rTouchThreshold);
316 
326  TSPoint getPoint();
327 
337  bool isTouched();
338 
359  void enableInterrupts(bool enable);
360 
367  float readTemperatureC();
368 
375  float readTemperatureF();
376 
386  float readBatteryVoltage();
387 
407  float readAuxiliaryVoltage();
408 
421  float effectiveVRef();
422 
423 private:
424  SPIClass *_spi;
425  Adafruit_SPIDevice *_spiDev;
426  int _spiCS;
427  int64_t _spiFrequency;
428  uint32_t _xResistance;
429 
430  // NOTE(Qyriad): In my testing, the absolute most delicate touch where the X
431  // and Y coordinate values were not completely nonsensical had R_TOUCH at
432  // about 5.7kΩ (and even then the X and Y coordinates were still fairly
433  // off), and every complete false positive was above 100kΩ. To be on the
434  // safe side we'll use 100kΩ as the default threshold.
435  float _touchedThreshold = 100000.f;
436 
437  bool _interruptsEnabled = true;
438  float _vRef;
439 
440  float readTemperatureK();
441 
442  // Performs a 12-bit differential reference mode read,
443  // used for coordinate reads.
444  uint16_t readCoord(uint8_t channelSelect);
445 
446  // Performs a 12-bit single-ended reference mode read,
447  // used for non-coordinate reads.
448  uint16_t readExtra(uint8_t channelSelect);
449 
450  static uint16_t parse12BitValue(uint8_t spiUpperByte, uint8_t spiLowerByte);
451 };
452 
453 // NOTE: Bitfield structs like these are not generally portable as the union'd
454 // word can be different on machines of different endianness. Normally we would
455 // detect a big endian platform with #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__,
456 // and #error in that case. However, here, our struct is only 1 byte wide, so
457 // byte order shouldn't come into play.
459 union Command {
460  // NOTE: The order these are defined in is the opposite as they appear in the
461  // datasheet, because bitfield structs are least-significant-field first.
462  struct {
474  uint8_t enableOrIdleADC : 1;
475 
477  uint8_t enableInternalVRef : 1;
478 
486  uint8_t singleEndedRef : 1;
487 
489  uint8_t use8BitConv : 1;
490 
495  uint8_t addr : 3;
496 
498  uint8_t start : 1;
499  };
500  uint8_t word;
501 };
502 
503 #endif
Class for interfacing with a TSC2046 touchscreen controller.
Definition: Adafruit_TSC2046.h:240
float xPercent()
The X-coordinate as a percentage. Note that physical touchscreens vary, and the range of yours may no...
Definition: Adafruit_TSC2046.cpp:29
int16_t y
The full scale raw Y coordinate from the touchscreen. For the Y-coordinate as a percentage, see TSPoint::yPercent. If the touchscreen is not being touched, this value is meaningless.
Definition: Adafruit_TSC2046.h:186
The type returned by Adafruit_TSC2046::getPoint.
Definition: Adafruit_TSC2046.h:164
float z
The resistance measurement that corresponds to the pressure currently exerted on the touchscreen...
Definition: Adafruit_TSC2046.h:194
float yPercent()
The Y-coordinate as a percentage. Note that physical touchscreens vary, and the range of yours may no...
Definition: Adafruit_TSC2046.cpp:31
TSPoint(int16_t x, int16_t y, float z)
Create a new TSPoint with these exact values. You usually don&#39;t need to call this constructor in user...
Definition: Adafruit_TSC2046.cpp:23
int16_t x
The full scale raw X coordinate from the touchscreen. For the X-coordinate as a percentage, see TSPoint::xPercent. If the touchscreen is not being touched, this value is meaningless.
Definition: Adafruit_TSC2046.h:180