py_ext/plc_python.c
author Edouard Tisserant <edouard.tisserant@gmail.com>
Mon, 22 May 2023 14:37:15 +0200
changeset 3812 92e528718733
parent 2272 28b0a783975e
permissions -rw-r--r--
CLI: simpler status polling, fixing pyro5 thread
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     1
/*
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     2
 * Python Asynchronous execution code
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     3
 *
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     4
 * PLC put python commands in a fifo, respecting execution order
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     5
 * with the help of C pragmas inserted in python_eval FB code
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     6
 *
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     7
 * Buffer content is read asynchronously, (from non real time part),
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     8
 * commands are executed and result stored for later use by PLC.
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     9
 *
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    10
 * In this implementation, fifo is a list of pointer to python_eval
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    11
 * function blocks structures. Some local variables have been added in
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    12
 * python_eval interface. We use those local variables as buffer and state
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    13
 * flags.
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    14
 *
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    15
 * */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    16
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    17
#include "iec_types_all.h"
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    18
#include "POUS.h"
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    19
#include <string.h>
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    20
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    21
/* The fifo (fixed size, as number of FB is fixed) */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    22
static PYTHON_EVAL* EvalFBs[%(python_eval_fb_count)d];
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    23
/* Producer and consumer cursors */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    24
static int Current_PLC_EvalFB;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    25
static int Current_Python_EvalFB;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    26
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    27
/* A global IEC-Python gateway state, for use inside python_eval FBs*/
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    28
static int PythonState;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    29
#define PYTHON_LOCKED_BY_PYTHON 0
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    30
#define PYTHON_LOCKED_BY_PLC 1
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    31
#define PYTHON_MUSTWAKEUP 2
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    32
#define PYTHON_FINISHED 4
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    33
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    34
/* Each python_eval FunctionBlock have it own state */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    35
#define PYTHON_FB_FREE 0
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    36
#define PYTHON_FB_REQUESTED 1
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    37
#define PYTHON_FB_PROCESSING 2
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    38
#define PYTHON_FB_ANSWERED 3
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    39
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    40
int WaitPythonCommands(void);
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    41
void UnBlockPythonCommands(void);
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    42
int TryLockPython(void);
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    43
void UnLockPython(void);
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    44
void LockPython(void);
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    45
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 721
diff changeset
    46
int __init_py_ext()
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    47
{
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    48
	int i;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    49
	/* Initialize cursors */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    50
	Current_Python_EvalFB = 0;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    51
	Current_PLC_EvalFB = 0;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    52
	PythonState = PYTHON_LOCKED_BY_PYTHON;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    53
	for(i = 0; i < %(python_eval_fb_count)d; i++)
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    54
		EvalFBs[i] = NULL;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    55
  return 0;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    56
}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    57
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 721
diff changeset
    58
void __cleanup_py_ext()
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    59
{
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    60
	PythonState = PYTHON_FINISHED;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    61
	UnBlockPythonCommands();
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    62
}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    63
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 721
diff changeset
    64
void __retrieve_py_ext()
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    65
{
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    66
	/* Check Python thread is not being
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    67
	 * modifying internal python_eval data */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    68
	PythonState = TryLockPython() ?
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    69
	                PYTHON_LOCKED_BY_PLC :
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    70
	                PYTHON_LOCKED_BY_PYTHON;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    71
	/* If python thread _is_ in, then PythonState remains PYTHON_LOCKED_BY_PYTHON
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    72
	 * and python_eval will no do anything */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    73
}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    74
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 721
diff changeset
    75
void __publish_py_ext()
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    76
{
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    77
	if(PythonState & PYTHON_LOCKED_BY_PLC){
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    78
		/* If runnig PLC did push something in the fifo*/
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    79
		if(PythonState & PYTHON_MUSTWAKEUP){
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    80
			/* WakeUp python thread */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    81
			UnBlockPythonCommands();
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    82
		}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    83
		UnLockPython();
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    84
	}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    85
}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    86
/**
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    87
 * Called by the PLC, each time a python_eval
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    88
 * FB instance is executed
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    89
 */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    90
