-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_pyhaptic.py
executable file
·185 lines (169 loc) · 4.97 KB
/
test_pyhaptic.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import os
import glob
import sys
import time
import serial
import logging
import random
logging.basicConfig(level=logging.DEBUG)
from pyhaptic import HapticInterface
def find_comm_port():
comm_port = []
if os.name == 'posix':
comm_port = glob.glob('/dev/tty.*')
comm_port.extend( glob.glob('/dev/ttyACM*'))
comm_port.extend( glob.glob('/dev/ttyUSB*'))
elif os.name == 'nt':
available = []
for i in range(256):
try:
s = serial.Serial(i)
available.append('COM'+str(i + 1))
s.close()
except serial.SerialException:
pass
comm_port.extend(available)
print "Printing current available comm ports.\n"
for i in comm_port:
print i
comm_choice = raw_input("\nPlease choose the full path to the comm port that the haptic controller is connected to:")
return comm_choice
def function_zero():
print "running zero"
for x in xrange(two_d_display.qry_number_motors()):
two_d_display.vibrate(x,0,0,1)
time.sleep(.1)
print "completed zero"
def function_one():
print "running one"
number = two_d_display.qry_number_motors()
for x in xrange(number):
two_d_display.vibrate(x,0,0,1)
time.sleep(.1)
for x in xrange(number,-1,-1):
two_d_display.vibrate(x,0,0,1)
time.sleep(.1)
print "completed one"
def function_two():
print "running two"
num_motors = two_d_display.qry_number_motors()
for x in xrange(num_motors):
if x % 8 == 0:
two_d_display.vibrate(x,0,0,1)
time.sleep(.5)
for x in xrange(num_motors):
if x % 8 == 1:
two_d_display.vibrate(x,0,0,1)
time.sleep(.5)
for x in xrange(num_motors):
if x % 8 == 2:
two_d_display.vibrate(x,0,0,1)
time.sleep(.5)
for x in xrange(num_motors):
if x % 8 == 3:
two_d_display.vibrate(x,0,0,1)
time.sleep(.5)
for x in xrange(num_motors):
if x % 8 == 4:
two_d_display.vibrate(x,0,0,1)
time.sleep(.5)
for x in xrange(num_motors):
if x % 8 == 5:
two_d_display.vibrate(x,0,0,1)
time.sleep(.5)
for x in xrange(num_motors):
if x % 8 == 6:
two_d_display.vibrate(x,0,0,1)
time.sleep(.5)
for x in xrange(num_motors):
if x % 8 == 7:
two_d_display.vibrate(x,0,0,1)
time.sleep(1)
# And back the other direction.
for x in xrange(num_motors):
if x % 8 == 6:
two_d_display.vibrate(x,0,0,1)
time.sleep(.5)
for x in xrange(num_motors):
if x % 8 == 5:
two_d_display.vibrate(x,0,0,1)
time.sleep(.5)
for x in xrange(num_motors):
if x % 8 == 4:
two_d_display.vibrate(x,0,0,1)
time.sleep(.5)
for x in xrange(num_motors):
if x % 8 == 3:
two_d_display.vibrate(x,0,0,1)
time.sleep(.5)
for x in xrange(num_motors):
if x % 8 == 2:
two_d_display.vibrate(x,0,0,1)
time.sleep(.5)
for x in xrange(num_motors):
if x % 8 == 1:
two_d_display.vibrate(x,0,0,1)
time.sleep(.5)
for x in xrange(num_motors):
if x % 8 == 0:
two_d_display.vibrate(x,0,0,1)
print "completed two"
def function_three():
print "running three"
num_motors = two_d_display.qry_number_motors()
for x in xrange(num_motors):
two_d_display.vibrate(random.randrange(num_motors),0,0,1)
time.sleep(.1)
print "completed three"
def function_four():
print "running four"
print "completed four"
def function_five():
print "running five"
print "completed five"
def function_six():
print "running six"
print "completed six"
def function_seven():
print "running seven"
print "completed seven"
def function_eight():
print "running eight"
print "completed eight"
def function_nine():
print "running nine"
print "completed nine"
if __name__ == '__main__':
two_d_display = HapticInterface(find_comm_port())
try:
two_d_display.connect()
except:
print "Failed to connect on ..."
sys.exit(1)
print "enter a number 0-9 to activate that function"
print "anything else to exit"
while True:
comm_choice = sys.stdin.read(1)
sys.stdin.read(1) #dump the newline char
if comm_choice == "0":
function_zero()
elif comm_choice == "1":
function_one()
elif comm_choice == "2":
function_two()
elif comm_choice == "3":
function_three()
elif comm_choice == "4":
function_four()
elif comm_choice == "5":
function_five()
elif comm_choice == "6":
function_six()
elif comm_choice == "7":
function_seven()
elif comm_choice == "8":
function_eight()
elif comm_choice == "9":
function_nine()
else:
sys.exit(0)