Skip to content

Commit 4748599

Browse files
committed
refactor TFT Featherwings to use base class for avoiding duplicate
1 parent c8e76bb commit 4748599

File tree

3 files changed

+68
-58
lines changed

3 files changed

+68
-58
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# SPDX-FileCopyrightText: 2020 Melissa LeBlanc-Williams for Adafruit Industries
2+
# SPDX-FileCopyrightText: 2020 Foamyguy for Adafruit Industries
3+
#
4+
# SPDX-License-Identifier: MIT
5+
6+
"""
7+
`adafruit_featherwing.tft_featherwing`
8+
====================================================
9+
10+
Super class for helpers for using the TFT FeatherWing devices
11+
see tft_featherwng_24 and tft_featherwing_35
12+
13+
* Author(s): Melissa LeBlanc-Williams, Foamyguy
14+
15+
Requires:
16+
* adafruit_stmpe610
17+
"""
18+
import board
19+
import digitalio
20+
import displayio
21+
from adafruit_stmpe610 import Adafruit_STMPE610_SPI
22+
import sdcardio
23+
import storage
24+
25+
# pylint: disable-msg=too-few-public-methods
26+
class TFTFeatherWing:
27+
"""Class representing an `TFT FeatherWing 2.4
28+
<https://www.adafruit.com/product/3315>`_.
29+
30+
"""
31+
32+
def __init__(self, spi=None, cs=None, dc=None):
33+
displayio.release_displays()
34+
if spi is None:
35+
spi = board.SPI()
36+
if cs is None:
37+
cs = board.D9
38+
if dc is None:
39+
dc = board.D10
40+
41+
ts_cs = digitalio.DigitalInOut(board.D6)
42+
self.touchscreen = Adafruit_STMPE610_SPI(spi, ts_cs)
43+
44+
self._display_bus = displayio.FourWire(spi, command=dc, chip_select=cs)
45+
46+
sd_cs = board.D5
47+
self._sdcard = None
48+
try:
49+
self._sdcard = sdcardio.SDCard(spi, sd_cs)
50+
vfs = storage.VfsFat(self._sdcard)
51+
storage.mount(vfs, "/sd")
52+
except OSError as error:
53+
print("No SD card found:", error)

adafruit_featherwing/tft_featherwing_24.py

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,21 @@
2020
__version__ = "0.0.0-auto.0"
2121
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git"
2222

23-
import board
24-
import digitalio
25-
import displayio
23+
2624
import adafruit_ili9341
27-
from adafruit_stmpe610 import Adafruit_STMPE610_SPI
28-
import sdcardio
29-
import storage
3025

3126
# pylint: disable-msg=too-few-public-methods
32-
class TFTFeatherWing24:
27+
from adafruit_featherwing.tft_featherwing import TFTFeatherWing
28+
29+
30+
class TFTFeatherWing24(TFTFeatherWing):
3331
"""Class representing an `TFT FeatherWing 2.4
3432
<https://www.adafruit.com/product/3315>`_.
3533
3634
"""
3735

3836
def __init__(self, spi=None, cs=None, dc=None):
39-
displayio.release_displays()
40-
if spi is None:
41-
spi = board.SPI()
42-
if cs is None:
43-
cs = board.D9
44-
if dc is None:
45-
dc = board.D10
46-
47-
ts_cs = digitalio.DigitalInOut(board.D6)
48-
self.touchscreen = Adafruit_STMPE610_SPI(spi, ts_cs)
49-
50-
display_bus = displayio.FourWire(spi, command=dc, chip_select=cs)
51-
self.display = adafruit_ili9341.ILI9341(display_bus, width=320, height=240)
52-
53-
sd_cs = board.D5
54-
self._sdcard = None
55-
try:
56-
self._sdcard = sdcardio.SDCard(spi, sd_cs)
57-
vfs = storage.VfsFat(self._sdcard)
58-
storage.mount(vfs, "/sd")
59-
except OSError as error:
60-
print("No SD card found:", error)
37+
super().__init__(spi, cs, dc)
38+
self.display = adafruit_ili9341.ILI9341(
39+
self._display_bus, width=320, height=240
40+
)

adafruit_featherwing/tft_featherwing_35.py

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,18 @@
2020
__version__ = "0.0.0-auto.0"
2121
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git"
2222

23-
import board
24-
import digitalio
25-
import displayio
2623
from adafruit_hx8357 import HX8357
27-
from adafruit_stmpe610 import Adafruit_STMPE610_SPI
28-
import sdcardio
29-
import storage
3024

3125
# pylint: disable-msg=too-few-public-methods
32-
class TFTFeatherWing35:
26+
from adafruit_featherwing.tft_featherwing import TFTFeatherWing
27+
28+
29+
class TFTFeatherWing35(TFTFeatherWing):
3330
"""Class representing an `TFT FeatherWing 3.5
3431
<https://www.adafruit.com/product/3651>`_.
3532
3633
"""
3734

3835
def __init__(self, spi=None, cs=None, dc=None):
39-
displayio.release_displays()
40-
if spi is None:
41-
spi = board.SPI()
42-
if cs is None:
43-
cs = board.D9
44-
if dc is None:
45-
dc = board.D10
46-
47-
ts_cs = digitalio.DigitalInOut(board.D6)
48-
self.touchscreen = Adafruit_STMPE610_SPI(spi, ts_cs)
49-
50-
display_bus = displayio.FourWire(spi, command=dc, chip_select=cs)
51-
self.display = HX8357(display_bus, width=480, height=320)
52-
53-
sd_cs = board.D5
54-
self._sdcard = None
55-
try:
56-
self._sdcard = sdcardio.SDCard(spi, sd_cs)
57-
vfs = storage.VfsFat(self._sdcard)
58-
storage.mount(vfs, "/sd")
59-
except OSError as error:
60-
print("No SD card found:", error)
36+
super().__init__(spi, cs, dc)
37+
self.display = HX8357(self._display_bus, width=480, height=320)

0 commit comments

Comments
 (0)