| Linduino 1.3.0 Linear Technology Arduino-Compatible Demonstration Board

breakouttouchpaint.ino
Go to the documentation of this file.
1 /***************************************************
2 This is our touchscreen painting example for the Adafruit ILI9341 Breakout
3 ----> http://www.adafruit.com/products/1770
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 /** NOT FOR USE WITH THE TOUCH SHIELD, ONLY FOR THE BREAKOUT! **/
17
18 #include <Adafruit_GFX.h> // Core graphics library
19 #include <SPI.h>
20 #include <Adafruit_ILI9341.h>
21 #include "TouchScreen.h"
22
23 // These are the four touchscreen analog pins
24 #define YP A2 // must be an analog pin, use "An" notation!
25 #define XM A3 // must be an analog pin, use "An" notation!
26 #define YM 5 // can be a digital pin
27 #define XP 4 // can be a digital pin
28
29 // This is calibration data for the raw touch data to the screen coordinates
30 #define TS_MINX 150
31 #define TS_MINY 120
32 #define TS_MAXX 920
33 #define TS_MAXY 940
34
35 #define MINPRESSURE 10
36 #define MAXPRESSURE 1000
37
38 // The display uses hardware SPI, plus #9 & #10
39 #define TFT_CS 10
40 #define TFT_DC 9
41 Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
42
43 // For better pressure precision, we need to know the resistance
44 // between X+ and X- Use any multimeter to read it
45 // For the one we're using, its 300 ohms across the X plate
46 TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
47
48 // Size of the color selection boxes and the paintbrush size
49 #define BOXSIZE 40
50 #define PENRADIUS 3
51 int oldcolor, currentcolor;
52
53 void setup(void)
54 {
55 // while (!Serial); // used for leonardo debugging
56
57 Serial.begin(9600);
58 Serial.println(F("Touch Paint!"));
59
60 tft.begin();
61 tft.fillScreen(ILI9341_BLACK);
62
63 // make the color selection boxes
64 tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
65 tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW);
66 tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN);
67 tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_CYAN);
68 tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE);
69 tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA);
70
71 // select the current color 'red'
72 tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
73 currentcolor = ILI9341_RED;
74 }
75
76
77 void loop()
78 {
79 // Retrieve a point
80 TSPoint p = ts.getPoint();
81
82 /*
83 Serial.print("X = "); Serial.print(p.x);
84 Serial.print("\tY = "); Serial.print(p.y);
85 Serial.print("\tPressure = "); Serial.println(p.z);
86 */
87
88 // we have some minimum pressure we consider 'valid'
89 // pressure of 0 means no pressing!
90 if (p.z < MINPRESSURE || p.z > MAXPRESSURE)
91 {
92 return;
93 }
94
95 // Scale from ~0->1000 to tft.width using the calibration #'s
96 p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
97 p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
98
99 /*
100 Serial.print("("); Serial.print(p.x);
101 Serial.print(", "); Serial.print(p.y);
102 Serial.println(")");
103 */
104
105
106 if (p.y < BOXSIZE)
107 {
108 oldcolor = currentcolor;
109
110 if (p.x < BOXSIZE)
111 {
112 currentcolor = ILI9341_RED;
113 tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
114 }
115 else if (p.x < BOXSIZE*2)
116 {
117 currentcolor = ILI9341_YELLOW;
118 tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
119 }
120 else if (p.x < BOXSIZE*3)
121 {
122 currentcolor = ILI9341_GREEN;
123 tft.drawRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
124 }
125 else if (p.x < BOXSIZE*4)
126 {
127 currentcolor = ILI9341_CYAN;
128 tft.drawRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
129 }
130 else if (p.x < BOXSIZE*5)
131 {
132 currentcolor = ILI9341_BLUE;
133 tft.drawRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
134 }
135 else if (p.x < BOXSIZE*6)
136 {
137 currentcolor = ILI9341_MAGENTA;
138 tft.drawRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
139 }
140
141 if (oldcolor != currentcolor)
142 {
143 if (oldcolor == ILI9341_RED)
144 tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
145 if (oldcolor == ILI9341_YELLOW)
146 tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW);
147 if (oldcolor == ILI9341_GREEN)
148 tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN);
149 if (oldcolor == ILI9341_CYAN)
150 tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_CYAN);
151 if (oldcolor == ILI9341_BLUE)
152 tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE);
153 if (oldcolor == ILI9341_MAGENTA)
154 tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA);
155 }
156 }
157 if (((p.y-PENRADIUS) > BOXSIZE) && ((p.y+PENRADIUS) < tft.height()))
158 {
159 tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);
160 }
161 }
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
BOXSIZE
#define BOXSIZE
Definition: breakouttouchpaint.ino:49
TS_MAXX
#define TS_MAXX
Definition: breakouttouchpaint.ino:32
TS_MAXY
#define TS_MAXY
Definition: breakouttouchpaint.ino:33
setup
static void setup(void)
Definition: breakouttouchpaint.ino:53
Adafruit_GFX::height
int16_t height(void) const
Definition: Adafruit_GFX.cpp:1025
oldcolor
static int oldcolor
Definition: breakouttouchpaint.ino:51
TS_MINY
#define TS_MINY
Definition: breakouttouchpaint.ino:31
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
Point::x
int x
Definition: SeeedTouchScreen.h:11
YM
#define YM
Definition: breakouttouchpaint.ino:26
currentcolor
static int currentcolor
Definition: breakouttouchpaint.ino:51
tft
Adafruit_ILI9341 tft
Definition: breakouttouchpaint.ino:41
ILI9341_BLACK
#define ILI9341_BLACK
Definition: Adafruit_ILI9341.h:98
ILI9341_RED
#define ILI9341_RED
Definition: Adafruit_ILI9341.h:110
ILI9341_YELLOW
#define ILI9341_YELLOW
Definition: Adafruit_ILI9341.h:112
MINPRESSURE
#define MINPRESSURE
Definition: breakouttouchpaint.ino:35
TouchScreen::getPoint
Point getPoint()
Definition: SeeedTouchScreen.cpp:74
TFT_CS
#define TFT_CS
Definition: breakouttouchpaint.ino:39
Adafruit_GFX.h
XM
#define XM
Definition: breakouttouchpaint.ino:25
MAXPRESSURE
#define MAXPRESSURE
Definition: breakouttouchpaint.ino:36
XP
#define XP
Definition: breakouttouchpaint.ino:27
ts
TouchScreen ts
Definition: breakouttouchpaint.ino:46
ILI9341_BLUE
#define ILI9341_BLUE
Definition: Adafruit_ILI9341.h:107
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
TS_MINX
#define TS_MINX
Definition: breakouttouchpaint.ino:30
TouchScreen
Definition: SeeedTouchScreen.h:21
loop
static void loop()
Definition: breakouttouchpaint.ino:77
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
TFT_DC
#define TFT_DC
Definition: breakouttouchpaint.ino:40
PENRADIUS
#define PENRADIUS
Definition: breakouttouchpaint.ino:50
Adafruit_ILI9341.h
Adafruit_ILI9341
Definition: Adafruit_ILI9341.h:118
YP
#define YP
NOT FOR USE WITH THE TOUCH SHIELD, ONLY FOR THE BREAKOUT!
Definition: breakouttouchpaint.ino:24