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/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/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): """ 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): diff --git a/adafruit_featherwing/tft_featherwing.py b/adafruit_featherwing/tft_featherwing.py new file mode 100644 index 0000000..415f1ad --- /dev/null +++ b/adafruit_featherwing/tft_featherwing.py @@ -0,0 +1,67 @@ +# 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 time +import board +import digitalio +import displayio +from adafruit_stmpe610 import Adafruit_STMPE610_SPI +import sdcardio +import storage + + +# 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, ts_cs=None, sd_cs=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 + + 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._display_bus = displayio.FourWire(spi, command=dc, chip_select=cs) + + 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) + + 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) 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 new file mode 100644 index 0000000..a3b0a2c --- /dev/null +++ b/adafruit_featherwing/tft_featherwing_35.py @@ -0,0 +1,37 @@ +# 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" + +from adafruit_hx8357 import HX8357 + +# pylint: disable-msg=too-few-public-methods +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): + super().__init__(spi, cs, dc) + self.display = HX8357(self._display_bus, width=480, height=320) 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())