ts_calibration.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. ******************************************************************************
  3. * @file Display/LCD_Paint/Src/ts_calibration.c
  4. * @author MCD Application Team
  5. * @brief This application code shows how to calibrate the touchscreen.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics International N.V.
  10. * All rights reserved.</center></h2>
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted, provided that the following conditions are met:
  14. *
  15. * 1. Redistribution of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. Neither the name of STMicroelectronics nor the names of other
  21. * contributors to this software may be used to endorse or promote products
  22. * derived from this software without specific written permission.
  23. * 4. This software, including modifications and/or derivative works of this
  24. * software, must execute solely and exclusively on microcontroller or
  25. * microprocessor devices manufactured by or for STMicroelectronics.
  26. * 5. Redistribution and use of this software other than as permitted under
  27. * this license is void and will automatically terminate your rights under
  28. * this license.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  31. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  33. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  34. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  35. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  36. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  37. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  38. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  39. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  40. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  41. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. ******************************************************************************
  44. */
  45. /* Includes ------------------------------------------------------------------*/
  46. #include "main.h"
  47. /** @addtogroup STM32F4xx_HAL_Applications
  48. * @{
  49. */
  50. /** @addtogroup LCD_Paint
  51. * @{
  52. */
  53. /* Private typedef -----------------------------------------------------------*/
  54. /* Private define ------------------------------------------------------------*/
  55. /* Private macro -------------------------------------------------------------*/
  56. /* Private variables ---------------------------------------------------------*/
  57. static TS_StateTypeDef TS_State;
  58. static uint8_t Calibration_Done = 0;
  59. static int16_t A1, A2, B1, B2;
  60. static int16_t aPhysX[2], aPhysY[2], aLogX[2], aLogY[2];
  61. /* Private function prototypes -----------------------------------------------*/
  62. static void TouchscreenCalibration_SetHint(void);
  63. static void GetPhysValues(int16_t LogX, int16_t LogY, int16_t * pPhysX, int16_t * pPhysY);
  64. static void WaitForPressedState(uint8_t Pressed);
  65. /* Private functions ---------------------------------------------------------*/
  66. /**
  67. * @brief Performs the TS calibration
  68. * @param None
  69. * @retval None
  70. */
  71. void Touchscreen_Calibration(void)
  72. {
  73. uint8_t status = 0;
  74. uint8_t i = 0;
  75. TouchscreenCalibration_SetHint();
  76. status = BSP_TS_Init(BSP_LCD_GetXSize(), BSP_LCD_GetYSize());
  77. if (status != TS_OK)
  78. {
  79. BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
  80. BSP_LCD_SetTextColor(LCD_COLOR_RED);
  81. BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize()- 95, (uint8_t*)"ERROR", CENTER_MODE);
  82. BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize()- 80, (uint8_t*)"Touchscreen cannot be initialized", CENTER_MODE);
  83. }
  84. while (1)
  85. {
  86. if (status == TS_OK)
  87. {
  88. aLogX[0] = 15;
  89. aLogY[0] = 15;
  90. aLogX[1] = BSP_LCD_GetXSize() - 15;
  91. aLogY[1] = BSP_LCD_GetYSize() - 15;
  92. for (i = 0; i < 2; i++)
  93. {
  94. GetPhysValues(aLogX[i], aLogY[i], &aPhysX[i], &aPhysY[i]);
  95. }
  96. A1 = (1000 * ( aLogX[1] - aLogX[0]))/ ( aPhysX[1] - aPhysX[0]);
  97. B1 = (1000 * aLogX[0]) - A1 * aPhysX[0];
  98. A2 = (1000 * ( aLogY[1] - aLogY[0]))/ ( aPhysY[1] - aPhysY[0]);
  99. B2 = (1000 * aLogY[0]) - A2 * aPhysY[0];
  100. Calibration_Done = 1;
  101. return;
  102. }
  103. HAL_Delay(5);
  104. }
  105. }
  106. /**
  107. * @brief Display calibration hint
  108. * @param None
  109. * @retval None
  110. */
  111. static void TouchscreenCalibration_SetHint(void)
  112. {
  113. /* Clear the LCD */
  114. BSP_LCD_Clear(LCD_COLOR_WHITE);
  115. /* Set Touchscreen Demo description */
  116. BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
  117. BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
  118. BSP_LCD_SetFont(&Font12);
  119. BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize()/2 - 27, (uint8_t*)"Before using the Touchscreen", CENTER_MODE);
  120. BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize()/2 - 12, (uint8_t*)"you need to calibrate it.", CENTER_MODE);
  121. BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize()/2 + 3, (uint8_t*)"Press on the black circles", CENTER_MODE);
  122. }
  123. /**
  124. * @brief Get Physical position
  125. * @param LogX : logical X position
  126. * @param LogY : logical Y position
  127. * @param pPhysX : Physical X position
  128. * @param pPhysY : Physical Y position
  129. * @retval None
  130. */
  131. static void GetPhysValues(int16_t LogX, int16_t LogY, int16_t * pPhysX, int16_t * pPhysY)
  132. {
  133. /* Draw the ring */
  134. BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
  135. BSP_LCD_FillCircle(LogX, LogY, 5);
  136. BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
  137. BSP_LCD_FillCircle(LogX, LogY, 2);
  138. /* Wait until touch is pressed */
  139. WaitForPressedState(1);
  140. BSP_TS_GetState(&TS_State);
  141. *pPhysX = TS_State.x;
  142. *pPhysY = TS_State.y;
  143. /* Wait until touch is released */
  144. WaitForPressedState(0);
  145. BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
  146. BSP_LCD_FillCircle(LogX, LogY, 5);
  147. }
  148. /**
  149. * @brief wait for touch detected
  150. * @param Pressed: touch pressed.
  151. * @retval None
  152. */
  153. static void WaitForPressedState(uint8_t Pressed)
  154. {
  155. TS_StateTypeDef State;
  156. do
  157. {
  158. BSP_TS_GetState(&State);
  159. HAL_Delay(10);
  160. if (State.TouchDetected == Pressed)
  161. {
  162. uint16_t TimeStart = HAL_GetTick();
  163. do {
  164. BSP_TS_GetState(&State);
  165. HAL_Delay(10);
  166. if (State.TouchDetected != Pressed)
  167. {
  168. break;
  169. } else if ((HAL_GetTick() - 100) > TimeStart)
  170. {
  171. return;
  172. }
  173. } while (1);
  174. }
  175. } while (1);
  176. }
  177. /**
  178. * @brief Calibrate X position
  179. * @param x : X position
  180. * @retval calibrated x
  181. */
  182. uint16_t Calibration_GetX(uint16_t x)
  183. {
  184. return (((A1 * x) + B1)/1000);
  185. }
  186. /**
  187. * @brief Calibrate Y position
  188. * @param y : Y position
  189. * @retval calibrated y
  190. */
  191. uint16_t Calibration_GetY(uint16_t y)
  192. {
  193. return (((A2 * y) + B2)/1000);
  194. }
  195. /**check if the TS is calibrated
  196. * @param None
  197. * @retval calibration state (1 : calibrated / 0: no)
  198. */
  199. uint8_t IsCalibrationDone(void)
  200. {
  201. return (Calibration_Done);
  202. }
  203. /**
  204. * @}
  205. */
  206. /**
  207. * @}
  208. */
  209. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/