-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhapticviewmain.py
341 lines (312 loc) · 12 KB
/
hapticviewmain.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# Test Button Program
# Checkout Line Example for possible visual rhythm based interface.
# To play with a bunch of kivy widgets live checkout:
# examples/demo/kivycatalog or examples/demo/showcase
# examples/widgets/tabbed_panel_showcase.py
# File Chooser
#
# Interface Ideas:
# Show 8 motors visually. When you press on each one they will vibrate.
# Each motor has a series of pictures: off state, on state
# Show a sequencer visualization with the max measure length you can have. Allow to record rhythm live and save as preset.
# Looks like I want togglebuttons in a group for the changing tabs
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup
from kivy.lang import Builder
from kivy.uix.spinner import Spinner
from kivy.graphics import Color, Rectangle
import serial
import glob
import os
import time
# Populate the text section with the info pulled from a query all statement.
# Find out which mode the atmel starts in - learning or active?
# Substitute "Spinner" with "DropDown" in the future.
# Needs Fixing:
# 1. Currently the Spinner actually requires you to press the button after selecting an option to update the
# "text" field. Is there an action other than on_release that I can use to trigger defining the current text.
# 2. Altering or referencing variables within the kv. language from the python script can be a pain. Maybe
# convert the "Builder.load_string" stuff to python classes/objects instead and just don't use the kv language.
# 3. Find a cleaner way of generating the togglebuttons to define the rhythm.
# 4. Track state of toggle buttons
# 5. Provide user option to add an additional row of buttons to define a more intricate rhythym.
Builder.load_string("""
<ControlLayout>:
size_hint: 1, 1
pos_hint: {'center_x': .5, 'center_y': .5}
do_default_tab: False
TabbedPanelItem:
text: 'Home'
GridLayout:
cols: 2
canvas:
Rectangle:
pos: self.center_x, 5
size: 5, self.height - 10
Rectangle:
pos: self.width/2, self.center_y + 15
size: self.width/2, 5
BoxLayout:
orientation: 'vertical'
Label:
text: "Device Information"
bold: True
font_size: 22
markup: True
Button:
text: 'Update!'
size_hint: (.55, .10)
pos_hint: {'center_x': .5, 'center_y': .5}
on_press: root.fieldUpdate(vers_label, motor_num_label, rhythm_label, magnitude_label)
Label:
text: 'Version:'
bold: True
Label:
id: vers_label
text: ''
Label:
text: 'Number of Motors:'
bold: True
Label:
id: motor_num_label
text: ''
Label:
text: 'Rhythms:'
bold: True
Label:
id: rhythm_label
text: ''
Label:
text: 'Magnitudes:'
bold: True
Label:
id: magnitude_label
text: ''
FloatLayout:
Label:
text: 'Select CommPort:'
pos_hint: {'center_x': .5, 'center_y': .95}
bold: True
font_size: 22
markup: True
Spinner:
text: root.commPort[0]
values: root.commPort[0], root.commPort[1], root.commPort[2]
size_hint: (.55, .10)
pos_hint: {'center_x': .5, 'center_y': .75}
on_release: root.commChoice = self.text
Button:
text: 'Connect'
size_hint: (.95, .5)
pos_hint: {'center_x': .51, 'center_y': .26}
on_press: root.ser_connect(root.commChoice)
TabbedPanelItem:
text: 'Define Rhythm'
BoxLayout:
orientation: 'vertical'
canvas:
Rectangle:
pos: 5, self.center_y + 175
size: self.width - 10, 5
Rectangle:
pos: 5, self.center_y - 175
size: self.width - 10, 5
Label:
text: 'Midi-Like Interface'
bold: True
font_size: 22
BoxLayout:
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
state: 'normal'
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
BoxLayout:
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
ToggleButton:
size_hint: (1, .25)
pos_hint: {'center_x': .5, 'center_y': .5}
Label:
text: 'Each toggle button above represents 50ms of vibration time.'
BoxLayout:
Label:
text: 'Rhythm Name:'
TextInput:
text: 'Enter rhythm name here.'
size_hint: (1, .5)
pos_hint: {'center_x': .5, 'center_y': .5}
TabbedPanelItem:
text: 'Help'
RstDocument:
text: '\\n'.join(("Reserve for Training", "-----------", "You are in the third tab."))
TabbedPanelItem:
text: 'About'
Label:
text: 'Fourth tab content area'
<CustomPopup>:
size_hint: .5, .5
auto_dismiss: False
title: 'Comm Port Select'
Button:
text: 'Connect!'
on_press: root.dismiss()
""")
# Trying to implement a popup that asks to select your comm port and connect before going to main screen
# Would include this .kv syntax above:
# And the following class in the body:
class CustomPopup(Popup):
pass
class ControlLayout(TabbedPanel):
def __init__(self):
TabbedPanel.__init__(self)
self.commChoice = ""
self.ser = ""
self.enterCount = ['Enter', 0]
self.qryData = []
def show_popup(self):
p = CustomPopup()
p.open()
def printMenu(self):
time.sleep(2)
while 1:
line = self.ser.readline()
if len(line) > 1:
print line
else:
break
def ser_connect(self, commChoice):
try:
print commChoice
self.ser = serial.Serial(commChoice, timeout=1)
print "Connecting!"
except:
print "Failed to connect on ..."
# Program expects enter/return hit 3 times to bring up initial menu options
for x in range(4):
self.ser.write(os.linesep)
self.enterCount[1] += 1
print self.enterCount
time.sleep(2)
#self.printMenu()
self.qry_all()
def qry_all(self):
self.ser.write("4")
time.sleep(2)
self.ser.write("QRY ALL")
self.ser.write(os.linesep)
while 1:
line = self.ser.readline()
#line = ser.read(20)
if len(line) > 1:
print line
self.qryData.append(line)
else:
break
print self.qryData
def fieldUpdate(self, ver, motor, rhythm, mag):
ver.text = self.qryData[10]
motor.text = self.qryData[11]
rhythm.text = self.qryData[12]
mag.text = self.qryData[13] + self.qryData[14] + self.qryData[15] + self.qryData[16]
# Need a way for the item list in the above kv spinner to be variable.
if os.name == 'posix':
commPort = glob.glob('/dev/tty.*')
print "Printing current available comm ports.\n"
for i in commPort:
print i
# Need to build in comm post scans for linux and windows.
class HapticControlApp(App):
def build(self):
return ControlLayout()
if __name__ == '__main__':
HapticControlApp().run()