diff --git a/.gitignore b/.gitignore index 9c562a6..b23a6a3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ build/ +openocd/ imgui.ini stmdspgui stmdspgui.exe diff --git a/openocd.cfg b/openocd.cfg new file mode 100644 index 0000000..33f2c52 --- /dev/null +++ b/openocd.cfg @@ -0,0 +1,2 @@ +source [find interface/stlink.cfg] +source [find target/stm32l4x.cfg] \ No newline at end of file diff --git a/source/code.cpp b/source/code.cpp index 8e3bd6c..f24bb5c 100644 --- a/source/code.cpp +++ b/source/code.cpp @@ -2,13 +2,14 @@ * @file code.cpp * @brief Functionality for compiling and disassembling source code. * - * Copyright (C) 2021 Clyne Sullivan + * Copyright (C) 2022 Clyne Sullivan * * Distributed under the GNU GPL v3 or later. You should have received a copy of * the GNU General Public License along with this program. * If not, see . */ +#include "main.hpp" #include "stmdsp.hpp" #include "stmdsp_code.hpp" @@ -20,20 +21,32 @@ #include extern std::shared_ptr m_device; -void log(const std::string& str); - -std::ifstream compileOpenBinaryFile(); -void compileEditorCode(const std::string& code); -void disassembleCode(); +// Stores the temporary file name currently used for compiling the algorithm. static std::string tempFileName; + +/** + * Generates a new temporary file name. + * @return A string containing the path and file name. + */ static std::string newTempFileName(); -static bool codeExecuteCommand( - const std::string& command, - const std::string& file); -static void stringReplaceAll( - std::string& str, - const std::string& what, + +/** + * Executes the given command using system(), collecting the text output in the + * given file. + * @param command The command to be executed. + * @param file The file to write command output to. + * @return True if the command was successful (i.e. returned zero). + */ +static bool codeExecuteCommand(const std::string& command, const std::string& file); + +/** + * Does an in-place replacement of all occurances of "what" with "with". + * @param str The text string to operate on. + * @param what The text to search for. + * @param with The text that will replace occurances of "what". + */ +static void stringReplaceAll(std::string& str, const std::string& what, const std::string& with); std::ifstream compileOpenBinaryFile() diff --git a/source/code.hpp b/source/code.hpp new file mode 100644 index 0000000..edb46e7 --- /dev/null +++ b/source/code.hpp @@ -0,0 +1,39 @@ +/** + * @file code.hpp + * @brief Functionality for compiling and disassembling source code. + * + * Copyright (C) 2022 Clyne Sullivan + * + * Distributed under the GNU GPL v3 or later. You should have received a copy of + * the GNU General Public License along with this program. + * If not, see . + */ + +#ifndef STMDSPGUI_CODE_HPP +#define STMDSPGUI_CODE_HPP + +#include +#include +#include + +/** + * Attempts to open the most recently created binary file. + * @return An opened stream of the file if it exists, an empty stream otherwise. + */ +std::ifstream compileOpenBinaryFile(); + +/** + * Attempts to compile the given C++ algorithm code into a binary. + * Errors are reported to the log view. + * @param code The C++ code for the algorithm (usually from the text editor). + */ +void compileEditorCode(const std::string& code); + +/** + * Disassembles the most recently compiled binary, outputting the results to + * the log view. + */ +void disassembleCode(); + +#endif // STMDSPGUI_CODE_HPP + diff --git a/source/gui_code.cpp b/source/gui_code.cpp index 50fd0c8..b797e51 100644 --- a/source/gui_code.cpp +++ b/source/gui_code.cpp @@ -9,6 +9,7 @@ * If not, see . */ +#include "code.hpp" #include "imgui.h" #include "backends/imgui_impl_sdl.h" #include "backends/imgui_impl_opengl2.h" @@ -16,9 +17,6 @@ #include -extern void compileEditorCode(const std::string& code); -extern void disassembleCode(); - TextEditor editor; // file.cpp static std::string editorCompiled; diff --git a/source/gui_help.cpp b/source/gui_help.cpp new file mode 100644 index 0000000..b60387b --- /dev/null +++ b/source/gui_help.cpp @@ -0,0 +1,124 @@ +/** + * @file gui_help.cpp + * @brief Defines the "Help" menu and provides its functionality. + * + * Copyright (C) 2022 Clyne Sullivan + * + * Distributed under the GNU GPL v3 or later. You should have received a copy of + * the GNU General Public License along with this program. + * If not, see . + */ + +#include "imgui.h" +#include "ImGuiFileDialog.h" + +#include +#include +#include +#include + +void log(const std::string& str); + +static bool showDownloadFirmware = false; +static bool showHelp = false; +static std::string firmwareFile; + +static void helpDownloadThread(); + +void helpRenderMenu() +{ + if (ImGui::BeginMenu("Help")) { + if (ImGui::MenuItem("Open wiki...")) { +#ifdef STMDSP_WIN32 + system("start " +#else + system("xdg-open " +#endif + "https://code.bitgloo.com/clyne/stmdspgui/wiki"); + } + + if (ImGui::MenuItem("Download firmware...")) { + showDownloadFirmware = true; + } + + ImGui::Separator(); + if (ImGui::MenuItem("About")) { + showHelp = true; + } + + ImGui::EndMenu(); + } +} + +void helpRenderDialog() +{ + if (showDownloadFirmware) { + showDownloadFirmware = false; + ImGuiFileDialog::Instance()->OpenModal( + "ChooseFileFW", "Choose Firmware File", ".hex", "."); + } + + if (ImGuiFileDialog::Instance()->Display("ChooseFileFW", + ImGuiWindowFlags_NoCollapse, + ImVec2(460, 540))) + { + if (ImGuiFileDialog::Instance()->IsOk()) { + firmwareFile = ImGuiFileDialog::Instance()->GetFilePathName(); +#ifdef STMDSP_WIN32 + size_t i = 0; + while ((i = firmwareFile.find('\\', i)) != std::string::npos) { + firmwareFile.replace(i, 1, "\\\\"); + i += 2; + } +#endif + + std::thread(helpDownloadThread).detach(); + } + + ImGuiFileDialog::Instance()->Close(); + } + + if (showHelp) { + ImGui::Begin("help"); + + ImGui::Text("stmdspgui\nCompiled on " __DATE__ ".\n\nWritten by Clyne Sullivan.\n"); + + if (ImGui::Button("Close")) { + showHelp = false; + } + + ImGui::End(); + } + + if (!firmwareFile.empty()) { + ImGui::Begin("Downloading"); + + ImGui::Text("Downloading firmware to device..."); + + ImGui::End(); + } +} + +void helpDownloadThread() +{ + std::string command ( +#ifdef STMDSP_WIN32 + "openocd\\bin\\openocd.exe" +#else + "openocd" +#endif + " -f openocd.cfg -c \"program $0 reset exit\""); + + command.replace(command.find("$0"), 2, firmwareFile); + + std::cout << "Run: " << command << std::endl; + + if (system(command.c_str()) == 0) { + log("Programming finished."); + } else { + log("Error while programming device!"); + } + + firmwareFile.clear(); +} + diff --git a/source/gui_help.hpp b/source/gui_help.hpp new file mode 100644 index 0000000..89c00ec --- /dev/null +++ b/source/gui_help.hpp @@ -0,0 +1,19 @@ +/** + * @file gui_help.hpp + * @brief Defines the "Help" menu and provides its functionality. + * + * Copyright (C) 2022 Clyne Sullivan + * + * Distributed under the GNU GPL v3 or later. You should have received a copy of + * the GNU General Public License along with this program. + * If not, see . + */ + +#ifndef STMDSPGUI_GUI_HELP +#define STMDSPGUI_GUI_HELP + +void helpRenderMenu(); +void helpRenderDialog(); + +#endif // STMDSPGUI_GUI_HELP + diff --git a/source/main.cpp b/source/main.cpp index b5514e4..a624499 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -2,7 +2,7 @@ * @file main.cpp * @brief Program entry point and main loop. * - * Copyright (C) 2021 Clyne Sullivan + * Copyright (C) 2022 Clyne Sullivan * * Distributed under the GNU GPL v3 or later. You should have received a copy of * the GNU General Public License along with this program. @@ -13,7 +13,9 @@ #include "backends/imgui_impl_sdl.h" #include "backends/imgui_impl_opengl2.h" +#include "gui_help.hpp" #include "logview.h" +#include "main.hpp" #include "stmdsp.hpp" #include @@ -38,8 +40,6 @@ bool guiHandleEvents(); void guiShutdown(); void guiRender(); -void log(const std::string& str); - static LogView logView; static ImFont *fontSans = nullptr; static ImFont *fontMono = nullptr; @@ -90,8 +90,6 @@ void log(const std::string& str) template void renderWindow() { - static bool showHelp = false; - // Start the new window frame and render the menu bar. ImGui_ImplOpenGL2_NewFrame(); ImGui_ImplSDL2_NewFrame(); @@ -101,24 +99,7 @@ void renderWindow() fileRenderMenu(); deviceRenderMenu(); codeRenderMenu(); - - if (ImGui::BeginMenu("Help")) { - if (ImGui::MenuItem("Open wiki...")) { -#ifdef STMDSP_WIN32 - system("start " -#else - system("xdg-open " -#endif - "https://code.bitgloo.com/clyne/stmdspgui/wiki"); - } - - ImGui::Separator(); - if (ImGui::MenuItem("About")) { - showHelp = true; - } - - ImGui::EndMenu(); - } + helpRenderMenu(); ImGui::EndMainMenuBar(); } @@ -142,6 +123,7 @@ void renderWindow() codeRenderToolbar(); deviceRenderToolbar(); fileRenderDialog(); + helpRenderDialog(); deviceRenderWidgets(); ImGui::PopFont(); @@ -160,18 +142,6 @@ void renderWindow() deviceRenderDraw(); - if (showHelp) { - ImGui::Begin("help"); - - ImGui::Text("stmdspgui\nCompiled on " __DATE__ ".\n\nWritten by Clyne Sullivan.\n"); - - if (ImGui::Button("Close")) { - showHelp = false; - } - - ImGui::End(); - } - // Draw everything to the screen. guiRender(); } diff --git a/source/main.hpp b/source/main.hpp new file mode 100644 index 0000000..921d1ef --- /dev/null +++ b/source/main.hpp @@ -0,0 +1,20 @@ +/** + * @file main.hpp + * @brief Common functions. + * + * Copyright (C) 2022 Clyne Sullivan + * + * Distributed under the GNU GPL v3 or later. You should have received a copy of + * the GNU General Public License along with this program. + * If not, see . + */ + +#ifndef STMDSPGUI_MAIN_HPP +#define STMDSPGUI_MAIN_HPP + +#include + +void log(const std::string& str); + +#endif // STMDSPGUI_MAIN_HPP + diff --git a/source/stmdsp/stmdsp.cpp b/source/stmdsp/stmdsp.cpp index 294e98f..c50845f 100644 --- a/source/stmdsp/stmdsp.cpp +++ b/source/stmdsp/stmdsp.cpp @@ -14,6 +14,7 @@ #include #include +#include extern void log(const std::string& str);