
from machine import Pin,SPI,PWM
import framebuf
import time
import os

BL = 13
DC = 8
RST = 12
MOSI = 11
SCK = 10
CS = 9


class LCD_1inch3(framebuf.FrameBuffer):
    def __init__(self):
        self.width = 240
        self.height = 240
        
        self.cs = Pin(CS,Pin.OUT)
        self.rst = Pin(RST,Pin.OUT)
        
        self.cs(1)
        self.spi = SPI(1)
        self.spi = SPI(1,1000_000)
        self.spi = SPI(1,100000_000,polarity=0, phase=0,sck=Pin(SCK),mosi=Pin(MOSI),miso=None)
        self.dc = Pin(DC,Pin.OUT)
        self.dc(1)
        self.buffer = bytearray(self.height * self.width * 2)
        super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
        self.init_display()
        
        self.red   =   0x07E0
        self.green =   0x001f
        self.blue  =   0xf800
        self.white =   0xffff
        
    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):
        """Initialize dispaly"""  
        self.rst(1)
        self.rst(0)
        self.rst(1)
        
        self.write_cmd(0x36)
        self.write_data(0x70)

        self.write_cmd(0x3A) 
        self.write_data(0x05)

        self.write_cmd(0xB2)
        self.write_data(0x0C)
        self.write_data(0x0C)
        self.write_data(0x00)
        self.write_data(0x33)
        self.write_data(0x33)

        self.write_cmd(0xB7)
        self.write_data(0x35) 

        self.write_cmd(0xBB)
        self.write_data(0x19)

        self.write_cmd(0xC0)
        self.write_data(0x2C)

        self.write_cmd(0xC2)
        self.write_data(0x01)

        self.write_cmd(0xC3)
        self.write_data(0x12)   

        self.write_cmd(0xC4)
        self.write_data(0x20)

        self.write_cmd(0xC6)
        self.write_data(0x0F) 

        self.write_cmd(0xD0)
        self.write_data(0xA4)
        self.write_data(0xA1)

        self.write_cmd(0xE0)
        self.write_data(0xD0)
        self.write_data(0x04)
        self.write_data(0x0D)
        self.write_data(0x11)
        self.write_data(0x13)
        self.write_data(0x2B)
        self.write_data(0x3F)
        self.write_data(0x54)
        self.write_data(0x4C)
        self.write_data(0x18)
        self.write_data(0x0D)
        self.write_data(0x0B)
        self.write_data(0x1F)
        self.write_data(0x23)

        self.write_cmd(0xE1)
        self.write_data(0xD0)
        self.write_data(0x04)
        self.write_data(0x0C)
        self.write_data(0x11)
        self.write_data(0x13)
        self.write_data(0x2C)
        self.write_data(0x3F)
        self.write_data(0x44)
        self.write_data(0x51)
        self.write_data(0x2F)
        self.write_data(0x1F)
        self.write_data(0x1F)
        self.write_data(0x20)
        self.write_data(0x23)
        
        self.write_cmd(0x21)

        self.write_cmd(0x11)

        self.write_cmd(0x29)

    def show(self):
        self.write_cmd(0x2A)
        self.write_data(0x00)
        self.write_data(0x00)
        self.write_data(0x00)
        self.write_data(0xef)
        
        self.write_cmd(0x2B)
        self.write_data(0x00)
        self.write_data(0x00)
        self.write_data(0x00)
        self.write_data(0xEF)
        
        self.write_cmd(0x2C)
        
        self.cs(1)
        self.dc(1)
        self.cs(0)
        self.spi.write(self.buffer)
        self.cs(1)
        
  
