| Linduino 1.3.0 Linear Technology Arduino-Compatible Demonstration Board

CapTouchPaint.ino
Go to the documentation of this file.
1 /***************************************************
2 This is our touchscreen painting example for the Adafruit ILI9341
3 captouch shield
4 ----> http://www.adafruit.com/products/1947
5
6 Check out the links above for our tutorials and wiring diagrams
7
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> // this is needed for display
19 #include <Adafruit_ILI9341.h>
20 #include <Wire.h> // this is needed for FT6206
21 #include <Adafruit_FT6206.h>
22
23 // The FT6206 uses hardware I2C (SCL/SDA)
24 Adafruit_FT6206 ctp = Adafruit_FT6206();
25
26 // The display also uses hardware SPI, plus #9 & #10
27 #define TFT_CS 10
28 #define TFT_DC 9
29 Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
30
31 // Size of the color selection boxes and the paintbrush size
32 #define BOXSIZE 40
33 #define PENRADIUS 3
34 int oldcolor, currentcolor;
35
36 void setup(void)
37 {
38 while (!Serial); // used for leonardo debugging
39
40 Serial.begin(115200);
41 Serial.println(F("Cap Touch Paint!"));
42
43 tft.begin();
44
45 if (! ctp.begin(40)) // pass in 'sensitivity' coefficient
46 {
47 Serial.println("Couldn't start FT6206 touchscreen controller");
48 while (1);
49 }
50
51 Serial.println("Capacitive touchscreen started");
52
53 tft.fillScreen(ILI9341_BLACK);
54
55 // make the color selection boxes
56 tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
57 tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW);
58 tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN);
59 tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_CYAN);
60 tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE);
61 tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA);
62
63 // select the current color 'red'
64 tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
65 currentcolor = ILI9341_RED;
66 }
67
68 void loop()
69 {
70 // Wait for a touch
71 if (! ctp.touched())
72 {
73 return;
74 }
75
76 // Retrieve a point
77 TS_Point p = ctp.getPoint();
78
79 /*
80 // Print out raw data from screen touch controller
81 Serial.print("X = "); Serial.print(p.x);
82 Serial.print("\tY = "); Serial.print(p.y);
83 Serial.print(" -> ");
84 */
85
86 // flip it around to match the screen.
87 p.x = map(p.x, 0, 240, 240, 0);
88 p.y = map(p.y, 0, 320, 320, 0);
89
90 // Print out the remapped (rotated) coordinates
91 Serial.print("(");
92 Serial.print(p.x);
93 Serial.print(", ");
94 Serial.print(p.y);
95 Serial.println(")");
96
97
98 if (p.y < BOXSIZE)
99 {
100 oldcolor = currentcolor;
101
102 if (p.x < BOXSIZE)
103 {
104 currentcolor = ILI9341_RED;
105 tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
106 }
107 else if (p.x < BOXSIZE*2)
108 {
109 currentcolor = ILI9341_YELLOW;
110 tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
111 }
112 else if (p.x < BOXSIZE*3)
113 {
114 currentcolor = ILI9341_GREEN;
115 tft.drawRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
116 }
117 else if (p.x < BOXSIZE*4)
118 {
119 currentcolor = ILI9341_CYAN;
120 tft.drawRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
121 }
122 else if (p.x < BOXSIZE*5)
123 {
124 currentcolor = ILI9341_BLUE;
125 tft.drawRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
126 }
127 else if (p.x <= BOXSIZE*6)
128 {
129 currentcolor = ILI9341_MAGENTA;
130 tft.drawRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
131 }
132
133 if (oldcolor != currentcolor)
134 {
135 if (oldcolor == ILI9341_RED)
136 tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
137 if (oldcolor == ILI9341_YELLOW)
138 tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW);
139 if (oldcolor == ILI9341_GREEN)
140 tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN);
141 if (oldcolor == ILI9341_CYAN)
142 tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_CYAN);
143 if (oldcolor == ILI9341_BLUE)
144 tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE);
145 if (oldcolor == ILI9341_MAGENTA)
146 tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA);
147 }
148 }
149 if (((p.y-PENRADIUS) > BOXSIZE) && ((p.y+PENRADIUS) < tft.height()))
150 {
151 tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);
152 }
153 }
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
PENRADIUS
#define PENRADIUS
Definition: CapTouchPaint.ino:33
TS_Point::x
int16_t x
Definition: Adafruit_FT6206.h:58
tft
Adafruit_ILI9341 tft
Definition: CapTouchPaint.ino:29
loop
static void loop()
Definition: CapTouchPaint.ino:68
Adafruit_GFX::height
int16_t height(void) const
Definition: Adafruit_GFX.cpp:1025
BOXSIZE
#define BOXSIZE
Definition: CapTouchPaint.ino:32
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
ctp
Adafruit_FT6206 ctp
Definition: CapTouchPaint.ino:24
TFT_CS
#define TFT_CS
Definition: CapTouchPaint.ino:27
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
Adafruit_GFX.h
TFT_DC
#define TFT_DC
Definition: CapTouchPaint.ino:28
Adafruit_FT6206::begin
boolean begin(uint8_t thresh=FT6206_DEFAULT_THRESSHOLD)
Setups the HW.
Definition: Adafruit_FT6206.cpp:48
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
Adafruit_FT6206
Definition: Adafruit_FT6206.h:61
Adafruit_FT6206::getPoint
TS_Point getPoint(void)
Definition: Adafruit_FT6206.cpp:184
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
Adafruit_FT6206.h
oldcolor
static int oldcolor
Definition: CapTouchPaint.ino:34
Adafruit_ILI9341.h
currentcolor
static int currentcolor
Definition: CapTouchPaint.ino:34
Adafruit_FT6206::touched
boolean touched(void)
Definition: Adafruit_FT6206.cpp:102
Adafruit_ILI9341
Definition: Adafruit_ILI9341.h:118
setup
static void setup(void)
Definition: CapTouchPaint.ino:36