aboutsummaryrefslogtreecommitdiffstats
path: root/source/gui_help.cpp
blob: a17d9fa470941a5ccc7845b67a639c994e1f9675 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "imgui.h"
#include "ImGuiFileDialog.h"

#include <cstdlib>
#include <iostream>
#include <string>
#include <thread>

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();
}