if __name__=='__main__':
    pwm = PWM(Pin(BL))
    pwm.freq(1000)
    pwm.duty_u16(32768)#max 65535

    LCD = LCD_1inch3()
    #color BRG
    LCD.fill(LCD.white)
    LCD.show()
    

    keyA = Pin(15,Pin.IN,Pin.PULL_UP)
    keyB = Pin(17,Pin.IN,Pin.PULL_UP)
    keyX = Pin(19 ,Pin.IN,Pin.PULL_UP)
    keyY= Pin(21 ,Pin.IN,Pin.PULL_UP)
    
    up = Pin(2,Pin.IN,Pin.PULL_UP)
    dowm = Pin(18,Pin.IN,Pin.PULL_UP)
    left = Pin(16,Pin.IN,Pin.PULL_UP)
    right = Pin(20,Pin.IN,Pin.PULL_UP)
    ctrl = Pin(3,Pin.IN,Pin.PULL_UP)
    
    while(1):
        if keyA.value() == 0:
            LCD.fill_rect(208,15,30,30,LCD.red)
            print("A")
        else :
            LCD.fill_rect(208,15,30,30,LCD.white)
            LCD.rect(208,15,30,30,LCD.red)
            
            
        if(keyB.value() == 0):
            LCD.fill_rect(208,75,30,30,LCD.red)
            print("B")
        else :
            LCD.fill_rect(208,75,30,30,LCD.white)
            LCD.rect(208,75,30,30,LCD.red)
            
            
        if(keyX.value() == 0):
            LCD.fill_rect(208,135,30,30,LCD.red)
            print("C")
        else :
            LCD.fill_rect(208,135,30,30,LCD.white)
            LCD.rect(208,135,30,30,LCD.red)
            
        if(keyY.value() == 0):
            LCD.fill_rect(208,195,30,30,LCD.red)
            print("D")
        else :
            LCD.fill_rect(208,195,30,30,LCD.white)
            LCD.rect(208,195,30,30,LCD.red)
            
        if(up.value() == 0):
            LCD.fill_rect(60,60,30,30,LCD.red)
            print("UP")
        else :
            LCD.fill_rect(60,60,30,30,LCD.white)
            LCD.rect(60,60,30,30,LCD.red)
            
        if(dowm.value() == 0):
            LCD.fill_rect(60,150,30,30,LCD.red)
            print("DOWM")
        else :
            LCD.fill_rect(60,150,30,30,LCD.white)
            LCD.rect(60,150,30,30,LCD.red)
            
        if(left.value() == 0):
            LCD.fill_rect(15,105,30,30,LCD.red)
            print("LEFT")
        else :
            LCD.fill_rect(15,105,30,30,LCD.white)
            LCD.rect(15,105,30,30,LCD.red)
        
        if(right.value() == 0):
            LCD.fill_rect(105,105,30,30,LCD.red)
            print("RIGHT")
        else :
            LCD.fill_rect(105,105,30,30,LCD.white)
            LCD.rect(105,105,30,30,LCD.red)
        
        if(ctrl.value() == 0):
            LCD.fill_rect(60,105,30,30,LCD.red)
            print("CTRL")
        else :
            LCD.fill_rect(60,105,30,30,LCD.white)
            LCD.rect(60,105,30,30,LCD.red)
                       
        LCD.show()
    time.sleep(1)
    LCD.fill(0xFFFF)





ced541ccb0630796c6f7dceeba6e50032ef8d84c
<mask token>


class LCD_1inch3(framebuf.FrameBuffer):

    def __init__(self):
        self.width = 240
        self.height = 240
        self.cs = Pin(CS, Pin.OUT)
        self.rst = Pin(RST, Pin.OUT)
        self.cs(1)
        self.spi = SPI(1)
        self.spi = SPI(1, 1000000)
        self.spi = SPI(1, 100000000, polarity=0, phase=0, sck=Pin(SCK),
            mosi=Pin(MOSI), miso=None)
        self.dc = Pin(DC, Pin.OUT)
        self.dc(1)
        self.buffer = bytearray(self.height * self.width * 2)
        super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
        self.init_display()
        self.red = 2016
        self.green = 31
        self.blue = 63488
        self.white = 65535

    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):
        """Initialize dispaly"""
        self.rst(1)
        self.rst(0)
        self.rst(1)
        self.write_cmd(54)
        self.write_data(112)
        self.write_cmd(58)
        self.write_data(5)
        self.write_cmd(178)
        self.write_data(12)
        self.write_data(12)
        self.write_data(0)
        self.write_data(51)
        self.write_data(51)
        self.write_cmd(183)
        self.write_data(53)
        self.write_cmd(187)
        self.write_data(25)
        self.write_cmd(192)
        self.write_data(44)
        self.write_cmd(194)
        self.write_data(1)
        self.write_cmd(195)
        self.write_data(18)
        self.write_cmd(196)
        self.write_data(32)
        self.write_cmd(198)
        self.write_data(15)
        self.write_cmd(208)
        self.write_data(164)
        self.write_data(161)
        self.write_cmd(224)
        self.write_data(208)
        self.write_data(4)
        self.write_data(13)
        self.write_data(17)
        self.write_data(19)
        self.write_data(43)
        self.write_data(63)
        self.write_data(84)
        self.write_data(76)
        self.write_data(24)
        self.write_data(13)
        self.write_data(11)
        self.write_data(31)
        self.write_data(35)
        self.write_cmd(225)
        self.write_data(208)
        self.write_data(4)
        self.write_data(12)
        self.write_data(17)
        self.write_data(19)
        self.write_data(44)
        self.write_data(63)
        self.write_data(68)
        self.write_data(81)
        self.write_data(47)
        self.write_data(31)
        self.write_data(31)
        self.write_data(32)
        self.write_data(35)
        self.write_cmd(33)
        self.write_cmd(17)
        self.write_cmd(41)

    def show(self):
        self.write_cmd(42)
        self.write_data(0)
        self.write_data(0)
        self.write_data(0)
        self.write_data(239)
        self.write_cmd(43)
        self.write_data(0)
        self.write_data(0)
        self.write_data(0)
        self.write_data(239)
        self.write_cmd(44)
        self.cs(1)
        self.dc(1)
        self.cs(0)
        self.spi.write(self.buffer)
        self.cs(1)


