fix: fixed bogus support for waveshare lcd displays (fixes #364)

This commit is contained in:
Simone Margaritelli 2019-10-23 15:37:12 +02:00
parent a78a4b0b3e
commit 277906a673
3 changed files with 53 additions and 114 deletions

View File

@ -7,24 +7,25 @@ import numpy as np
class ST7789(object): class ST7789(object):
"""class for ST7789 240*240 1.3inch OLED displays.""" """class for ST7789 240*240 1.3inch OLED displays."""
def __init__(self,spi,rst = 27,dc = 25,bl = 24): def __init__(self, spi, rst=27, dc=25, bl=24):
self.width = 240 self.width = 240
self.height = 240 self.height = 240
#Initialize DC RST pin # Initialize DC RST pin
self._dc = dc self._dc = dc
self._rst = rst self._rst = rst
self._bl = bl self._bl = bl
GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False) GPIO.setwarnings(False)
GPIO.setup(self._dc,GPIO.OUT) GPIO.setup(self._dc, GPIO.OUT)
GPIO.setup(self._rst,GPIO.OUT) GPIO.setup(self._rst, GPIO.OUT)
GPIO.setup(self._bl,GPIO.OUT) GPIO.setup(self._bl, GPIO.OUT)
GPIO.output(self._bl, GPIO.HIGH) GPIO.output(self._bl, GPIO.HIGH)
#Initialize SPI # Initialize SPI
self._spi = spi self._spi = spi
self._spi.max_speed_hz = 40000000 self._spi.max_speed_hz = 40000000
""" Write register address and data """ """ Write register address and data """
def command(self, cmd): def command(self, cmd):
GPIO.output(self._dc, GPIO.LOW) GPIO.output(self._dc, GPIO.LOW)
self._spi.writebytes([cmd]) self._spi.writebytes([cmd])
@ -34,13 +35,13 @@ class ST7789(object):
self._spi.writebytes([val]) self._spi.writebytes([val])
def Init(self): def Init(self):
"""Initialize dispaly""" """Initialize dispaly"""
self.reset() self.reset()
self.command(0x36) self.command(0x36)
self.data(0x70) #self.data(0x00) self.data(0x70) # self.data(0x00)
self.command(0x3A) self.command(0x3A)
self.data(0x05) self.data(0x05)
self.command(0xB2) self.command(0xB2)
@ -51,7 +52,7 @@ class ST7789(object):
self.data(0x33) self.data(0x33)
self.command(0xB7) self.command(0xB7)
self.data(0x35) self.data(0x35)
self.command(0xBB) self.command(0xBB)
self.data(0x19) self.data(0x19)
@ -63,13 +64,13 @@ class ST7789(object):
self.data(0x01) self.data(0x01)
self.command(0xC3) self.command(0xC3)
self.data(0x12) self.data(0x12)
self.command(0xC4) self.command(0xC4)
self.data(0x20) self.data(0x20)
self.command(0xC6) self.command(0xC6)
self.data(0x0F) self.data(0x0F)
self.command(0xD0) self.command(0xD0)
self.data(0xA4) self.data(0xA4)
@ -106,7 +107,7 @@ class ST7789(object):
self.data(0x1F) self.data(0x1F)
self.data(0x20) self.data(0x20)
self.data(0x23) self.data(0x23)
self.command(0x21) self.command(0x21)
self.command(0x11) self.command(0x11)
@ -115,51 +116,51 @@ class ST7789(object):
def reset(self): def reset(self):
"""Reset the display""" """Reset the display"""
GPIO.output(self._rst,GPIO.HIGH) GPIO.output(self._rst, GPIO.HIGH)
time.sleep(0.01) time.sleep(0.01)
GPIO.output(self._rst,GPIO.LOW) GPIO.output(self._rst, GPIO.LOW)
time.sleep(0.01) time.sleep(0.01)
GPIO.output(self._rst,GPIO.HIGH) GPIO.output(self._rst, GPIO.HIGH)
time.sleep(0.01) time.sleep(0.01)
def SetWindows(self, Xstart, Ystart, Xend, Yend): def SetWindows(self, Xstart, Ystart, Xend, Yend):
#set the X coordinates # set the X coordinates
self.command(0x2A) self.command(0x2A)
self.data(0x00) #Set the horizontal starting point to the high octet self.data(0x00) # Set the horizontal starting point to the high octet
self.data(Xstart & 0xff) #Set the horizontal starting point to the low octet self.data(Xstart & 0xff) # Set the horizontal starting point to the low octet
self.data(0x00) #Set the horizontal end to the high octet self.data(0x00) # Set the horizontal end to the high octet
self.data((Xend - 1) & 0xff) #Set the horizontal end to the low octet self.data((Xend - 1) & 0xff) # Set the horizontal end to the low octet
#set the Y coordinates # set the Y coordinates
self.command(0x2B) self.command(0x2B)
self.data(0x00) self.data(0x00)
self.data((Ystart & 0xff)) self.data((Ystart & 0xff))
self.data(0x00) self.data(0x00)
self.data((Yend - 1) & 0xff ) self.data((Yend - 1) & 0xff)
self.command(0x2C) self.command(0x2C)
def ShowImage(self,Image,Xstart,Ystart): def ShowImage(self, Image, Xstart, Ystart):
"""Set buffer to value of Python Imaging Library image.""" """Set buffer to value of Python Imaging Library image."""
"""Write display buffer to physical display""" """Write display buffer to physical display"""
imwidth, imheight = Image.size imwidth, imheight = Image.size
if imwidth != self.width or imheight != self.height: if imwidth != self.width or imheight != self.height:
raise ValueError('Image must be same dimensions as display \ raise ValueError('Image must be same dimensions as display \
({0}x{1}).' .format(self.width, self.height)) ({0}x{1}).'.format(self.width, self.height))
img = np.asarray(Image) img = np.asarray(Image)
pix = np.zeros((self.width,self.height,2), dtype = np.uint8) pix = np.zeros((self.width, self.height, 2), dtype=np.uint8)
pix[...,[0]] = np.add(np.bitwise_and(img[...,[0]],0xF8),np.right_shift(img[...,[1]],5)) pix[..., [0]] = np.add(np.bitwise_and(img[..., [0]], 0xF8), np.right_shift(img[..., [1]], 5))
pix[...,[1]] = np.add(np.bitwise_and(np.left_shift(img[...,[1]],3),0xE0),np.right_shift(img[...,[2]],3)) pix[..., [1]] = np.add(np.bitwise_and(np.left_shift(img[..., [1]], 3), 0xE0), np.right_shift(img[..., [2]], 3))
pix = pix.flatten().tolist() pix = pix.flatten().tolist()
self.SetWindows ( 0, 0, self.width, self.height) self.SetWindows(0, 0, self.width, self.height)
GPIO.output(self._dc,GPIO.HIGH) GPIO.output(self._dc, GPIO.HIGH)
for i in range(0,len(pix),4096): for i in range(0, len(pix), 4096):
self._spi.writebytes(pix[i:i+4096]) self._spi.writebytes(pix[i:i + 4096])
def clear(self): def clear(self):
"""Clear contents of image buffer""" """Clear contents of image buffer"""
_buffer = [0xff]*(self.width * self.height * 2) _buffer = [0xff] * (self.width * self.height * 2)
self.SetWindows ( 0, 0, self.width, self.height) self.SetWindows(0, 0, self.width, self.height)
GPIO.output(self._dc,GPIO.HIGH) GPIO.output(self._dc, GPIO.HIGH)
for i in range(0,len(_buffer),4096): for i in range(0, len(_buffer), 4096):
self._spi.writebytes(_buffer[i:i+4096]) self._spi.writebytes(_buffer[i:i + 4096])

