Skip to content

Commit

Permalink
[Fx-335] LSL Demo (#128)
Browse files Browse the repository at this point in the history
* adds dependencies, adds demo5, renames demo files

* WIP

* adds lsl streaming demo

* updates pre-commit config to work with new demo

* updates poetry dependencies to solve sec vulnerabilities

* makes try/xfepts more localized

* minor change
  • Loading branch information
Sotilrac authored Dec 4, 2023
1 parent 082fcc3 commit 676e39a
Show file tree
Hide file tree
Showing 8 changed files with 490 additions and 513 deletions.
16 changes: 4 additions & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: trailing-whitespace
exclude: |
Expand All @@ -27,20 +27,11 @@ repos:
- id: check-docstring-first
- id: check-case-conflict
- repo: https://github.com/psf/black
rev: 23.7.0
rev: 23.11.0
hooks:
- id: black
# https://stackoverflow.com/a/61238571/8087342
# - repo: local
# hooks:
# - id: pylint
# name: pylint
# entry: pylint
# language: system
# types: [python]
# require_serial: true
- repo: https://github.com/PyCQA/pylint
rev: v3.0.0a7
rev: v3.0.1
hooks:
- id: pylint
args:
Expand All @@ -55,3 +46,4 @@ repos:
- pandas
- pyyaml
- pendulum
- pylsl
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
101 changes: 101 additions & 0 deletions demos/demo5_lsl_streaming.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
"""
Demo 5: Lab Streaming Layer
Reads data from a Dephy device and streams it using the LSL protocol.
Besided the python dependencies in teh project, the LSL compiled
library is required: https://github.com/sccn/liblsl/releases
To visualize the data stream, use PlotJuggler:
https://github.com/Sotilrac/plotjuggler-lsl
"""
# pylint: disable=duplicate-code

import argparse
import platform
import uuid
from time import sleep

from pylsl import StreamInfo, StreamOutlet
from flexsea.device import Device


def main():
# Create an ArgumentParser object
parser = argparse.ArgumentParser(description="LSL Streaming Demo")

# Adding arguments
parser.add_argument(
"--port", default="/dev/ttyACM0", help="Port to use (default: /dev/ttyACM0)"
)
parser.add_argument(
"--freq", type=int, default=100, help="Freqency setting in Hz (default: 100)"
)
parser.add_argument(
"--api", default="12.0.0", help="Version number (default: 12.0.0)"
)

# Parsing arguments
args = parser.parse_args()

# Accessing the arguments
print(f"Port: {args.port}")
print(f"Frequency: {args.freq} Hz")
print(f"API Version: {args.api}")

if "windows" == platform.system().lower():
msg = "WARNING: these demos may not function properly on Windows "
msg += "due to the way the OS handles timing. They are best run on "
msg += "linux."
print(msg)

try:
device = Device(port=args.port, firmwareVersion=args.api)
except RuntimeError as err:
print(f"There's a problem connecting to the device at {args.port}: \n {err}")
return

device.open()
device.start_streaming(args.freq)

# Get the data labels
print(len(device._fields))
labels = sorted(device.read().keys())

print(f"{len(labels)} labels to be streamed:")
print(labels)

# Create LSL Stream
info = StreamInfo(
f"{device.id:X}",
"Dephy",
len(labels),
float(args.freq),
"int64",
f"{uuid.uuid4()}",
)

chns = info.desc().append_child("channels")
for label in labels:
ch = chns.append_child("channel")
ch.append_child_value("label", label)
outlet = StreamOutlet(info, 64, 360)

sleep(0.5)
while True:
try:
data = device.read(allData=True)
except AssertionError as err:
data = []
print(f"Problem reading: {err}")
for sample in data:
if sample["System Time"] != 0: # Filter out invalid readings
outlet.push_sample([sample[key] for key in sorted(sample)])
sleep(0.1)

device.close()


if __name__ == "__main__":
main()
885 changes: 384 additions & 501 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ ipython = "^8.12.0"
sphinx = "^7.0.1"
pydata-sphinx-theme = "^0.13.3"
sphinx-copybutton = "^0.5.2"
pylsl = "^1.16.2"

[build-system]
requires = ["poetry-core"]
Expand Down

0 comments on commit 676e39a

Please sign in to comment.