Skip to content

Instantly share code, notes, and snippets.

@ufian
Last active July 22, 2016 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ufian/3d4d09c0e2c67a021370614252787b88 to your computer and use it in GitHub Desktop.
Save ufian/3d4d09c0e2c67a021370614252787b88 to your computer and use it in GitHub Desktop.
from microbit import *
import radio
last_x = 2
last_y = 2
matrix = [0 for i in range(25)]
def chunks(l, n):
n = max(1, n)
return [l[i:i + n] for i in range(0, len(l), n)]
def get_index(raw_value, last_index):
index = int(round((raw_value / 100) + 2, 0))
return min(max(index, max(last_index - 1, 0)), min(last_index + 1, 4))
radio.on()
last_data = bytes([0 for i in range(25)])
mlimit = lambda x: 9 if x >= 9 else x
while True:
for num in range(2):
x = accelerometer.get_x()
y = accelerometer.get_y()
z = accelerometer.get_z()
last_x = x = get_index(x, last_x)
last_y = y = get_index(y, last_y)
if not num:
for i in range(5):
for j in range(5):
value = matrix[j*5 + i]
if value > 0:
matrix[j*5 + i] = value - 1
#display.clear()
value = matrix[y*5 + x]
value = 9#min(value + 1, 9)
matrix[y*5 + x] = value
radio.send_bytes(bytes(matrix))
data = radio.receive_bytes()
if data and len(data) == 25:
last_data = data
new_matrix = [mlimit(m + int(b)) for m,b in zip(matrix, last_data)]
matrix_str = ":".join(["".join([str(cell) for cell in row]) for row in chunks(new_matrix, 5)])
display.show(Image(matrix_str))
#sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment