FT5406EE8.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * @file FT5406EE8.c
  3. *
  4. */
  5. /*********************
  6. * INCLUDES
  7. *********************/
  8. #include "FT5406EE8.h"
  9. #if USE_FT5406EE8
  10. #include <stddef.h>
  11. #include <stdbool.h>
  12. #include LV_DRV_INDEV_INCLUDE
  13. #include LV_DRV_DELAY_INCLUDE
  14. /*********************
  15. * DEFINES
  16. *********************/
  17. #define I2C_WR_BIT 0x00
  18. #define I2C_RD_BIT 0x01
  19. /*DEVICE MODES*/
  20. #define OPERAT_MD 0x00
  21. #define TEST_MD 0x04
  22. #define SYS_INF_MD 0x01
  23. /*OPERATING MODE*/
  24. #define DEVICE_MODE 0x00
  25. #define GEST_ID 0x01
  26. #define TD_STATUS 0x02
  27. #define FT5406EE8_FINGER_MAX 10
  28. /*Register adresses*/
  29. #define FT5406EE8_REG_DEVICE_MODE 0x00
  30. #define FT5406EE8_REG_GEST_ID 0x01
  31. #define FT5406EE8_REG_TD_STATUS 0x02
  32. #define FT5406EE8_REG_YH 0x03
  33. #define FT5406EE8_REG_YL 0x04
  34. #define FT5406EE8_REG_XH 0x05
  35. #define FT5406EE8_REG_XL 0x06
  36. /**********************
  37. * TYPEDEFS
  38. **********************/
  39. /**********************
  40. * STATIC PROTOTYPES
  41. **********************/
  42. static bool ft5406ee8_get_touch_num(void);
  43. static bool ft5406ee8_read_finger1(int16_t * x, int16_t * y);
  44. /**********************
  45. * STATIC VARIABLES
  46. **********************/
  47. /**********************
  48. * MACROS
  49. **********************/
  50. /**********************
  51. * GLOBAL FUNCTIONS
  52. **********************/
  53. /**
  54. *
  55. */
  56. void ft5406ee8_init(void)
  57. {
  58. }
  59. /**
  60. * Get the current position and state of the touchpad
  61. * @param data store the read data here
  62. * @return false: because no ore data to be read
  63. */
  64. bool ft5406ee8_read(lv_indev_data_t * data)
  65. {
  66. static int16_t x_last;
  67. static int16_t y_last;
  68. int16_t x;
  69. int16_t y;
  70. bool valid = true;
  71. valid = ft5406ee8_get_touch_num();
  72. if(valid == true) {
  73. valid = ft5406ee8_read_finger1(&x, &y);
  74. }
  75. if(valid == true) {
  76. x = (uint32_t)((uint32_t)x * 320) / 2048;
  77. y = (uint32_t)((uint32_t)y * 240) / 2048;
  78. x_last = x;
  79. y_last = y;
  80. }
  81. else {
  82. x = x_last;
  83. y = y_last;
  84. }
  85. data->point.x = x;
  86. data->point.y = y;
  87. data->state = valid == false ? LV_INDEV_STATE_REL : LV_INDEV_STATE_PR;
  88. return false;
  89. }
  90. /**********************
  91. * STATIC FUNCTIONS
  92. **********************/
  93. static bool ft5406ee8_get_touch_num(void)
  94. {
  95. bool ok = true;
  96. uint8_t t_num = 0;
  97. LV_DRV_INDEV_I2C_START;
  98. LV_DRV_INDEV_I2C_WR((FT5406EE8_I2C_ADR << 1) | I2C_WR_BIT);
  99. LV_DRV_INDEV_I2C_WR(FT5406EE8_REG_TD_STATUS)
  100. LV_DRV_INDEV_I2C_RESTART;
  101. LV_DRV_INDEV_I2C_WR((FT5406EE8_I2C_ADR << 1) | I2C_RD_BIT);
  102. t_num = LV_DRV_INDEV_I2C_READ(0);
  103. /* Error if not touched or too much finger */
  104. if (t_num > FT5406EE8_FINGER_MAX || t_num == 0){
  105. ok = false;
  106. }
  107. return ok;
  108. }
  109. /**
  110. * Read the x and y coordinated
  111. * @param x store the x coordinate here
  112. * @param y store the y coordinate here
  113. * @return false: not valid point; true: valid point
  114. */
  115. static bool ft5406ee8_read_finger1(int16_t * x, int16_t * y)
  116. {
  117. uint8_t temp_xH = 0;
  118. uint8_t temp_xL = 0;
  119. uint8_t temp_yH = 0;
  120. uint8_t temp_yL = 0;
  121. /*Read Y High and low byte*/
  122. LV_DRV_INDEV_I2C_START;
  123. LV_DRV_INDEV_I2C_WR((FT5406EE8_I2C_ADR << 1) | I2C_WR_BIT);
  124. LV_DRV_INDEV_I2C_WR(FT5406EE8_REG_YH)
  125. LV_DRV_INDEV_I2C_RESTART;
  126. LV_DRV_INDEV_I2C_WR((FT5406EE8_I2C_ADR << 1) | I2C_RD_BIT);
  127. temp_yH = LV_DRV_INDEV_I2C_READ(1);
  128. temp_yL = LV_DRV_INDEV_I2C_READ(1);
  129. /*The upper two bit must be 2 on valid press*/
  130. if(((temp_yH >> 6) & 0xFF) != 2) {
  131. (void) LV_DRV_INDEV_I2C_READ(0); /*Dummy read to close read sequence*/
  132. *x = 0;
  133. *y = 0;
  134. return false;
  135. }
  136. /*Read X High and low byte*/
  137. temp_xH = LV_DRV_INDEV_I2C_READ(1);
  138. temp_xL = LV_DRV_INDEV_I2C_READ(0);
  139. /*Save the result*/
  140. *x = (temp_xH & 0x0F) << 8;
  141. *x += temp_xL;
  142. *y = (temp_yH & 0x0F) << 8;
  143. *y += temp_yL;
  144. return true;
  145. }
  146. #endif