File: TestGUI.cpp

package info (click to toggle)
limesuite 22.09.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 17,208 kB
  • sloc: cpp: 157,429; ansic: 6,852; python: 197; sh: 56; makefile: 19
file content (369 lines) | stat: -rw-r--r-- 8,995 bytes parent folder | download | duplicates (4)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#ifdef QUICKTEST_GUI
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Multiline_Output.H>
#include <FL/Fl_Box.H>
#include "FL/fl_draw.H"
#endif
#include <vector>
#include <string>
#include <fstream>

#include "LimeSDRTest.h"
#include <iostream>
#include <getopt.h>

#ifdef QUICKTEST_GUI
#ifdef NDEBUG
#ifdef _MSC_VER
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
#endif
#endif

static Fl_Button** buttons;
static Fl_Window* popup = nullptr;
static Fl_Multiline_Output* out;
static Fl_Output* detect;
static std::string logfile;
static int started = false;
Fl_Button* start;

const Fl_Color CL_DARK = fl_rgb_color(0x18, 0x8A, 0x2E);

class Lms_Double_window : public Fl_Double_Window
{
private:
    int error_msg;
public:
    Lms_Double_window(int W, int H, const char* L = 0) : Fl_Double_Window(W, H, L)
    {
        error_msg = 0;
    };
    void SetErrorMsg(int msg) {
        error_msg = msg;
    }
    void draw()
    {
        Fl_Double_Window::draw();
        if (error_msg != 0)
        {
            fl_rectf(w() / 4, h() / 4, w() / 2, h() / 4,FL_WHITE);
            fl_rect(w() / 4, h() / 4, w() / 2, h() / 4, FL_BLACK);
            fl_color(FL_RED);
            fl_font(FL_HELVETICA, 20);
            if (error_msg == -1)
            {
                fl_draw("No Board Detected", 320, h()/4+50);
                fl_font(FL_HELVETICA, 20);
            }
            else if (error_msg == -2)
            {
                fl_font(FL_HELVETICA, 20);
                fl_draw("Multiple Boards Detected", 295, h() / 4 + 50);
            }
        }
    }
};

Lms_Double_window* win;

class LmsPopupResult : public Fl_Window {
public:
    LmsPopupResult(int X, int Y, int W, int H, bool passed, const char* L=0) :Fl_Window(W, H, L)
    {
        position(X, Y);
        color(FL_WHITE);
        Fl_Button* b = new Fl_Button(W / 2 - 40, H - 50, 80, 30, "OK");
        b->callback(Button_CB, this);
        b->shortcut(FL_Enter);
        Fl_Multiline_Output* out = new Fl_Multiline_Output(64, 15, W - 128, 50);
        out->box(FL_NO_BOX);
        out->textsize(24);
        out->textcolor(passed ? CL_DARK : FL_RED);
        out->value(passed ? "Board Tests PASSED" : "Board Tests FAILED");
        end();
        set_modal();
        show();
    }
    ~LmsPopupResult() {};
private:
    static void Button_CB(Fl_Widget* obj, void* v)
    {
        ((Fl_Window*)v)->hide();
    }
};

class LmsPopupError : public Fl_Window {
    Fl_Output* out;
public:
    LmsPopupError(int W, int H, const char* L = 0) :Fl_Window(W, H, L)
    {
        color(FL_WHITE);
        out = new Fl_Output(64, 15, W - 128, 40);
        out->box(FL_NO_BOX);
        out->textsize(32);
        out->textcolor(FL_RED);
        end();
        set_modal();
    };
    void Update(int X, int Y, const char* msg)
    {
        position(X, Y);
        out->value(msg);
    }
    ~LmsPopupError() {};
};

static LmsPopupError* error_popup = nullptr;

void ShowMessage(void* v)
{
    if (popup != nullptr)
    {
        popup->hide();
        delete popup;
        popup = nullptr;
    }
    bool passed = *((bool*)v);
    popup = new LmsPopupResult(win->x() + win->w() / 4, win->y() + win->h() / 4, win->w() / 2, 120, passed, "");
    while (popup && popup->shown())
        Fl::wait();
    delete (bool*)v;
    delete popup;
    popup = nullptr;
}

void Timer_CB(void *data)
{
    static int counter = 30;
    Fl::repeat_timeout(1.0f / 25.0f, Timer_CB, nullptr);

    if (counter++ == 30)
    {
        counter = 0;
        Fl::lock();
        std::string str;

        win->SetErrorMsg(0);
        int ret;
        str = "";
        if ((ret = LimeSDRTest::CheckDevice(str)) == 0)
        {
            detect->textcolor(CL_DARK);
            if (!started)
                start->activate();
            win->SetErrorMsg(0);
        }
        else
        {
            detect->textcolor(FL_RED);
            if (!started)
                start->deactivate();
            if (popup != nullptr)
                popup->hide();
            win->SetErrorMsg(ret);
        }
        if (str != "")
            detect->value(str.c_str());

        win->redraw();
        win->damage(FL_DAMAGE_ALL, 150, 460, 320, 30);
        Fl::unlock();
    }
}

