|
| 1 | +// SPDX-FileCopyrightText: 2023 Limor Fried/Ladyada for Adafruit Industries |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +/*************************************************** |
| 5 | + This is our touchscreen painting example for the updated Adafruit |
| 6 | + ILI9341 Shield with TSC2007 |
| 7 | + ----> http://www.adafruit.com/products/1651 |
| 8 | +
|
| 9 | + Check out the links above for our tutorials and wiring diagrams |
| 10 | + These displays use SPI to communicate, 4 or 5 pins are required to |
| 11 | + interface (RST is optional) |
| 12 | + Adafruit invests time and resources providing this open source code, |
| 13 | + please support Adafruit and open-source hardware by purchasing |
| 14 | + products from Adafruit! |
| 15 | +
|
| 16 | + Written by Limor Fried/Ladyada for Adafruit Industries. |
| 17 | + MIT license, all text above must be included in any redistribution |
| 18 | + ****************************************************/ |
| 19 | + |
| 20 | + |
| 21 | +#include <Adafruit_GFX.h> |
| 22 | +#include <SPI.h> |
| 23 | +#include <Wire.h> |
| 24 | +#include <Adafruit_ILI9341.h> |
| 25 | +#include <Adafruit_TSC2007.h> |
| 26 | + |
| 27 | +// This is calibration data for the raw touch data to the screen coordinates |
| 28 | +#define TS_MINX 150 |
| 29 | +#define TS_MINY 130 |
| 30 | +#define TS_MAXX 3800 |
| 31 | +#define TS_MAXY 4000 |
| 32 | +#define TS_MIN_PRESSURE 200 |
| 33 | + |
| 34 | +Adafruit_TSC2007 ts; |
| 35 | + |
| 36 | +// The display also uses hardware SPI, plus #9 & #10 |
| 37 | +#define TFT_CS 10 |
| 38 | +#define TFT_DC 9 |
| 39 | +Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC); |
| 40 | + |
| 41 | +// Size of the color selection boxes and the paintbrush size |
| 42 | +#define BOXSIZE 40 |
| 43 | +#define PENRADIUS 3 |
| 44 | +int oldcolor, currentcolor; |
| 45 | + |
| 46 | +void setup(void) { |
| 47 | + |
| 48 | + Serial.begin(115200); |
| 49 | + // while (!Serial) delay(10); |
| 50 | + |
| 51 | + tft.begin(); |
| 52 | + |
| 53 | + if (!ts.begin()) { |
| 54 | + Serial.println("Couldn't start touchscreen controller"); |
| 55 | + while (1); |
| 56 | + } |
| 57 | + Serial.println("Touchscreen started"); |
| 58 | + |
| 59 | + tft.fillScreen(ILI9341_BLACK); |
| 60 | + |
| 61 | + // make the color selection boxes |
| 62 | + tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED); |
| 63 | + tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW); |
| 64 | + tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN); |
| 65 | + tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_CYAN); |
| 66 | + tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE); |
| 67 | + tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA); |
| 68 | + |
| 69 | + // select the current color 'red' |
| 70 | + tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE); |
| 71 | + currentcolor = ILI9341_RED; |
| 72 | +} |
| 73 | + |
| 74 | +void loop(){ |
| 75 | + uint16_t x, y, z1, z2; |
| 76 | + if (ts.read_touch(&x, &y, &z1, &z2) && (z1 > TS_MIN_PRESSURE)) { |
| 77 | + |
| 78 | + Serial.print("Touch point: ("); |
| 79 | + Serial.print(x); Serial.print(", "); |
| 80 | + Serial.print(y); Serial.print(", "); |
| 81 | + Serial.print(z1); Serial.print(" / "); |
| 82 | + Serial.print(z2); Serial.println(")"); |
| 83 | + |
| 84 | + // Scale from ~0->4000 to tft.width using the calibration #'s |
| 85 | + x = map(x, TS_MINX, TS_MAXX, 0, tft.width()); |
| 86 | + y = map(y, TS_MINY, TS_MAXY, 0, tft.height()); |
| 87 | + |
| 88 | + if (y < BOXSIZE) { |
| 89 | + oldcolor = currentcolor; |
| 90 | + |
| 91 | + if (x < BOXSIZE) { |
| 92 | + currentcolor = ILI9341_RED; |
| 93 | + tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE); |
| 94 | + } else if (x < BOXSIZE*2) { |
| 95 | + currentcolor = ILI9341_YELLOW; |
| 96 | + tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE); |
| 97 | + } else if (x < BOXSIZE*3) { |
| 98 | + currentcolor = ILI9341_GREEN; |
| 99 | + tft.drawRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE); |
| 100 | + } else if (x < BOXSIZE*4) { |
| 101 | + currentcolor = ILI9341_CYAN; |
| 102 | + tft.drawRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE); |
| 103 | + } else if (x < BOXSIZE*5) { |
| 104 | + currentcolor = ILI9341_BLUE; |
| 105 | + tft.drawRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE); |
| 106 | + } else if (x < BOXSIZE*6) { |
| 107 | + currentcolor = ILI9341_MAGENTA; |
| 108 | + tft.drawRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE); |
| 109 | + } |
| 110 | + |
| 111 | + if (oldcolor != currentcolor) { |
| 112 | + if (oldcolor == ILI9341_RED) |
| 113 | + tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED); |
| 114 | + if (oldcolor == ILI9341_YELLOW) |
| 115 | + tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW); |
| 116 | + if (oldcolor == ILI9341_GREEN) |
| 117 | + tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN); |
| 118 | + if (oldcolor == ILI9341_CYAN) |
| 119 | + tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_CYAN); |
| 120 | + if (oldcolor == ILI9341_BLUE) |
| 121 | + tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE); |
| 122 | + if (oldcolor == ILI9341_MAGENTA) |
| 123 | + tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA); |
| 124 | + } |
| 125 | + } |
| 126 | + if (((y-PENRADIUS) > BOXSIZE) && ((y+PENRADIUS) < tft.height())) { |
| 127 | + tft.fillCircle(x, y, PENRADIUS, currentcolor); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments