diff --git a/adafruit_featherwing/tft_featherwing.py b/adafruit_featherwing/tft_featherwing.py index ba6ae62..c64d995 100644 --- a/adafruit_featherwing/tft_featherwing.py +++ b/adafruit_featherwing/tft_featherwing.py @@ -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) diff --git a/adafruit_featherwing/tft_featherwing_24.py b/adafruit_featherwing/tft_featherwing_24.py index ca4d1b0..81e9631 100644 --- a/adafruit_featherwing/tft_featherwing_24.py +++ b/adafruit_featherwing/tft_featherwing_24.py @@ -20,12 +20,13 @@ __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 @@ -33,8 +34,7 @@ # pylint: disable-msg=too-few-public-methods, too-many-arguments class TFTFeatherWing24(TFTFeatherWing): - """Class representing an `TFT FeatherWing 2.4 - `_. + """Class representing a TFT FeatherWing 2.4 V1 Attempts to mount the SD card to /sd. """ @@ -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 + `_. + 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 ) diff --git a/adafruit_featherwing/tft_featherwing_35.py b/adafruit_featherwing/tft_featherwing_35.py index 821fda0..b5d56d6 100644 --- a/adafruit_featherwing/tft_featherwing_35.py +++ b/adafruit_featherwing/tft_featherwing_35.py @@ -20,12 +20,13 @@ __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 @@ -33,8 +34,7 @@ # pylint: disable-msg=too-few-public-methods, too-many-arguments class TFTFeatherWing35(TFTFeatherWing): - """Class representing a `TFT FeatherWing 3.5 - `_. + """Class representing a TFT FeatherWing 3.5 V1 Attempts to mount the SD card to /sd. """ @@ -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 + `_. + 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.""" diff --git a/examples/featherwing_tft24v2_simpletest.py b/examples/featherwing_tft24v2_simpletest.py new file mode 100644 index 0000000..69b6cf5 --- /dev/null +++ b/examples/featherwing_tft24v2_simpletest.py @@ -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()) diff --git a/examples/featherwing_tft35v2_simpletest.py b/examples/featherwing_tft35v2_simpletest.py new file mode 100644 index 0000000..e02a270 --- /dev/null +++ b/examples/featherwing_tft35v2_simpletest.py @@ -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()) diff --git a/requirements.txt b/requirements.txt index 4cf9999..556856e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -18,3 +18,5 @@ adafruit-circuitpython-hx8357 adafruit-circuitpython-st7735r adafruit-circuitpython-adxl34x adafruit-circuitpython-seesaw +adafruit-circuitpython-tsc2007 +adafruit-circuitpython-focaltouch