void __PythonEvalFB(int poll, PYTHON_EVAL* data__)
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    91
{
2272
28b0a783975e Change behaviour of python_eval function block : if TRIG is false, then ACK is also false except a pulse precisely when receiving answer from interpreter
Edouard Tisserant
parents: 1470
diff changeset
    92
    if(!__GET_VAR(data__->TRIG)){
28b0a783975e Change behaviour of python_eval function block : if TRIG is false, then ACK is also false except a pulse precisely when receiving answer from interpreter
Edouard Tisserant
parents: 1470
diff changeset
    93
        /* ACK is False when TRIG is false, except a pulse when receiving result */
28b0a783975e Change behaviour of python_eval function block : if TRIG is false, then ACK is also false except a pulse precisely when receiving answer from interpreter
Edouard Tisserant
parents: 1470
diff changeset
    94
        __SET_VAR(data__->, ACK,, 0);
28b0a783975e Change behaviour of python_eval function block : if TRIG is false, then ACK is also false except a pulse precisely when receiving answer from interpreter
Edouard Tisserant
parents: 1470
diff changeset
    95
    }
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    96
	/* detect rising edge on TRIG to trigger evaluation */
454
fd316d2babd5 Made python plugin comaptible with accessors
ed
parents: 366
diff changeset
    97
	if(((__GET_VAR(data__->TRIG) && !__GET_VAR(data__->TRIGM1)) ||
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    98
	   /* polling is equivalent to trig on value rather than on rising edge*/
454
fd316d2babd5 Made python plugin comaptible with accessors
ed
parents: 366
diff changeset
    99
	    (poll && __GET_VAR(data__->TRIG) )) &&
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   100
	    /* trig only if not already trigged */
454
fd316d2babd5 Made python plugin comaptible with accessors
ed
parents: 366
diff changeset
   101
	    __GET_VAR(data__->TRIGGED) == 0){
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   102
		/* mark as trigged */
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   103
	    __SET_VAR(data__->, TRIGGED,, 1);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   104
		/* make a safe copy of the code */
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   105
		__SET_VAR(data__->, PREBUFFER,, __GET_VAR(data__->CODE));
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   106
	}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   107
	/* retain value for next rising edge detection */
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   108
	__SET_VAR(data__->, TRIGM1,, __GET_VAR(data__->TRIG));
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   109
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   110
	/* python thread is not in ? */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   111
	if( PythonState & PYTHON_LOCKED_BY_PLC){
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   112
		/* if some answer are waiting, publish*/
454
fd316d2babd5 Made python plugin comaptible with accessors
ed
parents: 366
diff changeset
   113
		if(__GET_VAR(data__->STATE) == PYTHON_FB_ANSWERED){
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   114
			/* Copy buffer content into result*/
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   115
			__SET_VAR(data__->, RESULT,, __GET_VAR(data__->BUFFER));
2272
28b0a783975e Change behaviour of python_eval function block : if TRIG is false, then ACK is also false except a pulse precisely when receiving answer from interpreter
Edouard Tisserant
parents: 1470
diff changeset
   116
			/* signal result presence to PLC*/
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   117
			__SET_VAR(data__->, ACK,, 1);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   118
			/* Mark as free */
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   119
			__SET_VAR(data__->, STATE,, PYTHON_FB_FREE);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   120
			/* mark as not trigged */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   121
			if(!poll)
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   122
			    __SET_VAR(data__->, TRIGGED,, 0);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   123
			/*printf("__PythonEvalFB pop %%d - %%*s\n",Current_PLC_EvalFB, data__->BUFFER.len, data__->BUFFER.body);*/
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   124
		}else if(poll){
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   125
			/* when in polling, no answer == ack down */
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   126
		    __SET_VAR(data__->, ACK,, 0);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   127
		}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   128
		/* got the order to act ?*/
454
fd316d2babd5 Made python plugin comaptible with accessors
ed
parents: 366
diff changeset
   129
		if(__GET_VAR(data__->TRIGGED) == 1 &&
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   130
		   /* and not already being processed */
454
fd316d2babd5 Made python plugin comaptible with accessors
ed
parents: 366
diff changeset
   131
		   __GET_VAR(data__->STATE) == PYTHON_FB_FREE)
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   132
		{
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   133
			/* Enter the block in the fifo
613
be0ceea3b6dd fixed warnings in plc_python.c
Edouard Tisserant
parents: 483
diff changeset
   134
			 * Don't have to check if fifo cell is free
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   135
			 * as fifo size == FB count, and a FB cannot
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   136
			 * be requested twice */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   137
			EvalFBs[Current_PLC_EvalFB] = data__;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   138
			/* copy into BUFFER local*/
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   139
			__SET_VAR(data__->, BUFFER,, __GET_VAR(data__->PREBUFFER));
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   140
			/* Set ACK pin to low so that we can set a rising edge on result */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   141
			if(!poll){
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   142
				/* when not polling, a new answer imply reseting ack*/
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   143
			    __SET_VAR(data__->, ACK,, 0);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   144
			}else{
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   145
				/* when in polling, acting reset trigger */
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   146
			    __SET_VAR(data__->, TRIGGED,, 0);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   147
			}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   148
			/* Mark FB busy */
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   149
			__SET_VAR(data__->, STATE,, PYTHON_FB_REQUESTED);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   150
			/* Have to wakeup python thread in case he was asleep */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   151
			PythonState |= PYTHON_MUSTWAKEUP;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   152
			/*printf("__PythonEvalFB push %%d - %%*s\n",Current_PLC_EvalFB, data__->BUFFER.len, data__->BUFFER.body);*/
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   153
			/* Get a new line */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   154
			Current_PLC_EvalFB = (Current_PLC_EvalFB + 1) %% %(python_eval_fb_count)d;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   155
		}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   156
	}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   157
}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   158
851
666f5bdad301 Added FBID variable to PY_EVAL evaluation context. FBID does identify uniquely py_eval block instance triggering execution
Edouard Tisserant
parents: 728
diff changeset
   159
