|
| 1 | +/*************************************************** |
| 2 | + This is our touchscreen painting example for the Adafruit TFT FeatherWing |
| 3 | + ----> http://www.adafruit.com/products/3315 |
| 4 | +
|
| 5 | + Check out the links above for our tutorials and wiring diagrams |
| 6 | +
|
| 7 | + Adafruit invests time and resources providing this open source code, |
| 8 | + please support Adafruit and open-source hardware by purchasing |
| 9 | + products from Adafruit! |
| 10 | +
|
| 11 | + Written by Limor Fried/Ladyada for Adafruit Industries. |
| 12 | + MIT license, all text above must be included in any redistribution |
| 13 | + ****************************************************/ |
| 14 | + |
| 15 | +#include <SPI.h> |
| 16 | +#include <Wire.h> // this is needed even tho we aren't using it |
| 17 | + |
| 18 | +#include <Adafruit_GFX.h> // Core graphics library |
| 19 | +#include <Adafruit_ILI9341.h> // Hardware-specific library |
| 20 | +#include <Adafruit_STMPE610.h> |
| 21 | + |
| 22 | +#ifdef ESP8266 |
| 23 | + #define STMPE_CS 16 |
| 24 | + #define TFT_CS 0 |
| 25 | + #define TFT_DC 15 |
| 26 | + #define SD_CS 2 |
| 27 | +#endif |
| 28 | +#ifdef __AVR_ATmega32U4__ |
| 29 | + #define STMPE_CS 6 |
| 30 | + #define TFT_CS 9 |
| 31 | + #define TFT_DC 10 |
| 32 | + #define SD_CS 5 |
| 33 | +#endif |
| 34 | +#ifdef ARDUINO_SAMD_FEATHER_M0 |
| 35 | + #define STMPE_CS 6 |
| 36 | + #define TFT_CS 9 |
| 37 | + #define TFT_DC 10 |
| 38 | + #define SD_CS 5 |
| 39 | +#endif |
| 40 | +#ifdef TEENSYDUINO |
| 41 | + #define TFT_DC 10 |
| 42 | + #define TFT_CS 4 |
| 43 | + #define STMPE_CS 3 |
| 44 | + #define SD_CS 8 |
| 45 | +#endif |
| 46 | +#ifdef ARDUINO_STM32_FEATHER |
| 47 | + #define TFT_DC PB4 |
| 48 | + #define TFT_CS PA15 |
| 49 | + #define STMPE_CS PC7 |
| 50 | + #define SD_CS PC5 |
| 51 | +#endif |
| 52 | + |
| 53 | +Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC); |
| 54 | +Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS); |
| 55 | + |
| 56 | + |
| 57 | +// This is calibration data for the raw touch data to the screen coordinates |
| 58 | +#define TS_MINX 3800 |
| 59 | +#define TS_MAXX 100 |
| 60 | +#define TS_MINY 100 |
| 61 | +#define TS_MAXY 3750 |
| 62 | + |
| 63 | +// Size of the color selection boxes and the paintbrush size |
| 64 | +#define BOXSIZE 40 |
| 65 | +#define PENRADIUS 3 |
| 66 | +int oldcolor, currentcolor; |
| 67 | + |
| 68 | +void setup(void) { |
| 69 | + Serial.begin(115200); |
| 70 | + |
| 71 | + delay(10); |
| 72 | + Serial.println("FeatherWing TFT"); |
| 73 | + if (!ts.begin()) { |
| 74 | + Serial.println("Couldn't start touchscreen controller"); |
| 75 | + while (1); |
| 76 | + } |
| 77 | + Serial.println("Touchscreen started"); |
| 78 | + |
| 79 | + tft.begin(); |
| 80 | + tft.fillScreen(ILI9341_BLACK); |
| 81 | + |
| 82 | + // make the color selection boxes |
| 83 | + tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED); |
| 84 | + tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW); |
| 85 | + tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN); |
| 86 | + tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_CYAN); |
| 87 | + tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE); |
| 88 | + tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA); |
| 89 | + |
| 90 | + // select the current color 'red' |
| 91 | + tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE); |
| 92 | + currentcolor = ILI9341_RED; |
| 93 | +} |
| 94 | + |
| 95 | +void loop() { |
| 96 | + // Retrieve a point |
| 97 | + TS_Point p = ts.getPoint(); |
| 98 | + |
| 99 | + Serial.print("X = "); Serial.print(p.x); |
| 100 | + Serial.print("\tY = "); Serial.print(p.y); |
| 101 | + Serial.print("\tPressure = "); Serial.println(p.z); |
| 102 | + |
| 103 | + |
| 104 | + // Scale from ~0->4000 to tft.width using the calibration #'s |
| 105 | + p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width()); |
| 106 | + p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height()); |
| 107 | + |
| 108 | + if (p.y < BOXSIZE) { |
| 109 | + oldcolor = currentcolor; |
| 110 | + |
| 111 | + if (p.x < BOXSIZE) { |
| 112 | + currentcolor = ILI9341_RED; |
| 113 | + tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE); |
| 114 | + } else if (p.x < BOXSIZE*2) { |
| 115 | + currentcolor = ILI9341_YELLOW; |
| 116 | + tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE); |
| 117 | + } else if (p.x < BOXSIZE*3) { |
| 118 | + currentcolor = ILI9341_GREEN; |
| 119 | + tft.drawRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE); |
| 120 | + } else if (p.x < BOXSIZE*4) { |
| 121 | + currentcolor = ILI9341_CYAN; |
| 122 | + tft.drawRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE); |
| 123 | + } else if (p.x < BOXSIZE*5) { |
| 124 | + currentcolor = ILI9341_BLUE; |
| 125 | + tft.drawRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE); |
| 126 | + } else if (p.x < BOXSIZE*6) { |
| 127 | + currentcolor = ILI9341_MAGENTA; |
| 128 | + tft.drawRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE); |
| 129 | + } |
| 130 | + |
| 131 | + if (oldcolor != currentcolor) { |
| 132 | + if (oldcolor == ILI9341_RED) |
| 133 | + tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED); |
| 134 | + if (oldcolor == ILI9341_YELLOW) |
| 135 | + tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW); |
| 136 | + if (oldcolor == ILI9341_GREEN) |
| 137 | + tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN); |
| 138 | + if (oldcolor == ILI9341_CYAN) |
| 139 | + tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_CYAN); |
| 140 | + if (oldcolor == ILI9341_BLUE) |
| 141 | + tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE); |
| 142 | + if (oldcolor == ILI9341_MAGENTA) |
| 143 | + tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + if (((p.y-PENRADIUS) > 0) && ((p.y+PENRADIUS) < tft.height())) { |
| 148 | + tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor); |
| 149 | + } |
| 150 | +} |
0 commit comments