Skip to content

Commit 0c3955b

Browse files
committed
examples: Update conwaylife to work with new LCD API.
1 parent 21ca2d7 commit 0c3955b

3 files changed

Lines changed: 47 additions & 47 deletions

File tree

examples/conwaylife.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#import essential libraries
2-
import lcd
32
import pyb
43

4+
lcd = pyb.LCD('x')
5+
lcd.light(1)
6+
57
# do 1 iteration of Conway's Game of Life
68
def conway_step():
79
for x in range(128): # loop over x coordinates
@@ -21,26 +23,24 @@ def conway_step():
2123

2224
# apply the rules of life
2325
if self and not (2 <= num_neighbours <= 3):
24-
lcd.reset(x, y) # not enough, or too many neighbours: cell dies
26+
lcd.pixel(x, y, 0) # not enough, or too many neighbours: cell dies
2527
elif not self and num_neighbours == 3:
26-
lcd.set(x, y) # exactly 3 neigbours around an empty cell: cell is born
28+
lcd.pixel(x, y, 1) # exactly 3 neigbours around an empty cell: cell is born
2729

2830
# randomise the start
2931
def conway_rand():
30-
lcd.clear() # clear the LCD
32+
lcd.fill(0) # clear the LCD
3133
for x in range(128): # loop over x coordinates
3234
for y in range(32): # loop over y coordinates
33-
if pyb.rand() & 1: # get a 1-bit random number
34-
lcd.set(x, y) # set the pixel randomly
35+
lcd.pixel(x, y, pyb.rng() & 1) # set the pixel randomly
3536

3637
# loop for a certain number of frames, doing iterations of Conway's Game of Life
3738
def conway_go(num_frames):
3839
for i in range(num_frames):
3940
conway_step() # do 1 iteration
4041
lcd.show() # update the LCD
41-
pyb.delay(300)
42+
pyb.delay(50)
4243

43-
# PC testing
44-
lcd = lcd.LCD(128, 32)
44+
# testing
4545
conway_rand()
46-
conway_go(1000)
46+
conway_go(100)

examples/lcd.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

examples/pyb.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,44 @@ def delay(n):
66
pass
77

88
rand_seed = 1
9-
def rand():
9+
def rng():
1010
global rand_seed
1111
# for these choice of numbers, see P L'Ecuyer, "Tables of linear congruential generators of different sizes and good lattice structure"
1212
rand_seed = (rand_seed * 653276) % 8388593
1313
return rand_seed
14+
15+
# LCD testing object for PC
16+
# uses double buffering
17+
class LCD:
18+
def __init__(self, port):
19+
self.width = 128
20+
self.height = 32
21+
self.buf1 = [[0 for x in range(self.width)] for y in range(self.height)]
22+
self.buf2 = [[0 for x in range(self.width)] for y in range(self.height)]
23+
24+
def light(self, value):
25+
pass
26+
27+
def fill(self, value):
28+
for y in range(self.height):
29+
for x in range(self.width):
30+
self.buf1[y][x] = self.buf2[y][x] = value
31+
32+
def show(self):
33+
print('') # blank line to separate frames
34+
for y in range(self.height):
35+
for x in range(self.width):
36+
self.buf1[y][x] = self.buf2[y][x]
37+
for y in range(self.height):
38+
row = ''.join(['*' if self.buf1[y][x] else ' ' for x in range(self.width)])
39+
print(row)
40+
41+
def get(self, x, y):
42+
if 0 <= x < self.width and 0 <= y < self.height:
43+
return self.buf1[y][x]
44+
else:
45+
return 0
46+
47+
def pixel(self, x, y, value):
48+
if 0 <= x < self.width and 0 <= y < self.height:
49+
self.buf2[y][x] = value

0 commit comments

Comments
 (0)