Add some example scripts for pyboard (some can run on PC).
This commit is contained in:
parent
7b21c2d8f0
commit
fd04bb3bac
|
@ -0,0 +1,14 @@
|
|||
# log the accelerometer values to a file, 1 per second
|
||||
|
||||
f = open('motion.dat', 'w') # open the file for writing
|
||||
|
||||
for i in range(60): # loop 60 times
|
||||
time = pyb.time() # get the current time
|
||||
accel = pyb.accel() # get the accelerometer data
|
||||
|
||||
# write time and x,y,z values to the file
|
||||
f.write('{} {} {} {}\n'.format(time, accel[0], accel[1], accel[2]))
|
||||
|
||||
pyb.delay(1000) # wait 1000 ms = 1 second
|
||||
|
||||
f.close() # close the file
|
|
@ -0,0 +1,43 @@
|
|||
# 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.reset(x, y) # not enough, or too many neighbours: cell dies
|
||||
elif not self and num_neighbours == 3:
|
||||
lcd.set(x, y) # exactly 3 neigbours around an empty cell: cell is born
|
||||
|
||||
# randomise the start
|
||||
def conway_rand():
|
||||
lcd.clear() # clear the LCD
|
||||
for x in range(128): # loop over x coordinates
|
||||
for y in range(32): # loop over y coordinates
|
||||
if pyb.rand() & 1: # get a 1-bit random number
|
||||
lcd.set(x, y) # 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
|
||||
|
||||
# PC testing
|
||||
import lcd
|
||||
import pyb
|
||||
lcd = lcd.LCD(128, 32)
|
||||
conway_rand()
|
||||
conway_go(100)
|
|
@ -0,0 +1,36 @@
|
|||
# LCD testing object for PC
|
||||
# uses double buffering
|
||||
class LCD:
|
||||
def __init__(self, width, height):
|
||||
self.width = width
|
||||
self.height = height
|
||||
self.buf1 = [[0 for x in range(self.width)] for y in range(self.height)]
|
||||
self.buf2 = [[0 for x in range(self.width)] for y in range(self.height)]
|
||||
|
||||
def clear(self):
|
||||
for y in range(self.height):
|
||||
for x in range(self.width):
|
||||
self.buf1[y][x] = self.buf2[y][x] = 0
|
||||
|
||||
def show(self):
|
||||
print('') # blank line to separate frames
|
||||
for y in range(self.height):
|
||||
for x in range(self.width):
|
||||
self.buf1[y][x] = self.buf2[y][x]
|
||||
for y in range(self.height):
|
||||
row = ''.join(['*' if self.buf1[y][x] else ' ' for x in range(self.width)])
|
||||
print(row)
|
||||
|
||||
def get(self, x, y):
|
||||
if 0 <= x < self.width and 0 <= y < self.height:
|
||||
return self.buf1[y][x]
|
||||
else:
|
||||
return 0
|
||||
|
||||
def reset(self, x, y):
|
||||
if 0 <= x < self.width and 0 <= y < self.height:
|
||||
self.buf2[y][x] = 0
|
||||
|
||||
def set(self, x, y):
|
||||
if 0 <= x < self.width and 0 <= y < self.height:
|
||||
self.buf2[y][x] = 1
|
|
@ -0,0 +1,22 @@
|
|||
def led_angle(seconds_to_run_for):
|
||||
# make LED objects
|
||||
l1 = pyb.Led(1)
|
||||
l2 = pyb.Led(2)
|
||||
|
||||
for i in range(20 * seconds_to_run_for):
|
||||
# get x-axis
|
||||
accel = pyb.accel()[0]
|
||||
|
||||
# turn on LEDs depending on angle
|
||||
if accel < -10:
|
||||
l1.on()
|
||||
l2.off()
|
||||
elif accel > 10:
|
||||
l1.off()
|
||||
l2.on()
|
||||
else:
|
||||
l1.off()
|
||||
l2.off()
|
||||
|
||||
# delay so that loop runs at at 1/50ms = 20Hz
|
||||
pyb.delay(50)
|
|
@ -1,5 +1,7 @@
|
|||
@micropython.native
|
||||
def in_set(c):
|
||||
def mandelbrot():
|
||||
# returns True if c, complex, is in the Mandelbrot set
|
||||
@micropython.native
|
||||
def in_set(c):
|
||||
z = 0
|
||||
for i in range(40):
|
||||
z = z*z + c
|
||||
|
@ -7,8 +9,14 @@ def in_set(c):
|
|||
return False
|
||||
return True
|
||||
|
||||
for v in range(31):
|
||||
line = []
|
||||
lcd.clear()
|
||||
for u in range(91):
|
||||
line.append('*' if in_set((u / 30 - 2) + (v / 15 - 1) * 1j) else ' ')
|
||||
print(''.join(line))
|
||||
for v in range(31):
|
||||
if in_set((u / 30 - 2) + (v / 15 - 1) * 1j):
|
||||
lcd.set(u, v)
|
||||
lcd.show()
|
||||
|
||||
# PC testing
|
||||
import lcd
|
||||
lcd = lcd.LCD(128, 32)
|
||||
mandelbrot()
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
# pyboard testing functions for PC
|
||||
|
||||
def delay(n):
|
||||
pass
|
||||
|
||||
rand_seed = 1
|
||||
def rand():
|
||||
global rand_seed
|
||||
# for these choice of numbers, see P L'Ecuyer, "Tables of linear congruential generators of different sizes and good lattice structure"
|
||||
rand_seed = (rand_seed * 653276) % 8388593
|
||||
return rand_seed
|
Loading…
Reference in New Issue