PikaStdTask_Task.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include "BaseObj.h"
  2. #include "PikaVM.h"
  3. void PikaStdTask_Task___init__(PikaObj* self) {
  4. obj_setInt(self, "is_period", 0);
  5. }
  6. void PikaStdTask_Task_call_always(PikaObj* self, Arg* fun_todo) {
  7. obj_setArg(self, "fun_todo", fun_todo);
  8. PIKA_PYTHON_BEGIN
  9. /* clang-format off */
  10. PIKA_PYTHON(
  11. calls.append('always')
  12. calls.append(fun_todo)
  13. )
  14. /* clang-format on */
  15. const uint8_t bytes[] = {
  16. 0x10, 0x00, 0x00, 0x00, /* instruct array size */
  17. 0x10, 0x83, 0x01, 0x00, 0x00, 0x02, 0x08, 0x00, 0x10, 0x81, 0x15,
  18. 0x00, 0x00, 0x02, 0x08, 0x00, /* instruct array */
  19. 0x1e, 0x00, 0x00, 0x00, /* const pool size */
  20. 0x00, 0x61, 0x6c, 0x77, 0x61, 0x79, 0x73, 0x00, 0x63, 0x61, 0x6c,
  21. 0x6c, 0x73, 0x2e, 0x61, 0x70, 0x70, 0x65, 0x6e, 0x64, 0x00, 0x66,
  22. 0x75, 0x6e, 0x5f, 0x74, 0x6f, 0x64, 0x6f, 0x00, /* const pool */
  23. };
  24. PIKA_PYTHON_END
  25. pikaVM_runByteCode(self, (uint8_t*)bytes);
  26. }
  27. void PikaStdTask_Task_call_when(PikaObj* self, Arg* fun_todo, Arg* fun_when) {
  28. obj_setArg(self, "fun_todo", fun_todo);
  29. obj_setArg(self, "fun_when", fun_when);
  30. PIKA_PYTHON_BEGIN
  31. /* clang-format off */
  32. PIKA_PYTHON(
  33. calls.append('when')
  34. calls.append(fun_when)
  35. calls.append(fun_todo)
  36. )
  37. /* clang-format on */
  38. const uint8_t bytes[] = {
  39. 0x18, 0x00, 0x00, 0x00, /* instruct array size */
  40. 0x10, 0x83, 0x01, 0x00, 0x00, 0x02, 0x06, 0x00, 0x10, 0x81, 0x13, 0x00,
  41. 0x00, 0x02, 0x06, 0x00, 0x10, 0x81, 0x1c, 0x00, 0x00, 0x02, 0x06, 0x00,
  42. /* instruct array */
  43. 0x25, 0x00, 0x00, 0x00, /* const pool size */
  44. 0x00, 0x77, 0x68, 0x65, 0x6e, 0x00, 0x63, 0x61, 0x6c, 0x6c, 0x73, 0x2e,
  45. 0x61, 0x70, 0x70, 0x65, 0x6e, 0x64, 0x00, 0x66, 0x75, 0x6e, 0x5f, 0x77,
  46. 0x68, 0x65, 0x6e, 0x00, 0x66, 0x75, 0x6e, 0x5f, 0x74, 0x6f, 0x64, 0x6f,
  47. 0x00,
  48. /* const pool */
  49. };
  50. PIKA_PYTHON_END
  51. pikaVM_runByteCode(self, (uint8_t*)bytes);
  52. }
  53. void PikaStdTask_Task_call_period_ms(PikaObj* self,
  54. Arg* fun_todo,
  55. int period_ms) {
  56. obj_setArg(self, "fun_todo", fun_todo);
  57. obj_setInt(self, "period_ms", period_ms);
  58. PIKA_PYTHON_BEGIN
  59. /* clang-format off */
  60. PIKA_PYTHON(
  61. calls.append('period_ms')
  62. calls.append(period_ms)
  63. calls.append(fun_todo)
  64. calls.append(0)
  65. is_period = 1
  66. )
  67. /* clang-format on */
  68. const uint8_t bytes[] =
  69. {
  70. 0x28, 0x00, 0x00, 0x00, /* instruct array size */
  71. 0x10, 0x83, 0x01, 0x00, 0x00, 0x02, 0x0b, 0x00, 0x10, 0x81, 0x01,
  72. 0x00, 0x00, 0x02, 0x0b, 0x00, 0x10, 0x81, 0x18, 0x00, 0x00, 0x02,
  73. 0x0b, 0x00, 0x10, 0x85, 0x21, 0x00, 0x00, 0x02, 0x0b, 0x00, 0x00,
  74. 0x85, 0x23, 0x00, 0x00, 0x04, 0x25, 0x00, /* instruct array */
  75. 0x2f, 0x00, 0x00, 0x00, /* const pool size */
  76. 0x00, 0x70, 0x65, 0x72, 0x69, 0x6f, 0x64, 0x5f, 0x6d, 0x73, 0x00,
  77. 0x63, 0x61, 0x6c, 0x6c, 0x73, 0x2e, 0x61, 0x70, 0x70, 0x65, 0x6e,
  78. 0x64, 0x00, 0x66, 0x75, 0x6e, 0x5f, 0x74, 0x6f, 0x64, 0x6f, 0x00,
  79. 0x30, 0x00, 0x31, 0x00, 0x69, 0x73, 0x5f, 0x70, 0x65, 0x72, 0x69,
  80. 0x6f, 0x64, 0x00, /* const pool */
  81. };
  82. PIKA_PYTHON_END
  83. pikaVM_runByteCode(self, (uint8_t*)bytes);
  84. }
  85. void PikaStdTask_Task_run_once(PikaObj* self) {
  86. /* clang-format off */
  87. PIKA_PYTHON(
  88. len = calls.len()
  89. mode = 'none'
  90. info_index = 0
  91. for i in range(0, len):
  92. if len == 0:
  93. break
  94. if info_index == 0:
  95. mode = calls[i]
  96. info_index = 1
  97. elif info_index == 1:
  98. if mode == 'always':
  99. todo = calls[i]
  100. todo()
  101. info_index = 0
  102. elif mode == 'when':
  103. when = calls[i]
  104. info_index = 2
  105. elif mode == 'period_ms':
  106. period_ms = calls[i]
  107. info_index = 2
  108. elif info_index == 2:
  109. if mode == 'when':
  110. if when():
  111. todo = calls[i]
  112. todo()
  113. info_index = 0
  114. elif mode == 'period_ms':
  115. todo = calls[i]
  116. info_index = 3
  117. elif info_index == 3:
  118. if mode == 'period_ms':
  119. if tick > calls[i]:
  120. todo()
  121. calls[i] = tick + period_ms
  122. info_index = 0
  123. )
  124. /* clang-format on */
  125. const uint8_t bytes[] = {
  126. 0xf0, 0x01, 0x00, 0x00, /* instruct array size */
  127. 0x00, 0x82, 0x01, 0x00, 0x00, 0x04, 0x0b, 0x00, 0x00, 0x83, 0x0f, 0x00,
  128. 0x00, 0x04, 0x14, 0x00, 0x00, 0x85, 0x19, 0x00, 0x00, 0x04, 0x1b, 0x00,
  129. 0x20, 0x85, 0x19, 0x00, 0x20, 0x01, 0x0b, 0x00, 0x10, 0x02, 0x26, 0x00,
  130. 0x00, 0x02, 0x2c, 0x00, 0x00, 0x04, 0x31, 0x00, 0x00, 0x82, 0x35, 0x00,
  131. 0x00, 0x04, 0x42, 0x00, 0x00, 0x0d, 0x42, 0x00, 0x00, 0x07, 0x44, 0x00,
  132. 0x11, 0x81, 0x0b, 0x00, 0x11, 0x05, 0x19, 0x00, 0x01, 0x08, 0x46, 0x00,
  133. 0x01, 0x07, 0x49, 0x00, 0x02, 0x8e, 0x00, 0x00, 0x11, 0x81, 0x1b, 0x00,
  134. 0x11, 0x05, 0x19, 0x00, 0x01, 0x08, 0x46, 0x00, 0x01, 0x07, 0x49, 0x00,
  135. 0x12, 0x81, 0x4b, 0x00, 0x12, 0x01, 0x42, 0x00, 0x02, 0x1d, 0x00, 0x00,
  136. 0x02, 0x04, 0x14, 0x00, 0x02, 0x85, 0x49, 0x00, 0x02, 0x04, 0x1b, 0x00,
  137. 0x01, 0x8b, 0x49, 0x00, 0x11, 0x01, 0x1b, 0x00, 0x11, 0x05, 0x49, 0x00,
  138. 0x01, 0x08, 0x46, 0x00, 0x01, 0x07, 0x49, 0x00, 0x12, 0x81, 0x14, 0x00,
  139. 0x12, 0x03, 0x51, 0x00, 0x02, 0x08, 0x46, 0x00, 0x02, 0x07, 0x49, 0x00,
  140. 0x13, 0x81, 0x4b, 0x00, 0x13, 0x01, 0x42, 0x00, 0x03, 0x1d, 0x00, 0x00,
  141. 0x03, 0x04, 0x58, 0x00, 0x03, 0x82, 0x58, 0x00, 0x03, 0x85, 0x19, 0x00,
  142. 0x03, 0x04, 0x1b, 0x00, 0x02, 0x8b, 0x49, 0x00, 0x12, 0x01, 0x14, 0x00,
  143. 0x12, 0x03, 0x5d, 0x00, 0x02, 0x08, 0x46, 0x00, 0x02, 0x07, 0x49, 0x00,
  144. 0x13, 0x81, 0x4b, 0x00, 0x13, 0x01, 0x42, 0x00, 0x03, 0x1d, 0x00, 0x00,
  145. 0x03, 0x04, 0x5d, 0x00, 0x03, 0x85, 0x44, 0x00, 0x03, 0x04, 0x1b, 0x00,
  146. 0x02, 0x8b, 0x49, 0x00, 0x12, 0x01, 0x14, 0x00, 0x12, 0x03, 0x62, 0x00,
  147. 0x02, 0x08, 0x46, 0x00, 0x02, 0x07, 0x49, 0x00, 0x13, 0x81, 0x4b, 0x00,
  148. 0x13, 0x01, 0x42, 0x00, 0x03, 0x1d, 0x00, 0x00, 0x03, 0x04, 0x62, 0x00,
  149. 0x03, 0x85, 0x44, 0x00, 0x03, 0x04, 0x1b, 0x00, 0x01, 0x8b, 0x49, 0x00,
  150. 0x11, 0x01, 0x1b, 0x00, 0x11, 0x05, 0x44, 0x00, 0x01, 0x08, 0x46, 0x00,
  151. 0x01, 0x07, 0x49, 0x00, 0x12, 0x81, 0x14, 0x00, 0x12, 0x03, 0x5d, 0x00,
  152. 0x02, 0x08, 0x46, 0x00, 0x02, 0x07, 0x49, 0x00, 0x03, 0x82, 0x5d, 0x00,
  153. 0x03, 0x07, 0x49, 0x00, 0x14, 0x81, 0x4b, 0x00, 0x14, 0x01, 0x42, 0x00,
  154. 0x04, 0x1d, 0x00, 0x00, 0x04, 0x04, 0x58, 0x00, 0x04, 0x82, 0x58, 0x00,
  155. 0x03, 0x85, 0x19, 0x00, 0x03, 0x04, 0x1b, 0x00, 0x02, 0x8b, 0x49, 0x00,
  156. 0x12, 0x01, 0x14, 0x00, 0x12, 0x03, 0x62, 0x00, 0x02, 0x08, 0x46, 0x00,
  157. 0x02, 0x07, 0x49, 0x00, 0x13, 0x81, 0x4b, 0x00, 0x13, 0x01, 0x42, 0x00,
  158. 0x03, 0x1d, 0x00, 0x00, 0x03, 0x04, 0x58, 0x00, 0x03, 0x85, 0x6c, 0x00,
  159. 0x03, 0x04, 0x1b, 0x00, 0x01, 0x8b, 0x49, 0x00, 0x11, 0x01, 0x1b, 0x00,
  160. 0x11, 0x05, 0x6c, 0x00, 0x01, 0x08, 0x46, 0x00, 0x01, 0x07, 0x49, 0x00,
  161. 0x12, 0x81, 0x14, 0x00, 0x12, 0x03, 0x62, 0x00, 0x02, 0x08, 0x46, 0x00,
  162. 0x02, 0x07, 0x49, 0x00, 0x13, 0x81, 0x6e, 0x00, 0x23, 0x01, 0x4b, 0x00,
  163. 0x23, 0x01, 0x42, 0x00, 0x13, 0x1d, 0x00, 0x00, 0x03, 0x08, 0x73, 0x00,
  164. 0x03, 0x07, 0x49, 0x00, 0x04, 0x82, 0x58, 0x00, 0x14, 0x81, 0x4b, 0x00,
  165. 0x14, 0x01, 0x42, 0x00, 0x24, 0x01, 0x6e, 0x00, 0x24, 0x01, 0x62, 0x00,
  166. 0x14, 0x08, 0x75, 0x00, 0x04, 0x02, 0x77, 0x00, 0x04, 0x04, 0x4b, 0x00,
  167. 0x03, 0x85, 0x19, 0x00, 0x03, 0x04, 0x1b, 0x00, 0x00, 0x86, 0x83, 0x00,
  168. 0x00, 0x8c, 0x31, 0x00, /* instruct array */
  169. 0x86, 0x00, 0x00, 0x00, /* const pool size */
  170. 0x00, 0x63, 0x61, 0x6c, 0x6c, 0x73, 0x2e, 0x6c, 0x65, 0x6e, 0x00, 0x6c,
  171. 0x65, 0x6e, 0x00, 0x6e, 0x6f, 0x6e, 0x65, 0x00, 0x6d, 0x6f, 0x64, 0x65,
  172. 0x00, 0x30, 0x00, 0x69, 0x6e, 0x66, 0x6f, 0x5f, 0x69, 0x6e, 0x64, 0x65,
  173. 0x78, 0x00, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x00, 0x69, 0x74, 0x65, 0x72,
  174. 0x00, 0x5f, 0x6c, 0x30, 0x00, 0x5f, 0x6c, 0x30, 0x2e, 0x5f, 0x5f, 0x6e,
  175. 0x65, 0x78, 0x74, 0x5f, 0x5f, 0x00, 0x69, 0x00, 0x32, 0x00, 0x3d, 0x3d,
  176. 0x00, 0x31, 0x00, 0x63, 0x61, 0x6c, 0x6c, 0x73, 0x00, 0x61, 0x6c, 0x77,
  177. 0x61, 0x79, 0x73, 0x00, 0x74, 0x6f, 0x64, 0x6f, 0x00, 0x77, 0x68, 0x65,
  178. 0x6e, 0x00, 0x70, 0x65, 0x72, 0x69, 0x6f, 0x64, 0x5f, 0x6d, 0x73, 0x00,
  179. 0x33, 0x00, 0x74, 0x69, 0x63, 0x6b, 0x00, 0x3e, 0x00, 0x2b, 0x00, 0x5f,
  180. 0x5f, 0x73, 0x65, 0x74, 0x69, 0x74, 0x65, 0x6d, 0x5f, 0x5f, 0x00, 0x2d,
  181. 0x31, 0x00, /* const pool */
  182. };
  183. pikaVM_runByteCode(self, (uint8_t*)bytes);
  184. }
  185. void __Task_update_tick(PikaObj* self) {
  186. if (obj_getInt(self, "is_perod")) {
  187. obj_runNativeMethod(self, "platformGetTick", NULL);
  188. }
  189. }
  190. void PikaStdTask_Task_run_forever(PikaObj* self) {
  191. while (1) {
  192. __Task_update_tick(self);
  193. PikaStdTask_Task_run_once(self);
  194. pika_platform_thread_yield();
  195. }
  196. }
  197. void PikaStdTask_Task_run_until_ms(PikaObj* self, int until_ms) {
  198. while (1) {
  199. __Task_update_tick(self);
  200. PikaStdTask_Task_run_once(self);
  201. if (obj_getInt(self, "tick") > until_ms) {
  202. return;
  203. }
  204. pika_platform_thread_yield();
  205. }
  206. }
  207. void PikaStdTask_Task_platformGetTick(PikaObj* self) {
  208. obj_setInt(self, "tick", pika_platform_get_tick());
  209. }