Skip to content

Commit fd04bb3

Browse files
committed
Add some example scripts for pyboard (some can run on PC).
1 parent 7b21c2d commit fd04bb3

File tree

6 files changed

+146
-12
lines changed

6 files changed

+146
-12
lines changed

examples/accellog.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# log the accelerometer values to a file, 1 per second
2+
3+
f = open('motion.dat', 'w') # open the file for writing
4+
5+
for i in range(60): # loop 60 times
6+
time = pyb.time() # get the current time
7+
accel = pyb.accel() # get the accelerometer data
8+
9+
# write time and x,y,z values to the file
10+
f.write('{} {} {} {}\n'.format(time, accel[0], accel[1], accel[2]))
11+
12+
pyb.delay(1000) # wait 1000 ms = 1 second
13+
14+
f.close() # close the file

examples/conwaylife.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# do 1 iteration of Conway's Game of Life
2+
def conway_step():
3+
for x in range(128): # loop over x coordinates
4+
for y in range(32): # loop over y coordinates
5+
# count number of neigbours
6+
num_neighbours = (lcd.get(x - 1, y - 1) +
7+
lcd.get(x, y - 1) +
8+
lcd.get(x + 1, y - 1) +
9+
lcd.get(x - 1, y) +
10+
lcd.get(x + 1, y) +
11+
lcd.get(x + 1, y + 1) +
12+
lcd.get(x, y + 1) +
13+
lcd.get(x - 1, y + 1))
14+
15+
# check if the centre cell is alive or not
16+
self = lcd.get(x, y)
17+
18+
# apply the rules of life
19+
if self and not (2 <= num_neighbours <= 3):
20+
lcd.reset(x, y) # not enough, or too many neighbours: cell dies
21+
elif not self and num_neighbours == 3:
22+
lcd.set(x, y) # exactly 3 neigbours around an empty cell: cell is born
23+
24+
# randomise the start
25+
def conway_rand():
26+
lcd.clear() # clear the LCD
27+
for x in range(128): # loop over x coordinates
28+
for y in range(32): # loop over y coordinates
29+
if pyb.rand() & 1: # get a 1-bit random number
30+
lcd.set(x, y) # set the pixel randomly
31+
32+
# loop for a certain number of frames, doing iterations of Conway's Game of Life
33+
def conway_go(num_frames):
34+
for i in range(num_frames):
35+
conway_step() # do 1 iteration
36+
lcd.show() # update the LCD
37+
38+
# PC testing
39+
import lcd
40+
import pyb
41+
lcd = lcd.LCD(128, 32)
42+
conway_rand()
43+
conway_go(100)

examples/lcd.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# LCD testing object for PC
2+
# uses double buffering
3+
class LCD:
4+
def __init__(self, width, height):
5+
self.width = width
6+
self.height = height
7+
self.buf1 = [[0 for x in range(self.width)] for y in range(self.height)]
8+
self.buf2 = [[0 for x in range(self.width)] for y in range(self.height)]
9+
10+
def clear(self):
11+
for y in range(self.height):
12+
for x in range(self.width):
13+
self.buf1[y][x] = self.buf2[y][x] = 0
14+
15+
def show(self):
16+
print('') # blank line to separate frames
17+
for y in range(self.height):
18+
for x in range(self.width):
19+
self.buf1[y][x] = self.buf2[y][x]
20+
for y in range(self.height):
21+
row = ''.join(['*' if self.buf1[y][x] else ' ' for x in range(self.width)])
22+
print(row)
23+
24+
def get(self, x, y):
25+
if 0 <= x < self.width and 0 <= y < self.height:
26+
return self.buf1[y][x]
27+
else:
28+
return 0
29+
30+
def reset(self, x, y):
31+
if 0 <= x < self.width and 0 <= y < self.height:
32+
self.buf2[y][x] = 0
33+
34+
def set(self, x, y):
35+
if 0 <= x < self.width and 0 <= y < self.height:
36+
self.buf2[y][x] = 1

examples/ledangle.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def led_angle(seconds_to_run_for):
2+
# make LED objects
3+
l1 = pyb.Led(1)
4+
l2 = pyb.Led(2)
5+
6+
for i in range(20 * seconds_to_run_for):
7+
# get x-axis
8+
accel = pyb.accel()[0]
9+
10+
# turn on LEDs depending on angle
11+
if accel < -10:
12+
l1.on()
13+
l2.off()
14+
elif accel > 10:
15+
l1.off()
16+
l2.on()
17+
else:
18+
l1.off()
19+
l2.off()
20+
21+
# delay so that loop runs at at 1/50ms = 20Hz
22+
pyb.delay(50)

examples/mandel.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
@micropython.native
2-
def in_set(c):
3-
z = 0
4-
for i in range(40):
5-
z = z*z + c
6-
if abs(z) > 60:
7-
return False
8-
return True
1+
def mandelbrot():
2+
# returns True if c, complex, is in the Mandelbrot set
3+
@micropython.native
4+
def in_set(c):
5+
z = 0
6+
for i in range(40):
7+
z = z*z + c
8+
if abs(z) > 60:
9+
return False
10+
return True
911

10-
for v in range(31):
11-
line = []
12+
lcd.clear()
1213
for u in range(91):
13-
line.append('*' if in_set((u / 30 - 2) + (v / 15 - 1) * 1j) else ' ')
14-
print(''.join(line))
14+
for v in range(31):
15+
if in_set((u / 30 - 2) + (v / 15 - 1) * 1j):
16+
lcd.set(u, v)
17+
lcd.show()
18+
19+
# PC testing
20+
import lcd
21+
lcd = lcd.LCD(128, 32)
22+
mandelbrot()

examples/pyb.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# pyboard testing functions for PC
2+
3+
def delay(n):
4+
pass
5+
6+
rand_seed = 1
7+
def rand():
8+
global rand_seed
9+
# for these choice of numbers, see P L'Ecuyer, "Tables of linear congruential generators of different sizes and good lattice structure"
10+
rand_seed = (rand_seed * 653276) % 8388593
11+
return rand_seed

0 commit comments

Comments
 (0)