LCD skin Graphic-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 skin Graphic-Demo

Post by polygontwist » Sun Jun 29, 2014 10:35 pm

Hello,
i have adapted the code from "Adafruit-GFX-Library-master" for graphic lcd.
origin: https://github.com/adafruit/Adafruit-GFX-Library

Code: Select all

lcd = pyb.LCD('X')	#128*32
lcd.light(1)

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)



def drawLine(x0,y0,x1,y1,thelcd,fill):
	steep= abs(y1 - y0) > abs(x1 - x0);
	if steep:
		x0, y0 = y0, x0 
		x1, y1 = y1, x1 
	if x0>x1:
		x0, x1 = x1, x0
		y0, y1 = y1, y0
	dx=x1-x0
	dy=abs(y1-y0)
	err=dx/2
	ystep=-1
	if y0 < y1: ystep = 1
	for xx in range(x0, x1):
		if steep:
			thelcd.pixel(y0,xx,fill)
		else:
			thelcd.pixel(xx,y0,fill)
		err-=dy
		if err<0:
			y0+=ystep
			err+=dx


def drawRect(x,y,w,h,thelcd,fill):
	drawLine(x,y,x+w,y,thelcd,fill)
	drawLine(x+w-1,y,x+w-1,y+h,thelcd,fill)
	drawLine(x+w,y+h-1,x,y+h-1,thelcd,fill)
	drawLine(x,y+h,x,y,thelcd,fill)

def drawCircle(x0,y0,r,thelcd,fill):
	f=1-r
	ddF_x = 1
	ddF_y = -2 * r
	x = 0
	y = r
	thelcd.pixel(x0  , y0+r,fill)
	thelcd.pixel(x0  , y0-r,fill)
	thelcd.pixel(x0+r, y0  ,fill)
	thelcd.pixel(x0-r, y0  ,fill)
	while x<y:
		if f>=0:
			y-=1
			ddF_y+=2
			f+=ddF_y
		x+=1
		ddF_x+=2
		f+=ddF_x
		thelcd.pixel(x0+x, y0+y, fill);
		thelcd.pixel(x0-x, y0+y, fill);
		thelcd.pixel(x0+x, y0-y, fill);
		thelcd.pixel(x0-x, y0-y, fill);
		thelcd.pixel(x0+y, y0+x, fill);
		thelcd.pixel(x0-y, y0+x, fill);
		thelcd.pixel(x0+y, y0-x, fill);
		thelcd.pixel(x0-y, y0-x, fill);

def drawfillCircle(x0,y0,r,thelcd,fill):
	drawLine(x0, y0-r, x0,y0-r+2*r+1,thelcd, fill)
	f   = 1 - r
	ddF_x = 1
	ddF_y = -2 * r
	x = 0
	y = r
	while x<y:
		if f>=0:
			y-=1
			ddF_y+=2
			f+=ddF_y
		x+=1
		ddF_x+=2
		f+=ddF_x
		drawLine(x0+x, y0-y, x0+x, y0-y+2*y+1,thelcd, fill);
		drawLine(x0+y, y0-x, x0+y, y0-x+2*x+1,thelcd, fill);
		drawLine(x0-x, y0-y, x0-x, y0-y+2*y+1,thelcd, fill);
		drawLine(x0-y, y0-x, x0-y, y0-x+2*x+1,thelcd, fill);
    


lcd.fill(0)

drawRect(0,0,10,16,lcd,1)
drawFillRect(15,0,10,16,lcd,1)

drawLine(0,31,30,16, lcd,1)

drawfillCircle(64,16,10,lcd,1)
drawCircle(128-6,32-6, 5,lcd,1)

lcd.show()
lcd_grafik-demo.jpg
LCD skin Graphic-Demo
lcd_grafik-demo.jpg (190.82 KiB) Viewed 11225 times

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

lcd_gfx library

Post by polygontwist » Thu Jul 03, 2014 9:08 pm

the gfx-graphics functions as a library:
  • drawLine
  • drawTrie
  • drawFillTrie
  • drawRect
  • drawFillRect
  • drawCircle
  • drawfillCircle
file "lcd_gfx.py":

Code: Select all

#adaptiert von Adafruit_GFX.cpp
#for LCD and touch-sensor skin


def drawLine(x0,y0,x1,y1,thelcd,fill):
	steep= abs(y1 - y0) > abs(x1 - x0);
	if steep:
		x0, y0 = y0, x0 
		x1, y1 = y1, x1 
	if x0>x1:
		x0, x1 = x1, x0
		y0, y1 = y1, y0
	dx=x1-x0
	dy=abs(y1-y0)
	err=dx/2
	ystep=-1
	if y0 < y1: ystep = 1
	for xx in range(x0, x1):
		if steep:
			thelcd.pixel(y0,xx,fill)
		else:
			thelcd.pixel(xx,y0,fill)
		err-=dy
		if err<0:
			y0+=ystep
			err+=dx

def drawTrie(x0,y0,x1,y1,x2,y2,thelcd,fill):
	drawLine(x0,y0,x1,y1,thelcd,fill)
	drawLine(x2,y2,x1,y1,thelcd,fill)
	drawLine(x2,y2,x0,y0,thelcd,fill)

