Created
September 26, 2022 23:40
-
-
Save elpekenin/a75bf3ed89a36d542acade8b9b86aa91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from machine import Pin,SPI,PWM | |
import framebuf | |
import time | |
import os | |
FREC = 6_000_000 | |
LCD_SCK = 2 | |
LCD_MOSI = 3 | |
LCD_MISO = 4 | |
LCD_CS = 5 | |
LCD_DC = 6 | |
LCD_BL = 7 | |
LCD_RST = 8 | |
TP_CS = 9 | |
TP_IRQ = 10 | |
class LCD_3inch5(framebuf.FrameBuffer): | |
def __init__(self): | |
self.RED = 0x07E0 | |
self.GREEN = 0x001f | |
self.BLUE = 0xf800 | |
self.WHITE = 0xffff | |
self.BLACK = 0x0000 | |
self.rotate = 0 # Set the rotation Angle to 0°, 90°, 180° and 270° | |
if self.rotate in [0, 180]: | |
self.width = 320 | |
self.height = 240 | |
else: | |
self.width = 480 | |
self.height = 160 | |
self.cs = Pin(LCD_CS,Pin.OUT) | |
self.rst = Pin(LCD_RST,Pin.OUT) | |
self.dc = Pin(LCD_DC,Pin.OUT) | |
self.tp_cs = Pin(TP_CS,Pin.OUT) | |
self.irq = Pin(TP_IRQ,Pin.IN) | |
self.cs(1) | |
self.dc(1) | |
self.rst(1) | |
self.tp_cs(1) | |
self.spi = SPI( | |
0, | |
FREC, | |
sck=Pin(LCD_SCK), | |
mosi=Pin(LCD_MOSI), | |
miso=Pin(LCD_MISO) | |
) | |
self.buffer = bytearray(self.height * self.width * 2) | |
super().__init__(self.buffer, self.width, self.height, framebuf.RGB565) | |
self.init_display() | |
def write_cmd(self, cmd): | |
self.cs(1) | |
self.dc(0) | |
self.cs(0) | |
self.spi.write(bytearray([cmd])) | |
self.cs(1) | |
def write_data(self, buf): | |
self.cs(1) | |
self.dc(1) | |
self.cs(0) | |
self.spi.write(bytearray([buf])) | |
self.cs(1) | |
def init_display(self): | |
# Init sequence | |
self.write_cmd(0x01) | |
time.sleep_ms(120) | |
self.write_cmd(0X3A) | |
self.write_data(0x55) | |
self.write_cmd(0XE0) | |
self.write_data(0x0F) | |
self.write_data(0x1F) | |
self.write_data(0x1C) | |
self.write_data(0x0C) | |
self.write_data(0x0F) | |
self.write_data(0x08) | |
self.write_data(0x48) | |
self.write_data(0x98) | |
self.write_data(0x37) | |
self.write_data(0x0A) | |
self.write_data(0x13) | |
self.write_data(0x04) | |
self.write_data(0x11) | |
self.write_data(0x0D) | |
self.write_data(0x00) | |
self.write_cmd(0XE1) | |
self.write_data(0x0F) | |
self.write_data(0x32) | |
self.write_data(0x2E) | |
self.write_data(0x0B) | |
self.write_data(0x0D) | |
self.write_data(0x05) | |
self.write_data(0x47) | |
self.write_data(0x75) | |
self.write_data(0x37) | |
self.write_data(0x06) | |
self.write_data(0x10) | |
self.write_data(0x03) | |
self.write_data(0x24) | |
self.write_data(0x20) | |
self.write_data(0x00) | |
self.write_cmd(0xC0) | |
self.write_data(0x0D) | |
self.write_data(0x0D) | |
self.write_cmd(0xC1) | |
self.write_data(0x43) | |
self.write_data(0x00) | |
self.write_cmd(0xC2) | |
self.write_data(0x00) | |
self.write_cmd(0XC5) | |
self.write_data(0x00) | |
self.write_data(0x48) | |
self.write_data(0x00) | |
self.write_data(0x48) | |
self.write_cmd(0xB4) | |
self.write_data(0x02) | |
# Rotation sequence | |
self.write_cmd(0x36) | |
if self.rotate in [0, 180]: | |
self.write_data(0x08) | |
else: | |
self.write_data(0x28) | |
self.write_cmd(0xB6) | |
self.write_data(0x00) | |
if self.rotate == 0: | |
self.write_data(0x42) | |
elif self.rotate == 90: | |
self.write_data(0x62) | |
elif self.rotate == 180: | |
self.write_data(0x22) | |
else: | |
self.write_data(0x02) | |
self.write_cmd(0x11) | |
time.sleep_ms(5) | |
self.write_cmd(0x29) | |
time.sleep_ms(5) | |
def show_up(self): | |
if self.rotate in [0 ,180]: | |
self.write_cmd(0x2A) | |
self.write_data(0x00) | |
self.write_data(0x00) | |
self.write_data(0x01) | |
self.write_data(0x3f) | |
self.write_cmd(0x2B) | |
self.write_data(0x00) | |
self.write_data(0x00) | |
self.write_data(0x00) | |
self.write_data(0xef) | |
else: | |
self.write_cmd(0x2A) | |
self.write_data(0x00) | |
self.write_data(0x00) | |
self.write_data(0x01) | |
self.write_data(0xdf) | |
self.write_cmd(0x2B) | |
self.write_data(0x00) | |
self.write_data(0x00) | |
self.write_data(0x00) | |
self.write_data(0x9f) | |
self.write_cmd(0x2C) | |
self.dc(1) | |
self.cs(0) | |
self.spi.write(self.buffer) | |
self.cs(1) | |
def show_down(self): | |
if self.rotate in [0, 180]: | |
self.write_cmd(0x2A) | |
self.write_data(0x00) | |
self.write_data(0x00) | |
self.write_data(0x01) | |
self.write_data(0x3f) | |
self.write_cmd(0x2B) | |
self.write_data(0x00) | |
self.write_data(0xf0) | |
self.write_data(0x01) | |
self.write_data(0xdf) | |
else: | |
self.write_cmd(0x2A) | |
self.write_data(0x00) | |
self.write_data(0x00) | |
self.write_data(0x01) | |
self.write_data(0xdf) | |
self.write_cmd(0x2B) | |
self.write_data(0x00) | |
self.write_data(0xA0) | |
self.write_data(0x01) | |
self.write_data(0x3f) | |
self.write_cmd(0x2C) | |
self.cs(1) | |
self.dc(1) | |
self.cs(0) | |
self.spi.write(self.buffer) | |
self.cs(1) | |
def bl_ctrl(self,duty): | |
pwm = PWM(Pin(LCD_BL)) | |
pwm.freq(1000) | |
if(duty>=100): | |
pwm.duty_u16(65535) | |
else: | |
pwm.duty_u16(655*duty) | |
def draw_point(self, x, y, color): | |
self.write_cmd(0x2A) | |
self.write_data((x-2)>>8) | |
self.write_data((x-2)&0xff) | |
self.write_data(x>>8) | |
self.write_data(x&0xff) | |
self.write_cmd(0x2B) | |
self.write_data((y-2)>>8) | |
self.write_data((y-2)&0xff) | |
self.write_data(y>>8) | |
self.write_data(y&0xff) | |
self.write_cmd(0x2C) | |
self.cs(1) | |
self.dc(1) | |
self.cs(0) | |
for i in range(0,9): | |
h_color = bytearray(color>>8) | |
l_color = bytearray(color&0xff) | |
self.spi.write(h_color) | |
self.spi.write(l_color) | |
self.cs(1) | |
def touch_get(self): | |
if self.irq() != 0: # Nothing to read | |
return | |
self.spi = SPI( | |
0, | |
5_000_000, | |
sck=Pin(LCD_SCK), | |
mosi=Pin(LCD_MOSI), | |
miso=Pin(LCD_MISO) | |
) | |
self.tp_cs(0) | |
X_Point = 0 | |
Y_Point = 0 | |
for i in range(0,3): | |
self.spi.write(bytearray([0XD0])) | |
Read_date = self.spi.read(2) | |
time.sleep_us(10) | |
X_Point=X_Point+(((Read_date[0]<<8)+Read_date[1])>>3) | |
self.spi.write(bytearray([0X90])) | |
Read_date = self.spi.read(2) | |
Y_Point=Y_Point+(((Read_date[0]<<8)+Read_date[1])>>3) | |
X_Point=X_Point/3 | |
Y_Point=Y_Point/3 | |
self.tp_cs(1) | |
self.spi = SPI(0, FREC, sck=Pin(LCD_SCK), mosi=Pin(LCD_MOSI), miso=Pin(LCD_MISO)) | |
Result_list = [X_Point,Y_Point] | |
#print(Result_list) | |
return(Result_list) | |
def my_point(self, x, y, color=None): | |
if color is None: | |
color = self.BLUE | |
# X viewport | |
self.write_cmd(0x2A) | |
for _ in range(2): | |
self.write_data(x >> 8) | |
self.write_data(x & 0xFF) | |
# Y viewport | |
self.write_cmd(0x2B) | |
for _ in range(2): | |
self.write_data(y >> 8) | |
self.write_data(y & 0xFF) | |
# Data | |
self.write_cmd(0x2C) | |
self.write_data(color >> 8) | |
self.write_data(color & 0xFF) | |
if __name__=='__main__': | |
import sys | |
time.sleep(2) | |
lcd = LCD_3inch5() | |
lcd.bl_ctrl(100) | |
# Fill screen black for non-noisy screen, !! uses the framebuffer | |
lcd.show_up() | |
lcd.show_down() | |
# Manually sending a rect | |
lcd.write_cmd(0x2A) | |
lcd.write_data(0x00) | |
lcd.write_data(0x00) | |
lcd.write_data(0x00) | |
lcd.write_data(0x0F) | |
lcd.write_cmd(0x2B) | |
lcd.write_data(0x00) | |
lcd.write_data(0x00) | |
lcd.write_data(0x00) | |
lcd.write_data(0x0F) | |
lcd.write_cmd(0x2C) | |
for _ in range(0xF ** 2): | |
lcd.write_data(lcd.BLUE >> 8) | |
lcd.write_data(lcd.BLUE & 0xFF) | |
sys.exit() | |
if lcd.rotate in [0, 180]: #Determining the display direction | |
lcd.fill_rect(60,75,200,30,lcd.RED) | |
lcd.text("Raspberry Pi Pico",90,87,lcd.WHITE) | |
display_color = 0x001F | |
lcd.text("3.5' IPS LCD TEST",90,127,lcd.BLACK) | |
for i in range(0,12): | |
lcd.fill_rect(i*20+35,170,30,120,(display_color)) | |
display_color = display_color << 1 | |
lcd.show_up() | |
sys.exit() | |
while True: | |
get = lcd.touch_get() | |
if get != None: | |
X_Point = int((get[1]-430)*480/3270) | |
if(X_Point>480): | |
X_Point = 480 | |
elif X_Point<0: | |
X_Point = 0 | |
Y_Point = 320-int((get[0]-430)*320/3270) | |
if lcd.rotate == 0: | |
if(X_Point>400 and X_Point<450): | |
lcd.fill(lcd.BLACK) | |
if(Y_Point>240): | |
lcd.fill_rect(10,150,75,50,lcd.RED) | |
lcd.text("Button0",20,170,lcd.WHITE) | |
elif(Y_Point>160): | |
lcd.fill_rect(85,150,75,50,lcd.RED) | |
lcd.text("Button1",100,170,lcd.WHITE) | |
elif(Y_Point>90): | |
lcd.fill_rect(160,150,75,50,lcd.RED) | |
lcd.text("Button2",175,170,lcd.WHITE) | |
else: | |
lcd.fill_rect(235,150,75,50,lcd.RED) | |
lcd.text("Button3",250,170,lcd.WHITE) | |
else : | |
lcd.fill(lcd.BLACK) | |
lcd.text("Button0",20,170,lcd.WHITE) | |
lcd.text("Button1",100,170,lcd.WHITE) | |
lcd.text("Button2",175,170,lcd.WHITE) | |
lcd.text("Button3",250,170,lcd.WHITE) | |
lcd.show_down() | |
time.sleep(0.1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment