Initial commit

This commit is contained in:
arofarn 2020-04-15 00:14:13 +02:00
commit 5a008b3052
26 changed files with 183 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*~

1
boot_out.txt Normal file
View File

@ -0,0 +1 @@
Adafruit CircuitPython 5.1.0 on 2020-04-02; Adafruit Trellis M4 Express with samd51g19

120
code.py Normal file
View File

@ -0,0 +1,120 @@
import time
import board
import busio
from adafruit_neotrellis.neotrellis import NeoTrellis
from adafruit_neotrellis.multitrellis import MultiTrellis
from neotrellism4 import NeoTrellisM4
import conway
import adafruit_adxl34x
ACCEL_THRESHOLD = 100
#create the i2c object for the trellis
I2C = busio.I2C(board.SCL, board.SDA)
"""create the trellis. This is for a 2x2 array of TrellisM4 (first row) with
2 Neotrellis (second row).
[ NeoM4_left | NeoM4_right ]
neotrellis0 | neotrellis1
"""
trellim4_left = NeoTrellisM4()
trellim4_right = NeoTrellisM4(left_part=trellim4_left)
trelli = [
[trellim4_left, trellim4_right],
[NeoTrellis(I2C, False, addr=0x2F), NeoTrellis(I2C, False, addr=0x2E)]
]
trellis = MultiTrellis(trelli)
#some color definitions
OFF = (0, 0, 0)
ON = (100, 100, 80)
# RED = (127, 0, 0)
# YELLOW = (127, 75, 0)
# GREEN = (0, 127, 0)
# CYAN = (0, 127, 127)
# BLUE = (0, 0, 127)
# PURPLE = (90, 0, 127)
I2C_ACCEL = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
accelerometer = adafruit_adxl34x.ADXL343(I2C_ACCEL)
# Init universe
univers = [[False for x in range(8)] for y in range(8)]
universe = conway.GLIDER
#this will be called when button events are received
def genesis_callback(xcoord, ycoord, edge):
#turn the LED on when a rising edge is detected
if edge == NeoTrellis.EDGE_RISING:
universe[xcoord][ycoord] = not(universe[xcoord][ycoord])
if univers[xcoord][ycoord]:
trellis.color(xcoord, ycoord, ON)
else:
trellis.color(xcoord, ycoord, OFF)
conway.draw_universe(trellis, universe)
def evo_callback(xcoord, ycoord, edge):
pass
# Init. all the keys for Genesis
for x in range(8):
for y in range(8):
#activate rising edge events on all keys
trellis.activate_key(x, y, NeoTrellis.EDGE_RISING)
trellis.set_callback(x, y, genesis_callback)
trellis.sync()
conway.draw_universe(trellis, universe)
accelerometer.enable_motion_detection(threshold=ACCEL_THRESHOLD)
# Genesis
while not(accelerometer.events["motion"]):
#the trellis can only be read every 17 millisecons or so
trellis.sync()
time.sleep(.02)
print("The end of Genesis, time to evolve...")
# Init. all the keys for Evolution
for x in range(8):
for y in range(8):
#activate rising edge events on all keys
trellis.activate_key(x, y, NeoTrellis.EDGE_RISING)
trellis.set_callback(x, y, evo_callback)
trellis.sync()
conway.draw_universe(trellis, universe)
generation = 0
#Evolution
while True:
new_universe = [[False for x in range(8)] for y in range(8)]
ext_universe = conway.extend_universe(universe)
for x in range(8):
for y in range(8):
n = conway.living_neighbour_count(x, y, ext_universe)
if n == 3:
new_universe[x][y] = True
# print(x, y, n, "alive")
elif n == 2 and universe[x][y]:
new_universe[x][y] = True
# print(x, y, n, "still alive")
else:
new_universe[x][y] = False
# print(x, y, n, "dead")
generation += 1
print(generation)
universe = new_universe.copy()
conway.draw_universe(trellis, universe)

BIN
lib/adafruit_adxl34x.mpy Normal file

Binary file not shown.

View File

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

Binary file not shown.

Binary file not shown.

View File

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
lib/conway.mpy Normal file

Binary file not shown.

BIN
lib/neopixel.mpy Normal file

Binary file not shown.

BIN
lib/neotrellism4.mpy Normal file

Binary file not shown.

61
src/conway.py Normal file
View File

@ -0,0 +1,61 @@
GLIDER = [[False, False, False, False, False, False, False, False],
[False, False, False, False, True, False, False, False],
[False, False, True, False, True, False, False, False],
[False, False, False, True, True, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False]]
OFF = (0, 0, 0)
ON = (100, 100, 80)
def draw_universe(trellis, universe):
for x in range(8):
for y in range(8):
#activate rising edge events on all keys
if universe[x][y]:
trellis.color(x, y, ON)
else:
trellis.color(x, y, OFF)
def extend_universe(universe):
"""Extend an 8x8 universe to 10x10 with first and last columns and rows as in an donut-shaped universe"""
ext_universe = [[False for x_ext in range(10)] for y_ext in range(10)]
# Copy universe in an extended donut-shaped universe
for x_ext in range(10):
for y_ext in range(10):
if x_ext == 0:
loc_x = 7
elif x_ext == 9:
loc_x = 0
else:
loc_x = x_ext-1
if y_ext == 0:
loc_y = 7
elif y_ext == 9:
loc_y = 0
else:
loc_y = y_ext-1
ext_universe[x_ext][y_ext] = universe[loc_x][loc_y]
return ext_universe
def living_neighbour_count(xcoord, ycoord, ext_universe):
"""Count living cells around xcoord, ycoord"""
living_neighbours = []
living_neighbours.append(ext_universe[xcoord][ycoord])
living_neighbours.append(ext_universe[xcoord][ycoord+1])
living_neighbours.append(ext_universe[xcoord][ycoord+2])
living_neighbours.append(ext_universe[xcoord+1][ycoord])
living_neighbours.append(ext_universe[xcoord+1][ycoord+2])
living_neighbours.append(ext_universe[xcoord+2][ycoord])
living_neighbours.append(ext_universe[xcoord+2][ycoord+1])
living_neighbours.append(ext_universe[xcoord+2][ycoord+2])
# print(living_neighbours.count(True), living_neighbours)
return living_neighbours.count(True)