Skip to content

Commit

Permalink
fix: fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
YarikRevich committed Apr 7, 2024
1 parent abcdb31 commit 673e3a9
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 44 deletions.
25 changes: 24 additions & 1 deletion Resources/Proto/Container/Content/settings.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,33 @@ enum SettingsType {
SetIntegralTime = 3;
}

// Represents all the available set grain value presets, which can be used with settings bus.
enum SetGrainSettingType {
SetGrainSettingNone = 0; // Stub enum to handle serialization.
LOW = 1;
MEDIUM = 2;
HIGH = 3;
MAX = 4;
}

// Represents all the available set integral time value presets, which can be used with settings bus.
enum SetIntegralTimeSettingType {
SetIntegralTimeSettingNone = 0; // Stub enum to handle serialization.
100MS = 1;
200MS = 2;
300MS = 3;
400MS = 4;
500MS = 5;
600MS = 6;
}

// Represents settings bus request content send from the client to the board.
message SettingsBusRequestContent {
SettingsType settingsType = 1;
optional int32 value = 2;
oneof value {
SetGrainSettingType setGainValue = 2;
SetIntegralTimeSettingType setIntegralTimeValue = 3;
}
}

// Represents settings bus response content send to the client from the board.
Expand Down
4 changes: 2 additions & 2 deletions Scripts/cli/src/command/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ def get_available_devices() -> None:
GetAvailableDevicesCommand.handle()

@staticmethod
def get_data(device: str, baud_rate: int, type: str, series: int = 1, export: str = None,
def get_data(device: str, baud_rate: int, type: str, series: int = 1, export: str = None, generate: bool = False,
figure: str = "scatter") -> None:
"""
Returns sensor data of selected type. The available data types are 'raw', 'full', 'infrared', 'visible'.
Allows to perform a series of measurements and export that data to a graph view. The available figure types are
'scatter' and 'bar'.
"""

GetDataCommand.handle(device, baud_rate, type, series, export, figure)
GetDataCommand.handle(device, baud_rate, type, series, export, generate, figure)

@staticmethod
def get_info(device: str, baud_rate: int, type: str) -> None:
Expand Down
15 changes: 9 additions & 6 deletions Scripts/cli/src/command/get_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class GetDataCommand:
VISIBLE_TYPE: str = "visible"

@staticmethod
def handle(device: str, baud_rate: int, type: str, series: int, export: str, figure: str):
def handle(device: str, baud_rate: int, type: str, series: int, export: str, generate: bool, figure: str):
"""Handles the execution of command wrapper."""

if not is_device_available(device):
Expand All @@ -28,7 +28,7 @@ def handle(device: str, baud_rate: int, type: str, series: int, export: str, fig

data: list[RetrievedDataDto] = []

for i in range(series):
for _ in range(series):
match type:
case GetDataCommand.RAW_TYPE:
data.append(GetDataCommand.process_get_raw_data(device, baud_rate))
Expand All @@ -50,20 +50,23 @@ def handle(device: str, baud_rate: int, type: str, series: int, export: str, fig
logging.info("Data has been successfully retrieved.")

if is_export_valid(export):
figure = Visualizer(export, data)
visualizer = Visualizer(export, data)

match figure:
case Visualizer.SCATTER_FIGURE:
figure.select_scatter()
visualizer.select_scatter()

case Visualizer.BAR_FIGURE:
figure.select_bar()
visualizer.select_bar()

case Visualizer.PLOT_FIGURE:
visualizer.select_plot()

case _:
logging.error("Given figure type is not valid.")
return

figure.save()
visualizer.save()

@staticmethod
def process_get_raw_data(device: str, baud_rate: int) -> RetrievedDataDto:
Expand Down
67 changes: 67 additions & 0 deletions Scripts/cli/src/command/set_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import logging

from middleware import is_device_available
from dto import RetrievedInfoDto
from client import Client
from tools import print_output


class SetSettingsCommand:
"""Represents 'set_settings' command."""

# Represents 'reset' type of the setting(does not allow any value).
RESET_TYPE: str = "reset"

# Represents 'gain' type of the setting.
SET_GAIN_TYPE: str = "gain"

# Represents 'integral_time' type of the setting.
SET_INTEGRAL_TIME_TYPE: str = "integral_time"

@staticmethod
def handle(device: str, baud_rate: int, type: str):
"""Handles the execution of command wrapper."""

if not is_device_available(device):
logging.error("Selected device is not available")
return

data: RetrievedInfoDto

match type:
case SetSettingsCommand.RESET_TYPE:
data = SetSettingsCommand.process_reset_settings(device, baud_rate)

case SetSettingsCommand.SET_GAIN_TYPE:
data = SetSettingsCommand.process_set_gain_settings(device, baud_rate)

case SetSettingsCommand.SET_INTEGRAL_TIME_TYPE:
data = SetSettingsCommand.process_set_integral_time_settings(device, baud_rate)

case _:
logging.error("Given settings type is not valid.")
return

print_output(data)
logging.info("Settings has been successfully set.")

@staticmethod
def process_reset_settings(device: str, baud_rate: int) -> RetrievedInfoDto:
"""Processes request to retrieve 'reset' metadata info from the device"""

with Client(device, baud_rate) as client:
return client.send_info_bus_request_gain_info_type_content()

@staticmethod
def process_set_gain_settings(device: str, baud_rate: int) -> RetrievedInfoDto:
"""Processes request to retrieve 'integral_time' metadata info from the device"""

with Client(device, baud_rate) as client:
return client.send_info_bus_request_integral_time_info_type_content()

@staticmethod
def process_set_integral_time_settings(device: str, baud_rate: int) -> RetrievedInfoDto:
"""Processes request to retrieve 'processed_requests' metadata info from the device"""

with Client(device, baud_rate) as client:
return client.send_info_bus_request_processed_requests_info_type_content()
5 changes: 4 additions & 1 deletion Scripts/cli/src/dto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
from .retrieved_data_dto import RetrievedDataDto

from .retrieved_info_dto import InfoTypeCompound
from .retrieved_info_dto import RetrievedInfoDto
from .retrieved_info_dto import RetrievedInfoDto

from .set_settings_dto import SettingsTypeCompound
from .set_settings_dto import SetSettingsDto
37 changes: 37 additions & 0 deletions Scripts/cli/src/dto/set_settings_dto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from enum import Enum


# uint32 deviceId = 1;
# SettingsType settingsType = 2;
# bool result = 3;
# uint32 nonce = 4;


class SettingsTypeCompound(Enum):
"""Represents settings compound used to represent result settings type."""

RESET = 'Reset'
SET_GAIN = 'SetGain'
SET_INTEGRAL_TIME = 'SetIntegralTime'


class SetSettingsDto:
"""Represents dto used to hold the result of a 'set_settings' command."""

# Represents remote device identification number.
device_id: int

# Represents settings type of the received result.
settings_type: SettingsTypeCompound

# Represents result of the received result.
result: int

# Represents nonce of the received result.
nonce: int

def __init__(self, device_id: int, settings_type: SettingsTypeCompound, result: bool, nonce: int):
self.device_id = device_id
self.settings_type = settings_type
self.result = result
self.nonce = nonce
10 changes: 9 additions & 1 deletion Scripts/cli/src/middleware/middleware.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from serial.tools import list_ports

# Represents STM32 board manufacturer name.
Expand Down Expand Up @@ -42,4 +44,10 @@ def is_device_available(device: str) -> bool:
def is_export_valid(src: str) -> bool:
"""Checks if the given export path is valid and can be used for diagram generation."""

pass
return not os.path.isfile(src)


def generate_export() -> str:
"""Generates random export location."""

pass
48 changes: 15 additions & 33 deletions Scripts/cli/src/visualizer/visualizer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
# import tkinter
# from tkinter import Tk

# from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg)
# from matplotlib.figure import Figure

import matplotlib.pyplot as plt

from dto import RetrievedDataDto
Expand All @@ -14,54 +8,42 @@ class Visualizer:

SCATTER_FIGURE = "scatter"
BAR_FIGURE = "bar"
PLOT_FIGURE = "plot"

# Represents a path where generated visualization will be saved.
location: str

# Represents a collection of values used for visualization generation.
values: list[RetrievedDataDto]

# Represents a base plot figure.
figure: plt.Figure

def __init__(self, location: str, values: list[RetrievedDataDto]):
self.location = location
self.values = values

self.figure = plt.figure()

def select_scatter(self) -> None:
"""Selects scatter figure as the output visualization type."""

pass


def select_bar(self) -> None:
"""Selects bar figure as the output visualization type."""

pass

def save(self) -> None:
"""Saves select figure in the given location."""

# fig = plt.figure()
# plt.plot([1,2,3])
# plt.savefig('/tmp/test0.png')
# plt.close(fig)

def select_plot(self) -> None:
"""Selects plot figure as the output visualization type."""

# plt.savefig('foo.png')
plot = self.figure.add_subplot(111)
plot.plot([value.value for value in self.values])

def save(self) -> None:
"""Saves select figure in the given location."""

def render_figure(data: list[str]) -> None:
"""Renderes figure with the given content"""

root = Tk()
root.wm_title("Viewport")

figure = Figure(figsize=(5, 2), dpi=80)
plot = figure.add_subplot(111)

data = [x ** 2 for x in range(101)]
plot.plot(data)

canvas = FigureCanvasTkAgg(figure, master=root)

canvas.draw()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)

root.mainloop()
self.figure.savefig(self.location)
plt.close(self.figure)

0 comments on commit 673e3a9

Please sign in to comment.