python_microbit.min.js 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  1. 'use strict';
  2. goog.provide('Blockly.Python.microbit');
  3. goog.require('Blockly.Python');
  4. Blockly.Python.addReservedWords("microbit");
  5. Blockly.Python.botbit_rec_from_microbit = function (a) {
  6. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  7. Blockly.Python.definitions_.import_math = "import math";
  8. Blockly.Python.codeFunctions_.botbit_rec_from_microbit = "display.off()\nmotion(0, 0)\nx \x3d 0\ny \x3d 0\nz \x3d 0\na \x3d 0\nleft \x3d 0\nright \x3d 0\n";
  9. return "x \x3d msg[0]*256 + msg[1]\nx \x3d x - 10000\ny \x3d msg[2]*256 + msg[3]\ny \x3d y - 10000\nz \x3d msg[4]*256 + msg[5]\nz \x3d z - 10000\na \x3d msg[6]*256 + msg[7]\nif a \x3d\x3d 0:\n left \x3d int((y + x))\n right \x3d int((y - x))\n motion(-right, -left)\nif (a \x26 0x03) !\x3d 0:\n motion(0, 0)\n y \x3d min(max(-1000, y), 1000)\n x \x3d min(max(-1000, x), 1000)\n sv \x3d math.asin(y/1000)*180/math.pi\n sh \x3d math.asin(x/1000)*180/math.pi\n sv \x3d min(max(-45, sv), 45)\n sh \x3d min(max(-45, sh), 45)\nif (a \x26 0x01) !\x3d 0:\n setServo(0, -sv)\n setServo(2, -sh)\nif (a \x26 0x02) !\x3d 0:\n setServo(1, sv)\n setServo(3, sh)\nupdatePosition()"
  10. }
  11. ;
  12. Blockly.Python.microbit_update_msg_args = function (a) {
  13. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  14. var b = Blockly.Python.valueToCode(a, "msg", Blockly.Python.ORDER_ATOMIC)
  15. , c = Blockly.Python.valueToCode(a, "x", Blockly.Python.ORDER_ATOMIC)
  16. , d = Blockly.Python.valueToCode(a, "y", Blockly.Python.ORDER_ATOMIC)
  17. , e = Blockly.Python.valueToCode(a, "z", Blockly.Python.ORDER_ATOMIC)
  18. , f = Blockly.Python.valueToCode(a, "L", Blockly.Python.ORDER_ATOMIC);
  19. a = Blockly.Python.valueToCode(a, "R", Blockly.Python.ORDER_ATOMIC);
  20. Blockly.Python.codeFunctions_.msg_args = "x \x3d 0\ny \x3d 0\nz \x3d 0\na \x3d 0";
  21. return "x \x3d " + c + "\ny \x3d " + d + "\nz \x3d " + e + "\nif " + f + ":\n a \x3d a | 0x01\nelse:\n a \x3d a \x26 0xFE\nif " + a + ":\n a \x3d a | 0x02\nelse:\n a \x3d a \x26 0xFD\nx \x3d x + 10000\n" + b + "[0] \x3d int(x / 256)\n" + b + "[1] \x3d x % 256\ny \x3d y + 10000\n" + b + "[2] \x3d int(y / 256)\n" + b + "[3] \x3d y % 256\nz \x3d z + 10000\n" + b + "[4] \x3d int(z / 256)\n" + b + "[5] \x3d z % 256\n" + b + "[6] \x3d int(a / 256)\n" + b + "[7] \x3d a % 256\n"
  22. }
  23. ;
  24. Blockly.Python.microbit_microbit_panic = function (a) {
  25. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  26. return "panic(" + a.getFieldValue("code") + ")\n"
  27. }
  28. ;
  29. Blockly.Python.microbit_microbit_reset = function (a) {
  30. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  31. return "reset()\n"
  32. }
  33. ;
  34. Blockly.Python.microbit_microbit_sleep = function (a) {
  35. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  36. return "sleep(" + Blockly.Python.valueToCode(a, "duration", Blockly.Python.ORDER_ATOMIC) + ")\n"
  37. }
  38. ;
  39. Blockly.Python.microbit_microbit_running_time = function (a) {
  40. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  41. return ["running_time()", Blockly.Python.ORDER_MEMBER]
  42. }
  43. ;
  44. Blockly.Python.microbit_microbit_temperature = function (a) {
  45. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  46. return ["temperature()", Blockly.Python.ORDER_MEMBER]
  47. }
  48. ;
  49. Blockly.Python.microbit_init_i2c = function (a) {
  50. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  51. var b = Blockly.Python.valueToCode(a, "fqs", Blockly.Python.ORDER_ATOMIC)
  52. , c = Blockly.Python.valueToCode(a, "sda", Blockly.Python.ORDER_ATOMIC);
  53. a = Blockly.Python.valueToCode(a, "scl", Blockly.Python.ORDER_ATOMIC);
  54. return "i2c.init(freq\x3d" + b + ", sda\x3dpin" + c + ", scl\x3dpin" + a + ")\n"
  55. }
  56. ;
  57. Blockly.Python.microbit_i2c_read = function (a) {
  58. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  59. var b = Blockly.Python.valueToCode(a, "addr", Blockly.Python.ORDER_ATOMIC);
  60. a = Blockly.Python.valueToCode(a, "bytes", Blockly.Python.ORDER_ATOMIC);
  61. return ["i2c.read(addr\x3d" + b + ", n\x3d" + a + ", repeat\x3dFalse)\n", Blockly.Python.ORDER_MEMBER]
  62. }
  63. ;
  64. Blockly.Python.microbit_i2c_write = function (a) {
  65. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  66. var b = Blockly.Python.valueToCode(a, "addr", Blockly.Python.ORDER_ATOMIC);
  67. a = Blockly.Python.valueToCode(a, "buf", Blockly.Python.ORDER_ATOMIC);
  68. return "i2c.write(addr\x3d" + b + ", buf\x3d" + a + ", repeat\x3dFalse)\n"
  69. }
  70. ;
  71. Blockly.Python.microbit_convert_bytearray = function (a) {
  72. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  73. return ["bytearray(" + Blockly.Python.valueToCode(a, "arr", Blockly.Python.ORDER_ATOMIC) + ")", Blockly.Python.ORDER_MEMBER]
  74. }
  75. ;
  76. Blockly.Python.microbit_uart_init = function (a) {
  77. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  78. var b = Blockly.Python.valueToCode(a, "tx", Blockly.Python.ORDER_ATOMIC)
  79. , c = Blockly.Python.valueToCode(a, "rx", Blockly.Python.ORDER_ATOMIC);
  80. return "uart.init(baudrate\x3d" + Blockly.Python.valueToCode(a, "baudrate", Blockly.Python.ORDER_ATOMIC) + ", bits\x3d8, parity\x3dNone, stop\x3d1, tx\x3dpin" + b + ", rx\x3dpin" + c + ")\n"
  81. }
  82. ;
  83. Blockly.Python.microbit_uart_any_msg_received = function (a) {
  84. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  85. return ["uart.any()", Blockly.Python.ORDER_ATOMIC]
  86. }
  87. ;
  88. Blockly.Python.microbit_uart_read_msg = function (a) {
  89. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  90. a = Blockly.Python.valueToCode(a, "type0", Blockly.Python.ORDER_ATOMIC);
  91. return ["uart." + ("1" == a ? "read()" : "readall" == a ? "readall()" : "readline" == a ? "readline()" : "read(" + a + ")"), Blockly.Python.ORDER_ATOMIC]
  92. }
  93. ;
  94. Blockly.Python.microbit_uart_read_msg_to = function (a) {
  95. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  96. var b = Blockly.Python.valueToCode(a, "type0", Blockly.Python.ORDER_ATOMIC)
  97. , b = "readall" == b ? "" : ", " + b;
  98. return "uart.readinto(" + Blockly.Python.valueToCode(a, "buf0", Blockly.Python.ORDER_ATOMIC) + b + ")\n"
  99. }
  100. ;
  101. Blockly.Python.microbit_uart_write_msg = function (a) {
  102. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  103. return "uart.write(" + Blockly.Python.valueToCode(a, "msg0", Blockly.Python.ORDER_ATOMIC) + ")\n"
  104. }
  105. ;
  106. Blockly.Python.microbit_uart_msg_type = function (a) {
  107. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  108. return [a.getFieldValue("type0"), Blockly.Python.ORDER_ATOMIC]
  109. }
  110. ;
  111. Blockly.Python.microbit_uart_msg_type1 = function (a) {
  112. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  113. return [a.getFieldValue("type0"), Blockly.Python.ORDER_ATOMIC]
  114. }
  115. ;
  116. Blockly.Python.CocoRobo_custom_code = function (a) {
  117. var b = a.getFieldValue("custom_type");
  118. a = a.getFieldValue("custom_code");
  119. return "topping" == b ? (Blockly.Python.definitions_.import_custom = a,
  120. "") : a + "\n"
  121. }
  122. ;
  123. Blockly.Python.CocoRobo_print_to_chart = function (a) {
  124. for (var b = Array(a.itemCount_), c = 0; c < a.itemCount_; c++)
  125. b[c] = Blockly.Python.valueToCode(a, "ADD" + c, Blockly.Python.ORDER_NONE) || "None";
  126. return 1 == a.itemCount_ ? "print((" + b[0] + ",))\n" : "print((" + b.join(", ") + "))\n"
  127. }
  128. ;
  129. Blockly.Python.CocoRobo_init_chart = function (a) {
  130. var b = Array(a.itemCount_ + 1);
  131. b[0] = "'__TITLE'";
  132. for (var c = 0; c < a.itemCount_; c++)
  133. b[c + 1] = Blockly.Python.valueToCode(a, "ADD" + c, Blockly.Python.ORDER_NONE) || "None";
  134. return "sleep(50);print((" + b.join(", ") + "));sleep(50)\n"
  135. }
  136. ;
  137. Blockly.Python.math_random_int = function (a) {
  138. // Blockly.Python.definitions_.import_time = "import time";
  139. Blockly.Python.definitions_.import_random = "import random";
  140. // Blockly.Python.codeFunctions_.random_seed = "random.seed(time.ticks_cpu())";
  141. var b = Blockly.Python.valueToCode(a, "FROM", Blockly.Python.ORDER_NONE) || "0";
  142. a = Blockly.Python.valueToCode(a, "TO", Blockly.Python.ORDER_NONE) || "0";
  143. return ["random.randint(" + b + ", " + a + ")", Blockly.Python.ORDER_FUNCTION_CALL]
  144. }
  145. ;
  146. Blockly.Python.math_random_float = function (a) {
  147. Blockly.Python.definitions_.import_random = "import random";
  148. return ["random.random()", Blockly.Python.ORDER_FUNCTION_CALL]
  149. }
  150. ;
  151. Blockly.Python.addReservedWords("microbit");
  152. Blockly.Python.microbit_accelerometer_get_x = function (a) {
  153. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  154. return ["accelerometer.get_x()", Blockly.Python.ORDER_MEMBER]
  155. }
  156. ;
  157. Blockly.Python.microbit_accelerometer_get_y = function (a) {
  158. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  159. return ["accelerometer.get_y()", Blockly.Python.ORDER_MEMBER]
  160. }
  161. ;
  162. Blockly.Python.microbit_accelerometer_get_z = function (a) {
  163. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  164. return ["accelerometer.get_z()", Blockly.Python.ORDER_MEMBER]
  165. }
  166. ;
  167. Blockly.Python.microbit_accelerometer_was_gesture = function (a) {
  168. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  169. return ['accelerometer.was_gesture("' + a.getFieldValue("gesture") + '")', Blockly.Python.ORDER_MEMBER]
  170. }
  171. ;
  172. Blockly.Python.microbit_accelerometer_is_gesture = function (a) {
  173. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  174. return ['accelerometer.is_gesture("' + a.getFieldValue("gesture") + '")', Blockly.Python.ORDER_MEMBER]
  175. }
  176. ;
  177. Blockly.Python.microbit_accelerometer_get_gestures = function (a) {
  178. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  179. return ["accelerometer.get_gestures()", Blockly.Python.ORDER_MEMBER]
  180. }
  181. ;
  182. Blockly.Python.microbit_accelerometer_current_gesture = function (a) {
  183. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  184. return ["accelerometer.current_gesture()", Blockly.Python.ORDER_MEMBER]
  185. }
  186. ;
  187. Blockly.Python.botbit_spd = function (a) {
  188. return [a.getFieldValue("spd"), Blockly.Python.ORDER_ATOMIC]
  189. }
  190. ;
  191. Blockly.Python.botbit_motor_on = function (a) {
  192. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  193. Blockly.Python.definitions_.botbit_motor = "\nmotor_pwm \x3d bytearray([8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])\ndef motion(leftSpeed, rightSpeed):\n if leftSpeed \x3e 2000:\n leftSpeed \x3d 2000\n if leftSpeed \x3c -2000:\n leftSpeed \x3d -2000\n if leftSpeed \x3d\x3d 0:\n motor_pwm[1] \x3d 0\n motor_pwm[2] \x3d 0\n motor_pwm[3] \x3d 0\n motor_pwm[4] \x3d 0\n if leftSpeed \x3e 0:\n motor_pwm[1] \x3d int(leftSpeed / 256)\n motor_pwm[2] \x3d int(leftSpeed % 256)\n motor_pwm[3] \x3d 0\n motor_pwm[4] \x3d 0\n if leftSpeed \x3c 0:\n leftSpeed \x3d -leftSpeed\n motor_pwm[1] \x3d 0\n motor_pwm[2] \x3d 0\n motor_pwm[3] \x3d int(leftSpeed / 256)\n motor_pwm[4] \x3d int(leftSpeed % 256)\n if rightSpeed \x3e 2000:\n rightSpeed \x3d 2000\n if rightSpeed \x3c -2000:\n rightSpeed \x3d -2000\n if rightSpeed \x3d\x3d 0:\n motor_pwm[5] \x3d 0\n motor_pwm[6] \x3d 0\n motor_pwm[7] \x3d 0\n motor_pwm[8] \x3d 0\n if rightSpeed \x3e 0:\n motor_pwm[5] \x3d 0\n motor_pwm[6] \x3d 0\n motor_pwm[7] \x3d int(rightSpeed / 256)\n motor_pwm[8] \x3d int(rightSpeed % 256)\n if rightSpeed \x3c 0:\n rightSpeed \x3d -rightSpeed\n motor_pwm[5] \x3d int(rightSpeed / 256)\n motor_pwm[6] \x3d int(rightSpeed % 256)\n motor_pwm[7] \x3d 0\n motor_pwm[8] \x3d 0\n i2c.write(0x2A, motor_pwm)\n";
  194. var b = Blockly.Python.valueToCode(a, "speedl", Blockly.Python.ORDER_ATOMIC);
  195. a = Blockly.Python.valueToCode(a, "speedr", Blockly.Python.ORDER_ATOMIC);
  196. return "motion(" + b + "," + a + ")\n"
  197. }
  198. ;
  199. Blockly.Python.botbit_Steering_EngineL = function (a) {
  200. var _d = a.getFieldValue('dir');
  201. Blockly.Python.definitions_.import_machine = 'from machine import I2C, Pin';
  202. Blockly.Python.definitions_.import_QMI8658 = 'import QMI8658';
  203. Blockly.Python.addVariable('i2c', 'i2c = I2C(scl=Pin(17), sda=Pin(16))', true);
  204. Blockly.Python.addVariable('accel', 'accel = QMI8658.accel(i2c)', true);
  205. var _code = _d; // 'accel.get_values()[' + _d + ']';
  206. return _code;
  207. // Blockly.Python.definitions_.import_microbit = "from microbit import *";
  208. // Blockly.Python.definitions_.botbit_servo_pos = "servo_pos \x3d bytearray([0, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC])";
  209. // Blockly.Python.definitions_.botbit_setServo = "\ndef setServo(servo, angle):\n a \x3d (1.5 + angle/90) * 1000\n servo_pos[servo*2 + 1] \x3d int(a / 256)\n servo_pos[servo*2 + 2] \x3d int(a % 256)\n";
  210. // var b = a.getFieldValue("hands")
  211. // , c = a.getFieldValue("dir");
  212. // a = Blockly.Python.valueToCode(a, "angle", Blockly.Python.ORDER_ATOMIC);
  213. // "l" == b ? b = "y" == c ? "0" : "2" : (b = "y" == c ? "1" : "3",
  214. // a = "-" + a + "");
  215. // return "setServo(" + b + "," + a + ")\n"
  216. }
  217. ;
  218. Blockly.Python.botbit_Steering_Tilt = function (a) {
  219. Blockly.Python.definitions_.import_machine = 'from machine import I2C, Pin';
  220. Blockly.Python.definitions_.import_QMI8658 = 'import QMI8658';
  221. Blockly.Python.addVariable('i2c', 'i2c = I2C(scl=Pin(17), sda=Pin(16))', true);
  222. Blockly.Python.addVariable('accel', 'accel = QMI8658.accel(i2c)', true);
  223. var _d = a.getFieldValue('dir');
  224. Blockly.Python.definitions_.get_tilt_angle = 'def get_tilt_angle(_axis):\n' +
  225. ' _Ax = accel.get_values()[\'AcX\']\n' +
  226. ' _Ay = accel.get_values()[\'AcY\']\n' +
  227. ' _Az = accel.get_values()[\'AcZ\']\n' +
  228. ' if \'X\' == _axis:\n' +
  229. ' _T = math.sqrt(_Ay ** 2 + _Az ** 2)\n' +
  230. ' if _Az < 0: return math.degrees(math.atan2(_Ax , _T))\n' +
  231. ' else: return 180 - math.degrees(math.atan2(_Ax , _T))\n' +
  232. ' elif \'Y\' == _axis:\n' +
  233. ' _T = math.sqrt(_Ax ** 2 + _Az ** 2)\n' +
  234. ' if _Az < 0: return math.degrees(math.atan2(_Ay , _T))\n' +
  235. ' else: return 180 - math.degrees(math.atan2(_Ay , _T))\n' +
  236. ' elif \'Z\' == _axis:\n' +
  237. ' _T = math.sqrt(_Ax ** 2 + _Ay ** 2)\n' +
  238. ' if (_Ax + _Ay) < 0: return 180 - math.degrees(math.atan2(_T , _Az))\n' +
  239. ' else: return math.degrees(math.atan2(_T , _Az)) - 180\n' +
  240. ' return 0\n';
  241. return "get_tilt_angle(\"" + _d + "\")";
  242. }
  243. ;
  244. Blockly.Python.botbit_Steering_Engine_leg = function (a) {
  245. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  246. Blockly.Python.definitions_.botbit_servo_pos = "servo_pos \x3d bytearray([0, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC])";
  247. Blockly.Python.definitions_.botbit_setServo = "\ndef setServo(servo, angle):\n a \x3d (1.5 + angle/90) * 1000\n servo_pos[servo*2 + 1] \x3d int(a / 256)\n servo_pos[servo*2 + 2] \x3d int(a % 256)\n";
  248. var b = a.getFieldValue("leg")
  249. , c = a.getFieldValue("dir");
  250. a = Blockly.Python.valueToCode(a, "walk", Blockly.Python.ORDER_ATOMIC);
  251. return "setServo(" + ("l" == b ? "y" == c ? "0" : "2" : "y" == c ? "1" : "3") + "," + a + ")\n"
  252. }
  253. ;
  254. Blockly.Python.botbit_updatePosition = function (a) {
  255. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  256. Blockly.Python.definitions_.botbit_servo_pos = "servo_pos \x3d bytearray([0, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC])";
  257. Blockly.Python.definitions_.botbit_updatePosition = "\ndef updatePosition():\n servo_pos[0] \x3d 0\n i2c.write(0x2A, servo_pos)\n";
  258. return "updatePosition()\n"
  259. }
  260. ;
  261. Blockly.Python.botbit_Ultrasonic = function (a) {
  262. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  263. Blockly.Python.definitions_.botbit_Ultrasonic = "\ndef getDistance():\n i2c.write(0x0b, bytearray([1]))\n temp \x3d i2c.read(0x0B, 2)\n dis \x3d (temp[0]+temp[1]*256)/10\n return dis\n";
  264. return ["getDistance()", Blockly.Python.ORDER_ATOMIC]
  265. }
  266. ;
  267. Blockly.Python.botbit_holdBall = function (a) {
  268. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  269. Blockly.Python.definitions_.botbit_servo_pos = "servo_pos \x3d bytearray([0, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC])";
  270. Blockly.Python.definitions_.botbit_setServo = "\ndef setServo(servo, angle):\n a \x3d (1.5 + angle/90) * 1000\n servo_pos[servo*2 + 1] \x3d int(a / 256)\n servo_pos[servo*2 + 2] \x3d int(a % 256)\n";
  271. Blockly.Python.definitions_.botbit_updatePosition = "\ndef updatePosition():\n servo_pos[0] \x3d 0\n i2c.write(0x2A, servo_pos)\n";
  272. Blockly.Python.codeFunctions_.botbit_func_holdBall = "def holdBall():\n a \x3d 0\n b \x3d 0\n c \x3d 0\n d \x3d 0\n setServo(2,0)\n setServo(3,0)\n setServo(1,0)\n setServo(0,0)\n sleep(1000)\n for count in range(45):\n a \x3d a - 0.5\n b \x3d b + 0.5\n setServo(1,b)\n setServo(0,a)\n sleep(100)\n updatePosition()\n for count in range(45):\n c \x3d c - 5\n d \x3d d + 5\n setServo(2,c)\n setServo(3,d)\n sleep(100)\n updatePosition()\n for count in range(45):\n a \x3d a + 0.5\n b \x3d b - 0.5\n setServo(1,b)\n setServo(0,a)\n sleep(100)\n updatePosition()\n";
  273. return "holdBall()\n"
  274. }
  275. ;
  276. Blockly.Python.botbit_walking = function (a) {
  277. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  278. Blockly.Python.definitions_.import_math = "import math";
  279. var b = Blockly.Python.valueToCode(a, "steps", Blockly.Python.ORDER_ATOMIC)
  280. , c = Blockly.Python.valueToCode(a, "T", Blockly.Python.ORDER_ATOMIC);
  281. a = a.getFieldValue("fx");
  282. return "walking(" + b + "," + c + "," + a + ")\n"
  283. }
  284. ;
  285. Blockly.Python.botbit_moonwalker = function (a) {
  286. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  287. Blockly.Python.definitions_.import_math = "import math";
  288. var b = Blockly.Python.valueToCode(a, "amplitude", Blockly.Python.ORDER_ATOMIC)
  289. , c = Blockly.Python.valueToCode(a, "steps2", Blockly.Python.ORDER_ATOMIC)
  290. , d = Blockly.Python.valueToCode(a, "T2", Blockly.Python.ORDER_ATOMIC);
  291. a = a.getFieldValue("fx");
  292. return "moonwalker(" + c + "," + d + "," + b + "," + a + ")\n"
  293. }
  294. ;
  295. Blockly.Python.botbit_crusaito = function (a) {
  296. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  297. Blockly.Python.definitions_.import_math = "import math";
  298. var b = Blockly.Python.valueToCode(a, "steps3", Blockly.Python.ORDER_ATOMIC)
  299. , c = Blockly.Python.valueToCode(a, "T3", Blockly.Python.ORDER_ATOMIC)
  300. , d = Blockly.Python.valueToCode(a, "amplitude3", Blockly.Python.ORDER_ATOMIC);
  301. a = a.getFieldValue("fx");
  302. return "crusaito(" + b + "," + c + "," + d + "," + a + ")\n"
  303. }
  304. ;
  305. Blockly.Python.botbit_flapping = function (a) {
  306. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  307. Blockly.Python.definitions_.import_math = "import math";
  308. var b = Blockly.Python.valueToCode(a, "steps4", Blockly.Python.ORDER_ATOMIC)
  309. , c = Blockly.Python.valueToCode(a, "T4", Blockly.Python.ORDER_ATOMIC)
  310. , d = Blockly.Python.valueToCode(a, "amplitude4", Blockly.Python.ORDER_ATOMIC);
  311. a = a.getFieldValue("fx");
  312. return "flapping(" + b + "," + c + "," + d + "," + a + ")\n"
  313. }
  314. ;
  315. Blockly.Python.botbit_home = function (a) {
  316. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  317. Blockly.Python.definitions_.import_math = "import math";
  318. Blockly.Python.definitions_.botbit_servo_pos = "servo_pos \x3d bytearray([0, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC])";
  319. Blockly.Python.definitions_.botbit_setServo = "\ndef setServo(servo, angle):\n a \x3d (1.5 + angle/90) * 1000\n servo_pos[servo*2 + 1] \x3d int(a / 256)\n servo_pos[servo*2 + 2] \x3d int(a % 256)\n";
  320. Blockly.Python.definitions_.botbit_updatePosition = "\ndef updatePosition():\n servo_pos[0] \x3d 0\n i2c.write(0x2A, servo_pos)\n";
  321. Blockly.Python.codeFunctions_.botbit_dance_init = "\ninc \x3d 0\nphase_start\x3d[0, 0, 0, 0]\nphase\x3d[0, 0, 0, 0]\noffset\x3d[0, 0, 0, 0]\namplitude\x3d[0, 0, 0, 0]\nt \x3d 0\ndef refresh():\n global t, phase, inc, amplitude, phase_start\n if (running_time() - t) \x3e 50:\n t \x3d running_time()\n for i in range(0, 4):\n pos \x3d round(amplitude[i]*math.sin(phase[i] + phase_start[i]) + offset[i])\n setServo(i, pos)\n phase[i] \x3d phase[i] + inc\n updatePosition()\ndef action(A, O, DIFF, T, steps):\n global inc, amplitude, phase_start, offset\n t2 \x3d 0\n inc \x3d 2*math.pi/(T/50)\n for i in range(0, 4):\n amplitude[i] \x3d A[i]\n phase_start[i] \x3d DIFF[i]\n offset[i] \x3d O[i]\n cycle \x3d int(steps)\n t2 \x3d running_time() + T*cycle\n while (running_time() \x3c t2):\n refresh()\n for i in range(0, 4):\n amplitude[i] \x3d A[i]\n phase_start[i] \x3d DIFF[i]\n offset[i] \x3d O[i]\n t2 \x3d running_time() + T*(steps - cycle)\n while (running_time() \x3c t2):\n refresh()\ndef walking(steps, T\x3d1000, dir\x3d1):\n AMP \x3d (30, 30, 20, 20)\n OFFSET \x3d (0, 0, 4, -4) \n DIFF \x3d (0, 0, -math.pi/2 * dir, -math.pi/2 * dir)\n action(AMP, OFFSET, DIFF, T, steps)\ndef turn(steps, T\x3d2000, dir\x3d1):\n OFFSET \x3d [0, 0, 4, -4] \n DIFF \x3d (0, 0, -math.pi/2 * dir, -math.pi/2 * dir)\n if dir \x3d\x3d 1:\n AMP \x3d (30, 10, 20, 20)\n else:\n AMP \x3d (10, 30, 20, 20)\n action(AMP, OFFSET, DIFF, T, steps)\ndef moonwalker(steps, T\x3d900, h\x3d20, dir\x3d1):\n 'Moonwalker. Otto moves like Michael Jackson'\n AMP \x3d [0, 0, h, h]\n OFFSET \x3d [0, 0, h/2 + 2, -h/2 -2] \n DIFF \x3d [0, 0, math.pi/180*dir*-90, math.pi/180*dir*-150]\n action(AMP, OFFSET, DIFF, T, steps)\ndef crusaito(steps, T, h, dir):\n AMP \x3d [25, 25, h, h]\n OFFSET \x3d [0, 0, h/2+ 4, -h/2 - 4] \n DIFF \x3d [90, 90, 0, math.pi/180*dir*-60]\n action(AMP, OFFSET, DIFF, T, steps)\ndef flapping(steps, T, h, dir):\n AMP \x3d [12, 12, h, h]\n OFFSET \x3d [0, 0, h-10, -h+10] \n DIFF \x3d [0, math.pi/180*180, math.pi/180*dir*-90, math.pi/180*dir*90]\n action(AMP, OFFSET, DIFF, T, steps)\n\nservo_position \x3d [0, 0, 0, 0]\nservo_increment \x3d [0, 0, 0, 0]\ndef moveServos(time, servo_target):\n if time \x3e 20:\n for i in range(0, 4):\n servo_increment[i] \x3d (servo_target[i] - servo_position[i])/(time/20) \n final_time \x3d running_time() + time;\n iteration \x3d 1\n while running_time() \x3c final_time:\n partial_time \x3d running_time()+20\n for i in range(0, 4):\n setServo(i, servo_position[i]+iteration*servo_increment[i])\n updatePosition()\n while running_time() \x3c partial_time:\n pass\n iteration \x3d iteration+1\n else:\n for i in range(0, 4):\n setServo(i, servo_target[i])\n updatePosition()\n for i in range(0, 4):\n servo_position[i] \x3d servo_target[i]\n\ndef jump(T):\n up \x3d [0, 0, 45, -45]\n moveServos(T, up)\n down \x3d [0, 0, 0, 0]\n moveServos(T, down)\n\ndef home():\n for i in range(0, 4):\n setServo(i, 0)\n servo_position[i] \x3d 0\n updatePosition()\n\ndisplay.off()\n";
  322. return ""
  323. }
  324. ;
  325. Blockly.Python.botbit_jump = function (a) {
  326. return "jump(" + Blockly.Python.valueToCode(a, "t", Blockly.Python.ORDER_ATOMIC) + ")\n"
  327. }
  328. ;
  329. Blockly.Python.botbit_turn = function (a) {
  330. var b = a.getFieldValue("dir")
  331. , c = Blockly.Python.valueToCode(a, "t", Blockly.Python.ORDER_ATOMIC);
  332. return "turn(" + Blockly.Python.valueToCode(a, "steps", Blockly.Python.ORDER_ATOMIC) + ", " + c + ", " + b + ")\n"
  333. }
  334. ;
  335. Blockly.Python.botbit_setRGB = function (a) {
  336. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  337. Blockly.Python.definitions_.botbit_RGB = "def setRGB(cmd, r, g, b): \n i2c.write(0x0b, bytearray([cmd, r, g, b]))\n sleep(1)";
  338. var b = a.getFieldValue("left_right")
  339. , c = Blockly.Python.valueToCode(a, "b", Blockly.Python.ORDER_ATOMIC)
  340. , d = Blockly.Python.valueToCode(a, "g", Blockly.Python.ORDER_ATOMIC);
  341. a = Blockly.Python.valueToCode(a, "r", Blockly.Python.ORDER_ATOMIC);
  342. return "setRGB(" + b + ", " + a + ", " + d + ", " + c + ")\n"
  343. }
  344. ;
  345. Blockly.Python.botbit_setHSV = function (a) {
  346. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  347. Blockly.Python.definitions_.botbit_HSV = "def setHSV(cmd, h, s, v): \n _h1 \x3d h % 256\n _h2 \x3d h//256\n _s \x3d int(s*100)\n _v \x3d int(v*100)\n i2c.write(0x0b, bytearray([cmd, _h1, _h2, _s, _v]))\n sleep(1)";
  348. var b = a.getFieldValue("left_right")
  349. , c = Blockly.Python.valueToCode(a, "v", Blockly.Python.ORDER_ATOMIC)
  350. , d = Blockly.Python.valueToCode(a, "s", Blockly.Python.ORDER_ATOMIC);
  351. a = Blockly.Python.valueToCode(a, "h", Blockly.Python.ORDER_ATOMIC);
  352. return "setHSV(" + b + ", " + a + ", " + d + ", " + c + ")\n"
  353. }
  354. ;
  355. Blockly.Python.botbit_PIDtracking = function (a) {
  356. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  357. Blockly.Python.definitions_.import_math = "import math";
  358. var b = Blockly.Python.valueToCode(a, "kp", Blockly.Python.ORDER_ATOMIC)
  359. , c = Blockly.Python.valueToCode(a, "kd", Blockly.Python.ORDER_ATOMIC);
  360. a = Blockly.Python.valueToCode(a, "trackSpeed", Blockly.Python.ORDER_ATOMIC);
  361. Blockly.Python.definitions_.botbit_servo_pos = "servo_pos \x3d bytearray([0, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC])";
  362. Blockly.Python.definitions_.botbit_setServo = "\ndef setServo(servo, angle):\n a \x3d (1.5 + angle/90) * 1000\n servo_pos[servo*2 + 1] \x3d int(a / 256)\n servo_pos[servo*2 + 2] \x3d int(a % 256)\n";
  363. Blockly.Python.definitions_.botbit_updatePosition = "\ndef updatePosition():\n servo_pos[0] \x3d 0\n i2c.write(0x2A, servo_pos)\n";
  364. Blockly.Python.definitions_.botbit_motor = "\nmotor_pwm \x3d bytearray([8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])\ndef motion(leftSpeed, rightSpeed):\n if leftSpeed \x3e 2000:\n leftSpeed \x3d 2000\n if leftSpeed \x3c -2000:\n leftSpeed \x3d -2000\n if leftSpeed \x3d\x3d 0:\n motor_pwm[1] \x3d 0\n motor_pwm[2] \x3d 0\n motor_pwm[3] \x3d 0\n motor_pwm[4] \x3d 0\n if leftSpeed \x3e 0:\n motor_pwm[1] \x3d int(leftSpeed / 256)\n motor_pwm[2] \x3d int(leftSpeed % 256)\n motor_pwm[3] \x3d 0\n motor_pwm[4] \x3d 0\n if leftSpeed \x3c 0:\n leftSpeed \x3d -leftSpeed\n motor_pwm[1] \x3d 0\n motor_pwm[2] \x3d 0\n motor_pwm[3] \x3d int(leftSpeed / 256)\n motor_pwm[4] \x3d int(leftSpeed % 256)\n if rightSpeed \x3e 2000:\n rightSpeed \x3d 2000\n if rightSpeed \x3c -2000:\n rightSpeed \x3d -2000\n if rightSpeed \x3d\x3d 0:\n motor_pwm[5] \x3d 0\n motor_pwm[6] \x3d 0\n motor_pwm[7] \x3d 0\n motor_pwm[8] \x3d 0\n if rightSpeed \x3e 0:\n motor_pwm[5] \x3d 0\n motor_pwm[6] \x3d 0\n motor_pwm[7] \x3d int(rightSpeed / 256)\n motor_pwm[8] \x3d int(rightSpeed % 256)\n if rightSpeed \x3c 0:\n rightSpeed \x3d -rightSpeed\n motor_pwm[5] \x3d int(rightSpeed / 256)\n motor_pwm[6] \x3d int(rightSpeed % 256)\n motor_pwm[7] \x3d 0\n motor_pwm[8] \x3d 0\n i2c.write(0x2A, motor_pwm)\n";
  365. Blockly.Python.codeFunctions_.botbit_tracking_int = "\nsensor_min \x3d [1024, 1024, 1024, 1024, 1024]\nsensor_max \x3d [0, 0, 0, 0, 0]\nsensor \x3d [0, 0, 0, 0, 0]\nsensor_pin \x3d (pin3, pin1, pin10, pin2, pin4)\n\ndef Calibrate():\n global sensor_min, sensor_max, sensor,sensor_pin\n motion(-1000, 1000)\n t \x3d running_time()\n while (running_time() - t) \x3c 5000:\n for i in range(5):\n sensor[i] \x3d sensor_pin[i].read_analog()\n sensor_min[i] \x3d min(sensor[i], sensor_min[i])\n sensor_max[i] \x3d max(sensor[i], sensor_max[i])\n motion(0, 0)\n\ndef ReadLineSensor():\n global sensor_min, sensor_max, sensor,sensor_pin\n for i in range(5):\n sensor[i] \x3d sensor_pin[i].read_analog()\n sensor[i] \x3d round((sensor[i] - sensor_min[i]) / (sensor_max[i] - sensor_min[i]) * 1000)\n sum \x3d sensor[0] + sensor[1] + sensor[2] + sensor[3] + sensor[4]\n if sum \x3c\x3d 0:\n return 0\n else:\n return (sensor[0] + sensor[1] * 1000 + sensor[2] * 2000 + sensor[3] * 3000 + sensor[4]*4000) / sum\n\ndef PIDtracking(kp,kd,trackSpeed):\n global pre_line_pos\n line_pos \x3d ReadLineSensor() - 2000\n correction \x3d kp * line_pos + kd * (line_pos - pre_line_pos)\n pre_line_pos \x3d line_pos\n if correction \x3e 0:\n motion(trackSpeed - correction, trackSpeed)\n else:\n motion(trackSpeed, trackSpeed + correction)\n\nt\x3d0\ndisplay.off()\nmotion(0, 0)\npin3.read_digital()\npin4.read_digital()\npin10.read_digital()\npin3.set_pull(pin3.NO_PULL)\npin4.set_pull(pin4.NO_PULL)\npin10.set_pull(pin10.NO_PULL)\nsetServo(0,-45)\nsetServo(2,-45)\nsetServo(1,45)\nsetServo(3,45)\nupdatePosition()\nCalibrate()\npre_line_pos \x3d ReadLineSensor() - 2000";
  366. return "PIDtracking(" + b + ", " + c + ", " + a + ")\nsleep(10)\n"
  367. }
  368. ;
  369. Blockly.Python.addReservedWords("microbit");
  370. Blockly.Python.microbit_button_is_pressed = function (a) {
  371. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  372. return ["button_" + a.getFieldValue("button") + ".is_pressed()", Blockly.Python.ORDER_MEMBER]
  373. }
  374. ;
  375. Blockly.Python.microbit_button_was_pressed = function (a) {
  376. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  377. return ["button_" + a.getFieldValue("button") + ".was_pressed()", Blockly.Python.ORDER_MEMBER]
  378. }
  379. ;
  380. Blockly.Python.microbit_button_get_presses = function (a) {
  381. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  382. return ["button_" + a.getFieldValue("button") + ".get_presses()", Blockly.Python.ORDER_MEMBER]
  383. }
  384. ;
  385. Blockly.Python.Carbit_init = function (a) {
  386. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  387. return "def setLeftMotor(speed):\n i2c.write(0x10, bytearray([0x01, speed]))\ndef setRightMotor(speed):\n i2c.write(0x10, bytearray([0x02, speed]))\ndef getSpeed():\n return i2c.read(0x10,4)\ndef Distance():\n i2c.write(0x0b, bytearray([1]))\n sleep(2)\n temp\x3di2c.read(0x0b,2)\n return (temp[0]+temp[1]*256)/10\n"
  388. }
  389. ;
  390. Blockly.Python.Carbit_setLeftMotor = function (a) {
  391. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  392. Blockly.Python.definitions_.Carbit_setLeftMotor = "\ndef setLeftMotor(speed):\n i2c.write(0x10, bytearray([0x01, speed]))\n";
  393. return "setLeftMotor(" + Blockly.Python.valueToCode(a, "LeftMotor", Blockly.Python.ORDER_ATOMIC) + ")\n"
  394. }
  395. ;
  396. Blockly.Python.Carbit_setRightMotor = function (a) {
  397. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  398. Blockly.Python.definitions_.Carbit_setRightMotor = "\ndef setRightMotor(speed): \n i2c.write(0x10, bytearray([0x02, speed]))\n";
  399. return "setRightMotor(" + Blockly.Python.valueToCode(a, "RightMotor", Blockly.Python.ORDER_ATOMIC) + ")\n"
  400. }
  401. ;
  402. Blockly.Python.Carbit_analog_light = function (a) {
  403. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  404. return "pin" + a.getFieldValue("light") + ".read_analog()\n"
  405. }
  406. ;
  407. Blockly.Python.Carbit_Speed_U10 = function (a) {
  408. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  409. Blockly.Python.definitions_.Carbit_Speed = "\ndef getSpeed():\n return i2c.read(0x10,4)\n";
  410. return ["(getSpeed()[2]+getSpeed()[3]*256)", Blockly.Python.ORDER_ATOMIC]
  411. }
  412. ;
  413. Blockly.Python.Carbit_Speed_U11 = function (a) {
  414. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  415. Blockly.Python.definitions_.Carbit_Speed = "\ndef getSpeed():\n return i2c.read(0x10,4)\n";
  416. return ["(getSpeed()[0]+getSpeed()[1]*256)", Blockly.Python.ORDER_ATOMIC]
  417. }
  418. ;
  419. Blockly.Python.Carbit_Distance = function (a) {
  420. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  421. Blockly.Python.definitions_.Carbit_Distance = "\ndef Distance():\n i2c.write(0x0b, bytearray([1]))\n sleep(2)\n temp\x3di2c.read(0x0b,2)\n return (temp[0]+temp[1]*256)/10\n";
  422. return ["Distance()", Blockly.Python.ORDER_ATOMIC]
  423. }
  424. ;
  425. Blockly.Python.Carbit_tracking = function (a) {
  426. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  427. var b = Blockly.Python.valueToCode(a, "k_speed", Blockly.Python.ORDER_ATOMIC)
  428. , c = Blockly.Python.valueToCode(a, "speedMax", Blockly.Python.ORDER_ATOMIC)
  429. , d = Blockly.Python.valueToCode(a, "speedScale", Blockly.Python.ORDER_ATOMIC);
  430. a = Blockly.Python.valueToCode(a, "turnDelay", Blockly.Python.ORDER_ATOMIC);
  431. Blockly.Python.definitions_.Carbit_tracking_int = "kp \x3d 1\nki \x3d 1\nkd \x3d 1\npreError \x3d 0\nintegral \x3d 0\ndelays \x3d 1\nleftMotorSpeed \x3d 0\nrightMotorSpeed \x3d 0\nk_speed \x3d" + b + "\nspeedMax \x3d" + c + "\nturnDelay \x3d" + a + "\n\ndef pidInit(kp_ \x3d 8, ki_ \x3d 0.0, kd_ \x3d 15, delays_ \x3d 1):\n global kp, ki,kd, delays\n kp \x3d kp_\n ki \x3d ki_\n kd \x3d kd_\n delays \x3d delays_\n\ndef getError():\n err \x3d 0\n varLeft1 \x3d pin1.read_digital()\n varLeft2 \x3d pin14.read_digital()\n varLeft3 \x3d pin13.read_digital()\n varRight1 \x3d pin2.read_digital()\n varRight2 \x3d pin15.read_digital()\n varRight3 \x3d pin16.read_digital()\n if (varLeft1 \x3d\x3d 1 and varRight1 \x3d\x3d 0 and varLeft2 \x3d\x3d 0):\n err \x3d 1\n elif (varLeft2 \x3d\x3d 1 and varLeft1 \x3d\x3d 1):\n err \x3d 2\n elif (varLeft2 \x3d\x3d 1 and varLeft1 \x3d\x3d 0 and varLeft3 \x3d\x3d 0):\n err \x3d 3\n elif (varLeft3 \x3d\x3d 1 and varLeft2 \x3d\x3d 1):\n err \x3d 4\n elif (varLeft3 \x3d\x3d 1 and varLeft2 \x3d\x3d 0):\n err \x3d 5\n elif (varRight1 \x3d\x3d 1 and varLeft1 \x3d\x3d 0 and varRight2 \x3d\x3d 0):\n err \x3d -1\n elif (varRight1 \x3d\x3d 1 and varRight2 \x3d\x3d 1):\n err \x3d -2\n elif (varRight2 \x3d\x3d 1 and varRight1 \x3d\x3d 0 and varRight3 \x3d\x3d 0):\n err \x3d -3\n elif (varRight3 \x3d\x3d 1 and varRight2 \x3d\x3d 1):\n err \x3d -4\n elif (varRight3 \x3d\x3d 1 and varRight2 \x3d\x3d 0):\n err \x3d -5\n #print(err)\n return err\n\ndef pidControl():\n global preError,integral,delays,leftMotorSpeed,rightMotorSpeed\n global kd, ki, kd\n\n speedScale \x3d " + d + "\n error \x3d getError()\n if (error !\x3d 0):\n speedScale \x3d 2/abs(error)\n if speedScale \x3e 1:\n speedScale \x3d 1\n integral \x3d integral + error\n derivative \x3d error - preError\n output \x3d (int)(kp*error + ki*integral*delays + kd*derivative/delays)\n print(output)\n preError \x3d error\n leftMotorSpeed \x3d int((k_speed - output)*speedScale)\n rightMotorSpeed \x3d int((k_speed + output)*speedScale)\n\n if leftMotorSpeed \x3c - speedMax:\n leftMotorSpeed \x3d -speedMax\n elif leftMotorSpeed \x3e speedMax:\n leftMotorSpeed \x3d speedMax\n if rightMotorSpeed \x3c -speedMax:\n rightMotorSpeed \x3d -speedMax\n elif rightMotorSpeed \x3e speedMax:\n rightMotorSpeed \x3d speedMax\n setLeftMotor(leftMotorSpeed)\n setRightMotor(rightMotorSpeed)\n\n sleep(abs(error)*turnDelay)\n\n";
  432. return "pidControl()\n"
  433. }
  434. ;
  435. Blockly.Python.addReservedWords("microbit");
  436. Blockly.Python.microbit_compass_calibrate = function (a) {
  437. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  438. return "compass.calibrate()\n"
  439. }
  440. ;
  441. Blockly.Python.microbit_compass_is_calibrated = function (a) {
  442. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  443. return ["compass.is_calibrated()", Blockly.Python.ORDER_MEMBER]
  444. }
  445. ;
  446. Blockly.Python.microbit_compass_heading = function (a) {
  447. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  448. return ["compass.heading()", Blockly.Python.ORDER_MEMBER]
  449. }
  450. ;
  451. Blockly.Python.microbit_compass_get_field_strength = function (a) {
  452. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  453. return ["compass.get_field_strength()", Blockly.Python.ORDER_MEMBER]
  454. }
  455. ;
  456. Blockly.Python.microbit_compass_x = function (a) {
  457. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  458. return ["compass.get_x()", Blockly.Python.ORDER_MEMBER]
  459. }
  460. ;
  461. Blockly.Python.microbit_compass_y = function (a) {
  462. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  463. return ["compass.get_y()", Blockly.Python.ORDER_MEMBER]
  464. }
  465. ;
  466. Blockly.Python.microbit_compass_z = function (a) {
  467. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  468. return ["compass.get_z()", Blockly.Python.ORDER_MEMBER]
  469. }
  470. ;
  471. Blockly.Python.addReservedWords("microbit");
  472. Blockly.Python.microbit_display_get_pixel = function (a) {
  473. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  474. var b = Blockly.Python.valueToCode(a, "x", Blockly.Python.ORDER_ATOMIC);
  475. a = Blockly.Python.valueToCode(a, "y", Blockly.Python.ORDER_ATOMIC);
  476. return ["display.get_pixel(" + b + ", " + a + ")", Blockly.Python.ORDER_MEMBER]
  477. }
  478. ;
  479. Blockly.Python.microbit_display_set_pixel = function (a) {
  480. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  481. var b = Blockly.Python.valueToCode(a, "x", Blockly.Python.ORDER_ATOMIC)
  482. , c = Blockly.Python.valueToCode(a, "y", Blockly.Python.ORDER_ATOMIC);
  483. a = Blockly.Python.valueToCode(a, "value", Blockly.Python.ORDER_ATOMIC);
  484. return "display.set_pixel(" + b + ", " + c + ", " + a + ")\n"
  485. }
  486. ;
  487. Blockly.Python.microbit_display_clear = function (a) {
  488. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  489. return "display.clear()\n"
  490. }
  491. ;
  492. Blockly.Python.microbit_display_show_image = function (a) {
  493. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  494. return "display.show(" + Blockly.Python.valueToCode(a, "image", Blockly.Python.ORDER_MEMBER) + ")\n"
  495. }
  496. ;
  497. Blockly.Python.microbit_display_show_image_with_bri = function (a) {
  498. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  499. var b = Blockly.Python.valueToCode(a, "image", Blockly.Python.ORDER_MEMBER)
  500. , c = Blockly.Python.valueToCode(a, "bri1", Blockly.Python.ORDER_ATOMIC);
  501. a = Blockly.Python.valueToCode(a, "bri2", Blockly.Python.ORDER_ATOMIC);
  502. return "display.show(" + b + "*(" + c + "/" + a + "))\n"
  503. }
  504. ;
  505. Blockly.Python.microbit_display_show_animation = function (a) {
  506. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  507. var b = Blockly.Python.valueToCode(a, "images", Blockly.Python.ORDER_MEMBER)
  508. , c = a.getFieldValue("delay")
  509. , d = "TRUE" == a.getFieldValue("wait") ? "True" : "False"
  510. , e = "TRUE" == a.getFieldValue("loop") ? "True" : "False";
  511. a = "TRUE" == a.getFieldValue("clear") ? "True" : "False";
  512. return "display.show(" + b + ", delay\x3d" + c + ", wait\x3d" + d + ", loop\x3d" + e + ", clear\x3d" + a + ")\n"
  513. }
  514. ;
  515. Blockly.Python.microbit_display_scroll = function (a) {
  516. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  517. return "display.scroll(" + Blockly.Python.valueToCode(a, "message", Blockly.Python.ORDER_ATOMIC) + ")\n"
  518. }
  519. ;
  520. Blockly.Python.microbit_display_scroll_animation = function (a) {
  521. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  522. var b = Blockly.Python.valueToCode(a, "message", Blockly.Python.ORDER_ATOMIC)
  523. , c = a.getFieldValue("delay")
  524. , d = "TRUE" == a.getFieldValue("wait") ? "True" : "False"
  525. , e = "TRUE" == a.getFieldValue("loop") ? "True" : "False";
  526. a = "TRUE" == a.getFieldValue("monospace") ? "True" : "False";
  527. return "display.scroll(" + b + ", delay\x3d" + c + ", wait\x3d" + d + ", loop\x3d" + e + ", monospace\x3d" + a + ")\n"
  528. }
  529. ;
  530. Blockly.Python.microbit_display_on = function (a) {
  531. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  532. return "display.on()\n"
  533. }
  534. ;
  535. Blockly.Python.microbit_display_off = function (a) {
  536. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  537. return "display.off()\npin3.read_digital()\npin4.read_digital()\npin10.read_digital()\npin3.set_pull(pin3.NO_PULL)\npin4.set_pull(pin4.NO_PULL)\npin10.set_pull(pin10.NO_PULL)\n"
  538. }
  539. ;
  540. Blockly.Python.microbit_display_is_on = function (a) {
  541. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  542. return ["display.is_on()", Blockly.Python.ORDER_MEMBER]
  543. }
  544. ;
  545. Blockly.Python.gamebit_xy = function (a) {
  546. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  547. return ["pin" + a.getFieldValue("xy") + ".read_analog()", Blockly.Python.ORDER_ATOMIC]
  548. }
  549. ;
  550. Blockly.Python.gamebit_shake = function (a) {
  551. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  552. return "pin16.write_digital(" + a.getFieldValue("shake") + ")\n"
  553. }
  554. ;
  555. Blockly.Python.gamebit_button = function (a) {
  556. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  557. a = a.getFieldValue("button");
  558. Blockly.Python.definitions_.gamebit_getKeyVal = "def getKeyVal():\n key \x3d 0\n btVal \x3d pin0.read_analog()\n if (btVal \x3c 70):\n key \x3d 1 # button Y\n elif (btVal \x3c 270 and btVal \x3e 130):\n key \x3d 2 # button X\n elif (btVal \x3c 470 and btVal \x3e 330):\n key \x3d 3 # button B\n elif (btVal \x3c 670 and btVal \x3e 530):\n key \x3d 4 # button A\n elif (btVal \x3c 870 and btVal \x3e 730):\n key \x3d 5 # rocker button\n if button_a.is_pressed():\n key \x3d 6 # button start\n if button_b.is_pressed():\n key \x3d 7 # button select\n return key";
  559. return ["getKeyVal() \x3d\x3d " + a, Blockly.Python.ORDER_ATOMIC]
  560. }
  561. ;
  562. Blockly.Python.addReservedWords("microbit");
  563. Blockly.Python.microbit_image_builtins = function (a) {
  564. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  565. return ["Image." + a.getFieldValue("image"), Blockly.Python.ORDER_MEMBER]
  566. }
  567. ;
  568. Blockly.Python.microbit_image_copy = function (a) {
  569. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  570. return [Blockly.Python.valueToCode(a, "image", Blockly.Python.ORDER_MEMBER) + ".copy()", Blockly.Python.ORDER_MEMBER]
  571. }
  572. ;
  573. Blockly.Python.microbit_image_invert = function (a) {
  574. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  575. return [Blockly.Python.valueToCode(a, "image", Blockly.Python.ORDER_MEMBER) + ".invert()", Blockly.Python.ORDER_MEMBER]
  576. }
  577. ;
  578. Blockly.Python.microbit_image_create = function (a) {
  579. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  580. var b = {
  581. "#000000": "0",
  582. "#440000": "1",
  583. "#660000": "2",
  584. "#880000": "3",
  585. "#aa0000": "4",
  586. "#bb0000": "5",
  587. "#cc0000": "6",
  588. "#dd0000": "7",
  589. "#ee0000": "8",
  590. "#ff0000": "9"
  591. }
  592. , c = b[a.getFieldValue("00")]
  593. , d = b[a.getFieldValue("01")]
  594. , e = b[a.getFieldValue("02")]
  595. , f = b[a.getFieldValue("03")]
  596. , g = b[a.getFieldValue("04")]
  597. , h = b[a.getFieldValue("10")]
  598. , n = b[a.getFieldValue("11")]
  599. , k = b[a.getFieldValue("12")]
  600. , p = b[a.getFieldValue("13")]
  601. , q = b[a.getFieldValue("14")]
  602. , r = b[a.getFieldValue("20")]
  603. , t = b[a.getFieldValue("21")]
  604. , u = b[a.getFieldValue("22")]
  605. , v = b[a.getFieldValue("23")]
  606. , w = b[a.getFieldValue("24")]
  607. , l = b[a.getFieldValue("30")]
  608. , x = b[a.getFieldValue("31")]
  609. , y = b[a.getFieldValue("32")]
  610. , z = b[a.getFieldValue("33")]
  611. , A = b[a.getFieldValue("34")]
  612. , B = b[a.getFieldValue("40")]
  613. , C = b[a.getFieldValue("41")]
  614. , D = b[a.getFieldValue("42")]
  615. , m = b[a.getFieldValue("43")];
  616. a = b[a.getFieldValue("44")];
  617. return ['Image("' + c + d + e + f + g + ":" + h + n + k + p + q + ":" + r + t + u + v + w + ":" + l + x + y + z + A + ":" + B + C + D + m + a + '")', Blockly.Python.ORDER_MEMBER]
  618. }
  619. ;
  620. Blockly.Python.microbit_image_create1 = function (a) {
  621. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  622. var b = Blockly.Python.valueToCode(a, "row0", Blockly.Python.ORDER_MEMBER)
  623. , c = Blockly.Python.valueToCode(a, "row1", Blockly.Python.ORDER_MEMBER)
  624. , d = Blockly.Python.valueToCode(a, "row2", Blockly.Python.ORDER_MEMBER)
  625. , e = Blockly.Python.valueToCode(a, "row3", Blockly.Python.ORDER_MEMBER);
  626. a = Blockly.Python.valueToCode(a, "row4", Blockly.Python.ORDER_MEMBER);
  627. return ["Image(str(" + b + ") + ':' + str(" + c + ") + ':' + str(" + d + ") + ':' + str(" + e + ") + ':' + str(" + a + "))", Blockly.Python.ORDER_MEMBER]
  628. }
  629. ;
  630. Blockly.Python.microbit_image_shift = function (a) {
  631. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  632. var b = Blockly.Python.valueToCode(a, "image", Blockly.Python.ORDER_MEMBER)
  633. , c = a.getFieldValue("shift");
  634. a = Blockly.Python.valueToCode(a, "pos", Blockly.Python.ORDER_ATOMIC);
  635. switch (c) {
  636. case "LEFT":
  637. b = b + ".shift_left(" + a + ")";
  638. break;
  639. case "RIGHT":
  640. b = b + ".shift_right(" + a + ")";
  641. break;
  642. case "UP":
  643. b = b + ".shift_up(" + a + ")";
  644. break;
  645. case "DOWN":
  646. b = b + ".shift_down(" + a + ")"
  647. }
  648. return [b, Blockly.Python.ORDER_MEMBER]
  649. }
  650. ;
  651. Blockly.Python.labplus_digital_output = function (a) {
  652. return [a.getFieldValue("outputSt"), Blockly.Python.ORDER_ATOMIC]
  653. }
  654. ;
  655. Blockly.Python.labplus_analog_output = function (a) {
  656. return [a.getFieldValue("num"), Blockly.Python.ORDER_ATOMIC]
  657. }
  658. ;
  659. Blockly.Python.labplus_servo_output = function (a) {
  660. return [a.getFieldValue("num"), Blockly.Python.ORDER_ATOMIC]
  661. }
  662. ;
  663. Blockly.Python.labplus_labplus_gpi = function (a) {
  664. return [a.getFieldValue("pin"), Blockly.Python.ORDER_ATOMIC]
  665. }
  666. ;
  667. Blockly.Python.labplus_analog_pins = function (a) {
  668. return [a.getFieldValue("pin"), Blockly.Python.ORDER_ATOMIC]
  669. }
  670. ;
  671. Blockly.Python.labplus_extended_pins = function (a) {
  672. return [a.getFieldValue("pin"), Blockly.Python.ORDER_ATOMIC]
  673. }
  674. ;
  675. Blockly.Python.labplus_infrared_is_triggered = function (a) {
  676. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  677. Blockly.Python.codeFunctions_.digitalRead = "def digitalRead(pin):\n pin.read_digital()\n pin.set_pull(pin.NO_PULL)\n return pin.read_digital()\n";
  678. return ["digitalRead(pin" + Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC) + ")", Blockly.Python.ORDER_ATOMIC]
  679. }
  680. ;
  681. Blockly.Python.labplus_smoke_detector_is_triggered = Blockly.Python.labplus_infrared_is_triggered;
  682. Blockly.Python.labplus_line_follower_is_triggered = Blockly.Python.labplus_infrared_is_triggered;
  683. Blockly.Python.labplus_shake_detector_is_triggered = Blockly.Python.labplus_infrared_is_triggered;
  684. Blockly.Python.labplus_fingerprint_sensor = Blockly.Python.labplus_infrared_is_triggered;
  685. Blockly.Python.labplus_get_rfid_id = Blockly.Python.labplus_infrared_is_triggered;
  686. Blockly.Python.labplus_sound_is_triggered = Blockly.Python.labplus_infrared_is_triggered;
  687. Blockly.Python.labplus_human_detector_is_triggered = Blockly.Python.labplus_infrared_is_triggered;
  688. Blockly.Python.labplus_magnetic_switch_is_triggered = Blockly.Python.labplus_infrared_is_triggered;
  689. Blockly.Python.labplus_button_module_is_pressed = Blockly.Python.labplus_infrared_is_triggered;
  690. Blockly.Python.labplus_water_level_detector = function (a) {
  691. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  692. return ["pin" + Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC) + ".read_analog()", Blockly.Python.ORDER_ATOMIC]
  693. }
  694. ;
  695. Blockly.Python.labplus_smoke_sensor = Blockly.Python.labplus_water_level_detector;
  696. Blockly.Python.labplus_sound_level_sensor = Blockly.Python.labplus_water_level_detector;
  697. Blockly.Python.labplus_soil_moisture_sensor = Blockly.Python.labplus_water_level_detector;
  698. Blockly.Python.labplus_rotate_dimmer = Blockly.Python.labplus_water_level_detector;
  699. Blockly.Python.labplus_light_sensor = Blockly.Python.labplus_water_level_detector;
  700. Blockly.Python.labplus_temperature_sensor = function (a) {
  701. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  702. Blockly.Python.definitions_.import_math = "import math";
  703. return ["pin" + Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC) + ".read_analog() * (3/10.24)", Blockly.Python.ORDER_ATOMIC]
  704. }
  705. ;
  706. Blockly.Python.labplus_heat_sensitive_detector = function (a) {
  707. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  708. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  709. return ["get_ntc_temp(pin" + Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC) + ")/10", Blockly.Python.ORDER_ATOMIC]
  710. }
  711. ;
  712. Blockly.Python.labplus_4button = function (a) {
  713. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  714. Blockly.Python.definitions_.labplus_4button = "def get4ButtonVal(index, pin):\n val \x3d pin.read_analog()\n btn \x3d 0\n if val \x3c 51:\n btn \x3d 1\n elif val \x3e 199 and val \x3c 301:\n btn \x3d 2\n elif val \x3e 449 and val \x3c 551:\n btn \x3d 3\n elif val \x3e 699 and val \x3c 801:\n btn \x3d 4\n return btn \x3d\x3d index\n";
  715. var b = Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC);
  716. return ["get4ButtonVal(" + a.getFieldValue("button_val") + ", pin" + b + ")", Blockly.Python.ORDER_ATOMIC]
  717. }
  718. ;
  719. Blockly.Python.labplus_joystick = function (a) {
  720. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  721. Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC);
  722. var b = Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC)
  723. , c = Blockly.Python.valueToCode(a, "pin1", Blockly.Python.ORDER_ATOMIC);
  724. "X" == a.getFieldValue("axis") && (b = c);
  725. return ["pin" + b + ".read_analog()", Blockly.Python.ORDER_ATOMIC]
  726. }
  727. ;
  728. Blockly.Python.labplus_i2c_light_sensor = function (a) {
  729. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  730. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  731. return ["get_bh1750_light()", Blockly.Python.ORDER_ATOMIC]
  732. }
  733. ;
  734. Blockly.Python.labplus_temp_hum_sensor = function (a) {
  735. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  736. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  737. return ["temperature" == a.getFieldValue("opt") ? "get_sht20_temp()/10" : "get_sht20_humi()", Blockly.Python.ORDER_ATOMIC]
  738. }
  739. ;
  740. Blockly.Python.labplus_i2c_ultrasonic = function (a) {
  741. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  742. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  743. return ["get_distance()", Blockly.Python.ORDER_ATOMIC]
  744. }
  745. ;
  746. Blockly.Python.labplus_read_color_from_i2c = function (a) {
  747. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  748. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  749. return "rgbColor \x3d get_color()\n"
  750. }
  751. ;
  752. Blockly.Python.labplus_rgb_color = function (a) {
  753. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  754. a = a.getFieldValue("color");
  755. var b = "rgbColor[0]";
  756. "G" == a ? b = "rgbColor[1]" : "B" == a && (b = "rgbColor[2]");
  757. return [b, Blockly.Python.ORDER_ATOMIC]
  758. }
  759. ;
  760. Blockly.Python.labplus_led = function (a) {
  761. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  762. var b = Blockly.Python.valueToCode(a, "output", Blockly.Python.ORDER_ATOMIC);
  763. return "pin" + Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC) + ".write_analog(" + b + ")\n"
  764. }
  765. ;
  766. Blockly.Python.labplus_usb_switch = function (a) {
  767. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  768. var b = a.getFieldValue("status");
  769. return "pin" + Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC) + ".write_digital(" + b + ")\n"
  770. }
  771. ;
  772. Blockly.Python.labplus_fan_switch = Blockly.Python.labplus_usb_switch;
  773. Blockly.Python.labplus_laser_switch = Blockly.Python.labplus_usb_switch;
  774. Blockly.Python.labplus_voice_record_on1 = function (a) {
  775. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  776. var b = a.getFieldValue("func")
  777. , c = Blockly.Python.valueToCode(a, "rec", Blockly.Python.ORDER_ATOMIC);
  778. a = Blockly.Python.valueToCode(a, "play", Blockly.Python.ORDER_ATOMIC);
  779. return "play" == b ? "pin" + c + ".write_digital(0)\npin" + a + ".write_digital(1)\n" : "pin" + a + ".write_digital(0)\npin" + c + ".write_digital(1)\n"
  780. }
  781. ;
  782. Blockly.Python.labplus_voice_record_off1 = function (a) {
  783. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  784. var b = Blockly.Python.valueToCode(a, "rec", Blockly.Python.ORDER_ATOMIC);
  785. a = Blockly.Python.valueToCode(a, "play", Blockly.Python.ORDER_ATOMIC);
  786. return "pin" + b + ".write_digital(0)\npin" + a + ".write_digital(0)\n"
  787. }
  788. ;
  789. Blockly.Python.labplus_motor_on1 = function (a) {
  790. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  791. var b = Blockly.Python.valueToCode(a, "ain1", Blockly.Python.ORDER_ATOMIC)
  792. , c = Blockly.Python.valueToCode(a, "ain2", Blockly.Python.ORDER_ATOMIC)
  793. , d = a.getFieldValue("status");
  794. a = Blockly.Python.valueToCode(a, "speed", Blockly.Python.ORDER_ATOMIC);
  795. 0 >= a && (a = 1);
  796. return "1" == d ? "pin" + b + ".write_analog(" + a + ")\nsleep(2)\npin" + c + ".write_analog(1)\nsleep(2)\n" : "pin" + c + ".write_analog(" + a + ")\nsleep(2)\npin" + b + ".write_analog(1)\nsleep(2)\n"
  797. }
  798. ;
  799. Blockly.Python.labplus_motor_off1 = function (a) {
  800. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  801. var b = Blockly.Python.valueToCode(a, "ain1", Blockly.Python.ORDER_ATOMIC);
  802. a = Blockly.Python.valueToCode(a, "ain2", Blockly.Python.ORDER_ATOMIC);
  803. return "pin" + b + ".write_analog(1)\nsleep(2)\npin" + a + ".write_analog(1)\nsleep(2)\n"
  804. }
  805. ;
  806. Blockly.Python.labplus_set_i2c_digits = function (a) {
  807. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  808. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  809. Blockly.Python.codeFunctions_.func_tm1650_init = "tm1650_init()";
  810. return "tm1650_print(str(" + Blockly.Python.valueToCode(a, "val", Blockly.Python.ORDER_ATOMIC) + "))\nsleep(1)\n"
  811. }
  812. ;
  813. Blockly.Python.labplus_turn_off_i2c_digits = function (a) {
  814. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  815. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  816. Blockly.Python.codeFunctions_.func_tm1650_init = "tm1650_init()";
  817. return "tm1650_clear()\n"
  818. }
  819. ;
  820. Blockly.Python.labplus_uart_set_midi_melodic = function (a) {
  821. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  822. Blockly.Python.codeFunctions_.func_bluebit_init_midi = "def midi_init(pin):\n uart.init(baudrate\x3d31250,tx\x3dpin)\n sleep(30)\n uart.write(bytearray([0xb0,0x78,0x00]))\n sleep(5)\n uart.write(bytearray([0xb0,0x79,0x7f]))\n sleep(15)\ndef midi_set_volume(vol):\n uart.write(bytearray([0xb0,0x07,vol]))\n sleep(10)\ndef midi_set_instrument(ins):\n uart.write(bytearray([0xc0,ins]))\n sleep(10)\ndef midi_note(note, on_off):\n if on_off \x3d\x3d 1:\n uart.write(bytearray([0x90,note,0x7f]))\n elif on_off \x3d\x3d 0:\n uart.write(bytearray([0x80,note,0x00]))\n sleep(5)";
  823. return "midi_set_instrument(" + a.getFieldValue("melodic") + ")\n"
  824. }
  825. ;
  826. Blockly.Python.labplus_uart_play_midi_note = function (a) {
  827. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  828. Blockly.Python.codeFunctions_.func_bluebit_init_midi = "def midi_init(pin):\n uart.init(baudrate\x3d31250,tx\x3dpin)\n sleep(30)\n uart.write(bytearray([0xb0,0x78,0x00]))\n sleep(5)\n uart.write(bytearray([0xb0,0x79,0x7f]))\n sleep(15)\ndef midi_set_volume(vol):\n uart.write(bytearray([0xb0,0x07,vol]))\n sleep(10)\ndef midi_set_instrument(ins):\n uart.write(bytearray([0xc0,ins]))\n sleep(10)\ndef midi_note(note, on_off):\n if on_off \x3d\x3d 1:\n uart.write(bytearray([0x90,note,0x7f]))\n elif on_off \x3d\x3d 0:\n uart.write(bytearray([0x80,note,0x00]))\n sleep(5)";
  829. var b = a.getFieldValue("note");
  830. a = a.getFieldValue("status");
  831. return "midi_note(" + b + "," + a + ")\n"
  832. }
  833. ;
  834. Blockly.Python.labplus_uart_set_midi_vol = function (a) {
  835. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  836. Blockly.Python.codeFunctions_.func_bluebit_init_midi = "def midi_init(pin):\n uart.init(baudrate\x3d31250,tx\x3dpin)\n sleep(30)\n uart.write(bytearray([0xb0,0x78,0x00]))\n sleep(5)\n uart.write(bytearray([0xb0,0x79,0x7f]))\n sleep(15)\ndef midi_set_volume(vol):\n uart.write(bytearray([0xb0,0x07,vol]))\n sleep(10)\ndef midi_set_instrument(ins):\n uart.write(bytearray([0xc0,ins]))\n sleep(10)\ndef midi_note(note, on_off):\n if on_off \x3d\x3d 1:\n uart.write(bytearray([0x90,note,0x7f]))\n elif on_off \x3d\x3d 0:\n uart.write(bytearray([0x80,note,0x00]))\n sleep(5)";
  837. return "midi_set_volume(" + Blockly.Python.valueToCode(a, "volume", Blockly.Python.ORDER_ATOMIC) + ")\n"
  838. }
  839. ;
  840. Blockly.Python.labplus_play_buzzer_note = function (a) {
  841. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  842. Blockly.Python.codeFunctions_["import music"] = "import music";
  843. return "music.pitch(" + Blockly.Python.valueToCode(a, "note", Blockly.Python.ORDER_ATOMIC) + ",-1)\n"
  844. }
  845. ;
  846. Blockly.Python.labplus_uart_init_midi = function (a) {
  847. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  848. Blockly.Python.codeFunctions_.func_bluebit_init_midi = "def midi_init(pin):\n uart.init(baudrate\x3d31250,tx\x3dpin)\n sleep(30)\n uart.write(bytearray([0xb0,0x78,0x00]))\n sleep(5)\n uart.write(bytearray([0xb0,0x79,0x7f]))\n sleep(15)\ndef midi_set_volume(vol):\n uart.write(bytearray([0xb0,0x07,vol]))\n sleep(10)\ndef midi_set_instrument(ins):\n uart.write(bytearray([0xc0,ins]))\n sleep(10)\ndef midi_note(note, on_off):\n if on_off \x3d\x3d 1:\n uart.write(bytearray([0x90,note,0x7f]))\n elif on_off \x3d\x3d 0:\n uart.write(bytearray([0x80,note,0x00]))\n sleep(5)";
  849. return "midi_init(pin" + Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC) + ")\n"
  850. }
  851. ;
  852. Blockly.Python.labplus_set_i2c_lcd1602_cursor = function (a) {
  853. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  854. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  855. Blockly.Python.codeFunctions_.init_lcd1602 = "lcd_init()\n";
  856. var b = Blockly.Python.valueToCode(a, "xaxis", Blockly.Python.ORDER_ATOMIC);
  857. a = Blockly.Python.valueToCode(a, "yaxis", Blockly.Python.ORDER_ATOMIC);
  858. return "lcd_set_cursor(" + b + ", " + a + ")\n"
  859. }
  860. ;
  861. Blockly.Python.labplus_set_i2c_lcd1602_print = function (a) {
  862. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  863. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  864. Blockly.Python.codeFunctions_.init_lcd1602 = "lcd_init()\n";
  865. return "lcd_print(" + Blockly.Python.valueToCode(a, "msg0", Blockly.Python.ORDER_ATOMIC) + ")\n"
  866. }
  867. ;
  868. Blockly.Python.labplus_set_i2c_lcd1602_scroll = function (a) {
  869. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  870. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  871. Blockly.Python.codeFunctions_.init_lcd1602 = "lcd_init()\n";
  872. return "aleft" == this.getFieldValue("dir") ? "lcd_cmd(0x18)\n" : "lcd_cmd(0x1c)\n"
  873. }
  874. ;
  875. Blockly.Python.labplus_set_i2c_lcd1602_clear = function (a) {
  876. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  877. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  878. Blockly.Python.codeFunctions_.init_lcd1602 = "lcd_init()\n";
  879. return "lcd_clear()\n"
  880. }
  881. ;
  882. Blockly.Python.labplus_set_i2c_lcd1602_state = function (a) {
  883. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  884. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  885. Blockly.Python.codeFunctions_.init_lcd1602 = "lcd_init()\n";
  886. return "lcd_cmd(" + this.getFieldValue("action") + ")\n"
  887. }
  888. ;
  889. Blockly.Python.labplus_set_i2c_led_matrix2 = function (a) {
  890. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  891. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  892. Blockly.Python.codeFunctions_.init_bluebit8x8Led = "matrix_init()";
  893. var b = "TRUE" == a.getFieldValue("00") ? "1" : "0"
  894. , c = "TRUE" == a.getFieldValue("01") ? "1" : "0"
  895. , d = "TRUE" == a.getFieldValue("02") ? "1" : "0"
  896. , e = "TRUE" == a.getFieldValue("03") ? "1" : "0"
  897. , f = "TRUE" == a.getFieldValue("04") ? "1" : "0"
  898. , g = "TRUE" == a.getFieldValue("05") ? "1" : "0"
  899. , h = "TRUE" == a.getFieldValue("06") ? "1" : "0"
  900. , n = "TRUE" == a.getFieldValue("07") ? "1" : "0"
  901. , k = "TRUE" == a.getFieldValue("10") ? "1" : "0"
  902. , p = "TRUE" == a.getFieldValue("11") ? "1" : "0"
  903. , q = "TRUE" == a.getFieldValue("12") ? "1" : "0"
  904. , r = "TRUE" == a.getFieldValue("13") ? "1" : "0"
  905. , t = "TRUE" == a.getFieldValue("14") ? "1" : "0"
  906. , u = "TRUE" == a.getFieldValue("15") ? "1" : "0"
  907. , v = "TRUE" == a.getFieldValue("16") ? "1" : "0"
  908. , w = "TRUE" == a.getFieldValue("17") ? "1" : "0"
  909. , l = "TRUE" == a.getFieldValue("20") ? "1" : "0"
  910. , x = "TRUE" == a.getFieldValue("21") ? "1" : "0"
  911. , y = "TRUE" == a.getFieldValue("22") ? "1" : "0"
  912. , z = "TRUE" == a.getFieldValue("23") ? "1" : "0"
  913. , A = "TRUE" == a.getFieldValue("24") ? "1" : "0"
  914. , B = "TRUE" == a.getFieldValue("25") ? "1" : "0"
  915. , C = "TRUE" == a.getFieldValue("26") ? "1" : "0"
  916. , D = "TRUE" == a.getFieldValue("27") ? "1" : "0"
  917. , m = "TRUE" == a.getFieldValue("30") ? "1" : "0"
  918. , I = "TRUE" == a.getFieldValue("31") ? "1" : "0"
  919. , J = "TRUE" == a.getFieldValue("32") ? "1" : "0"
  920. , K = "TRUE" == a.getFieldValue("33") ? "1" : "0"
  921. , L = "TRUE" == a.getFieldValue("34") ? "1" : "0"
  922. , M = "TRUE" == a.getFieldValue("35") ? "1" : "0"
  923. , N = "TRUE" == a.getFieldValue("36") ? "1" : "0"
  924. , O = "TRUE" == a.getFieldValue("37") ? "1" : "0"
  925. , E = "TRUE" == a.getFieldValue("40") ? "1" : "0"
  926. , P = "TRUE" == a.getFieldValue("41") ? "1" : "0"
  927. , Q = "TRUE" == a.getFieldValue("42") ? "1" : "0"
  928. , R = "TRUE" == a.getFieldValue("43") ? "1" : "0"
  929. , S = "TRUE" == a.getFieldValue("44") ? "1" : "0"
  930. , T = "TRUE" == a.getFieldValue("45") ? "1" : "0"
  931. , U = "TRUE" == a.getFieldValue("46") ? "1" : "0"
  932. , V = "TRUE" == a.getFieldValue("47") ? "1" : "0"
  933. , F = "TRUE" == a.getFieldValue("50") ? "1" : "0"
  934. , W = "TRUE" == a.getFieldValue("51") ? "1" : "0"
  935. , X = "TRUE" == a.getFieldValue("52") ? "1" : "0"
  936. , Y = "TRUE" == a.getFieldValue("53") ? "1" : "0"
  937. , Z = "TRUE" == a.getFieldValue("54") ? "1" : "0"
  938. , aa = "TRUE" == a.getFieldValue("55") ? "1" : "0"
  939. , ba = "TRUE" == a.getFieldValue("56") ? "1" : "0"
  940. , ca = "TRUE" == a.getFieldValue("57") ? "1" : "0"
  941. , G = "TRUE" == a.getFieldValue("60") ? "1" : "0"
  942. , da = "TRUE" == a.getFieldValue("61") ? "1" : "0"
  943. , ea = "TRUE" == a.getFieldValue("62") ? "1" : "0"
  944. , fa = "TRUE" == a.getFieldValue("63") ? "1" : "0"
  945. , ga = "TRUE" == a.getFieldValue("64") ? "1" : "0"
  946. , ha = "TRUE" == a.getFieldValue("65") ? "1" : "0"
  947. , ia = "TRUE" == a.getFieldValue("66") ? "1" : "0"
  948. , ja = "TRUE" == a.getFieldValue("67") ? "1" : "0"
  949. , H = "TRUE" == a.getFieldValue("70") ? "1" : "0"
  950. , ka = "TRUE" == a.getFieldValue("71") ? "1" : "0"
  951. , la = "TRUE" == a.getFieldValue("72") ? "1" : "0"
  952. , ma = "TRUE" == a.getFieldValue("73") ? "1" : "0"
  953. , na = "TRUE" == a.getFieldValue("74") ? "1" : "0"
  954. , oa = "TRUE" == a.getFieldValue("75") ? "1" : "0"
  955. , pa = "TRUE" == a.getFieldValue("76") ? "1" : "0";
  956. a = "TRUE" == a.getFieldValue("77") ? "1" : "0";
  957. b = parseInt(n + h + g + f + e + d + c + b, 2);
  958. k = parseInt(w + v + u + t + r + q + p + k, 2);
  959. l = parseInt(D + C + B + A + z + y + x + l, 2);
  960. m = parseInt(O + N + M + L + K + J + I + m, 2);
  961. E = parseInt(V + U + T + S + R + Q + P + E, 2);
  962. F = parseInt(ca + ba + aa + Z + Y + X + W + F, 2);
  963. G = parseInt(ja + ia + ha + ga + fa + ea + da + G, 2);
  964. H = parseInt(a + pa + oa + na + ma + la + ka + H, 2);
  965. return "matrix_draw_bmp(bytearray([" + b + ", " + k + ", " + l + ", " + m + ", " + E + ", " + F + ", " + G + ", " + H + "]))\nmatrix_show()\n"
  966. }
  967. ;
  968. Blockly.Python.labplus_set_i2c_led_matrix = function (a) {
  969. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  970. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  971. Blockly.Python.codeFunctions_.init_bluebit8x8Led = "matrix_init()";
  972. var b = Blockly.Python.valueToCode(a, "row1", Blockly.Python.ORDER_ATOMIC)
  973. , c = Blockly.Python.valueToCode(a, "row2", Blockly.Python.ORDER_ATOMIC)
  974. , d = Blockly.Python.valueToCode(a, "row3", Blockly.Python.ORDER_ATOMIC)
  975. , e = Blockly.Python.valueToCode(a, "row4", Blockly.Python.ORDER_ATOMIC)
  976. , f = Blockly.Python.valueToCode(a, "row5", Blockly.Python.ORDER_ATOMIC)
  977. , g = Blockly.Python.valueToCode(a, "row6", Blockly.Python.ORDER_ATOMIC)
  978. , h = Blockly.Python.valueToCode(a, "row7", Blockly.Python.ORDER_ATOMIC);
  979. a = Blockly.Python.valueToCode(a, "row8", Blockly.Python.ORDER_ATOMIC);
  980. return "matrix_draw_bmp(bytearray([" + b + ", " + c + ", " + d + ", " + e + ", " + f + ", " + g + ", " + h + ", " + a + "]))\nmatrix_show()\n"
  981. }
  982. ;
  983. Blockly.Python.labplus_i2c_led_matrix_side = function (a) {
  984. return [a.getFieldValue("side"), Blockly.Python.ORDER_ATOMIC]
  985. }
  986. ;
  987. Blockly.Python.labplus_set_i2c_led_matrix_draw_pixel = function (a) {
  988. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  989. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  990. Blockly.Python.codeFunctions_.init_bluebit8x8Led = "matrix_init()";
  991. var b = Blockly.Python.valueToCode(a, "x", Blockly.Python.ORDER_ATOMIC);
  992. a = Blockly.Python.valueToCode(a, "y", Blockly.Python.ORDER_ATOMIC);
  993. return "matrix_draw_pixel(" + b + ", " + a + ")\n"
  994. }
  995. ;
  996. Blockly.Python.labplus_set_i2c_led_matrix_show = function (a) {
  997. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  998. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  999. Blockly.Python.codeFunctions_.init_bluebit8x8Led = "matrix_init()";
  1000. return "matrix_show()\n"
  1001. }
  1002. ;
  1003. Blockly.Python.labplus_set_i2c_led_matrix_clear = function (a) {
  1004. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1005. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  1006. Blockly.Python.codeFunctions_.init_bluebit8x8Led = "matrix_init()";
  1007. return "matrix_clear()\n"
  1008. }
  1009. ;
  1010. Blockly.Python.labplus_uart_mp3_play = function (a) {
  1011. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1012. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  1013. return "mp3_play(" + Blockly.Python.valueToCode(a, "index", Blockly.Python.ORDER_ATOMIC) + ")\n"
  1014. }
  1015. ;
  1016. Blockly.Python.labplus_uart_mp3_set_volume = function (a) {
  1017. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1018. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  1019. return "mp3_volume(" + Blockly.Python.valueToCode(a, "vol", Blockly.Python.ORDER_ATOMIC) + ")\n"
  1020. }
  1021. ;
  1022. Blockly.Python.labplus_uart_mp3_set_status = function (a) {
  1023. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1024. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  1025. a = a.getFieldValue("status");
  1026. var b = "";
  1027. "PAUSE" == a && (b = "mp3_pause()\n");
  1028. "STOP" == a && (b = "mp3_stop()\n");
  1029. "PLAY" == a && (b = "mp3_start()\n");
  1030. return b
  1031. }
  1032. ;
  1033. Blockly.Python.labplus_uart_init = function (a) {
  1034. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1035. var b = Blockly.Python.valueToCode(a, "tx", Blockly.Python.ORDER_ATOMIC)
  1036. , c = Blockly.Python.valueToCode(a, "rx", Blockly.Python.ORDER_ATOMIC);
  1037. return "uart.init(baudrate\x3d" + Blockly.Python.valueToCode(a, "baudrate", Blockly.Python.ORDER_ATOMIC) + ", bits\x3d8, parity\x3dNone, stop\x3d1, tx\x3dpin" + b + ", rx\x3dpin" + c + ")\n"
  1038. }
  1039. ;
  1040. Blockly.Python.labplus_uart_any_msg_received = function (a) {
  1041. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1042. return ["uart.any()", Blockly.Python.ORDER_ATOMIC]
  1043. }
  1044. ;
  1045. Blockly.Python.labplus_uart_read_msg = function (a) {
  1046. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1047. return ["uart.read()", Blockly.Python.ORDER_ATOMIC]
  1048. }
  1049. ;
  1050. Blockly.Python.labplus_uart_write_msg = function (a) {
  1051. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1052. return "uart.write(" + Blockly.Python.valueToCode(a, "msg0", Blockly.Python.ORDER_ATOMIC) + ")\n"
  1053. }
  1054. ;
  1055. Blockly.Python.labplus_uart_set_oled = function (a) {
  1056. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1057. var b = Blockly.Python.valueToCode(a, "x1", Blockly.Python.ORDER_ATOMIC)
  1058. , c = Blockly.Python.valueToCode(a, "y1", Blockly.Python.ORDER_ATOMIC)
  1059. , d = Blockly.Python.valueToCode(a, "msg0", Blockly.Python.ORDER_ATOMIC);
  1060. a = a.getFieldValue("font");
  1061. return "uart.write('@' + str(int(" + b + ")) + ','+ str(int(" + c + ")) + '," + a + ":' + " + d + " + '\\r\\n')\n"
  1062. }
  1063. ;
  1064. Blockly.Python.labplus_uart_set_lcd_clear = function (a) {
  1065. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1066. var b = Blockly.Python.valueToCode(a, "x1", Blockly.Python.ORDER_ATOMIC)
  1067. , c = Blockly.Python.valueToCode(a, "y1", Blockly.Python.ORDER_ATOMIC)
  1068. , d = Blockly.Python.valueToCode(a, "x2", Blockly.Python.ORDER_ATOMIC);
  1069. a = Blockly.Python.valueToCode(a, "y2", Blockly.Python.ORDER_ATOMIC);
  1070. return "uart.write('#0:' + str(int(" + b + ")) + ','+ str(int(" + c + ")) + ','+ str(int(" + d + ")) + ','+ str(int(" + a + ")) + '\\r\\n')\n"
  1071. }
  1072. ;
  1073. Blockly.Python.labplus_uart_set_lcd_clear_all = function (a) {
  1074. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1075. return "uart.write('#0:0,0,127,63\\r\\n')\n"
  1076. }
  1077. ;
  1078. Blockly.Python.labplus_compitition_init_extended_pins = function (a) {
  1079. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1080. Blockly.Python.definitions_.def_extio = "def extIOInit(pin, mode):\n i2c.write(0x20, bytearray([3]))\n mode_old\x3di2c.read(0x20, 1)\n mode_new \x3d 0\n if mode \x3d\x3d 1:\n mode_new \x3d mode_old[0] | (1 \x3c\x3c pin)\n elif mode \x3d\x3d 0:\n mode_new \x3d mode_old[0] \x26 (~(1 \x3c\x3c pin))\n cfg\x3d bytearray([0x03,mode_new])\n i2c.write(0x20, cfg)";
  1081. var b = a.getFieldValue("extendedPins");
  1082. a = a.getFieldValue("PinMode");
  1083. return "extIOInit(" + b + "," + a + ")\n"
  1084. }
  1085. ;
  1086. Blockly.Python.labplus_set_extio_mode = function (a) {
  1087. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1088. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  1089. var b = a.getFieldValue("mode0")
  1090. , c = a.getFieldValue("mode1")
  1091. , d = a.getFieldValue("mode2")
  1092. , e = a.getFieldValue("mode3")
  1093. , f = a.getFieldValue("mode4")
  1094. , g = a.getFieldValue("mode5")
  1095. , h = a.getFieldValue("mode6");
  1096. return "extIO_mode(0b" + a.getFieldValue("mode7") + h + g + f + e + d + c + b + ")\n"
  1097. }
  1098. ;
  1099. Blockly.Python.labplus_read_extended_pin = function (a) {
  1100. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1101. Blockly.Python.definitions_.def_read_extio = "def readExtendedIO(pin):\n reg\x3dbytearray([0])\n i2c.write(0x20, reg)\n dat\x3di2c.read(0x20, 4)\n return (dat[0] \x3e\x3e pin) \x26 0x01";
  1102. return ["readExtendedIO(" + a.getFieldValue("extendedPins") + ")", Blockly.Python.ORDER_ATOMIC]
  1103. }
  1104. ;
  1105. Blockly.Python.labplus_write_extended_pin = function (a) {
  1106. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1107. Blockly.Python.definitions_.def_write_extio = "def writeExtendedIO(pin, output):\n reg\x3dbytearray([1])\n i2c.write(0x20, reg)\n stat_old\x3di2c.read(0x20, 3)\n stat_new \x3d 0\n if output \x3d\x3d 1:\n stat_new \x3d stat_old[0] | (1 \x3c\x3c pin)\n elif output \x3d\x3d 0:\n stat_new \x3d stat_old[0] \x26 (~(1 \x3c\x3c pin))\n cfg \x3d bytearray([0x01, stat_new])\n i2c.write(0x20,cfg)\n";
  1108. var b = a.getFieldValue("extendedPins");
  1109. a = Blockly.Python.valueToCode(a, "PinValue", Blockly.Python.ORDER_ATOMIC);
  1110. return "writeExtendedIO(" + b + "," + a + ")\n"
  1111. }
  1112. ;
  1113. Blockly.Python.labplus_voice_record_on = function (a) {
  1114. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1115. var b = a.getFieldValue("pinpair").split(" \x26 ");
  1116. a = a.getFieldValue("func");
  1117. var c = b[0]
  1118. , b = b[1];
  1119. return "play" == a ? "pin" + c + ".write_digital(0)\npin" + b + ".write_digital(1)\n" : "pin" + b + ".write_digital(0)\npin" + c + ".write_digital(1)\n"
  1120. }
  1121. ;
  1122. Blockly.Python.labplus_voice_record_off = function (a) {
  1123. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1124. a = a.getFieldValue("pinpair").split(" \x26 ");
  1125. return "pin" + a[0] + ".write_digital(0)\npin" + a[1] + ".write_digital(0)\n"
  1126. }
  1127. ;
  1128. Blockly.Python.labplus_mapping = function (a) {
  1129. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1130. var b = Blockly.Python.valueToCode(a, "inputNum", Blockly.Python.ORDER_ATOMIC);
  1131. Blockly.Python.valueToCode(a, "outputNum", Blockly.Python.ORDER_ATOMIC);
  1132. var c = Blockly.Python.valueToCode(a, "bMin", Blockly.Python.ORDER_ATOMIC)
  1133. , d = Blockly.Python.valueToCode(a, "bMax", Blockly.Python.ORDER_ATOMIC)
  1134. , e = Blockly.Python.valueToCode(a, "cMin", Blockly.Python.ORDER_ATOMIC);
  1135. a = Blockly.Python.valueToCode(a, "cMax", Blockly.Python.ORDER_ATOMIC);
  1136. Blockly.Python.definitions_.labplus_mapping = "def numberMap(inputNum,bMin,bMax,cMin,cMax):\n outputNum \x3d 0\n outputNum \x3d((cMax - cMin) / (bMax - bMin))*(inputNum - bMin)+cMin\n return outputNum\n";
  1137. return ["numberMap(" + b + "," + c + "," + d + "," + e + "," + a + ")", Blockly.Python.ORDER_ATOMIC]
  1138. }
  1139. ;
  1140. Blockly.Python.microbit_ext2_set_servo = function (a) {
  1141. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1142. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  1143. Blockly.Python.codeFunctions_.init_microbit_ext2 = "pca9685_init()";
  1144. var b = a.getFieldValue("servo");
  1145. a = Blockly.Python.valueToCode(a, "degree", Blockly.Python.ORDER_ATOMIC);
  1146. return "pca9685_servo(" + b + ", " + a + ")\n"
  1147. }
  1148. ;
  1149. Blockly.Python.microbit_ext2_set_motor = function (a) {
  1150. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1151. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  1152. Blockly.Python.codeFunctions_.init_microbit_ext2 = "pca9685_init()";
  1153. var b = a.getFieldValue("motor")
  1154. , c = a.getFieldValue("dir");
  1155. a = Blockly.Python.valueToCode(a, "spd", Blockly.Python.ORDER_ATOMIC);
  1156. "1" == c && (a = "-" + a);
  1157. return "pca9685_dc_motor(" + b + ", " + a + ")\n"
  1158. }
  1159. ;
  1160. Blockly.Python.microbit_ext2_stop_motor = function (a) {
  1161. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1162. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  1163. Blockly.Python.codeFunctions_.init_microbit_ext2 = "pca9685_init()";
  1164. return "pca9685_dc_motor(" + a.getFieldValue("motor") + ", 0)\n"
  1165. }
  1166. ;
  1167. Blockly.Python.microbit_ext2_set_pwm = function (a) {
  1168. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1169. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  1170. Blockly.Python.codeFunctions_.init_microbit_ext2 = "pca9685_init()";
  1171. var b = a.getFieldValue("channel");
  1172. a = Blockly.Python.valueToCode(a, "pwm", Blockly.Python.ORDER_ATOMIC);
  1173. return "pca9685_set_pwm(" + b + ", " + a + ")\n"
  1174. }
  1175. ;
  1176. Blockly.Python.microbit_ext2_step_motor = function (a) {
  1177. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1178. Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
  1179. Blockly.Python.codeFunctions_.init_microbit_ext2 = "pca9685_init()";
  1180. var b = a.getFieldValue("motor")
  1181. , c = a.getFieldValue("dir");
  1182. a = Blockly.Python.valueToCode(a, "speed", Blockly.Python.ORDER_ATOMIC);
  1183. return "pca9685_step_motor(" + b + ", " + c + ", " + a + ")\n"
  1184. }
  1185. ;
  1186. Blockly.Python.microbit_ext2_sound_value = function (a) {
  1187. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1188. return ["pin2.read_analog()", Blockly.Python.ORDER_ATOMIC]
  1189. }
  1190. ;
  1191. Blockly.Python.microbit_ext2_light_value = function (a) {
  1192. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1193. return ["pin1.read_analog()", Blockly.Python.ORDER_ATOMIC]
  1194. }
  1195. ;
  1196. Blockly.Python.addReservedWords("music");
  1197. var _microbit_music_default_pin = 0;
  1198. Blockly.Python.microbit_music_play_built_in = function (a) {
  1199. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1200. Blockly.Python.definitions_.import_music = "import music";
  1201. var b = a.getFieldValue("melody")
  1202. , c = "TRUE" == a.getFieldValue("wait") ? "True" : "False";
  1203. a = "TRUE" == a.getFieldValue("loop") ? "True" : "False";
  1204. return "music.play(music." + b + ", pin" + _microbit_music_default_pin + ", wait\x3d" + c + ", loop\x3d" + a + ")\n"
  1205. }
  1206. ;
  1207. Blockly.Python.microbit_music_pitch = function (a) {
  1208. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1209. Blockly.Python.definitions_.import_music = "import music";
  1210. var b = Blockly.Python.valueToCode(a, "pitch", Blockly.Python.ORDER_ATOMIC);
  1211. a = Blockly.Python.valueToCode(a, "duration", Blockly.Python.ORDER_ATOMIC);
  1212. return "music.pitch(" + b + ", " + a + ", pin" + _microbit_music_default_pin + ")\n"
  1213. }
  1214. ;
  1215. Blockly.Python.microbit_music_play_list_of_notes = function (a) {
  1216. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1217. Blockly.Python.definitions_.import_music = "import music";
  1218. var b = Blockly.Python.valueToCode(a, "notes", Blockly.Python.ORDER_ATOMIC)
  1219. , c = "TRUE" == a.getFieldValue("wait") ? "True" : "False";
  1220. a = "TRUE" == a.getFieldValue("loop") ? "True" : "False";
  1221. return "music.play(" + b + ",pin" + _microbit_music_default_pin + ", wait\x3d" + c + ", loop\x3d" + a + ")\n"
  1222. }
  1223. ;
  1224. Blockly.Python.microbit_music_reset = function (a) {
  1225. Blockly.Python.definitions_.import_music = "import music";
  1226. return "music.reset()\n"
  1227. }
  1228. ;
  1229. Blockly.Python.microbit_music_stop = function (a) {
  1230. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1231. Blockly.Python.definitions_.import_music = "import music";
  1232. return "music.stop(pin" + _microbit_music_default_pin + ")\n"
  1233. }
  1234. ;
  1235. Blockly.Python.microbit_music_set_tempo = function (a) {
  1236. Blockly.Python.definitions_.import_music = "import music";
  1237. var b = Blockly.Python.valueToCode(a, "ticks", Blockly.Python.ORDER_ATOMIC);
  1238. a = Blockly.Python.valueToCode(a, "bpm", Blockly.Python.ORDER_ATOMIC);
  1239. return "music.set_tempo(ticks\x3d" + b + ", bpm\x3d" + a + ")\n"
  1240. }
  1241. ;
  1242. Blockly.Python.microbit_music_get_tempo = function (a) {
  1243. Blockly.Python.definitions_.import_music = "import music";
  1244. return ["music.get_tempo()", Blockly.Python.ORDER_MEMBER]
  1245. }
  1246. ;
  1247. Blockly.Python.microbit_set_music_pin = function (a) {
  1248. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1249. _microbit_music_default_pin = Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC);
  1250. return ""
  1251. }
  1252. ;
  1253. Blockly.Python.addReservedWords("neopixel");
  1254. Blockly.Python.microbit_neopixel_initialise = function (a) {
  1255. Blockly.Python.definitions_.import_neopixel = "import neopixel";
  1256. var b = a.getFieldValue("n");
  1257. return ["neopixel.NeoPixel(" + a.getFieldValue("pin") + ", " + b + ")", Blockly.Python.ORDER_MEMBER]
  1258. }
  1259. ;
  1260. Blockly.Python.microbit_neopixel_clear = function (a) {
  1261. Blockly.Python.definitions_.import_neopixel = "import neopixel";
  1262. return Blockly.Python.valueToCode(a, "neopixel_object", Blockly.Python.ORDER_MEMBER) + ".clear()\n"
  1263. }
  1264. ;
  1265. Blockly.Python.microbit_neopixel_show = function (a) {
  1266. Blockly.Python.definitions_.import_neopixel = "import neopixel";
  1267. return Blockly.Python.valueToCode(a, "neopixel_object", Blockly.Python.ORDER_MEMBER) + ".show()\n"
  1268. }
  1269. ;
  1270. Blockly.Python.microbit_neopixel_set = function (a) {
  1271. Blockly.Python.definitions_.import_neopixel = "import neopixel";
  1272. var b = Blockly.Python.valueToCode(a, "pixel", Blockly.Python.ORDER_ATOMIC)
  1273. , c = Blockly.Python.valueToCode(a, "red", Blockly.Python.ORDER_ATOMIC)
  1274. , d = Blockly.Python.valueToCode(a, "green", Blockly.Python.ORDER_ATOMIC)
  1275. , e = Blockly.Python.valueToCode(a, "blue", Blockly.Python.ORDER_ATOMIC);
  1276. return Blockly.Python.valueToCode(a, "neopixel_object", Blockly.Python.ORDER_MEMBER) + "[" + b + "] \x3d (" + c + ", " + d + ", " + e + ")\n"
  1277. }
  1278. ;
  1279. Blockly.Python.addReservedWords("microbit");
  1280. Blockly.Python.microbit_pin_touched = function (a) {
  1281. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1282. return ["pin" + a.getFieldValue("pin") + ".is_touched()", Blockly.Python.ORDER_MEMBER]
  1283. }
  1284. ;
  1285. Blockly.Python.microbit_pin_was_pressed = function (a) {
  1286. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1287. return ["pin" + a.getFieldValue("pin") + ".was_pressed()", Blockly.Python.ORDER_MEMBER]
  1288. }
  1289. ;
  1290. Blockly.Python.microbit_pin_get_presses = function (a) {
  1291. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1292. return ["pin" + a.getFieldValue("pin") + ".get_presses()", Blockly.Python.ORDER_MEMBER]
  1293. }
  1294. ;
  1295. Blockly.Python.microbit_pin_read_analog = function (a) {
  1296. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1297. return ["pin" + Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC) + ".read_analog()", Blockly.Python.ORDER_MEMBER]
  1298. }
  1299. ;
  1300. Blockly.Python.microbit_pin_write_analog = function (a) {
  1301. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1302. var b = Blockly.Python.valueToCode(a, "output", Blockly.Python.ORDER_ATOMIC);
  1303. return "pin" + Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC) + ".write_analog(" + b + ")\n"
  1304. }
  1305. ;
  1306. Blockly.Python.microbit_pin_read_digital = function (a) {
  1307. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1308. return ["pin" + Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC) + ".read_digital()", Blockly.Python.ORDER_MEMBER]
  1309. }
  1310. ;
  1311. Blockly.Python.microbit_pin_write_digital = function (a) {
  1312. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1313. var b = Blockly.Python.valueToCode(a, "output", Blockly.Python.ORDER_ATOMIC);
  1314. 0 < b ? b = 1 : 0 > b && (b = 0);
  1315. return "pin" + Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC) + ".write_digital(" + b + ")\n"
  1316. }
  1317. ;
  1318. Blockly.Python.microbit_pin_set_servo = function (a) {
  1319. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1320. var b = Blockly.Python.valueToCode(a, "degree", Blockly.Python.ORDER_ATOMIC);
  1321. a = Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC);
  1322. Blockly.Python.definitions_["init_servo" + a] = "pin" + a + ".set_analog_period(20)";
  1323. return "pin" + a + ".write_analog((0.5 + " + b + "/90)*1023/20)\n"
  1324. }
  1325. ;
  1326. Blockly.Python.microbit_pin_set_mode = function (a) {
  1327. Blockly.Python.definitions_.import_microbit = "from microbit import *";
  1328. var b = a.getFieldValue("mode");
  1329. a = Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC);
  1330. switch (b) {
  1331. case "UP":
  1332. return "pin" + a + ".set_pull(pin" + a + ".PULL_UP)\n";
  1333. case "DOWN":
  1334. return "pin" + a + ".set_pull(pin" + a + ".PULL_DOWN)\n";
  1335. default:
  1336. return "pin" + a + ".set_pull(pin" + a + ".NO_PULL)\n"
  1337. }
  1338. }
  1339. ;
  1340. Blockly.Python.addReservedWords("radio");
  1341. Blockly.Python.microbit_radio_on = function (a) {
  1342. Blockly.Python.definitions_.import_radio = "import radio";
  1343. return "radio.on()\n"
  1344. }
  1345. ;
  1346. Blockly.Python.microbit_radio_off = function (a) {
  1347. Blockly.Python.definitions_.import_radio = "import radio";
  1348. return "radio.off()\n"
  1349. }
  1350. ;
  1351. Blockly.Python.microbit_radio_config = function (a) {
  1352. Blockly.Python.definitions_.import_radio = "import radio";
  1353. var b = a.getFieldValue("length")
  1354. , c = a.getFieldValue("queue")
  1355. , d = a.getFieldValue("channel")
  1356. , e = a.getFieldValue("power")
  1357. , f = a.getFieldValue("address")
  1358. , g = a.getFieldValue("group");
  1359. a = a.getFieldValue("data_rate");
  1360. return "radio.config(length\x3d" + b + ", queue\x3d" + c + ", channel\x3d" + d + ", power\x3d" + e + ", address\x3d" + f + ", group\x3d" + g + ", data_rate\x3dradio." + a + ")\n"
  1361. }
  1362. ;
  1363. Blockly.Python.microbit_radio_reset = function (a) {
  1364. Blockly.Python.definitions_.import_radio = "import radio";
  1365. return "radio.reset()\n"
  1366. }
  1367. ;
  1368. Blockly.Python.microbit_radio_send_string = function (a) {
  1369. Blockly.Python.definitions_.import_radio = "import radio";
  1370. return "radio.send(" + Blockly.Python.valueToCode(a, "message", Blockly.Python.ORDER_ATOMIC) + ")\n"
  1371. }
  1372. ;
  1373. Blockly.Python.microbit_radio_send_bytes = function (a) {
  1374. Blockly.Python.definitions_.import_radio = "import radio";
  1375. return "radio.send_bytes(" + Blockly.Python.valueToCode(a, "message", Blockly.Python.ORDER_ATOMIC) + ")\n"
  1376. }
  1377. ;
  1378. Blockly.Python.microbit_radio_receive = function (a) {
  1379. Blockly.Python.definitions_.import_radio = "import radio";
  1380. return ["radio.receive()", Blockly.Python.ORDER_MEMBER]
  1381. }
  1382. ;
  1383. Blockly.Python.microbit_radio_receive_bytes = function (a) {
  1384. Blockly.Python.definitions_.import_radio = "import radio";
  1385. return ["radio.receive_bytes()", Blockly.Python.ORDER_MEMBER]
  1386. }
  1387. ;
  1388. Blockly.Python.addReservedWords("speech");
  1389. Blockly.Python.microbit_speech_say = function (a) {
  1390. Blockly.Python.definitions_.import_speech = "import speech";
  1391. return "speech.say(" + Blockly.Python.valueToCode(a, "english", Blockly.Python.ORDER_ATOMIC) + ")\n"
  1392. }
  1393. ;
  1394. Blockly.Python.microbit_speech_pronounce = function (a) {
  1395. Blockly.Python.definitions_.import_speech = "import speech";
  1396. return "speech.pronounce(" + Blockly.Python.valueToCode(a, "phonemes", Blockly.Python.ORDER_ATOMIC) + ")\n"
  1397. }
  1398. ;
  1399. Blockly.Python.microbit_speech_sing = function (a) {
  1400. Blockly.Python.definitions_.import_speech = "import speech";
  1401. return "speech.sing(" + Blockly.Python.valueToCode(a, "song", Blockly.Python.ORDER_ATOMIC) + ")\n"
  1402. }
  1403. ;
  1404. Blockly.Python.microbit_mu_led_level = function (a) {
  1405. return [a.getFieldValue("level"), Blockly.Python.ORDER_ATOMIC]
  1406. }
  1407. ;
  1408. Blockly.Python.microbit_mu_vision_type = function (a) {
  1409. return [a.getFieldValue("vision_type"), Blockly.Python.ORDER_ATOMIC]
  1410. }
  1411. ;
  1412. Blockly.Python.microbit_mu_vision_type_multi = function (a) {
  1413. var b = "TRUE" == a.getFieldValue("type1")
  1414. , c = "TRUE" == a.getFieldValue("type2")
  1415. , d = "TRUE" == a.getFieldValue("type3")
  1416. , e = "TRUE" == a.getFieldValue("type4")
  1417. , f = "TRUE" == a.getFieldValue("type5")
  1418. , g = "TRUE" == a.getFieldValue("type6")
  1419. , h = "TRUE" == a.getFieldValue("type7");
  1420. if ("TRUE" == a.getFieldValue("type8"))
  1421. return ["VISION_ALL", Blockly.Python.ORDER_ATOMIC];
  1422. a = "";
  1423. b && (a += " | VISION_COLOR_DETECT");
  1424. c && (a += " | VISION_COLOR_RECOGNITION");
  1425. d && (a += " | VISION_BALL_DETECT");
  1426. e && (a += " | VISION_BODY_DETECT");
  1427. f && (a += " | VISION_SHAPE_CARD_DETECT");
  1428. g && (a += " | VISION_TRAFFIC_CARD_DETECT");
  1429. h && (a += " | VISION_NUM_CARD_DETECT");
  1430. "" != a && (a = a.substring(3));
  1431. return [a, Blockly.Python.ORDER_ATOMIC]
  1432. }
  1433. ;
  1434. Blockly.Python.microbit_mu_init = function (a) {
  1435. Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
  1436. a = a.getFieldValue("address");
  1437. Blockly.Python.codeFunctions_.define_mu = "mu \x3d MuVisionSensor(" + a + ")";
  1438. return "mu.begin()\n"
  1439. }
  1440. ;
  1441. Blockly.Python.microbit_mu_begin = function (a) {
  1442. Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
  1443. return "mu.VisionBegin(" + Blockly.Python.valueToCode(a, "vision_type", Blockly.Python.ORDER_ATOMIC) + ")\n"
  1444. }
  1445. ;
  1446. Blockly.Python.microbit_mu_set_level = function (a) {
  1447. Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
  1448. var b = Blockly.Python.valueToCode(a, "vision_type", Blockly.Python.ORDER_ATOMIC);
  1449. a = a.getFieldValue("level");
  1450. return "mu.VisionSetLevel(" + b + ", " + a + ")\n"
  1451. }
  1452. ;
  1453. Blockly.Python.microbit_mu_get_level = function (a) {
  1454. Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
  1455. return ["mu.VisionSetLevel(" + Blockly.Python.valueToCode(a, "vision_type", Blockly.Python.ORDER_ATOMIC) + ")", Blockly.Python.ORDER_ATOMIC]
  1456. }
  1457. ;
  1458. Blockly.Python.microbit_mu_set_fps_mode = function (a) {
  1459. Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
  1460. return "mu.CameraSetFPS(" + a.getFieldValue("mode") + ")\n"
  1461. }
  1462. ;
  1463. Blockly.Python.microbit_mu_get_fps_mode = function (a) {
  1464. Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
  1465. return ["mu.CameraGetFPS()", Blockly.Python.ORDER_ATOMIC]
  1466. }
  1467. ;
  1468. Blockly.Python.microbit_mu_set_awb_mode = function (a) {
  1469. Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
  1470. return "mu.CameraSetAwb(" + a.getFieldValue("mode") + ")\n"
  1471. }
  1472. ;
  1473. Blockly.Python.microbit_mu_get_awb_mode = function (a) {
  1474. Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
  1475. return ["mu.CameraGetAwb()", Blockly.Python.ORDER_ATOMIC]
  1476. }
  1477. ;
  1478. Blockly.Python.microbit_mu_set_zoom_mode = function (a) {
  1479. Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
  1480. return "mu.CameraSetZoom(" + a.getFieldValue("mode") + ")\n"
  1481. }
  1482. ;
  1483. Blockly.Python.microbit_mu_get_zoom_mode = function (a) {
  1484. Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
  1485. return ["mu.CameraGetZoom()", Blockly.Python.ORDER_ATOMIC]
  1486. }
  1487. ;
  1488. Blockly.Python.microbit_mu_set_default = function (a) {
  1489. Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
  1490. return "mu.SensorSetDefault()\n"
  1491. }
  1492. ;
  1493. Blockly.Python.microbit_mu_restart = function (a) {
  1494. Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
  1495. return "mu.SensorSetRestart()\n"
  1496. }
  1497. ;
  1498. Blockly.Python.microbit_mu_led_set_color = function (a) {
  1499. Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
  1500. var b = a.getFieldValue("led")
  1501. , c = a.getFieldValue("detected_color")
  1502. , d = a.getFieldValue("undetected_color");
  1503. a = Blockly.Python.valueToCode(a, "level", Blockly.Python.ORDER_ATOMIC);
  1504. return "mu.LedSetColor(" + b + ", " + c + ", " + d + ", " + a + ")\n"
  1505. }
  1506. ;
  1507. Blockly.Python.microbit_mu_get_value = function (a) {
  1508. Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
  1509. var b = Blockly.Python.valueToCode(a, "vision_type", Blockly.Python.ORDER_ATOMIC);
  1510. a = a.getFieldValue("object_inf");
  1511. return ["mu.GetValue(" + b + ", " + a + ")", Blockly.Python.ORDER_ATOMIC]
  1512. }
  1513. ;
  1514. Blockly.Python.microbit_mu_ls_type = function (a) {
  1515. return [a.getFieldValue("ls_type"), Blockly.Python.ORDER_ATOMIC]
  1516. }
  1517. ;
  1518. Blockly.Python.microbit_mu_ls_begin = function (a) {
  1519. Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
  1520. return "mu.LsBegin(" + Blockly.Python.valueToCode(a, "ls_type", Blockly.Python.ORDER_ATOMIC) + ")\n"
  1521. }
  1522. ;
  1523. Blockly.Python.microbit_mu_ls_end = function (a) {
  1524. Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
  1525. return "mu.LsEnd(" + Blockly.Python.valueToCode(a, "ls_type", Blockly.Python.ORDER_ATOMIC) + ")\n"
  1526. }
  1527. ;
  1528. Blockly.Python.microbit_mu_ls_set_sensitivity = function (a) {
  1529. Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
  1530. return "mu.LsSetSensitivity(" + a.getFieldValue("sensitivity") + ")\n"
  1531. }
  1532. ;
  1533. Blockly.Python.microbit_mu_ls_wb_enable = function (a) {
  1534. Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
  1535. return "mu.LsWhiteBalanceEnable()\n"
  1536. }
  1537. ;
  1538. Blockly.Python.microbit_mu_ls_read_proximity = function (a) {
  1539. Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
  1540. return ["mu.LsReadProximity()", Blockly.Python.ORDER_ATOMIC]
  1541. }
  1542. ;
  1543. Blockly.Python.microbit_mu_ls_read_ambient_light = function (a) {
  1544. Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
  1545. return ["mu.LsReadAmbientLight()", Blockly.Python.ORDER_ATOMIC]
  1546. }
  1547. ;
  1548. Blockly.Python.microbit_mu_ls_read_color = function (a) {
  1549. Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
  1550. a = a.getFieldValue("color_t");
  1551. switch (a) {
  1552. case "LsRawColorRed":
  1553. case "LsRawColorGreen":
  1554. case "LsRawColorBlue":
  1555. a = "mu.LsReadRawColor(" + a + ")";
  1556. break;
  1557. default:
  1558. a = "mu.LsReadColor(" + a + ")"
  1559. }
  1560. return [a, Blockly.Python.ORDER_ATOMIC]
  1561. }
  1562. ;
  1563. Blockly.Python.microbit_mu_ls_read_gesture = function (a) {
  1564. Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
  1565. return ["mu.LsReadGesture()", Blockly.Python.ORDER_ATOMIC]
  1566. }
  1567. ;