84 } |
84 } |
85 /** |
85 /** |
86 * Called by the PLC, each time a python_eval |
86 * Called by the PLC, each time a python_eval |
87 * FB instance is executed |
87 * FB instance is executed |
88 */ |
88 */ |
89 void __PythonEvalFB(PYTHON_EVAL* data__) |
89 void __PythonEvalFB(int poll, PYTHON_EVAL* data__) |
90 { |
90 { |
91 /* detect rising edge on TRIG */ |
91 /* detect rising edge on TRIG to trigger evaluation */ |
92 if(data__->TRIG && !data__->TRIGM1 && data__->TRIGGED == 0){ |
92 if(((data__->TRIG && !data__->TRIGM1) || |
|
93 /* polling is equivalent to trig on value rather than on rising edge*/ |
|
94 (poll && data__->TRIG )) && |
|
95 /* trig only if not already trigged */ |
|
96 data__->TRIGGED == 0){ |
93 /* mark as trigged */ |
97 /* mark as trigged */ |
94 data__->TRIGGED = 1; |
98 data__->TRIGGED = 1; |
95 /* make a safe copy of the code */ |
99 /* make a safe copy of the code */ |
96 data__->PREBUFFER = data__->CODE; |
100 data__->PREBUFFER = data__->CODE; |
97 } |
101 } |
98 /* retain value for next detection */ |
102 /* retain value for next rising edge detection */ |
99 data__->TRIGM1 = data__->TRIG; |
103 data__->TRIGM1 = data__->TRIG; |
100 |
104 |
101 /* python thread is not in ? */ |
105 /* python thread is not in ? */ |
102 if( PythonState & PYTHON_LOCKED_BY_PLC){ |
106 if( PythonState & PYTHON_LOCKED_BY_PLC){ |
103 /* got the order to act */ |
107 /* if some answer are waiting, publish*/ |
|
108 if(data__->STATE == PYTHON_FB_ANSWERED){ |
|
109 /* Copy buffer content into result*/ |
|
110 data__->RESULT = data__->BUFFER; |
|
111 /* signal result presece to PLC*/ |
|
112 data__->ACK = 1; |
|
113 /* Mark as free */ |
|
114 data__->STATE = PYTHON_FB_FREE; |
|
115 /* mark as not trigged */ |
|
116 if(!poll) |
|
117 data__->TRIGGED = 0; |
|
118 /*printf("__PythonEvalFB pop %%d - %%*s\n",Current_PLC_EvalFB, data__->BUFFER.len, data__->BUFFER.body);*/ |
|
119 }else if(poll){ |
|
120 /* when in polling, no answer == ack down */ |
|
121 data__->ACK = 0; |
|
122 } |
|
123 /* got the order to act ?*/ |
104 if(data__->TRIGGED == 1 && |
124 if(data__->TRIGGED == 1 && |
105 /* and not already being processed */ |
125 /* and not already being processed */ |
106 data__->STATE == PYTHON_FB_FREE) |
126 data__->STATE == PYTHON_FB_FREE) |
107 { |
127 { |
108 /* Enter the block in the fifo |
128 /* Enter the block in the fifo |
111 * be requested twice */ |
131 * be requested twice */ |
112 EvalFBs[Current_PLC_EvalFB] = data__; |
132 EvalFBs[Current_PLC_EvalFB] = data__; |
113 /* copy into BUFFER local*/ |
133 /* copy into BUFFER local*/ |
114 data__->BUFFER = data__->PREBUFFER; |
134 data__->BUFFER = data__->PREBUFFER; |
115 /* Set ACK pin to low so that we can set a rising edge on result */ |
135 /* Set ACK pin to low so that we can set a rising edge on result */ |
116 data__->ACK = 0; |
136 if(!poll){ |
|
137 /* when not polling, a new answer imply reseting ack*/ |
|
138 data__->ACK = 0; |
|
139 }else{ |
|
140 /* when in polling, acting reset trigger */ |
|
141 data__->TRIGGED = 0; |
|
142 } |
117 /* Mark FB busy */ |
143 /* Mark FB busy */ |
118 data__->STATE = PYTHON_FB_REQUESTED; |
144 data__->STATE = PYTHON_FB_REQUESTED; |
119 /* Have to wakeup python thread in case he was asleep */ |
145 /* Have to wakeup python thread in case he was asleep */ |
120 PythonState |= PYTHON_MUSTWAKEUP; |
146 PythonState |= PYTHON_MUSTWAKEUP; |
121 //printf("__PythonEvalFB push %%d - %%*s\n",Current_PLC_EvalFB, data__->BUFFER.len, data__->BUFFER.body); |
147 /*printf("__PythonEvalFB push %%d - %%*s\n",Current_PLC_EvalFB, data__->BUFFER.len, data__->BUFFER.body);*/ |
122 /* Get a new line */ |
148 /* Get a new line */ |
123 Current_PLC_EvalFB = (Current_PLC_EvalFB + 1) %% %(python_eval_fb_count)d; |
149 Current_PLC_EvalFB = (Current_PLC_EvalFB + 1) %% %(python_eval_fb_count)d; |
124 }else if(data__->STATE == PYTHON_FB_ANSWERED){ |
|
125 /* Copy buffer content into result*/ |
|
126 data__->RESULT = data__->BUFFER; |
|
127 /* signal result presece to PLC*/ |
|
128 data__->ACK = 1; |
|
129 /* Mark as free */ |
|
130 data__->STATE = PYTHON_FB_FREE; |
|
131 /* mark as not trigged */ |
|
132 data__->TRIGGED = 0; |
|
133 //printf("__PythonEvalFB pop %%d - %%*s\n",Current_PLC_EvalFB, data__->BUFFER.len, data__->BUFFER.body); |
|
134 } |
150 } |
135 } |
151 } |
136 } |
152 } |
137 |
153 |
138 char* PythonIterator(char* result) |
154 char* PythonIterator(char* result) |