hosaka / micropython-st7735

MicroPython driver and HAL example for Sitronix ST7735 TFT displays.
MIT License
26 stars 20 forks source link

Almost works with ESP8266 #1

Open mcauser opened 8 years ago

mcauser commented 8 years ago

Tried using your library on a WeMos D1 mini running MicroPython v1.8.138 Almost works. Calls to self.write_cmd() are failing as it requires a buffer.

>>> import st7735
>>> import tft
>>> from machine import SPI, Pin
>>> spi = SPI(baudrate=100000, polarity=1, phase=0, sck=Pin(14), mosi=Pin(13), miso=Pin(12))
>>> t = tft.TFT_GREEN(128, 128, spi, Pin(5), Pin(15), Pin(0))
>>> t.init()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "tft.py", line 105, in init
  File "tft.py", line 80, in write_cmd
TypeError: object with buffer protocol required
>>> t.clear()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "st7735.py", line 106, in clear
  File "st7735.py", line 142, in rect
  File "st7735.py", line 79, in _set_window
  File "tft.py", line 80, in write_cmd
TypeError: object with buffer protocol required
hosaka commented 8 years ago

Hi,

Thank you for reporting the issue and sorry about the delay, I'll have a look into it asap.

hosaka commented 8 years ago

Hi Mike,

I haven't honestly tried the driver with the esp port yet and used WiPy for developing, which still works okay. The problem might be related to the fact that esp SPI is implemented in software, as opposed to WiPy. I'll get my hands on the esp board again and will try to make it work.

You should explicitly define the pins as Pin.OUT by the way.

hosaka commented 8 years ago

Try changing the line in the tft module's write_cmd:

self.spi.write(cmd)

to

self.spi.write(bytearray([cmd]))
PeterKenyon commented 8 years ago

Thanks for contributing this driver it looks really well written and commented however... Tried this driver with the micropython board and no success yet,

  1. micropython seems to have pyb.SPI and machine.SPI not sure which I'm supposed to use, but neither provide a write cmd
  2. Whats up with all the GREEEN TAB stuff, I'm trying this out on a GHI N18 board, so could well be issues there, ho hum
PeterKenyon commented 8 years ago
class Spi2(pyb.SPI):
    def write(self, b):
        self.send(b)

class Hack(TFT_GREEN):
    def __init__(self):
        width=128
        height=160
        spi=Spi2(1,pyb.SPI.MASTER)
        dc=pyb.Pin('X2', pyb.Pin.OUT)
        cs=pyb.Pin('X5', pyb.Pin.OUT)
        rst=pyb.Pin('X4', pyb.Pin.OUT)
        bl=pyb.Pin('X3', pyb.Pin.OUT)

        super().__init__(width, height, spi, dc, cs, rst, bl)
        pass
PeterKenyon commented 8 years ago

sorry I cant get the damn markdown to do anything sensible.. presumably crapness on my part

hosaka commented 8 years ago

Hi @PeterKenyon,

To figure out how to use the SPI and others with your board, see the official docs and make sure you're looking at the right board: pyboard, wipy etc., but looks like you're using pyboard.

The write_cmd is not a part of your board SPI, it is a function that glues the user pin/SPI commands without the driver knowing anything about it. See tft.py#L74. I have added a main file to the project, just to show an example how you'd go about using the driver.

I am not familiar with GHI N18 boards, but a quick google suggests they are indeed using the st7735. The "tab" stuff is actually quite confusing: displays come with a little protective film over them and the "tab" that you can use to pull the display off has a particular colour, in my case it's green. This tab affects the way in which you would init the display before using it. You can read more about it in the st7735 datasheet if you're into that sort of thing. I am planning to add other display versions too.

If you want to have a go at adding your version, create a new sublass from TFT and implement the init function yourself, exactly the way the TFT_GREEN class is written.

Hope this helps!

PeterKenyon commented 8 years ago

