1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567 |
- 'use strict';
- goog.provide('Blockly.Python.microbit');
- goog.require('Blockly.Python');
- Blockly.Python.addReservedWords("microbit");
- Blockly.Python.botbit_rec_from_microbit = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_math = "import math";
- 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";
- 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()"
- }
- ;
- Blockly.Python.microbit_update_msg_args = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "msg", Blockly.Python.ORDER_ATOMIC)
- , c = Blockly.Python.valueToCode(a, "x", Blockly.Python.ORDER_ATOMIC)
- , d = Blockly.Python.valueToCode(a, "y", Blockly.Python.ORDER_ATOMIC)
- , e = Blockly.Python.valueToCode(a, "z", Blockly.Python.ORDER_ATOMIC)
- , f = Blockly.Python.valueToCode(a, "L", Blockly.Python.ORDER_ATOMIC);
- a = Blockly.Python.valueToCode(a, "R", Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.codeFunctions_.msg_args = "x \x3d 0\ny \x3d 0\nz \x3d 0\na \x3d 0";
- 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"
- }
- ;
- Blockly.Python.microbit_microbit_panic = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return "panic(" + a.getFieldValue("code") + ")\n"
- }
- ;
- Blockly.Python.microbit_microbit_reset = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return "reset()\n"
- }
- ;
- Blockly.Python.microbit_microbit_sleep = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return "sleep(" + Blockly.Python.valueToCode(a, "duration", Blockly.Python.ORDER_ATOMIC) + ")\n"
- }
- ;
- Blockly.Python.microbit_microbit_running_time = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["running_time()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_microbit_temperature = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["temperature()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_init_i2c = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "fqs", Blockly.Python.ORDER_ATOMIC)
- , c = Blockly.Python.valueToCode(a, "sda", Blockly.Python.ORDER_ATOMIC);
- a = Blockly.Python.valueToCode(a, "scl", Blockly.Python.ORDER_ATOMIC);
- return "i2c.init(freq\x3d" + b + ", sda\x3dpin" + c + ", scl\x3dpin" + a + ")\n"
- }
- ;
- Blockly.Python.microbit_i2c_read = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "addr", Blockly.Python.ORDER_ATOMIC);
- a = Blockly.Python.valueToCode(a, "bytes", Blockly.Python.ORDER_ATOMIC);
- return ["i2c.read(addr\x3d" + b + ", n\x3d" + a + ", repeat\x3dFalse)\n", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_i2c_write = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "addr", Blockly.Python.ORDER_ATOMIC);
- a = Blockly.Python.valueToCode(a, "buf", Blockly.Python.ORDER_ATOMIC);
- return "i2c.write(addr\x3d" + b + ", buf\x3d" + a + ", repeat\x3dFalse)\n"
- }
- ;
- Blockly.Python.microbit_convert_bytearray = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["bytearray(" + Blockly.Python.valueToCode(a, "arr", Blockly.Python.ORDER_ATOMIC) + ")", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_uart_init = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "tx", Blockly.Python.ORDER_ATOMIC)
- , c = Blockly.Python.valueToCode(a, "rx", Blockly.Python.ORDER_ATOMIC);
- 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"
- }
- ;
- Blockly.Python.microbit_uart_any_msg_received = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["uart.any()", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.microbit_uart_read_msg = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- a = Blockly.Python.valueToCode(a, "type0", Blockly.Python.ORDER_ATOMIC);
- return ["uart." + ("1" == a ? "read()" : "readall" == a ? "readall()" : "readline" == a ? "readline()" : "read(" + a + ")"), Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.microbit_uart_read_msg_to = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "type0", Blockly.Python.ORDER_ATOMIC)
- , b = "readall" == b ? "" : ", " + b;
- return "uart.readinto(" + Blockly.Python.valueToCode(a, "buf0", Blockly.Python.ORDER_ATOMIC) + b + ")\n"
- }
- ;
- Blockly.Python.microbit_uart_write_msg = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return "uart.write(" + Blockly.Python.valueToCode(a, "msg0", Blockly.Python.ORDER_ATOMIC) + ")\n"
- }
- ;
- Blockly.Python.microbit_uart_msg_type = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return [a.getFieldValue("type0"), Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.microbit_uart_msg_type1 = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return [a.getFieldValue("type0"), Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.CocoRobo_custom_code = function (a) {
- var b = a.getFieldValue("custom_type");
- a = a.getFieldValue("custom_code");
- return "topping" == b ? (Blockly.Python.definitions_.import_custom = a,
- "") : a + "\n"
- }
- ;
- Blockly.Python.CocoRobo_print_to_chart = function (a) {
- for (var b = Array(a.itemCount_), c = 0; c < a.itemCount_; c++)
- b[c] = Blockly.Python.valueToCode(a, "ADD" + c, Blockly.Python.ORDER_NONE) || "None";
- return 1 == a.itemCount_ ? "print((" + b[0] + ",))\n" : "print((" + b.join(", ") + "))\n"
- }
- ;
- Blockly.Python.CocoRobo_init_chart = function (a) {
- var b = Array(a.itemCount_ + 1);
- b[0] = "'__TITLE'";
- for (var c = 0; c < a.itemCount_; c++)
- b[c + 1] = Blockly.Python.valueToCode(a, "ADD" + c, Blockly.Python.ORDER_NONE) || "None";
- return "sleep(50);print((" + b.join(", ") + "));sleep(50)\n"
- }
- ;
- Blockly.Python.math_random_int = function (a) {
- Blockly.Python.definitions_.import_random = "import random";
- var b = Blockly.Python.valueToCode(a, "FROM", Blockly.Python.ORDER_NONE) || "0";
- a = Blockly.Python.valueToCode(a, "TO", Blockly.Python.ORDER_NONE) || "0";
- return ["random.randint(" + b + ", " + a + ")", Blockly.Python.ORDER_FUNCTION_CALL]
- }
- ;
- Blockly.Python.math_random_float = function (a) {
- Blockly.Python.definitions_.import_random = "import random";
- return ["random.random()", Blockly.Python.ORDER_FUNCTION_CALL]
- }
- ;
- Blockly.Python.addReservedWords("microbit");
- Blockly.Python.microbit_accelerometer_get_x = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["accelerometer.get_x()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_accelerometer_get_y = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["accelerometer.get_y()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_accelerometer_get_z = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["accelerometer.get_z()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_accelerometer_was_gesture = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ['accelerometer.was_gesture("' + a.getFieldValue("gesture") + '")', Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_accelerometer_is_gesture = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ['accelerometer.is_gesture("' + a.getFieldValue("gesture") + '")', Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_accelerometer_get_gestures = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["accelerometer.get_gestures()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_accelerometer_current_gesture = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["accelerometer.current_gesture()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.botbit_spd = function (a) {
- return [a.getFieldValue("spd"), Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.botbit_motor_on = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- 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";
- var b = Blockly.Python.valueToCode(a, "speedl", Blockly.Python.ORDER_ATOMIC);
- a = Blockly.Python.valueToCode(a, "speedr", Blockly.Python.ORDER_ATOMIC);
- return "motion(" + b + "," + a + ")\n"
- }
- ;
- Blockly.Python.botbit_Steering_EngineL = function (a) {
- var _d = a.getFieldValue('dir');
- Blockly.Python.definitions_.import_machine = 'from machine import I2C, Pin';
- Blockly.Python.definitions_.import_mpu6050 = 'import mpu6050';
- Blockly.Python.addVariable('i2c', 'i2c = I2C(scl=Pin(17), sda=Pin(16))', true);
- Blockly.Python.addVariable('accel', 'accel = mpu6050.accel(i2c)', true);
- var _code = _d; // 'accel.get_values()[' + _d + ']';
- return _code;
- // Blockly.Python.definitions_.import_microbit = "from microbit import *";
- // Blockly.Python.definitions_.botbit_servo_pos = "servo_pos \x3d bytearray([0, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC])";
- // 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";
- // var b = a.getFieldValue("hands")
- // , c = a.getFieldValue("dir");
- // a = Blockly.Python.valueToCode(a, "angle", Blockly.Python.ORDER_ATOMIC);
- // "l" == b ? b = "y" == c ? "0" : "2" : (b = "y" == c ? "1" : "3",
- // a = "-" + a + "");
- // return "setServo(" + b + "," + a + ")\n"
- }
- ;
- Blockly.Python.botbit_Steering_Tilt = function (a) {
- Blockly.Python.definitions_.import_machine = 'from machine import I2C, Pin';
- Blockly.Python.definitions_.import_mpu6050 = 'import mpu6050';
- Blockly.Python.addVariable('i2c', 'i2c = I2C(scl=Pin(17), sda=Pin(16))', true);
- Blockly.Python.addVariable('accel', 'accel = mpu6050.accel(i2c)', true);
- var _d = a.getFieldValue('dir');
- Blockly.Python.definitions_.get_tilt_angle = 'def get_tilt_angle(_axis):\n' +
- ' _Ax = accel.get_values()[\'AcX\']\n' +
- ' _Ay = accel.get_values()[\'AcY\']\n' +
- ' _Az = accel.get_values()[\'AcZ\']\n' +
- ' if \'X\' == _axis:\n' +
- ' _T = math.sqrt(_Ay ** 2 + _Az ** 2)\n' +
- ' if _Az < 0: return math.degrees(math.atan2(_Ax , _T))\n' +
- ' else: return 180 - math.degrees(math.atan2(_Ax , _T))\n' +
- ' elif \'Y\' == _axis:\n' +
- ' _T = math.sqrt(_Ax ** 2 + _Az ** 2)\n' +
- ' if _Az < 0: return math.degrees(math.atan2(_Ay , _T))\n' +
- ' else: return 180 - math.degrees(math.atan2(_Ay , _T))\n' +
- ' elif \'Z\' == _axis:\n' +
- ' _T = math.sqrt(_Ax ** 2 + _Ay ** 2)\n' +
- ' if (_Ax + _Ay) < 0: return 180 - math.degrees(math.atan2(_T , _Az))\n' +
- ' else: return math.degrees(math.atan2(_T , _Az)) - 180\n' +
- ' return 0\n';
- return "get_tilt_angle(\"" + _d + "\")";
- }
- ;
- Blockly.Python.botbit_Steering_Engine_leg = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.botbit_servo_pos = "servo_pos \x3d bytearray([0, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC])";
- 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";
- var b = a.getFieldValue("leg")
- , c = a.getFieldValue("dir");
- a = Blockly.Python.valueToCode(a, "walk", Blockly.Python.ORDER_ATOMIC);
- return "setServo(" + ("l" == b ? "y" == c ? "0" : "2" : "y" == c ? "1" : "3") + "," + a + ")\n"
- }
- ;
- Blockly.Python.botbit_updatePosition = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.botbit_servo_pos = "servo_pos \x3d bytearray([0, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC])";
- Blockly.Python.definitions_.botbit_updatePosition = "\ndef updatePosition():\n servo_pos[0] \x3d 0\n i2c.write(0x2A, servo_pos)\n";
- return "updatePosition()\n"
- }
- ;
- Blockly.Python.botbit_Ultrasonic = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- 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";
- return ["getDistance()", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.botbit_holdBall = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.botbit_servo_pos = "servo_pos \x3d bytearray([0, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC])";
- 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";
- Blockly.Python.definitions_.botbit_updatePosition = "\ndef updatePosition():\n servo_pos[0] \x3d 0\n i2c.write(0x2A, servo_pos)\n";
- 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";
- return "holdBall()\n"
- }
- ;
- Blockly.Python.botbit_walking = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_math = "import math";
- var b = Blockly.Python.valueToCode(a, "steps", Blockly.Python.ORDER_ATOMIC)
- , c = Blockly.Python.valueToCode(a, "T", Blockly.Python.ORDER_ATOMIC);
- a = a.getFieldValue("fx");
- return "walking(" + b + "," + c + "," + a + ")\n"
- }
- ;
- Blockly.Python.botbit_moonwalker = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_math = "import math";
- var b = Blockly.Python.valueToCode(a, "amplitude", Blockly.Python.ORDER_ATOMIC)
- , c = Blockly.Python.valueToCode(a, "steps2", Blockly.Python.ORDER_ATOMIC)
- , d = Blockly.Python.valueToCode(a, "T2", Blockly.Python.ORDER_ATOMIC);
- a = a.getFieldValue("fx");
- return "moonwalker(" + c + "," + d + "," + b + "," + a + ")\n"
- }
- ;
- Blockly.Python.botbit_crusaito = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_math = "import math";
- var b = Blockly.Python.valueToCode(a, "steps3", Blockly.Python.ORDER_ATOMIC)
- , c = Blockly.Python.valueToCode(a, "T3", Blockly.Python.ORDER_ATOMIC)
- , d = Blockly.Python.valueToCode(a, "amplitude3", Blockly.Python.ORDER_ATOMIC);
- a = a.getFieldValue("fx");
- return "crusaito(" + b + "," + c + "," + d + "," + a + ")\n"
- }
- ;
- Blockly.Python.botbit_flapping = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_math = "import math";
- var b = Blockly.Python.valueToCode(a, "steps4", Blockly.Python.ORDER_ATOMIC)
- , c = Blockly.Python.valueToCode(a, "T4", Blockly.Python.ORDER_ATOMIC)
- , d = Blockly.Python.valueToCode(a, "amplitude4", Blockly.Python.ORDER_ATOMIC);
- a = a.getFieldValue("fx");
- return "flapping(" + b + "," + c + "," + d + "," + a + ")\n"
- }
- ;
- Blockly.Python.botbit_home = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_math = "import math";
- Blockly.Python.definitions_.botbit_servo_pos = "servo_pos \x3d bytearray([0, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC])";
- 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";
- Blockly.Python.definitions_.botbit_updatePosition = "\ndef updatePosition():\n servo_pos[0] \x3d 0\n i2c.write(0x2A, servo_pos)\n";
- 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";
- return ""
- }
- ;
- Blockly.Python.botbit_jump = function (a) {
- return "jump(" + Blockly.Python.valueToCode(a, "t", Blockly.Python.ORDER_ATOMIC) + ")\n"
- }
- ;
- Blockly.Python.botbit_turn = function (a) {
- var b = a.getFieldValue("dir")
- , c = Blockly.Python.valueToCode(a, "t", Blockly.Python.ORDER_ATOMIC);
- return "turn(" + Blockly.Python.valueToCode(a, "steps", Blockly.Python.ORDER_ATOMIC) + ", " + c + ", " + b + ")\n"
- }
- ;
- Blockly.Python.botbit_setRGB = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.botbit_RGB = "def setRGB(cmd, r, g, b): \n i2c.write(0x0b, bytearray([cmd, r, g, b]))\n sleep(1)";
- var b = a.getFieldValue("left_right")
- , c = Blockly.Python.valueToCode(a, "b", Blockly.Python.ORDER_ATOMIC)
- , d = Blockly.Python.valueToCode(a, "g", Blockly.Python.ORDER_ATOMIC);
- a = Blockly.Python.valueToCode(a, "r", Blockly.Python.ORDER_ATOMIC);
- return "setRGB(" + b + ", " + a + ", " + d + ", " + c + ")\n"
- }
- ;
- Blockly.Python.botbit_setHSV = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- 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)";
- var b = a.getFieldValue("left_right")
- , c = Blockly.Python.valueToCode(a, "v", Blockly.Python.ORDER_ATOMIC)
- , d = Blockly.Python.valueToCode(a, "s", Blockly.Python.ORDER_ATOMIC);
- a = Blockly.Python.valueToCode(a, "h", Blockly.Python.ORDER_ATOMIC);
- return "setHSV(" + b + ", " + a + ", " + d + ", " + c + ")\n"
- }
- ;
- Blockly.Python.botbit_PIDtracking = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_math = "import math";
- var b = Blockly.Python.valueToCode(a, "kp", Blockly.Python.ORDER_ATOMIC)
- , c = Blockly.Python.valueToCode(a, "kd", Blockly.Python.ORDER_ATOMIC);
- a = Blockly.Python.valueToCode(a, "trackSpeed", Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_.botbit_servo_pos = "servo_pos \x3d bytearray([0, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC])";
- 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";
- Blockly.Python.definitions_.botbit_updatePosition = "\ndef updatePosition():\n servo_pos[0] \x3d 0\n i2c.write(0x2A, servo_pos)\n";
- 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";
- 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";
- return "PIDtracking(" + b + ", " + c + ", " + a + ")\nsleep(10)\n"
- }
- ;
- Blockly.Python.addReservedWords("microbit");
- Blockly.Python.microbit_button_is_pressed = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["button_" + a.getFieldValue("button") + ".is_pressed()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_button_was_pressed = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["button_" + a.getFieldValue("button") + ".was_pressed()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_button_get_presses = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["button_" + a.getFieldValue("button") + ".get_presses()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.Carbit_init = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- 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"
- }
- ;
- Blockly.Python.Carbit_setLeftMotor = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.Carbit_setLeftMotor = "\ndef setLeftMotor(speed):\n i2c.write(0x10, bytearray([0x01, speed]))\n";
- return "setLeftMotor(" + Blockly.Python.valueToCode(a, "LeftMotor", Blockly.Python.ORDER_ATOMIC) + ")\n"
- }
- ;
- Blockly.Python.Carbit_setRightMotor = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.Carbit_setRightMotor = "\ndef setRightMotor(speed): \n i2c.write(0x10, bytearray([0x02, speed]))\n";
- return "setRightMotor(" + Blockly.Python.valueToCode(a, "RightMotor", Blockly.Python.ORDER_ATOMIC) + ")\n"
- }
- ;
- Blockly.Python.Carbit_analog_light = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return "pin" + a.getFieldValue("light") + ".read_analog()\n"
- }
- ;
- Blockly.Python.Carbit_Speed_U10 = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.Carbit_Speed = "\ndef getSpeed():\n return i2c.read(0x10,4)\n";
- return ["(getSpeed()[2]+getSpeed()[3]*256)", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.Carbit_Speed_U11 = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.Carbit_Speed = "\ndef getSpeed():\n return i2c.read(0x10,4)\n";
- return ["(getSpeed()[0]+getSpeed()[1]*256)", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.Carbit_Distance = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- 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";
- return ["Distance()", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.Carbit_tracking = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "k_speed", Blockly.Python.ORDER_ATOMIC)
- , c = Blockly.Python.valueToCode(a, "speedMax", Blockly.Python.ORDER_ATOMIC)
- , d = Blockly.Python.valueToCode(a, "speedScale", Blockly.Python.ORDER_ATOMIC);
- a = Blockly.Python.valueToCode(a, "turnDelay", Blockly.Python.ORDER_ATOMIC);
- 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";
- return "pidControl()\n"
- }
- ;
- Blockly.Python.addReservedWords("microbit");
- Blockly.Python.microbit_compass_calibrate = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return "compass.calibrate()\n"
- }
- ;
- Blockly.Python.microbit_compass_is_calibrated = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["compass.is_calibrated()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_compass_heading = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["compass.heading()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_compass_get_field_strength = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["compass.get_field_strength()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_compass_x = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["compass.get_x()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_compass_y = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["compass.get_y()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_compass_z = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["compass.get_z()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.addReservedWords("microbit");
- Blockly.Python.microbit_display_get_pixel = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "x", Blockly.Python.ORDER_ATOMIC);
- a = Blockly.Python.valueToCode(a, "y", Blockly.Python.ORDER_ATOMIC);
- return ["display.get_pixel(" + b + ", " + a + ")", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_display_set_pixel = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "x", Blockly.Python.ORDER_ATOMIC)
- , c = Blockly.Python.valueToCode(a, "y", Blockly.Python.ORDER_ATOMIC);
- a = Blockly.Python.valueToCode(a, "value", Blockly.Python.ORDER_ATOMIC);
- return "display.set_pixel(" + b + ", " + c + ", " + a + ")\n"
- }
- ;
- Blockly.Python.microbit_display_clear = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return "display.clear()\n"
- }
- ;
- Blockly.Python.microbit_display_show_image = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return "display.show(" + Blockly.Python.valueToCode(a, "image", Blockly.Python.ORDER_MEMBER) + ")\n"
- }
- ;
- Blockly.Python.microbit_display_show_image_with_bri = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "image", Blockly.Python.ORDER_MEMBER)
- , c = Blockly.Python.valueToCode(a, "bri1", Blockly.Python.ORDER_ATOMIC);
- a = Blockly.Python.valueToCode(a, "bri2", Blockly.Python.ORDER_ATOMIC);
- return "display.show(" + b + "*(" + c + "/" + a + "))\n"
- }
- ;
- Blockly.Python.microbit_display_show_animation = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "images", Blockly.Python.ORDER_MEMBER)
- , c = a.getFieldValue("delay")
- , d = "TRUE" == a.getFieldValue("wait") ? "True" : "False"
- , e = "TRUE" == a.getFieldValue("loop") ? "True" : "False";
- a = "TRUE" == a.getFieldValue("clear") ? "True" : "False";
- return "display.show(" + b + ", delay\x3d" + c + ", wait\x3d" + d + ", loop\x3d" + e + ", clear\x3d" + a + ")\n"
- }
- ;
- Blockly.Python.microbit_display_scroll = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return "display.scroll(" + Blockly.Python.valueToCode(a, "message", Blockly.Python.ORDER_ATOMIC) + ")\n"
- }
- ;
- Blockly.Python.microbit_display_scroll_animation = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "message", Blockly.Python.ORDER_ATOMIC)
- , c = a.getFieldValue("delay")
- , d = "TRUE" == a.getFieldValue("wait") ? "True" : "False"
- , e = "TRUE" == a.getFieldValue("loop") ? "True" : "False";
- a = "TRUE" == a.getFieldValue("monospace") ? "True" : "False";
- return "display.scroll(" + b + ", delay\x3d" + c + ", wait\x3d" + d + ", loop\x3d" + e + ", monospace\x3d" + a + ")\n"
- }
- ;
- Blockly.Python.microbit_display_on = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return "display.on()\n"
- }
- ;
- Blockly.Python.microbit_display_off = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- 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"
- }
- ;
- Blockly.Python.microbit_display_is_on = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["display.is_on()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.gamebit_xy = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["pin" + a.getFieldValue("xy") + ".read_analog()", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.gamebit_shake = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return "pin16.write_digital(" + a.getFieldValue("shake") + ")\n"
- }
- ;
- Blockly.Python.gamebit_button = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- a = a.getFieldValue("button");
- 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";
- return ["getKeyVal() \x3d\x3d " + a, Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.addReservedWords("microbit");
- Blockly.Python.microbit_image_builtins = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["Image." + a.getFieldValue("image"), Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_image_copy = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return [Blockly.Python.valueToCode(a, "image", Blockly.Python.ORDER_MEMBER) + ".copy()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_image_invert = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return [Blockly.Python.valueToCode(a, "image", Blockly.Python.ORDER_MEMBER) + ".invert()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_image_create = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = {
- "#000000": "0",
- "#440000": "1",
- "#660000": "2",
- "#880000": "3",
- "#aa0000": "4",
- "#bb0000": "5",
- "#cc0000": "6",
- "#dd0000": "7",
- "#ee0000": "8",
- "#ff0000": "9"
- }
- , c = b[a.getFieldValue("00")]
- , d = b[a.getFieldValue("01")]
- , e = b[a.getFieldValue("02")]
- , f = b[a.getFieldValue("03")]
- , g = b[a.getFieldValue("04")]
- , h = b[a.getFieldValue("10")]
- , n = b[a.getFieldValue("11")]
- , k = b[a.getFieldValue("12")]
- , p = b[a.getFieldValue("13")]
- , q = b[a.getFieldValue("14")]
- , r = b[a.getFieldValue("20")]
- , t = b[a.getFieldValue("21")]
- , u = b[a.getFieldValue("22")]
- , v = b[a.getFieldValue("23")]
- , w = b[a.getFieldValue("24")]
- , l = b[a.getFieldValue("30")]
- , x = b[a.getFieldValue("31")]
- , y = b[a.getFieldValue("32")]
- , z = b[a.getFieldValue("33")]
- , A = b[a.getFieldValue("34")]
- , B = b[a.getFieldValue("40")]
- , C = b[a.getFieldValue("41")]
- , D = b[a.getFieldValue("42")]
- , m = b[a.getFieldValue("43")];
- a = b[a.getFieldValue("44")];
- 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]
- }
- ;
- Blockly.Python.microbit_image_create1 = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "row0", Blockly.Python.ORDER_MEMBER)
- , c = Blockly.Python.valueToCode(a, "row1", Blockly.Python.ORDER_MEMBER)
- , d = Blockly.Python.valueToCode(a, "row2", Blockly.Python.ORDER_MEMBER)
- , e = Blockly.Python.valueToCode(a, "row3", Blockly.Python.ORDER_MEMBER);
- a = Blockly.Python.valueToCode(a, "row4", Blockly.Python.ORDER_MEMBER);
- return ["Image(str(" + b + ") + ':' + str(" + c + ") + ':' + str(" + d + ") + ':' + str(" + e + ") + ':' + str(" + a + "))", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_image_shift = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "image", Blockly.Python.ORDER_MEMBER)
- , c = a.getFieldValue("shift");
- a = Blockly.Python.valueToCode(a, "pos", Blockly.Python.ORDER_ATOMIC);
- switch (c) {
- case "LEFT":
- b = b + ".shift_left(" + a + ")";
- break;
- case "RIGHT":
- b = b + ".shift_right(" + a + ")";
- break;
- case "UP":
- b = b + ".shift_up(" + a + ")";
- break;
- case "DOWN":
- b = b + ".shift_down(" + a + ")"
- }
- return [b, Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.labplus_digital_output = function (a) {
- return [a.getFieldValue("outputSt"), Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.labplus_analog_output = function (a) {
- return [a.getFieldValue("num"), Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.labplus_servo_output = function (a) {
- return [a.getFieldValue("num"), Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.labplus_labplus_gpi = function (a) {
- return [a.getFieldValue("pin"), Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.labplus_analog_pins = function (a) {
- return [a.getFieldValue("pin"), Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.labplus_extended_pins = function (a) {
- return [a.getFieldValue("pin"), Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.labplus_infrared_is_triggered = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.codeFunctions_.digitalRead = "def digitalRead(pin):\n pin.read_digital()\n pin.set_pull(pin.NO_PULL)\n return pin.read_digital()\n";
- return ["digitalRead(pin" + Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC) + ")", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.labplus_smoke_detector_is_triggered = Blockly.Python.labplus_infrared_is_triggered;
- Blockly.Python.labplus_line_follower_is_triggered = Blockly.Python.labplus_infrared_is_triggered;
- Blockly.Python.labplus_shake_detector_is_triggered = Blockly.Python.labplus_infrared_is_triggered;
- Blockly.Python.labplus_fingerprint_sensor = Blockly.Python.labplus_infrared_is_triggered;
- Blockly.Python.labplus_get_rfid_id = Blockly.Python.labplus_infrared_is_triggered;
- Blockly.Python.labplus_sound_is_triggered = Blockly.Python.labplus_infrared_is_triggered;
- Blockly.Python.labplus_human_detector_is_triggered = Blockly.Python.labplus_infrared_is_triggered;
- Blockly.Python.labplus_magnetic_switch_is_triggered = Blockly.Python.labplus_infrared_is_triggered;
- Blockly.Python.labplus_button_module_is_pressed = Blockly.Python.labplus_infrared_is_triggered;
- Blockly.Python.labplus_water_level_detector = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["pin" + Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC) + ".read_analog()", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.labplus_smoke_sensor = Blockly.Python.labplus_water_level_detector;
- Blockly.Python.labplus_sound_level_sensor = Blockly.Python.labplus_water_level_detector;
- Blockly.Python.labplus_soil_moisture_sensor = Blockly.Python.labplus_water_level_detector;
- Blockly.Python.labplus_rotate_dimmer = Blockly.Python.labplus_water_level_detector;
- Blockly.Python.labplus_light_sensor = Blockly.Python.labplus_water_level_detector;
- Blockly.Python.labplus_temperature_sensor = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_math = "import math";
- return ["pin" + Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC) + ".read_analog() * (3/10.24)", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.labplus_heat_sensitive_detector = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- return ["get_ntc_temp(pin" + Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC) + ")/10", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.labplus_4button = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- 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";
- var b = Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC);
- return ["get4ButtonVal(" + a.getFieldValue("button_val") + ", pin" + b + ")", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.labplus_joystick = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC);
- var b = Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC)
- , c = Blockly.Python.valueToCode(a, "pin1", Blockly.Python.ORDER_ATOMIC);
- "X" == a.getFieldValue("axis") && (b = c);
- return ["pin" + b + ".read_analog()", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.labplus_i2c_light_sensor = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- return ["get_bh1750_light()", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.labplus_temp_hum_sensor = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- return ["temperature" == a.getFieldValue("opt") ? "get_sht20_temp()/10" : "get_sht20_humi()", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.labplus_i2c_ultrasonic = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- return ["get_distance()", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.labplus_read_color_from_i2c = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- return "rgbColor \x3d get_color()\n"
- }
- ;
- Blockly.Python.labplus_rgb_color = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- a = a.getFieldValue("color");
- var b = "rgbColor[0]";
- "G" == a ? b = "rgbColor[1]" : "B" == a && (b = "rgbColor[2]");
- return [b, Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.labplus_led = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "output", Blockly.Python.ORDER_ATOMIC);
- return "pin" + Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC) + ".write_analog(" + b + ")\n"
- }
- ;
- Blockly.Python.labplus_usb_switch = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = a.getFieldValue("status");
- return "pin" + Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC) + ".write_digital(" + b + ")\n"
- }
- ;
- Blockly.Python.labplus_fan_switch = Blockly.Python.labplus_usb_switch;
- Blockly.Python.labplus_laser_switch = Blockly.Python.labplus_usb_switch;
- Blockly.Python.labplus_voice_record_on1 = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = a.getFieldValue("func")
- , c = Blockly.Python.valueToCode(a, "rec", Blockly.Python.ORDER_ATOMIC);
- a = Blockly.Python.valueToCode(a, "play", Blockly.Python.ORDER_ATOMIC);
- 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"
- }
- ;
- Blockly.Python.labplus_voice_record_off1 = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "rec", Blockly.Python.ORDER_ATOMIC);
- a = Blockly.Python.valueToCode(a, "play", Blockly.Python.ORDER_ATOMIC);
- return "pin" + b + ".write_digital(0)\npin" + a + ".write_digital(0)\n"
- }
- ;
- Blockly.Python.labplus_motor_on1 = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "ain1", Blockly.Python.ORDER_ATOMIC)
- , c = Blockly.Python.valueToCode(a, "ain2", Blockly.Python.ORDER_ATOMIC)
- , d = a.getFieldValue("status");
- a = Blockly.Python.valueToCode(a, "speed", Blockly.Python.ORDER_ATOMIC);
- 0 >= a && (a = 1);
- 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"
- }
- ;
- Blockly.Python.labplus_motor_off1 = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "ain1", Blockly.Python.ORDER_ATOMIC);
- a = Blockly.Python.valueToCode(a, "ain2", Blockly.Python.ORDER_ATOMIC);
- return "pin" + b + ".write_analog(1)\nsleep(2)\npin" + a + ".write_analog(1)\nsleep(2)\n"
- }
- ;
- Blockly.Python.labplus_set_i2c_digits = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- Blockly.Python.codeFunctions_.func_tm1650_init = "tm1650_init()";
- return "tm1650_print(str(" + Blockly.Python.valueToCode(a, "val", Blockly.Python.ORDER_ATOMIC) + "))\nsleep(1)\n"
- }
- ;
- Blockly.Python.labplus_turn_off_i2c_digits = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- Blockly.Python.codeFunctions_.func_tm1650_init = "tm1650_init()";
- return "tm1650_clear()\n"
- }
- ;
- Blockly.Python.labplus_uart_set_midi_melodic = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- 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)";
- return "midi_set_instrument(" + a.getFieldValue("melodic") + ")\n"
- }
- ;
- Blockly.Python.labplus_uart_play_midi_note = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- 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)";
- var b = a.getFieldValue("note");
- a = a.getFieldValue("status");
- return "midi_note(" + b + "," + a + ")\n"
- }
- ;
- Blockly.Python.labplus_uart_set_midi_vol = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- 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)";
- return "midi_set_volume(" + Blockly.Python.valueToCode(a, "volume", Blockly.Python.ORDER_ATOMIC) + ")\n"
- }
- ;
- Blockly.Python.labplus_play_buzzer_note = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.codeFunctions_["import music"] = "import music";
- return "music.pitch(" + Blockly.Python.valueToCode(a, "note", Blockly.Python.ORDER_ATOMIC) + ",-1)\n"
- }
- ;
- Blockly.Python.labplus_uart_init_midi = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- 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)";
- return "midi_init(pin" + Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC) + ")\n"
- }
- ;
- Blockly.Python.labplus_set_i2c_lcd1602_cursor = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- Blockly.Python.codeFunctions_.init_lcd1602 = "lcd_init()\n";
- var b = Blockly.Python.valueToCode(a, "xaxis", Blockly.Python.ORDER_ATOMIC);
- a = Blockly.Python.valueToCode(a, "yaxis", Blockly.Python.ORDER_ATOMIC);
- return "lcd_set_cursor(" + b + ", " + a + ")\n"
- }
- ;
- Blockly.Python.labplus_set_i2c_lcd1602_print = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- Blockly.Python.codeFunctions_.init_lcd1602 = "lcd_init()\n";
- return "lcd_print(" + Blockly.Python.valueToCode(a, "msg0", Blockly.Python.ORDER_ATOMIC) + ")\n"
- }
- ;
- Blockly.Python.labplus_set_i2c_lcd1602_scroll = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- Blockly.Python.codeFunctions_.init_lcd1602 = "lcd_init()\n";
- return "aleft" == this.getFieldValue("dir") ? "lcd_cmd(0x18)\n" : "lcd_cmd(0x1c)\n"
- }
- ;
- Blockly.Python.labplus_set_i2c_lcd1602_clear = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- Blockly.Python.codeFunctions_.init_lcd1602 = "lcd_init()\n";
- return "lcd_clear()\n"
- }
- ;
- Blockly.Python.labplus_set_i2c_lcd1602_state = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- Blockly.Python.codeFunctions_.init_lcd1602 = "lcd_init()\n";
- return "lcd_cmd(" + this.getFieldValue("action") + ")\n"
- }
- ;
- Blockly.Python.labplus_set_i2c_led_matrix2 = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- Blockly.Python.codeFunctions_.init_bluebit8x8Led = "matrix_init()";
- var b = "TRUE" == a.getFieldValue("00") ? "1" : "0"
- , c = "TRUE" == a.getFieldValue("01") ? "1" : "0"
- , d = "TRUE" == a.getFieldValue("02") ? "1" : "0"
- , e = "TRUE" == a.getFieldValue("03") ? "1" : "0"
- , f = "TRUE" == a.getFieldValue("04") ? "1" : "0"
- , g = "TRUE" == a.getFieldValue("05") ? "1" : "0"
- , h = "TRUE" == a.getFieldValue("06") ? "1" : "0"
- , n = "TRUE" == a.getFieldValue("07") ? "1" : "0"
- , k = "TRUE" == a.getFieldValue("10") ? "1" : "0"
- , p = "TRUE" == a.getFieldValue("11") ? "1" : "0"
- , q = "TRUE" == a.getFieldValue("12") ? "1" : "0"
- , r = "TRUE" == a.getFieldValue("13") ? "1" : "0"
- , t = "TRUE" == a.getFieldValue("14") ? "1" : "0"
- , u = "TRUE" == a.getFieldValue("15") ? "1" : "0"
- , v = "TRUE" == a.getFieldValue("16") ? "1" : "0"
- , w = "TRUE" == a.getFieldValue("17") ? "1" : "0"
- , l = "TRUE" == a.getFieldValue("20") ? "1" : "0"
- , x = "TRUE" == a.getFieldValue("21") ? "1" : "0"
- , y = "TRUE" == a.getFieldValue("22") ? "1" : "0"
- , z = "TRUE" == a.getFieldValue("23") ? "1" : "0"
- , A = "TRUE" == a.getFieldValue("24") ? "1" : "0"
- , B = "TRUE" == a.getFieldValue("25") ? "1" : "0"
- , C = "TRUE" == a.getFieldValue("26") ? "1" : "0"
- , D = "TRUE" == a.getFieldValue("27") ? "1" : "0"
- , m = "TRUE" == a.getFieldValue("30") ? "1" : "0"
- , I = "TRUE" == a.getFieldValue("31") ? "1" : "0"
- , J = "TRUE" == a.getFieldValue("32") ? "1" : "0"
- , K = "TRUE" == a.getFieldValue("33") ? "1" : "0"
- , L = "TRUE" == a.getFieldValue("34") ? "1" : "0"
- , M = "TRUE" == a.getFieldValue("35") ? "1" : "0"
- , N = "TRUE" == a.getFieldValue("36") ? "1" : "0"
- , O = "TRUE" == a.getFieldValue("37") ? "1" : "0"
- , E = "TRUE" == a.getFieldValue("40") ? "1" : "0"
- , P = "TRUE" == a.getFieldValue("41") ? "1" : "0"
- , Q = "TRUE" == a.getFieldValue("42") ? "1" : "0"
- , R = "TRUE" == a.getFieldValue("43") ? "1" : "0"
- , S = "TRUE" == a.getFieldValue("44") ? "1" : "0"
- , T = "TRUE" == a.getFieldValue("45") ? "1" : "0"
- , U = "TRUE" == a.getFieldValue("46") ? "1" : "0"
- , V = "TRUE" == a.getFieldValue("47") ? "1" : "0"
- , F = "TRUE" == a.getFieldValue("50") ? "1" : "0"
- , W = "TRUE" == a.getFieldValue("51") ? "1" : "0"
- , X = "TRUE" == a.getFieldValue("52") ? "1" : "0"
- , Y = "TRUE" == a.getFieldValue("53") ? "1" : "0"
- , Z = "TRUE" == a.getFieldValue("54") ? "1" : "0"
- , aa = "TRUE" == a.getFieldValue("55") ? "1" : "0"
- , ba = "TRUE" == a.getFieldValue("56") ? "1" : "0"
- , ca = "TRUE" == a.getFieldValue("57") ? "1" : "0"
- , G = "TRUE" == a.getFieldValue("60") ? "1" : "0"
- , da = "TRUE" == a.getFieldValue("61") ? "1" : "0"
- , ea = "TRUE" == a.getFieldValue("62") ? "1" : "0"
- , fa = "TRUE" == a.getFieldValue("63") ? "1" : "0"
- , ga = "TRUE" == a.getFieldValue("64") ? "1" : "0"
- , ha = "TRUE" == a.getFieldValue("65") ? "1" : "0"
- , ia = "TRUE" == a.getFieldValue("66") ? "1" : "0"
- , ja = "TRUE" == a.getFieldValue("67") ? "1" : "0"
- , H = "TRUE" == a.getFieldValue("70") ? "1" : "0"
- , ka = "TRUE" == a.getFieldValue("71") ? "1" : "0"
- , la = "TRUE" == a.getFieldValue("72") ? "1" : "0"
- , ma = "TRUE" == a.getFieldValue("73") ? "1" : "0"
- , na = "TRUE" == a.getFieldValue("74") ? "1" : "0"
- , oa = "TRUE" == a.getFieldValue("75") ? "1" : "0"
- , pa = "TRUE" == a.getFieldValue("76") ? "1" : "0";
- a = "TRUE" == a.getFieldValue("77") ? "1" : "0";
- b = parseInt(n + h + g + f + e + d + c + b, 2);
- k = parseInt(w + v + u + t + r + q + p + k, 2);
- l = parseInt(D + C + B + A + z + y + x + l, 2);
- m = parseInt(O + N + M + L + K + J + I + m, 2);
- E = parseInt(V + U + T + S + R + Q + P + E, 2);
- F = parseInt(ca + ba + aa + Z + Y + X + W + F, 2);
- G = parseInt(ja + ia + ha + ga + fa + ea + da + G, 2);
- H = parseInt(a + pa + oa + na + ma + la + ka + H, 2);
- return "matrix_draw_bmp(bytearray([" + b + ", " + k + ", " + l + ", " + m + ", " + E + ", " + F + ", " + G + ", " + H + "]))\nmatrix_show()\n"
- }
- ;
- Blockly.Python.labplus_set_i2c_led_matrix = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- Blockly.Python.codeFunctions_.init_bluebit8x8Led = "matrix_init()";
- var b = Blockly.Python.valueToCode(a, "row1", Blockly.Python.ORDER_ATOMIC)
- , c = Blockly.Python.valueToCode(a, "row2", Blockly.Python.ORDER_ATOMIC)
- , d = Blockly.Python.valueToCode(a, "row3", Blockly.Python.ORDER_ATOMIC)
- , e = Blockly.Python.valueToCode(a, "row4", Blockly.Python.ORDER_ATOMIC)
- , f = Blockly.Python.valueToCode(a, "row5", Blockly.Python.ORDER_ATOMIC)
- , g = Blockly.Python.valueToCode(a, "row6", Blockly.Python.ORDER_ATOMIC)
- , h = Blockly.Python.valueToCode(a, "row7", Blockly.Python.ORDER_ATOMIC);
- a = Blockly.Python.valueToCode(a, "row8", Blockly.Python.ORDER_ATOMIC);
- return "matrix_draw_bmp(bytearray([" + b + ", " + c + ", " + d + ", " + e + ", " + f + ", " + g + ", " + h + ", " + a + "]))\nmatrix_show()\n"
- }
- ;
- Blockly.Python.labplus_i2c_led_matrix_side = function (a) {
- return [a.getFieldValue("side"), Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.labplus_set_i2c_led_matrix_draw_pixel = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- Blockly.Python.codeFunctions_.init_bluebit8x8Led = "matrix_init()";
- var b = Blockly.Python.valueToCode(a, "x", Blockly.Python.ORDER_ATOMIC);
- a = Blockly.Python.valueToCode(a, "y", Blockly.Python.ORDER_ATOMIC);
- return "matrix_draw_pixel(" + b + ", " + a + ")\n"
- }
- ;
- Blockly.Python.labplus_set_i2c_led_matrix_show = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- Blockly.Python.codeFunctions_.init_bluebit8x8Led = "matrix_init()";
- return "matrix_show()\n"
- }
- ;
- Blockly.Python.labplus_set_i2c_led_matrix_clear = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- Blockly.Python.codeFunctions_.init_bluebit8x8Led = "matrix_init()";
- return "matrix_clear()\n"
- }
- ;
- Blockly.Python.labplus_uart_mp3_play = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- return "mp3_play(" + Blockly.Python.valueToCode(a, "index", Blockly.Python.ORDER_ATOMIC) + ")\n"
- }
- ;
- Blockly.Python.labplus_uart_mp3_set_volume = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- return "mp3_volume(" + Blockly.Python.valueToCode(a, "vol", Blockly.Python.ORDER_ATOMIC) + ")\n"
- }
- ;
- Blockly.Python.labplus_uart_mp3_set_status = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- a = a.getFieldValue("status");
- var b = "";
- "PAUSE" == a && (b = "mp3_pause()\n");
- "STOP" == a && (b = "mp3_stop()\n");
- "PLAY" == a && (b = "mp3_start()\n");
- return b
- }
- ;
- Blockly.Python.labplus_uart_init = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "tx", Blockly.Python.ORDER_ATOMIC)
- , c = Blockly.Python.valueToCode(a, "rx", Blockly.Python.ORDER_ATOMIC);
- 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"
- }
- ;
- Blockly.Python.labplus_uart_any_msg_received = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["uart.any()", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.labplus_uart_read_msg = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["uart.read()", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.labplus_uart_write_msg = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return "uart.write(" + Blockly.Python.valueToCode(a, "msg0", Blockly.Python.ORDER_ATOMIC) + ")\n"
- }
- ;
- Blockly.Python.labplus_uart_set_oled = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "x1", Blockly.Python.ORDER_ATOMIC)
- , c = Blockly.Python.valueToCode(a, "y1", Blockly.Python.ORDER_ATOMIC)
- , d = Blockly.Python.valueToCode(a, "msg0", Blockly.Python.ORDER_ATOMIC);
- a = a.getFieldValue("font");
- return "uart.write('@' + str(int(" + b + ")) + ','+ str(int(" + c + ")) + '," + a + ":' + " + d + " + '\\r\\n')\n"
- }
- ;
- Blockly.Python.labplus_uart_set_lcd_clear = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "x1", Blockly.Python.ORDER_ATOMIC)
- , c = Blockly.Python.valueToCode(a, "y1", Blockly.Python.ORDER_ATOMIC)
- , d = Blockly.Python.valueToCode(a, "x2", Blockly.Python.ORDER_ATOMIC);
- a = Blockly.Python.valueToCode(a, "y2", Blockly.Python.ORDER_ATOMIC);
- return "uart.write('#0:' + str(int(" + b + ")) + ','+ str(int(" + c + ")) + ','+ str(int(" + d + ")) + ','+ str(int(" + a + ")) + '\\r\\n')\n"
- }
- ;
- Blockly.Python.labplus_uart_set_lcd_clear_all = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return "uart.write('#0:0,0,127,63\\r\\n')\n"
- }
- ;
- Blockly.Python.labplus_compitition_init_extended_pins = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- 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)";
- var b = a.getFieldValue("extendedPins");
- a = a.getFieldValue("PinMode");
- return "extIOInit(" + b + "," + a + ")\n"
- }
- ;
- Blockly.Python.labplus_set_extio_mode = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- var b = a.getFieldValue("mode0")
- , c = a.getFieldValue("mode1")
- , d = a.getFieldValue("mode2")
- , e = a.getFieldValue("mode3")
- , f = a.getFieldValue("mode4")
- , g = a.getFieldValue("mode5")
- , h = a.getFieldValue("mode6");
- return "extIO_mode(0b" + a.getFieldValue("mode7") + h + g + f + e + d + c + b + ")\n"
- }
- ;
- Blockly.Python.labplus_read_extended_pin = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- 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";
- return ["readExtendedIO(" + a.getFieldValue("extendedPins") + ")", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.labplus_write_extended_pin = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- 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";
- var b = a.getFieldValue("extendedPins");
- a = Blockly.Python.valueToCode(a, "PinValue", Blockly.Python.ORDER_ATOMIC);
- return "writeExtendedIO(" + b + "," + a + ")\n"
- }
- ;
- Blockly.Python.labplus_voice_record_on = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = a.getFieldValue("pinpair").split(" \x26 ");
- a = a.getFieldValue("func");
- var c = b[0]
- , b = b[1];
- 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"
- }
- ;
- Blockly.Python.labplus_voice_record_off = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- a = a.getFieldValue("pinpair").split(" \x26 ");
- return "pin" + a[0] + ".write_digital(0)\npin" + a[1] + ".write_digital(0)\n"
- }
- ;
- Blockly.Python.labplus_mapping = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "inputNum", Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.valueToCode(a, "outputNum", Blockly.Python.ORDER_ATOMIC);
- var c = Blockly.Python.valueToCode(a, "bMin", Blockly.Python.ORDER_ATOMIC)
- , d = Blockly.Python.valueToCode(a, "bMax", Blockly.Python.ORDER_ATOMIC)
- , e = Blockly.Python.valueToCode(a, "cMin", Blockly.Python.ORDER_ATOMIC);
- a = Blockly.Python.valueToCode(a, "cMax", Blockly.Python.ORDER_ATOMIC);
- 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";
- return ["numberMap(" + b + "," + c + "," + d + "," + e + "," + a + ")", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.microbit_ext2_set_servo = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- Blockly.Python.codeFunctions_.init_microbit_ext2 = "pca9685_init()";
- var b = a.getFieldValue("servo");
- a = Blockly.Python.valueToCode(a, "degree", Blockly.Python.ORDER_ATOMIC);
- return "pca9685_servo(" + b + ", " + a + ")\n"
- }
- ;
- Blockly.Python.microbit_ext2_set_motor = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- Blockly.Python.codeFunctions_.init_microbit_ext2 = "pca9685_init()";
- var b = a.getFieldValue("motor")
- , c = a.getFieldValue("dir");
- a = Blockly.Python.valueToCode(a, "spd", Blockly.Python.ORDER_ATOMIC);
- "1" == c && (a = "-" + a);
- return "pca9685_dc_motor(" + b + ", " + a + ")\n"
- }
- ;
- Blockly.Python.microbit_ext2_stop_motor = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- Blockly.Python.codeFunctions_.init_microbit_ext2 = "pca9685_init()";
- return "pca9685_dc_motor(" + a.getFieldValue("motor") + ", 0)\n"
- }
- ;
- Blockly.Python.microbit_ext2_set_pwm = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- Blockly.Python.codeFunctions_.init_microbit_ext2 = "pca9685_init()";
- var b = a.getFieldValue("channel");
- a = Blockly.Python.valueToCode(a, "pwm", Blockly.Python.ORDER_ATOMIC);
- return "pca9685_set_pwm(" + b + ", " + a + ")\n"
- }
- ;
- Blockly.Python.microbit_ext2_step_motor = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_bluebit = "from bluebit import *";
- Blockly.Python.codeFunctions_.init_microbit_ext2 = "pca9685_init()";
- var b = a.getFieldValue("motor")
- , c = a.getFieldValue("dir");
- a = Blockly.Python.valueToCode(a, "speed", Blockly.Python.ORDER_ATOMIC);
- return "pca9685_step_motor(" + b + ", " + c + ", " + a + ")\n"
- }
- ;
- Blockly.Python.microbit_ext2_sound_value = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["pin2.read_analog()", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.microbit_ext2_light_value = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["pin1.read_analog()", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.addReservedWords("music");
- var _microbit_music_default_pin = 0;
- Blockly.Python.microbit_music_play_built_in = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_music = "import music";
- var b = a.getFieldValue("melody")
- , c = "TRUE" == a.getFieldValue("wait") ? "True" : "False";
- a = "TRUE" == a.getFieldValue("loop") ? "True" : "False";
- return "music.play(music." + b + ", pin" + _microbit_music_default_pin + ", wait\x3d" + c + ", loop\x3d" + a + ")\n"
- }
- ;
- Blockly.Python.microbit_music_pitch = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_music = "import music";
- var b = Blockly.Python.valueToCode(a, "pitch", Blockly.Python.ORDER_ATOMIC);
- a = Blockly.Python.valueToCode(a, "duration", Blockly.Python.ORDER_ATOMIC);
- return "music.pitch(" + b + ", " + a + ", pin" + _microbit_music_default_pin + ")\n"
- }
- ;
- Blockly.Python.microbit_music_play_list_of_notes = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_music = "import music";
- var b = Blockly.Python.valueToCode(a, "notes", Blockly.Python.ORDER_ATOMIC)
- , c = "TRUE" == a.getFieldValue("wait") ? "True" : "False";
- a = "TRUE" == a.getFieldValue("loop") ? "True" : "False";
- return "music.play(" + b + ",pin" + _microbit_music_default_pin + ", wait\x3d" + c + ", loop\x3d" + a + ")\n"
- }
- ;
- Blockly.Python.microbit_music_reset = function (a) {
- Blockly.Python.definitions_.import_music = "import music";
- return "music.reset()\n"
- }
- ;
- Blockly.Python.microbit_music_stop = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- Blockly.Python.definitions_.import_music = "import music";
- return "music.stop(pin" + _microbit_music_default_pin + ")\n"
- }
- ;
- Blockly.Python.microbit_music_set_tempo = function (a) {
- Blockly.Python.definitions_.import_music = "import music";
- var b = Blockly.Python.valueToCode(a, "ticks", Blockly.Python.ORDER_ATOMIC);
- a = Blockly.Python.valueToCode(a, "bpm", Blockly.Python.ORDER_ATOMIC);
- return "music.set_tempo(ticks\x3d" + b + ", bpm\x3d" + a + ")\n"
- }
- ;
- Blockly.Python.microbit_music_get_tempo = function (a) {
- Blockly.Python.definitions_.import_music = "import music";
- return ["music.get_tempo()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_set_music_pin = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- _microbit_music_default_pin = Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC);
- return ""
- }
- ;
- Blockly.Python.addReservedWords("neopixel");
- Blockly.Python.microbit_neopixel_initialise = function (a) {
- Blockly.Python.definitions_.import_neopixel = "import neopixel";
- var b = a.getFieldValue("n");
- return ["neopixel.NeoPixel(" + a.getFieldValue("pin") + ", " + b + ")", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_neopixel_clear = function (a) {
- Blockly.Python.definitions_.import_neopixel = "import neopixel";
- return Blockly.Python.valueToCode(a, "neopixel_object", Blockly.Python.ORDER_MEMBER) + ".clear()\n"
- }
- ;
- Blockly.Python.microbit_neopixel_show = function (a) {
- Blockly.Python.definitions_.import_neopixel = "import neopixel";
- return Blockly.Python.valueToCode(a, "neopixel_object", Blockly.Python.ORDER_MEMBER) + ".show()\n"
- }
- ;
- Blockly.Python.microbit_neopixel_set = function (a) {
- Blockly.Python.definitions_.import_neopixel = "import neopixel";
- var b = Blockly.Python.valueToCode(a, "pixel", Blockly.Python.ORDER_ATOMIC)
- , c = Blockly.Python.valueToCode(a, "red", Blockly.Python.ORDER_ATOMIC)
- , d = Blockly.Python.valueToCode(a, "green", Blockly.Python.ORDER_ATOMIC)
- , e = Blockly.Python.valueToCode(a, "blue", Blockly.Python.ORDER_ATOMIC);
- return Blockly.Python.valueToCode(a, "neopixel_object", Blockly.Python.ORDER_MEMBER) + "[" + b + "] \x3d (" + c + ", " + d + ", " + e + ")\n"
- }
- ;
- Blockly.Python.addReservedWords("microbit");
- Blockly.Python.microbit_pin_touched = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["pin" + a.getFieldValue("pin") + ".is_touched()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_pin_was_pressed = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["pin" + a.getFieldValue("pin") + ".was_pressed()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_pin_get_presses = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["pin" + a.getFieldValue("pin") + ".get_presses()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_pin_read_analog = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["pin" + Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC) + ".read_analog()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_pin_write_analog = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "output", Blockly.Python.ORDER_ATOMIC);
- return "pin" + Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC) + ".write_analog(" + b + ")\n"
- }
- ;
- Blockly.Python.microbit_pin_read_digital = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- return ["pin" + Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC) + ".read_digital()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_pin_write_digital = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "output", Blockly.Python.ORDER_ATOMIC);
- 0 < b ? b = 1 : 0 > b && (b = 0);
- return "pin" + Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC) + ".write_digital(" + b + ")\n"
- }
- ;
- Blockly.Python.microbit_pin_set_servo = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = Blockly.Python.valueToCode(a, "degree", Blockly.Python.ORDER_ATOMIC);
- a = Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_["init_servo" + a] = "pin" + a + ".set_analog_period(20)";
- return "pin" + a + ".write_analog((0.5 + " + b + "/90)*1023/20)\n"
- }
- ;
- Blockly.Python.microbit_pin_set_mode = function (a) {
- Blockly.Python.definitions_.import_microbit = "from microbit import *";
- var b = a.getFieldValue("mode");
- a = Blockly.Python.valueToCode(a, "pin", Blockly.Python.ORDER_ATOMIC);
- switch (b) {
- case "UP":
- return "pin" + a + ".set_pull(pin" + a + ".PULL_UP)\n";
- case "DOWN":
- return "pin" + a + ".set_pull(pin" + a + ".PULL_DOWN)\n";
- default:
- return "pin" + a + ".set_pull(pin" + a + ".NO_PULL)\n"
- }
- }
- ;
- Blockly.Python.addReservedWords("radio");
- Blockly.Python.microbit_radio_on = function (a) {
- Blockly.Python.definitions_.import_radio = "import radio";
- return "radio.on()\n"
- }
- ;
- Blockly.Python.microbit_radio_off = function (a) {
- Blockly.Python.definitions_.import_radio = "import radio";
- return "radio.off()\n"
- }
- ;
- Blockly.Python.microbit_radio_config = function (a) {
- Blockly.Python.definitions_.import_radio = "import radio";
- var b = a.getFieldValue("length")
- , c = a.getFieldValue("queue")
- , d = a.getFieldValue("channel")
- , e = a.getFieldValue("power")
- , f = a.getFieldValue("address")
- , g = a.getFieldValue("group");
- a = a.getFieldValue("data_rate");
- 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"
- }
- ;
- Blockly.Python.microbit_radio_reset = function (a) {
- Blockly.Python.definitions_.import_radio = "import radio";
- return "radio.reset()\n"
- }
- ;
- Blockly.Python.microbit_radio_send_string = function (a) {
- Blockly.Python.definitions_.import_radio = "import radio";
- return "radio.send(" + Blockly.Python.valueToCode(a, "message", Blockly.Python.ORDER_ATOMIC) + ")\n"
- }
- ;
- Blockly.Python.microbit_radio_send_bytes = function (a) {
- Blockly.Python.definitions_.import_radio = "import radio";
- return "radio.send_bytes(" + Blockly.Python.valueToCode(a, "message", Blockly.Python.ORDER_ATOMIC) + ")\n"
- }
- ;
- Blockly.Python.microbit_radio_receive = function (a) {
- Blockly.Python.definitions_.import_radio = "import radio";
- return ["radio.receive()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.microbit_radio_receive_bytes = function (a) {
- Blockly.Python.definitions_.import_radio = "import radio";
- return ["radio.receive_bytes()", Blockly.Python.ORDER_MEMBER]
- }
- ;
- Blockly.Python.addReservedWords("speech");
- Blockly.Python.microbit_speech_say = function (a) {
- Blockly.Python.definitions_.import_speech = "import speech";
- return "speech.say(" + Blockly.Python.valueToCode(a, "english", Blockly.Python.ORDER_ATOMIC) + ")\n"
- }
- ;
- Blockly.Python.microbit_speech_pronounce = function (a) {
- Blockly.Python.definitions_.import_speech = "import speech";
- return "speech.pronounce(" + Blockly.Python.valueToCode(a, "phonemes", Blockly.Python.ORDER_ATOMIC) + ")\n"
- }
- ;
- Blockly.Python.microbit_speech_sing = function (a) {
- Blockly.Python.definitions_.import_speech = "import speech";
- return "speech.sing(" + Blockly.Python.valueToCode(a, "song", Blockly.Python.ORDER_ATOMIC) + ")\n"
- }
- ;
- Blockly.Python.microbit_mu_led_level = function (a) {
- return [a.getFieldValue("level"), Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.microbit_mu_vision_type = function (a) {
- return [a.getFieldValue("vision_type"), Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.microbit_mu_vision_type_multi = function (a) {
- var b = "TRUE" == a.getFieldValue("type1")
- , c = "TRUE" == a.getFieldValue("type2")
- , d = "TRUE" == a.getFieldValue("type3")
- , e = "TRUE" == a.getFieldValue("type4")
- , f = "TRUE" == a.getFieldValue("type5")
- , g = "TRUE" == a.getFieldValue("type6")
- , h = "TRUE" == a.getFieldValue("type7");
- if ("TRUE" == a.getFieldValue("type8"))
- return ["VISION_ALL", Blockly.Python.ORDER_ATOMIC];
- a = "";
- b && (a += " | VISION_COLOR_DETECT");
- c && (a += " | VISION_COLOR_RECOGNITION");
- d && (a += " | VISION_BALL_DETECT");
- e && (a += " | VISION_BODY_DETECT");
- f && (a += " | VISION_SHAPE_CARD_DETECT");
- g && (a += " | VISION_TRAFFIC_CARD_DETECT");
- h && (a += " | VISION_NUM_CARD_DETECT");
- "" != a && (a = a.substring(3));
- return [a, Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.microbit_mu_init = function (a) {
- Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
- a = a.getFieldValue("address");
- Blockly.Python.codeFunctions_.define_mu = "mu \x3d MuVisionSensor(" + a + ")";
- return "mu.begin()\n"
- }
- ;
- Blockly.Python.microbit_mu_begin = function (a) {
- Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
- return "mu.VisionBegin(" + Blockly.Python.valueToCode(a, "vision_type", Blockly.Python.ORDER_ATOMIC) + ")\n"
- }
- ;
- Blockly.Python.microbit_mu_set_level = function (a) {
- Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
- var b = Blockly.Python.valueToCode(a, "vision_type", Blockly.Python.ORDER_ATOMIC);
- a = a.getFieldValue("level");
- return "mu.VisionSetLevel(" + b + ", " + a + ")\n"
- }
- ;
- Blockly.Python.microbit_mu_get_level = function (a) {
- Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
- return ["mu.VisionSetLevel(" + Blockly.Python.valueToCode(a, "vision_type", Blockly.Python.ORDER_ATOMIC) + ")", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.microbit_mu_set_fps_mode = function (a) {
- Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
- return "mu.CameraSetFPS(" + a.getFieldValue("mode") + ")\n"
- }
- ;
- Blockly.Python.microbit_mu_get_fps_mode = function (a) {
- Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
- return ["mu.CameraGetFPS()", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.microbit_mu_set_awb_mode = function (a) {
- Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
- return "mu.CameraSetAwb(" + a.getFieldValue("mode") + ")\n"
- }
- ;
- Blockly.Python.microbit_mu_get_awb_mode = function (a) {
- Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
- return ["mu.CameraGetAwb()", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.microbit_mu_set_zoom_mode = function (a) {
- Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
- return "mu.CameraSetZoom(" + a.getFieldValue("mode") + ")\n"
- }
- ;
- Blockly.Python.microbit_mu_get_zoom_mode = function (a) {
- Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
- return ["mu.CameraGetZoom()", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.microbit_mu_set_default = function (a) {
- Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
- return "mu.SensorSetDefault()\n"
- }
- ;
- Blockly.Python.microbit_mu_restart = function (a) {
- Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
- return "mu.SensorSetRestart()\n"
- }
- ;
- Blockly.Python.microbit_mu_led_set_color = function (a) {
- Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
- var b = a.getFieldValue("led")
- , c = a.getFieldValue("detected_color")
- , d = a.getFieldValue("undetected_color");
- a = Blockly.Python.valueToCode(a, "level", Blockly.Python.ORDER_ATOMIC);
- return "mu.LedSetColor(" + b + ", " + c + ", " + d + ", " + a + ")\n"
- }
- ;
- Blockly.Python.microbit_mu_get_value = function (a) {
- Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
- var b = Blockly.Python.valueToCode(a, "vision_type", Blockly.Python.ORDER_ATOMIC);
- a = a.getFieldValue("object_inf");
- return ["mu.GetValue(" + b + ", " + a + ")", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.microbit_mu_ls_type = function (a) {
- return [a.getFieldValue("ls_type"), Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.microbit_mu_ls_begin = function (a) {
- Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
- return "mu.LsBegin(" + Blockly.Python.valueToCode(a, "ls_type", Blockly.Python.ORDER_ATOMIC) + ")\n"
- }
- ;
- Blockly.Python.microbit_mu_ls_end = function (a) {
- Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
- return "mu.LsEnd(" + Blockly.Python.valueToCode(a, "ls_type", Blockly.Python.ORDER_ATOMIC) + ")\n"
- }
- ;
- Blockly.Python.microbit_mu_ls_set_sensitivity = function (a) {
- Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
- return "mu.LsSetSensitivity(" + a.getFieldValue("sensitivity") + ")\n"
- }
- ;
- Blockly.Python.microbit_mu_ls_wb_enable = function (a) {
- Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
- return "mu.LsWhiteBalanceEnable()\n"
- }
- ;
- Blockly.Python.microbit_mu_ls_read_proximity = function (a) {
- Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
- return ["mu.LsReadProximity()", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.microbit_mu_ls_read_ambient_light = function (a) {
- Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
- return ["mu.LsReadAmbientLight()", Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.microbit_mu_ls_read_color = function (a) {
- Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
- a = a.getFieldValue("color_t");
- switch (a) {
- case "LsRawColorRed":
- case "LsRawColorGreen":
- case "LsRawColorBlue":
- a = "mu.LsReadRawColor(" + a + ")";
- break;
- default:
- a = "mu.LsReadColor(" + a + ")"
- }
- return [a, Blockly.Python.ORDER_ATOMIC]
- }
- ;
- Blockly.Python.microbit_mu_ls_read_gesture = function (a) {
- Blockly.Python.definitions_.import_muvisionsensor = "from MuVisionSensor import *";
- return ["mu.LsReadGesture()", Blockly.Python.ORDER_ATOMIC]
- }
- ;
|