summaryrefslogtreecommitdiff
path: root/modules/xcos/macros/xcos_debug_gui.sci
blob: 7e0ee28f737310db8d3ec9ddf621cf9e550bc023 (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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
function flag=xcos_debug_gui(flag,block)
    // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
    // Copyright (C) 2013 - INRIA - Serge Steer <serge.steer@inria.fr>
    //
    // This file must be used under the terms of the CeCILL.
    // This source file is licensed as described in the file COPYING, which
    // you should have received as part of this distribution.  The terms
    // are also available at;
    // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt

    if scicos_debug()==0  then return;end
    gui =  findobj("tag","Xcos_debug_gui");
    init=gui==[]

    if init then
        gui=xcos_debug_create_gui()
        before=%t
        t=0
        cb=curblock()
        f=%cpr.sim.funs(cb)
        path=%cpr.corinv(cb);
    end
    ud=gui.user_data;
    if ~init then
        //at least one flags must be selected
        Flags=find(ud.Flags.value==1)
        if Flags==[] then
            Flags=2
            ud.Flags(2).value=1
        end
        //at least before or after must be selected
        When=find(ud.When.value==1)
        if When==[] then
            When=1
            ud.When(1).value=1
        end

        if and(flag+1<>Flags) then return,end
        t=scicos_time();
        time_to_wait=evstr(ud.Time.string);
        if t< time_to_wait then return,end

        instr=stripblanks(ud.Condition.string);
        if instr<>"" then
            if execstr("Cond="+instr,"errcatch")==0 then
                if ~Cond then return,end
            end
        end

        cb=curblock();// l'index du block dans la structure résultant de la compilation
        f=%cpr.sim.funs(cb);//fonction de calcul

        path=%cpr.corinv(cb);

        before=ud.previous==[];
        if before then
            ud.previous=path
            if and(When<>1) then set(gui,"user_data",ud);return,end
        else
            ud.previous=[]
            if and(When<>2) then set(gui,"user_data",ud);return,end
        end
    end
    //break asked
    handles=ud.handles;
    if before then //before block
        handles(4).string=_("Stopped before block:")
    else //after
        handles(4).string=_("Stopped after block:")
    end
    Actions=[_("State derivative computation")
    _("Regular outputs update")
    _("States update on discrete event")
    _("Output activation dates")
    _("Initialization")
    _("Ending")
    _("ReInitialization")
    _("Continous states properties update")
    ""
    _("Zero crossing surfaces computation")
    _("Residual computation")];

    handles(1).string=msprintf("%.3g",t);
    handles(2).string=string(cb);
    handles(3).string=string(flag)+" ("+Actions(flag+1)+")"
    handles(5).string=sci2exp(path,0)
    //Highlight current block
    blk=scs_m.objs(path(1));
    uids=blk.model.label;
    for k=2:size(path,"*")
        blk=blk.model.rpar.objs(path(k));
        uids=[uids blk.model.label];
    end
    ud.Buttons.enable="on";
    warnBlockByUID(uids, "Error");
    while %t //To handle pause actions
        realtimeinit(0.01);
        tt=0;
        global ret;
        ret=0;
        while ret==0 then
            //Wait till user select a button
            //slow down the waiting loop
            tt=tt+1;
            realtime(tt);
        end
        for k=size(uids,"*"):-1:1
            warnBlockByUID(uids(1:k), "")
        end

        r=ret;clearglobal ret;clear ret
        select r
        case 1 then //Next
            if xcos_debug_check_time(ud.Time)&xcos_debug_check_cond(ud.Condition) then
                if before then ud.previous=path;end
                ud.Buttons.enable="off"
                set(gui,"user_data",ud)
                return
            end
        case 2 then //End debug
            scicos_debug(0)
            delete(gui.parent)
            return
        case 3 then //pause
            Y=list()
            if block.nout>0 then
                outtb=%cpr.state.outtb
                sz=matrix(block.outsz,block.nout,-1)
                for i=1:block.nout
                    Y($+1)=%cpr.state.outtb(block.outptr(i)+(1:prod(sz(i,1:2))))
                end
            end
            U=list()
            if block.nin>0 then
                outtb=%cpr.state.outtb
                sz=matrix(block.insz,block.nin,-1)
                for i=1:block.nout
                    U($+1)=%cpr.state.outtb(block.inptr(i)+(1:prod(sz(i,1:2))))
                end
            end
            ud.Buttons.enable="off"
            pause
            ud.Buttons.enable="on"
            if before then ud.previous=path;end
            set(gui,"user_data",ud)
            //    end
        end
    end
endfunction

function gui=xcos_debug_create_gui()
    margin_x     = 5;      // Horizontal margin between each elements
    margin_y     = 5;      // Vertical margin between each elements
    button_w     = 80;
    button_h     = 20;
    label_h      = 20;
    label_h1      = 30;
    label_w1      = 200;
    label_w2      = 80;
    frame_w     = max(4*margin_x+label_w1+label_w2,4*margin_x+3*button_w,12*margin_x+10*label_h)
    frame_h     = 16*margin_y+10*label_h+3*label_h1+button_h

    axes_w       = 2*margin_x+frame_w;
    axes_h       = 2*margin_y+frame_h;// Frame height
    defaultfont  = "arial"; // Default Font

    fig_id=max(winsid())+1
    fig = scf(fig_id)
    fig.visible = "off";
    toolbar(fig.figure_id, "off");
    fig.axes_size       = [axes_w axes_h];
    // Remove Scilab graphics menus & toolbar
    delmenu(fig.figure_id, gettext("&File"));
    delmenu(fig.figure_id, gettext("&Tools"));
    delmenu(fig.figure_id, gettext("&Edit"));
    delmenu(fig.figure_id, gettext("&?"));
    fig.figure_size(2)=axes_h
    fig.axes_size       = [axes_w axes_h];
    fig.background      = addcolor([0.8 0.8 0.8]);
    fig.figure_name     = _("Xcos debug");
    fig.closerequestfcn = "xcos_debug_gui_close()"
    ax=fig.children;
    ax.background= fig.background ;

    gui=uicontrol( ...
    "parent"              , fig,...
    "style"               , "frame",...
    "units"               , "pixels",...
    "position"            , [margin_x margin_y frame_w frame_h],...
    "background"          , [1 1 1]*0.8, ...
    "tag"                 , "Xcos_debug_gui",...
    "visible"             , "on");

    yo=axes_h-button_h;


    y=frame_h-margin_y-label_h1
    x=margin_x;
    uicontrol( ...
    "parent"              , gui,...
    "style"               , "text",...
    "string"              , _("Break point conditions"),...
    "units"               , "pixels",...
    "position"            , [x y frame_w-2*margin_x label_h1],....
    "fontname"            , defaultfont,...
    "fontunits"           , "points",...
    "fontsize"            , 16,...
    "fontweight"          , "bold", ...
    "horizontalalignment" , "center", ...
    "background"          , [1 1 1]*0.8, ...
    "visible"             , "on");
    y=y-label_h-margin_y;
    uicontrol( ...
    "parent"              , gui,...
    "style"               , "text",...
    "string"              , _("Break on selected flags"),...
    "units"               , "pixels",...
    "position"            , [x y frame_w-2*margin_x label_h],....
    "fontname"            , defaultfont,...
    "fontunits"           , "points",...
    "fontsize"            , 12,...
    "fontweight"          , "bold", ...
    "horizontalalignment" , "left", ...
    "background"          , [1 1 1]*0.8, ...
    "visible"             , "on");
    yl=y-label_h-margin_y;
    y=yl-label_h-margin_y;
    xk=x
    Flags=[]
    for k=0:9
        uicontrol( ...
        "parent"              , gui,...
        "style"               , "text",...
        "string"              , string(k),...
        "units"               , "pixels",...
        "position"            , [xk yl label_h label_h],....
        "fontname"            , defaultfont,...
        "fontunits"           , "points",...
        "fontsize"            , 12,...
        "fontweight"          , "bold", ...
        "horizontalalignment" , "left", ...
        "background"          , [1 1 1]*0.8, ...
        "visible"             , "on");
        Flags=[Flags,uicontrol( ...
        "parent"              , gui,...
        "style"               , "checkbox",...
        "value"               , 0,...
        "units"               , "pixels",...
        "position"            , [xk y label_h label_h],....
        "fontname"            , defaultfont,...
        "fontunits"           , "points",...
        "fontsize"            , 12,...
        "fontweight"          , "bold", ...
        "horizontalalignment" , "left", ...
        "background"          , [1 1 1]*0.8, ...
        "visible"             , "on")];
        xk=xk+label_h+margin_x
    end
    Flags(2).value=1;
    yl=y-label_h-margin_y;
    y=yl-label_h-margin_y;
    xk=x;
    When=[]
    L=[_("Before call"),_("After call")]
    for k=1:2
        uicontrol( ...
        "parent"              , gui,...
        "style"               , "text",...
        "string"              , L(k),...
        "units"               , "pixels",...
        "position"            , [xk yl label_w2 label_h],....
        "fontname"            , defaultfont,...
        "fontunits"           , "points",...
        "fontsize"            , 12,...
        "fontweight"          , "bold", ...
        "horizontalalignment" , "left", ...
        "background"          , [1 1 1]*0.8, ...
        "visible"             , "on");
        When=[When,uicontrol( ...
        "parent"              , gui,...
        "style"               , "checkbox",...
        "value"               , 0,...
        "units"               , "pixels",...
        "position"            , [xk y label_h label_h],....
        "fontname"            , defaultfont,...
        "fontunits"           , "points",...
        "fontsize"            , 12,...
        "fontweight"          , "bold", ...
        "horizontalalignment" , "left", ...
        "background"          , [1 1 1]*0.8, ...
        "visible"             , "on")];
        xk=xk+label_w2+margin_x
    end
    When(1).value=1;
    uicontrol( ...
    "parent"              , gui,...
    "style"               , "text",...
    "string"              , _("After date"),...
    "units"               , "pixels",...
    "position"            , [xk yl label_w2 label_h],....
    "fontname"            , defaultfont,...
    "fontunits"           , "points",...
    "fontsize"            , 12,...
    "fontweight"          , "bold", ...
    "horizontalalignment" , "left", ...
    "background"          , [1 1 1]*0.8, ...
    "visible"             , "on");
    Time=uicontrol( ...
    "parent"              , gui,...
    "style"               , "edit",...
    "string"               , "0",...
    "units"               , "pixels",...
    "position"            , [xk y label_w2 label_h],....
    "fontname"            , defaultfont,...
    "fontunits"           , "points",...
    "fontsize"            , 12,...
    "fontweight"          , "bold", ...
    "horizontalalignment" , "left", ...
    "background"          , [1 1 1]*0.8, ...
    "callback"            , "xcos_debug_check_time();" ,...
    "visible"             , "on");
    y=y-label_h-2*margin_y;
    uicontrol( ...
    "parent"              , gui,...
    "style"               , "text",...
    "string"              , _("On the Scilab condition above"),...
    "units"               , "pixels",...
    "position"            , [x y frame_w-2*margin_x label_h],....
    "fontname"            , defaultfont,...
    "fontunits"           , "points",...
    "fontsize"            , 12,...
    "fontweight"          , "bold", ...
    "horizontalalignment" , "left", ...
    "background"          , [1 1 1]*0.8, ...
    "visible"             , "on");
    y=y-label_h-2*margin_y;
    Condition=uicontrol( ...
    "parent"              , gui,...
    "style"               , "edit",...
    "string"              , "",...
    "units"               , "pixels",...
    "position"            , [x y frame_w-6*margin_x label_h],....
    "fontname"            , defaultfont,...
    "fontunits"           , "points",...
    "fontsize"            , 12,...
    "fontweight"          , "bold", ...
    "horizontalalignment" , "left", ...
    "background"          , [1 1 1]*0.8, ...
    "callback"            , "xcos_debug_check_cond();" ,...
    "visible"             , "on");
    y=y-label_h1-margin_y;
    uicontrol( ...
    "parent"              , gui,...
    "style"               , "text",...
    "string"              , _("Current simulation state"),...
    "units"               , "pixels",...
    "position"            , [x y frame_w-2*margin_x label_h1],....
    "fontname"            , defaultfont,...
    "fontunits"           , "points",...
    "fontsize"            , 16,...
    "fontweight"          , "bold", ...
    "horizontalalignment" , "center", ...
    "background"          , [1 1 1]*0.8, ...
    "visible"             , "on");
    y=y-label_h-margin_y;
    X=uicontrol( ...
    "parent"              , gui,...
    "style"               , "text",...
    "string"              , "",...
    "units"               , "pixels",...
    "position"            , [x y label_w1 label_h],....
    "fontname"            , defaultfont,...
    "fontunits"           , "points",...
    "fontsize"            , 12,...
    "horizontalalignment" , "left", ...
    "fontweight"          , "bold", ..
    "background"          , [1 1 1]*0.8, ...
    "visible"             , "on");

    B=uicontrol( ...
    "parent"              , gui,...
    "style"               , "text",...
    "string"              , "0",...
    "units"               , "pixels",...
    "position"            , [x+label_w1+margin_x y label_w2 label_h],....
    "fontname"            , defaultfont,...
    "fontunits"           , "points",...
    "fontsize"            , 12,...
    "horizontalalignment" , "right", ...
    "background"          , [1 1 1]*0.8, ...
    "visible"             , "on");
    y=y-label_h-margin_y;
    //%%%%%%
    uicontrol( ...
    "parent"              , gui,...
    "style"               , "text",...
    "string"              , _("scs_m block path:"),...
    "units"               , "pixels",...
    "position"            , [x y label_w1 label_h],....
    "fontname"            , defaultfont,...
    "fontunits"           , "points",...
    "fontsize"            , 12,...
    "fontweight"          , "bold", ...
    "horizontalalignment" , "left", ...
    "background"          , [1 1 1]*0.8, ...
    "visible"             , "on");

    P=uicontrol( ...
    "parent"              , gui,...
    "style"               , "text",...
    "string"              , "0",...
    "units"               , "pixels",...
    "position"            , [x+label_w1+margin_x y label_w2 label_h],....
    "fontname"            , defaultfont,...
    "fontunits"           , "points",...
    "fontsize"            , 12,...
    "horizontalalignment" , "right", ...
    "background"          , [1 1 1]*0.8, ...
    "visible"             , "on");
    y=y-label_h-margin_y;
    uicontrol( ...
    "parent"              , gui,...
    "style"               , "text",...
    "string"              , _("Time:"),...
    "units"               , "pixels",...
    "position"            , [x y label_w1 label_h],....
    "fontname"            , defaultfont,...
    "fontunits"           , "points",...
    "fontsize"            , 12,...
    "fontweight"          , "bold", ...
    "horizontalalignment" , "left", ...
    "background"          , [1 1 1]*0.8, ...
    "visible"             , "on");

    T=uicontrol( ...
    "parent"              , gui,...
    "style"               , "text",...
    "string"              , "0",...
    "units"               , "pixels",...
    "position"            , [x+label_w1+margin_x y label_w2 label_h],....
    "fontname"            , defaultfont,...
    "fontunits"           , "points",...
    "fontsize"            , 12,...
    "horizontalalignment" , "right", ...
    "background"          , [1 1 1]*0.8, ...
    "visible"             , "on");

    y=y-label_h-margin_y;
    uicontrol( ...
    "parent"              , gui,...
    "style"               , "text",...
    "string"              , _("Flag:"),...
    "units"               , "pixels",...
    "position"            , [x y 4*label_h label_h],....
    "fontname"            , defaultfont,...
    "fontunits"           , "points",...
    "fontsize"            , 12,...
    "fontweight"          , "bold", ...
    "horizontalalignment" , "left", ...
    "background"          , [1 1 1]*0.8, ...
    "visible"             , "on");

    F=uicontrol( ...
    "parent"              , gui,...
    "style"               , "text",...
    "string"              , "6",...
    "units"               , "pixels",...
    "position"            , [x+4*label_h+margin_x y label_w2+label_w1+-4*label_h label_h],....
    "fontname"            , defaultfont,...
    "fontunits"           , "points",...
    "fontsize"            , 12,...
    "horizontalalignment" , "right", ...
    "background"          , [1 1 1]*0.8, ...
    "visible"             , "on");

    //buttons
    y=y-button_h-margin_y;
    xb=x;
    Next = uicontrol( ...
    "parent"              , gui,...
    "relief"              , "groove",...
    "style"               , "pushbutton",...
    "string"              , _("Next"),...
    "units"               , "pixels",...
    "position"            , [xb,y,button_w button_h],..
    "fontname"            , "arial",...
    "fontunits"           , "points",...
    "fontsize"            , 12,...
    "fontweight"          , "bold", ...
    "horizontalalignment" , "center", ...
    "visible"             , "on", ...
    "callback"            , "global ret;ret=1;");
    xb=xb+margin_x+button_w
    Stop = uicontrol( ...
    "parent"              , gui,...
    "relief"              , "groove",...
    "style"               , "pushbutton",...
    "string"              , _("End debug"),...
    "units"               , "pixels",...
    "position"            , [xb,y,button_w button_h],..
    "fontname"            , "arial",...
    "fontunits"           , "points",...
    "fontsize"            , 12,...
    "fontweight"          , "bold", ...
    "horizontalalignment" , "center", ...
    "visible"             , "on", ...
    "callback"            , "global ret;ret=2;");
    xb=xb+margin_x+button_w
    Pause = uicontrol( ...
    "parent"              , gui,...
    "relief"              , "groove",...
    "style"               , "pushbutton",...
    "string"              , _("Pause"),...
    "units"               , "pixels",...
    "position"            , [xb,y,button_w button_h],..
    "fontname"            , "arial",...
    "fontunits"           , "points",...
    "fontsize"            , 12,...
    "fontweight"          , "bold", ...
    "horizontalalignment" , "center", ...
    "visible"             , "on", ...
    "callback"            , "global ret;ret=3;");

    set(gui,"user_data",struct("handles",[T,B,F,X,P],"previous",[],...
    "Flags",Flags,"When",When,"Time",Time,...
    "Condition",Condition,...
    "Buttons",[Next,Stop,Pause]))
    fig.visible = "on";
endfunction
function ok=xcos_debug_check_time(Time)
    if argn(2)<1 then Time=gcbo;end
    ud=Time.parent.user_data
    ok=execstr("t="+Time.string,"errcatch")==0
    if ~ok|type(t)<>1|size(t,"*")<>1 then
        Time.ForegroundColor=[1 0 0];
        ud.Buttons.enable="off"
        ok=%f
    else
        Time.ForegroundColor=[0 0 0];
        ud.Buttons.enable="on"
        ok=%t
    end
endfunction
function ok=xcos_debug_check_cond(Condition)
    if argn(2)<1 then Condition=gcbo;end
    ud=Condition.parent.user_data
    Cond=stripblanks(Condition.string)
    if Cond=="" then
        ok=%t
        c=%f
    else
        ok=execstr("c="+Cond,"errcatch")==0
    end
    if ~ok|and(type(c)<>[1 4])|size(c,"*")<>1 then
        Condition.ForegroundColor=[1 0 0];
        ud.Buttons.enable="off"
        ok=%f
    else
        Condition.ForegroundColor=[0 0 0];
        ud.Buttons.enable="on"
        ok=%t
    end

endfunction
function xcos_debug_gui_close()
    messagebox(msprintf(_("Use the ""%s"" button to end the debugging"),_("End debug")),"modal")
endfunction