<mask token>

<mask token>


class LCD_1inch3(framebuf.FrameBuffer):

    def __init__(self):
        self.width = 240
        self.height = 240
        self.cs = Pin(CS, Pin.OUT)
        self.rst = Pin(RST, Pin.OUT)
        self.cs(1)
        self.spi = SPI(1)
        self.spi = SPI(1, 1000000)
        self.spi = SPI(1, 100000000, polarity=0, phase=0, sck=Pin(SCK),
            mosi=Pin(MOSI), miso=None)
        self.dc = Pin(DC, Pin.OUT)
        self.dc(1)
        self.buffer = bytearray(self.height * self.width * 2)
        super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
        self.init_display()
        self.red = 2016
        self.green = 31
        self.blue = 63488
        self.white = 65535

    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):
        """Initialize dispaly"""
        self.rst(1)
        self.rst(0)
        self.rst(1)
        self.write_cmd(54)
        self.write_data(112)
        self.write_cmd(58)
        self.write_data(5)
        self.write_cmd(178)
        self.write_data(12)
        self.write_data(12)
        self.write_data(0)
        self.write_data(51)
        self.write_data(51)
        self.write_cmd(183)
        self.write_data(53)
        self.write_cmd(187)
        self.write_data(25)
        self.write_cmd(192)
        self.write_data(44)
        self.write_cmd(194)
        self.write_data(1)
        self.write_cmd(195)
        self.write_data(18)
        self.write_cmd(196)
        self.write_data(32)
        self.write_cmd(198)
        self.write_data(15)
        self.write_cmd(208)
        self.write_data(164)
        self.write_data(161)
        self.write_cmd(224)
        self.write_data(208)
        self.write_data(4)
        self.write_data(13)
        self.write_data(17)
        self.write_data(19)
        self.write_data(43)
        self.write_data(63)
        self.write_data(84)
        self.write_data(76)
        self.write_data(24)
        self.write_data(13)
        self.write_data(11)
        self.write_data(31)
        self.write_data(35)
        self.write_cmd(225)
        self.write_data(208)
        self.write_data(4)
        self.write_data(12)
        self.write_data(17)
        self.write_data(19)
        self.write_data(44)
        self.write_data(63)
        self.write_data(68)
        self.write_data(81)
        self.write_data(47)
        self.write_data(31)
        self.write_data(31)
        self.write_data(32)
        self.write_data(35)
        self.write_cmd(33)
        self.write_cmd(17)
        self.write_cmd(41)

    def show(self):
        self.write_cmd(42)
        self.write_data(0)
        self.write_data(0)
        self.write_data(0)
        self.write_data(239)
        self.write_cmd(43)
        self.write_data(0)
        self.write_data(0)
        self.write_data(0)
        self.write_data(239)
        self.write_cmd(44)
        self.cs(1)
        self.dc(1)
        self.cs(0)
        self.spi.write(self.buffer)
        self.cs(1)


if __name__ == '__main__':
    pwm = PWM(Pin(BL))
    pwm.freq(1000)
    pwm.duty_u16(32768)
    LCD = LCD_1inch3()
    LCD.fill(LCD.white)
    LCD.show()
    keyA = Pin(15, Pin.IN, Pin.PULL_UP)
    keyB = Pin(17, Pin.IN, Pin.PULL_UP)
    keyX = Pin(19, Pin.IN, Pin.PULL_UP)
    keyY = Pin(21, Pin.IN, Pin.PULL_UP)
    up = Pin(2, Pin.IN, Pin.PULL_UP)
    dowm = Pin(18, Pin.IN, Pin.PULL_UP)
    left = Pin(16, Pin.IN, Pin.PULL_UP)
    right = Pin(20, Pin.IN, Pin.PULL_UP)
    ctrl = Pin(3, Pin.IN, Pin.PULL_UP)
    while 1:
        if keyA.value() == 0:
            LCD.fill_rect(208, 15, 30, 30, LCD.red)
            print('A')
        else:
            LCD.fill_rect(208, 15, 30, 30, LCD.white)
            LCD.rect(208, 15, 30, 30, LCD.red)
        if keyB.value() == 0:
            LCD.fill_rect(208, 75, 30, 30, LCD.red)
            print('B')
        else:
            LCD.fill_rect(208, 75, 30, 30, LCD.white)
            LCD.rect(208, 75, 30, 30, LCD.red)
        if keyX.value() == 0:
            LCD.fill_rect(208, 135, 30, 30, LCD.red)
            print('C')
        else:
            LCD.fill_rect(208, 135, 30, 30, LCD.white)
            LCD.rect(208, 135, 30, 30, LCD.red)
        if keyY.value() == 0:
            LCD.fill_rect(208, 195, 30, 30, LCD.red)
            print('D')
        else:
            LCD.fill_rect(208, 195, 30, 30, LCD.white)
            LCD.rect(208, 195, 30, 30, LCD.red)
        if up.value() == 0:
            LCD.fill_rect(60, 60, 30, 30, LCD.red)
            print('UP')
        else:
            LCD.fill_rect(60, 60, 30, 30, LCD.white)
            LCD.rect(60, 60, 30, 30, LCD.red)
        if dowm.value() == 0:
            LCD.fill_rect(60, 150, 30, 30, LCD.red)
            print('DOWM')
        else:
            LCD.fill_rect(60, 150, 30, 30, LCD.white)
            LCD.rect(60, 150, 30, 30, LCD.red)
        if left.value() == 0:
            LCD.fill_rect(15, 105, 30, 30, LCD.red)
            print('LEFT')
        else:
            LCD.fill_rect(15, 105, 30, 30, LCD.white)
            LCD.rect(15, 105, 30, 30, LCD.red)
        if right.value() == 0:
            LCD.fill_rect(105, 105, 30, 30, LCD.red)
            print('RIGHT')
        else:
            LCD.fill_rect(105, 105, 30, 30, LCD.white)
            LCD.rect(105, 105, 30, 30, LCD.red)
        if ctrl.value() == 0:
            LCD.fill_rect(60, 105, 30, 30, LCD.red)
            print('CTRL')
        else:
            LCD.fill_rect(60, 105, 30, 30, LCD.white)
            LCD.rect(60, 105, 30, 30, LCD.red)
        LCD.show()
    time.sleep(1)
    LCD.fill(65535)

<mask token>
BL = 13
DC = 8
RST = 12
MOSI = 11
SCK = 10
CS = 9


class LCD_1inch3(framebuf.FrameBuffer):

    def __init__(self):
        self.width = 240
        self.height = 240
        self.cs = Pin(CS, Pin.OUT)
        self.rst = Pin(RST, Pin.OUT)
        self.cs(1)
        self.spi = SPI(1)
        self.spi = SPI(1, 1000000)
        self.spi = SPI(1, 100000000, polarity=0, phase=0, sck=Pin(SCK),
            mosi=Pin(MOSI), miso=None)
        self.dc = Pin(DC, Pin.OUT)
        self.dc(1)
        self.buffer = bytearray(self.height * self.width * 2)
        super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
        self.init_display()
        self.red = 2016
        self.green = 31
        self.blue = 63488
        self.white = 65535

    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):
        """Initialize dispaly"""
        self.rst(1)
        self.rst(0)
        self.rst(1)
        self.write_cmd(54)
        self.write_data(112)
        self.write_cmd(58)
        self.write_data(5)
        self.write_cmd(178)
        self.write_data(12)
        self.write_data(12)
        self.write_data(0)
        self.write_data(51)
        self.write_data(51)
        self.write_cmd(183)
        self.write_data(53)
        self.write_cmd(187)
        self.write_data(25)
        self.write_cmd(192)
        self.write_data(44)
        self.write_cmd(194)
        self.write_data(1)
        self.write_cmd(195)
        self.write_data(18)
        self.write_cmd(196)
        self.write_data(32)
        self.write_cmd(198)
        self.write_data(15)
        self.write_cmd(208)
        self.write_data(164)
        self.write_data(161)
        self.write_cmd(224)
        self.write_data(208)
        self.write_data(4)
        self.write_data(13)
        self.write_data(17)
        self.write_data(19)
        self.write_data(43)
        self.write_data(63)
        self.write_data(84)
        self.write_data(76)
        self.write_data(24)
        self.write_data(13)
        self.write_data(11)
        self.write_data(31)
        self.write_data(35)
        self.write_cmd(225)
        self.write_data(208)
        self.write_data(4)
        self.write_data(12)
        self.write_data(17)
        self.write_data(19)
        self.write_data(44)
        self.write_data(63)
        self.write_data(68)
        self.write_data(81)
        self.write_data(47)
        self.write_data(31)
        self.write_data(31)
        self.write_data(32)
        self.write_data(35)
        self.write_cmd(33)
        self.write_cmd(17)
        self.write_cmd(41)

    def show(self):
        self.write_cmd(42)
        self.write_data(0)
        self.write_data(0)
        self.write_data(0)
        self.write_data(239)
        self.write_cmd(43)
        self.write_data(0)
        self.write_data(0)
        self.write_data(0)
        self.write_data(239)
        self.write_cmd(44)
        self.cs(1)
        self.dc(1)
        self.cs(0)
        self.spi.write(self.buffer)
        self.cs(1)


