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
37 
38 // Size of the color selection boxes and the paintbrush size
39 #define BOXSIZE 40
40 #define PENRADIUS 3
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 
60 
61  // make the color selection boxes
62  tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
68 
69  // select the current color '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  {
111 
112  if (p.x < BOXSIZE)
113  {
115  tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
116  }
117  else if (p.x < BOXSIZE*2)
118  {
121  }
122  else if (p.x < BOXSIZE*3)
123  {
126  }
127  else if (p.x < BOXSIZE*4)
128  {
131  }
132  else if (p.x < BOXSIZE*5)
133  {
136  }
137  else if (p.x < BOXSIZE*6)
138  {
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)
149  if (oldcolor == ILI9341_GREEN)
151  if (oldcolor == ILI9341_CYAN)
153  if (oldcolor == ILI9341_BLUE)
155  if (oldcolor == 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 }
virtual void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
int16_t x
int16_t height(void) const
#define PENRADIUS
Definition: touchpaint.ino:40
Adafruit_STMPE610 ts
Definition: touchpaint.ino:31
#define TFT_DC
Definition: touchpaint.ino:35
int16_t width(void) const
#define ILI9341_WHITE
#define ILI9341_MAGENTA
static int currentcolor
Definition: touchpaint.ino:41
#define STMPE_CS
Definition: touchpaint.ino:30
#define ILI9341_BLACK
#define ILI9341_RED
int16_t y
#define ILI9341_YELLOW
#define BOXSIZE
Definition: touchpaint.ino:39
#define TS_MINY
Definition: touchpaint.ino:25
static void loop()
Definition: touchpaint.ino:75
#define ILI9341_BLUE
#define TS_MAXY
Definition: touchpaint.ino:27
#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)
static int oldcolor
Definition: touchpaint.ino:41
Adafruit_ILI9341 tft
Definition: touchpaint.ino:36
void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
#define TS_MAXX
Definition: touchpaint.ino:26
static void setup(void)
Definition: touchpaint.ino:43
#define TFT_CS
Definition: touchpaint.ino:34
#define TS_MINX
Definition: touchpaint.ino:24