-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
46 lines (35 loc) · 1.19 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from microbit import *
granularity = [20, 100, 500]
def position(reading):
''' position, calculate the position on the display '''
res = 0
modification = 1
if reading < 0:
reading = abs(reading)
modification = -1
for pos, value in enumerate(granularity):
res = 2 + (pos * modification)
if min(value, reading) == reading:
break
return res
def replace_char_position(string, position, char):
''' replace_char_position, replace a certain position of string '''
res = string[0:position] + char + string[position+1:]
return res
def draw(x, y):
''' draw, return a image to print in the display '''
res = ['0' * 5]*5
if y-1 > -1:
res[y-1] = replace_char_position(res[y-1], x, '7')
if y+1 < 5:
res[y+1] = replace_char_position(res[y+1], x, '7')
if x-1 > -1:
res[y] = replace_char_position(res[y], x-1, '7')
if x+1 < 5:
res[y] = replace_char_position(res[y], x+1, '7')
res[y] = replace_char_position(res[y], x, '9')
return Image(''+':'.join(res))
while True:
x = position(accelerometer.get_x())
y = position(accelerometer.get_y())
display.show(draw(x, y))