if __name__ == '__main__':
    pwm = PWM(Pin(BL))
    pwm.freq(1000)
    pwm.duty_u16(32768)
    LCD = LCD_1inch3()
    LCD.fill(LCD.white)
    LCD.show()
    keyA = Pin(15, Pin.IN, Pin.PULL_UP)
    keyB = Pin(17, Pin.IN, Pin.PULL_UP)
    keyX = Pin(19, Pin.IN, Pin.PULL_UP)
    keyY = Pin(21, Pin.IN, Pin.PULL_UP)
    up = Pin(2, Pin.IN, Pin.PULL_UP)
    dowm = Pin(18, Pin.IN, Pin.PULL_UP)
    left = Pin(16, Pin.IN, Pin.PULL_UP)
    right = Pin(20, Pin.IN, Pin.PULL_UP)
    ctrl = Pin(3, Pin.IN, Pin.PULL_UP)
    while 1:
        if keyA.value() == 0:
            LCD.fill_rect(208, 15, 30, 30, LCD.red)
            print('A')
        else:
            LCD.fill_rect(208, 15, 30, 30, LCD.white)
            LCD.rect(208, 15, 30, 30, LCD.red)
        if keyB.value() == 0:
            LCD.fill_rect(208, 75, 30, 30, LCD.red)
            print('B')
        else:
            LCD.fill_rect(208, 75, 30, 30, LCD.white)
            LCD.rect(208, 75, 30, 30, LCD.red)
        if keyX.value() == 0:
            LCD.fill_rect(208, 135, 30, 30, LCD.red)
            print('C')
        else:
            LCD.fill_rect(208, 135, 30, 30, LCD.white)
            LCD.rect(208, 135, 30, 30, LCD.red)
        if keyY.value() == 0:
            LCD.fill_rect(208, 195, 30, 30, LCD.red)
            print('D')
        else:
            LCD.fill_rect(208, 195, 30, 30, LCD.white)
            LCD.rect(208, 195, 30, 30, LCD.red)
        if up.value() == 0:
            LCD.fill_rect(60, 60, 30, 30, LCD.red)
            print('UP')
        else:
            LCD.fill_rect(60, 60, 30, 30, LCD.white)
            LCD.rect(60, 60, 30, 30, LCD.red)
        if dowm.value() == 0:
            LCD.fill_rect(60, 150, 30, 30, LCD.red)
            print('DOWM')
        else:
            LCD.fill_rect(60, 150, 30, 30, LCD.white)
            LCD.rect(60, 150, 30, 30, LCD.red)
        if left.value() == 0:
            LCD.fill_rect(15, 105, 30, 30, LCD.red)
            print('LEFT')
        else:
            LCD.fill_rect(15, 105, 30, 30, LCD.white)
            LCD.rect(15, 105, 30, 30, LCD.red)
        if right.value() == 0:
            LCD.fill_rect(105, 105, 30, 30, LCD.red)
            print('RIGHT')
        else:
            LCD.fill_rect(105, 105, 30, 30, LCD.white)
            LCD.rect(105, 105, 30, 30, LCD.red)
        if ctrl.value() == 0:
            LCD.fill_rect(60, 105, 30, 30, LCD.red)
            print('CTRL')
        else:
            LCD.fill_rect(60, 105, 30, 30, LCD.white)
            LCD.rect(60, 105, 30, 30, LCD.red)
        LCD.show()
    time.sleep(1)
    LCD.fill(65535)

