LCD st7920 on esp8266 ModeMcu v3 Lolin

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
piterski
Posts: 5
Joined: Fri Mar 27, 2020 10:12 am

LCD st7920 on esp8266 ModeMcu v3 Lolin

Post by piterski » Mon Apr 06, 2020 12:02 pm

helow
I'm trying to run st7920 on NodeMcu v3 Lolin. I chose it because it has 5V
most likely the system has SPI("GPIO_8 MOSI" ," GPIO_6 CLK") and HSPI("GPIO_13 MOSI" GPIO_14 CLK")

I try like mcauser

Code: Select all

from machine import Pin, SPI
import st7920
spi = SPI(1)
screen = st7920.Screen(slaveSelectPin=Pin(4), resetDisplayPin=Pin(5))
screen.plot(5, 5)
screen.plot(5, 5) evokes error

Code: Select all

Traceback (most recent call last):
  File "<stdin>", line 1 in <module>
  File "canvas.py", line 2 in raiseError
 NotImplemementedError:
Where should I start ?

piterski
Posts: 5
Joined: Fri Mar 27, 2020 10:12 am

Re: LCD st7920 on esp8266 ModeMcu v3 Lolin

Post by piterski » Fri Apr 10, 2020 8:54 am

On my Raspberry Pi works OK!

Code: Select all

import spidev

class ST7920:
	def __init__(self):
		self.spi = spidev.SpiDev()
		self.spi.open(0,0)
		self.spi.cshigh = True # use inverted CS
		self.spi.max_speed_hz = 1800000 # set SPI clock to 1.8MHz, up from 125kHz

		self.send(0,0,0x30) # basic instruction set
		self.send(0,0,0x30) # repeated
		self.send(0,0,0x0C) # display on
		self.send(0,0,0x34) #enable RE mode
		self.send(0,0,0x34)
		self.send(0,0,0x36) #enable graphics display
		self.set_rotation(0) # rotate to 0 degrees

	def send(self, rs, rw, cmds):
		if type(cmds) is int: # if a single arg, convert to a list
			cmds = [cmds]
		b1 = 0b11111000 | ((rw&0x01)<<2) | ((rs&0x01)<<1)
		bytes = []
		for cmd in cmds:
			bytes.append(cmd & 0xF0)
			bytes.append((cmd & 0x0F)<<4)
		return self.spi.xfer2([b1] + bytes)
On esp8266 no module spidev error spi.xfer2. Can i replace it spi = hspi or self.spi = SPI(-1, baudrate=baudrate, polarity=polarity, phase=phase, sck=sck, mosi=mosi, miso=miso)

piterski
Posts: 5
Joined: Fri Mar 27, 2020 10:12 am

Re: LCD st7920 on esp8266 ModeMcu v3 Lolin

Post by piterski » Mon Apr 13, 2020 6:32 pm

On Raspberry Pi works ok
I replaced cod python2 on python3
I'm using the function plot(x,y) to draw a point x,y

Code: Select all

import spidev

class ST7920:
    def __init__(self):
        self.spi = spidev.SpiDev()
        self.spi.open(0, 0)
        self.spi.cshigh = True  # use inverted CS
        self.spi.max_speed_hz = 1800000  # set SPI clock to 1.8MHz, up from 125kHz
        self.send(0, 0, 0x30)  # basic instruction set
        self.send(0, 0, 0x30)  # repeated
        self.send(0, 0, 0x0C)  # display on
        self.send(0, 0, 0x34)  # enable RE mode
        self.send(0, 0, 0x34)
        self.send(0, 0, 0x36)  # enable graphics display

        self.width = 128
        self.height = 64

        self.clear()

#        self.plot(0,63)

        self.redraw()

    def send(self, rs, rw, cmds):
        if type(cmds) is int: 
            cmds = [cmds]
        b1 = 0b11111000 | ((rw & 0x01) << 2) | ((rs & 0x01) << 1)
        bytes = []
        for cmd in cmds:
            cmd = int(cmd)
            bytes.append(cmd & 0xF0)
            bytes.append((cmd & 0x0F) << 4)
        return self.spi.xfer2([b1] + bytes)

    def plot(self, x, y):
        if x < 0 or x >= self.width or y < 0 or y >= self.height:
            return
        self.fbuff[y][x // 8] |= 1 << (7 - (x % 8))

    def clear(self):
        self.fbuff = [[0] * (128 // 8) for i in range(64)]

    def redraw(self, dx1=0, dy1=0, dx2=127, dy2=63):
        for i in range(dy1, dy2 + 1):
            self.send(0, 0, [0x80 + i % 32, 0x80 + ((dx1 // 16) + (8 if i >= 32 else 0))])
            self.send(1, 0, self.fbuff[i][dx1 // 8:(dx2 // 8) + 1])
SPI sends packets in the format

Code: Select all

([248, 144, 240, 128, 128])
([250, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16])
Now I would like to use SPI.write on micropython

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: LCD st7920 on esp8266 ModeMcu v3 Lolin

Post by jimmo » Tue Apr 14, 2020 5:45 am

piterski wrote:
Fri Apr 10, 2020 8:54 am
On esp8266 no module spidev error spi.xfer2. Can i replace it spi = hspi or self.spi = SPI(-1, baudrate=baudrate, polarity=polarity, phase=phase, sck=sck, mosi=mosi, miso=miso)
yes, you will have to use machine.SPI instead. xfer2 looks to be the same as the write() method on machine.SPI.

piterski
Posts: 5
Joined: Fri Mar 27, 2020 10:12 am

Re: LCD st7920 on esp8266 ModeMcu v3 Lolin

Post by piterski » Wed Apr 15, 2020 7:36 am

Works on esp8266 NodeMcu V3. Without additional power supply.

The solution is:
change int SPI and send to SPI

Code: Select all

spi = SPI(1, baudrate=1800000, polarity=0, phase=0 
return self.spi.write(bytearray([b1] + bytes)) 
Next problem is Fonts. I can upload a maximum of 2 fonts. And I lack memory.
A very long time ago I did a project on Audrino Uno. I used 3 fonts and 4 Graphic Icon 26x26 and 10szt 18b20 and keyPad.
Works until today. I used u8g2lib in Audrino

My question is how to optimize in micropython the font table as much as possible so that it takes as little memory as possible

piterski
Posts: 5
Joined: Fri Mar 27, 2020 10:12 am

Re: LCD st7920 on esp8266 ModeMcu v3 Lolin

Post by piterski » Thu May 07, 2020 7:25 am

Hello.
Maybe someone will need this information

I replaced the double list "bytearray" on a single list "int". About 10 times less space

I launched the alpha version. Its working.
If I develop a project, I will make it available

Post Reply