* bug 8630 fixed - Scopes crashed Scilab on simulation when they had the same
[scilab.git] / scilab / modules / scicos_blocks / src / c / cevscpe.c
1 /*  Scicos
2 *
3 *  Copyright (C) INRIA - METALAU Project <scicos@inria.fr>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 * See the file ./license.txt
20 */
21 /*--------------------------------------------------------------------------*/ 
22 /**
23    \file cevscpe.c
24    \author Benoit Bayol
25    \version 1.0
26    \date September 2006 - January 2007
27    \brief CEVSCPE is a scope that indicates when the clocks is activated
28    \see CEVENTSCOPE.sci in macros/scicos_blocks/Sinks/
29 */
30 /*--------------------------------------------------------------------------*/ 
31 #include "CurrentObjectsManagement.h"
32 #include "scicos.h"
33 #include "scoMemoryScope.h"
34 #include "scoWindowScope.h"
35 #include "scoMisc.h"
36 #include "scoGetProperty.h"
37 #include "scoSetProperty.h"
38 #include "scicos_block4.h"
39 #include "DrawingBridge.h"
40 #include "SetJavaProperty.h"
41 #include "scicos_malloc.h"
42 #include "scicos_free.h"
43 #include "MALLOC.h"
44 #include "dynlib_scicos_blocks.h"
45 /*--------------------------------------------------------------------------*/ 
46 /** \fn cscopxy_draw(scicos_block * block, ScopeMemory ** pScopeMemory, int firstdraw)
47     \brief Function to draw or redraw the window
48 */
49 SCICOS_BLOCKS_IMPEXP void cevscpe_draw(scicos_block * block, ScopeMemory ** pScopeMemory, int firstdraw)
50 {
51   /* Declarations */
52
53   int nipar = 0; //Number of elements in ipar vector
54   int i; //As usual
55   int * ipar = NULL;
56   double * rpar = NULL; //Integer Parameter
57   int nbr_colors = 0; //Number of colors and lines IS ALSO number of channels
58   int win = 0; //To give a name to the window
59   int color_flag = 0; //0/1 color flag -- NOT USED
60   int  * colors = NULL; //Begin at ipar[2] and has a measure of 8 max
61   int dimension = 2;
62   double period = 0.; //Refresh Period of the scope is a vector here
63   int number_of_subwin = 0;
64   int number_of_curves_by_subwin = 0;
65   double xmin = 0., xmax = 0., ymin = 0., ymax = 0;
66   int win_pos[2], win_dim[2];
67   char *label = NULL;
68
69   /* Initialization */
70   ipar =  GetIparPtrs(block);
71   win = ipar[0];
72   color_flag = ipar[1]; /*not used*/
73   rpar = GetRparPtrs(block);
74   period = rpar[0];
75   nipar = GetNipar(block);
76   label = GetLabelPtrs(block);
77   nbr_colors = nipar-6;
78   colors=(int*)scicos_malloc(nbr_colors*sizeof(int));
79   for( i = 2 ; i < nbr_colors+2 ; i++)
80     {
81       colors[i-2] = ipar[i];
82     }
83
84   number_of_subwin = 1;
85   number_of_curves_by_subwin = nbr_colors;
86
87   ymin = 0;
88   ymax = 1;
89
90   win_pos[0] = ipar[(nipar-1) - 3];
91   win_pos[1] = ipar[(nipar-1) - 2];
92   win_dim[0] = ipar[(nipar-1) - 1];
93   win_dim[1] = ipar[nipar-1];
94
95   if(firstdraw == 1)
96     {
97       scoInitScopeMemory(block->work,pScopeMemory, number_of_subwin, &number_of_curves_by_subwin);
98       scoSetLongDrawSize(*pScopeMemory,0,5000);
99       scoSetShortDrawSize(*pScopeMemory,0,1);
100       scoSetPeriod(*pScopeMemory,0,period);
101     }
102
103   xmin = period*scoGetPeriodCounter(*pScopeMemory,0);
104   xmax = period*(scoGetPeriodCounter(*pScopeMemory,0)+1);
105
106   scoInitOfWindow(*pScopeMemory, dimension, win, win_pos, win_dim, &xmin, &xmax, &ymin, &ymax, NULL, NULL);
107   if(scoGetScopeActivation(*pScopeMemory) == 1)
108     {
109       scoAddTitlesScope(*pScopeMemory,label,"t","y",NULL);
110       scoAddCoupleOfSegments(*pScopeMemory,colors);
111     }
112   scicos_free(colors);
113
114     /* use only single buffering to be sure to draw on the screen */
115     if (scoGetPointerScopeWindow(*pScopeMemory) != NULL) {
116         sciSetJavaUseSingleBuffer(scoGetPointerScopeWindow(*pScopeMemory), TRUE);
117     }
118 }
119 /*--------------------------------------------------------------------------*/ 
120 /** \fn void cevscpe(scicos_block * block, int flag)
121     \brief the computational function
122     \param block A pointer to a scicos_block
123     \param flag An int which indicates the state of the block (init, update, ending)
124 */
125 SCICOS_BLOCKS_IMPEXP void cevscpe(scicos_block * block, int flag)
126 {
127
128   ScopeMemory * pScopeMemory = NULL;
129   int nbseg = 0;
130   int tab[20];
131   scoGraphicalObject pShortDraw, pLongDraw;
132   int i = 0;
133   double t = 0;
134
135   switch(flag)
136     {
137     case Initialization:
138       {
139         cevscpe_draw(block,&pScopeMemory,1);
140         break;
141       }
142
143     case StateUpdate:
144       {
145
146         /* Charging elements */
147
148         scoRetrieveScopeMemory(block->work,&pScopeMemory);
149
150         if(scoGetScopeActivation(pScopeMemory) == 1)
151           {
152            
153             t = get_scicos_time();
154             if(scoGetPointerScopeWindow(pScopeMemory) == NULL)
155               {
156                 cevscpe_draw(block,&pScopeMemory,0);
157               }
158
159             scoRefreshDataBoundsX(pScopeMemory,t);
160         
161             /*Not Factorize*/
162
163             for(i = 0 ; i < scoGetNumberOfCurvesBySubwin(pScopeMemory,0) ; i++)
164               {
165                 if((GetNevIn(block)&(1<<i))==(1<<i))
166                   {
167                     tab[nbseg]=i;
168                     nbseg++;
169                   }
170               }
171
172             for(i = 0 ; i < nbseg ; i++)
173               {
174                 pShortDraw = scoGetPointerShortDraw(pScopeMemory,0,tab[i]);
175                 pSEGS_FEATURE(pShortDraw)->vx[0] = t;
176                 pSEGS_FEATURE(pShortDraw)->vx[1] = t;
177                 pSEGS_FEATURE(pShortDraw)->vy[0] = i*0.8/nbseg;
178                 pSEGS_FEATURE(pShortDraw)->vy[1] = (i+1)*0.8/nbseg;
179                 pSEGS_FEATURE(pShortDraw)->Nbr1 = 2;
180                 pSEGS_FEATURE(pShortDraw)->Nbr2 = 2;
181               }
182             /*End of Not Factorize*/
183             scoDrawScopeAmplitudeTimeStyle(pScopeMemory,t);
184           }
185         break;
186       }
187
188     case Ending:
189       {
190
191                                 scoRetrieveScopeMemory(block->work, &pScopeMemory);
192                                 if(scoGetScopeActivation(pScopeMemory) == 1)
193                                 {
194                                         /* sciSetUsedWindow(scoGetWindowID(pScopeMemory)); */
195                                         /* Check if figure is still opened, otherwise, don't try to destroy it again. */
196                                         if(scoGetPointerScopeWindow(pScopeMemory) != NULL)
197                                         {
198                                                 for(i = 0 ; i < scoGetNumberOfCurvesBySubwin(pScopeMemory,0) ; i++)
199                                                 {
200                                                         /* maybe a bug here in the last argument of the following instruction (see tab[i]) */
201                                                         pLongDraw = scoGetPointerLongDraw(pScopeMemory,0,i);
202                                                         forceRedraw(pLongDraw);
203                                                 }
204                                         
205
206                                                 /* pShortDraw = sciGetCurrentFigure(); */
207                                                 pShortDraw = scoGetPointerScopeWindow(pScopeMemory);
208                                                 clearUserData(pShortDraw);
209                                                 /* pFIGURE_FEATURE(pShortDraw)->user_data = NULL; */
210                                                 /* pFIGURE_FEATURE(pShortDraw)->size_of_user_data = 0; */
211                                                 /* restore double buffering */
212                         if (pShortDraw) {
213                             sciSetJavaUseSingleBuffer(pShortDraw, FALSE);
214                         }
215                                         }
216                                 }
217                                 scoFreeScopeMemory(block->work,&pScopeMemory);
218                                 break;
219                         }
220         }
221 }
222 /*--------------------------------------------------------------------------*/