Skip to content

Commit

Permalink
Merge pull request #178 from skunkforce/Dev_v1.0.0
Browse files Browse the repository at this point in the history
Version 1.0.0 of OmnAIView

Changes: 

- Calibration 
- ImGuiInstance changed 
- saving bugs corrected
- better Design
  • Loading branch information
AKMaily authored Sep 16, 2024
2 parents 6c6ff86 + d9c2034 commit 99bbf01
Show file tree
Hide file tree
Showing 13 changed files with 1,732 additions and 9,553 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ jobs:
include:
- os: windows-latest
triplet: x64-windows
- os: ubuntu-latest
- os: ubuntu-24.04
compiler: gcc
gcc: 13
gcc: 13.1
triplet: x64-linux

steps:
Expand Down
2 changes: 1 addition & 1 deletion ImGuiInstance
2 changes: 1 addition & 1 deletion ai_omniscope-v2-communication_sw
8 changes: 4 additions & 4 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@
},
"font": "comicsans",
"minscale": 1.0,
"scale": 1.5000001192092896
"scale": 1.01
},
"title": "Omniview 0.5",
"window": {
"color": {
"blue": 0.7,
"green": 0.7,
"red": 0.7,
"blue": 0.1,
"green": 0.1,
"red": 0.1,
"transparency": 1.0
},
"sizex": 1920,
Expand Down
Binary file added fonts/Inter_24pt-Medium.ttf
Binary file not shown.
136 changes: 118 additions & 18 deletions src/handler.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,99 @@
#include "handler.hpp"

#include <functional>
#include <set>
#include <implot.h>


#include "popups.hpp"
#include "get_from_github.hpp"

#include "../imgui-stdlib/imgui_stdlib.h"

void addPlots(const char *name, std::function<void(double)> axesSetup) {
std::vector<AxisInfo> getDeviceInfos() {
std::vector<AxisInfo> axisInfos;
std::vector<Omniscope::Id> samplerDvcs; // store live devices
std::vector<std::pair<std::string, ImAxis_>> assignedEgus;
if (sampler.has_value())
for (auto const &device : sampler->sampleDevices) {
// TODO replace ADC counts with language variable
std::string egu = device.first->getEgu().value_or("ADC counts");
auto id = device.first->getId();
if (id.has_value()) {
auto deviceId = id.value();
samplerDvcs.push_back(deviceId);
std::string timebase{std::to_string(deviceId.sampleRate)};
if (captureData.find(deviceId) != captureData.end()) {
auto eguIterator = std::ranges::find(
assignedEgus, egu, &std::pair<std::string, ImAxis_>::first);
if (eguIterator == assignedEgus.end()) {
if (assignedEgus.size() <= 3) {
ImAxis_ nextYAxis =
static_cast<ImAxis_>(ImAxis_Y1 + assignedEgus.size());
assignedEgus.push_back(std::make_pair(egu, nextYAxis));
eguIterator = (assignedEgus.end() - 1);
} else {
fmt::print("too many Axes added, egu not added: "
"{}\nDevice id: {}",
egu, id.value());
break;
}
}
AxisInfo axisInfo{
std::make_pair(deviceId, std::ref(captureData[deviceId])),
*eguIterator, timebase};
axisInfos.push_back(axisInfo);
}
} else
fmt::println("Error no device id found");
}
// also add loaded files into plotAxes
for (auto &[device, values] : captureData)
if (std::ranges::find(samplerDvcs, device.serial, &Omniscope::Id::serial) ==
samplerDvcs.end()) {
axisInfos.push_back({{device, values},
{"y [Volts]", ImAxis_Y1},
std::to_string(device.sampleRate)});
}
return axisInfos;
}

void addPlots(const char *name, bool const flagPaused,
std::function<void(double, std::string, ImAxis_, double, double)>
axesSetup) {
static std::set<std::string> firstRun;
const auto &plots{captureData};
auto const plotRegion = ImGui::GetContentRegionAvail();
// TODO search devices must work aswell
plotAxes = getDeviceInfos();

if (ImPlot::BeginPlot(name, plotRegion, ImPlotFlags_NoFrame)) {
double x_min = std::numeric_limits<double>::max();
double x_max = std::numeric_limits<double>::min();
for (auto const &plot : plots)
if (!plot.second.empty()) {
x_min = std::min(x_min, plot.second.front().first);
x_max = std::max(x_max, plot.second.back().first);

for (auto const &axes : plotAxes) {
// fmt::print("data size:{}, egu: {}\n", axes.data.second.size(),
// axes.egu.first);
if (!axes.data.second.empty()) {
x_max = std::max(x_max, axes.data.second.back().first);
// TODO save max and min value over same axis
auto [min, max] = std::minmax_element(axes.data.second.begin(),
axes.data.second.end());
double yMin = min->first + (min->first * 0.15);
double yMax = max->second + (max->second * 0.15);
// fmt::print("yMin {}, yMax{}\n", yMin, yMax);
axesSetup(x_max, axes.egu.first, axes.egu.second, yMin, yMax);
}
}

axesSetup(x_max);
auto const limits = [&]() {
if (!firstRun.contains(name)) {
firstRun.insert(name);
return ImPlotRect(x_min, x_max, 0, 0);
}
return ImPlot::GetPlotLimits();
}();
auto addPlot = [&](auto const &plot) {

auto addPlot = [&](auto const &plot, ImAxis_ yAxis) {
if (!plot.second.empty()) {
auto const start = [&]() {
auto p = std::lower_bound(plot.second.begin(), plot.second.end(),
Expand All @@ -51,32 +118,53 @@ void addPlots(const char *name, std::function<void(double)> axesSetup) {
return static_cast<std::size_t>(s);
}();

// determine which axes is the right one to choose
ImPlot::SetAxes(ImAxis_X1, yAxis);
ImPlot::PlotLine(
fmt::format("{}-{}", plot.first.type, plot.first.serial).c_str(),
std::addressof(start->first), std::addressof(start->second),
static_cast<std::size_t>(std::distance(start, end)) / stride, 0, 0,
2 * sizeof(double) * stride);
}
};

for (auto const &plot : plots) {
ImPlot::SetNextLineStyle(ImVec4{colorMap[plot.first][0],
colorMap[plot.first][1],
colorMap[plot.first][2], 1.0f});
addPlot(plot);
for (int count = 0; auto const &plot : plotAxes) {
ImPlot::SetNextLineStyle(ImVec4{colorMap[plot.data.first][0],
colorMap[plot.data.first][1],
colorMap[plot.data.first][2], 1.0f});
addPlot(plot.data, plot.egu.second);
}

ImPlot::EndPlot();
}
}

void parseDeviceMetaData(Omniscope::MetaData metaData,
std::shared_ptr<OmniscopeDevice> &device) {
try {
nlohmann::json metaJson = nlohmann::json::parse(metaData.data);
fmt::print("{}\n", metaJson.dump());
device->setScale(std::stod(metaJson["scale"].dump()));
device->setOffset(std::stod(metaJson["offset"].dump()));
device->setEgu(metaJson["egu"]);
} catch (...) {
fmt::print("parsing Meta Data error: {}", metaData.data);
}
}

void initDevices() {
constexpr int VID = 0x2e8au;
constexpr int PID = 0x000au;

devices = deviceManager.getDevices(VID, PID);
for (auto &device : devices) {
auto metaDataCb = [&](auto const &msg) {
if (std::holds_alternative<Omniscope::MetaData>(msg)) {
parseDeviceMetaData(std::get<Omniscope::MetaData>(msg), device);
}
};
auto id = device->getId().value();
auto sampleRate = static_cast<double>(id.sampleRate);
device->setTimeScale(static_cast<double>(1 / sampleRate));
if (!colorMap.contains(id)) {
ImPlot::PushColormap(ImPlotColormap_Dark);
auto c = ImPlot::GetColormapColor((colorMap.size() % 7) + 1);
Expand All @@ -87,10 +175,13 @@ void initDevices() {
device->send(Omniscope::SetRgb{static_cast<std::uint8_t>(color[0] * 255),
static_cast<std::uint8_t>(color[1] * 255),
static_cast<std::uint8_t>(color[2] * 255)});
// set Callback for MetaData
device->setMessageCallback(metaDataCb);
device->send(Omniscope::GetMetaData{});
}
}

void devicesList() {
void devicesList(bool const &flagPaused) {
auto doDevice = [&](auto &device, auto msg) {
auto &color = colorMap[device->getId().value()];
if (ImGui::ColorEdit3(
Expand Down Expand Up @@ -123,8 +214,13 @@ void devicesList() {
};

if (sampler.has_value())
for (auto &device : sampler->sampleDevices)
doDevice(device.first, appLanguage[Key::Measurement]);
for (auto &device : sampler->sampleDevices) {
if (!flagPaused) {
doDevice(device.first, appLanguage[Key::Measurement]);
} else {
doDevice(device.first, appLanguage[Key::Stop]);
}
}
else
for (auto &device : devices)
doDevice(device, appLanguage[Key::Ready]);
Expand Down Expand Up @@ -154,14 +250,17 @@ void load_files(decltype(captureData) &loadedFiles,
if (i == 4) // fifth element (serial of scope)
loadedFile.first.serial = substr;
}
// each y-value is recorded at 1/sampleRate time
double step{0.00001}, base{step};
size_t indx{2}; // y_values start from line 2 of the file
while (!readfile.eof()) { // fill the vector of the values
double value{};
readfile >> value;
loadedFile.second.emplace_back(indx++, value);
loadedFile.second.emplace_back(base, value);
static constexpr size_t bigNumber{10'000'000};
readfile.ignore(bigNumber,
'\n'); // new line separator between elements
base += step;
}
readfile.close();
loadedFile.second.pop_back(); // pop the extra last element
Expand Down Expand Up @@ -253,6 +352,7 @@ void rstSettings(const decltype(captureData) &loadedFiles) {
devices.clear();
savedFileNames.clear();
deviceManager.clearDevices();
plotAxes.clear();
// erase all elements excpet loadedFiles
for (auto it = captureData.begin(); it != captureData.end();) {
if (!loadedFiles.contains(it->first))
Expand Down
22 changes: 20 additions & 2 deletions src/handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
#include <nlohmann/json.hpp>
#include <nlohmann/json_fwd.hpp>
#include <set>
#include <implot.h>

struct AxisInfo {
std::pair<Omniscope::Id, std::vector<std::pair<double, double>> &> data;
std::pair<std::string, ImAxis_> egu;
std::string timebase;

AxisInfo(
std::pair<Omniscope::Id, std::vector<std::pair<double, double>> &> data_,
std::pair<std::string, ImAxis_> egu_, std::string timebase_)
: data{data_}, egu{egu_}, timebase{timebase_} {}
};

// global variables
inline OmniscopeDeviceManager deviceManager{};
Expand All @@ -16,9 +28,15 @@ inline std::set<std::string> savedFileNames; // unique and ordered filenames
inline std::optional<OmniscopeSampler> sampler{};
inline std::map<Omniscope::Id, std::vector<std::pair<double, double>>>
captureData;
void addPlots(const char *, std::function<void(double)>);

inline std::vector<AxisInfo> plotAxes;
void addPlots(
const char *, const bool,
std::function<void(double, std::string, ImAxis_, double, double)>);
void parseDeviceMetaData(Omniscope::MetaData,
std::shared_ptr<OmniscopeDevice> &);
void initDevices();
void devicesList();
void devicesList(bool const &flagPaused);
void load_files(decltype(captureData) &, std::map<Omniscope::Id, std::string> &,
bool &);
void set_config(const std::string &);
Expand Down
Loading

0 comments on commit 99bbf01

Please sign in to comment.