Skip to content

Add TFT V2 FeatherWing support #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 56 additions & 28 deletions adafruit_featherwing/tft_featherwing.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,62 +19,90 @@
import board
import digitalio
import displayio
from adafruit_stmpe610 import Adafruit_STMPE610_SPI

try:
from adafruit_stmpe610 import Adafruit_STMPE610_SPI
except ImportError:
pass
try:
from adafruit_tsc2007 import TSC2007
except ImportError:
pass
try:
from adafruit_focaltouch import Adafruit_FocalTouch
except ImportError:
pass

import sdcardio
import storage

try:
from typing import Optional
from busio import SPI
from busio import SPI, I2C
from microcontroller import Pin
except ImportError:
pass


# pylint: disable-msg=too-few-public-methods, too-many-arguments
# pylint: disable-msg=too-few-public-methods, too-many-arguments, too-many-branches
class TFTFeatherWing:
"""Base class for TFT FeatherWings."""

def __init__(
self,
spi: Optional[SPI] = None,
cs: Optional[Pin] = None, # pylint: disable=invalid-name
dc: Optional[Pin] = None, # pylint: disable=invalid-name
ts_cs: Optional[Pin] = None,
sd_cs: Optional[Pin] = None,
i2c: Optional[I2C] = None,
cs_pin: Optional[Pin] = None,
dc_pin: Optional[Pin] = None,
ts_cs_pin: Optional[Pin] = None,
sd_cs_pin: Optional[Pin] = None,
irq_pin: Optional[Pin] = None,
resistive: Optional[bool] = True,
):
# Initialize Display Bus
displayio.release_displays()
if spi is None:
spi = board.SPI()
if cs is None:
cs = board.D9
if dc is None:
dc = board.D10

if ts_cs is None:
ts_cs = board.D6
if sd_cs is None:
sd_cs = board.D5
if cs_pin is None:
cs_pin = board.D9
if dc_pin is None:
dc_pin = board.D10

ts_cs = digitalio.DigitalInOut(ts_cs)
self._display_bus = displayio.FourWire(spi, command=dc_pin, chip_select=cs_pin)

self._display_bus = displayio.FourWire(spi, command=dc, chip_select=cs)
# Initialize SD Card
if sd_cs_pin is None:
sd_cs_pin = board.D5

self._sdcard = None
try:
self._sdcard = sdcardio.SDCard(spi, sd_cs)
self._sdcard = sdcardio.SDCard(spi, sd_cs_pin)
vfs = storage.VfsFat(self._sdcard)
storage.mount(vfs, "/sd")
except OSError as error:
print("No SD card found:", error)

# Initialize Touchscreen
self.touchscreen = None
"""Controller for the resistive touchscreen."""
try:
# the screen might not be ready from cold boot
time.sleep(0.8)
self.touchscreen = Adafruit_STMPE610_SPI(spi, ts_cs)
except RuntimeError:
# wait and try once more
time.sleep(1.0)
self.touchscreen = Adafruit_STMPE610_SPI(spi, ts_cs)
if resistive:
if i2c is None: # STMPE610
if ts_cs_pin is None:
ts_cs_pin = board.D6
ts_cs = digitalio.DigitalInOut(ts_cs_pin)
try:
# the screen might not be ready from cold boot
time.sleep(0.8)
self.touchscreen = Adafruit_STMPE610_SPI(spi, ts_cs)
except RuntimeError:
# wait and try once more
time.sleep(1.0)
self.touchscreen = Adafruit_STMPE610_SPI(spi, ts_cs)
else: # TSC2007
if irq_pin is None:
irq_pin = board.D6
irq = digitalio.DigitalInOut(irq_pin)
self.touchscreen = TSC2007(i2c, irq=irq)
else: # FocalTouch
if irq_pin is None:
irq_pin = board.D6
self.touchscreen = Adafruit_FocalTouch(i2c, irq_pin=irq_pin)
41 changes: 37 additions & 4 deletions adafruit_featherwing/tft_featherwing_24.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@
__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git"

import board
import adafruit_ili9341
from adafruit_featherwing.tft_featherwing import TFTFeatherWing

try:
from typing import Optional
from busio import SPI
from busio import SPI, I2C
from microcontroller import Pin
except ImportError:
pass


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

Expand All @@ -46,7 +46,40 @@ def __init__(
ts_cs: Optional[Pin] = None,
sd_cs: Optional[Pin] = None,
):
super().__init__(spi, cs, dc, ts_cs, sd_cs)
super().__init__(
spi, cs_pin=cs, dc_pin=dc, ts_cs_pin=ts_cs, sd_cs_pin=sd_cs, resistive=True
)
self.display = adafruit_ili9341.ILI9341(
self._display_bus, width=320, height=240
)
"""Display object for the FeatherWing's screen."""


