forked from musabkilic/MicroBike
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
72 lines (61 loc) · 1.86 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
72
import microbit
import time
from pykeyboard import PyKeyboard
#Function for Changing a Key
def changeKeyState(key, value, key_name):
global keyboard_keys
#Change Only Neccessary
if value!=keyboard_keys[key_name]:
if value:
keyboard.press_key(key)
else:
keyboard.release_key(key)
keyboard_keys[key_name] = value
#Specify Keyboard
keyboard = PyKeyboard()
#Set Accelerometer Values
previous_values = microbit.accelerometer.get_values()
#Set Keyboard Keys
keyboard_keys = {"L":False,"R":False,"F":False,"S":False}
#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(keyboard.up_key,y>400,"F")
changeKeyState(keyboard.right_key,x>60,"R")
changeKeyState(keyboard.left_key,x<-60,"L")
changeKeyState(keyboard.shift_key,motion>500,"S")
#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