Skip to content

Commit 8c3fccd

Browse files
authored
Merge pull request #81 from makermelissa/master
Add TFT V2 FeatherWing support
2 parents ec17737 + ef57c4c commit 8c3fccd

File tree

6 files changed

+185
-36
lines changed

6 files changed

+185
-36
lines changed

adafruit_featherwing/tft_featherwing.py

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,62 +19,90 @@
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,
41-
cs: Optional[Pin] = None, # pylint: disable=invalid-name
42-
dc: Optional[Pin] = None, # pylint: disable=invalid-name
43-
ts_cs: Optional[Pin] = None,
44-
sd_cs: Optional[Pin] = None,
54+
i2c: Optional[I2C] = None,
55+
cs_pin: Optional[Pin] = None,
56+
dc_pin: Optional[Pin] = None,
57+
ts_cs_pin: Optional[Pin] = None,
58+
sd_cs_pin: Optional[Pin] = None,
59+
irq_pin: 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()
49-
if cs is None:
50-
cs = board.D9
51-
if dc is None:
52-
dc = board.D10
53-
54-
if ts_cs is None:
55-
ts_cs = board.D6
56-
if sd_cs is None:
57-
sd_cs = board.D5
66+
if cs_pin is None:
67+
cs_pin = board.D9
68+
if dc_pin is None:
69+
dc_pin = board.D10
5870

59-
ts_cs = digitalio.DigitalInOut(ts_cs)
71+
self._display_bus = displayio.FourWire(spi, command=dc_pin, chip_select=cs_pin)
6072

61-
self._display_bus = displayio.FourWire(spi, command=dc, chip_select=cs)
73+
# Initialize SD Card
74+
if sd_cs_pin is None:
75+
sd_cs_pin = board.D5
6276

6377
self._sdcard = None
6478
try:
65-
self._sdcard = sdcardio.SDCard(spi, sd_cs)
79+
self._sdcard = sdcardio.SDCard(spi, sd_cs_pin)
6680
vfs = storage.VfsFat(self._sdcard)
6781
storage.mount(vfs, "/sd")
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_pin is None:
90+
ts_cs_pin = board.D6
91+
ts_cs = digitalio.DigitalInOut(ts_cs_pin)
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_pin is None:
102+
irq_pin = board.D6
103+
irq = digitalio.DigitalInOut(irq_pin)
104+
self.touchscreen = TSC2007(i2c, irq=irq)
105+
else: # FocalTouch
106+
if irq_pin is None:
107+
irq_pin = board.D6
108+
self.touchscreen = Adafruit_FocalTouch(i2c, irq_pin=irq_pin)

adafruit_featherwing/tft_featherwing_24.py

Lines changed: 37 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,40 @@ 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__(
50+
spi, cs_pin=cs, dc_pin=dc, ts_cs_pin=ts_cs, sd_cs_pin=sd_cs, resistive=True
51+
)
52+
self.display = adafruit_ili9341.ILI9341(
53+
self._display_bus, width=320, height=240
54+
)
55+
"""Display object for the FeatherWing's screen."""
56+
57+
58+
# pylint: disable-msg=too-few-public-methods, too-many-arguments
59+
class TFTFeatherWing24V2(TFTFeatherWing):
60+
"""Class representing a `TFT FeatherWing 2.4 V2
61+
<https://www.adafruit.com/product/3315>`_.
62+
Attempts to mount the SD card to /sd.
63+
"""
64+
65+
def __init__(
66+
self,
67+
spi: Optional[SPI] = None,
68+
cs_pin: Optional[Pin] = None,
69+
dc_pin: Optional[Pin] = None,
70+
sd_cs_pin: Optional[Pin] = None,
71+
i2c: Optional[I2C] = None,
72+
):
73+
if i2c is None:
74+
i2c = board.I2C()
75+
super().__init__(
76+
spi,
77+
cs_pin=cs_pin,
78+
dc_pin=dc_pin,
79+
sd_cs_pin=sd_cs_pin,
80+
i2c=i2c,
81+
resistive=True,
82+
)
5083
self.display = adafruit_ili9341.ILI9341(
5184
self._display_bus, width=320, height=240
5285
)

adafruit_featherwing/tft_featherwing_35.py

Lines changed: 35 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,37 @@ 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__(
50+
spi, cs_pin=cs, dc_pin=dc, ts_cs_pin=ts_cs, sd_cs_pin=sd_cs, resistive=True
51+
)
52+
self.display = HX8357(self._display_bus, width=480, height=320)
53+
"""Display object for the FeatherWing's screen."""
54+
55+
56+
# pylint: disable-msg=too-few-public-methods, too-many-arguments
57+
class TFTFeatherWing35V2(TFTFeatherWing):
58+
"""Class representing a `TFT FeatherWing 3.5 V2
59+
<https://www.adafruit.com/product/3651>`_.
60+
Attempts to mount the SD card to /sd.
61+
"""
62+
63+
def __init__(
64+
self,
65+
spi: Optional[SPI] = None,
66+
cs_pin: Optional[Pin] = None,
67+
dc_pin: Optional[Pin] = None,
68+
sd_cs_pin: Optional[Pin] = None,
69+
i2c: Optional[I2C] = None,
70+
):
71+
if i2c is None:
72+
i2c = board.I2C()
73+
super().__init__(
74+
spi,
75+
cs_pin=cs_pin,
76+
dc_pin=dc_pin,
77+
sd_cs_pin=sd_cs_pin,
78+
i2c=i2c,
79+
resistive=True,
80+
)
5081
self.display = HX8357(self._display_bus, width=480, height=320)
5182
"""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)