|
@@ -1531,33 +1531,28 @@ const get_c_code = () => {
|
|
|
}
|
|
|
let c_code = ''
|
|
|
if (crtProject.value) {
|
|
|
- c_code = `
|
|
|
- #include "iec_def.h"
|
|
|
+ c_code = `#include "iec_def.h"
|
|
|
|
|
|
- TIME __CURRENT_TIME;
|
|
|
- uint8_t coil[20];
|
|
|
- uint16_t ai[4];
|
|
|
- uint16_t ao[2];
|
|
|
-
|
|
|
- extern uint8_t reg[70];
|
|
|
- `
|
|
|
+extern uint8_t coil[20];
|
|
|
+extern uint16_t ai[4];
|
|
|
+extern uint16_t ao[2];`
|
|
|
let call_code = ''
|
|
|
crtProject.value.commandList.forEach((item, index) => {
|
|
|
if (!item.stepList.some((cItem) => cItem.type === 'exec' && cItem.list?.length !== 0)) return
|
|
|
- call_code += `PROGRAM${index}_body(&p${index}, &p${index}_delay);`
|
|
|
+ call_code += `
|
|
|
+ PROGRAM${index}_body(&p${index}, &p${index}_delay);`
|
|
|
let body_code = `
|
|
|
- static struct pt p${index};
|
|
|
- static TIME p${index}_delay;
|
|
|
- static PT_THREAD(PROGRAM${index}_body(struct pt *pt, TIME *pt_delay))
|
|
|
- {
|
|
|
- PT_BEGIN(pt);
|
|
|
- `
|
|
|
+
|
|
|
+static struct pt p${index};
|
|
|
+static TIME p${index}_delay;
|
|
|
+static PT_THREAD(PROGRAM${index}_body(struct pt *pt, TIME *pt_delay))
|
|
|
+{
|
|
|
+ PT_BEGIN(pt);`
|
|
|
item.stepList.forEach((sItem, sIndex) => {
|
|
|
if (sItem.type === 'delay') {
|
|
|
body_code += `
|
|
|
- delay_set(pt_delay, ${+sItem.value! * 1000});
|
|
|
- PT_WAIT_UNTIL(pt, delay_expired(pt_delay));
|
|
|
- `
|
|
|
+ delay_set(pt_delay, ${+sItem.value! * 1000});
|
|
|
+ PT_WAIT_UNTIL(pt, delay_expired(pt_delay));`
|
|
|
} else {
|
|
|
let condition_code = ''
|
|
|
sItem.list!.forEach((cItem, cIndex) => {
|
|
@@ -1599,64 +1594,34 @@ const get_c_code = () => {
|
|
|
if (cItem.label.includes('DO')) {
|
|
|
const idx = cItem.label.slice(2)
|
|
|
body_code += `
|
|
|
- SET_DO(${idx}, ${valMap[cItem.value]});
|
|
|
- `
|
|
|
+ SET_DO(${idx}, ${valMap[cItem.value]});`
|
|
|
}
|
|
|
if (cItem.label.includes('AO')) {
|
|
|
const idx = cItem.label.slice(2)
|
|
|
body_code += `
|
|
|
- SET_AO(${idx}, ${+cItem.value * 100});
|
|
|
- `
|
|
|
+ SET_AO(${idx}, ${+cItem.value * 100});`
|
|
|
}
|
|
|
}
|
|
|
})
|
|
|
if (condition_code !== '') {
|
|
|
body_code += `
|
|
|
- if (!(${condition_code}))
|
|
|
- { PT_EXIT(pt); }
|
|
|
- `
|
|
|
+ if (!(${condition_code}))
|
|
|
+ {
|
|
|
+ PT_EXIT(pt);
|
|
|
+ }`
|
|
|
}
|
|
|
}
|
|
|
})
|
|
|
body_code += `
|
|
|
- PT_END(pt);
|
|
|
- }
|
|
|
- `
|
|
|
+ PT_END(pt);
|
|
|
+}`
|
|
|
c_code += body_code
|
|
|
})
|
|
|
|
|
|
c_code += `
|
|
|
- void IEC_body(void)
|
|
|
- {
|
|
|
- extern uint32_t usec;
|
|
|
- __CURRENT_TIME.tv_sec = ((uint32_t)reg[54] << 24) | ((uint32_t)reg[55] << 16) | ((uint32_t)reg[56] << 8) | (uint32_t)reg[57];
|
|
|
- __CURRENT_TIME.tv_nsec = usec + TIM_Get();
|
|
|
- for (int i = 0; i < 10; i++)
|
|
|
- {
|
|
|
- coil[i] = DI(i);
|
|
|
- }
|
|
|
- for (int i = 0; i < 4; i++)
|
|
|
- {
|
|
|
- ai[i] = AI(i);
|
|
|
- reg[2 * i + 58] = ai[i] >> 8;
|
|
|
- reg[2 * i + 59] = ai[i];
|
|
|
- }
|
|
|
- // 下面需要动态生成,用于程序中的函数调用
|
|
|
- ${call_code}
|
|
|
- // 动态生成结束
|
|
|
- for (int i = 0; i < 10; i++)
|
|
|
- {
|
|
|
- DO(i, coil[i + 10]);
|
|
|
- }
|
|
|
- for (int i = 0; i < 2; i++)
|
|
|
- {
|
|
|
- AO(i, ao[i]);
|
|
|
- reg[2 * i + 66] = ao[i] >> 8;
|
|
|
- reg[2 * i + 67] = ao[i];
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- `
|
|
|
+void IEC_run(void)
|
|
|
+{${call_code}
|
|
|
+}`
|
|
|
}
|
|
|
console.log(c_code)
|
|
|
const params = {
|