from machine import Pin, SPI, PWM
import framebuf
import time
import os
BL = 13
DC = 8
RST = 12
MOSI = 11
SCK = 10
CS = 9


class LCD_1inch3(framebuf.FrameBuffer):

    def __init__(self):
        self.width = 240
        self.height = 240
        self.cs = Pin(CS, Pin.OUT)
        self.rst = Pin(RST, Pin.OUT)
        self.cs(1)
        self.spi = SPI(1)
        self.spi = SPI(1, 1000000)
        self.spi = SPI(1, 100000000, polarity=0, phase=0, sck=Pin(SCK),
            mosi=Pin(MOSI), miso=None)
        self.dc = Pin(DC, Pin.OUT)
        self.dc(1)
        self.buffer = bytearray(self.height * self.width * 2)
        super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
        self.init_display()
        self.red = 2016
        self.green = 31
        self.blue = 63488
        self.white = 65535

    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):
        """Initialize dispaly"""
        self.rst(1)
        self.rst(0)
        self.rst(1)
        self.write_cmd(54)
        self.write_data(112)
        self.write_cmd(58)
        self.write_data(5)
        self.write_cmd(178)
        self.write_data(12)
        self.write_data(12)
        self.write_data(0)
        self.write_data(51)
        self.write_data(51)
        self.write_cmd(183)
        self.write_data(53)
        self.write_cmd(187)
        self.write_data(25)
        self.write_cmd(192)
        self.write_data(44)
        self.write_cmd(194)
        self.write_data(1)
        self.write_cmd(195)
        self.write_data(18)
        self.write_cmd(196)
        self.write_data(32)
        self.write_cmd(198)
        self.write_data(15)
        self.write_cmd(208)
        self.write_data(164)
        self.write_data(161)
        self.write_cmd(224)
        self.write_data(208)
        self.write_data(4)
        self.write_data(13)
        self.write_data(17)
        self.write_data(19)
        self.write_data(43)
        self.write_data(63)
        self.write_data(84)
        self.write_data(76)
        self.write_data(24)
        self.write_data(13)
        self.write_data(11)
        self.write_data(31)
        self.write_data(35)
        self.write_cmd(225)
        self.write_data(208)
        self.write_data(4)
        self.write_data(12)
        self.write_data(17)
        self.write_data(19)
        self.write_data(44)
        self.write_data(63)
        self.write_data(68)
        self.write_data(81)
        self.write_data(47)
        self.write_data(31)
        self.write_data(31)
        self.write_data(32)
        self.write_data(35)
        self.write_cmd(33)
        self.write_cmd(17)
        self.write_cmd(41)

    def show(self):
        self.write_cmd(42)
        self.write_data(0)
        self.write_data(0)
        self.write_data(0)
        self.write_data(239)
        self.write_cmd(43)
        self.write_data(0)
        self.write_data(0)
        self.write_data(0)
        self.write_data(239)
        self.write_cmd(44)
        self.cs(1)
        self.dc(1)
        self.cs(0)
        self.spi.write(self.buffer)
        self.cs(1)


