Skip to content

Commit

Permalink
Merge pull request #2 from klonyyy/devel
Browse files Browse the repository at this point in the history
Refactor + bugfix
  • Loading branch information
klonyyy authored Jul 2, 2023
2 parents f3d976a + 70bf7ee commit befc31c
Show file tree
Hide file tree
Showing 197 changed files with 27,296 additions and 362 deletions.
36 changes: 25 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ endif()

project(STMViewer)

set(STMVIEWER_VERSION 0.1.1)
set(STMVIEWER_VERSION 0.1.2)

set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_STANDARD 17)
Expand All @@ -34,6 +34,7 @@ if(UNIX)
find_package(libusb REQUIRED)
find_package(glfw3 REQUIRED)
set(STLINK_LINUX ${CMAKE_CURRENT_SOURCE_DIR}/third_party/stlink/lib/linux/libstlink.a)
set(SPDLOG_LINUX ${CMAKE_CURRENT_SOURCE_DIR}/third_party/spdlog/lib/linux/libspdlog.so.1.11.0)
set(INSTALL_PATH /usr/local/STMViewer)
set(DESKTOP_FILE_PATH /usr/share/applications)
endif()
Expand All @@ -45,18 +46,23 @@ if(WIN32)
set(STLINK_WINDOWS ${CMAKE_CURRENT_SOURCE_DIR}/third_party/stlink/lib/windows/libstlink.a)
set(LIBUSB_WINDOWS ${CMAKE_CURRENT_SOURCE_DIR}/third_party/libusb/lib/windows/libusb-1.0.dll)
set(LIBUSB_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/libusb/inc/libusb-1.0)
set(SPDLOG_WINDOWS ${CMAKE_CURRENT_SOURCE_DIR}/third_party/spdlog/lib/windows/libspdlog.dll)
set(INSTALL_PATH bin)
endif()



set(PROJECT_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/ElfReader/ElfReader.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Gui/Gui.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/VarReader/VarReader.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/TargetMemoryHandler/TargetMemoryHandler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/TargetMemoryHandler/StlinkHandler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Plot/Plot.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Variable/Variable.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/ConfigHandler/ConfigHandler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Plot/PlotHandler.cpp)
${CMAKE_CURRENT_SOURCE_DIR}/src/Plot/PlotHandler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/FileHandler/NFDFileHandler.cpp)

