You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WheelchairAdapter.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # TODO für python3 anpassen und in gui mit einfügen
  2. # imports
  3. from socket import *
  4. import thread, threading, struct
  5. import Queue
  6. import time, datetime
  7. from time import clock
  8. import os
  9. import ctypes
  10. # connect to BCI2000 in a new thread
  11. class BCI2000ReceiverThread(threading.Thread):
  12. def __init__(self, id, phaseInSequenceQueue, StimulusCodeQueue):
  13. threading.Thread.__init__(self, name="BCI2000ReceiverThread%d" % (id,))
  14. self.phaseInSequenceQueue=phaseInSequenceQueue
  15. self.StimulusCodeQueue=StimulusCodeQueue
  16. self.running=True
  17. self.firstData = True
  18. self.repeat = 0
  19. #udp settings
  20. self.host = "localhost"
  21. self.port = 5003 # BCI2000
  22. self.UNITY_PORT = 5002 # unity
  23. self.FEATHER_PORT = 5001 # feather gui
  24. self.go = True
  25. self.buf = 1024
  26. self.addr = (self.host,self.port)
  27. #connect to udp socket
  28. self.UDPSock = socket(AF_INET,SOCK_DGRAM)
  29. self.UDPSock.bind(self.addr) # BIND to LISTEN
  30. print('%s initialized to %s at port %s' % (self.getName(), self.host, self.port))
  31. print('Relaying SelectedTargerts to port -- %s ---' % self.UNITY_PORT)
  32. self.relay_unity = socket(AF_INET,SOCK_DGRAM) # UDP RELAY TO UNITY
  33. print('Relaying all to port -- %s ---' % self.FEATHER_PORT)
  34. self.relay_feather = socket(AF_INET,SOCK_DGRAM) # UDP RELAY TO FEATHER
  35. def kill(self):
  36. self.running=False
  37. log('%s terminated' % (self.getName()))
  38. def run(self):
  39. # define a new contains method comparing strings
  40. def contains(theString, theQueryValue):
  41. return theString.find(theQueryValue) > -1
  42. while self.running:
  43. # try to receive from BCI2000 AppConnector
  44. try:
  45. self.data,self.addr = self.UDPSock.recvfrom(self.buf)
  46. #print(self.data) #####
  47. if self.firstData:
  48. print ("UDP is receiving...")
  49. self.firstData=0
  50. if "SelectedTarget 0" in self.data:
  51. self.go = True
  52. if "SelectedTarget" in self.data and "SelectedTarget 0" not in self.data and self.go == True:
  53. print "Relaying Command to Unity: ", self.data
  54. self.relay_unity.sendto(self.data, (self.host, self.UNITY_PORT))
  55. self.go = False
  56. if contains(self.data, "PhaseInSequence"):
  57. phaseInSequence=int(self.data.replace('PhaseInSequence ',''))
  58. self.phaseInSequenceQueue.put(phaseInSequence)
  59. #print (phaseInSequence)
  60. if contains(self.data, "StimulusCode "):
  61. print "Relaying Command to Feather: ", self.data
  62. self.relay_feather.sendto(self.data, (self.host, self.FEATHER_PORT))
  63. StimulusCode=int(self.data.replace('StimulusCode ',''))
  64. if not StimulusCode == 0 and self.repeat == 0:
  65. self.repeat = 1
  66. self.StimulusCodeQueue.put(StimulusCode)
  67. self.Code = StimulusCode
  68. elif StimulusCode == 0:
  69. self.repeat = 0
  70. # print (StimulusCode)
  71. # wait a short time and try again
  72. except error:
  73. time.sleep(0.0001)
  74. print('sleeping')
  75. continue
  76. # def run(self):
  77. # a = 1
  78. def returnProgramStatus(self):
  79. return self.running
  80. def kill(self):
  81. self.running=False
  82. log('%s terminated' % (self.getName()))
  83. ### M A I N P R O G R A M M ###
  84. # initialize queue
  85. phaseInSequenceQueue = Queue.Queue()
  86. StimulusCodeQueue = Queue.Queue()
  87. RunApplication = True
  88. #start BCI2000ReceiverThread
  89. BCI2000ReceiverThread1=BCI2000ReceiverThread(1, phaseInSequenceQueue, StimulusCodeQueue)
  90. BCI2000ReceiverThread1.start()