-
Notifications
You must be signed in to change notification settings - Fork 7
/
controller.py
71 lines (60 loc) · 1.75 KB
/
controller.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import microbit
import time
from pynput.keyboard import Key, Controller
#Function for Changing a Key
def changeKeyState(key, value):
global keyboard_keys
#Change Only Neccessary
if value!=keyboard_keys.get(key, False):
if value:
keyboard.press(key)
else:
keyboard.release(key)
keyboard_keys[key] = value
#Specify Keyboard
keyboard = Controller()
#Set Accelerometer Values
previous_values = microbit.accelerometer.get_values()
keyboard_keys = {}
#Set Images
stable = microbit.Image("00000:00000:99999:00000:00000")
images = {"N": microbit.Image.ARROW_N,
"NE": microbit.Image.ARROW_NE,
"NW": microbit.Image.ARROW_NW,
"E": microbit.Image.ARROW_E,
"W": microbit.Image.ARROW_W,
"": stable}
#Wait for User to Press a Button
while 1:
#Blink
microbit.display.show(microbit.Image.ARROW_W)
time.sleep(0.5)
microbit.display.clear()
#Start the Program if a Button is Pressed
if microbit.button_a.was_pressed() or microbit.button_b.was_pressed():
break
time.sleep(0.5)
#Start the Loop
while 1:
#Get Accelerometer Values
accelerometer_values = microbit.accelerometer.get_values()
x,y,z = accelerometer_values
#Calculate Avarege Motion in X,Y,Z Directions
motion = sum(map(lambda x:abs(accelerometer_values[x]-previous_values[x]),range(3)))/3
#Change Direction
changeKeyState(Key.up, y>400)
changeKeyState(Key.right, x>60)
changeKeyState(Key.left, x<-60)
changeKeyState(Key.shift, motion>500)
#Set Direction to Show
direction = ""
if y>400:
direction += "N"
if x>60:
direction += "E"
elif x<-60:
direction += "W"
#Show the Direction
microbit.display.show(images[direction])
#Set Current Accelerometer Values to Previous
previous_values = accelerometer_values