|
19 | 19 | import board |
20 | 20 | import digitalio |
21 | 21 | import displayio |
22 | | -from adafruit_stmpe610 import Adafruit_STMPE610_SPI |
| 22 | + |
| 23 | +try: |
| 24 | + from adafruit_stmpe610 import Adafruit_STMPE610_SPI |
| 25 | +except ImportError: |
| 26 | + pass |
| 27 | +try: |
| 28 | + from adafruit_tsc2007 import TSC2007 |
| 29 | +except ImportError: |
| 30 | + pass |
| 31 | +try: |
| 32 | + from adafruit_focaltouch import Adafruit_FocalTouch |
| 33 | +except ImportError: |
| 34 | + pass |
| 35 | + |
23 | 36 | import sdcardio |
24 | 37 | import storage |
25 | 38 |
|
26 | 39 | try: |
27 | 40 | from typing import Optional |
28 | | - from busio import SPI |
| 41 | + from busio import SPI, I2C |
29 | 42 | from microcontroller import Pin |
30 | 43 | except ImportError: |
31 | 44 | pass |
32 | 45 |
|
33 | 46 |
|
34 | | -# pylint: disable-msg=too-few-public-methods, too-many-arguments |
| 47 | +# pylint: disable-msg=too-few-public-methods, too-many-arguments, too-many-branches |
35 | 48 | class TFTFeatherWing: |
36 | 49 | """Base class for TFT FeatherWings.""" |
37 | 50 |
|
38 | 51 | def __init__( |
39 | 52 | self, |
40 | 53 | spi: Optional[SPI] = None, |
41 | | - cs: Optional[Pin] = None, # pylint: disable=invalid-name |
42 | | - dc: Optional[Pin] = None, # pylint: disable=invalid-name |
43 | | - ts_cs: Optional[Pin] = None, |
44 | | - sd_cs: Optional[Pin] = None, |
| 54 | + i2c: Optional[I2C] = None, |
| 55 | + cs_pin: Optional[Pin] = None, |
| 56 | + dc_pin: Optional[Pin] = None, |
| 57 | + ts_cs_pin: Optional[Pin] = None, |
| 58 | + sd_cs_pin: Optional[Pin] = None, |
| 59 | + irq_pin: Optional[Pin] = None, |
| 60 | + resistive: Optional[bool] = True, |
45 | 61 | ): |
| 62 | + # Initialize Display Bus |
46 | 63 | displayio.release_displays() |
47 | 64 | if spi is None: |
48 | 65 | spi = board.SPI() |
49 | | - if cs is None: |
50 | | - cs = board.D9 |
51 | | - if dc is None: |
52 | | - dc = board.D10 |
53 | | - |
54 | | - if ts_cs is None: |
55 | | - ts_cs = board.D6 |
56 | | - if sd_cs is None: |
57 | | - sd_cs = board.D5 |
| 66 | + if cs_pin is None: |
| 67 | + cs_pin = board.D9 |
| 68 | + if dc_pin is None: |
| 69 | + dc_pin = board.D10 |
58 | 70 |
|
59 | | - ts_cs = digitalio.DigitalInOut(ts_cs) |
| 71 | + self._display_bus = displayio.FourWire(spi, command=dc_pin, chip_select=cs_pin) |
60 | 72 |
|
61 | | - self._display_bus = displayio.FourWire(spi, command=dc, chip_select=cs) |
| 73 | + # Initialize SD Card |
| 74 | + if sd_cs_pin is None: |
| 75 | + sd_cs_pin = board.D5 |
62 | 76 |
|
63 | 77 | self._sdcard = None |
64 | 78 | try: |
65 | | - self._sdcard = sdcardio.SDCard(spi, sd_cs) |
| 79 | + self._sdcard = sdcardio.SDCard(spi, sd_cs_pin) |
66 | 80 | vfs = storage.VfsFat(self._sdcard) |
67 | 81 | storage.mount(vfs, "/sd") |
68 | 82 | except OSError as error: |
69 | 83 | print("No SD card found:", error) |
70 | 84 |
|
| 85 | + # Initialize Touchscreen |
71 | 86 | self.touchscreen = None |
72 | | - """Controller for the resistive touchscreen.""" |
73 | | - try: |
74 | | - # the screen might not be ready from cold boot |
75 | | - time.sleep(0.8) |
76 | | - self.touchscreen = Adafruit_STMPE610_SPI(spi, ts_cs) |
77 | | - except RuntimeError: |
78 | | - # wait and try once more |
79 | | - time.sleep(1.0) |
80 | | - self.touchscreen = Adafruit_STMPE610_SPI(spi, ts_cs) |
| 87 | + if resistive: |
| 88 | + if i2c is None: # STMPE610 |
| 89 | + if ts_cs_pin is None: |
| 90 | + ts_cs_pin = board.D6 |
| 91 | + ts_cs = digitalio.DigitalInOut(ts_cs_pin) |
| 92 | + try: |
| 93 | + # the screen might not be ready from cold boot |
| 94 | + time.sleep(0.8) |
| 95 | + self.touchscreen = Adafruit_STMPE610_SPI(spi, ts_cs) |
| 96 | + except RuntimeError: |
| 97 | + # wait and try once more |
| 98 | + time.sleep(1.0) |
| 99 | + self.touchscreen = Adafruit_STMPE610_SPI(spi, ts_cs) |
| 100 | + else: # TSC2007 |
| 101 | + if irq_pin is None: |
| 102 | + irq_pin = board.D6 |
| 103 | + irq = digitalio.DigitalInOut(irq_pin) |
| 104 | + self.touchscreen = TSC2007(i2c, irq=irq) |
| 105 | + else: # FocalTouch |
| 106 | + if irq_pin is None: |
| 107 | + irq_pin = board.D6 |
| 108 | + self.touchscreen = Adafruit_FocalTouch(i2c, irq_pin=irq_pin) |
0 commit comments