code.py
1 # SPDX-FileCopyrightText: 2020 Carter Nelson for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 import time 6 import board 7 import adafruit_lsm6ds.lsm6ds33 8 from adafruit_ht16k33 import matrix 9 import matrixsand 10 11 DELAY = 0.05 # overall update rate 12 13 # the accelo 14 accelo = adafruit_lsm6ds.lsm6ds33.LSM6DS33(board.I2C()) 15 16 # the matrices 17 m1 = matrix.Matrix8x8(board.I2C(), 0x70) 18 m2 = matrix.Matrix8x8(board.I2C(), 0x71) 19 20 # the sand 21 sand1 = matrixsand.MatrixSand(8, 8) 22 sand2 = matrixsand.MatrixSand(8, 8) 23 24 # simple helper 25 def update_matrix(m, s): 26 for x in range(8): 27 for y in range(8): 28 m[x,y] = s[x,y] 29 30 # fill up some sand 31 for sx in range(8): 32 for sy in range(8): 33 sand1[sx, sy] = True 34 sand1[0,0] = sand1[0,1] = sand1[1,0] = False 35 sand1[0,2] = sand1[1,1] = sand1[2,0] = False 36 37 update_matrix(m1, sand1) 38 update_matrix(m2, sand2) 39 40 updated1 = updated2 = False 41 42 while True: 43 # read accelo 44 ax, ay, az = accelo.acceleration 45 # rotate coords 46 xx = -ax - ay 47 yy = -ax + ay 48 zz = az 49 50 # move grain of sand from upper to lower? 51 if yy > 0 and sand1[7,7] and not sand2[0,0] and not updated2: 52 sand1[7,7] = False 53 sand2[0,0] = True 54 updated1 = updated2 = True 55 # move grain of sand from lower to upper? 56 elif yy <= 0 and sand2[0,0] and not sand1[7,7] and not updated1: 57 sand2[0,0] = False 58 sand1[7,7] = True 59 updated1 = updated2 = True 60 # nope, just a regular update 61 else: 62 updated1 = sand1.iterate((xx, yy, zz)) 63 updated2 = sand2.iterate((xx, yy, zz)) 64 65 # update matrices if needed 66 if updated1: 67 update_matrix(m1, sand1) 68 if updated2: 69 update_matrix(m2, sand2) 70 71 time.sleep(DELAY)