set(IMGUI_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/third_party/imgui/imgui.cpp
Expand Down Expand Up @@ -88,12 +94,13 @@ target_compile_definitions(${EXECUTABLE}
target_include_directories(${EXECUTABLE} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/ElfReader
${CMAKE_CURRENT_SOURCE_DIR}/src/Gui
${CMAKE_CURRENT_SOURCE_DIR}/src/VarReader
${CMAKE_CURRENT_SOURCE_DIR}/src/TargetMemoryHandler
${CMAKE_CURRENT_SOURCE_DIR}/src/Plot
${CMAKE_CURRENT_SOURCE_DIR}/src/ScrollingBuffer
${CMAKE_CURRENT_SOURCE_DIR}/src/Variable
${CMAKE_CURRENT_SOURCE_DIR}/src/ConfigHandler
${CMAKE_CURRENT_SOURCE_DIR}/src/ImguiPlugins)
${CMAKE_CURRENT_SOURCE_DIR}/src/ImguiPlugins
${CMAKE_CURRENT_SOURCE_DIR}/src/FileHandler)

target_include_directories(${EXECUTABLE} SYSTEM PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/third_party/stlink/inc
Expand All @@ -104,12 +111,13 @@ target_include_directories(${EXECUTABLE} SYSTEM PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/third_party/GLFW/inc/GLFW/
${CMAKE_CURRENT_SOURCE_DIR}/third_party/implot/
${CMAKE_CURRENT_SOURCE_DIR}/third_party/mINI/src/mini/
${CMAKE_CURRENT_SOURCE_DIR}/third_party/nfd/src/include/)
${CMAKE_CURRENT_SOURCE_DIR}/third_party/nfd/src/include/
${CMAKE_CURRENT_SOURCE_DIR}/third_party/spdlog/inc/)

if(UNIX)
target_link_libraries(${EXECUTABLE} ${STLINK_LINUX} ${LIBUSB_LIBRARY} pthread dl GL glfw nfd)
target_link_libraries(${EXECUTABLE} ${STLINK_LINUX} ${LIBUSB_LIBRARY} ${SPDLOG_LINUX} pthread dl GL glfw nfd)
elseif(WIN32)
target_link_libraries(${EXECUTABLE} ${GLFW3_WINDOWS} ${STLINK_WINDOWS} ${LIBUSB_WINDOWS} -static opengl32 nfd -static-libstdc++ -static-libgcc)
target_link_libraries(${EXECUTABLE} ${GLFW3_WINDOWS} ${STLINK_WINDOWS} ${LIBUSB_WINDOWS} ${SPDLOG_WINDOWS} -static opengl32 nfd -static-libstdc++ -static-libgcc)
endif()

if(WIN32)
Expand All @@ -133,21 +141,27 @@ install(TARGETS ${EXECUTABLE} RUNTIME DESTINATION ${INSTALL_PATH} COMPONENT appl
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/imgui.ini DESTINATION ${INSTALL_PATH})

if(WIN32)
set_target_properties(${EXECUTABLE} PROPERTIES WIN32_EXECUTABLE TRUE)
if(PRODUCTION)
set_target_properties(${EXECUTABLE} PROPERTIES WIN32_EXECUTABLE TRUE)
endif()
install(FILES ${LIBUSB_WINDOWS} ${STLINK_WINDOWS} ${GLFW3_WINDOWS} DESTINATION ${INSTALL_PATH})
set(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "
SetOutPath \\\"$INSTDIR\\\\bin\\\"
CreateShortCut \\\"$SMPROGRAMS\\\\$STARTMENU_FOLDER\\\\${EXECUTABLE}.lnk\\\" \\\"$INSTDIR\\\\bin\\\\${EXECUTABLE}.exe\\\"
CreateShortCut \\\"$DESKTOP\\\\${EXECUTABLE}.lnk\\\" \\\"$INSTDIR\\\\bin\\\\${EXECUTABLE}.exe\\\"")
set(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "Delete \\\"$DESKTOP\\\\${EXECUTABLE}.lnk\\\"")
set(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "
Delete \\\"$SMPROGRAMS\\\\$START_MENU\\\\${EXECUTABLE}.lnk\\\"
Delete \\\"$DESKTOP\\\\${EXECUTABLE}.lnk\\\"")
endif()

if(UNIX)
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/third_party/stlink/chips DESTINATION ${INSTALL_PATH})
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/launch/icon.png DESTINATION ${INSTALL_PATH})
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/launch/STMViewer.desktop
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
DESTINATION ${DESKTOP_FILE_PATH})
set(CPACK_GENERATOR "DEB")
set(CPACK_DEBIAN_PACKAGE_DEPENDS "libusb-1.0-0-dev, libglfw3-dev, libgtk-3-dev, gdb")
set(CPACK_DEBIAN_PACKAGE_DEPENDS "libusb-1.0-0-dev, libglfw3-dev, libgtk-3-dev, gdb, libspdlog-dev")
endif()

