| Linduino 1.3.0 Linear Technology Arduino-Compatible Demonstration Board

touchpaint.ino
Go to the documentation of this file.
1 /***************************************************
2 This is our touchscreen painting example for the Adafruit ILI9341 Shield
3 ----> http://www.adafruit.com/products/1651
4
5 Check out the links above for our tutorials and wiring diagrams
6 These displays use SPI to communicate, 4 or 5 pins are required to
7 interface (RST is optional)
8 Adafruit invests time and resources providing this open source code,
9 please support Adafruit and open-source hardware by purchasing
10 products from Adafruit!
11
12 Written by Limor Fried/Ladyada for Adafruit Industries.
13 MIT license, all text above must be included in any redistribution
14 ****************************************************/
15
16
17 #include <Adafruit_GFX.h> // Core graphics library
18 #include <SPI.h>
19 #include <Wire.h> // this is needed even tho we aren't using it
20 #include <Adafruit_ILI9341.h>
21 #include <Adafruit_STMPE610.h>
22
23 // This is calibration data for the raw touch data to the screen coordinates
24 #define TS_MINX 150
25 #define TS_MINY 130
26 #define TS_MAXX 3800
27 #define TS_MAXY 4000
28
29 // The STMPE610 uses hardware SPI on the shield, and #8
30 #define STMPE_CS 8
31 Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
32
33 // The display also uses hardware SPI, plus #9 & #10
34 #define TFT_CS 10
35 #define TFT_DC 9
36 Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
37
38 // Size of the color selection boxes and the paintbrush size
39 #define BOXSIZE 40
40 #define PENRADIUS 3
41 int oldcolor, currentcolor;
42
43 void setup(void)
44 {
45 // while (!Serial); // used for leonardo debugging
46
47 Serial.begin(9600);
48 Serial.println(F("Touch Paint!"));
49
50 tft.begin();
51
52 if (!ts.begin())
53 {
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
75 void loop()
76 {
77 // See if there's any touch data for us
78 if (ts.bufferEmpty())
79 {
80 return;
81 }
82 /*
83 // You can also wait for a touch
84 if (! ts.touched()) {
85 return;
86 }
87 */
88
89 // Retrieve a point
90 TS_Point p = ts.getPoint();
91
92 /*
93 Serial.print("X = "); Serial.print(p.x);
94 Serial.print("\tY = "); Serial.print(p.y);
95 Serial.print("\tPressure = "); Serial.println(p.z);
96 */
97
98 // Scale from ~0->4000 to tft.width using the calibration #'s
99 p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
100 p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
101
102 /*
103 Serial.print("("); Serial.print(p.x);
104 Serial.print(", "); Serial.print(p.y);
105 Serial.println(")");
106 */
107
108 if (p.y < BOXSIZE)
109 {
110 oldcolor = currentcolor;
111
112 if (p.x < BOXSIZE)
113 {
114 currentcolor = ILI9341_RED;
115 tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
116 }
117 else if (p.x < BOXSIZE*2)
118 {
119 currentcolor = ILI9341_YELLOW;
120 tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
121 }
122 else if (p.x < BOXSIZE*3)
123 {
124 currentcolor = ILI9341_GREEN;
125 tft.drawRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
126 }
127 else if (p.x < BOXSIZE*4)
128 {
129 currentcolor = ILI9341_CYAN;
130 tft.drawRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
131 }
132 else if (p.x < BOXSIZE*5)
133 {
134 currentcolor = ILI9341_BLUE;
135 tft.drawRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
136 }
137 else if (p.x < BOXSIZE*6)
138 {
139 currentcolor = ILI9341_MAGENTA;
140 tft.drawRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
141 }
142
143 if (oldcolor != currentcolor)
144 {
145 if (oldcolor == ILI9341_RED)
146 tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
147 if (oldcolor == ILI9341_YELLOW)
148 tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW);
149 if (oldcolor == ILI9341_GREEN)
150 tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN);
151 if (oldcolor == ILI9341_CYAN)
152 tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_CYAN);
153 if (oldcolor == ILI9341_BLUE)
154 tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE);
155 if (oldcolor == ILI9341_MAGENTA)
156 tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA);
157 }
158 }
159 if (((p.y-PENRADIUS) > BOXSIZE) && ((p.y+PENRADIUS) < tft.height()))
160 {
161 tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);
162 }
163 }
TS_Point
Definition: Adafruit_FT6206.h:49
Adafruit_GFX::drawRect
virtual void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
Definition: Adafruit_GFX.cpp:264
TS_Point::x
int16_t x
Definition: Adafruit_FT6206.h:58
Adafruit_GFX::height
int16_t height(void) const
Definition: Adafruit_GFX.cpp:1025
PENRADIUS
#define PENRADIUS
Definition: touchpaint.ino:40
ts
Adafruit_STMPE610 ts
Definition: touchpaint.ino:31
TFT_DC
#define TFT_DC
Definition: touchpaint.ino:35
Adafruit_GFX::width
int16_t width(void) const
Definition: Adafruit_GFX.cpp:1020
Adafruit_ILI9341::begin
void begin(void)
Definition: Adafruit_ILI9341.cpp:219
ILI9341_WHITE
#define ILI9341_WHITE
Definition: Adafruit_ILI9341.h:113
ILI9341_MAGENTA
#define ILI9341_MAGENTA
Definition: Adafruit_ILI9341.h:111
currentcolor
static int currentcolor
Definition: touchpaint.ino:41
STMPE_CS
#define STMPE_CS
Definition: touchpaint.ino:30
ILI9341_BLACK
#define ILI9341_BLACK
Definition: Adafruit_ILI9341.h:98
ILI9341_RED
#define ILI9341_RED
Definition: Adafruit_ILI9341.h:110
TS_Point::y
int16_t y
Definition: Adafruit_FT6206.h:58
ILI9341_YELLOW
#define ILI9341_YELLOW
Definition: Adafruit_ILI9341.h:112
BOXSIZE
#define BOXSIZE
Definition: touchpaint.ino:39
Adafruit_GFX.h
TS_MINY
#define TS_MINY
Definition: touchpaint.ino:25
loop
static void loop()
Definition: touchpaint.ino:75
ILI9341_BLUE
#define ILI9341_BLUE
Definition: Adafruit_ILI9341.h:107
TS_MAXY
#define TS_MAXY
Definition: touchpaint.ino:27
ILI9341_CYAN
#define ILI9341_CYAN
Definition: Adafruit_ILI9341.h:109
Adafruit_GFX::fillCircle
void fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color)
Definition: Adafruit_GFX.cpp:168
ILI9341_GREEN
#define ILI9341_GREEN
Definition: Adafruit_ILI9341.h:108
Adafruit_ILI9341::fillScreen
void fillScreen(uint16_t color)
Definition: Adafruit_ILI9341.cpp:552
oldcolor
static int oldcolor
Definition: touchpaint.ino:41
tft
Adafruit_ILI9341 tft
Definition: touchpaint.ino:36
Adafruit_ILI9341::fillRect
void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
Definition: Adafruit_ILI9341.cpp:558
TS_MAXX
#define TS_MAXX
Definition: touchpaint.ino:26
setup
static void setup(void)
Definition: touchpaint.ino:43
TFT_CS
#define TFT_CS
Definition: touchpaint.ino:34
Adafruit_ILI9341.h
Adafruit_ILI9341
Definition: Adafruit_ILI9341.h:118
TS_MINX
#define TS_MINX
Definition: touchpaint.ino:24