static void Window_CB(Fl_Widget*, void*)
{
    if (Fl::event() == FL_SHORTCUT && Fl::event_key() == FL_Escape)
        return; // ignore Escape
    exit(0);
}

static void AddLine(const char * str)
{
    std::string val;
    val = out->value();
    val += str;
    val += "\n";
    out->value(val.c_str());
    out->position(val.length());
    if (logfile != "")
    {
        std::ofstream file(logfile, std::ios_base::out | std::ios_base::app);
        if (file.is_open())
        {
            file << str << std::endl;
            file.close();
        }
    }
}

static void DrawButtons(int X, int Y)
{
    buttons = new Fl_Button*[testNames.size()];
    Fl_Group* g = new Fl_Group(X-10,Y-8,240,testNames.size()*55+10,"Board Tests");
    //g->align(FL_ALIGN_TOP_LEFT);
    g->box(FL_ENGRAVED_FRAME);
    for (unsigned i = 0; i < testNames.size(); i++)
    {
        auto button = new Fl_Button(X, Y+i*55, 220, 40,testNames[i].c_str());
        button->box(FL_ROUND_UP_BOX);
        button->deactivate();
        buttons[i] = button;
    }
    g->end();
}

static int CB_Function(int id, int event, const char* msg)
{
    Fl::lock();
    if (event == LMS_TEST_INFO)
    {
        if (id != -1)
        {
            buttons[id]->activate();
            buttons[id]->color(FL_YELLOW);
        }
    }
    else if (event == LMS_TEST_FAIL)
    {
        if (id != -1)
        {
            buttons[id]->activate();
            buttons[id]->color(FL_RED);
        }
        else
        {
            bool* passed = new bool;
            *passed = false;
            started = false;
            Fl::awake(ShowMessage, passed);
        }
    }
    else if (event == LMS_TEST_SUCCESS)
    {
        if (id != -1)
        {
            buttons[id]->activate();
            buttons[id]->color(FL_GREEN);
        }
        else
        {
            bool* passed = new bool;
            *passed = true;
            started = false;
            Fl::awake(ShowMessage, passed);
        }
    }
    else if (event == LMS_TEST_LOGFILE)
    {
        logfile = "LimeSDR-Mini_";
        logfile += msg;
        logfile +=".log";
        std::ofstream file(logfile, std::ios_base::out | std::ios_base::app);
        if (file.is_open())
        {
            file << out->value();
            file.close();
        }
        std::string str = "  Serial Number: ";
        str += msg;
        AddLine(str.c_str());
    }

    if (msg && event != LMS_TEST_LOGFILE)
        AddLine(msg);

    if (id == -1)
    start->activate();

    win->redraw();
    Fl::unlock();
    return 0;
}

static void Start_CB(Fl_Widget*, void*)
{
    out->value("");
    logfile = "";
    for (unsigned i = 0; i < testNames.size(); i++)
    {
        buttons[i]->color(FL_BACKGROUND_COLOR);
        buttons[i]->deactivate();
    }
    started = true;
    if (LimeSDRTest::RunTests(CB_Function) == 0)
        start->deactivate();
}
#endif

static int CB_FunctionCLI(int id, int event, const char* msg)
{
    if (event == LMS_TEST_LOGFILE)
    {
        std::string str = "  Serial Number: ";
        str += msg;
        std::cout << str << std::endl;
    }
    else
        std::cout << msg << std::endl;
    return 0;
}


int main(int argc, char** argv)
{
#ifdef QUICKTEST_GUI
#ifdef __unix
    int gui = 0;
#else
    int gui = 1;
#endif
    struct option long_options[] = {
        {"gui", no_argument, &gui, 1},
        {"no-gui", no_argument, &gui, 0},
        {0, 0, 0, 0}
    };

    int option;
    int option_index = 0;
    while ((option = getopt_long(argc, argv, "", long_options, &option_index)) != -1)
    {
        if (option != 0)
        {
            std::cout << " --gui\t\tenables GUI\n --no-gui\tdisables GUI\n" << std::endl;
            return -1;
        }
    }
    if (gui)
    {
        Fl::scheme("gleam");
        win = new Lms_Double_window(860, 135+testNames.size()*55, "LimeSDR TestApp");
        out = new Fl_Multiline_Output(260, 22, win->w()-270, win->h()-50, "Message Log");
        out->align(FL_ALIGN_TOP_LEFT);
        out->textsize(11);
        start = new Fl_Button(20, win->h()-82, 220, 40, "Start Test");
        start->callback(Start_CB);
        start->shortcut(FL_Enter);
        DrawButtons(20, 30);
        detect = new Fl_Output(10, win->h()-28, 480, 30);
        detect->box(FL_NO_BOX);
        detect->textsize(16);
        win->show();
        win->end();
        error_popup = new LmsPopupError(win->w()/2, win->h()/4,"");
        win->callback(Window_CB);
        Fl::add_timeout(0.1f, Timer_CB, nullptr);
        Fl::lock();
        Fl::run();
        delete win;
        return 0;
    }
#endif
    return LimeSDRTest::RunTests(CB_FunctionCLI, false); //returns bit field of failed tests

}