# pylint: disable-msg=too-few-public-methods, too-many-arguments
class TFTFeatherWing24V2(TFTFeatherWing):
"""Class representing a `TFT FeatherWing 2.4 V2
<https://www.adafruit.com/product/3315>`_.
Attempts to mount the SD card to /sd.
"""

def __init__(
self,
spi: Optional[SPI] = None,
cs_pin: Optional[Pin] = None,
dc_pin: Optional[Pin] = None,
sd_cs_pin: Optional[Pin] = None,
i2c: Optional[I2C] = None,
):
if i2c is None:
i2c = board.I2C()
super().__init__(
spi,
cs_pin=cs_pin,
dc_pin=dc_pin,
sd_cs_pin=sd_cs_pin,
i2c=i2c,
resistive=True,
)
self.display = adafruit_ili9341.ILI9341(
self._display_bus, width=320, height=240
)
Expand Down
39 changes: 35 additions & 4 deletions adafruit_featherwing/tft_featherwing_35.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@
__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git"

import board
from adafruit_hx8357 import HX8357
from adafruit_featherwing.tft_featherwing import TFTFeatherWing

try:
from typing import Optional
from busio import SPI
from busio import SPI, I2C
from microcontroller import Pin
except ImportError:
pass


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

Expand All @@ -46,6 +46,37 @@ def __init__(
ts_cs: Optional[Pin] = None,
sd_cs: Optional[Pin] = None,
):
super().__init__(spi, cs, dc, ts_cs, sd_cs)
super().__init__(
spi, cs_pin=cs, dc_pin=dc, ts_cs_pin=ts_cs, sd_cs_pin=sd_cs, resistive=True
)
self.display = HX8357(self._display_bus, width=480, height=320)
"""Display object for the FeatherWing's screen."""


# pylint: disable-msg=too-few-public-methods, too-many-arguments
class TFTFeatherWing35V2(TFTFeatherWing):
"""Class representing a `TFT FeatherWing 3.5 V2
<https://www.adafruit.com/product/3651>`_.
Attempts to mount the SD card to /sd.
"""

def __init__(
self,
spi: Optional[SPI] = None,
cs_pin: Optional[Pin] = None,
dc_pin: Optional[Pin] = None,
sd_cs_pin: Optional[Pin] = None,
i2c: Optional[I2C] = None,
):
if i2c is None:
i2c = board.I2C()
super().__init__(
spi,
cs_pin=cs_pin,
dc_pin=dc_pin,
sd_cs_pin=sd_cs_pin,
i2c=i2c,
resistive=True,
)
self.display = HX8357(self._display_bus, width=480, height=320)
"""Display object for the FeatherWing's screen."""
28 changes: 28 additions & 0 deletions examples/featherwing_tft24v2_simpletest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""
This example will display a CircuitPython console and
print the coordinates of touchscreen presses.

It will also try to write and then read a file on the
SD Card.
"""
from adafruit_featherwing import tft_featherwing_24

tft_featherwing = tft_featherwing_24.TFTFeatherWing24V2()

try:
with open("/sd/tft_featherwing.txt", "w") as f:
f.write("Blinka\nBlackberry Q10 Keyboard")

with open("/sd/tft_featherwing.txt", "r") as f:
print(f.read())

except OSError as error:
print("Unable to write to SD Card.")


while True:
if not tft_featherwing.touchscreen.buffer_empty:
print(tft_featherwing.touchscreen.read_data())
27 changes: 27 additions & 0 deletions examples/featherwing_tft35v2_simpletest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""
This example will display a CircuitPython console and
print the coordinates of touchscreen presses.

It will also try to write and then read a file on the
SD Card.
"""
from adafruit_featherwing import tft_featherwing_35

tft_featherwing = tft_featherwing_35.TFTFeatherWing35V2()

try:
with open("/sd/tft_featherwing.txt", "w") as f:
f.write("Blinka\nBlackberry Q10 Keyboard")

with open("/sd/tft_featherwing.txt", "r") as f:
print(f.read())
except OSError as error:
print("Unable to write to SD Card.")


while True:
if not tft_featherwing.touchscreen.buffer_empty:
print(tft_featherwing.touchscreen.read_data())
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ adafruit-circuitpython-hx8357
adafruit-circuitpython-st7735r
adafruit-circuitpython-adxl34x
adafruit-circuitpython-seesaw
adafruit-circuitpython-tsc2007
adafruit-circuitpython-focaltouch