-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial_test_6_12_19.py
executable file
·169 lines (138 loc) · 5.71 KB
/
serial_test_6_12_19.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
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.core.text import LabelBase
#font file needs to be in the folder
LabelBase.register(name="Dodger", fn_regular= "dodger3condital.ttf")
LabelBase.register(name="Roboto", fn_regular= "RobotoSlab-Regular.ttf")
import serial
import threading
import serial.tools.list_ports
import pyglet
import multiprocessing
from apscheduler.schedulers.background import BackgroundScheduler
import os
SERIAL_PORT = ""
def port_listB():
try:
comlist = serial.tools.list_ports.comports()
connected = []
for element in comlist:
connected.append(element.device)
print("Connected COM ports: " + str(connected))
label2.text = "Connected COM ports: " + str(connected)
except:
label2.text = 'serial problem'
print('serial problem')
button = [" X "," A "," B "," Y "," Lb "," Rb "," Lt "," Rt "," Back "," Start "," Hat_1 "," Hat_2 "]
def joystick():
try:
while True:
joysticks = pyglet.input.get_joysticks()
joystick = joysticks[0]
joystick.open()
#print(joystick.event_types)
#joystick.register_event_type('on_joyaxis_motion',)
#for k, v in vars(joystick).items():
#print (k, v)
if not int(joystick.x) == 0:
label10.text = 'x'+str(joystick.x)
if not int(joystick.y) == 0:
label10.text = 'y'+str(joystick.y)
if not int(joystick.z) == 0:
label10.text = 'z'+str(joystick.z)
if not int(joystick.hat_x) == 0:
label10.text = 'hx'+str(joystick.hat_x)
if not int(joystick.hat_y) == 0:
label10.text = 'hy'+str(joystick.hat_y)
for i in range(len(joystick.buttons)):
if not (joystick.buttons[i]) == False:
label10.text = str(button[i])
#Don't like this but it's the only way I got it to behave
#For the mac the joystick needs to be in D
#x and y on the hats are discrete
#green light on the controller needs to be on and solid
#if the joystick connection fails the program has to be restarted
label5.text = "Direction Pad x "+str(joystick.x)+" y "+str(joystick.y)+"\n"+"Hats x "+str(joystick.hat_x)+" y "+str(joystick.hat_y)+" z "+str(joystick.z)+"\n"+"Buttons " + str(button[0]) + str(joystick.buttons[0]) + str(button[1]) + str(joystick.buttons[1])+ str(button[2]) + str(joystick.buttons[2])+ str(button[3]) + str(joystick.buttons[3])+ str(button[4]) + str(joystick.buttons[4])+ str(button[5]) + str(joystick.buttons[5])+"\n"+ str(button[6]) + str(joystick.buttons[6])+ str(button[7]) + str(joystick.buttons[7])+ str(button[8]) + str(joystick.buttons[8])+ str(button[9]) + str(joystick.buttons[9])+ str(button[10]) + str(joystick.buttons[10])+ str(button[11]) + str(joystick.buttons[11])
#writing to the arduino will have to be handled with the apscheduler
except:
print('Joystick Failed')
label5.text = 'Joystick Failed'
def port_contC():
if SERIAL_PORT != "":
global newsp
try:
newsp = serial.Serial(SERIAL_PORT, 115200, timeout=1)
label3.text = 'Connected'
except:
label3.text = 'Serial Problem'
else:
label3.text = 'Input Serialport Address'
def serial_write():
if not label10.text =="Data":
b = label10.text.encode('utf-8')
newsp.write(b)
label10.text = "Data"
label12.text = str(newsp.out_waiting)
def serial_read():
label9.text = str(newsp.read(10))
label11.text = str(newsp.in_waiting)
def arduinoB():
try:
global sched
sched = BackgroundScheduler(dameon = True)
sched.add_job(serial_write,'interval', seconds = .15)
sched.add_job(serial_read,'interval', seconds = .15)
sched.start()
label6.text = "Serial Streaming"
except:
label6.text = "Job Failed"
class SerialTestWindow(FloatLayout):
def term(self):
newsp.close()
sched.shutdown(wait=False)
os._exit(-1)
def port_list(self):
global label2
label2 = self.ids['l2']
t1 = threading.Thread(target=port_listB)
t1.daemon = True
t1.start()
def port_cont(self):
global label3
label3 = self.ids['l3']
t2 = threading.Thread(target=port_contC)
t2.daemon = True
t2.start()
def got_port(self):
global SERIAL_PORT
SERIAL_PORT = self.ids['l4'].text
def got_joystick(self):
global label5
global label10
label5 = self.ids['l5']
label10 = self.ids['l10']
t3 = threading.Thread(target=joystick)
t3.daemon = True
t3.start()
def got_data(self):
global label6
global label9
global label10
global label11
global label12
label6 = self.ids['l6']
label9 = self.ids['l9']
label10 = self.ids['l10']
label11 = self.ids['l11']
label12 = self.ids['l12']
t4 = threading.Thread(target=arduinoB())
t4.daemon = True
t4.start()
class Serial_test_6_12_19App(App):
def build(self):
return SerialTestWindow()
if __name__=='__main__':
Serial_test_6_12_19App().run()