acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

touch.h (4958B)

      1 /*******************************************************************************
      2  * Touch libraries:
      3  * XPT2046: https://github.com/PaulStoffregen/XPT2046_Touchscreen.git
      4  *
      5  * Capacitive touchscreen libraries
      6  * TouchLib: https://github.com/mmMicky/TouchLib.git
      7  ******************************************************************************/
      8 
      9 /* uncomment for XPT2046 */
     10 // #define TOUCH_XPT2046
     11 // #define TOUCH_XPT2046_SCK 12
     12 // #define TOUCH_XPT2046_MISO 13
     13 // #define TOUCH_XPT2046_MOSI 11
     14 // #define TOUCH_XPT2046_CS 10
     15 // #define TOUCH_XPT2046_INT 18
     16 // #define TOUCH_XPT2046_ROTATION 0
     17 // #define TOUCH_XPT2046_SAMPLES 50
     18 
     19 // uncomment for most capacitive touchscreen
     20 // #define TOUCH_MODULES_FT5x06 // GT911 / CST_SELF / CST_MUTUAL / ZTW622 / L58 / FT3267 / FT5x06
     21 // #define TOUCH_MODULE_ADDR FT5x06_ADDR // CTS328_SLAVE_ADDRESS / L58_SLAVE_ADDRESS / CTS826_SLAVE_ADDRESS / CTS820_SLAVE_ADDRESS / CTS816S_SLAVE_ADDRESS / FT3267_SLAVE_ADDRESS / FT5x06_ADDR / GT911_SLAVE_ADDRESS1 / GT911_SLAVE_ADDRESS2 / ZTW622_SLAVE1_ADDRESS / ZTW622_SLAVE2_ADDRESS
     22 // #define TOUCH_SCL 5
     23 // #define TOUCH_SDA 6
     24 // #define TOUCH_RES -1
     25 // #define TOUCH_INT -1
     26 
     27 // Please fill below values from Arduino_GFX Example - TouchCalibration
     28 bool touch_swap_xy = false;
     29 int16_t touch_map_x1 = -1;
     30 int16_t touch_map_x2 = -1;
     31 int16_t touch_map_y1 = -1;
     32 int16_t touch_map_y2 = -1;
     33 
     34 int16_t touch_max_x = 0, touch_max_y = 0;
     35 int16_t touch_raw_x = 0, touch_raw_y = 0;
     36 int16_t touch_last_x = 0, touch_last_y = 0;
     37 
     38 #if defined(TOUCH_XPT2046)
     39 #include <XPT2046_Touchscreen.h>
     40 #include <SPI.h>
     41 XPT2046_Touchscreen ts(TOUCH_XPT2046_CS, TOUCH_XPT2046_INT);
     42 
     43 #elif defined(TOUCH_MODULE_ADDR) // TouchLib
     44 #include <Wire.h>
     45 #include <TouchLib.h>
     46 TouchLib touch(Wire, TOUCH_SDA, TOUCH_SCL, TOUCH_MODULE_ADDR);
     47 
     48 #endif // TouchLib
     49 
     50 void touch_init(int16_t w, int16_t h, uint8_t r)
     51 {
     52   touch_max_x = w - 1;
     53   touch_max_y = h - 1;
     54   if (touch_map_x1 == -1)
     55   {
     56     switch (r)
     57     {
     58     case 3:
     59       touch_swap_xy = true;
     60       touch_map_x1 = touch_max_x;
     61       touch_map_x2 = 0;
     62       touch_map_y1 = 0;
     63       touch_map_y2 = touch_max_y;
     64       break;
     65     case 2:
     66       touch_swap_xy = false;
     67       touch_map_x1 = touch_max_x;
     68       touch_map_x2 = 0;
     69       touch_map_y1 = touch_max_y;
     70       touch_map_y2 = 0;
     71       break;
     72     case 1:
     73       touch_swap_xy = true;
     74       touch_map_x1 = 0;
     75       touch_map_x2 = touch_max_x;
     76       touch_map_y1 = touch_max_y;
     77       touch_map_y2 = 0;
     78       break;
     79     default: // case 0:
     80       touch_swap_xy = false;
     81       touch_map_x1 = 0;
     82       touch_map_x2 = touch_max_x;
     83       touch_map_y1 = 0;
     84       touch_map_y2 = touch_max_y;
     85       break;
     86     }
     87   }
     88 
     89 #if defined(TOUCH_XPT2046)
     90   SPI.begin(TOUCH_XPT2046_SCK, TOUCH_XPT2046_MISO, TOUCH_XPT2046_MOSI, TOUCH_XPT2046_CS);
     91   ts.begin();
     92   ts.setRotation(TOUCH_XPT2046_ROTATION);
     93 
     94 #elif defined(TOUCH_MODULE_ADDR) // TouchLib
     95   // Reset touchscreen
     96 #if (TOUCH_RES > 0)
     97   pinMode(TOUCH_RES, OUTPUT);
     98   digitalWrite(TOUCH_RES, 0);
     99   delay(200);
    100   digitalWrite(TOUCH_RES, 1);
    101   delay(200);
    102 #endif
    103   Wire.begin(TOUCH_SDA, TOUCH_SCL);
    104   touch.init();
    105 
    106 #endif // TouchLib
    107 }
    108 
    109 bool touch_has_signal()
    110 {
    111 #if defined(TOUCH_XPT2046)
    112   return ts.tirqTouched();
    113 
    114 #elif defined(TOUCH_MODULE_ADDR) // TouchLib
    115   // TODO: implement TOUCH_INT
    116   return true;
    117 #endif                           // TouchLib
    118 
    119   return false;
    120 }
    121 
    122 void translate_touch_raw()
    123 {
    124   if (touch_swap_xy)
    125   {
    126     touch_last_x = map(touch_raw_y, touch_map_x1, touch_map_x2, 0, touch_max_x);
    127     touch_last_y = map(touch_raw_x, touch_map_y1, touch_map_y2, 0, touch_max_y);
    128   }
    129   else
    130   {
    131     touch_last_x = map(touch_raw_x, touch_map_x1, touch_map_x2, 0, touch_max_x);
    132     touch_last_y = map(touch_raw_y, touch_map_y1, touch_map_y2, 0, touch_max_y);
    133   }
    134   // Serial.printf("touch_raw_x: %d, touch_raw_y: %d, touch_last_x: %d, touch_last_y: %d\n", touch_raw_x, touch_raw_y, touch_last_x, touch_last_y);
    135 }
    136 
    137 bool touch_touched()
    138 {
    139 #if defined(TOUCH_XPT2046)
    140   if (ts.touched())
    141   {
    142     TS_Point p = ts.getPoint();
    143     touch_raw_x = p.x;
    144     touch_raw_y = p.y;
    145     int max_z = p.z;
    146     int count = 0;
    147     while ((ts.touched()) && (count < TOUCH_XPT2046_SAMPLES))
    148     {
    149       count++;
    150 
    151       TS_Point p = ts.getPoint();
    152       if (p.z > max_z)
    153       {
    154         touch_raw_x = p.x;
    155         touch_raw_y = p.y;
    156         max_z = p.z;
    157       }
    158       // Serial.printf("touch_raw_x: %d, touch_raw_y: %d, p.z: %d\n", touch_raw_x, touch_raw_y, p.z);
    159     }
    160     translate_touch_raw();
    161     return true;
    162   }
    163 #elif defined(TOUCH_MODULE_ADDR) // TouchLib
    164   if (touch.read())
    165   {
    166     TP_Point t = touch.getPoint(0);
    167     touch_raw_x = t.x;
    168     touch_raw_y = t.y;
    169 
    170     touch_last_x = touch_raw_x;
    171     touch_last_y = touch_raw_y;
    172 
    173     translate_touch_raw();
    174     return true;
    175   }
    176 
    177 #endif // TouchLib
    178 
    179   return false;
    180 }
    181 
    182 bool touch_released()
    183 {
    184 #if defined(TOUCH_XPT2046)
    185   return true;
    186 
    187 #elif defined(TOUCH_MODULE_ADDR) // TouchLib
    188   return false;
    189 #endif                           // TouchLib
    190 }