if __name__ == '__main__':
    pwm = PWM(Pin(BL))
    pwm.freq(1000)
    pwm.duty_u16(32768)
    LCD = LCD_1inch3()
    LCD.fill(LCD.white)
    LCD.show()
    keyA = Pin(15, Pin.IN, Pin.PULL_UP)
    keyB = Pin(17, Pin.IN, Pin.PULL_UP)
    keyX = Pin(19, Pin.IN, Pin.PULL_UP)
    keyY = Pin(21, Pin.IN, Pin.PULL_UP)
    up = Pin(2, Pin.IN, Pin.PULL_UP)
    dowm = Pin(18, Pin.IN, Pin.PULL_UP)
    left = Pin(16, Pin.IN, Pin.PULL_UP)
    right = Pin(20, Pin.IN, Pin.PULL_UP)
    ctrl = Pin(3, Pin.IN, Pin.PULL_UP)
    while 1:
        if keyA.value() == 0:
            LCD.fill_rect(208, 15, 30, 30, LCD.red)
            print('A')
        else:
            LCD.fill_rect(208, 15, 30, 30, LCD.white)
            LCD.rect(208, 15, 30, 30, LCD.red)
        if keyB.value() == 0:
            LCD.fill_rect(208, 75, 30, 30, LCD.red)
            print('B')
        else:
            LCD.fill_rect(208, 75, 30, 30, LCD.white)
            LCD.rect(208, 75, 30, 30, LCD.red)
        if keyX.value() == 0:
            LCD.fill_rect(208, 135, 30, 30, LCD.red)
            print('C')
        else:
            LCD.fill_rect(208, 135, 30, 30, LCD.white)
            LCD.rect(208, 135, 30, 30, LCD.red)
        if keyY.value() == 0:
            LCD.fill_rect(208, 195, 30, 30, LCD.red)
            print('D')
        else:
            LCD.fill_rect(208, 195, 30, 30, LCD.white)
            LCD.rect(208, 195, 30, 30, LCD.red)
        if up.value() == 0:
            LCD.fill_rect(60, 60, 30, 30, LCD.red)
            print('UP')
        else:
            LCD.fill_rect(60, 60, 30, 30, LCD.white)
            LCD.rect(60, 60, 30, 30, LCD.red)
        if dowm.value() == 0:
            LCD.fill_rect(60, 150, 30, 30, LCD.red)
            print('DOWM')
        else:
            LCD.fill_rect(60, 150, 30, 30, LCD.white)
            LCD.rect(60, 150, 30, 30, LCD.red)
        if left.value() == 0:
            LCD.fill_rect(15, 105, 30, 30, LCD.red)
            print('LEFT')
        else:
            LCD.fill_rect(15, 105, 30, 30, LCD.white)
            LCD.rect(15, 105, 30, 30, LCD.red)
        if right.value() == 0:
            LCD.fill_rect(105, 105, 30, 30, LCD.red)
            print('RIGHT')
        else:
            LCD.fill_rect(105, 105, 30, 30, LCD.white)
            LCD.rect(105, 105, 30, 30, LCD.red)
        if ctrl.value() == 0:
            LCD.fill_rect(60, 105, 30, 30, LCD.red)
            print('CTRL')
        else:
            LCD.fill_rect(60, 105, 30, 30, LCD.white)
            LCD.rect(60, 105, 30, 30, LCD.red)
        LCD.show()
    time.sleep(1)
    LCD.fill(65535)


from machine import Pin,SPI,PWM
import framebuf
import time
import os

BL = 13
DC = 8
RST = 12
MOSI = 11
SCK = 10
CS = 9


class LCD_1inch3(framebuf.FrameBuffer):
    def __init__(self):
        self.width = 240
        self.height = 240
        
        self.cs = Pin(CS,Pin.OUT)
        self.rst = Pin(RST,Pin.OUT)
        
        self.cs(1)
        self.spi = SPI(1)
        self.spi = SPI(1,1000_000)
        self.spi = SPI(1,100000_000,polarity=0, phase=0,sck=Pin(SCK),mosi=Pin(MOSI),miso=None)
        self.dc = Pin(DC,Pin.OUT)
        self.dc(1)
        self.buffer = bytearray(self.height * self.width * 2)
        super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
        self.init_display()
        
        self.red   =   0x07E0
        self.green =   0x001f
        self.blue  =   0xf800
        self.white =   0xffff
        
    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):
        """Initialize dispaly"""  
        self.rst(1)
        self.rst(0)
        self.rst(1)
        
        self.write_cmd(0x36)
        self.write_data(0x70)

        self.write_cmd(0x3A) 
        self.write_data(0x05)

        self.write_cmd(0xB2)
        self.write_data(0x0C)
        self.write_data(0x0C)
        self.write_data(0x00)
        self.write_data(0x33)
        self.write_data(0x33)

        self.write_cmd(0xB7)
        self.write_data(0x35) 

        self.write_cmd(0xBB)
        self.write_data(0x19)

        self.write_cmd(0xC0)
        self.write_data(0x2C)

        self.write_cmd(0xC2)
        self.write_data(0x01)

        self.write_cmd(0xC3)
        self.write_data(0x12)   

        self.write_cmd(0xC4)
        self.write_data(0x20)

        self.write_cmd(0xC6)
        self.write_data(0x0F) 

        self.write_cmd(0xD0)
        self.write_data(0xA4)
        self.write_data(0xA1)

        self.write_cmd(0xE0)
        self.write_data(0xD0)
        self.write_data(0x04)
        self.write_data(0x0D)
        self.write_data(0x11)
        self.write_data(0x13)
        self.write_data(0x2B)
        self.write_data(0x3F)
        self.write_data(0x54)
        self.write_data(0x4C)
        self.write_data(0x18)
        self.write_data(0x0D)
        self.write_data(0x0B)
        self.write_data(0x1F)
        self.write_data(0x23)

        self.write_cmd(0xE1)
        self.write_data(0xD0)
        self.write_data(0x04)
        self.write_data(0x0C)
        self.write_data(0x11)
        self.write_data(0x13)
        self.write_data(0x2C)
        self.write_data(0x3F)
        self.write_data(0x44)
        self.write_data(0x51)
        self.write_data(0x2F)
        self.write_data(0x1F)
        self.write_data(0x1F)
        self.write_data(0x20)
        self.write_data(0x23)
        
        self.write_cmd(0x21)

        self.write_cmd(0x11)

        self.write_cmd(0x29)

    def show(self):
        self.write_cmd(0x2A)
        self.write_data(0x00)
        self.write_data(0x00)
        self.write_data(0x00)
        self.write_data(0xef)
        
        self.write_cmd(0x2B)
        self.write_data(0x00)
        self.write_data(0x00)
        self.write_data(0x00)
        self.write_data(0xEF)
        
        self.write_cmd(0x2C)
        
        self.cs(1)
        self.dc(1)
        self.cs(0)
        self.spi.write(self.buffer)
        self.cs(1)
        
  
