XPT2046.h
Go to the documentation of this file.
1 /****
2  * XPT2046.h
3  *
4  * Copyright 2021 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the Sming-Graphics Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  * @author: May 2021 - mikee47 <mike@sillyhouse.net>
19  *
20  ****/
21 
22 #pragma once
23 
24 #include "../Touch.h"
25 #include <HSPI/Device.h>
26 #include <Platform/System.h>
27 #include <SimpleTimer.h>
28 
29 namespace Graphics
30 {
31 class XPT2046 : private HSPI::Device, public Touch
32 {
33 public:
34  static constexpr uint16_t sampleMax{0x0fff};
35 
37  {
38  }
39 
41  {
43  .origin = {224, 301},
44  .num = {280, 200},
45  .den = {3138, 3016},
46  });
47  }
48 
50  {
51  timer.stop();
52  }
53 
54  bool begin(HSPI::PinSet pinSet, uint8_t chipSelect, uint8_t irqPin = PIN_NONE);
55 
56  void end()
57  {
58  timer.stop();
59  }
60 
61  /* Touch */
62 
63  Size getNativeSize() const override
64  {
65  return Size{sampleMax, sampleMax};
66  }
67 
68  State getState() const override
69  {
70  return State{Point(xraw, yraw), zraw};
71  }
72 
74  {
75  if(!updateRequested) {
76  beginUpdate();
77  }
78  }
79 
80 private:
81  HSPI::IoModes getSupportedIoModes() const override
82  {
83  return HSPI::IoMode::SPI;
84  }
85 
86  static void isr();
87  static void staticOnChange(void* param)
88  {
89  static_cast<XPT2046*>(param)->beginUpdate();
90  }
91  void beginUpdate();
92 
93  static bool IRAM_ATTR staticRequestComplete(HSPI::Request& req)
94  {
95  System.queueCallback(staticUpdate, req.param);
96  return true;
97  }
98  static void staticUpdate(void* param)
99  {
100  static_cast<XPT2046*>(param)->update();
101  }
102  void update();
103 
104  void printBuffer(const char* tag);
105 
106  SimpleTimer timer;
107  HSPI::Request req;
108  uint16_t buffer[10];
109  uint8_t irqPin{PIN_NONE};
110  bool updateRequested{false};
111  uint16_t xraw{0};
112  uint16_t yraw{0};
113  uint16_t zraw{0};
114  uint8_t offcount{0};
115 };
116 
117 } // namespace Graphics
Manage a set of bit values using enumeration.
Definition: BitSet.h:45
void stop()
Stops timer.
Definition: CallbackTimer.h:239
A physical display device.
Definition: Libraries/Graphics/src/include/Graphics/Device.h:33
Definition: Touch.h:31
void setCalibration(const Calibration &cal)
Definition: Touch.h:106
Device * device
Definition: Touch.h:120
Definition: XPT2046.h:32
~XPT2046()
Definition: XPT2046.h:49
void end()
Definition: XPT2046.h:56
void requestUpdate()
Definition: XPT2046.h:73
State getState() const override
Get current state.
Definition: XPT2046.h:68
static constexpr uint16_t sampleMax
Definition: XPT2046.h:34
Size getNativeSize() const override
Get physical size of display.
Definition: XPT2046.h:63
bool begin(HSPI::PinSet pinSet, uint8_t chipSelect, uint8_t irqPin=PIN_NONE)
XPT2046(HSPI::Controller &controller)
Definition: XPT2046.h:36
XPT2046(HSPI::Controller &controller, Graphics::Device &device)
Definition: XPT2046.h:40
Manages access to SPI hardware.
Definition: HardwareSPI/src/include/HSPI/Controller.h:52
Manages a specific SPI device instance attached to a controller.
Definition: Libraries/HardwareSPI/src/include/HSPI/Device.h:45
Controller & controller
Definition: Libraries/HardwareSPI/src/include/HSPI/Device.h:195
static bool queueCallback(TaskCallback32 callback, uint32_t param=0)
Queue a deferred callback.
SystemClass System
Global instance of system object.
Definition: Virtual.h:31
TPoint< int16_t > Point
Definition: Libraries/Graphics/src/include/Graphics/Types.h:280
static constexpr uint8_t PIN_NONE
Undefined I/O pin value.
Definition: Libraries/Graphics/src/include/Graphics/Types.h:37
Definition: Common.h:35
@ SPI
One bit per clock, MISO stage concurrent with MISO (full-duplex)
PinSet
How SPI hardware pins are connected.
Definition: Common.h:108
Size of rectangular area (width x height)
Definition: Libraries/Graphics/src/include/Graphics/Types.h:105
Definition: Touch.h:33
Defines an SPI Request Packet.
Definition: HardwareSPI/src/include/HSPI/Request.h:57
void * param
User parameter.
Definition: HardwareSPI/src/include/HSPI/Request.h:72