-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRokenbok_Controller.py
123 lines (112 loc) · 3.32 KB
/
Rokenbok_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#Imports
from Rokenbok_Hub import Rokenbok_Hub, Button
################################################################################
class Rokenbok_Controller:
"""
Represents a controller for the rokenbok hub, but for the computer. Maps
key presses to button pushes on the controller.
"""
############################################################################
def __init__(self, player, hub):
"""
PURPOSE: creates a new Rokenbok_Controller
ARGS:
player (int): player number (1-8)
hub (Rokenbok_Hub): the hub this controller belongs to
RETURNS: new instance of a Rokenbok_Controller
NOTES:
"""
#Save player
if player < 1 or player > 8:
raise ValueError("Argument 'player' must be between 1 and 8!")
self.player = player
#Save hub
self.hub = hub
#Define default keymapping
self.key_map = {
24 : Button.FORWARD, #up arrow
25 : Button.BACK, #down arrow
26 : Button.RIGHT, #right arrow
27 : Button.LEFT, #left arrow
ord('s') : Button.A,
ord('w') : Button.B,
ord('a') : Button.X,
ord('d') : Button.Y,
ord('q') : Button.SLOW
}
############################################################################
def get_sel(self):
"""
PURPOSE: gets the current selection of this controller
ARGS: none
RETURNS: (int) current selection (1-8) or 0 if nothing is selected
NOTES:
"""
#TODO change to current selection rather than desired
cur_sel = self.hub.get_sels()[1][self.player - 1]
if cur_sel == 0xFF:
return 0
return cur_sel + 1
############################################################################
def press_key(self, ascii_code):
"""
PURPOSE: reacts to a key press
ARGS:
ascii_code (int): the ascii code of the key pressed
RETURNS: none
NOTES:
"""
if ascii_code in self.key_map:
self.hub.cmd(self.key_map[ascii_code], self.player, True)
elif ascii_code >= 49 or ascii_code <= 56:
#number keys 1 - 8 were pressed
des_sel = ascii_code - 48
self.hub.change_sel(self.player, des_sel)
elif ascii_code == 114:
#r
self.hub.restart_arduino()
elif ascii_code == 48:
#0
self.deselect()
############################################################################
def release_key(self, ascii_code):
"""
PURPOSE: reacts to a key release
ARGS:
ascii_code (int): the ascii code of the key released
RETURNS: none
NOTES:
"""
if ascii_code in self.key_map:
self.hub.cmd(self.key_map[ascii_code], self.player, False)
############################################################################
def release_all(self):
"""
PURPOSE: releases all the buttons
ARGS: none
RETURNS: none
NOTES:
"""
for key, button in self.key_map.items():
self.hub.cmd(button, self.player, False)
############################################################################
def deselect(self):
"""
PURPOSE: deselects from a car
ARGS: none
RETURNS: none
NOTES:
"""
self.hub.change_sel(self.player, 0)
############################################################################
def release_all_and_deselect(self):
"""
PURPOSE: releases all the buttons and deselects from a car
ARGS: none
RETURNS: none
NOTES:
"""
self.release_all()
self.deselect()
############################################################################
################################################################################