Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow non-serial port device addresses #209

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/visiomode/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
# Copyright (c) 2024 Olivier Delree <[email protected]>
# 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 = {
Expand All @@ -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",
}

Expand All @@ -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.
"""

Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/visiomode/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
18 changes: 15 additions & 3 deletions src/visiomode/devices/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# This file is part of visiomode.
# Copyright (c) 2020 Constantinos Eleftheriou <[email protected]>
# Copyright (c) 2024 Olivier Delree <[email protected]>
# Distributed under the terms of the MIT Licence.
import os
import queue
Expand All @@ -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()
)
celefthe marked this conversation as resolved.
Show resolved Hide resolved


def get_input_device(profile_id, address=None):
Expand Down
2 changes: 1 addition & 1 deletion src/visiomode/webpanel/templates/tasks/task.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<label id="response_address_label" for="response_address">Input Device Address</label>
<select id="response_address" class="custom-select form-control mb-3">
{% for address in serial_devices %}
<option value="{{ address }}">{{ address }}</option>
<option value="{{ address }}" {% if loop.index == 2 %} selected {% endif %}>{{ address }}</option>
{% endfor %}
</select>
</div>
Expand Down
Loading