219a9f89ed3f9737a625eaf74177987cfff46cf3
[scilab.git] / scilab / modules / scicos_blocks / src / c / cscope.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 cscope.c
23    \author Benoit Bayol
24    \version 1.0
25    \date September 2006 - January 2007
26    \brief CSCOPE is a typical scope which links its input to the simulation time but there is only one input instead of CMSCOPE
27    \see CSCOPE.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 cscope_draw(scicos_block * block, ScopeMemory ** pScopeMemory, int firstdraw)
38     \brief Function to draw or redraw the window
39 */
40 void cscope_draw(scicos_block * block, ScopeMemory ** pScopeMemory, int firstdraw)
41 {
42   int i;
43   double *rpar;
44   int *ipar, nipar;
45   double period;
46   int dimension;
47   double ymin, ymax, xmin, xmax;
48   int buffer_size;
49   int win_pos[2];
50   int win_dim[2];
51   int win;
52   int number_of_subwin;
53   int number_of_curves_by_subwin[1];
54   int * colors;
55
56   /*Retrieving Parameters*/
57   rpar = GetRparPtrs(block);
58   ipar = GetIparPtrs(block);
59   nipar = GetNipar(block);
60   buffer_size = ipar[2];
61   win = ipar[0];
62   period  = rpar[3];
63   win_pos[0] = ipar[(nipar-1) - 3];
64   win_pos[1] = ipar[(nipar-1) - 2];
65   win_dim[0] = ipar[(nipar-1) - 1];
66   win_dim[1] = ipar[nipar-1];
67   dimension = 2;
68   number_of_curves_by_subwin[0] = GetInPortRows(block,1);
69
70   number_of_subwin = 1;
71   ymin = rpar[1];
72   ymax = rpar[2];
73
74
75   colors = (int*)scicos_malloc(number_of_curves_by_subwin[0]*sizeof(int));
76   for(i = 0 ; i < number_of_curves_by_subwin[0] ; i++)
77     {
78       colors[i] = ipar[3+i];
79     }
80
81   /*Allocating memory*/
82   if(firstdraw == 1)
83     {
84       scoInitScopeMemory(block->work,pScopeMemory, number_of_subwin, number_of_curves_by_subwin);
85       /*Must be placed before adding polyline or other elements*/
86       scoSetLongDrawSize(*pScopeMemory, 0, 50);
87       scoSetShortDrawSize(*pScopeMemory,0,buffer_size);
88       scoSetPeriod(*pScopeMemory,0,period);
89     }
90
91   xmin = period*scoGetPeriodCounter(*pScopeMemory,0);
92   xmax = period*(scoGetPeriodCounter(*pScopeMemory,0)+1);
93
94   /*Creating the Scope*/
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       /*Add a couple of polyline : one for the shortdraw and one for the longdraw*/
100       scoAddCoupleOfPolylines(*pScopeMemory,colors);
101       /* scoAddPolylineLineStyle(*pScopeMemory,colors); */
102     }
103   scicos_free(colors);
104 }
105
106 /** \fn void cscope(scicos_block * block,int flag)
107     \brief the computational function
108     \param block A pointer to a scicos_block
109     \param flag An integer which indicates the state of the block (init, update, ending)
110 */
111 void cscope(scicos_block * block,int flag)
112 {
113   ScopeMemory * pScopeMemory;
114   int i;
115   double t;
116   int NbrPtsShort;
117   double * u1;
118   scoGraphicalObject pShortDraw;
119   switch(flag) 
120     {
121     case Initialization:
122       {
123         cscope_draw(block,&pScopeMemory,1);
124         break;
125       }
126     case StateUpdate:
127       {
128         scoRetrieveScopeMemory(block->work,&pScopeMemory);
129         if(scoGetScopeActivation(pScopeMemory) == 1)
130           {
131             t = get_scicos_time();
132         /*Retreiving Scope in the block->work*/
133             
134         /*If window has been destroyed we recreate it*/
135         if(scoGetPointerScopeWindow(pScopeMemory) == NULL)
136           {
137             cscope_draw(block,&pScopeMemory,0);
138           }
139         /*Maybe we are in the end of axes so we have to draw new ones */
140         scoRefreshDataBoundsX(pScopeMemory,t);
141
142         //Cannot be factorized depends of the scope
143         u1 = GetRealInPortPtrs(block,1);
144         for (i = 0 ; i < scoGetNumberOfCurvesBySubwin(pScopeMemory,0) ; i++)
145           {
146             pShortDraw  = scoGetPointerShortDraw(pScopeMemory,0,i);
147             NbrPtsShort = pPOLYLINE_FEATURE(pShortDraw)->n1;
148             pPOLYLINE_FEATURE(pShortDraw)->pvx[NbrPtsShort] = t;
149             pPOLYLINE_FEATURE(pShortDraw)->pvy[NbrPtsShort] = u1[i];
150             pPOLYLINE_FEATURE(pShortDraw)->n1++;
151           }
152         //End of Cannot
153
154         //Draw the Scope
155         scoDrawScopeAmplitudeTimeStyle(pScopeMemory, t);
156           }
157         break;
158       }
159     case Ending:
160       {
161         scoRetrieveScopeMemory(block->work, &pScopeMemory);
162         if(scoGetScopeActivation(pScopeMemory) == 1)
163           {
164             sciSetUsedWindow(scoGetWindowID(pScopeMemory));
165             pShortDraw = sciGetCurrentFigure();
166             pFIGURE_FEATURE(pShortDraw)->user_data = NULL;
167             pFIGURE_FEATURE(pShortDraw)->size_of_user_data = 0;
168             scoDelCoupleOfPolylines(pScopeMemory);
169           }
170         scoFreeScopeMemory(block->work, &pScopeMemory);
171         break;  
172       }
173     }
174 }