pikapython/package/PikaStdTask/PikaStdTask_Task.c

206 lines
6.5 KiB
C
Raw Normal View History

2022-01-20 18:16:08 +08:00
#include "PikaStdTask_Task.h"
2022-01-20 21:50:38 +08:00
#include "BaseObj.h"
2022-01-20 18:16:08 +08:00
#include "PikaVM.h"
extern PikaObj* __pikaMain;
void PikaStdTask_Task___init__(PikaObj* self) {
2022-01-20 21:59:43 +08:00
pikaVM_runAsm(self,
"B0\n"
"0 RUN calls_always.__init__\n"
"0 RUN calls_when.__init__\n"
"0 RUN assert_when.__init__\n"
"0 RUN calls_period.__init__\n"
"0 RUN assert_period.__init__\n"
"0 RUN time_period.__init__\n");
2022-01-21 02:03:36 +08:00
obj_setPtr(__pikaMain, "__calls_always", obj_getPtr(self, "calls_always"));
obj_setPtr(__pikaMain, "__calls_when", obj_getPtr(self, "calls_when"));
obj_setPtr(__pikaMain, "__assert_when", obj_getPtr(self, "assert_when"));
obj_setPtr(__pikaMain, "__calls_period", obj_getPtr(self, "calls_period"));
obj_setPtr(__pikaMain, "__assert_period",
obj_getPtr(self, "assert_period"));
obj_setPtr(__pikaMain, "__time_period", obj_getPtr(self, "time_period"));
2022-01-20 18:16:08 +08:00
}
2022-01-20 21:50:38 +08:00
2022-01-20 18:16:08 +08:00
void PikaStdTask_Task_call_always(PikaObj* self, Arg* fun_todo) {
obj_setArg(self, "fun_todo", fun_todo);
2022-01-23 00:13:16 +08:00
pikaVM_runAsm(self, "B0\n1 REF fun_todo\n0 RUN calls_always.append\n");
2022-01-20 18:16:08 +08:00
}
2022-01-20 21:50:38 +08:00
2022-01-21 02:03:36 +08:00
void PikaStdTask_Task_call_when(PikaObj* self, Arg* fun_todo, Arg* fun_when) {
obj_setArg(self, "fun_todo", fun_todo);
obj_setArg(self, "fun_when", fun_when);
pikaVM_runAsm(self,
"B0\n"
"1 REF fun_todo\n"
"0 RUN calls_when.append\n"
"B0\n"
"1 REF fun_when\n"
"0 RUN assert_when.append\n");
}
void PikaStdTask_Task_call_period_ms(PikaObj* self,
Arg* fun_todo,
int period_ms) {
obj_setArg(self, "fun_todo", fun_todo);
obj_setInt(self, "period_ms", period_ms);
pikaVM_runAsm(self,
"B0\n"
"1 REF fun_todo\n"
"0 RUN calls_period.append\n"
"B0\n"
"1 REF period_ms\n"
"0 RUN assert_period.append\n"
"B0\n"
"1 REF 0\n"
"0 RUN time_period.append\n");
2022-01-21 02:03:36 +08:00
}
2022-01-20 18:16:08 +08:00
void PikaStdTask_Task_run_once(PikaObj* self) {
obj_run(self, "platformGetTick()");
/* transfer the tick to pikaMain */
obj_setInt(__pikaMain, "__tick", obj_getInt(self, "tick"));
/* Python
__len = __calls_period.len()
for i in range(0, __len):
if __len == 0:
break
time = __time_period[i]
if __tick > __time_period[i]:
todo = __calls_period[i]
todo()
__time_period[i] = __tick + __assert_period[i]
*/
pikaVM_runAsm(__pikaMain,
"B0\n"
"0 RUN __calls_period.len\n"
"0 OUT __len\n"
"B0\n"
"2 NUM 0\n"
"2 REF __len\n"
"1 RUN range\n"
"0 RUN iter\n"
"0 OUT _l0\n"
"0 REF _r1\n"
"0 REF _r2\n"
"0 REF _r3\n"
"0 OUT _l0.a1\n"
"0 OUT _l0.a2\n"
"0 OUT _l0.a3\n"
"B0\n"
"0 RUN _l0.__next__\n"
"0 OUT i\n"
"0 EST i\n"
"0 JEZ 2\n"
"B1\n"
"1 REF __len\n"
"1 NUM 0\n"
"0 OPT ==\n"
"0 JEZ 1\n"
"B2\n"
"0 BRK\n"
"B1\n"
"1 REF __time_period\n"
"1 REF i\n"
"0 RUN __get__\n"
"0 OUT time\n"
"B1\n"
"1 REF __tick\n"
"2 REF __time_period\n"
"2 REF i\n"
"1 RUN __get__\n"
"0 OPT >\n"
"0 JEZ 1\n"
"B2\n"
"1 REF __calls_period\n"
"1 REF i\n"
"0 RUN __get__\n"
"0 OUT todo\n"
"B2\n"
"0 RUN todo\n"
"B2\n"
"1 REF __time_period\n"
"1 REF i\n"
"2 REF __tick\n"
"3 REF __assert_period\n"
"3 REF i\n"
"2 RUN __get__\n"
"1 OPT +\n"
"1 STR __time_period\n"
"0 RUN __set__\n"
"B0\n"
"0 JMP -1\n"
"B0\n"
"0 DEL _l0\n"
"B0 \n");
/* Python
__len = __calls_when.len()
for i in range(0, __len):
if __len == 0:
break
when = __assert_when[i]
if when():
todo = __calls_when[i]
todo()
*/
pikaVM_runAsm(__pikaMain,
"B0\n"
"0 RUN __calls_when.len\n"
"0 OUT __len\n"
"B0\n"
"2 NUM 0\n"
"2 REF __len\n"
"1 RUN range\n"
"0 RUN iter\n"
"0 OUT _l0\n"
"0 REF _r1\n"
"0 REF _r2\n"
"0 REF _r3\n"
"0 OUT _l0.a1\n"
"0 OUT _l0.a2\n"
"0 OUT _l0.a3\n"
"B0\n"
"0 RUN _l0.__next__\n"
"0 OUT i\n"
"0 EST i\n"
"0 JEZ 2\n"
"B1\n"
"1 REF __len\n"
"1 NUM 0\n"
"0 OPT ==\n"
"0 JEZ 1\n"
"B2\n"
"0 BRK\n"
"B1\n"
"1 REF __assert_when\n"
"1 REF i\n"
"0 RUN __get__\n"
"0 OUT when\n"
"B1\n"
"0 RUN when\n"
"0 JEZ 1\n"
"B2\n"
"1 REF __calls_when\n"
"1 REF i\n"
"0 RUN __get__\n"
"0 OUT todo\n"
"B2\n"
"0 RUN todo\n"
"B0\n"
"0 JMP -1\n"
"B0\n"
"0 DEL _l0\n"
"B0\n");
2022-01-20 18:16:08 +08:00
}
2022-01-20 21:50:38 +08:00
void PikaStdTask_Task_run_always(PikaObj* self) {
while (1) {
2022-01-20 18:16:08 +08:00
PikaStdTask_Task_run_once(self);
}
}
void PikaStdTask_Task_platformGetTick(PikaObj* self) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error] platform method need to be override.");
}