3 * Copyright (C) INRIA - METALAU Project <scicos@inria.fr>
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.
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.
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.
19 * See the file ./license.txt
25 \date September 2006 - January 2007
26 \brief CMSCOPE is a typical scope which links its input to the simulation time
27 \see CMSCOPE.sci in macros/scicos_blocks/Sinks/
29 #include "CurrentObjectsManagement.h"
30 #include "scoMemoryScope.h"
31 #include "scoWindowScope.h"
33 #include "scoGetProperty.h"
34 #include "scoSetProperty.h"
35 #include "scicos_block4.h"
36 #include "SetJavaProperty.h"
38 /** \fn cmscope_draw(scicos_block * block, ScopeMemory ** pScopeMemory, int firstdraw)
39 \brief Function to draw or redraw the window
41 void cmscope_draw(scicos_block * block, ScopeMemory ** pScopeMemory, int firstdraw)
44 int * ipar; //Integer Parameters
45 int * colors; //Colors
46 int win; //Windows ID : To give a name to the window
47 int buffer_size; //Buffer Size
48 int win_pos[2]; //Position of the Window
49 int win_dim[2]; //Dimension of the Window
53 double * rpar; //Reals parameters
55 double * period; //Refresh Period of the scope is a vector here
56 double * ymin,* ymax; //Ymin and Ymax are vectors here
59 int * number_of_curves_by_subwin;
64 rpar = GetRparPtrs(block);
65 ipar = GetIparPtrs(block);
66 nipar = GetNipar(block);
68 number_of_subwin = ipar[1];
69 buffer_size = ipar[2];
75 //Don't forget malloc for 'type *'
76 number_of_curves_by_subwin = (int*)scicos_malloc(number_of_subwin*sizeof(int));
77 for (i = 7; i < 7+number_of_subwin ; i++)
79 number_of_curves_by_subwin[i-7] = ipar[i];
80 nbr_total_curves = nbr_total_curves + ipar[i];
82 colors = (int*)scicos_malloc(nbr_total_curves*sizeof(int));
83 for(i = 0; i < nbr_total_curves ; i++)
85 colors[i] = ipar[i+7+number_of_subwin];
87 inherited_events = ipar[7+number_of_subwin+number_of_subwin];
92 period = (double*)scicos_malloc(number_of_subwin*sizeof(double));
93 for (i = 0 ; i < number_of_subwin ; i++)
95 period[i] = rpar[i+1];
98 ymin = (double*)scicos_malloc(number_of_subwin*sizeof(double));
99 ymax = (double*)scicos_malloc(number_of_subwin*sizeof(double));
100 for (i = 0 ; i < number_of_subwin ; i++)
102 ymin[i] = rpar[2*i+nbr_period+1];
103 ymax[i] = rpar[2*i+nbr_period+2];
106 /*Allocating memory*/
110 scoInitScopeMemory(block->work,pScopeMemory, number_of_subwin, number_of_curves_by_subwin);
111 for(i = 0 ; i < number_of_subwin ; i++)
113 scoSetLongDrawSize(*pScopeMemory, i, 5000);
114 scoSetShortDrawSize(*pScopeMemory,i,buffer_size);
115 scoSetPeriod(*pScopeMemory,i,period[i]);
119 /* Xmin and Xmax are calculated here because we need a variable which is only existing in the pScopeMemory. pScopeMemory is allocated just few lines before. Indeed in this TimeAmplitudeScope Xmin and Xmax have to change often. To be sure to redraw in the correct scale we have to calculate xmin and xmax thanks to the period_counter. If the window haven't to be redraw (recreate) it wouldn't be necessary*/
120 xmin = (double*)scicos_malloc(number_of_subwin*sizeof(double));
121 xmax = (double*)scicos_malloc(number_of_subwin*sizeof(double));
122 for (i = 0 ; i < number_of_subwin ; i++)
124 xmin[i] = period[i]*(scoGetPeriodCounter(*pScopeMemory,i));
125 xmax[i] = period[i]*(scoGetPeriodCounter(*pScopeMemory,i)+1);
128 /*Creating the Scope*/
129 scoInitOfWindow(*pScopeMemory, dimension, win, win_pos, win_dim, xmin, xmax, ymin, ymax, NULL, NULL);
130 if(scoGetScopeActivation(*pScopeMemory) == 1)
132 scoAddTitlesScope(*pScopeMemory,"t","y",NULL);
134 /*Add a couple of polyline : one for the shortdraw and one for the longdraw*/
135 /* scoAddPolylineLineStyle(*pScopeMemory,colors); */
136 scoAddCoupleOfPolylines(*pScopeMemory,colors);
138 scicos_free(number_of_curves_by_subwin);
146 /* use only single buffering to be sure to draw on the screen */
147 sciSetJavaUseSingleBuffer(scoGetPointerScopeWindow(*pScopeMemory), TRUE);
150 /** \fn void cmscope(scicos_block * block, int flag)
151 \brief the computational function
152 \param block A pointer to a scicos_block
153 \param flag An int which indicates the state of the block (init, update, ending)
155 void cmscope(scicos_block * block, int flag)
158 ScopeMemory * pScopeMemory;
161 double t; //get_scicos_time()
162 scoGraphicalObject pShortDraw;
165 double d_current_real_time ;
167 /* Initializations and Allocations*/
168 //Allocations are done here because there are dependent of some values presents below
170 /* State Machine Control */
175 cmscope_draw(block,&pScopeMemory,1);
177 //** Init the real time section
178 //** current real time as double [second]
179 d_current_real_time = scoGetRealTime();
180 pScopeMemory->d_last_scope_update_time = d_current_real_time ; //** update the ds for the next step
182 break; //Break of the switch condition: dont forget it
183 } //End of Initialization
187 /*Retreiving Scope in the block->work*/
188 scoRetrieveScopeMemory(block->work,&pScopeMemory);
189 if(scoGetScopeActivation(pScopeMemory) == 1)
191 /* Charging Elements */
192 t = get_scicos_time();
193 /* If window has been destroyed we recreate it */
194 if(scoGetPointerScopeWindow(pScopeMemory) == NULL)
196 cmscope_draw(block,&pScopeMemory,0);
199 scoRefreshDataBoundsX(pScopeMemory,t);
201 //Here we are calculating the points in the polylines
202 for (i = 0 ; i < scoGetNumberOfSubwin(pScopeMemory) ; i++)
204 u1 = GetRealInPortPtrs(block,i+1);
205 pShortDraw = scoGetPointerShortDraw(pScopeMemory,i,0);
206 NbrPtsShort = pPOLYLINE_FEATURE(pShortDraw)->n1;
207 for (j = 0; j < scoGetNumberOfCurvesBySubwin(pScopeMemory,i) ; j++)
209 pShortDraw = scoGetPointerShortDraw(pScopeMemory,i,j);
210 pPOLYLINE_FEATURE(pShortDraw)->pvx[NbrPtsShort] = t;
211 pPOLYLINE_FEATURE(pShortDraw)->pvy[NbrPtsShort] = u1[j];
212 pPOLYLINE_FEATURE(pShortDraw)->n1++;
217 scoDrawScopeAmplitudeTimeStyle(pScopeMemory, t); //** the scope update condition
223 } //**End of stateupdate
225 //** Ending is activated when the simulation is done or when we close Scicos
228 scoRetrieveScopeMemory(block->work, &pScopeMemory);
229 if(scoGetScopeActivation(pScopeMemory) == 1)
231 // sciSetUsedWindow(scoGetWindowID(pScopeMemory));
232 // pShortDraw = sciGetCurrentFigure();
233 // pFIGURE_FEATURE(pShortDraw)->user_data = NULL;
234 // pFIGURE_FEATURE(pShortDraw)->size_of_user_data = 0;
235 ///* restore double buffering */
236 //sciSetJavaUseSingleBuffer(pShortDraw, FALSE);
237 // scoDelCoupleOfPolylines(pScopeMemory);
240 /* Check if figure is still opened, otherwise, don't try to destroy it again. */
241 scoGraphicalObject figure = scoGetPointerScopeWindow(pScopeMemory);
244 /*pShortDraw = scoGetPointerScopeWindow(pScopeMemory);*/
245 clearUserData(figure);
247 sciSetJavaUseSingleBuffer(figure, FALSE);
248 scoDelCoupleOfPolylines(pScopeMemory);
253 scoFreeScopeMemory(block->work, &pScopeMemory);