Welcome to mirror list, hosted at ThFree Co, Russian Federation.

XPT2046.cpp « Display - github.com/nickshl/DevCore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0e7f39629d2940d031cffce9f41c39a3b7c47f75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//******************************************************************************
//  @file XPT2046.cpp
//  @author Nicolai Shlapunov
//
//  @details DevCore: XPT2046 Low Level Driver Class, implementation
//
//  @copyright Copyright (c) 2016, Devtronic & Nicolai Shlapunov
//             All rights reserved.
//
//  @section SUPPORT
//
//   Devtronic invests time and resources providing this open source code,
//   please support Devtronic and open-source hardware/software by
//   donations and/or purchasing products from Devtronic.
//
//******************************************************************************

// *****************************************************************************
// ***   Includes   ************************************************************
// *****************************************************************************
#include <XPT2046.h>

// *****************************************************************************
// ***   Init touchscreen   ****************************************************
// *****************************************************************************
void XPT2046::Init(void)
{
  // Pull down CS
  HAL_GPIO_WritePin(TOUCH_CS_GPIO_Port, TOUCH_CS_Pin, GPIO_PIN_RESET);
  // Send ON command
  SpiWrite(TON);
  // Send empty byte for skip answer
  SpiWrite(EMP);
  // Send empty byte for skip answer
  SpiWrite(EMP);
  // Pull up CS
  HAL_GPIO_WritePin(TOUCH_CS_GPIO_Port, TOUCH_CS_Pin, GPIO_PIN_SET);
}

// *****************************************************************************
// ***   If touched - return true.   *******************************************
// *****************************************************************************
bool XPT2046::IsTouch(void)
{
  // Check T_IRQ input and return state
  return(HAL_GPIO_ReadPin(T_IRQ_GPIO_Port, T_IRQ_Pin) == GPIO_PIN_RESET);
}

// *****************************************************************************
// ***   Get X and Y coordinates. If touched - return true.   ******************
// *****************************************************************************
bool XPT2046::GetRawXY(int32_t& x, int32_t& y)
{
  // Return value
  bool ret = false;
  // If touch present
  if(HAL_GPIO_ReadPin(T_IRQ_GPIO_Port, T_IRQ_Pin) == GPIO_PIN_RESET)
  {
    // Pull down CS
    HAL_GPIO_WritePin(TOUCH_CS_GPIO_Port, TOUCH_CS_Pin, GPIO_PIN_RESET);
    // Request X coordinate
    SpiWrite(CHX);
    // Receive High byte for X
    x = SpiWriteRead(EMP) << 8;
    // Receive Low byte for X
    x |= SpiWriteRead(EMP);
    // Shift, because result have only 12 bits, 3 because answer started from
    // second rise edge
    x >>= 3;
    // Pull up CS
    HAL_GPIO_WritePin(TOUCH_CS_GPIO_Port, TOUCH_CS_Pin, GPIO_PIN_SET);

    // Pull down CS
    HAL_GPIO_WritePin(TOUCH_CS_GPIO_Port, TOUCH_CS_Pin, GPIO_PIN_RESET);
    // Request Y coordinate
    SpiWrite(CHY);
    // Receive High byte for Y
    y = SpiWriteRead(EMP) << 8;
    // Receive Low byte for Y
    y |= SpiWriteRead(EMP);
    // Shift, because result have only 12 bits, 3 because answer started from
    // second rise edge
    y >>= 3;
    // Pull up CS
    HAL_GPIO_WritePin(TOUCH_CS_GPIO_Port, TOUCH_CS_Pin, GPIO_PIN_SET);

    // Touch present
    ret = true;
  }
  // Return result
  return ret;
}

// *****************************************************************************
// ***   Get X and Y coordinates. If touched - return true.   ******************
// *****************************************************************************
bool XPT2046::GetXY(int32_t& x, int32_t& y)
{
  // Return value
  bool ret = GetRawXY(x, y);
  // If touch present
  if(ret)
  {
    // Calculate X
    x = ((x * COEF) / kx) + bx;
    // Calculate Y
    y = ((y * COEF) / ky) + by;
  }
  // Return touch state
  return ret;
}

// *****************************************************************************
// ***   SetCalibrationConsts   ************************************************
// *****************************************************************************
void XPT2046::SetCalibrationConsts(int32_t nkx, int32_t nky, int32_t nbx, int32_t nby)
{
  // Save calibration constants
  kx = nkx;
  ky = nky;
  bx = nbx;
  by = nby;
}

// *****************************************************************************
// ***   Write byte to SPI   ***************************************************
// *****************************************************************************
inline void XPT2046::SpiWrite(uint8_t c)
{
  // Call HAL function for send byte by SPI
  (void) HAL_SPI_Transmit(hspi, &c, sizeof(c), 1U);
}

// *****************************************************************************
// ***   Write and read byte to/from SPI   *************************************
// *****************************************************************************
inline uint8_t XPT2046::SpiWriteRead(uint8_t c)
{
  // Temporary variable for receive byte
  uint8_t rcv;
  // Call HAL function for send/receive byte by SPI
  (void) HAL_SPI_TransmitReceive(hspi, &c, &rcv, sizeof(uint8_t), 1U);
  // Return received byte
  return rcv;
}