summaryrefslogtreecommitdiff
path: root/src/hal/classicladder/emc_mods.c
blob: 9337c97db6315a296c88635c324928ba2503fe9b (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
// classicladder v7.124 adaptation for emc2 January 08

// this is a collection of completely new functions added for the adaptation to EMC2
// it is easier to keep them all together in one place --I hope!
// please add all new functions here!

/* This library is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Lesser General Public */
/* License as published by the Free Software Foundation; either */
/* version 2.1 of the License, or (at your option) any later version. */

/* This library is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU */
/* Lesser General Public License for more details. */

/* You should have received a copy of the GNU Lesser General Public */
/* License along with this library; if not, write to the Free Software */
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


#include "hal.h"
#include "hal/hal_priv.h"

#include <stdio.h>
#include <string.h>
#include "classicladder.h"
#include "global.h"
#include "symbols.h"
#include "vars_names.h"
#include "emc_mods.h"



// This function is a development of Jeff's orginal work for ver 7.100
// it converts variables or symbols to HAL signal names (if present)

// It is called from the GetElementPropertiesForStatusBar function in edit.c 
// it checks to see if the VarsNameParam is a symbol, if it is, converts it to a variable name
// if Variable is B or W then exports that it is a memory variable 
//also checks to see if in the range of max number of word variables loaded by realtime
// if not I, Q, QW, or IW  then returns an error message 
// then checks for what the pinname would be if it is a I, Q, IW or QW  variable 
// (these are the only variables that can connect to the outside world)
// if I or Q checks to see it is really QW or QI then checks
// if in the range of number of HAL pins loaded by realtime module
// then checks to see if a signal is connected to that pin
// if there is a signal returns that name with an arrow to show if the signal is coming in or going out
// if there is no signal connected returns that fact
// anything else should return an error
// edit.c uses this function 
// Chris Morley NOV 08


char * ConvVarNameToHalSigName( char * VarNameParam ) 
{
    if( (VarNameParam[0]=='\0') ||  (VarNameParam[0]==' ')) {return "Error blank symbol name";}
    if( VarNameParam[0] != '%') {VarNameParam= ConvSymbolToVarName(VarNameParam);}
    if( VarNameParam[0] == '%'){
        char pin_name[100] = {0};
        int arrowside=0;
        int idx;

        switch(VarNameParam[1]) {
            case 'I':
                switch(VarNameParam[2]) {
                    case 'W':
                        sscanf(VarNameParam+3, "%d", &idx);
                        if((idx) >= InfosGene->GeneralParams.SizesInfos.nbr_phys_words_inputs) {return "out of bounds variable number";}
                        snprintf(pin_name, 100, "classicladder.0.s32in-%02d", idx);
                        arrowside = 1;
                        break;
                    case 'F':
                        sscanf(VarNameParam+3, "%d", &idx);
                        if((idx) >= InfosGene->GeneralParams.SizesInfos.nbr_phys_float_inputs) {return "out of bounds variable number";}
                        snprintf(pin_name, 100, "classicladder.0.floatin-%02d", idx);
                        arrowside = 1;
                        break;
                    default:
                        sscanf(VarNameParam+2, "%d", &idx);
                        snprintf(pin_name, 100, "classicladder.0.in-%02d", idx);
                        arrowside = 1;
                        if((idx) >= InfosGene->GeneralParams.SizesInfos.nbr_phys_inputs) {return "out of bounds variable number";}
                        break;
                }
                break;
            case 'Q':
                switch(VarNameParam[2]) {
                    case 'W':
                        sscanf(VarNameParam+3, "%d", &idx);
                        if((idx) >= InfosGene->GeneralParams.SizesInfos.nbr_phys_words_outputs) {return "out of bounds variable number";}
                        snprintf(pin_name, 100, "classicladder.0.s32out-%02d", idx);
                        arrowside = 0;
                        break;
                    case 'F':
                        sscanf(VarNameParam+3, "%d", &idx);
                        if((idx) >= InfosGene->GeneralParams.SizesInfos.nbr_phys_float_outputs) {return "out of bounds variable number";}
                        snprintf(pin_name, 100, "classicladder.0.floatout-%02d", idx);
                        arrowside = 0;
                        break;
                    default:
                        sscanf(VarNameParam+2, "%d", &idx);
                        snprintf(pin_name, 100, "classicladder.0.out-%02d", idx);
                        arrowside = 0;
                        if((idx) >= InfosGene->GeneralParams.SizesInfos.nbr_phys_outputs) {return "out of bounds variable number";}
                        break;
                }
                break;
            case 'W':
                sscanf(VarNameParam+2, "%d", &idx);
                if((idx) >= InfosGene->GeneralParams.SizesInfos.nbr_words) {return "out of bounds variable number";}
                return "None -Internal Memory";
            case 'B':
                sscanf(VarNameParam+2, "%d", &idx);
                if((idx) >= InfosGene->GeneralParams.SizesInfos.nbr_bits) {return "out of bounds variable number";}
                return "None -Internal Memory";
            case 'E':
                sscanf(VarNameParam+2, "%d", &idx);
                if((idx) >= InfosGene->GeneralParams.SizesInfos.nbr_error_bits) {return "out of bounds variable number";}
                return "None -Internal Error Status";
            case 'T':
            case 'C':
            case 'M':
            case 'X':
                return "None";
            default:
                return "error";
        }

        if(*pin_name) {
            hal_pin_t *pin = halpr_find_pin_by_name(pin_name);
            if(pin && pin->signal) {
                hal_sig_t *sig = SHMPTR(pin->signal);
                if(sig->name) {
                    static char sig_name[100];
                    // char *arrow = "\xe2\x86\x90";
                    char *arrow = "\xe2\x87\x92";

                    if(arrowside == 0) {
                        snprintf(sig_name, 100, "%s%s", sig->name, arrow);
                    } else {
                        snprintf(sig_name, 100, "%s%s", arrow, sig->name);
                    }

                    return sig_name;
                }
            }
            if (pin && !pin->signal) {return "no signal connected";  }
        }
    }

    return "Conv. HAL signal ERROR";
}

// function to check for first Variable in an arithmetic expression
// ultimately so we can check for a HAL signal name
// It finds the first Variable or symbol name
// sends that to ConvVarNameToHalSigName() which returns status or
// name of a HAL signal connected .
// then we piece together the signal name, first variable/symbolname
// and  expression so it can be returned to be printed in the status bar
// Edit.c uses this function
// Chris Morley Feb 08

char * FirstVariableInArithm(char * Expr)
{
		static char Buffer[100];
		static char Tempbuf[100];
		char * Ptr = Expr;
		int i;
		Buffer[0] = '\0';

	if (Expr[0]=='\0' || Expr[0]=='#')
		return "No expression";
	
//parse expression till we find a symbol that marks the end of a variable or symbol name , or find the end of expression

	for (i=0;i<100;i++)
	{

		switch (Ptr[i])
		{
			case ':' :
			case '=' :
			case '\0':
			case '&' :
			case '!' :
			case '|' :
			case '[' :
			case '(' :

			snprintf(Buffer, i+1, "%s", Expr);
			snprintf(Tempbuf, 100, " %s (for %s)  Exprsn~ %s",ConvVarNameToHalSigName(Buffer),Buffer,Expr);
			return Tempbuf;
			break;

			default:;
		}
	}
	return "first var. expression error";
}

//auto assignment of names for symbols of s32in,s32out, bitin 
//and bitout if not already assigned a name
// you must assign s32in before s32out unless there are no s32in
void SymbolsAutoAssign (void)
{
       
	enum pinnames
		{BITINS=0,BITOUTS,BITS,WORDS,S32INS,S32OUTS,FLOATINS,FLOATOUTS,TIMERS,IEC_TIMERS,MONOS,COUNTERS,ERRORS};
#define NUMVARTYPES 13
	int scansymb,found = FALSE,i,v,numofvariable;
	char Buffer[30],SymbolBuf[5],CommentBuf[40]="";
	
        for (v=0;v<NUMVARTYPES;v++)
        {
	        switch(v)
	        {
			case BITINS:
                                    numofvariable= NBR_PHYS_INPUTS ;
                                    strcpy(SymbolBuf,"I");
                                    break;
			case BITOUTS:
                                    numofvariable= NBR_PHYS_OUTPUTS;
                                    strcpy(SymbolBuf,"Q");
                                    break;
                        case BITS:
                                    numofvariable= NBR_BITS;
                                    strcpy(SymbolBuf,"B");
                                    break;
                        case WORDS:
                                    numofvariable= NBR_WORDS;
                                    strcpy(SymbolBuf,"W");
                                    break;
			case S32INS:
                                    numofvariable= NBR_PHYS_WORDS_INPUTS ;
                                    strcpy(SymbolBuf,"IW");
                                    break;
			case S32OUTS:
                                    numofvariable= NBR_PHYS_WORDS_OUTPUTS;
                                    strcpy(SymbolBuf,"QW");
                                    break;
                        case FLOATINS:
                                    numofvariable= NBR_PHYS_FLOAT_INPUTS ;
                                    strcpy(SymbolBuf,"IF");
                                    break;
			case FLOATOUTS:
                                    numofvariable= NBR_PHYS_FLOAT_OUTPUTS;
                                    strcpy(SymbolBuf,"QF");
                                    break;
		        case TIMERS:
                                    numofvariable= NBR_TIMERS;
                                    strcpy(SymbolBuf,"T");
                                    strcpy(CommentBuf,"Old Timer");
                                    break;
                        case IEC_TIMERS:
                                    numofvariable= NBR_TIMERS_IEC ;
                                    strcpy(SymbolBuf,"TM");
                                    strcpy(CommentBuf,"New Timer");
                                    break;
                        case MONOS:
                                    numofvariable= NBR_MONOSTABLES;
                                    strcpy(SymbolBuf,"M");
                                    strcpy(CommentBuf,"One-shot");
                                    break;
			case COUNTERS:
                                    numofvariable= NBR_COUNTERS;
                                    strcpy(SymbolBuf,"C");
                                    strcpy(CommentBuf,"Counter");
                                    break;
		        case ERRORS:
                                    numofvariable= NBR_ERROR_BITS;
                                    strcpy(SymbolBuf,"E");
                                    strcpy(CommentBuf,"Error Flag Bit");
                                    break;
			default : 
                                 rtapi_print_msg(RTAPI_MSG_ERR, "Cannot auto assign symbol names-wrong variable name");
                                 return;
	        }
         //printf("symbol-%s number-%i\n",SymbolBuf,v);
	        for (i=0;i<numofvariable;i++)
	
                 {
   	                  found = FALSE;
	                //set buffer to variable to check variable name
    	                strcpy(Buffer,"");
   	                 sprintf(Buffer,"%%%s%d",SymbolBuf,i);  

	               // printf("%s\n",Buffer);
		        scansymb=0;	
	                // scan all symbol variables
		        while ( scansymb<NBR_SYMBOLS  ) 
		                { 
	                        // check for exsisting variable/symbol name
		                if (strcmp(SymbolArray[ scansymb ].VarName, Buffer) == FALSE)
			                {
				                found = TRUE ; // already a symbol for this variable
				                break;// stop looking then
			                }
	                        scansymb ++;// check the rest
	                        }
	
	                  scansymb=0;
                         // this assigns a symbol to an unassigned variable
                         // while there is no symbol already assigned and we are not at the
                         // end of the symbol data..
	                 while ( found == FALSE && scansymb<NBR_SYMBOLS)
	                      {
	                            // look for an empty spot...
		                    if (SymbolArray[ scansymb ].VarName[ 0 ] == '\0')
		                       { 
                                           //copy variable name already in Buffer to VarName array
			                  strcpy( SymbolArray[scansymb].VarName, Buffer ); 
	
		                           //put a symbol name (and it's number) in buffer
		        	          strcpy( SymbolArray[scansymb].Symbol, Buffer );
	    	        	        //printf("%s\n",Buffer);
                                        // copy a comment if there is one...
		        	        strcpy(Buffer,"");
		        	        sprintf(Buffer,"%s",CommentBuf);
		        	        strcpy( SymbolArray[scansymb].Comment, Buffer );
	
		       	                 break;// we are done looking
		                         }	
		                scansymb ++;// keep looking for empty spot if not done
	                         }	
                }
          }
         return;
}