from time import sleep_ms from machine import Pin, I2C from gt911 import GT911 import LCD import math # Touch Screen Initialization rst_pin = Pin(10, Pin.OUT) irq_pin = Pin(11) sda_pin = Pin(8) scl_pin = Pin(9) touch = GT911( I2C(0, scl=scl_pin, sda=sda_pin, freq=100_000), rst_pin, irq_pin ) touch.init(touch_points=1, refresh_rate=50) # LCD Initialization White = LCD.RGB(150,150,150) Black = LCD.RGB(0,0,0) Grey = LCD.RGB(100, 100, 0) def Place_Button(bx, by, x, y, Label): n = len(Label) Sx = 480//x Sy = 320//y x0 = bx*Sx y0 = by*Sy LCD.Box(x0, y0, min(479, x0 + Sx), min(319, y0 + Sy), White) LCD.Text2(Label, x0+Sx//2-8*n, y0+Sy//2-16, White, Black) def Draw_Keyboard(x,y): Sx = 480//x Sy = 320//y for i in range(0,y+1): LCD.Line(0,i*Sy,479,i*Sy, Grey) for i in range(0,x+1): LCD.Line(i*Sx,0,i*Sx,319, Grey) Place_Button(3, 0, x, y, '1') Place_Button(4, 0, x, y, '2') Place_Button(5, 0, x, y, '3') Place_Button(3, 1, x, y, '4') Place_Button(4, 1, x, y, '5') Place_Button(5, 1, x, y, '6') Place_Button(3, 2, x, y, '7') Place_Button(4, 2, x, y, '8') Place_Button(5, 2, x, y, '9') Place_Button(3, 3, x, y, 'clr') Place_Button(4, 3, x, y, '0') Place_Button(5, 3, x, y, 'ent') def Read_Keypad(x, y): Sx = 480//x Sy = 320//y num_points, points_data = touch.read_points() while(num_points == 0): num_points, points_data = touch.read_points() sleep_ms(30) tx = points_data[0][0] // Sx ty = points_data[0][1] // Sy while(num_points > 0): num_points, points_data = touch.read_points() sleep_ms(30) return(tx, ty) LCD.Init() LCD.Clear(Black) Draw_Keyboard(6, 4) X = Y = Z = T = 0 flag = 1 while(1): [x, y] = Read_Keypad(6,4) print(x, y) if(y==0): if(x==3): X = 10*X + 1 elif(x==4): X = 10*X + 2 elif(x==5): X = 10*X + 3 elif(y==1): if(x==3): X = 10*X + 4 elif(x==4): X = 10*X + 5 elif(x==5): X = 10*X + 6 elif(y==2): if(x==3): X = 10*X + 7 elif(x==4): X = 10*X + 8 elif(x==5): X = 10*X + 9 elif(y==3): if(x==3): X = X // 10 elif(x==4): X = 10*X + 0 elif(x==5): T = Z Z = Y Y = X X = 0 flag = 1 if(flag == 1): LCD.Number2(T, 9, 0, 10, 10, White, Black) LCD.Number2(Z, 9, 0, 10, 50, White, Black) LCD.Number2(Y, 9, 0, 10, 90, White, Black) flag = 0 LCD.Number2(X, 9, 0, 10,130, White, Black) while True: num_points, points_data = touch.read_points() for i in range(num_points): print(f"id: {points_data[i][3]} x: {points_data[i][0]} y: {points_data[i][1]} size: {points_data[i][2]}")