python_microbit.min.js 94 KB

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