char* PythonIterator(char* result, void** id)
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   160
{
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   161
	char* next_command;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   162
	PYTHON_EVAL* data__;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   163
	//printf("PythonIterator result %%s\n", result);
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 454
diff changeset
   164
    /*emergency exit*/
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 454
diff changeset
   165
    if(PythonState & PYTHON_FINISHED) return NULL;
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   166
	/* take python mutex to prevent changing PLC data while PLC running */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   167
	LockPython();
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   168
	/* Get current FB */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   169
	data__ = EvalFBs[Current_Python_EvalFB];
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   170
	if(data__ && /* may be null at first run */
454
fd316d2babd5 Made python plugin comaptible with accessors
ed
parents: 366
diff changeset
   171
	    __GET_VAR(data__->STATE) == PYTHON_FB_PROCESSING){ /* some answer awaited*/
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   172
	   	/* If result not None */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   173
	   	if(result){
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   174
			/* Get results len */
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   175
	   	    __SET_VAR(data__->, BUFFER, .len, strlen(result));
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   176
			/* prevent results overrun */
454
fd316d2babd5 Made python plugin comaptible with accessors
ed
parents: 366
diff changeset
   177
			if(__GET_VAR(data__->BUFFER, .len) > STR_MAX_LEN)
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   178
			{
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   179
			    __SET_VAR(data__->, BUFFER, .len, STR_MAX_LEN);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   180
				/* TODO : signal error */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   181
			}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   182
			/* Copy results to buffer */
613
be0ceea3b6dd fixed warnings in plc_python.c
Edouard Tisserant
parents: 483
diff changeset
   183
			strncpy((char*)__GET_VAR(data__->BUFFER, .body), result, __GET_VAR(data__->BUFFER,.len));
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   184
	   	}else{
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   185
	   	    __SET_VAR(data__->, BUFFER, .len, 0);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   186
	   	}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   187
		/* remove block from fifo*/
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   188
		EvalFBs[Current_Python_EvalFB] = NULL;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   189
		/* Mark block as answered */
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   190
		__SET_VAR(data__->, STATE,, PYTHON_FB_ANSWERED);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   191
		/* Get a new line */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   192
		Current_Python_EvalFB = (Current_Python_EvalFB + 1) %% %(python_eval_fb_count)d;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   193
		//printf("PythonIterator ++ Current_Python_EvalFB %%d\n", Current_Python_EvalFB);
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   194
	}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   195
	/* while next slot is empty */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   196
	while(((data__ = EvalFBs[Current_Python_EvalFB]) == NULL) ||
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   197
	 	  /* or doesn't contain command */
454
fd316d2babd5 Made python plugin comaptible with accessors
ed
parents: 366
diff changeset
   198
	      __GET_VAR(data__->STATE) != PYTHON_FB_REQUESTED)
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   199
	{
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   200
		UnLockPython();
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   201
		/* wait next FB to eval */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   202
		//printf("PythonIterator wait\n");
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   203
		if(WaitPythonCommands()) return NULL;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   204
		/*emergency exit*/
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   205
		if(PythonState & PYTHON_FINISHED) return NULL;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   206
		LockPython();
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   207
	}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   208
	/* Mark block as processing */
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   209
	__SET_VAR(data__->, STATE,, PYTHON_FB_PROCESSING);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   210
	//printf("PythonIterator\n");
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   211
	/* make BUFFER a null terminated string */
1410
0d34b69cc9b5 Propagated changes made in matiec/lib/accessor.h _SET_VAR macro (Mario's matiec changes merged at 39086e324665) to py_ext library, and other (not compiled in) XML standard FB definitions.
Edouard Tisserant
parents: 1288
diff changeset
   212
	__SET_VAR(data__->, BUFFER, .body[__GET_VAR(data__->BUFFER, .len)], 0);
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   213
	/* next command is BUFFER */
613
be0ceea3b6dd fixed warnings in plc_python.c
Edouard Tisserant
parents: 483
diff changeset
   214
	next_command = (char*)__GET_VAR(data__->BUFFER, .body);
1288
adc79fc44079 Fixed two typos in py_ext : FBID was not current but previous py_eval block FBID, and compiled AST cache was filled buy never used.
Edouard Tisserant
parents: 851
diff changeset
   215
	*id=data__;
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   216
	/* free python mutex */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   217
	UnLockPython();
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   218
	/* return the next command to eval */
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   219
	return next_command;
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   220
}
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
   221