Skip to content

Commit 2ee27f9

Browse files
authored
Merge pull request #2619 from adafruit/tft_shield
adding examples for 2.8 tft update
2 parents 6f314bc + 182c20f commit 2ee27f9

File tree

8 files changed

+204
-0
lines changed

8 files changed

+204
-0
lines changed

TFT_Shield_TSC2007_Demos/Arduino_TouchPaint_TSC2007/.uno.test.only

Whitespace-only changes.
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// SPDX-FileCopyrightText: 2023 Limor Fried/Ladyada for Adafruit Industries
2+
// SPDX-License-Identifier: MIT
3+
4+
/***************************************************
5+
This is our touchscreen painting example for the updated Adafruit
6+
ILI9341 Shield with TSC2007
7+
----> http://www.adafruit.com/products/1651
8+
9+
Check out the links above for our tutorials and wiring diagrams
10+
These displays use SPI to communicate, 4 or 5 pins are required to
11+
interface (RST is optional)
12+
Adafruit invests time and resources providing this open source code,
13+
please support Adafruit and open-source hardware by purchasing
14+
products from Adafruit!
15+
16+
Written by Limor Fried/Ladyada for Adafruit Industries.
17+
MIT license, all text above must be included in any redistribution
18+
****************************************************/
19+
20+
21+
#include <Adafruit_GFX.h>
22+
#include <SPI.h>
23+
#include <Wire.h>
24+
#include <Adafruit_ILI9341.h>
25+
#include <Adafruit_TSC2007.h>
26+
27+
// This is calibration data for the raw touch data to the screen coordinates
28+
#define TS_MINX 150
29+
#define TS_MINY 130
30+
#define TS_MAXX 3800
31+
#define TS_MAXY 4000
32+
#define TS_MIN_PRESSURE 200
33+
34+
Adafruit_TSC2007 ts;
35+
36+
// The display also uses hardware SPI, plus #9 & #10
37+
#define TFT_CS 10
38+
#define TFT_DC 9
39+
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
40+
41+
// Size of the color selection boxes and the paintbrush size
42+
#define BOXSIZE 40
43+
#define PENRADIUS 3
44+
int oldcolor, currentcolor;
45+
46+
void setup(void) {
47+
48+
Serial.begin(115200);
49+
// while (!Serial) delay(10);
50+
51+
tft.begin();
52+
53+
if (!ts.begin()) {
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+
void loop(){
75+
uint16_t x, y, z1, z2;
76+
if (ts.read_touch(&x, &y, &z1, &z2) && (z1 > TS_MIN_PRESSURE)) {
77+
78+
Serial.print("Touch point: (");
79+
Serial.print(x); Serial.print(", ");
80+
Serial.print(y); Serial.print(", ");
81+
Serial.print(z1); Serial.print(" / ");
82+
Serial.print(z2); Serial.println(")");
83+
84+
// Scale from ~0->4000 to tft.width using the calibration #'s
85+
x = map(x, TS_MINX, TS_MAXX, 0, tft.width());
86+
y = map(y, TS_MINY, TS_MAXY, 0, tft.height());
87+
88+
if (y < BOXSIZE) {
89+
oldcolor = currentcolor;
90+
91+
if (x < BOXSIZE) {
92+
currentcolor = ILI9341_RED;
93+
tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
94+
} else if (x < BOXSIZE*2) {
95+
currentcolor = ILI9341_YELLOW;
96+
tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
97+
} else if (x < BOXSIZE*3) {
98+
currentcolor = ILI9341_GREEN;
99+
tft.drawRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
100+
} else if (x < BOXSIZE*4) {
101+
currentcolor = ILI9341_CYAN;
102+
tft.drawRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
103+
} else if (x < BOXSIZE*5) {
104+
currentcolor = ILI9341_BLUE;
105+
tft.drawRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
106+
} else if (x < BOXSIZE*6) {
107+
currentcolor = ILI9341_MAGENTA;
108+
tft.drawRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
109+
}
110+
111+
if (oldcolor != currentcolor) {
112+
if (oldcolor == ILI9341_RED)
113+
tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
114+
if (oldcolor == ILI9341_YELLOW)
115+
tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW);
116+
if (oldcolor == ILI9341_GREEN)
117+
tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN);
118+
if (oldcolor == ILI9341_CYAN)
119+
tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_CYAN);
120+
if (oldcolor == ILI9341_BLUE)
121+
tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE);
122+
if (oldcolor == ILI9341_MAGENTA)
123+
tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA);
124+
}
125+
}
126+
if (((y-PENRADIUS) > BOXSIZE) && ((y+PENRADIUS) < tft.height())) {
127+
tft.fillCircle(x, y, PENRADIUS, currentcolor);
128+
}
129+
}
130+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
"""
5+
This test will initialize the display using displayio and display
6+
a bitmap image. The image advances when the touch screen is touched.
7+
8+
Pinouts are for the 2.8" TFT Shield
9+
"""
10+
import os
11+
import board
12+
import displayio
13+
import adafruit_ili9341
14+
import adafruit_tsc2007
15+
16+
# Release any resources currently in use for the displays
17+
displayio.release_displays()
18+
19+
# Use Hardware SPI
20+
spi = board.SPI()
21+
22+
# Use Software SPI if you have a shield with pins 11-13 jumpered
23+
# import busio
24+
# spi = busio.SPI(board.D11, board.D13)
25+
26+
tft_cs = board.D10
27+
tft_dc = board.D9
28+
29+
display_width = 320
30+
display_height = 240
31+
32+
display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs)
33+
display = adafruit_ili9341.ILI9341(display_bus, width=display_width, height=display_height)
34+
35+
i2c = board.I2C()
36+
37+
irq_dio = None
38+
tsc = adafruit_tsc2007.TSC2007(i2c, irq=irq_dio)
39+
40+
groups = []
41+
images = []
42+
for filename in os.listdir('/'):
43+
if filename.lower().endswith('.bmp') and not filename.startswith('.'):
44+
images.append("/"+filename)
45+
print(images)
46+
47+
for i in range(len(images)):
48+
splash = displayio.Group()
49+
bitmap = displayio.OnDiskBitmap(images[i])
50+
tile_grid = displayio.TileGrid(bitmap, pixel_shader=bitmap.pixel_shader)
51+
splash.append(tile_grid)
52+
groups.append(splash)
53+
54+
index = 0
55+
touch_state = False
56+
57+
display.show(groups[index])
58+
while True:
59+
if tsc.touched and not touch_state:
60+
point = tsc.touch
61+
touch_state = True
62+
if point["pressure"] < 200: # ignore touches with no 'pressure' as false
63+
continue
64+
print("Touchpoint: (%d, %d, %d)" % (point["x"], point["y"], point["pressure"]))
65+
# left side of the screen
66+
if point["y"] < 2000:
67+
index = (index - 1) % len(images)
68+
display.show(groups[index])
69+
# right side of the screen
70+
else:
71+
index = (index + 1) % len(images)
72+
display.show(groups[index])
73+
if not tsc.touched and touch_state:
74+
touch_state = False
76.1 KB
Binary file not shown.
76.1 KB
Binary file not shown.
76.1 KB
Binary file not shown.
76.1 KB
Binary file not shown.
76.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)