thanks, I have actually got it working as you recommend, I just used GHI's C# init code and translated into python. I created a N18 subclass similar to your GREEN_TAB. (Incidentally my tab is orange) I'm using micropython on a pyb and it doesn't have a spi.write method just a spi.send, so I overloaded to call the relevant function.

hosaka commented 8 years ago

Great!

Glad to hear you got it sorted. Also feel free to contribute your N18 subclass to the tft.py as a pull request. Would be good to have a variety of devices supported!

On Tue, 21 Jun 2016, 13:36 PeterKenyon, notifications@github.com wrote:

thanks, I have actually got it working as you recommend, I just used GHI's C# init code and translated into python. I created a N18 subclass similar to your GREEN_TAB. I'm using micropython on a pyb and it doesn't have a spi.write method just a spi.send, so I overloaded to call the relevant function.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/hosaka/micropython-st7735/issues/1#issuecomment-227426283, or mute the thread https://github.com/notifications/unsubscribe/AAKfZHinCnQzIMDk9RoIdFCT7owPprYiks5qN9rOgaJpZM4IpgIO .

Andon-A commented 7 years ago

Greetings, I'm attempting to get this working on an ESP8266. I tried your above method of self.spi.write(bytearray([cmd])), which then yelled at me for write_data, so I changed all instances of self.spi.write to use it.

This is what it gives me now: Traceback (most recent call last): File "", line 1, in File "tft.py", line 112, in init File "tft.py", line 89, in write_data TypeError: can't convert bytearray to int

I would definitely appreciate any assistance in getting this screen working on the ESP8266.

hosaka commented 7 years ago

Hi @Andon-A,

I haven't had much time recently to follow micropython/esp8266 development due to my full-time job being busy.

It does look like a simple type conversion problem that shouldn't be too hard to debug, but I need to see how you're using the driver. If you find any issues related to the driver itself, by all means please submit PRs and I hopefully will find time to review them!

tmueller1970 commented 7 years ago

Thanks to hosaka for his great work! I got it running with micropython v1.8.7 on an esp-12f board. Some minor fixes have to be taken. A stright forward job :-) I use a display ordered from china (ebay).

diff -Nru st7735.orig/main.py st7735.my/main.py
--- st7735.orig/main.py 2017-03-16 10:18:44.436059281 +0100
+++ st7735.my/main.py   2017-03-16 10:19:29.616044707 +0100
@@ -1,18 +1,25 @@
 # MicroPython ST7735 TFT display driver example usage

-from machine import Pin, SPI
+# My Pinlayout (a st7735r display from china)
+# SCK   -> GPIO14
+# SDA   -> GPIO13
+# A0    -> GPIO5
+# RESET -> GPIO15 (with a 22k resistor to GND "pulldown")
+# CS    -> GPIO4
+
+from machine import Pin, SPI, reset
 from tft import TFT_GREEN

 # DC       - RS/DC data/command flag
 # CS       - Chip Select, enable communication
 # RST/RES  - Reset
-dc  = Pin('GP3', Pin.OUT, Pin.PULL_DOWN)
-cs  = Pin('GP7', Pin.OUT, Pin.PULL_DOWN)
-rst = Pin('GP4', Pin.OUT, Pin.PULL_DOWN)
+dc  = Pin(5, Pin.OUT)
+cs  = Pin(4, Pin.OUT)
+rst = Pin(15, Pin.OUT)

 # SPI Bus (CLK/MOSI/MISO)
 # check your port docs to see which Pins you can use
-spi = SPI(0, mode=SPI.MASTER, baudrate=8000000, polarity=1, phase=0)
+spi = SPI(1, baudrate=8000000, polarity=1, phase=0)

 # TFT object, this is ST7735R green tab version
 tft = TFT_GREEN(128, 160, spi, dc, cs, rst)
@@ -21,4 +28,5 @@
 tft.init()

 # start using the driver
