-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVestiiInterface.py
89 lines (73 loc) · 3.6 KB
/
VestiiInterface.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
from __future__ import division
import serial
import glob
import platform
import os
"""A part of the Vestii project during Hack the Senses
Written by: David Turner <[email protected]>
Last modified by: David Turner on 25 June, 2016
Copyright (c) 2016 David J Turner. All right reserved
"""
__author__ = 'David Turner'
class Vestii(object):
def __init__(self, NumElec=2): # Runs on declaration of new Vestii object
self.NumElec = NumElec
SysID = platform.system() # Uses platform module to identify Windows/Mac/Other
if SysID == 'Windows':
Ports = list(serial.tools.list_ports.comports()) # List of Windows Ports
for i in range(0, len(Ports)):
if any("Arduino" in s for s in Ports[i]):
ArduinoPort = (str(Ports[i]))[2:6] # Searches the ports for Vestii
elif SysID == 'Darwin':
PortString = str(glob.glob("/dev/tty.usbmodem*")) # OS X port list
#ArduinoPort = PortString[2: (len(PortString)) - 2]
ArduinoPort = "/dev/cu.usbmodem1411"
else:
ArduinoPort = "/dev/" + os.popen("dmesg | egrep ttyACM | cut -f3 -d: | tail -n1").read().strip()
try: # Tries to connect to the Arduino, if not possible then error is displayed
self.Interface = serial.Serial(ArduinoPort, 9600)
self.Connected = True # Interface and Connected defined as class attributes
except OSError as x:
print(x)
print('There seems to be an issue connecting')
print('Either the Arduino is not connected, or Arduino serial monitor is open and accessing the port' + '\n')
self.Connected = False
exit()
self.History = {}
def UpdateVestii(self, EListPol, EListCur):
"""
:EListPol: is a list with the polarities
:EListCur: is a list of currents
"""
EDict = {}
for i in range(0, self.NumElec):
EDict["ElectrodePol{0}".format(i)] = EListPol[i] # ElectrodePol is a polarity value
EDict["ElectrodeCur{0}".format(i)] = EListCur[i] # ElectrodeCur is a current value
self.State = EDict
if self.State != self.History:
if self.Connected is True: # Only send update if data has changed
# ToSend = str(self.NumElec)
# for y in range(0, self.NumElec):
# ToSend = ToSend + (str(self.State["ElectrodeCur{0}".format(y)]) + '#' + str(self.State["ElectrodePol{0}".format(y)]) + '$')
# ToSend = ToSend + '\n' # Control string to send to Arduino
# self.Interface.write(ToSend) # Writes over serial to Arduino
to_send = "%d%d%3d%3d\n" % (EListPol[0],EListPol[1], EListCur[0],EListCur[1])
self.Interface.write(to_send)
#print "SENT!!!", to_send
# print "Read", self.Interface.read(1)
if self.Connected is False:
print('The fuck you doing')
# ToSend = str(self.NumElec)
# for y in range(0, self.NumElec):
# ToSend = ToSend + (str(self.State["ElectrodeCur{0}".format(y)]) + '#' + str(
# self.State["ElectrodePol{0}".format(y)]) + '$')
# ToSend = ToSend + '\n' # Control string to send to Arduino
# print(ToSend) # Prints string
self.History = self.State
def ZeroVestii(self):
ZeroPol = []
ZeroCur = []
for i in range(0, self.NumElec):
ZeroPol.append(0)
ZeroCur.append(0)
self.UpdateVestii(ZeroPol, ZeroCur)