View File

@ -7,70 +7,15 @@
# * | Date : 2019-10-18 # * | Date : 2019-10-18
# * | Info : # * | Info :
# ******************************************************************************/ # ******************************************************************************/
import RPi.GPIO as GPIO
import time
from smbus import SMBus
import spidev import spidev
import ctypes
# import spidev
# Pin definition # Pin definition
RST_PIN = 27 RST_PIN = 27
DC_PIN = 25 DC_PIN = 25
BL_PIN = 24 BL_PIN = 24
Device_SPI = 1 Device_SPI = 1
Device_I2C = 0 Device_I2C = 0
Device = Device_SPI Device = Device_SPI
spi = spidev.SpiDev(0, 0) spi = spidev.SpiDev(0, 0)
def digital_write(pin, value):
GPIO.output(pin, value)
def digital_read(pin):
return GPIO.input(BUSY_PIN)
def delay_ms(delaytime):
time.sleep(delaytime / 1000.0)
def spi_writebyte(data):
# SPI.writebytes(data)
spi.writebytes([data[0]])
def i2c_writebyte(reg, value):
bus.write_byte_data(address, reg, value)
# time.sleep(0.01)
def module_init():
# print("module_init")
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(RST_PIN, GPIO.OUT)
GPIO.setup(DC_PIN, GPIO.OUT)
# SPI.max_speed_hz = 2000000
# SPI.mode = 0b00
# i2c_writebyte(0xff,0xff)
# spi.SYSFS_software_spi_begin()
# spi.SYSFS_software_spi_setDataMode(0);
# spi.SYSFS_software_spi_setClockDivider(1);
#spi.max_speed_hz = 2000000
#spi.mode = 0b00
GPIO.output(BL_PIN, 1)
GPIO.output(DC_PIN, 0)
return 0
def module_exit():
spi.SYSFS_software_spi_end()
GPIO.output(RST_PIN, 0)
GPIO.output(DC_PIN, 0)
### END OF FILE ###

View File

@ -1,28 +1,21 @@
from . import ST7789 from . import ST7789
from . import config from . import config
# Display resolution
EPD_WIDTH = 240
EPD_HEIGHT = 240
disp = ST7789.ST7789(config.spi,config.RST_PIN, config.DC_PIN, config.BL_PIN)
class EPD(object): class EPD(object):
def __init__(self): def __init__(self):
self.reset_pin = config.RST_PIN self.reset_pin = config.RST_PIN
self.dc_pin = config.DC_PIN self.dc_pin = config.DC_PIN
#self.busy_pin = config.BUSY_PIN self.width = 240
#self.cs_pin = config.CS_PIN self.height = 240
self.width = EPD_WIDTH self.st7789 = ST7789.ST7789(config.spi, config.RST_PIN, config.DC_PIN, config.BL_PIN)
self.height = EPD_HEIGHT
def init(self): def init(self):
disp.Init() self.st7789.Init()
def Clear(self): def clear(self):
disp.clear() self.st7789.clear()
def display(self, image): def display(self, image):
rgb_im = image.convert('RGB') rgb_im = image.convert('RGB')
disp.ShowImage(rgb_im,0,0) self.st7789.ShowImage(rgb_im, 0, 0)