from machine import Pin, mem32
from utime import sleep_ms, ticks_us
import struct
LCD_RD = 8
LCD_WR = 9
LCD_RS = 10
LCD_CS = 11
LCD_RST = 12
SIO_BASE = 0xd0000000
GPIO_IN = SIO_BASE + 0x004
GPIO_OUT = SIO_BASE + 0x010
GPIO_MASK = 0xffffff00
BMP_WIDTH = 240
BMP_HEIGHT = 320
BUF_LINE = 32
BUF_SIZE = BMP_WIDTH * BUF_LINE * 3
def bmp_load(file_name):
with open(file_name, 'rb') as bmp_file:
offset = read_bmp_header(bmp_file)
if offset != False:
bmp_draw(bmp_file, offset)
def bmp_draw(bmp_file, offset):
address_set(0, 0, BMP_WIDTH, BMP_HEIGHT)
lcd_cs.value(0)
lcd_rs.value(1)
bmp_file.seek(offset)
start = ticks_us()
for y in range(BMP_HEIGHT / BUF_LINE):
bmp = bmp_file.read(BUF_SIZE)
for i in range(0, BUF_SIZE, 3):
b = (bmp[i] & 0xf8) >> 3
g = (bmp[i + 1] & 0xfc) << 3
r = (bmp[i + 2] & 0xf8) << 8
value = (r | g | b)
lcd_write_data(value >> 8)
lcd_write_data(value & 0xff)
end = ticks_us()
length_us = end - start
print('{} s'.format(length_us/1000000))
def read_bmp_header(bmp_file):
file_type = struct.unpack('<h', bmp_file.read(2))[0]
print('file type = ', hex(file_type))
if file_type != 0x4d42:
return False
file_size = struct.unpack('<i', bmp_file.read(4))[0]
print('file size(kbyte) = ', file_size / 1024)
reserved = bmp_file.read(4)
offset = struct.unpack('<i', bmp_file.read(4))[0]
print('offset = ', offset)
header_size = struct.unpack('<i', bmp_file.read(4))[0]
print('header size = ', header_size)
if header_size != 40:
return False
bmp_width = struct.unpack('<i', bmp_file.read(4))[0]
print('bmp width = ', bmp_width)
if bmp_width != BMP_WIDTH:
return False
bmp_height = struct.unpack('<i', bmp_file.read(4))[0]
print('bmp height = ', bmp_height)
if bmp_height != BMP_HEIGHT:
return False
planes = struct.unpack('<h', bmp_file.read(2))[0]
print('planes = ', planes)
if planes != 1:
return False
bmp_depth = struct.unpack('<h', bmp_file.read(2))[0]
print('bmp depth = ', bmp_depth)
compression = struct.unpack('<i', bmp_file.read(4))[0]
print('compression = ', compression)
if compression != 0:
return False
return offset
def lcd_write_com(d):
lcd_rs.value(0)
mem32[GPIO_OUT] = (mem32[GPIO_IN] & GPIO_MASK) | d
lcd_wr.value(0)
lcd_wr.value(1)
def lcd_write_data(d):
lcd_rs.value(1)
mem32[GPIO_OUT] = (mem32[GPIO_IN] & GPIO_MASK) | d
lcd_wr.value(0)
lcd_wr.value(1)
def address_set(x1, y1, x2, y2):
lcd_write_com(0x2a)
lcd_write_data(x1 >> 8)
lcd_write_data(x1 & 0xFF)
lcd_write_data(x2 >> 8)
lcd_write_data(x2 & 0xFF)
lcd_write_com(0x2b)
lcd_write_data(y1 >> 8)
lcd_write_data(y1 & 0xFF)
lcd_write_data(y2 >> 8)
lcd_write_data(y2 & 0xFF)
lcd_write_com(0x2c)
def lcd_init():
lcd_rst.value(1)
sleep_ms(5)
lcd_rst.value(0)
sleep_ms(15)
lcd_rst.value(1)
sleep_ms(15)
lcd_cs.value(1)
lcd_wr.value(1)
lcd_cs.value(0)
lcd_write_com(0xcb)
lcd_write_data(0x39)
lcd_write_data(0x2c)
lcd_write_data(0x00)
lcd_write_data(0x34)
lcd_write_data(0x02)
lcd_write_com(0xcf)
lcd_write_data(0x00)
lcd_write_data(0xc1)
lcd_write_data(0x30)
lcd_write_com(0xe8)
lcd_write_data(0x85)
lcd_write_data(0x00)
lcd_write_data(0x78)
lcd_write_com(0xea)
lcd_write_data(0x00)
lcd_write_data(0x00)
lcd_write_com(0xed)
lcd_write_data(0x64)
lcd_write_data(0x03)
lcd_write_data(0x12)
lcd_write_data(0x81)
lcd_write_com(0xf7)
lcd_write_data(0x20)
lcd_write_com(0xc0)
lcd_write_data(0x23)
lcd_write_com(0xc1)
lcd_write_data(0x10)
lcd_write_com(0xc5)
lcd_write_data(0x3e)
lcd_write_data(0x28)
lcd_write_com(0xc7)
lcd_write_data(0x86)
lcd_write_com(0x36)
lcd_write_data(0x48)
lcd_write_com(0x3a)
lcd_write_data(0x55)
lcd_write_com(0xb1)
lcd_write_data(0x00)
lcd_write_data(0x18)
lcd_write_com(0xb6)
lcd_write_data(0x08)
lcd_write_data(0x82)
lcd_write_data(0x27)
lcd_write_com(0x11)
sleep_ms(120)
lcd_write_com(0x29)
lcd_write_com(0x2c)
lcd_rd = Pin(LCD_RD, Pin.OUT, value = 1)
lcd_wr = Pin(LCD_WR, Pin.OUT, value = 1)
lcd_rs = Pin(LCD_RS, Pin.OUT, value = 1)
lcd_cs = Pin(LCD_CS, Pin.OUT, value = 1)
lcd_rst = Pin(LCD_RST, Pin.OUT, value = 1)
for i in range(9):
Pin(i, Pin.OUT, value = 1)
lcd_init()
bmp_load('sample.bmp')