diff --git a/src/visiomode/config.py b/src/visiomode/config.py index ea38b97a..18670f0d 100644 --- a/src/visiomode/config.py +++ b/src/visiomode/config.py @@ -5,12 +5,14 @@ # Copyright (c) 2024 Olivier Delree # Distributed under the terms of the MIT Licence. -import os -import shutil import json import logging +import os +import shutil import typing +import serial.tools.list_ports as ports + logging.basicConfig(level=logging.INFO) DEFAULT_CONFIG = { @@ -25,6 +27,12 @@ "height": 800, "fullscreen": False, "devices": "devices", + "input_device_address": ( + ports.comports()[1] if len(ports.comports()) > 1 else "/dev/ttyS0" + ), + "reward_device_address": ( + ports.comports()[0] if len(ports.comports()) else "/dev/ttyS0" + ), "config_path": ".visiomode.json", } @@ -46,6 +54,8 @@ class Config: height: Screen height. fullscreen: Fullscreen mode flag. devices: Path to devices directory. + input_device_address: Path to the input device address. + reward_device_address: Path to the device address. config_path: Path to the config. """ @@ -60,6 +70,8 @@ class Config: height: int fullscreen: bool devices: str + input_device_address: str + reward_device_address: str config_path: str _instance = None diff --git a/src/visiomode/core.py b/src/visiomode/core.py index f5a607c4..15397345 100644 --- a/src/visiomode/core.py +++ b/src/visiomode/core.py @@ -184,6 +184,17 @@ def request_listener(self): logging.error("Invalid request - {}".format(request)) continue if request["type"] == "start": + # Update config + conf.Config().input_device_address = ( + request["data"].get("response_address") + or conf.Config().input_device_address + ) + conf.Config().reward_device_address = ( + request["data"].get("reward_address") + or conf.Config().reward_device_address + ) + conf.Config().save() + task = tasks.get_task(request["data"].pop("task")) self.session = models.Session( animal_id=request["data"].pop("animal_id"), diff --git a/src/visiomode/devices/__init__.py b/src/visiomode/devices/__init__.py index 6621062e..ff4d325b 100644 --- a/src/visiomode/devices/__init__.py +++ b/src/visiomode/devices/__init__.py @@ -2,6 +2,7 @@ # This file is part of visiomode. # Copyright (c) 2020 Constantinos Eleftheriou +# Copyright (c) 2024 Olivier Delree # Distributed under the terms of the MIT Licence. import os import queue @@ -12,9 +13,20 @@ import visiomode.plugins as plugins -def get_available_devices(): - """Return list of all serial devices connected to the machine.""" - return [dev.device for dev in ports.comports()] +def get_available_devices() -> list[str]: + """ + Return list of all serial devices connected to the machine, with ones defined in the + config prepended. + """ + # Get an ordered set of keys so that options presented to the user are more + # pleasingly organised. + return list( + dict.fromkeys( + [conf.Config().reward_device_address] + + [conf.Config().input_device_address] + + [dev.device for dev in ports.comports()] + ).keys() + ) def get_input_device(profile_id, address=None): diff --git a/src/visiomode/webpanel/templates/tasks/task.html b/src/visiomode/webpanel/templates/tasks/task.html index b307cf74..47b9a039 100644 --- a/src/visiomode/webpanel/templates/tasks/task.html +++ b/src/visiomode/webpanel/templates/tasks/task.html @@ -11,7 +11,7 @@