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 |
TouchCalibration.ino (8477B)
1 /******************************************************************************* 2 * Touch Calibration tool for helping other example touchscreen support 3 * 4 * For resistive touchscreen, you can follow the display hint for calibration. 5 * And then copy the serial output result to touch.h. 6 * 7 * For capacitive touchscreen, it should no need to calibrate. 8 * This tool just help you check the connection and orientation. 9 * 10 * Resistive touchscreen libraries 11 * XPT2046: https://github.com/PaulStoffregen/XPT2046_Touchscreen.git 12 * 13 * Capacitive touchscreen libraries 14 * TouchLib: https://github.com/mmMicky/TouchLib.git 15 ******************************************************************************/ 16 17 /******************************************************************************* 18 * Please config the touch panel in touch.h 19 ******************************************************************************/ 20 #include "touch.h" 21 22 /******************************************************************************* 23 * Start of Arduino_GFX setting 24 * 25 * Arduino_GFX try to find the settings depends on selected board in Arduino IDE 26 * Or you can define the display dev kit not in the board list 27 * Defalult pin list for non display dev kit: 28 * Arduino Nano, Micro and more: CS: 9, DC: 8, RST: 7, BL: 6, SCK: 13, MOSI: 11, MISO: 12 29 * ESP32 various dev board : CS: 5, DC: 27, RST: 33, BL: 22, SCK: 18, MOSI: 23, MISO: nil 30 * ESP32-C3 various dev board : CS: 7, DC: 2, RST: 1, BL: 3, SCK: 4, MOSI: 6, MISO: nil 31 * ESP32-S2 various dev board : CS: 34, DC: 38, RST: 33, BL: 21, SCK: 36, MOSI: 35, MISO: nil 32 * ESP32-S3 various dev board : CS: 40, DC: 41, RST: 42, BL: 48, SCK: 36, MOSI: 35, MISO: nil 33 * ESP8266 various dev board : CS: 15, DC: 4, RST: 2, BL: 5, SCK: 14, MOSI: 13, MISO: 12 34 * Raspberry Pi Pico dev board : CS: 17, DC: 27, RST: 26, BL: 28, SCK: 18, MOSI: 19, MISO: 16 35 * RTL8720 BW16 old patch core : CS: 18, DC: 17, RST: 2, BL: 23, SCK: 19, MOSI: 21, MISO: 20 36 * RTL8720_BW16 Official core : CS: 9, DC: 8, RST: 6, BL: 3, SCK: 10, MOSI: 12, MISO: 11 37 * RTL8722 dev board : CS: 18, DC: 17, RST: 22, BL: 23, SCK: 13, MOSI: 11, MISO: 12 38 * RTL8722_mini dev board : CS: 12, DC: 14, RST: 15, BL: 13, SCK: 11, MOSI: 9, MISO: 10 39 * Seeeduino XIAO dev board : CS: 3, DC: 2, RST: 1, BL: 0, SCK: 8, MOSI: 10, MISO: 9 40 * Teensy 4.1 dev board : CS: 39, DC: 41, RST: 40, BL: 22, SCK: 13, MOSI: 11, MISO: 12 41 ******************************************************************************/ 42 #include <Arduino_GFX_Library.h> 43 44 #define GFX_BL DF_GFX_BL // default backlight pin, you may replace DF_GFX_BL to actual backlight pin 45 46 /* More dev device declaration: https://github.com/moononournation/Arduino_GFX/wiki/Dev-Device-Declaration */ 47 #if defined(DISPLAY_DEV_KIT) 48 Arduino_GFX *gfx = create_default_Arduino_GFX(); 49 #else /* !defined(DISPLAY_DEV_KIT) */ 50 51 /* More data bus class: https://github.com/moononournation/Arduino_GFX/wiki/Data-Bus-Class */ 52 Arduino_DataBus *bus = create_default_Arduino_DataBus(); 53 54 /* More display class: https://github.com/moononournation/Arduino_GFX/wiki/Display-Class */ 55 Arduino_GFX *gfx = new Arduino_ILI9341(bus, DF_GFX_RST, 0 /* rotation */, false /* IPS */); 56 57 #endif /* !defined(DISPLAY_DEV_KIT) */ 58 /******************************************************************************* 59 * End of Arduino_GFX setting 60 ******************************************************************************/ 61 62 int16_t w = -1, h = -1; 63 int16_t point_x[4] = {-1}; 64 int16_t point_y[4] = {-1}; 65 int16_t current_point = -1, next_point = 0; 66 int16_t touched_x[4] = {-1}; 67 int16_t touched_y[4] = {-1}; 68 69 void setup(void) 70 { 71 Serial.begin(115200); 72 // Serial.setDebugOutput(true); 73 // while(!Serial); 74 Serial.println("Touch Calibration"); 75 76 #ifdef GFX_EXTRA_PRE_INIT 77 GFX_EXTRA_PRE_INIT(); 78 #endif 79 80 Serial.println("Init display"); 81 gfx->begin(); 82 gfx->fillScreen(BLACK); 83 84 #ifdef GFX_BL 85 pinMode(GFX_BL, OUTPUT); 86 digitalWrite(GFX_BL, HIGH); 87 #endif 88 89 // Init touch device 90 w = gfx->width(); 91 h = gfx->height(); 92 touch_init(w, h, gfx->getRotation()); 93 94 // Top left 95 point_x[0] = w / 8; 96 point_y[0] = h / 8; 97 // Top right 98 point_x[1] = point_x[0] * 7; 99 point_y[1] = point_y[0]; 100 // Bottom left 101 point_x[2] = point_x[0]; 102 point_y[2] = point_y[0] * 7; 103 // Bottom right 104 point_x[3] = point_x[1]; 105 point_y[3] = point_y[2]; 106 107 gfx->setCursor(0, 0); 108 gfx->setTextColor(RED); 109 gfx->setTextSize(2); 110 gfx->println("Touch Calibration"); 111 } 112 113 void loop() 114 { 115 if (current_point != next_point) 116 { 117 current_point = next_point; 118 119 gfx->drawLine( 120 point_x[current_point] - 5, 121 point_y[current_point] - 5, 122 point_x[current_point] + 5, 123 point_y[current_point] + 5, 124 RED); 125 gfx->drawLine( 126 point_x[current_point] + 5, 127 point_y[current_point] - 5, 128 point_x[current_point] - 5, 129 point_y[current_point] + 5, 130 RED); 131 } 132 133 if (touch_touched()) 134 { 135 int32_t total_x = touch_raw_x, total_y = touch_raw_y; 136 int count = 1; 137 while (touch_touched()) 138 { 139 total_x += touch_raw_x; 140 total_y += touch_raw_y; 141 count++; 142 // Serial.printf("touch_raw_x: %d, touch_raw_y: %d, count: %d\n", touch_raw_x, touch_raw_y, count); 143 } 144 touched_x[current_point] = total_x / count; 145 touched_y[current_point] = total_y / count; 146 // Serial.printf("touched_x: %d, touched_y: %d\n", touched_x[current_point], touched_y[current_point]); 147 148 if (current_point == 3) 149 { 150 Serial.printf("touched_x[0]: %d, touched_y[0]: %d\n", touched_x[0], touched_y[0]); 151 Serial.printf("touched_x[1]: %d, touched_y[1]: %d\n", touched_x[1], touched_y[1]); 152 Serial.printf("touched_x[2]: %d, touched_y[2]: %d\n", touched_x[2], touched_y[2]); 153 Serial.printf("touched_x[3]: %d, touched_y[3]: %d\n", touched_x[3], touched_y[3]); 154 uint16_t delta_x = (touched_x[0] > touched_x[1]) ? (touched_x[0] - touched_x[1]) : (touched_x[1] - touched_x[0]); 155 uint16_t delta_y = (touched_y[0] > touched_y[1]) ? (touched_y[0] - touched_y[1]) : (touched_y[1] - touched_y[0]); 156 157 if (delta_x > delta_y) 158 { 159 touch_swap_xy = false; 160 touch_map_x1 = (touched_x[0] + touched_x[2]) / 2; 161 touch_map_x2 = (touched_x[1] + touched_x[3]) / 2; 162 touch_map_y1 = (touched_y[0] + touched_y[1]) / 2; 163 touch_map_y2 = (touched_y[2] + touched_y[3]) / 2; 164 } 165 else 166 { 167 touch_swap_xy = true; 168 touch_map_x1 = (touched_y[0] + touched_y[2]) / 2; 169 touch_map_x2 = (touched_y[1] + touched_y[3]) / 2; 170 touch_map_y1 = (touched_x[0] + touched_x[1]) / 2; 171 touch_map_y2 = (touched_x[2] + touched_x[3]) / 2; 172 } 173 174 if (touch_map_x1 > touch_map_x2) 175 { 176 delta_x = (touch_map_x1 - touch_map_x2) / 6; 177 touch_map_x1 += delta_x; 178 touch_map_x2 -= delta_x; 179 } 180 else 181 { 182 delta_x = (touch_map_x2 - touch_map_x1) / 6; 183 touch_map_x1 -= delta_x; 184 touch_map_x2 += delta_x; 185 } 186 187 if (touch_map_y1 > touch_map_y2) 188 { 189 delta_y = (touch_map_y1 - touch_map_y2) / 6; 190 touch_map_y1 += delta_y; 191 touch_map_y2 -= delta_y; 192 } 193 else 194 { 195 delta_y = (touch_map_y2 - touch_map_y1) / 6; 196 touch_map_y1 -= delta_y; 197 touch_map_y2 += delta_y; 198 } 199 200 Serial.printf("bool touch_swap_xy = %s;\n", touch_swap_xy ? "true" : "false"); 201 Serial.printf("int16_t touch_map_x1 = %d;\n", touch_map_x1); 202 Serial.printf("int16_t touch_map_x2 = %d;\n", touch_map_x2); 203 Serial.printf("int16_t touch_map_y1 = %d;\n", touch_map_y1); 204 Serial.printf("int16_t touch_map_y2 = %d;\n", touch_map_y2); 205 206 gfx->setCursor(0, point_y[0] + 10); 207 gfx->setTextColor(WHITE); 208 gfx->setTextSize(1); 209 gfx->printf("bool touch_swap_xy = %s;\n", touch_swap_xy ? "true" : "false"); 210 gfx->printf("int16_t touch_map_x1 = %d;\n", touch_map_x1); 211 gfx->printf("int16_t touch_map_x2 = %d;\n", touch_map_x2); 212 gfx->printf("int16_t touch_map_y1 = %d;\n", touch_map_y1); 213 gfx->printf("int16_t touch_map_y2 = %d;\n", touch_map_y2); 214 215 // wait next touch to continue 216 while (!touch_touched()) 217 ; 218 219 gfx->fillScreen(BLACK); 220 gfx->setCursor(0, 0); 221 gfx->setTextColor(RED); 222 gfx->setTextSize(2); 223 gfx->println("Touch Calibration"); 224 } 225 226 next_point = (current_point == 3) ? 0 : (current_point + 1); 227 } 228 229 delay(100); 230 }