Aharoni-Lab / Miniscope-DAQ-QT-Software

Software for streaming and controlling neural and behavioral data from freely behaving animals
GNU General Public License v3.0
116 stars 29 forks source link

Add socket into backend. That allow record/stop by python #56

Open chenxinfeng4 opened 1 year ago

chenxinfeng4 commented 1 year ago

The code for complete issue #55

image Use python3 to start / stop Control Pannel.

Prospect:

Demo:

1. Use python3 commands to synchronize

# %%
import socket
import time
# %% create connection
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serve_ip = 'localhost'
serve_port = 20172       #default backend Socket PORT [20172, 20173, 20174, 20175, ...]
tcp_socket.connect((serve_ip, serve_port))

def send_read(send_data):
    send_data_byte = send_data.encode("utf-8")
    tcp_socket.send(send_data_byte)

    from_server_msg = tcp_socket.recv(1024)
    print(from_server_msg.decode("utf-8"))

# %% Supported commands
cmds = ['query_record', 'start_record', 'stop_record']

for send_data in cmds:
    send_read(send_data)
    time.sleep(5)

image

2. Use bind hotkey to synchronize

Further, I can bind global-hotkeys to start_record(F2) and stop_record(F4). Multiple DAQ can bind the same hotkey to start together.

# !pip install global-hotkeys
import socket
import time
from global_hotkeys import *

# Flag to indicate the program whether should continue running.
is_alive = True

# %% create connection
def send_read(send_data):
    try:
        tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        serve_ip = 'localhost'
        serve_port = 20172       #default ArControl Recorder Socket PORT
        tcp_socket.connect((serve_ip, serve_port))
        send_data_byte = send_data.encode("utf-8")
        tcp_socket.send(send_data_byte)

        from_server_msg = tcp_socket.recv(1024)
        print(from_server_msg.decode("utf-8"))
        del tcp_socket
    except Exception:
        pass

bindings = [
    [["f2"], lambda: send_read("start_record"), None],
    [["f4"], lambda: send_read("stop_record"), None],
]

register_hotkeys(bindings)

# Finally, start listening for keypresses
start_checking_hotkeys()

print("Ready. \n1. [F2] to start. \n2. [F4] to stop.")
# Keep waiting until the user presses the exit_application keybinding.
# Note that the hotkey listener will exit when the main thread does.
while is_alive:
    time.sleep(2)