commit 5a008b30521f012dac8952f0a069f84387015c79 Author: arofarn Date: Wed Apr 15 00:14:13 2020 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b25c15b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ diff --git a/boot_out.txt b/boot_out.txt new file mode 100644 index 0000000..6746b57 --- /dev/null +++ b/boot_out.txt @@ -0,0 +1 @@ +Adafruit CircuitPython 5.1.0 on 2020-04-02; Adafruit Trellis M4 Express with samd51g19 diff --git a/code.py b/code.py new file mode 100644 index 0000000..72c741d --- /dev/null +++ b/code.py @@ -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) diff --git a/lib/adafruit_adxl34x.mpy b/lib/adafruit_adxl34x.mpy new file mode 100644 index 0000000..f15c7c8 Binary files /dev/null and b/lib/adafruit_adxl34x.mpy differ diff --git a/lib/adafruit_bus_device/__init__.py b/lib/adafruit_bus_device/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lib/adafruit_bus_device/i2c_device.mpy b/lib/adafruit_bus_device/i2c_device.mpy new file mode 100644 index 0000000..85c40e1 Binary files /dev/null and b/lib/adafruit_bus_device/i2c_device.mpy differ diff --git a/lib/adafruit_bus_device/spi_device.mpy b/lib/adafruit_bus_device/spi_device.mpy new file mode 100644 index 0000000..4fc28c0 Binary files /dev/null and b/lib/adafruit_bus_device/spi_device.mpy differ diff --git a/lib/adafruit_matrixkeypad.mpy b/lib/adafruit_matrixkeypad.mpy new file mode 100644 index 0000000..81fa24f Binary files /dev/null and b/lib/adafruit_matrixkeypad.mpy differ diff --git a/lib/adafruit_neotrellis/__init__.py b/lib/adafruit_neotrellis/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lib/adafruit_neotrellis/multitrellis.mpy b/lib/adafruit_neotrellis/multitrellis.mpy new file mode 100644 index 0000000..a0b454c Binary files /dev/null and b/lib/adafruit_neotrellis/multitrellis.mpy differ diff --git a/lib/adafruit_neotrellis/neotrellis.mpy b/lib/adafruit_neotrellis/neotrellis.mpy new file mode 100644 index 0000000..51dae50 Binary files /dev/null and b/lib/adafruit_neotrellis/neotrellis.mpy differ diff --git a/lib/adafruit_seesaw/__init__.py b/lib/adafruit_seesaw/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lib/adafruit_seesaw/analoginput.mpy b/lib/adafruit_seesaw/analoginput.mpy new file mode 100644 index 0000000..43be918 Binary files /dev/null and b/lib/adafruit_seesaw/analoginput.mpy differ diff --git a/lib/adafruit_seesaw/crickit.mpy b/lib/adafruit_seesaw/crickit.mpy new file mode 100644 index 0000000..981f631 Binary files /dev/null and b/lib/adafruit_seesaw/crickit.mpy differ diff --git a/lib/adafruit_seesaw/digitalio.mpy b/lib/adafruit_seesaw/digitalio.mpy new file mode 100644 index 0000000..c2c9bd6 Binary files /dev/null and b/lib/adafruit_seesaw/digitalio.mpy differ diff --git a/lib/adafruit_seesaw/keypad.mpy b/lib/adafruit_seesaw/keypad.mpy new file mode 100644 index 0000000..c60c313 Binary files /dev/null and b/lib/adafruit_seesaw/keypad.mpy differ diff --git a/lib/adafruit_seesaw/neopixel.mpy b/lib/adafruit_seesaw/neopixel.mpy new file mode 100644 index 0000000..f7e4595 Binary files /dev/null and b/lib/adafruit_seesaw/neopixel.mpy differ diff --git a/lib/adafruit_seesaw/pwmout.mpy b/lib/adafruit_seesaw/pwmout.mpy new file mode 100644 index 0000000..03e9c6c Binary files /dev/null and b/lib/adafruit_seesaw/pwmout.mpy differ diff --git a/lib/adafruit_seesaw/robohat.mpy b/lib/adafruit_seesaw/robohat.mpy new file mode 100644 index 0000000..59516f8 Binary files /dev/null and b/lib/adafruit_seesaw/robohat.mpy differ diff --git a/lib/adafruit_seesaw/samd09.mpy b/lib/adafruit_seesaw/samd09.mpy new file mode 100644 index 0000000..15995a4 Binary files /dev/null and b/lib/adafruit_seesaw/samd09.mpy differ diff --git a/lib/adafruit_seesaw/seesaw.mpy b/lib/adafruit_seesaw/seesaw.mpy new file mode 100644 index 0000000..7efd3af Binary files /dev/null and b/lib/adafruit_seesaw/seesaw.mpy differ diff --git a/lib/adafruit_seesaw/tftshield18.mpy b/lib/adafruit_seesaw/tftshield18.mpy new file mode 100644 index 0000000..18aaed1 Binary files /dev/null and b/lib/adafruit_seesaw/tftshield18.mpy differ diff --git a/lib/conway.mpy b/lib/conway.mpy new file mode 100644 index 0000000..efd104d Binary files /dev/null and b/lib/conway.mpy differ diff --git a/lib/neopixel.mpy b/lib/neopixel.mpy new file mode 100644 index 0000000..4322984 Binary files /dev/null and b/lib/neopixel.mpy differ diff --git a/lib/neotrellism4.mpy b/lib/neotrellism4.mpy new file mode 100644 index 0000000..b0bc3cd Binary files /dev/null and b/lib/neotrellism4.mpy differ diff --git a/src/conway.py b/src/conway.py new file mode 100644 index 0000000..f00aa1f --- /dev/null +++ b/src/conway.py @@ -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) +