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