if __name__=='__main__':
    pwm = PWM(Pin(BL))
    pwm.freq(1000)
    pwm.duty_u16(32768)#max 65535

    LCD = LCD_1inch3()
    #color BRG
    LCD.fill(LCD.white)
    LCD.show()
    

    keyA = Pin(15,Pin.IN,Pin.PULL_UP)
    keyB = Pin(17,Pin.IN,Pin.PULL_UP)
    keyX = Pin(19 ,Pin.IN,Pin.PULL_UP)
    keyY= Pin(21 ,Pin.IN,Pin.PULL_UP)
    
    up = Pin(2,Pin.IN,Pin.PULL_UP)
    dowm = Pin(18,Pin.IN,Pin.PULL_UP)
    left = Pin(16,Pin.IN,Pin.PULL_UP)
    right = Pin(20,Pin.IN,Pin.PULL_UP)
    ctrl = Pin(3,Pin.IN,Pin.PULL_UP)
    
    while(1):
        if keyA.value() == 0:
            LCD.fill_rect(208,15,30,30,LCD.red)
            print("A")
        else :
            LCD.fill_rect(208,15,30,30,LCD.white)
            LCD.rect(208,15,30,30,LCD.red)
            
            
        if(keyB.value() == 0):
            LCD.fill_rect(208,75,30,30,LCD.red)
            print("B")
        else :
            LCD.fill_rect(208,75,30,30,LCD.white)
            LCD.rect(208,75,30,30,LCD.red)
            
            
        if(keyX.value() == 0):
            LCD.fill_rect(208,135,30,30,LCD.red)
            print("C")
        else :
            LCD.fill_rect(208,135,30,30,LCD.white)
            LCD.rect(208,135,30,30,LCD.red)
            
        if(keyY.value() == 0):
            LCD.fill_rect(208,195,30,30,LCD.red)
            print("D")
        else :
            LCD.fill_rect(208,195,30,30,LCD.white)
            LCD.rect(208,195,30,30,LCD.red)
            
        if(up.value() == 0):
            LCD.fill_rect(60,60,30,30,LCD.red)
            print("UP")
        else :
            LCD.fill_rect(60,60,30,30,LCD.white)
            LCD.rect(60,60,30,30,LCD.red)
            
        if(dowm.value() == 0):
            LCD.fill_rect(60,150,30,30,LCD.red)
            print("DOWM")
        else :
            LCD.fill_rect(60,150,30,30,LCD.white)
            LCD.rect(60,150,30,30,LCD.red)
            
        if(left.value() == 0):
            LCD.fill_rect(15,105,30,30,LCD.red)
            print("LEFT")
        else :
            LCD.fill_rect(15,105,30,30,LCD.white)
            LCD.rect(15,105,30,30,LCD.red)
        
        if(right.value() == 0):
            LCD.fill_rect(105,105,30,30,LCD.red)
            print("RIGHT")
        else :
            LCD.fill_rect(105,105,30,30,LCD.white)
            LCD.rect(105,105,30,30,LCD.red)
        
        if(ctrl.value() == 0):
            LCD.fill_rect(60,105,30,30,LCD.red)
            print("CTRL")
        else :
            LCD.fill_rect(60,105,30,30,LCD.white)
            LCD.rect(60,105,30,30,LCD.red)
                       
        LCD.show()
    time.sleep(1)
    LCD.fill(0xFFFF)