def drawFillTrie(x0,y0,x1,y1,x2,y2,thelcd,fill):
	if y0 > y1:
		y0, y1=y1, y0
		x0, x1=x1, x0

	if y1 > y2: 
		y2, y1=y1,y2
		x2, x1=x1,x2

	if y0 > y1:
		y0, y1=y1,y0
		x0, x1=x1,x0
	
	if y0 == y2:
		a = x0
		b = x0
		if x1 < a: 
			a = x1
		else:
			if x1 > b: 
				b = x1
		if x2 < a:
			a = x2
		else:
			if x2 > b:
				b = x2
		drawLine(a, y0,b+1,y0,thelcd,fill)
		return
	
	dx01 = x1 - x0
	dy01 = y1 - y0
	dx02 = x2 - x0
	dy02 = y2 - y0
	dx12 = x2 - x1
	dy12 = y2 - y1
	sa   = 0
	sb   = 0

	if y1 == y2:
		last = y1
	else:
		last = y1-1

	y=y0
	
	for y in range(y0, last+1): 
		a= x0 + sa / dy01 
		b= x0 + sb / dy02 
		sa += dx01
		sb += dx02
		if a > b:
			a,b=b,a
		drawLine(int(a), y,int(b+1),y,thelcd,fill)
	
	sa = dx12 * (y - y1)
	sb = dx02 * (y - y0)
	
	for y in range(last+1, y2+1): 
		a   = x1 + sa / dy12
		b   = x0 + sb / dy02
		sa += dx12
		sb += dx02
		if a > b:
			a,b=b,a
		drawLine(int(a), y,int(b+1),y,thelcd,fill)


def drawRect(x,y,w,h,thelcd,fill):
	drawLine(x,y,x+w,y,thelcd,fill)
	drawLine(x+w-1,y,x+w-1,y+h,thelcd,fill)
	drawLine(x+w,y+h-1,x,y+h-1,thelcd,fill)
	drawLine(x,y+h,x,y,thelcd,fill)

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

def drawCircle(x0,y0,r,thelcd,fill):
	f=1-r
	ddF_x = 1
	ddF_y = -2 * r
	x = 0
	y = r
	thelcd.pixel(x0  , y0+r,fill)
	thelcd.pixel(x0  , y0-r,fill)
	thelcd.pixel(x0+r, y0  ,fill)
	thelcd.pixel(x0-r, y0  ,fill)
	while x<y:
		if f>=0:
			y-=1
			ddF_y+=2
			f+=ddF_y
		x+=1
		ddF_x+=2
		f+=ddF_x
		thelcd.pixel(x0+x, y0+y, fill);
		thelcd.pixel(x0-x, y0+y, fill);
		thelcd.pixel(x0+x, y0-y, fill);
		thelcd.pixel(x0-x, y0-y, fill);
		thelcd.pixel(x0+y, y0+x, fill);
		thelcd.pixel(x0-y, y0+x, fill);
		thelcd.pixel(x0+y, y0-x, fill);
		thelcd.pixel(x0-y, y0-x, fill);

def drawfillCircle(x0,y0,r,thelcd,fill):
	drawLine(x0, y0-r, x0,y0-r+2*r+1,thelcd, fill)
	f   = 1 - r
	ddF_x = 1
	ddF_y = -2 * r
	x = 0
	y = r
	while x<y:
		if f>=0:
			y-=1
			ddF_y+=2
			f+=ddF_y
		x+=1
		ddF_x+=2
		f+=ddF_x
		drawLine(x0+x, y0-y, x0+x, y0-y+2*y+1,thelcd, fill);
		drawLine(x0+y, y0-x, x0+y, y0-x+2*x+1,thelcd, fill);
		drawLine(x0-x, y0-y, x0-x, y0-y+2*y+1,thelcd, fill);
		drawLine(x0-y, y0-x, x0-y, y0-x+2*x+1,thelcd, fill);
    
Demo for file "main.py":

Code: Select all

import lcd_gfx

lcd = pyb.LCD('X')	#128*32
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)


while not switch():
	intensity=intensity+counter
	if intensity>15: counter=-1
	if intensity<1: counter=1

	lcd.fill(0)
	
	x0=0 +int(intensity/3)
	y0=0
	x1=25
	y1=5+int(intensity/2)
	x2=5+intensity*2
	y2=25

	lcd_gfx.drawFillTrie(x0,y0, x1,y1,  x2,y2, lcd,1 )

	x0+=50
	x1+=50
	x2+=50

	lcd_gfx.drawTrie(x0,y0, x1,y1,  x2,y2, lcd,1)


	lcd.show()

	myled.intensity(intensity)
	pyb.delay(12)
Video:
http://instagram.com/p/qAM4RUTftc

have fun.

User avatar
Markus Gritsch
Posts: 41
Joined: Fri May 16, 2014 9:04 pm

Re: LCD skin Graphic-Demo

Post by Markus Gritsch » Fri Jul 04, 2014 11:12 am

In the video there seems to be an irregular refresh rate. Maybe you can try adding

Code: Select all

gc.collect()
in the loop somewhere. It solved my issue with random garbage collector related delays (http://forum.micropython.org/viewtopic. ... t=108#p580)

Cheers,
Markus

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

Re: LCD skin Graphic-Demo

Post by polygontwist » Fri Jul 04, 2014 6:15 pm

thank you, its works better!

nande
Posts: 13
Joined: Fri Oct 10, 2014 6:11 pm
Location: argentina
Contact:

Re: LCD skin Graphic-Demo

Post by nande » Mon Nov 03, 2014 9:29 pm

Awesome thanks!
~namida de ashita ga mienai~

Post Reply