rllib  1
rlplc.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rlplc.cpp - description
3  -------------------
4  begin : Tue Dec 11 2008
5  copyright : (C) 2008 by R. Lehrig
6  email : lehrig@t-online.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as *
13  * published by the Free Software Foundation *
14  * *
15  ***************************************************************************/
16 #include <string.h>
17 #include "rlplc.h"
18 
19 rlPlcState::rlPlcState(int numInt, int numFloat, int numDouble, const char *shared_memory)
20 {
21  max_int = numInt;
22  if(max_int <= 0) max_int = 1;
23  max_float = numFloat;
24  if(max_float <= 0) max_float = 1;
25  max_double = numDouble;
26  if(max_double <= 0) max_double = 1;
27  if(shared_memory == NULL)
28  { // use local memory
29  i = new int[max_int];
30  i_old = new int[max_int];
31  f = new float[max_float];
32  f_old = new float[max_float];
33  d = new double[max_double];
34  d_old = new double[max_double];
35  memset(i ,0,sizeof(int)*max_int);
36  memset(i_old,0,sizeof(int)*max_int);
37  memset(f ,0,sizeof(float)*max_float);
38  memset(f_old,0,sizeof(float)*max_float);
39  memset(d ,0,sizeof(double)*max_double);
40  memset(d_old,0,sizeof(double)*max_double);
41  }
42  else
43  { // map memory to shared memory
44  shm = new rlSharedMemory(shared_memory, sizeof(int)*max_int*2 + sizeof(float)*max_float*2 + sizeof(double)*max_double*2 );
46  {
47  void *ptr = shm->getUserAdr();
48  i = (int *) ptr;
49  i_old = (int *) ptr + sizeof(int)*max_int;
50  f = (float *) ptr + sizeof(int)*max_int*2;
51  f_old = (float *) ptr + sizeof(int)*max_int*2 + sizeof(float)*max_float;
52  d = (double *) ptr + sizeof(int)*max_int*2 + sizeof(float)*max_float*2;
53  d_old = (double *) ptr + sizeof(int)*max_int*2 + sizeof(float)*max_float*2 + sizeof(double)*max_double;
54  }
55  else
56  {
57  printf("ERROR: rlPlcState sharedMemoryStatus(%s) is not OK\n", shared_memory);
58  }
59  }
60 }
61 
63 {
64  if(shm == NULL)
65  {
66  delete [] i;
67  delete [] i_old;
68  delete [] f;
69  delete [] f_old;
70  delete [] d;
71  delete [] d_old;
72  }
73  else
74  {
75  delete shm;
76  }
77 }
78 
80 {
81  if(i != NULL) memset(i ,0,sizeof(int)*max_int);
82  if(i_old != NULL) memset(i_old,0,sizeof(int)*max_int);
83  if(f != NULL) memset(f ,0,sizeof(float)*max_float);
84  if(f_old != NULL) memset(f_old,0,sizeof(float)*max_float);
85  if(d != NULL) memset(d ,0,sizeof(double)*max_double);
86  if(d_old != NULL) memset(d_old,0,sizeof(double)*max_double);
87 }
88 
90 {
91  memcpy(i_old,i,sizeof(int)*max_int);
92  memcpy(f_old,f,sizeof(float)*max_float);
93  memcpy(d_old,d,sizeof(double)*max_double);
94 }
95 
96 int rlPlcState::intChanged(int index)
97 {
98  if(index<0 || index >= max_int) return 0;
99  if(i[index] == i_old[index]) return 0;
100  else return 1;
101 }
102 
104 {
105  if(index<0 || index >= max_float) return 0;
106  if(f[index] == f_old[index]) return 0;
107  else return 1;
108 }
109 
111 {
112  if(index<0 || index >= max_double) return 0;
113  if(d[index] == d_old[index]) return 0;
114  else return 1;
115 }
116 
118 {
119  if(index<0 || index >= max_int) return 0;
120  if(i[index] > i_old[index]) return 1;
121  else return 0;
122 }
123 
125 {
126  if(index<0 || index >= max_float) return 0;
127  if(f[index] > f_old[index]) return 1;
128  else return 0;
129 }
130 
132 {
133  if(index<0 || index >= max_double) return 0;
134  if(d[index] > d_old[index]) return 1;
135  else return 0;
136 }
137 
139 {
140  if(index<0 || index >= max_int) return 0;
141  if(i[index] < i_old[index]) return 1;
142  else return 0;
143 }
144 
146 {
147  if(index<0 || index >= max_float) return 0;
148  if(f[index] < f_old[index]) return 1;
149  else return 0;
150 }
151 
153 {
154  if(index<0 || index >= max_double) return 0;
155  if(d[index] < d_old[index]) return 1;
156  else return 0;
157 }
158 
159 int rlPlcState::deltaInt(int index)
160 {
161  if(index<0 || index >= max_int) return 0;
162  return i[index] - i_old[index];
163 }
164 
165 float rlPlcState::deltaFloat(int index)
166 {
167  if(index<0 || index >= max_float) return 0;
168  return f[index] - f_old[index];
169 }
170 
171 double rlPlcState::deltaDouble(int index)
172 {
173  if(index<0 || index >= max_double) return 0;
174  return d[index] - d_old[index];
175 }
176 
177 void rlPlcState::set(int index, int bit)
178 {
179  if(index<0 || index >= max_int) return;
180  i[index] = i[index] | bit;
181 }
182 
183 void rlPlcState::clear(int index, int bit)
184 {
185  if(index<0 || index >= max_int) return;
186  i[index] = i[index] & ~bit;
187 }
188 
189 int rlPlcState::isSet(int index, int bit)
190 {
191  if(index<0 || index >= max_int) return 0;
192  if(i[index] & bit) return 1;
193  else return 0;
194 }
195 
196 int rlPlcState::isClear(int index, int bit)
197 {
198  if(index<0 || index >= max_int) return 0;
199  if(i[index] & bit) return 0;
200  else return 1;
201 }
202 
203 int rlPlcState::hasBeenSet(int index, int bit)
204 {
205  if(index<0 || index >= max_int) return 0;
206  if(i[index] & bit) // is bit set ?
207  {
208  if(i_old[index] & bit) return 0; // both are set
209  else return 1; // old one was not set
210  }
211  return 0;
212 }
213 
214 int rlPlcState::hasBeenCleared(int index, int bit)
215 {
216  if(index<0 || index >= max_int) return 0;
217  if((i[index] & bit) == 0) // is bit clear ?
218  {
219  if((i_old[index] & bit) == 0) return 0; // both are clear
220  else return 1; // old one was set
221  }
222  return 0;
223 }
224 
226 {
227  return max_int - 1;
228 }
229 
231 {
232  return max_float - 1;
233 }
234 
236 {
237  return max_float - 1;
238 }
239 
240 int rlPlcState::getInt(int index)
241 {
242  if(index<0 || index >= max_int) return 0;
243  return i[index];
244 }
245 
246 float rlPlcState::getFloat(int index)
247 {
248  if(index<0 || index >= max_float) return 0;
249  return f[index];
250 }
251 
252 double rlPlcState::getDouble(int index)
253 {
254  if(index<0 || index >= max_double) return 0;
255  return d[index];
256 }
257 
258 int rlPlcState::getOldInt(int index)
259 {
260  if(index<0 || index >= max_int) return 0;
261  return i_old[index];
262 }
263 
264 float rlPlcState::getOldFloat(int index)
265 {
266  if(index<0 || index >= max_float) return 0;
267  return f_old[index];
268 }
269 
270 double rlPlcState::getOldDouble(int index)
271 {
272  if(index<0 || index >= max_double) return 0;
273  return d_old[index];
274 }
275 
276 //###################################################
277 
279 {
280  i = 0;
281  i_old = 0;
282  f = 0.0f;
283  f_old = 0.0f;
284  d = 0.0;
285  d_old = 0.0;
286 }
287 
289 {
290 }
291 
293 {
294  i_old = i;
295  f_old = f;
296  d_old = d;
297 }
298 
300 {
301  if(i == i_old) return 0;
302  else return 1;
303 }
304 
306 {
307  if(f == f_old) return 0;
308  else return 1;
309 }
310 
312 {
313  if(d == d_old) return 0;
314  else return 1;
315 }
316 
318 {
319  if(i > i_old) return 1;
320  else return 0;
321 }
322 
324 {
325  if(f > f_old) return 1;
326  else return 0;
327 }
328 
330 {
331  if(d > d_old) return 1;
332  else return 0;
333 }
334 
336 {
337  if(i < i_old) return 1;
338  else return 0;
339 }
340 
342 {
343  if(f < f_old) return 1;
344  else return 0;
345 }
346 
348 {
349  if(d < d_old) return 1;
350  else return 0;
351 }
352 
354 {
355  return i - i_old;
356 }
357 
359 {
360  return f - f_old;
361 }
362 
364 {
365  return d - d_old;
366 }
367 
368 void rlPlcMem::set(int bit)
369 {
370  i = i | bit;
371 }
372 
373 void rlPlcMem::clear(int bit)
374 {
375  i = i & ~bit;
376 }
377 
378 int rlPlcMem::isSet(int bit)
379 {
380  if(i & bit) return 1;
381  else return 0;
382 }
383 
384 int rlPlcMem::isClear(int bit)
385 {
386  if(i & bit) return 0;
387  else return 1;
388 }
389 
391 {
392  if(i & bit) // is bit set ?
393  {
394  if(i_old & bit) return 0; // both are set
395  else return 1; // old one was not set
396  }
397  return 0;
398 }
399 
401 {
402  if((i & bit) == 0) // is bit clear ?
403  {
404  if((i_old & bit) == 0) return 0; // both are clear
405  else return 1; // old one was set
406  }
407  return 0;
408 }
409 
int doubleHasDecreased()
Definition: rlplc.cpp:347
rlPlcState(int numInt=100, int numFloat=100, int numDouble=0, const char *shared_memory=NULL)
Definition: rlplc.cpp:19
int getOldInt(int index)
Definition: rlplc.cpp:258
int i
Definition: rlplc.h:70
double deltaDouble()
Definition: rlplc.cpp:363
int doubleHasIncreased()
Definition: rlplc.cpp:329
double * d
Definition: rlplc.h:29
int maxFloat()
Definition: rlplc.cpp:230
virtual ~rlPlcState()
Definition: rlplc.cpp:62
double d
Definition: rlplc.h:72
int intChanged()
Definition: rlplc.cpp:299
int intChanged(int index)
Definition: rlplc.cpp:96
float deltaFloat(int index)
Definition: rlplc.cpp:165
int floatChanged(int index)
Definition: rlplc.cpp:103
float * f
Definition: rlplc.h:28
int doubleChanged(int index)
Definition: rlplc.cpp:110
float getFloat(int index)
Definition: rlplc.cpp:246
rlPlcMem()
Definition: rlplc.cpp:278
int doubleHasDecreased(int index)
Definition: rlplc.cpp:152
int i_old
Definition: rlplc.h:70
float getOldFloat(int index)
Definition: rlplc.cpp:264
int isSet(int index, int bit)
Definition: rlplc.cpp:189
int intHasIncreased()
Definition: rlplc.cpp:317
int max_int
Definition: rlplc.h:62
int hasBeenSet(int index, int bit)
Definition: rlplc.cpp:203
void set(int index, int bit)
Definition: rlplc.cpp:177
int floatChanged()
Definition: rlplc.cpp:305
int hasBeenSet(int bit)
Definition: rlplc.cpp:390
int floatHasDecreased(int index)
Definition: rlplc.cpp:145
int intHasDecreased(int index)
Definition: rlplc.cpp:138
int deltaInt()
Definition: rlplc.cpp:353
int intHasDecreased()
Definition: rlplc.cpp:335
void rememberState()
Definition: rlplc.cpp:89
int * i
Definition: rlplc.h:27
int maxInt()
Definition: rlplc.cpp:225
double getOldDouble(int index)
Definition: rlplc.cpp:270
void clear(int bit)
Definition: rlplc.cpp:373
int doubleHasIncreased(int index)
Definition: rlplc.cpp:131
void rememberState()
Definition: rlplc.cpp:292
double d_old
Definition: rlplc.h:72
void clear()
Definition: rlplc.cpp:79
int floatHasDecreased()
Definition: rlplc.cpp:341
double getDouble(int index)
Definition: rlplc.cpp:252
int maxDouble()
Definition: rlplc.cpp:235
rlSharedMemory * shm
Definition: rlplc.h:59
int max_double
Definition: rlplc.h:62
double deltaDouble(int index)
Definition: rlplc.cpp:171
int hasBeenCleared(int index, int bit)
Definition: rlplc.cpp:214
int floatHasIncreased()
Definition: rlplc.cpp:323
int max_float
Definition: rlplc.h:62
int floatHasIncreased(int index)
Definition: rlplc.cpp:124
int isClear(int index, int bit)
Definition: rlplc.cpp:196
int isSet(int bit)
Definition: rlplc.cpp:378
int getInt(int index)
Definition: rlplc.cpp:240
int * i_old
Definition: rlplc.h:27
int intHasIncreased(int index)
Definition: rlplc.cpp:117
int isClear(int bit)
Definition: rlplc.cpp:384
int hasBeenCleared(int bit)
Definition: rlplc.cpp:400
int doubleChanged()
Definition: rlplc.cpp:311
double * d_old
Definition: rlplc.h:29
float f_old
Definition: rlplc.h:71
virtual ~rlPlcMem()
Definition: rlplc.cpp:288
float deltaFloat()
Definition: rlplc.cpp:358
float * f_old
Definition: rlplc.h:28
int deltaInt(int index)
Definition: rlplc.cpp:159
float f
Definition: rlplc.h:71
void set(int bit)
Definition: rlplc.cpp:368