3d64393111010f6028c8f0edf7757de26d36f951
[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    \file cevscpe.c
23    \author Benoit Bayol
24    \version 1.0
25    \date September 2006 - January 2007
26    \brief CEVSCPE is a scope that indicates when the clocks is activated
27    \see CEVENTSCOPE.sci in macros/scicos_blocks/Sinks/
28 */
29
30 #include "scoMemoryScope.h"
31 #include "scoWindowScope.h"
32 #include "scoMisc.h"
33 #include "scoGetProperty.h"
34 #include "scoSetProperty.h"
35 #include "scicos_block4.h"
36
37 /** \fn cscopxy_draw(scicos_block * block, ScopeMemory ** pScopeMemory, int firstdraw)
38     \brief Function to draw or redraw the window
39 */
40 void cevscpe_draw(scicos_block * block, ScopeMemory ** pScopeMemory, int firstdraw)
41 {
42   /* Declarations */
43
44   int nipar; //Number of elements in ipar vector
45   int i; //As usual
46   int * ipar;
47   double * rpar; //Integer Parameter
48   int nbr_colors; //Number of colors and lines IS ALSO number of channels
49   int win; //To give a name to the window
50   int color_flag; //0/1 color flag -- NOT USED
51   int  * colors; //Begin at ipar[2] and has a measure of 8 max
52   int dimension = 2;
53   double period; //Refresh Period of the scope is a vector here
54   int number_of_subwin;
55   int number_of_curves_by_subwin;
56   double xmin, xmax, ymin, ymax;
57   int win_pos[2], win_dim[2];
58
59   /* Initialization */
60   ipar =  GetIparPtrs(block);
61   win = ipar[0];
62   color_flag = ipar[1]; /*not used*/
63   rpar = GetRparPtrs(block);
64   period = rpar[0];
65   nipar = GetNipar(block);
66   nbr_colors = nipar-6;
67   colors=(int*)scicos_malloc(nbr_colors*sizeof(int));
68   for( i = 2 ; i < nbr_colors+2 ; i++)
69     {
70       colors[i-2] = ipar[i];
71     }
72
73   number_of_subwin = 1;
74   number_of_curves_by_subwin = nbr_colors;
75
76   ymin = 0;
77   ymax = 1;
78
79   win_pos[0] = ipar[(nipar-1) - 3];
80   win_pos[1] = ipar[(nipar-1) - 2];
81   win_dim[0] = ipar[(nipar-1) - 1];
82   win_dim[1] = ipar[nipar-1];
83
84   if(firstdraw == 1)
85     {
86       scoInitScopeMemory(block->work,pScopeMemory, number_of_subwin, &number_of_curves_by_subwin);
87       scoSetLongDrawSize(*pScopeMemory,0,5000);
88       scoSetShortDrawSize(*pScopeMemory,0,1);
89       scoSetPeriod(*pScopeMemory,0,period);
90     }
91
92   xmin = period*scoGetPeriodCounter(*pScopeMemory,0);
93   xmax = period*(scoGetPeriodCounter(*pScopeMemory,0)+1);
94
95   scoInitOfWindow(*pScopeMemory, dimension, win, win_pos, win_dim, &xmin, &xmax, &ymin, &ymax, NULL, NULL);
96   if(scoGetScopeActivation(*pScopeMemory) == 1)
97     {
98       scoAddTitlesScope(*pScopeMemory,"t","y",NULL);
99       scoAddCoupleOfSegments(*pScopeMemory,colors);
100     }
101   scicos_free(colors);
102 }
103
104 /** \fn void cevscpe(scicos_block * block, int flag)
105     \brief the computational function
106     \param block A pointer to a scicos_block
107     \param flag An integer which indicates the state of the block (init, update, ending)
108 */
109 void cevscpe(scicos_block * block, int flag)
110 {
111   ScopeMemory * pScopeMemory;
112   int nbseg = 0;
113   int tab[20];
114   scoGraphicalObject pShortDraw;
115   int i;
116   double t;
117
118   switch(flag)
119     {
120     case Initialization:
121       {
122         cevscpe_draw(block,&pScopeMemory,1);
123         break;
124       }
125
126     case StateUpdate:
127       {
128
129         /* Charging elements */
130         scoRetrieveScopeMemory(block->work,&pScopeMemory);
131         if(scoGetScopeActivation(pScopeMemory) == 1)
132           {
133             t = get_scicos_time();
134             if(scoGetPointerScopeWindow(pScopeMemory) == NULL)
135               {
136                 cevscpe_draw(block,&pScopeMemory,0);
137               }
138
139             scoRefreshDataBoundsX(pScopeMemory,t);
140         
141             /*Not Factorize*/
142
143             for(i = 0 ; i < scoGetNumberOfCurvesBySubwin(pScopeMemory,0) ; i++)
144               {
145                 if((GetNevIn(block)&(1<<i))==(1<<i))
146                   {
147                     tab[nbseg]=i;
148                     nbseg++;
149                   }
150               }
151
152             for(i = 0 ; i < nbseg ; i++)
153               {
154                 pShortDraw = scoGetPointerShortDraw(pScopeMemory,0,tab[i]);
155                 pSEGS_FEATURE(pShortDraw)->vx[0] = t;
156                 pSEGS_FEATURE(pShortDraw)->vx[1] = t;
157                 pSEGS_FEATURE(pShortDraw)->vy[0] = i*0.8/nbseg;
158                 pSEGS_FEATURE(pShortDraw)->vy[1] = (i+1)*0.8/nbseg;
159                 pSEGS_FEATURE(pShortDraw)->Nbr1 = 2;
160                 pSEGS_FEATURE(pShortDraw)->Nbr2 = 2;
161               }
162             /*End of Not Factorize*/
163             scoDrawScopeAmplitudeTimeStyle(pScopeMemory,t);
164           }
165         break;
166       }
167
168     case Ending:
169       {
170         scoRetrieveScopeMemory(block->work, &pScopeMemory);
171         if(scoGetScopeActivation(pScopeMemory) == 1)
172           {
173             sciSetUsedWindow(scoGetWindowID(pScopeMemory));
174             pShortDraw = sciGetCurrentFigure();
175             pFIGURE_FEATURE(pShortDraw)->user_data = NULL;
176             pFIGURE_FEATURE(pShortDraw)->size_of_user_data = 0;
177             scoDelCoupleOfSegments(pScopeMemory);
178           }
179         scoFreeScopeMemory(block->work,&pScopeMemory);
180         break;
181       }
182     }
183 }