Skip to content

Commit

Permalink
Move ConsoleHandler function to handler.cpp and clean up main.cpp
Browse files Browse the repository at this point in the history
- Moved the 'consoleHandler' function from 'main.cpp' to 'handler.cpp' to improve modularity
- Update 'handler.hpp' to include the declaration of the 'consoleHandler' function
- The definition of the 'consoleHandler' function and the associated comments are removed from the 'main.cpp' file.

These changes improve code organization by placing the 'consoleHandler' function alongside other related functions in 'handler.cpp'.
  • Loading branch information
arkadiusz committed Jul 22, 2024
1 parent 804a121 commit 51adb6f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 66 deletions.
65 changes: 65 additions & 0 deletions src/handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <implot.h>
#include "get_from_github.hpp"
#include "../imgui-stdlib/imgui_stdlib.h"
#include <iostream>
#include <thread>

void addPlots(const char *name, std::function<void(double)> axesSetup) {
static std::set<std::string> firstRun;
Expand Down Expand Up @@ -154,3 +156,66 @@ void set_inital_config(nlohmann::json &config) {
appLanguage =
config["text"]["active_language"] == "German" ? germanLan : englishLan;
}

void consoleHandler(bool &flagInitState, nlohmann::json &config, bool &flagPaused, std::set<std::string>& selected_serials) {
std::string input;
while (true) {
std::cout << "Enter command: ";
std::getline(std::cin, input);
if (input == "Search") {
devices.clear();
deviceManager.clearDevices();
initDevices();
if (flagInitState) {
set_inital_config(config);
flagInitState = false;
}
if (!devices.empty()) {
std::cout << "Enter the device number: ";
std::getline(std::cin, input);
selected_serials.clear();
size_t pos = 0;
while ((pos = input.find(',')) != std::string::npos) {
selected_serials.insert(input.substr(0, pos));
input.erase(0, pos + 1);
}
selected_serials.insert(input);
}
else {
std::cout << "No devices fpound.\n";
}
}
else if (input == "Start") {
if (!devices.empty() && flagPaused) {
if (!sampler.has_value()) {
sampler.emplace(deviceManager, std::move(devices));
flagPaused = false;
}
}
}
else if (input == "Stop") {
if (!flagPaused) {
flagPaused = true;
for (auto &device : sampler->sampleDevices) {
device.first->send(Omniscope::Stop{});
}
}
}
else if (input == "Continue") {
if (flagPaused && sampler.has_value()) {
flagPaused = false;
for (auto &device : sampler->sampleDevices) {
device.first->send(Omniscope::Start{});
}
}
}
else if (input == "Reset") {
if (flagPaused && sampler.has_value()) {
sampler.reset();
devices.clear();
deviceManager.clearDevices();
flagPaused = true;
}
}
}
}
6 changes: 4 additions & 2 deletions src/handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ inline std::map<Omniscope::Id, std::array<float, 3>> colorMap;
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)>);
void initDevices();
void devicesList();
void load_files(decltype(captureData) &, std::map<Omniscope::Id, std::string> &,
bool &);
void load_files(decltype(captureData) &, std::map<Omniscope::Id, std::string> &, bool &);
void set_config(const std::string &);
void set_json(nlohmann::json &);
void set_inital_config(nlohmann::json &);

void consoleHandler(bool &flagInitState, nlohmann::json &config, bool &flagPaused, std::set<std::string>& selected_serials);

#endif
64 changes: 0 additions & 64 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,6 @@
#include "jasonhandler.hpp"
#include "websockethandler.hpp"

// Separate function to handle console input
void consoleHandler(bool &flagInitState, nlohmann::json &config, bool &flagPaused, std::set<std::string>& selected_serials) {
std::string input;
while (true) {
std::cout << "Enter command: ";
std::getline(std::cin, input);
if (input == "Search Device") {
devices.clear();
deviceManager.clearDevices();
initDevices();
if (flagInitState) {
set_inital_config(config);
flagInitState = false;
}
if (!devices.empty()) {
std::cout << "Enter the serial number of the device to select: ";
std::getline(std::cin, input);
selected_serials.clear();
size_t pos = 0;
while ((pos = input.find(',')) != std::string::npos) {
selected_serials.insert(input.substr(0, pos));
input.erase(0, pos + 1);
}
selected_serials.insert(input);
}
else {
std::cout << "No devices found.\n";
}
}
else if (input == "Start") {
if (!devices.empty() && flagPaused) {
if (!sampler.has_value()) {
sampler.emplace(deviceManager, std::move(devices));
flagPaused = false;
}
}
}
else if (input == "Stop") {
if (!flagPaused) {
flagPaused = true;
for (auto &device : sampler->sampleDevices) {
device.first->send(Omniscope::Stop{});
}
}
}
else if (input == "Continue") {
if (flagPaused && sampler.has_value()) {
flagPaused = false;
for (auto &device : sampler->sampleDevices) {
device.first->send(Omniscope::Start{});
}
}
}
else if (input == "Reset") {
if (flagPaused && sampler.has_value()) {
sampler.reset();
devices.clear();
deviceManager.clearDevices();
flagPaused = true;
}
}
}
}

int main() {

const std::string configpath = "config/config.json";
Expand Down

0 comments on commit 51adb6f

Please sign in to comment.