-tft.clear(tft.rgbcolor(255, 0, 0))
+tft.clear(tft.rgbcolor(255, 255, 255))
+tft.line(0,0,127,159,tft.rgbcolor(0, 0, 255))
diff -Nru st7735.orig/tft.py st7735.my/tft.py
--- st7735.orig/tft.py  2017-03-16 10:18:27.924064675 +0100
+++ st7735.my/tft.py    2017-03-16 10:19:18.976048115 +0100
@@ -77,7 +77,8 @@
         """
         self.dc.value(0)
         self.cs.value(0)
-        self.spi.write(cmd)
+        # self.spi.write(cmd)
+        self.spi.write(bytearray([cmd]))
         self.cs.value(1)

     def write_data(self, data):
@@ -96,8 +97,8 @@

     def init(self):
         # set column and row margins
-        self.margin_row = 1
-        self.margin_col = 2
+        self.margin_row = 0
+        self.margin_col = 0

         # hard reset first
         self.reset()
@@ -115,12 +116,12 @@
         time.sleep_ms(10)

         self.write_cmd(TFT.CMD_INVCTR)
-        self.write_data(0x07)
+        self.write_data(bytearray([0x07]))

         self.write_cmd(TFT.CMD_PWCTR1)
         self.write_data(bytearray([0xA2, 0x02, 0x84]))
         self.write_cmd(TFT.CMD_PWCTR2)
-        self.write_data(0xC5)
+        self.write_data(bytearray([0xC5]))
         self.write_cmd(TFT.CMD_PWCTR3)
         self.write_data(bytearray([0x8A, 0x00]))
         self.write_cmd(TFT.CMD_PWCTR4)
@@ -129,14 +130,14 @@
         self.write_data(bytearray([0x8A, 0xEE]))

         self.write_cmd(TFT.CMD_VMCTR1)
-        self.write_data(0x0E)
+        self.write_data(bytearray([0x0E]))

         self.write_cmd(TFT.CMD_INVOFF)
         self.write_cmd(TFT.CMD_MADCTL)
-        self.write_data(0x00) # RGB
+        self.write_data(bytearray([0x00])) # RGB

         self.write_cmd(TFT.CMD_COLMOD)
-        self.write_data(0x05)
+        self.write_data(bytearray([0x05]))

         self.write_cmd(TFT.CMD_CASET)
         self.write_data(bytearray([0x00, 0x01, 0x00, 127]))
tmueller1970 commented 7 years ago

Just played with an other display module. There I must use polarity=0 for the SPI bus. This results also in swapping colors from RGB to BGR. Strange!

hosaka commented 7 years ago

Hey @tmueller1970

Thanks for the patch, the changes look good, and all due to my mistakes :) Feel free to submit a PR, I'll be happy to merge the changes so others can use the driver and extend it.

rakovskij-stanislav commented 6 years ago

God, you exist! Incredible. Thank you, @hosaka @tmueller1970 ! It took me 2 days to find correct lib for ESP-12, find pinout for ESP-12 and TFT screen and make it work. So, how-to-wire pinout for ESP-12 (LoLin wemos nodemcu v3, in my case) for @tmueller1970 's fix:

The code variant @hosaka + @tmueller1970 perfectly works. My _main.py file:

from tft import TFT_GREEN
from machine import Pin, SPI, reset
dc  = Pin(5, Pin.OUT)
cs  = Pin(4, Pin.OUT)
rst = Pin(15, Pin.OUT)
spi = SPI(1, baudrate=8000000, polarity=1, phase=0)
t = TFT_GREEN(128, 128, spi, dc, cs, rst)
t.clear(t.rgbcolor(255, 255, 255))
import time
tt = time.time()
for i in range(128):
     t.line(0, i, 127, 127-i, t.rgbcolor(2*i, 0, 2*i))
     t.line(i, 0, 127-i, 127, t.rgbcolor(0, 2*i, 0))

This drawing lasts 99 seconds) I will examine this lib and Adafruit's works, thank you again