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
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
47 
48 // Size of the color selection boxes and the paintbrush size
49 #define BOXSIZE 40
50 #define PENRADIUS 3
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();
62 
63  // make the color selection boxes
64  tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
70 
71  // select the current color '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  {
109 
110  if (p.x < BOXSIZE)
111  {
113  tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
114  }
115  else if (p.x < BOXSIZE*2)
116  {
119  }
120  else if (p.x < BOXSIZE*3)
121  {
124  }
125  else if (p.x < BOXSIZE*4)
126  {
129  }
130  else if (p.x < BOXSIZE*5)
131  {
134  }
135  else if (p.x < BOXSIZE*6)
136  {
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)
147  if (oldcolor == ILI9341_GREEN)
149  if (oldcolor == ILI9341_CYAN)
151  if (oldcolor == ILI9341_BLUE)
153  if (oldcolor == 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 }
virtual void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
#define BOXSIZE
#define TS_MAXX
#define TS_MAXY
static void setup(void)
int16_t height(void) const
static int oldcolor
#define TS_MINY
int16_t width(void) const
#define ILI9341_WHITE
#define ILI9341_MAGENTA
#define YM
static int currentcolor
Adafruit_ILI9341 tft
#define ILI9341_BLACK
#define ILI9341_RED
#define ILI9341_YELLOW
#define MINPRESSURE
#define TFT_CS
#define XM
#define MAXPRESSURE
#define XP
TouchScreen ts
#define ILI9341_BLUE
#define ILI9341_CYAN
void fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color)
#define ILI9341_GREEN
void fillScreen(uint16_t color)
#define TS_MINX
static void loop()
void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
#define TFT_DC
#define PENRADIUS
#define YP
NOT FOR USE WITH THE TOUCH SHIELD, ONLY FOR THE BREAKOUT!