-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcom.py
172 lines (162 loc) · 6.01 KB
/
com.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
"""
Created on Thu Jun 16 17:54:49 2016
@author: antoine
"""
from threading import Thread
from multiprocessing import Process, Queue
import ctypes
import psutil
import time
import sys
class _DeviceProcess(Process):
""" Process for in/out control """
def __init__(self, FIFOreception, FIFOenvoi):
super(_DeviceProcess, self).__init__()
self.fifoin = FIFOreception
self.fifoout = FIFOenvoi
def lecture(self, name):
""" Read the device informations """
print(name +' started')
while True:
try:
self.fifoin.put(self.dev.read(1))
except (KeyboardInterrupt, SystemExit):
print("Exiting lecture...")
break
except:
print("Erreur Lecture")
def write(self, name):
""" Write the information to the device """
print(name +' started')
while True:
while self.fifoout.qsize() >= 1:
#self.dev.flush_input()
#print(self.fifoout.qsize())
tosend = self.fifoout.get()
try:
byte_data = bytes(tosend)
except TypeError:
# this will happen if we are Python3 and data is a str.
byte_data = tosend.encode("latin1")
except:
print("Erreur ecriture")
buf = ctypes.create_string_buffer(byte_data)
tc = self.dev.ftdi_fn.ftdi_write_data_submit(ctypes.byref(buf),len(byte_data))
#print("balbla")
#sys.stdout.flush()
#test = self.dev.fdll.ftdi_transfer_data_done(tc)
#print(test)
time.sleep(0.0001)
def run(self):
import pylibftdi
try:
self.dev = pylibftdi.Device()#pylint: disable=W0201
self.dev.baudrate = 230400
except pylibftdi._base.FtdiError:
print('haptic device not found')
writing = Thread(target=self.write, args=("Thread-write",))
writing.start()
lecturing = Thread(target=self.lecture, args=("Thread-read",))
lecturing.start()
class _DeviceProcessserial(Process):
""" Process for in/out control in serial """
def __init__(self, FIFOreception, FIFOenvoi, com):
super(_DeviceProcessserial, self).__init__()
self.fifoin = FIFOreception
self.fifoout = FIFOenvoi
self.com = com
def lecture(self, name):
""" Read the device informations """
print(name +' started')
while True:
try:
self.fifoin.put(self.dev.readline())
except (KeyboardInterrupt, SystemExit):
print("Exiting lecture...")
break
def write(self, name):
""" Write the information to the device """
print(name +' started')
while True:
if self.fifoout.qsize() >= 1:
tosend = self.fifoout.get()
self.dev.write(tosend)
def run(self):
import serial
try:
self.dev = serial.Serial(self.com, 115200, timeout=None)#pylint: disable=W0201
except serial.serialutil.SerialException:
print('haptic device not found')
writing = Thread(target=self.write, args=("Thread-write",))
writing.start()
lecturing = Thread(target=self.lecture, args=("Thread-read",))
lecturing.start()
#DEV = Device()
class HDevice:
""" Function to read/write to device """
def __init__(self, proto):
super(HDevice, self).__init__()
self.fifoin = Queue()
self.fifoout = Queue()
self.proto = proto
if proto == "ftdi":
self.processdev = _DeviceProcess(self.fifoin, self.fifoout)
else:
import re
globals()["re"] = re
self.processdev = _DeviceProcessserial(self.fifoin, self.fifoout, self.proto)
def launch(self):
""" Launch the process for device communication """
self.processdev.start()
pid = self.processdev.pid
p = psutil.Process(self.processdev.pid)
p.nice(psutil.HIGH_PRIORITY_CLASS)
print(str(pid) + "est le pid")
def get(self):
""" get byte from device """
return self.fifoin.get()
def quit(self):
""" quit device process """
self.processdev.terminate()
self.processdev.join()
def extract(self, size):
""" Extract 'size' bytes from 'fifo' and return a bytearray """
rec = bytearray([0]*size)
for i in range(0, size):
rec[i] = int.from_bytes(self.get(), 'big')
return rec
def readarray(self, size):
""" read a bytearray from device """
return bytearray(self.extract(size))
def readascii(self):
""" read data in ascii from serial port """
data = self.get()
return data.decode('ascii','backslashreplace')
def readsep(self, sep, size):
""" read data using Regexp """
data = self.readascii()
regexp = ""
for i in range(0,size):
regexp = regexp + r"([0-9]+(?:\.[0-9]+)?)(?:" + sep + ")"
#regexp = r"([0-9]+(?:\.[0-9]+)?)(?:\|)([0-9]+(?:\.[0-9]+)?)"
retour = re.findall(regexp, data)
try:
return retour[0]
except Exception:
return (0,0,0,0)
def incommingsize(self):
"""get the incomming buffer size"""
return self.fifoin.qsize()
def writeint(self, tosend):
"""write data to haptic device"""
bufenvoi = bytearray(4)
bufenvoi[0] = int(tosend) & int('0b00111111', 2)
bufenvoi[1] = ((int(tosend) >> 6) & int('0b00111111', 2)) | int('0b01000000', 2)
bufenvoi[2] = ((int(tosend) >> 12) & int('0b00111111', 2)) | int('0b10000000', 2)
bufenvoi[3] = int('0b11000000', 2)
self.fifoout.put(bufenvoi)
def write(self, tosend):
""" convert and send data to haptic device"""
forcenow = max(min(tosend, 130), -130)
forcenowint = 32767*(1+forcenow/130)
self.writeint(forcenowint)