Skip to content

[Feature Request] Untie from spidev #13

@jrmoserbaltimore

Description

@jrmoserbaltimore

This library requires spidev, which is not available everywhere. It would be more broadly useful if it accepted an spi instance already created, with a read() and write() member, which is available in MicroPython. A client program may need to extend spidev with a write() member for this library to work, import spidev, create the SPI object themselves, and pass it to the constructor; but that would work, and so would using machine.SPI, and other things.

This is a breaking change. The dependency on spidev and the import line itself prevents the use of this library on the Pi Pico. There are already three ST7735 libraries, all based on the same code base. It's a trivial modification and I could send a PR, but this is a pretty major decision as it is a mandatory API break.

See work done at my fork. I have not finished updating the examples to use spidev or MicroPython objects, but notice in framerate.py:

if sys.implementation.name == "micropython":
    # Hardware SPI device
    spi = machine.SPI(0)
    sck = machine.Pin(18)
    smosi = machine.Pin(19)
    smiso = machine.pin(20)
    scs = machine.pin(21)
    spi.init(baudrate=SPI_SPEED_MHZ * 1000000, sck=sck, mosi=smosi, miso=smiso)
    dc = machine.Pin(2)
    backlight = machine.Pin(3)
else:
    # Set up the spidev device
    spi = spidev.SpiDev(0, ST7735.BG_SPI_CS_FRONT)
    spi.mode = 0
    spi.lsbfirst = False
    spi.max_speed_hz = SPI_SPEED_MHZ * 1000000
    # add the send() method as alias to xfer3()
    spi.send = spi.xfer3
    # gpiozero pins
    dc = OutputDevice(9)
    backlight = OutputDevice(19)
# Create ST7735 LCD display class.
disp = ST7735.ST7735(
    port=spi,
    dc=dc,
    backlight=backlight,               # 18 for back BG slot, 19 for front BG slot.
    rotation=90,
)

The above uses micropython's facilities or spidev. Because spidev uses xfer3() instead of send(), it makes spi.send() a pointer to spi.xfer3(), so the spidev SPI object behaves the same as the micropython one when called the same way.

This approach, again, removes gpio and spidev stuff from the library itself, so it is now portable to the Raspberry Pi Pico, but also runs on the Raspberry Pi Linux series using spidev, just as long as the client hands it a duck.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions