Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
R-Abbasi committed Sep 17, 2024
1 parent c2145a3 commit 10fc36a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 45 deletions.
82 changes: 43 additions & 39 deletions src/handler.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#include "handler.hpp"
#include "../imgui-stdlib/imgui_stdlib.h"
#include "get_from_github.hpp"
#include "popups.hpp"
#include <functional>
#include <set>
#include <implot.h>
#include "popups.hpp"
#include "get_from_github.hpp"
#include "../imgui-stdlib/imgui_stdlib.h"
#include <set>

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

AxisInfo(
const std::pair<Omniscope::Id, std::vector<std::pair<double, double>> &>
Expand Down Expand Up @@ -39,14 +39,14 @@ static std::vector<AxisInfo> getDeviceInfos() {
static_assert((ImAxis_Y1 + 1) == ImAxis_Y2);
ImAxis_ nextYAxis =
static_cast<ImAxis_>(ImAxis_Y1 + assignedEgus.size());
assignedEgus.push_back({egu, nextYAxis});
assignedEgus.push_back(std::make_pair(egu, nextYAxis));
} else {
fmt::println("too many Axes added, egu not added: "
"{}\nDevice id: {}",
egu, id.value());
fmt::print("too many Axes added, egu not added: "
"{}\nDevice id: {}",
egu, id.value());
break;
}
axisInfos.push_back({{deviceId, captureData[deviceId]},
axisInfos.push_back({{deviceId, std::ref(captureData[deviceId])},
assignedEgus.back(),
std::to_string(deviceId.sampleRate)});
}
Expand All @@ -55,7 +55,8 @@ static std::vector<AxisInfo> getDeviceInfos() {
}
// also get loaded files info
for (auto &[device, values] : captureData)
if (!std::ranges::contains(samplerDvcs, device))
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)});
Expand All @@ -74,16 +75,17 @@ void addPlots(const char *name,
double x_min = std::numeric_limits<double>::max();
double x_max = std::numeric_limits<double>::min();

for (auto const &axes : plotAxes) {
x_max = std::max(x_max, axes.data.second.back().first);
// TODO save max and min value over same axis
// find first and last pairs of values
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);
axesSetup(x_max, axes.egu.first, axes.egu.second, yMin, yMax);
}
for (auto const &axes : plotAxes)
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);
}

auto const limits = [&]() {
if (!firstRun.contains(name)) {
Expand Down Expand Up @@ -132,16 +134,16 @@ void addPlots(const char *name,
}
}

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

Expand All @@ -158,7 +160,7 @@ void initDevices() {
};
auto id = device->getId().value();
auto sampleRate = static_cast<double>(id.sampleRate);
device->setTimeScale(1 / 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 Down Expand Up @@ -209,10 +211,11 @@ void devicesList(bool const &flagPaused) {

if (sampler.has_value())
for (auto &device : sampler->sampleDevices) {
if (!flagPaused)
if (!flagPaused) {
doDevice(device.first, appLanguage[Key::Measurement]);
else
} else {
doDevice(device.first, appLanguage[Key::Stop]);
}
}
else
for (auto &device : devices)
Expand Down Expand Up @@ -245,6 +248,7 @@ void load_files(decltype(captureData) &loadedFiles,
}
// 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;
Expand Down Expand Up @@ -313,21 +317,21 @@ void load_files(decltype(captureData) &loadedFiles,
}
}

void set_config(std::string_view configpath) {
void set_config(const std::string &configpath) {
if (fs::exists(configpath))
fmt::println("Found config.json\r");
fmt::print("found config.json\n\r");
else {
fmt::println("Did not find config.json.\nDownload from Github\r");
fmt::print("Did not find config.json.\n Download from Github\n\r");
update_config_from_github();
}
}

void set_json(const nlohmann::json &config) {
if (fs::exists(load_json<std::string>(config, "languagepath")))
fmt::println("Found language: {}\r", appLanguage[Key::German]);
void set_json(nlohmann::json &config) {
if (fs::exists(load_json<std::string>(config, ("languagepath"))))
fmt::print("Found language: {}\n\r", appLanguage[Key::German]);
else {
fmt::println("Did not find {}.\nDownload from Github\n",
appLanguage[Key::German]);
fmt::print("Did not find {}.\n Download from Github\n\r",
appLanguage[Key::German]);
update_language_from_github();
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ 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, std::string, ImAxis_, double, double)>);
void parseDeviceMetaData(Omniscope::MetaData,
std::shared_ptr<OmniscopeDevice> &);
void initDevices();
void devicesList(bool const &flagPaused);
void load_files(decltype(captureData) &, std::map<Omniscope::Id, std::string> &,
bool &);
void set_config(std::string_view);
void set_json(const nlohmann::json &);
void set_config(const std::string &);
void set_json(nlohmann::json &);
void set_inital_config(nlohmann::json &);
void rstSettings(const decltype(captureData) &);

#endif
#endif
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ int main() {
[flagPaused](double x_max, std::string yLabel, ImAxis_ axis,
double yMin, double yMax) {
ImPlot::SetupLegend(ImPlotLocation_NorthEast);
// auto auxFlagsMeasuring =
// ImPlotAxisFlags_AutoFit | ImPlotAxisFlags_NoGridLines;
// auto auxFlagsPaused = ImPlotAxisFlags_NoGridLines;
// auto auxFlagsMeasuring =
// ImPlotAxisFlags_AutoFit | ImPlotAxisFlags_NoGridLines;
// auto auxFlagsPaused = ImPlotAxisFlags_NoGridLines;
ImPlot::SetupAxisTicks(ImAxis_Y1, -10, 200, 22, nullptr, true);

if (!flagPaused) {
Expand Down

0 comments on commit 10fc36a

Please sign in to comment.