Skip to content

Commit 4e7b0f4

Browse files
committed
Add TFT V2 FeatherWing support
1 parent ec17737 commit 4e7b0f4

File tree

6 files changed

+157
-26
lines changed

6 files changed

+157
-26
lines changed

adafruit_featherwing/tft_featherwing.py

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,47 @@
1919
import board
2020
import digitalio
2121
import displayio
22-
from adafruit_stmpe610 import Adafruit_STMPE610_SPI
22+
23+
try:
24+
from adafruit_stmpe610 import Adafruit_STMPE610_SPI
25+
except ImportError:
26+
pass
27+
try:
28+
from adafruit_tsc2007 import TSC2007
29+
except ImportError:
30+
pass
31+
try:
32+
from adafruit_focaltouch import Adafruit_FocalTouch
33+
except ImportError:
34+
pass
35+
2336
import sdcardio
2437
import storage
2538

2639
try:
2740
from typing import Optional
28-
from busio import SPI
41+
from busio import SPI, I2C
2942
from microcontroller import Pin
3043
except ImportError:
3144
pass
3245

3346

34-
# pylint: disable-msg=too-few-public-methods, too-many-arguments
47+
# pylint: disable-msg=too-few-public-methods, too-many-arguments, too-many-branches
3548
class TFTFeatherWing:
3649
"""Base class for TFT FeatherWings."""
3750

