Skip to content

Commit

Permalink
Updated Whisker touch
Browse files Browse the repository at this point in the history
  • Loading branch information
theonlydvr committed Aug 27, 2024
1 parent ca40731 commit d00517d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
19 changes: 9 additions & 10 deletions pybehave/Components/TouchBinaryInput.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,23 @@
class TouchBinaryInput(BinaryInput):

def __init__(self, task: Task, component_id: str, component_address: str):
self.definition = None
super().__init__(task, component_id, component_address)
self.pos = None

def update(self, read: Union[Tuple[bool, float], bool]) -> None:
if isinstance(read, tuple):
def update(self, read: Union[Tuple[bool, float], bool]) -> bool:
if isinstance(read, tuple) or isinstance(read, list):
value = read[0]
pos = read[1]
else:
value = read
pos = None

if value == self.state:
repeat = True
return False
else:
repeat = False
self.state = value
if value and not repeat:
self.pos = pos
elif not value and not repeat:
self.pos = None
self.state = value
if value:
self.pos = pos
else:
self.pos = None
return True
43 changes: 31 additions & 12 deletions pybehave/Sources/WhiskerTouchScreenSource.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Dict

try:
import win32gui
except ModuleNotFoundError:
Expand Down Expand Up @@ -27,33 +29,43 @@ class WhiskerTouchScreenSource(Source):

def __init__(self, address='localhost', port=3233, display_num=0, whisker_path=r"C:\Program Files (x86)\WhiskerControl\WhiskerServer.exe"):
super().__init__()
self.available = True
self.address = address
self.port = int(port)
self.display_num = display_num
self.whisker_path = whisker_path
self.client = None
self.running = None
self.vals = {}

def initialize(self):
try:
win32gui.EnumWindows(look_for_program, 'WhiskerServer')
if not IsWhiskerRunning:
ws = whisker_path
ws = self.whisker_path
window = subprocess.Popen(ws)
time.sleep(2)
print("WHISKER server started", window)
win32gui.EnumWindows(look_for_program, 'WhiskerServer')
if IsWhiskerRunning:
self.available = True
self.display_num = display_num
self.display_num = self.display_num
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.client.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
self.client.connect((address, int(port)))
self.client.connect((self.address, int(self.port)))
self.running = threading.Event()
rt = threading.Thread(target=lambda: self.read())
rt.start()
self.client.send('DisplayClaim {}\n'.format(display_num).encode('utf-8'))
self.client.send('DisplayClaim {}\n'.format(self.display_num).encode('utf-8'))
self.client.send(b'DisplayEventCoords on\n')
self.client.send('DisplayCreateDocument {}\n'.format(display_num).encode('utf-8'))
self.client.send('DisplayShowDocument {} {}\n'.format(display_num, display_num).encode('utf-8'))
self.client.send('DisplayGetSize {}\n'.format(display_num).encode('utf-8'))
self.vals = {}
self.client.send('DisplayCreateDocument {}\n'.format(self.display_num).encode('utf-8'))
self.client.send('DisplayShowDocument {} {}\n'.format(self.display_num, self.display_num).encode('utf-8'))
self.client.send('DisplayGetSize {}\n'.format(self.display_num).encode('utf-8'))
except:
traceback.print_exc()
self.available = False


def close_source(self) -> None:
self.running.set()

Expand All @@ -66,6 +78,7 @@ def read(self):
if msg.startswith('Event:'):
split_msg = msg.split(' ')
self.vals[split_msg[1]] = (not self.vals[split_msg[1]][0], [int(split_msg[2]), int(split_msg[3])])
self.update_component(split_msg[1], self.vals[split_msg[1]])
elif msg.startswith('Info:'):
if 'size: ' in msg:
split = msg.split(': ')[2].split(' ')
Expand All @@ -79,7 +92,7 @@ def read(self):
self.client.send(b'DisplayRelinquishAll\n')
self.client.close()

def register_component(self, _, component):
def register_component(self, component, metadata):
self.components[component.id] = component
if component.get_type() == Component.Type.DIGITAL_OUTPUT:
self.client.send(
Expand All @@ -93,9 +106,6 @@ def register_component(self, _, component):
).encode('utf-8'))
self.vals[component.id] = (False, None)

def read_component(self, component_id):
return self.vals[component_id]

def write_component(self, component_id, msg):
if msg:
self.client.send('DisplayBringToFront {} {}\n'.format(self.display_num, component_id).encode('utf-8'))
Expand All @@ -108,3 +118,12 @@ def close_component(self, component_id: str) -> None:

def is_available(self):
return self.available

@staticmethod
def metadata_defaults(comp_type: Component.Type = None) -> Dict:
if comp_type == Component.Type.DIGITAL_OUTPUT:
return {"definition": ""}
elif comp_type == Component.Type.DIGITAL_INPUT:
return {"obj": ""}
else:
return {}

0 comments on commit d00517d

Please sign in to comment.