Skip to content

Commit 93fff1f

Browse files
authored
Merge pull request #2652 from adafruit/tft_featherwing
Adding example code for TFT FeatherWing V2
2 parents 266b80d + 537dd7d commit 93fff1f

8 files changed

Lines changed: 196 additions & 0 deletions

File tree

TFT_FeatherWing_TSC2007_Demos/Arduino_TouchPaint_TSC2007/.feather_rp2040.test.only

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