3851
def __init__(
3952
self,
4053
spi: Optional[SPI] = None,
54+
i2c: Optional[I2C] = None,
4155
cs: Optional[Pin] = None, # pylint: disable=invalid-name
4256
dc: Optional[Pin] = None, # pylint: disable=invalid-name
4357
ts_cs: Optional[Pin] = None,
4458
sd_cs: Optional[Pin] = None,
59+
irq: Optional[Pin] = None,
60+
resistive: Optional[bool] = True,
4561
):
62+
# Initialize Display Bus
4663
displayio.release_displays()
4764
if spi is None:
4865
spi = board.SPI()
@@ -51,15 +68,12 @@ def __init__(
5168
if dc is None:
5269
dc = board.D10
5370

54-
if ts_cs is None:
55-
ts_cs = board.D6
71+
self._display_bus = displayio.FourWire(spi, command=dc, chip_select=cs)
72+
73+
# Initialize SD Card
5674
if sd_cs is None:
5775
sd_cs = board.D5
5876

59-
ts_cs = digitalio.DigitalInOut(ts_cs)
60-
61-
self._display_bus = displayio.FourWire(spi, command=dc, chip_select=cs)
62-
6377
self._sdcard = None
6478
try:
6579
self._sdcard = sdcardio.SDCard(spi, sd_cs)
@@ -68,13 +82,27 @@ def __init__(
6882
except OSError as error:
6983
print("No SD card found:", error)
7084

85+
# Initialize Touchscreen
7186
self.touchscreen = None
72-
"""Controller for the resistive touchscreen."""
73-
try:
74-
# the screen might not be ready from cold boot
75-
time.sleep(0.8)
76-
self.touchscreen = Adafruit_STMPE610_SPI(spi, ts_cs)
77-
except RuntimeError:
78-
# wait and try once more
79-
time.sleep(1.0)
80-
self.touchscreen = Adafruit_STMPE610_SPI(spi, ts_cs)
87+
if resistive:
88+
if i2c is None: # STMPE610
89+
if ts_cs is None:
90+
ts_cs = board.D6
91+
ts_cs = digitalio.DigitalInOut(ts_cs)
92+
try:
93+
# the screen might not be ready from cold boot
94+
time.sleep(0.8)
95+
self.touchscreen = Adafruit_STMPE610_SPI(spi, ts_cs)
96+
except RuntimeError:
97+
# wait and try once more
98+
time.sleep(1.0)
99+
self.touchscreen = Adafruit_STMPE610_SPI(spi, ts_cs)
100+
else: # TSC2007
101+
if irq is None:
102+
irq = board.D6
103+
irq = digitalio.DigitalInOut(irq)
104+
self.touchscreen = TSC2007(i2c, irq=irq)
105+
else: # FocalTouch
106+
if irq is None:
107+
irq = board.D6
108+
self.touchscreen = Adafruit_FocalTouch(i2c, irq_pin=irq)

adafruit_featherwing/tft_featherwing_24.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,21 @@
2020
__version__ = "0.0.0+auto.0"
2121
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git"
2222

23+
import board
2324
import adafruit_ili9341
2425
from adafruit_featherwing.tft_featherwing import TFTFeatherWing
2526

2627
try:
2728
from typing import Optional
28-
from busio import SPI
29+
from busio import SPI, I2C
2930
from microcontroller import Pin
3031
except ImportError:
3132
pass
3233

3334

3435
# pylint: disable-msg=too-few-public-methods, too-many-arguments
3536
class TFTFeatherWing24(TFTFeatherWing):
36-
"""Class representing an `TFT FeatherWing 2.4
37-
<https://www.adafruit.com/product/3315>`_.
37+
"""Class representing a TFT FeatherWing 2.4 V1
3838
Attempts to mount the SD card to /sd.
3939
"""
4040

@@ -46,7 +46,31 @@ def __init__(
4646
ts_cs: Optional[Pin] = None,
4747
sd_cs: Optional[Pin] = None,
4848
):
49-
super().__init__(spi, cs, dc, ts_cs, sd_cs)
49+
super().__init__(spi, cs=cs, dc=dc, ts_cs=ts_cs, sd_cs=sd_cs, resistive=True)
50+
self.display = adafruit_ili9341.ILI9341(
51+
self._display_bus, width=320, height=240
52+
)
53+
"""Display object for the FeatherWing's screen."""
54+
55+
56+
# pylint: disable-msg=too-few-public-methods, too-many-arguments
57+
class TFTFeatherWing24V2(TFTFeatherWing):
58+
"""Class representing a `TFT FeatherWing 2.4 V2
59+
<https://www.adafruit.com/product/3315>`_.
60+
Attempts to mount the SD card to /sd.
61+
"""
62+
63+
def __init__(
64+
self,
65+
spi: Optional[SPI] = None,
66+
cs: Optional[Pin] = None,
67+
dc: Optional[Pin] = None,
68+
sd_cs: Optional[Pin] = None,
69+
i2c: Optional[I2C] = None,
70+
):
71+
if i2c is None:
72+
i2c = board.I2C()
73+
super().__init__(spi, cs=cs, dc=dc, sd_cs=sd_cs, i2c=i2c, resistive=True)
5074
self.display = adafruit_ili9341.ILI9341(
5175
self._display_bus, width=320, height=240
5276
)

adafruit_featherwing/tft_featherwing_35.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,21 @@
2020
__version__ = "0.0.0+auto.0"
2121
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git"
2222

23+
import board
2324
from adafruit_hx8357 import HX8357
2425
from adafruit_featherwing.tft_featherwing import TFTFeatherWing
2526

2627
try:
2728
from typing import Optional
28-
from busio import SPI
29+
from busio import SPI, I2C
2930
from microcontroller import Pin
3031
except ImportError:
3132
pass
3233

3334

3435
# pylint: disable-msg=too-few-public-methods, too-many-arguments
3536
class TFTFeatherWing35(TFTFeatherWing):
36-
"""Class representing a `TFT FeatherWing 3.5
37-
<https://www.adafruit.com/product/3651>`_.
37+
"""Class representing a TFT FeatherWing 3.5 V1
3838
Attempts to mount the SD card to /sd.
3939
"""
4040

@@ -46,6 +46,28 @@ def __init__(
4646
ts_cs: Optional[Pin] = None,
4747
sd_cs: Optional[Pin] = None,
4848
):
49-
super().__init__(spi, cs, dc, ts_cs, sd_cs)
49+
super().__init__(spi, cs=cs, dc=dc, ts_cs=ts_cs, sd_cs=sd_cs, resistive=True)
50+
self.display = HX8357(self._display_bus, width=480, height=320)
51+
"""Display object for the FeatherWing's screen."""
52+
53+
54+
# pylint: disable-msg=too-few-public-methods, too-many-arguments
55+
class TFTFeatherWing35V2(TFTFeatherWing):
56+
"""Class representing a `TFT FeatherWing 3.5 V2
57+
<https://www.adafruit.com/product/3651>`_.
58+
Attempts to mount the SD card to /sd.
59+
"""
60+
61+
def __init__(
62+
self,
63+
spi: Optional[SPI] = None,
64+
cs: Optional[Pin] = None,
65+
dc: Optional[Pin] = None,
66+
sd_cs: Optional[Pin] = None,
67+
i2c: Optional[I2C] = None,
68+
):
69+
if i2c is None:
70+
i2c = board.I2C()
71+
super().__init__(spi, cs=cs, dc=dc, sd_cs=sd_cs, i2c=i2c, resistive=True)
5072
self.display = HX8357(self._display_bus, width=480, height=320)
5173
"""Display object for the FeatherWing's screen."""
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
"""
5+
This example will display a CircuitPython console and
6+
print the coordinates of touchscreen presses.
7+
8+
It will also try to write and then read a file on the
9+
SD Card.
10+
"""
11+
from adafruit_featherwing import tft_featherwing_24
12+
13+
tft_featherwing = tft_featherwing_24.TFTFeatherWing24V2()
14+
15+
try:
16+
with open("/sd/tft_featherwing.txt", "w") as f:
17+
f.write("Blinka\nBlackberry Q10 Keyboard")
18+
19+
with open("/sd/tft_featherwing.txt", "r") as f:
20+
print(f.read())
21+
22+
except OSError as error:
23+
print("Unable to write to SD Card.")
24+
25+
26+
while True:
27+
if not tft_featherwing.touchscreen.buffer_empty:
28+
print(tft_featherwing.touchscreen.read_data())
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
"""
5+
This example will display a CircuitPython console and
6+
print the coordinates of touchscreen presses.
7+
8+
It will also try to write and then read a file on the
9+
SD Card.
10+
"""
11+
from adafruit_featherwing import tft_featherwing_35
12+
13+
tft_featherwing = tft_featherwing_35.TFTFeatherWing35V2()
14+
15+
try:
16+
with open("/sd/tft_featherwing.txt", "w") as f:
17+
f.write("Blinka\nBlackberry Q10 Keyboard")
18+
19+
with open("/sd/tft_featherwing.txt", "r") as f:
20+
print(f.read())
21+
except OSError as error:
22+
print("Unable to write to SD Card.")
23+
24+
25+
while True:
26+
if not tft_featherwing.touchscreen.buffer_empty:
27+
print(tft_featherwing.touchscreen.read_data())

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ adafruit-circuitpython-hx8357
1818
adafruit-circuitpython-st7735r
1919
adafruit-circuitpython-adxl34x
2020
adafruit-circuitpython-seesaw
21+
adafruit-circuitpython-tsc2007
22+
adafruit-circuitpython-focaltouch

0 commit comments

Comments
 (0)