LCD PySkin

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
mnfisher
Posts: 10
Joined: Fri Jun 20, 2014 7:30 pm

LCD PySkin

Post by mnfisher » Fri Jun 20, 2014 7:38 pm

Just received my micropython board and LCD pyskin - board up and running nicely (& an interesting bag of bits to play with too)
So turned my attention to the LCD (first sample code I found was for Mandelbrot) - soldered some header pins and plugged in.
How to get the LCD to work -

Code: Select all

lcd = pyb.LCD("what here")
But what for the argument - the LCD bus name. Have tried all the candidates I can think of (I2C etc) - but no joy. The sample code on github all seems to have

Code: Select all

import lcd
lcd.clear()
But this doesn't work (no module named lcd)

All suggestions gratefully received!

mnfisher
Posts: 10
Joined: Fri Jun 20, 2014 7:30 pm

Re: LCD PySkin

Post by mnfisher » Sat Jun 21, 2014 6:34 am

After a while of playing:

Code: Select all

lcd = pyb.LCD("X")
or

Code: Select all

lcd = pyb.LCD("Y")
Is the required code - depending which end you've plugged the lcd skin into.

Then using the terminal:
lcd.light(1) - and the backlight was on - and in business.
With a few modifications I got Conway's Life to work
>>> help (pyb.LCD)
object <class 'LCD'> is of type type
command -- <function>
contrast -- <function>
light -- <function>
write -- <function>
fill -- <function>
get -- <function>
pixel -- <function>
text -- <function>
show -- <function>
I tried a few commands:

Code: Select all

lcd = pyb.LCD("X")   # Create an LCD object
lcd.light(1)             # Turn on backlight (0 off)
lcd.fill(0)                #  clear display (1 fills to black)
lcd.pixel(x,y,1)       # Set a pixel on (0 off)
lcd.text("Hello", x, y, n) # String at x,y - n 1 to write 0 to clear.
lcd.show()              # Update display after drawing to it....
lcd.get(x,y)            # read pixel at x,y (as displayed - so don't forget lcd.show() first if relies on current display
lcd.contrast(n)       # Adjust contrast - haven't really needed this but display may vary
All worked - just remember to use show to update!
The interactive nature of the board is fantastic - just type to experiment!
Last edited by mnfisher on Thu Jun 26, 2014 6:22 am, edited 3 times in total.

mnfisher
Posts: 10
Joined: Fri Jun 20, 2014 7:30 pm

Re: LCD PySkin

Post by mnfisher » Sat Jun 21, 2014 7:16 am

Code for Life: Very little changed from the github source - just the lcd initialisation and the random number selection

Code: Select all

#import essential libraries 
import pyb 
# do 1 iteration of Conway's Game of Life 

def conway_step(): 
	for x in range(128): # loop over x coordinates 
		for y in range(32): # loop over y coordinates 
		# count number of neigbours 
			num_neighbours = (lcd.get(x - 1, y - 1) + 
				lcd.get(x, y - 1) + 
				lcd.get(x + 1, y - 1) + 
				lcd.get(x - 1, y) + 
				lcd.get(x + 1, y) + 
				lcd.get(x + 1, y + 1) + 
				lcd.get(x, y + 1) + 
				lcd.get(x - 1, y + 1)) 
			# check if the centre cell is alive or not 
			self = lcd.get(x, y) 
			# apply the rules of life 
			if self and not (2 <= num_neighbours <= 3): 
				lcd.pixel(x, y,0) # not enough, or too many neighbours: cell dies 
			elif not self and num_neighbours == 3: 
				lcd.pixel(x, y,1) # exactly 3 neigbours around an empty cell: cell is born 
# randomise the start 
def conway_rand():
	lcd.fill(0) # clear the LCD 
	for x in range(128): # loop over x coordinates 
		for y in range(32): # loop over y coordinates
			if  pyb.rng() & 1: # get a 1-bit random number 
				lcd.pixel(x, y,1) # set the pixel randomly 
# loop for a certain number of frames, doing iterations of Conway's Game of Life 
def conway_go(num_frames): 
	for i in range(num_frames): 
		conway_step() # do 1 iteration 
		lcd.show() # update the LCD 
		pyb.delay(300) 
# PC testing 
lcd = pyb.LCD("X")
lcd.light(1)
conway_rand() 
conway_go(1000)

mnfisher
Posts: 10
Joined: Fri Jun 20, 2014 7:30 pm

Re: LCD PySkin

Post by mnfisher » Sun Jun 22, 2014 8:54 am

A tutorial has just appeared for this:

http://micropython.org/doc/tut-lcd-skin

Details of how to read the buttons too!

More good stuff!

Post Reply