123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569 |
- '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_time = "import time";
- Blockly.Python.definitions_.import_random = "import random";
- // Blockly.Python.codeFunctions_.random_seed = "random.seed(time.ticks_cpu())";
- 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_QMI8658 = 'import QMI8658';
- Blockly.Python.addVariable('i2c', 'i2c = I2C(scl=Pin(17), sda=Pin(16))', true);
- Blockly.Python.addVariable('accel', 'accel = QMI8658.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_QMI8658 = 'import QMI8658';
- Blockly.Python.addVariable('i2c', 'i2c = I2C(scl=Pin(17), sda=Pin(16))', true);
- Blockly.Python.addVariable('accel', 'accel = QMI8658.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]
- }
- ;
|