LCD Touch Demo

Showroom for MicroPython related hardware projects.
Target audience: Users wanting to show off their project!
Post Reply
User avatar
polygontwist
Posts: 36
Joined: Sat Jun 28, 2014 4:54 pm
Location: Germany, Rostock
Contact:

LCD Touch Demo

Post by polygontwist » Sat Jun 28, 2014 8:12 pm

Demo for LCD, Touch, Switch, test bits of a Integer
1.copy the script in main.py
2.restart (e.g. CTRL+D in putty)
3.Script end when you press a Button (USR), e.g. control of putty

Code: Select all

lcd = pyb.LCD('X')
i2c = pyb.I2C(1, pyb.I2C.MASTER)
i2c.mem_write(4, 90, 0x5e)
switch = pyb.Switch()
lcd.light(1)

def tobin(n):
	re="b"
	if n & 8:
		re+="1"
	else:
		re+="0"
	if n & 4:
		re+="1"
	else:
		re+="0"
	if n & 2:
		re+="1"
	else:
		re+="0"
	if n & 1:
		re+="1"
	else:
		re+="0"
	return str(re)

while not switch():
	touch = i2c.mem_read(1, 90, 0)[0]
	lcd.fill(0)
	lcd.text("touchdemo "+tobin(touch), 0, 0, 1)
	if touch & 8:
		lcd.text("A", 0, 10, 1)
	if touch & 4:
		lcd.text("B", 16, 10, 1)
	if touch & 2:
		lcd.text("X", 0, 20, 1)
	if touch & 1:
		lcd.text("Y", 16, 20, 1)
		
	lcd.show()
	pyb.delay(25)
	
lcd.light(0)	
lcd.write('by\n\n\n ')

User avatar
polygontwist
Posts: 36
Joined: Sat Jun 28, 2014 4:54 pm
Location: Germany, Rostock
Contact:

Re: LCD Touch Demo

Post by polygontwist » Sun Jun 29, 2014 7:40 am

The demo does not work correctly. http://forum.micropython.org/viewtopic.php?f=3&t=190
After a while, all LEDs light up and no longer works.
The problem will be the use of STRING in LCD, following demo works without problems.

Code: Select all

lcd = pyb.LCD('X')
i2c = pyb.I2C(1, pyb.I2C.MASTER)
i2c.mem_write(4, 90, 0x5e)
switch = pyb.Switch()
lcd.light(1)
intensity = 0
counter = 1
myled=pyb.LED(4)
tbytes=["0","0","0","0"]


while not switch():
	touch = i2c.mem_read(1, 90, 0)[0]
	lcd.fill(0)
	if touch & 8:
		lcd.text("A", 0, 10, 1)
		tbytes[0]="1"
	else:
		tbytes[0]="0"
	if touch & 4:
		lcd.text("B", 16, 10, 1)
		tbytes[1]="1"
	else:
		tbytes[1]="0"
	if touch & 2:
		lcd.text("X", 0, 20, 1)
		tbytes[2]="1"
	else:
		tbytes[2]="0"
	if touch & 1:
		lcd.text("Y", 16, 20, 1)
		tbytes[3]="1"
	else:
		tbytes[3]="0"
		
	lcd.text("touchdemo b"+tbytes[0]+tbytes[1]+tbytes[2]+tbytes[3], 0, 0, 1)
	lcd.show()
	
	intensity=intensity+counter
	if intensity>15: counter=-1
	if intensity<1: counter=1
	myled.intensity(intensity)
	pyb.delay(75)
	
lcd.light(0)	
lcd.write('by\n\n\n ')
Video on http://instagram.com/p/p0hAeuzfqS/?modal=true
Attachments
touchdemo.jpg
touchdemo
touchdemo.jpg (56.16 KiB) Viewed 4282 times

User avatar
polygontwist
Posts: 36
Joined: Sat Jun 28, 2014 4:54 pm
Location: Germany, Rostock
Contact:

Re: LCD Touch Demo

Post by polygontwist » Sun Jun 29, 2014 4:17 pm

with drawFillRect-fuction, Accelerator, blue LED and handling Exceptions.
my Exeption is "Exception: HAL_I2C_Mem_Read failed with code 3". With this code continues to operate the program.

Code: Select all

lcd = pyb.LCD('X')
i2c = pyb.I2C(1, pyb.I2C.MASTER)
i2c.mem_write(4, 90, 0x5e)
switch = pyb.Switch()
lcd.light(1)
intensity = 0
counter = 1
myled=pyb.LED(4)
tbytes=["0","0","0","0"]
accel = pyb.Accel()

def drawFillRect(x,y,w,h,thelcd,fill):
	xa=x
	xe=x+w
	ya=y
	ye=y+h
	if xa>xe:
		t=xa
		xa=xe
		xe=t
	if ya>ye:
		t=ya
		ya=ye
		ye=t
	for yy in range(ya, ye):
		for xx in range(xa,xe):
			thelcd.pixel(xx,yy,fill)

touch =0

while not switch():

	try:
		touch = i2c.mem_read(1, 90, 0)[0]
		#Exception: HAL_I2C_Mem_Read failed with code 3
	except:
		touch =0
		print ("Error: i2c.mem_read")
	
	
	lcd.fill(0)
	if touch & 8:
		lcd.text("A", 0, 10, 1)
		tbytes[0]="1"
	else:
		tbytes[0]="0"
	if touch & 4:
		lcd.text("B", 16, 10, 1)
		tbytes[1]="1"
	else:
		tbytes[1]="0"
	if touch & 2:
		lcd.text("X", 0, 20, 1)
		tbytes[2]="1"
	else:
		tbytes[2]="0"
	if touch & 1:
		lcd.text("Y", 16, 20, 1)
		tbytes[3]="1"
	else:
		tbytes[3]="0"
		
	lcd.text("touchdemo b"+tbytes[0]+tbytes[1]+tbytes[2]+tbytes[3], 0, 0, 1)
	
	lcd.text(str(accel.x()), 32, 9, 1)
	lcd.text(str(accel.y()), 32, 17, 1)
	lcd.text(str(accel.z()), 32, 25, 1)
	
	wx=70+accel.x()
	wy=70+accel.y()
	wz=70+accel.z()
	
	drawFillRect(80,10,wx-70,2,lcd,1)
	drawFillRect(80,18,wy-70,2,lcd,1)
	drawFillRect(80,26,wz-70,2,lcd,1)
	
	lcd.show()
	
	intensity=intensity+counter
	if intensity>15: counter=-1
	if intensity<1: counter=1
	myled.intensity(intensity)
	pyb.delay(25)

lcd.fill(0)
lcd.show()
lcd.light(0)
lcd.write('by\n')

domgiles
Posts: 10
Joined: Fri Aug 08, 2014 2:54 pm

Re: LCD Touch Demo

Post by domgiles » Sun Sep 07, 2014 10:04 am

Great little demo... It should be the "Advanced Demo" LCD Skin.

I would delete the previous entries if possible... I missed working entry at the bottom go the page and spend a few minutes cleaning and attempting to figure out if I'd wired the skin up incorrectly.

Post Reply