set(CPACK_PACKAGE_NAME ${PROJECT_NAME})
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ I'm working in the motor control industry where it is crucial to visualize some
3. [implot](https://github.com/epezent/implot)
4. [mINI](https://github.com/pulzed/mINI)
5. [nfd](https://github.com/btzy/nativefiledialog-extended)
6. [spdlog](https://github.com/gabime/spdlog)

3 changes: 3 additions & 0 deletions launch/addGitVersion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
cd ..
git log --pretty=format:'#define GIT_INFO_PRESENT%n static const char* GIT_HASH = "%H";' -n 1 > src/gitversion.hpp
5 changes: 3 additions & 2 deletions launch/release.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#!/usr/bin/env bash
./launch/addGitVersion.sh
rm -rf build
mkdir -p build
cd build
mkdir packages

mkdir -p windows
cd windows
cmake -DPLATFORM=WIN ../..
cmake -DPLATFORM=WIN -DPRODUCTION=TRUE ../..
make package -j16
cp *win64.exe ../packages
cd -

mkdir -p linux
cd linux
cmake ../..
cmake -DPRODUCTION=TRUE ../..
make package -j16
cp *.deb ../packages
cd -
13 changes: 6 additions & 7 deletions src/ConfigHandler/ConfigHandler.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include "ConfigHandler.hpp"

#include <iostream>
#include <random>

ConfigHandler::ConfigHandler(const std::string& configFilePath, PlotHandler* plotHandler) : configFilePath(configFilePath), plotHandler(plotHandler)
ConfigHandler::ConfigHandler(const std::string& configFilePath, PlotHandler* plotHandler, std::shared_ptr<spdlog::logger> logger) : configFilePath(configFilePath), plotHandler(plotHandler), logger(logger)
{
ini = std::make_unique<mINI::INIStructure>();
file = std::make_unique<mINI::INIFile>(configFilePath);
Expand Down Expand Up @@ -57,13 +56,14 @@ bool ConfigHandler::readConfigFile(std::map<std::string, std::shared_ptr<Variabl
varId++;

if (newVar->getAddress() % 4 != 0)
std::cout << "--------- WARNING: unaligned variable address! ----------" << std::endl;

logger->warn("--------- Unaligned variable address! ----------");

if (!newVar->getName().empty())
{
vars[newVar->getName()] = newVar;
newVar->setIsFound(true);
std::cout << "ADDING VARIABLE: " << newVar->getName() << std::endl;
logger->info("Adding variable: {}", newVar->getName());
}
}

Expand All @@ -85,8 +85,7 @@ bool ConfigHandler::readConfigFile(std::map<std::string, std::shared_ptr<Variabl
plotHandler->addPlot(plotName);
plotHandler->getPlot(plotName)->setVisibility(visibility);
plotHandler->getPlot(plotName)->setType(type);

std::cout << "ADDING PLOT: " << plotName << std::endl;
logger->info("Adding plot: {}", plotName);
uint32_t seriesNumber = 0;
std::string varName = ini->get(plotSeriesFieldFromID(plotNumber, seriesNumber)).get("name");

Expand All @@ -99,7 +98,7 @@ bool ConfigHandler::readConfigFile(std::map<std::string, std::shared_ptr<Variabl
if (displayFormat == "")
displayFormat = "DEC";
plotHandler->getPlot(plotName)->getSeries(varName)->format = displayFormatMap.at(displayFormat);
std::cout << "ADDING SERIES: " << varName << std::endl;
logger->info("Adding series: {}", varName);
seriesNumber++;
varName = ini->get(plotSeriesFieldFromID(plotNumber, seriesNumber)).get("name");
}
Expand Down
4 changes: 3 additions & 1 deletion src/ConfigHandler/ConfigHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "PlotHandler.hpp"
#include "Variable.hpp"
#include "ini.h"
#include "spdlog/spdlog.h"

class ConfigHandler
{
Expand All @@ -19,7 +20,7 @@ class ConfigHandler
uint32_t maxViewportPoints = 5000;
} Settings;

ConfigHandler(const std::string& configFilePath, PlotHandler* plotHandler);
ConfigHandler(const std::string& configFilePath, PlotHandler* plotHandler, std::shared_ptr<spdlog::logger> logger);
~ConfigHandler() = default;

bool changeConfigFile(const std::string& newConfigFilePath);
Expand All @@ -34,6 +35,7 @@ class ConfigHandler

std::unique_ptr<mINI::INIFile> file;
std::unique_ptr<mINI::INIStructure> ini;
std::shared_ptr<spdlog::logger> logger;
};

#endif
10 changes: 5 additions & 5 deletions src/ElfReader/ElfReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#define _UNIX
#endif

ElfReader::ElfReader(std::string& filename) : elfname(filename)
ElfReader::ElfReader(std::string& filename, std::shared_ptr<spdlog::logger> logger) : elfname(filename), logger(logger)
{
}

Expand All @@ -35,7 +35,7 @@ bool ElfReader::updateVariableMap(std::map<std::string, std::shared_ptr<Variable
{
out += executeCommand(cmdFull.c_str());
cmdFull = startCmd;
std::cout << "Dividing command into smaller chunks." << std::endl;
logger->info("Dividing command into smaller chunks...");
}
}
out += executeCommand(cmdFull.c_str());
Expand Down Expand Up @@ -64,9 +64,9 @@ bool ElfReader::updateVariableMap(std::map<std::string, std::shared_ptr<Variable
vars.at(varName)->setAddress(atoi(temp.substr(offset, temp.find('\n', addrPos)).c_str()));
std::string type = temp.substr(typePos + 7, temp.find('\n', typePos));
vars.at(varName)->setType(getTypeFromString(type));
std::cout << "NAME: " << vars.at(varName)->getName() << std::endl;
std::cout << "ADDRESS: " << vars.at(varName)->getAddress() << std::endl;
std::cout << "TYPE: " << static_cast<uint32_t>(vars[varName]->getType()) << std::endl;
logger->info("NAME: {}", vars.at(varName)->getName());
logger->info("ADDRESS: {}", vars.at(varName)->getAddress());
logger->info("TYPE: {}", static_cast<uint32_t>(vars[varName]->getType()));
}
out.erase(0, temp.length());
}
Expand Down
4 changes: 3 additions & 1 deletion src/ElfReader/ElfReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
#include <vector>

#include "Variable.hpp"
#include "spdlog/spdlog.h"

class ElfReader
{
public:
ElfReader(std::string& filename);
ElfReader(std::string& filename, std::shared_ptr<spdlog::logger> logger);

bool updateVariableMap(std::map<std::string, std::shared_ptr<Variable>>& vars);
Variable::type getTypeFromString(const std::string& strType);
Expand All @@ -21,6 +22,7 @@ class ElfReader
static constexpr uint16_t maxGdbCmdLendth = 8160;
std::string& elfname;
std::string executeCommand(const char* cmd);
std::shared_ptr<spdlog::logger> logger;
};

#endif
17 changes: 17 additions & 0 deletions src/FileHandler/IFileHandler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef _FILEHANDLER_HPP
#define _FILEHANDLER_HPP

#include <string>
#include <utility>

class IFileHandler
{
public:
virtual ~IFileHandler() = default;
virtual bool init() = 0;
virtual bool deinit() = 0;
virtual std::string openFile(std::pair<std::string, std::string>&& filterFileNameFileExtension) = 0;
virtual std::string saveFile(std::pair<std::string, std::string>&& filterFileNameFileExtension) = 0;
};

#endif
44 changes: 44 additions & 0 deletions src/FileHandler/NFDFileHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <NFDFileHandler.hpp>
#include <algorithm>

#include "nfd.h"

bool NFDFileHandler::init()
{
return NFD_Init() != NFD_ERROR;
}
bool NFDFileHandler::deinit()
{
NFD_Quit();
return true;
}
std::string NFDFileHandler::openFile(std::pair<std::string, std::string>&& filterFileNameFileExtension)
{
return handleFile(handleType::OPEN, filterFileNameFileExtension);
}
std::string NFDFileHandler::saveFile(std::pair<std::string, std::string>&& filterFileNameFileExtension)
{
return handleFile(handleType::SAVE, filterFileNameFileExtension);
}

std::string NFDFileHandler::handleFile(handleType type, std::pair<std::string, std::string>& filterFileNameFileExtension)
{
nfdchar_t* outPath = nullptr;
nfdfilteritem_t filterItem[1] = {{filterFileNameFileExtension.first.c_str(), filterFileNameFileExtension.second.c_str()}};

nfdresult_t result = NFD_ERROR;

if (type == handleType::SAVE)
result = NFD_SaveDialog(&outPath, filterItem, 1, NULL, NULL);
else if (type == handleType::OPEN)
result = NFD_OpenDialog(&outPath, filterItem, 1, NULL);

if (result == NFD_OKAY)
{
std::string path = std::string(outPath);
std::replace(path.begin(), path.end(), '\\', '/');
NFD_FreePath(outPath);
return path;
}
return std::string("");
}
24 changes: 24 additions & 0 deletions src/FileHandler/NFDFileHandler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef _NFDFILEHANDLER_HPP
#define _NFDFILEHANDLER_HPP

#include <IFileHandler.hpp>

class NFDFileHandler : public IFileHandler
{
public:
bool
init() override;
bool deinit() override;
std::string openFile(std::pair<std::string, std::string>&& filterFileNameFileExtension) override;
std::string saveFile(std::pair<std::string, std::string>&& filterFileNameFileExtension) override;

private:
enum class handleType
{
SAVE,
OPEN
};
std::string handleFile(handleType type, std::pair<std::string, std::string>& filterFileNameFileExtension);
};

#endif
Loading

0 comments on commit befc31c

Please sign in to comment.