Skip to content
Snippets Groups Projects
Commit fc5f8b68 authored by Roberto-R's avatar Roberto-R
Browse files

Improved typing and documentation

parent 897e4bec
No related branches found
No related tags found
No related merge requests found
......@@ -10,8 +10,7 @@ class Mode(Enum):
class PlotDecoder:
"""
Class to decode the BioRobotics plot protocol
"""Class to decode the BioRobotics plot protocol.
The serial protocol is as follows:
......@@ -22,7 +21,7 @@ class PlotDecoder:
"""
def __init__(self, *args, **kwargs):
"""Constructor"""
"""Constructor."""
self.header_bytes = [b'\x7f', b'\xff', b'\xbf']
self.buffer = bytearray() # Store bytes while receiving
......@@ -33,8 +32,8 @@ class PlotDecoder:
self.time = 0 # Read microtime
self.data = [] # Received floats
def set_state(self, new_mode):
"""Simple wrapper to change mode"""
def set_state(self, new_mode: Mode):
"""Simple wrapper to change mode."""
self.mode = new_mode
self.bytes_count = 0
......@@ -43,9 +42,8 @@ class PlotDecoder:
if new_mode == Mode.DATA:
self.data = []
def receive_byte(self, byte):
"""
Precess new incoming byte
def receive_byte(self, byte: bytearray) -> bool:
"""Precess new incoming byte.
Return true when a complete package was received
"""
......
......@@ -3,11 +3,11 @@ from PyQt5.QtCore import pyqtSignal
class ComboBox(QComboBox):
"""Extend QComboBox widget to handle click event"""
"""Extend QComboBox widget to handle click event."""
popupAboutToBeShown = pyqtSignal()
def showPopup(self):
"""Fire event first, then call parent popup method"""
"""Fire event first, then call parent popup method."""
self.popupAboutToBeShown.emit()
super().showPopup()
from PyQt5.QtWidgets import QWidget, QMainWindow, QLabel, QCheckBox, \
QLineEdit, QPushButton, QMenu, QAction, QMessageBox, QFileDialog, \
QVBoxLayout, QHBoxLayout, QFormLayout
from PyQt5.QtGui import QIntValidator, QDoubleValidator, QIcon
from PyQt5.QtGui import QIntValidator, QDoubleValidator, QIcon, QCloseEvent
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtSerialPort import QSerialPort, QSerialPortInfo
import pyqtgraph as pg
......@@ -9,6 +9,7 @@ import numpy as np
import json
import os
import time
from typing import Optional, List
from plot_decoder.plot_decoder import PlotDecoder
from ui.combo_box import ComboBox
......@@ -45,8 +46,8 @@ class MainWindow(QMainWindow):
# Prepare data structure
self.channels = 0 # Wait for serial data, resize on the fly
self.data = None # Received data, each row is a channel
self.time = None # Timestamps of each data column
self.data: Optional[np.array] = None # Received data, each row is a channel
self.time: Optional[np.array] = None # Timestamps of each data column
self.data_points = 0 # Number of points recorded
self.data_size = 200 # Number of points in history
self.time_offset = None # Time offset in microseconds
......@@ -56,7 +57,7 @@ class MainWindow(QMainWindow):
self.y_scale = [-10.0, 10.0] # Y-scale values when not automatic
# The system time of the last frame update - Used to limit framerate
self.last_update = 0
self.last_update = 0.0
# Create property stubs
self.input_port = ComboBox()
......@@ -72,8 +73,8 @@ class MainWindow(QMainWindow):
self.layout_plots = pg.GraphicsLayoutWidget()
self.button_save = QPushButton('Save')
self.plots = [] # Start with empty plots
self.curves = []
self.plots: List[pg.PlotItem] = [] # Start with empty plots
self.curves: List[pg.PlotDataItem] = []
self.build_ui_elements() # Put actual GUI together
......@@ -158,7 +159,7 @@ class MainWindow(QMainWindow):
self.setCentralWidget(main_widget)
@pyqtSlot(bool)
def on_connect_toggle(self, checked):
def on_connect_toggle(self, checked: bool):
"""When the serial `connect` button is pressed"""
self.button_port.setText('Disconnect' if checked else 'Connect')
......@@ -189,7 +190,7 @@ class MainWindow(QMainWindow):
self.input_autoscale.setDisabled(False)
@pyqtSlot(bool)
def on_autoscale_toggle(self, checked):
def on_autoscale_toggle(self, checked: bool):
"""Callback for the autoscale checkbox"""
# Enable/disable manual scales
......@@ -213,10 +214,10 @@ class MainWindow(QMainWindow):
self.decoder.data)
@pyqtSlot(QAction)
def on_save(self, action):
def on_save(self, action: QAction):
self.save_data(action.text())
def save_data(self, file_format):
def save_data(self, file_format: str):
"""Save data, file_format is either `csv` or `numpy`"""
file_format = file_format.lower()
......@@ -288,7 +289,7 @@ class MainWindow(QMainWindow):
with open('settings.json', 'w') as file:
file.write(json.dumps(settings))
def closeEvent(self, event):
def closeEvent(self, event: QCloseEvent):
"""When main window is closed"""
self.serial.close()
self.save_settings()
......@@ -322,7 +323,7 @@ class MainWindow(QMainWindow):
self.serial.clear() # Get rid of data in buffer
def update_data(self, channels, micros, new_data):
def update_data(self, channels: int, micros: int, new_data: list):
"""Called when new row was received"""
if self.channels != channels:
......@@ -359,7 +360,7 @@ class MainWindow(QMainWindow):
for i, curve in enumerate(self.curves):
curve.setData(x=data_x[0, :], y=data_y[i, :])
def set_channels(self, channels):
def set_channels(self, channels: int):
"""
Resize number of channels
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment