From c8e76bb87049f671a78112cec2ed412fc23083fb Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 15 Mar 2021 20:43:47 -0500 Subject: [PATCH 1/6] adding tft 3.5 inch helper class --- adafruit_featherwing/tft_featherwing_35.py | 60 ++++++++++++++++++++++ examples/featherwing_tft35_simpletest.py | 29 +++++++++++ 2 files changed, 89 insertions(+) create mode 100644 adafruit_featherwing/tft_featherwing_35.py create mode 100644 examples/featherwing_tft35_simpletest.py diff --git a/adafruit_featherwing/tft_featherwing_35.py b/adafruit_featherwing/tft_featherwing_35.py new file mode 100644 index 0000000..102f4c0 --- /dev/null +++ b/adafruit_featherwing/tft_featherwing_35.py @@ -0,0 +1,60 @@ +# SPDX-FileCopyrightText: 2020 Melissa LeBlanc-Williams for Adafruit Industries +# SPDX-FileCopyrightText: 2020 Foamyguy for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +""" +`adafruit_featherwing.tft_featherwing_35` +==================================================== + +Helper for using the `TFT FeatherWing 3.5"` +`_. + +* Author(s): Melissa LeBlanc-Williams, Foamyguy + +Requires: +* adafruit_hx8357 +* adafruit_stmpe610 +""" + +__version__ = "0.0.0-auto.0" +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git" + +import board +import digitalio +import displayio +from adafruit_hx8357 import HX8357 +from adafruit_stmpe610 import Adafruit_STMPE610_SPI +import sdcardio +import storage + +# pylint: disable-msg=too-few-public-methods +class TFTFeatherWing35: + """Class representing an `TFT FeatherWing 3.5 + `_. + + """ + + def __init__(self, spi=None, cs=None, dc=None): + displayio.release_displays() + if spi is None: + spi = board.SPI() + if cs is None: + cs = board.D9 + if dc is None: + dc = board.D10 + + ts_cs = digitalio.DigitalInOut(board.D6) + self.touchscreen = Adafruit_STMPE610_SPI(spi, ts_cs) + + display_bus = displayio.FourWire(spi, command=dc, chip_select=cs) + self.display = HX8357(display_bus, width=480, height=320) + + sd_cs = board.D5 + self._sdcard = None + try: + self._sdcard = sdcardio.SDCard(spi, sd_cs) + vfs = storage.VfsFat(self._sdcard) + storage.mount(vfs, "/sd") + except OSError as error: + print("No SD card found:", error) diff --git a/examples/featherwing_tft35_simpletest.py b/examples/featherwing_tft35_simpletest.py new file mode 100644 index 0000000..475073a --- /dev/null +++ b/examples/featherwing_tft35_simpletest.py @@ -0,0 +1,29 @@ +# 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.TFTFeatherWing35() + +try: + f = open("/sd/tft_featherwing.txt", "w") + f.write("Blinka\nBlackberry Q10 Keyboard") + f.close() + + f = open("/sd/tft_featherwing.txt", "r") + print(f.read()) + f.close() +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()) From 4748599196defcd01f66b978f80527f026adb0a7 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 15 Mar 2021 21:03:12 -0500 Subject: [PATCH 2/6] refactor TFT Featherwings to use base class for avoiding duplicate --- adafruit_featherwing/tft_featherwing.py | 53 ++++++++++++++++++++++ adafruit_featherwing/tft_featherwing_24.py | 38 ++++------------ adafruit_featherwing/tft_featherwing_35.py | 35 +++----------- 3 files changed, 68 insertions(+), 58 deletions(-) create mode 100644 adafruit_featherwing/tft_featherwing.py diff --git a/adafruit_featherwing/tft_featherwing.py b/adafruit_featherwing/tft_featherwing.py new file mode 100644 index 0000000..5d636d7 --- /dev/null +++ b/adafruit_featherwing/tft_featherwing.py @@ -0,0 +1,53 @@ +# SPDX-FileCopyrightText: 2020 Melissa LeBlanc-Williams for Adafruit Industries +# SPDX-FileCopyrightText: 2020 Foamyguy for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +""" +`adafruit_featherwing.tft_featherwing` +==================================================== + +Super class for helpers for using the TFT FeatherWing devices +see tft_featherwng_24 and tft_featherwing_35 + +* Author(s): Melissa LeBlanc-Williams, Foamyguy + +Requires: +* adafruit_stmpe610 +""" +import board +import digitalio +import displayio +from adafruit_stmpe610 import Adafruit_STMPE610_SPI +import sdcardio +import storage + +# pylint: disable-msg=too-few-public-methods +class TFTFeatherWing: + """Class representing an `TFT FeatherWing 2.4 + `_. + + """ + + def __init__(self, spi=None, cs=None, dc=None): + displayio.release_displays() + if spi is None: + spi = board.SPI() + if cs is None: + cs = board.D9 + if dc is None: + dc = board.D10 + + ts_cs = digitalio.DigitalInOut(board.D6) + self.touchscreen = Adafruit_STMPE610_SPI(spi, ts_cs) + + self._display_bus = displayio.FourWire(spi, command=dc, chip_select=cs) + + sd_cs = board.D5 + self._sdcard = None + try: + self._sdcard = sdcardio.SDCard(spi, sd_cs) + vfs = storage.VfsFat(self._sdcard) + storage.mount(vfs, "/sd") + except OSError as error: + print("No SD card found:", error) diff --git a/adafruit_featherwing/tft_featherwing_24.py b/adafruit_featherwing/tft_featherwing_24.py index 84eb8d3..51da67c 100644 --- a/adafruit_featherwing/tft_featherwing_24.py +++ b/adafruit_featherwing/tft_featherwing_24.py @@ -20,41 +20,21 @@ __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git" -import board -import digitalio -import displayio + import adafruit_ili9341 -from adafruit_stmpe610 import Adafruit_STMPE610_SPI -import sdcardio -import storage # pylint: disable-msg=too-few-public-methods -class TFTFeatherWing24: +from adafruit_featherwing.tft_featherwing import TFTFeatherWing + + +class TFTFeatherWing24(TFTFeatherWing): """Class representing an `TFT FeatherWing 2.4 `_. """ def __init__(self, spi=None, cs=None, dc=None): - displayio.release_displays() - if spi is None: - spi = board.SPI() - if cs is None: - cs = board.D9 - if dc is None: - dc = board.D10 - - ts_cs = digitalio.DigitalInOut(board.D6) - self.touchscreen = Adafruit_STMPE610_SPI(spi, ts_cs) - - display_bus = displayio.FourWire(spi, command=dc, chip_select=cs) - self.display = adafruit_ili9341.ILI9341(display_bus, width=320, height=240) - - sd_cs = board.D5 - self._sdcard = None - try: - self._sdcard = sdcardio.SDCard(spi, sd_cs) - vfs = storage.VfsFat(self._sdcard) - storage.mount(vfs, "/sd") - except OSError as error: - print("No SD card found:", error) + super().__init__(spi, cs, dc) + 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 102f4c0..a3b0a2c 100644 --- a/adafruit_featherwing/tft_featherwing_35.py +++ b/adafruit_featherwing/tft_featherwing_35.py @@ -20,41 +20,18 @@ __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git" -import board -import digitalio -import displayio from adafruit_hx8357 import HX8357 -from adafruit_stmpe610 import Adafruit_STMPE610_SPI -import sdcardio -import storage # pylint: disable-msg=too-few-public-methods -class TFTFeatherWing35: +from adafruit_featherwing.tft_featherwing import TFTFeatherWing + + +class TFTFeatherWing35(TFTFeatherWing): """Class representing an `TFT FeatherWing 3.5 `_. """ def __init__(self, spi=None, cs=None, dc=None): - displayio.release_displays() - if spi is None: - spi = board.SPI() - if cs is None: - cs = board.D9 - if dc is None: - dc = board.D10 - - ts_cs = digitalio.DigitalInOut(board.D6) - self.touchscreen = Adafruit_STMPE610_SPI(spi, ts_cs) - - display_bus = displayio.FourWire(spi, command=dc, chip_select=cs) - self.display = HX8357(display_bus, width=480, height=320) - - sd_cs = board.D5 - self._sdcard = None - try: - self._sdcard = sdcardio.SDCard(spi, sd_cs) - vfs = storage.VfsFat(self._sdcard) - storage.mount(vfs, "/sd") - except OSError as error: - print("No SD card found:", error) + super().__init__(spi, cs, dc) + self.display = HX8357(self._display_bus, width=480, height=320) From dc4b58e3632d7d8d9ddc7c5b81ec45936ab88c82 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Tue, 16 Mar 2021 08:09:03 -0500 Subject: [PATCH 3/6] refactor keyboard_featherwing to use TFTFeatherWing superclass --- adafruit_featherwing/keyboard_featherwing.py | 37 +++++--------------- adafruit_featherwing/tft_featherwing.py | 13 ++++--- 2 files changed, 18 insertions(+), 32 deletions(-) diff --git a/adafruit_featherwing/keyboard_featherwing.py b/adafruit_featherwing/keyboard_featherwing.py index d083163..fc14303 100644 --- a/adafruit_featherwing/keyboard_featherwing.py +++ b/adafruit_featherwing/keyboard_featherwing.py @@ -20,19 +20,17 @@ __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git" import board -import digitalio -import displayio import adafruit_ili9341 -from adafruit_stmpe610 import Adafruit_STMPE610_SPI -import sdcardio -import storage from bbq10keyboard import BBQ10Keyboard import neopixel # pylint: disable-msg=too-few-public-methods # pylint: disable-msg=too-many-arguments -class KeyboardFeatherwing: +from adafruit_featherwing.tft_featherwing import TFTFeatherWing + + +class KeyboardFeatherwing(TFTFeatherWing): """Class representing a `Keyboard Featherwing` `_. @@ -48,32 +46,15 @@ def __init__( sd_cs=None, neopixel_pin=None, ): - displayio.release_displays() - if spi is None: - spi = board.SPI() - if cs is None: - cs = board.D9 - if dc is None: - dc = board.D10 + super().__init__(spi, cs, dc, ts_cs, sd_cs) + if i2c is None: i2c = board.I2C() - if ts_cs is None: - ts_cs = board.D6 - if sd_cs is None: - sd_cs = board.D5 if neopixel_pin is None: neopixel_pin = board.D11 - self.touchscreen = Adafruit_STMPE610_SPI(spi, digitalio.DigitalInOut(ts_cs)) - - display_bus = displayio.FourWire(spi, command=dc, chip_select=cs) - self.display = adafruit_ili9341.ILI9341(display_bus, width=320, height=240) + self.display = adafruit_ili9341.ILI9341( + self._display_bus, width=320, height=240 + ) self.neopixel = neopixel.NeoPixel(neopixel_pin, 1) self.keyboard = BBQ10Keyboard(i2c) - self._sdcard = None - try: - self._sdcard = sdcardio.SDCard(spi, sd_cs) - vfs = storage.VfsFat(self._sdcard) - storage.mount(vfs, "/sd") - except OSError as error: - print("No SD card found:", error) diff --git a/adafruit_featherwing/tft_featherwing.py b/adafruit_featherwing/tft_featherwing.py index 5d636d7..a141d90 100644 --- a/adafruit_featherwing/tft_featherwing.py +++ b/adafruit_featherwing/tft_featherwing.py @@ -22,14 +22,15 @@ import sdcardio import storage -# pylint: disable-msg=too-few-public-methods + +# pylint: disable-msg=too-few-public-methods, too-many-arguments class TFTFeatherWing: """Class representing an `TFT FeatherWing 2.4 `_. """ - def __init__(self, spi=None, cs=None, dc=None): + def __init__(self, spi=None, cs=None, dc=None, ts_cs=None, sd_cs=None): displayio.release_displays() if spi is None: spi = board.SPI() @@ -38,12 +39,16 @@ def __init__(self, spi=None, cs=None, dc=None): if dc is None: dc = board.D10 - ts_cs = digitalio.DigitalInOut(board.D6) + if ts_cs is None: + ts_cs = board.D6 + if sd_cs is None: + sd_cs = board.D5 + + ts_cs = digitalio.DigitalInOut(ts_cs) self.touchscreen = Adafruit_STMPE610_SPI(spi, ts_cs) self._display_bus = displayio.FourWire(spi, command=dc, chip_select=cs) - sd_cs = board.D5 self._sdcard = None try: self._sdcard = sdcardio.SDCard(spi, sd_cs) From 10f56ed25d7237c34d720db166ec161dbb11d5d7 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Tue, 16 Mar 2021 17:35:46 -0500 Subject: [PATCH 4/6] pylint fix: return None if error in unixtime --- adafruit_featherwing/rtc_featherwing.py | 1 + 1 file changed, 1 insertion(+) diff --git a/adafruit_featherwing/rtc_featherwing.py b/adafruit_featherwing/rtc_featherwing.py index 32ccfcd..8baa215 100755 --- a/adafruit_featherwing/rtc_featherwing.py +++ b/adafruit_featherwing/rtc_featherwing.py @@ -300,6 +300,7 @@ def unixtime(self): return time.mktime(self._rtc.datetime) except (AttributeError, RuntimeError) as error: print("Error attempting to run time.mktime() on this board\n", error) + return None @unixtime.setter def unixtime(self, unixtime): From 73c0f74cbdd68442889bbbf95e3bf2793bf07d57 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Tue, 16 Mar 2021 17:57:24 -0500 Subject: [PATCH 5/6] pylint dupe fix: refactor pixlematrix and matrix_featherwing to use superclass --- adafruit_featherwing/auto_writeable.py | 32 ++++++++++++++++++++++ adafruit_featherwing/matrix_featherwing.py | 20 ++++---------- adafruit_featherwing/pixelmatrix.py | 18 ++---------- 3 files changed, 40 insertions(+), 30 deletions(-) create mode 100644 adafruit_featherwing/auto_writeable.py diff --git a/adafruit_featherwing/auto_writeable.py b/adafruit_featherwing/auto_writeable.py new file mode 100644 index 0000000..c108aa8 --- /dev/null +++ b/adafruit_featherwing/auto_writeable.py @@ -0,0 +1,32 @@ +# SPDX-FileCopyrightText: 2021 Tim Cocks for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +""" +`adafruit_featherwing.auto_writeable` +==================================================== + +Superclass for the helpers pixelmatrix and matrix_featherwing + +* Author(s): Tim Cocks +""" + + +class AutoWriteable: + """Superclass for matrix_featherwing and pixelmatrix.""" + + def __init__(self): + self._auto_write = True + + @property + def auto_write(self): + """ + Whether or not we are automatically updating + If set to false, be sure to call show() to update + """ + return self._auto_write + + @auto_write.setter + def auto_write(self, write): + if isinstance(write, bool): + self._auto_write = write diff --git a/adafruit_featherwing/matrix_featherwing.py b/adafruit_featherwing/matrix_featherwing.py index 1af1fe6..066525e 100755 --- a/adafruit_featherwing/matrix_featherwing.py +++ b/adafruit_featherwing/matrix_featherwing.py @@ -18,21 +18,24 @@ import board import adafruit_ht16k33.matrix as matrix +from adafruit_featherwing.auto_writeable import AutoWriteable -class MatrixFeatherWing: + +class MatrixFeatherWing(AutoWriteable): """Class representing an `Adafruit 8x16 LED Matrix FeatherWing `_. Automatically uses the feather's I2C bus.""" def __init__(self, address=0x70, i2c=None): + if i2c is None: i2c = board.I2C() self._matrix = matrix.Matrix16x8(i2c, address) self._matrix.auto_write = False self.columns = 16 self.rows = 8 - self._auto_write = True + super().__init__() def __getitem__(self, key): """ @@ -125,19 +128,6 @@ def shift_down(self, rotate=False): self._matrix.shift_down(rotate) self._update() - @property - def auto_write(self): - """ - Whether or not we are automatically updating - If set to false, be sure to call show() to update - """ - return self._auto_write - - @auto_write.setter - def auto_write(self, write): - if isinstance(write, bool): - self._auto_write = write - @property def blink_rate(self): """ diff --git a/adafruit_featherwing/pixelmatrix.py b/adafruit_featherwing/pixelmatrix.py index 285dc56..9658ad3 100755 --- a/adafruit_featherwing/pixelmatrix.py +++ b/adafruit_featherwing/pixelmatrix.py @@ -16,9 +16,10 @@ __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git" # pylint: disable-msg=unsubscriptable-object, unsupported-assignment-operation +from adafruit_featherwing.auto_writeable import AutoWriteable -class PixelMatrix: +class PixelMatrix(AutoWriteable): """Base Class for DotStar and NeoPixel FeatherWings The feather uses pins D13 and D11""" @@ -27,7 +28,7 @@ def __init__(self): self.rows = 0 self.columns = 0 self._matrix = None - self._auto_write = True + super().__init__() def __setitem__(self, indices, value): """ @@ -158,19 +159,6 @@ def shift_down(self, rotate=False): self._matrix[(self.rows - 1) * self.columns + x] = last_pixel self._update() - @property - def auto_write(self): - """ - Whether or not we are automatically updating - If set to false, be sure to call show() to update - """ - return self._auto_write - - @auto_write.setter - def auto_write(self, write): - if isinstance(write, bool): - self._auto_write = write - @property def brightness(self): """ From 12f24d14fb08442397c256e0b0a057660939df90 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Fri, 19 Mar 2021 17:54:24 -0500 Subject: [PATCH 6/6] wait before initializing touch screen. Retry once --- adafruit_featherwing/tft_featherwing.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/adafruit_featherwing/tft_featherwing.py b/adafruit_featherwing/tft_featherwing.py index a141d90..415f1ad 100644 --- a/adafruit_featherwing/tft_featherwing.py +++ b/adafruit_featherwing/tft_featherwing.py @@ -15,6 +15,7 @@ Requires: * adafruit_stmpe610 """ +import time import board import digitalio import displayio @@ -45,7 +46,6 @@ def __init__(self, spi=None, cs=None, dc=None, ts_cs=None, sd_cs=None): sd_cs = board.D5 ts_cs = digitalio.DigitalInOut(ts_cs) - self.touchscreen = Adafruit_STMPE610_SPI(spi, ts_cs) self._display_bus = displayio.FourWire(spi, command=dc, chip_select=cs) @@ -56,3 +56,12 @@ def __init__(self, spi=None, cs=None, dc=None, ts_cs=None, sd_cs=None): storage.mount(vfs, "/sd") except OSError as error: print("No SD card found:", error) + + 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)