extension_module.dev.js 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. "use strict";
  2. // Serial
  3. Blockly.Blocks['serial_write_data'] = {
  4. init: function init() {
  5. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/uart_serial_send_header.png", 110, 35, {
  6. alt: "*",
  7. flipRtl: "FALSE"
  8. }));
  9. this.appendDummyInput().appendField(Blockly.Msg.serialcomm_write_on).appendField(new Blockly.FieldDropdown([[Blockly.Msg.serialcomm_write_on_iot, "iot"], [Blockly.Msg.serialcomm_write_on_ai, "ai"]]), "type");
  10. this.appendDummyInput().appendField(Blockly.Msg.serialcomm_write_title_text);
  11. this.appendDummyInput().appendField(Blockly.Msg.serialcomm_write_data_bps_title).appendField(new Blockly.FieldNumber(115200, 0, 500000, 1), "uart_bps").appendField(Blockly.Msg.serialcomm_write_data_bps_attrib);
  12. /*this.appendValueInput("first_input")
  13. .setCheck(null)
  14. .appendField("Field 0");*/
  15. this.itemCount_ = 1;
  16. this.updateShape_();
  17. this.setMutator(new Blockly.Mutator(['serial_write_data_create_with_item']));
  18. this.setColour("#22b845");
  19. this.setPreviousStatement(true, null);
  20. this.setNextStatement(true, null);
  21. var thisBlock = this;
  22. this.setTooltip(function () {
  23. var mode = thisBlock.getFieldValue('type');
  24. var TOOLTIPS = {
  25. 'iot': Blockly.Msg.Serial_Write_Data_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_iot),
  26. 'ai': Blockly.Msg.Serial_Write_Data_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_ai)
  27. };
  28. return TOOLTIPS[mode];
  29. });
  30. this.setHelpUrl("");
  31. },
  32. mutationToDom: function mutationToDom() {
  33. var container = document.createElement('mutation');
  34. container.setAttribute('items', this.itemCount_);
  35. return container;
  36. },
  37. domToMutation: function domToMutation(xmlElement) {
  38. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  39. this.updateShape_();
  40. },
  41. decompose: function decompose(workspace) {
  42. var containerBlock = workspace.newBlock('serial_write_data_create_with_container');
  43. containerBlock.initSvg();
  44. var connection = containerBlock.getInput('STACK').connection;
  45. for (var i = 0; i < this.itemCount_; i++) {
  46. var itemBlock = workspace.newBlock('serial_write_data_create_with_item');
  47. itemBlock.initSvg();
  48. connection.connect(itemBlock.previousConnection);
  49. connection = itemBlock.nextConnection;
  50. }
  51. return containerBlock;
  52. },
  53. compose: function compose(containerBlock) {
  54. var itemBlock = containerBlock.getInputTargetBlock('STACK'); // Count number of inputs.
  55. var connections = [];
  56. while (itemBlock) {
  57. connections.push(itemBlock.valueConnection_);
  58. itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock();
  59. } // Disconnect any children that don't belong.
  60. for (var i = 0; i < this.itemCount_; i++) {
  61. var connection = this.getInput('ADD' + i).connection.targetConnection;
  62. if (connection && connections.indexOf(connection) == -1) {
  63. connection.disconnect();
  64. }
  65. }
  66. this.itemCount_ = connections.length;
  67. this.updateShape_(); // Reconnect any child blocks.
  68. for (var i = 0; i < this.itemCount_; i++) {
  69. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  70. }
  71. },
  72. saveConnections: function saveConnections(containerBlock) {
  73. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  74. var i = 0;
  75. while (itemBlock) {
  76. var input = this.getInput('ADD' + i);
  77. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  78. i++;
  79. itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock();
  80. }
  81. },
  82. updateShape_: function updateShape_() {
  83. for (var i = 0; i < this.itemCount_; i++) {
  84. if (!this.getInput('ADD' + i)) {
  85. var input = this.appendValueInput('ADD' + i);
  86. input.appendField(Blockly.Msg.serialcomm_write_item_first + i + Blockly.Msg.serialcomm_write_item_last); //input.appendField("資料 " + (i + 1) + ":");
  87. //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
  88. // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
  89. }
  90. }
  91. while (this.getInput('ADD' + i)) {
  92. this.removeInput('ADD' + i);
  93. i++;
  94. }
  95. }
  96. };
  97. Blockly.Blocks['serial_write_data_create_with_container'] = {
  98. init: function init() {
  99. this.setColour("#22b845");
  100. this.appendDummyInput().appendField(Blockly.Msg.serialcomm_write_item);
  101. this.appendStatementInput('STACK');
  102. this.setTooltip('');
  103. this.contextMenu = false;
  104. }
  105. };
  106. Blockly.Blocks['serial_write_data_create_with_item'] = {
  107. init: function init() {
  108. this.setColour("#22b845");
  109. this.appendDummyInput().appendField(Blockly.Msg.serialcomm_write_data);
  110. this.setPreviousStatement(true);
  111. this.setNextStatement(true);
  112. this.setTooltip('');
  113. this.contextMenu = false;
  114. }
  115. };
  116. Blockly.Python['serial_write_data'] = function (block) {
  117. var api = block.getFieldValue('t_api');
  118. var number_uart_bps = block.getFieldValue('uart_bps');
  119. var dropdown_type = block.getFieldValue('type');
  120. if (dropdown_type == "ai") {
  121. Blockly.Python.definitions_['import_machine_uart'] = 'import machine, time\n' + 'from fpioa_manager import fm\n' + '\n';
  122. Blockly.Python.addVariable('uart_data', 'uart_data = ""', true);
  123. Blockly.Python.definitions_['uart_setup'] = '' + 'fm.register(13,fm.fpioa.UART2_TX)\n' + 'fm.register(14,fm.fpioa.UART2_RX)\n' + 'ai_uart = machine.UART(machine.UART.UART2,' + number_uart_bps + ',bits=8,parity=None,stop=1)\n' + ''; // var key = Blockly.Arduino.valueToCode(block, 'KEY', Blockly.Arduino.ORDER_ATOMIC).replace(/\"/g, '') || "";
  124. item_value = '';
  125. var ai_uart_data = "";
  126. for (var n = 0; n < this.itemCount_; n++) {
  127. item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
  128. ai_uart_data += 'str(' + item_value + ') + \"|\" + ';
  129. }
  130. var ai_uart_data_final = ai_uart_data + "";
  131. var code = '' + 'uart_data = ' + ai_uart_data_final.slice(0, -8) + '\n' + 'ai_uart.write(b"SOF|" + str(uart_data) + "|\\r\\n")\n' + 'time.sleep_ms(20)\n' + '';
  132. } else if (dropdown_type == "iot") {
  133. Blockly.Python.definitions_['import_machine_uart'] = 'from machine import UART, Pin\n' + 'import time\n' + '\n';
  134. Blockly.Python.definitions_['uart_setup'] = '' + '_iot_uart = UART(2, tx=17, rx=16)\n' + '_iot_uart.init(115200, bits=8, parity=None, stop=1)\n' + '';
  135. Blockly.Python.addVariable('_iot_uart_data', '_iot_uart_data = ""', true);
  136. item_value = '';
  137. var iot_uart_data = "";
  138. for (var n = 0; n < this.itemCount_; n++) {
  139. item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
  140. iot_uart_data += 'str(' + item_value + ') + \"|\" + ';
  141. }
  142. var iot_uart_data_final = iot_uart_data + "";
  143. var code = '' + '_iot_uart_data = ' + iot_uart_data_final.slice(0, -8) + '\n' + '_iot_uart.write("SOF|" + str(_iot_uart_data) + "|\\r\\n")\n' + 'time.sleep_ms(20)\n' + '';
  144. }
  145. return code;
  146. };
  147. Blockly.Blocks['serial_send_data_to_microbit'] = {
  148. init: function init() {
  149. var types = getLocalStorage("type");
  150. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/microbit_data_send_header.png", 90, 70, {
  151. alt: "*",
  152. flipRtl: "FALSE"
  153. }));
  154. types == 1 ? this.appendValueInput("NAME").appendField(Blockly.Msg.serial_send_data_on).setCheck(null).appendField(new Blockly.FieldDropdown([["A.I.", "ai"], ["IoT", "iot"]]), "module_type_microbit").appendField(Blockly.Msg.serial_send_data_on_send_data) : this.appendValueInput("NAME").appendField(Blockly.Msg.serial_send_data_on).setCheck(null).appendField(new Blockly.FieldDropdown([["IoT", "iot"], ["A.I.", "ai"]]), "module_type_microbit").appendField(Blockly.Msg.serial_send_data_on_send_data);
  155. this.setInputsInline(false);
  156. this.setColour("#22b845");
  157. this.setPreviousStatement(true, null);
  158. this.setNextStatement(true, null);
  159. var thisBlock = this;
  160. this.setTooltip(function () {
  161. var mode = thisBlock.getFieldValue('module_type_microbit');
  162. var TOOLTIPS = {
  163. 'iot': Blockly.Msg.Serial_Send_Data_To_Microbit_TOOLTIP.replace('%1', "IoT"),
  164. 'ai': Blockly.Msg.Serial_Send_Data_To_Microbit_TOOLTIP.replace('%1', "A.I.")
  165. };
  166. return TOOLTIPS[mode];
  167. });
  168. this.setHelpUrl("");
  169. }
  170. };
  171. Blockly.Python['serial_send_data_to_microbit'] = function (block) {
  172. var dropdown_module_type = block.getFieldValue('module_type_microbit');
  173. var value_name = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC) || ""; // TODO: Assemble Python into code variable.
  174. var code = '';
  175. if (dropdown_module_type == 'iot') {
  176. Blockly.Python.definitions_['import_microbit_iot_uart'] = "from machine import UART, Pin \nimport time\n";
  177. var iot = "\n_iot_microbit_uart = UART(2, tx=17, rx=16)\n_iot_microbit_uart.init(115200, bits=8, parity=None, stop=1)";
  178. Blockly.Python.addVariable('_iot_microbit_uart', iot, true);
  179. code = '\n' + "_iot_microbit_uart_data =" + value_name + "\n" + '_iot_microbit_uart.write(str(_iot_uart_data))\n' + "time.sleep_ms(20)\n" + '';
  180. } else {
  181. Blockly.Python.definitions_['import_microbit_ai_uart'] = "import machine, time \nfrom fpioa_manager import fm\n";
  182. var ai = "\nfm.register(13,fm.fpioa.UART2_TX)\nfm.register(14,fm.fpioa.UART2_RX)\nai_microbit_uart = machine.UART(machine.UART.UART2,115200,bits=8,parity=None,stop=1)";
  183. Blockly.Python.addVariable('ai_microbit_uart', ai, true);
  184. code = '\n' + "uart_microbit_data =" + value_name + "\n" + 'ai_microbit_uart.write(b""+str(uart_microbit_data)+"")\n' + "time.sleep_ms(20)\n" + '';
  185. }
  186. return code;
  187. };
  188. /*
  189. ____
  190. / ___| ___ _ ____ _____
  191. \___ \ / _ \ '__\ \ / / _ \
  192. ___) | __/ | \ V / (_) |
  193. |____/ \___|_| \_/ \___/
  194. */
  195. var SERVO_BLOCK_COLOR = "#5bb2d6";
  196. var ai_servo_timer = ["Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM", "Timer.TIMER0, Timer.CHANNEL1, mode=Timer.MODE_PWM", "Timer.TIMER0, Timer.CHANNEL2, mode=Timer.MODE_PWM", "Timer.TIMER0, Timer.CHANNEL3, mode=Timer.MODE_PWM", "Timer.TIMER1, Timer.CHANNEL0, mode=Timer.MODE_PWM", "Timer.TIMER1, Timer.CHANNEL1, mode=Timer.MODE_PWM", "Timer.TIMER1, Timer.CHANNEL2, mode=Timer.MODE_PWM", "Timer.TIMER1, Timer.CHANNEL3, mode=Timer.MODE_PWM", "Timer.TIMER2, Timer.CHANNEL0, mode=Timer.MODE_PWM", "Timer.TIMER2, Timer.CHANNEL1, mode=Timer.MODE_PWM", "Timer.TIMER2, Timer.CHANNEL2, mode=Timer.MODE_PWM", "Timer.TIMER2, Timer.CHANNEL3, mode=Timer.MODE_PWM"];
  197. var ai_servo_pin = ['0', '1', '2', '3', '13', '14', '15', '17', '21', '22', '23', '24'];
  198. Blockly.Blocks['extension_servo_setup_on_ai'] = {
  199. init: function init() {
  200. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/esp32_servo_setup.png", 50, 40, {
  201. alt: "*",
  202. flipRtl: "FALSE"
  203. }));
  204. this.appendDummyInput().appendField(Blockly.Msg.servo_setup_ai);
  205. this.setInputsInline(false);
  206. this.setPreviousStatement(true, null);
  207. this.setNextStatement(true, null);
  208. this.setColour(servoCloor);
  209. this.setTooltip("");
  210. this.setHelpUrl("");
  211. }
  212. };
  213. Blockly.Python['extension_servo_setup_on_ai'] = function (block) {
  214. Blockly.Python.definitions_.extension_servo_import = "from machine import Timer,PWM";
  215. Blockly.Python.addSetup("extension_servo_setup", '' + 'def _ai_servo_write(servo,angle):\n' + '\tangle = angle - 90\n' + '\tleftSpan = 180 - 0\n' + '\trightSpan = 175 - 5\n' + '\tvalueScaled = float(angle - 0) / float(leftSpan)\n' + '\tservo.duty(((10 + (valueScaled * rightSpan))+90)/180*10+2.5)\n' + '');
  216. var code = '\n';
  217. return code;
  218. };
  219. Blockly.Blocks['extension_servo_write_on_ai'] = {
  220. init: function init() {
  221. this.appendDummyInput().appendField(Blockly.Msg.servo_set_gpio_ai).appendField(new Blockly.FieldDropdown([["0", "0"], ["1", "1"], ["2", "2"], ["3", "3"], ["13", "4"], ["14", "5"], ["15", "6"], ["17", "7"], ["21", "8"], ["22", "9"], ["23", "10"], ["24", "11"]]), "gpio");
  222. this.appendValueInput("degree").setCheck(null).appendField(Blockly.Msg.servo_rotate_to_ai);
  223. this.appendDummyInput().appendField(Blockly.Msg.servo_degree_ai);
  224. this.setInputsInline(true);
  225. this.setPreviousStatement(true, null);
  226. this.setNextStatement(true, null);
  227. this.setColour(servoCloor);
  228. this.setTooltip("");
  229. this.setHelpUrl("");
  230. }
  231. };
  232. Blockly.Python['extension_servo_write_on_ai'] = function (block) {
  233. var value_degree = Blockly.Python.valueToCode(block, 'degree', Blockly.Python.ORDER_ATOMIC);
  234. var value_gpio = block.getFieldValue('gpio');
  235. Blockly.Python.addSetup("extension_servo_write_ai_" + value_gpio, '' + '_servo_tim1 = Timer(' + ai_servo_timer[value_gpio] + ')\n' + '_servo_gpio' + ai_servo_pin[value_gpio] + ' = PWM(_servo_tim1, freq=50, duty=0, pin=' + ai_servo_pin[value_gpio] + ')\n' + '');
  236. var real_servo_degree = value_degree;
  237. var code = '' + '_ai_servo_write(_servo_gpio' + ai_servo_pin[value_gpio] + ',' + real_servo_degree + ')\n' + '';
  238. return code;
  239. };
  240. var servoCloor = "#386dc8";
  241. Blockly.Blocks['esp32_main_controller_servo_setup'] = {
  242. init: function init() {
  243. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/esp32_servo_setup.png", 50, 40, {
  244. alt: "*",
  245. flipRtl: "FALSE"
  246. }));
  247. this.appendDummyInput().appendField(Blockly.Msg.servo_setup);
  248. this.setPreviousStatement(true, null);
  249. this.setNextStatement(true, null);
  250. this.setColour(servoCloor);
  251. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Servo_Setup_TOOLTIP);
  252. this.setHelpUrl("");
  253. }
  254. };
  255. Blockly.Python['esp32_main_controller_servo_setup'] = function (block) {
  256. // TODO: Assemble Python into code variable.
  257. Blockly.Python.definitions_['definition_servo_setup'] = '' + 'from machine import Pin, PWM\n' + '';
  258. var code = '\n';
  259. return code;
  260. };
  261. Blockly.Blocks['esp32_main_controller_servo_set'] = {
  262. init: function init() {
  263. this.appendDummyInput().appendField(Blockly.Msg.servo_set_gpio);
  264. this.appendValueInput("pin_select").setCheck(null);
  265. this.appendDummyInput().appendField(Blockly.Msg.servo_rotate_to);
  266. this.appendValueInput("degree").setCheck(null);
  267. this.appendDummyInput().appendField(Blockly.Msg.servo_degree);
  268. this.setPreviousStatement(true, null);
  269. this.setNextStatement(true, null);
  270. this.setColour(servoCloor);
  271. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Servo_Set_TOOLTIP);
  272. this.setHelpUrl("");
  273. }
  274. };
  275. Blockly.Python['esp32_main_controller_servo_set'] = function (block) {
  276. var value_pin_select = Blockly.Python.valueToCode(block, 'pin_select', Blockly.Python.ORDER_ATOMIC);
  277. var value_degree = Blockly.Python.valueToCode(block, 'degree', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable.
  278. Blockly.Python.addVariable('S' + value_pin_select, 'S' + value_pin_select + ' = PWM(Pin(' + value_pin_select + '), freq=50, duty=0)', true);
  279. Blockly.Python.definitions_['definition_servo_set'] = '' + 'def Servo(servo,angle):\n' + ' angle = angle - 90\n' + ' servo.duty(int(((angle+90)*2/180+0.5)/20*1023))\n' + '';
  280. var code = '' + 'Servo(S' + value_pin_select + ',' + value_degree + ')\n' + '';
  281. return code;
  282. };
  283. /*
  284. __ __ _
  285. | \/ | ___ | |_ ___ _ __
  286. | |\/| |/ _ \| __/ _ \| '__|
  287. | | | | (_) | || (_) | |
  288. |_| |_|\___/ \__\___/|_|
  289. */
  290. var MOTOR_BLOCK_COLOR = SERVO_BLOCK_COLOR;
  291. Blockly.Blocks['esp32_motor_setup'] = {
  292. init: function init() {
  293. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/motor_setup.png", 50, 40, {
  294. alt: "*",
  295. flipRtl: "FALSE"
  296. }));
  297. this.appendDummyInput().appendField(Blockly.Msg.x_motor_set_motor);
  298. this.setInputsInline(false);
  299. this.setPreviousStatement(true, null);
  300. this.setNextStatement(true, null);
  301. this.setColour(MOTOR_BLOCK_COLOR);
  302. this.setTooltip(Blockly.Msg.Esp32_Motor_Setup_TOOLTIP);
  303. this.setHelpUrl("");
  304. }
  305. };
  306. Blockly.Python['esp32_motor_setup'] = function (block) {
  307. // TODO: Assemble Python into code variable.
  308. Blockly.Python.definitions_['esp32_controller_set_motor'] = '' + 'import pca9685, motor\n' + '\n' + '_iot_motor_i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21))\n' + '_iot_motor = motor.DCMotors(_iot_motor_i2c)';
  309. var code = '';
  310. return code;
  311. };
  312. Blockly.Blocks['esp32_motor_run'] = {
  313. init: function init() {
  314. this.appendDummyInput().appendField(Blockly.Msg.x_motor_set_motor_turn).appendField(new Blockly.FieldDropdown([["A", "0"], ["B", "1"], ["C", "3"], ["D", "2"]]), "motor_type").appendField(Blockly.Msg.x_motor_set_motor_speed);
  315. this.appendValueInput("speed").setCheck(null);
  316. this.appendDummyInput().appendField(Blockly.Msg.x_motor_exec).appendField(new Blockly.FieldDropdown([[Blockly.Msg.x_motor_cw, "pos"], [Blockly.Msg.x_motor_acw, "neg"]]), "direction").appendField(Blockly.Msg.x_motor_set_motor_turns);
  317. this.setInputsInline(true);
  318. this.setPreviousStatement(true, null);
  319. this.setNextStatement(true, null);
  320. this.setColour(MOTOR_BLOCK_COLOR);
  321. this.setTooltip(Blockly.Msg.Esp32_Motor_Run_TOOLTIP);
  322. this.setHelpUrl("");
  323. }
  324. };
  325. Blockly.Python['esp32_motor_run'] = function (block) {
  326. var dropdown_motor_type = block.getFieldValue('motor_type');
  327. var value_speed = Blockly.Python.valueToCode(block, 'speed', Blockly.Python.ORDER_ATOMIC);
  328. var dropdown_direction = block.getFieldValue('direction');
  329. if (dropdown_direction == "pos") {
  330. value_speed = value_speed;
  331. } else if (dropdown_direction == "neg") {
  332. value_speed = -value_speed;
  333. }
  334. var code = '_iot_motor.speed(' + dropdown_motor_type + ',(' + value_speed + '*16))\n';
  335. return code;
  336. };
  337. Blockly.Blocks['ai_motor_setup'] = {
  338. init: function init() {
  339. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/motor_setup.png", 50, 40, {
  340. alt: "*",
  341. flipRtl: "FALSE"
  342. }));
  343. this.appendDummyInput().appendField(Blockly.Msg.x_motor_set_motor_ai);
  344. this.setInputsInline(false);
  345. this.setPreviousStatement(true, null);
  346. this.setNextStatement(true, null);
  347. this.setColour(MOTOR_BLOCK_COLOR);
  348. this.setTooltip("");
  349. this.setHelpUrl("");
  350. }
  351. };
  352. Blockly.Python['ai_motor_setup'] = function (block) {
  353. // TODO: Assemble Python into code variable.
  354. Blockly.Python.definitions_['ai_controller_set_motor'] = '' + 'import pca9685, motor\n' + 'from machine import I2C\n' + '\n' + '_ai_motor_scl = 30\n' + '_ai_motor_sda = 29\n' + '\n' + '_ai_motor_i2c = I2C(I2C.I2C0, freq=100000, scl=_ai_motor_scl, sda=_ai_motor_sda)\n' + '_ai_motor = motor.DCMotors(_ai_motor_i2c)\n' + '';
  355. var code = '';
  356. return code;
  357. };
  358. Blockly.Blocks['ai_motor_run'] = {
  359. init: function init() {
  360. this.appendDummyInput().appendField(Blockly.Msg.x_motor_set_motor_turn_ai).appendField(new Blockly.FieldDropdown([["A", "0"], ["B", "1"], ["C", "3"], ["D", "2"]]), "motor_type").appendField(Blockly.Msg.x_motor_set_motor_speed_ai);
  361. this.appendValueInput("speed").setCheck(null);
  362. this.appendDummyInput().appendField(Blockly.Msg.x_motor_exec_ai).appendField(new Blockly.FieldDropdown([[Blockly.Msg.x_motor_cw_ai, "pos"], [Blockly.Msg.x_motor_acw_ai, "neg"]]), "direction").appendField(Blockly.Msg.x_motor_set_motor_turns_ai);
  363. this.setInputsInline(true);
  364. this.setPreviousStatement(true, null);
  365. this.setNextStatement(true, null);
  366. this.setColour(MOTOR_BLOCK_COLOR);
  367. this.setTooltip("");
  368. this.setHelpUrl("");
  369. }
  370. };
  371. Blockly.Python['ai_motor_run'] = function (block) {
  372. var dropdown_motor_type = block.getFieldValue('motor_type');
  373. var value_speed = Blockly.Python.valueToCode(block, 'speed', Blockly.Python.ORDER_ATOMIC);
  374. var dropdown_direction = block.getFieldValue('direction');
  375. if (dropdown_direction == "pos") {
  376. value_speed = value_speed;
  377. } else if (dropdown_direction == "neg") {
  378. value_speed = -value_speed;
  379. }
  380. var code = '_ai_motor.speed(' + dropdown_motor_type + ', (' + value_speed + '*16))\n';
  381. return code;
  382. };
  383. /*
  384. _ _____ ____ __ __ _ _
  385. | | | ____| _ \ | \/ | __ _| |_ _ __(_)_ __
  386. | | | _| | | | | | |\/| |/ _` | __| '__| \ \/ /
  387. | |___| |___| |_| | | | | | (_| | |_| | | |> <
  388. |_____|_____|____/ |_| |_|\__,_|\__|_| |_/_/\_\
  389. */
  390. var AILEDCOLOR = "#e8795b";
  391. Blockly.Blocks['ai_led_matrix_setup'] = {
  392. init: function init() {
  393. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/led_setup.png", 50, 40, {
  394. alt: "*",
  395. flipRtl: "FALSE"
  396. }));
  397. this.appendDummyInput().appendField(Blockly.Msg.x_led_matrix_setup_ai);
  398. this.setInputsInline(false);
  399. this.setPreviousStatement(true, null);
  400. this.setNextStatement(true, null);
  401. this.setColour(AILEDCOLOR);
  402. this.setTooltip("");
  403. this.setHelpUrl("");
  404. }
  405. };
  406. Blockly.Python['ai_led_matrix_setup'] = function (block) {
  407. Blockly.Python.definitions_['ai_controller_led_setup'] = '' + 'from modules import ws2812\n' + '\n' + '_ai_led_matrix_total = 36\n' + '_ai_led_matrix = ws2812(24,_ai_led_matrix_total)\n' + ''; // TODO: Assemble Python into code variable.
  408. var code = '';
  409. return code;
  410. };
  411. Blockly.Blocks['ai_led_matrix_xy'] = {
  412. init: function init() {
  413. this.appendValueInput("x").setCheck(null).appendField(Blockly.Msg.x_led_matrix_width_ai_x);
  414. this.appendValueInput("y").setCheck(null).appendField(Blockly.Msg.x_led_matrix_width_ai_y);
  415. this.setInputsInline(true);
  416. this.setOutput(true, null);
  417. this.setColour(AILEDCOLOR);
  418. this.setTooltip("");
  419. this.setHelpUrl("");
  420. }
  421. };
  422. Blockly.Python['ai_led_matrix_xy'] = function (block) {
  423. var value_x = Blockly.Python.valueToCode(block, 'x', Blockly.Python.ORDER_ATOMIC);
  424. var value_y = Blockly.Python.valueToCode(block, 'y', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable.
  425. var code = value_x + ',' + value_y; // TODO: Change ORDER_NONE to the correct strength.
  426. return [code, Blockly.Python.ORDER_NONE];
  427. };
  428. Blockly.Blocks['ai_led_matrix_wh'] = {
  429. init: function init() {
  430. this.appendValueInput("w").setCheck(null).appendField(Blockly.Msg.x_led_matrix_width_ai);
  431. this.appendValueInput("h").setCheck(null).appendField(Blockly.Msg.x_led_matrix_height_ai);
  432. this.setInputsInline(true);
  433. this.setOutput(true, null);
  434. this.setColour(AILEDCOLOR);
  435. this.setTooltip("");
  436. this.setHelpUrl("");
  437. }
  438. };
  439. Blockly.Python['ai_led_matrix_wh'] = function (block) {
  440. var value_w = Blockly.Python.valueToCode(block, 'w', Blockly.Python.ORDER_ATOMIC);
  441. var value_h = Blockly.Python.valueToCode(block, 'h', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable.
  442. var code = value_w + ',' + value_h; // TODO: Change ORDER_NONE to the correct strength.
  443. return [code, Blockly.Python.ORDER_NONE];
  444. };
  445. Blockly.Blocks['ai_led_matrix_color_picker'] = {
  446. init: function init() {
  447. this.appendDummyInput().appendField(Blockly.Msg.x_led_matrix_color_ai).appendField(new Blockly.FieldColour("#ff0000"), "color");
  448. this.setInputsInline(true);
  449. this.setOutput(true, null);
  450. this.setColour(AILEDCOLOR);
  451. this.setTooltip("");
  452. this.setHelpUrl("");
  453. }
  454. };
  455. Blockly.Python['ai_led_matrix_color_picker'] = function (block) {
  456. var color = block.getFieldValue('color');
  457. var d = 0,
  458. e = 0,
  459. f = 0;
  460. try {
  461. 7 == color.length && (d = parseInt(color.substring(1, 3), 16), e = parseInt(color.substring(3, 5), 16), f = parseInt(color.substring(5, 7), 16));
  462. } catch (g) { } // TODO: Assemble Python into code variable.
  463. var code = d + ',' + e + ',' + f; // TODO: Change ORDER_NONE to the correct strength.
  464. return [code, Blockly.Python.ORDER_NONE];
  465. };
  466. Blockly.Blocks['ai_led_matrix_draw_pixel'] = {
  467. init: function init() {
  468. this.appendDummyInput().appendField(Blockly.Msg.x_led_matrix_draw_title_ai);
  469. this.appendDummyInput().appendField(Blockly.Msg.x_led_matrix_draw_pixel_ai);
  470. this.appendValueInput("color").setCheck(null).appendField(Blockly.Msg.x_led_matrix_parameter_color_ai);
  471. this.appendValueInput("coordinate").setCheck(null).appendField(Blockly.Msg.x_led_matrix_parameter_coord_ai);
  472. this.setInputsInline(false);
  473. this.setPreviousStatement(true, null);
  474. this.setNextStatement(true, null);
  475. this.setColour(AILEDCOLOR);
  476. this.setTooltip("");
  477. this.setHelpUrl("");
  478. }
  479. };
  480. Blockly.Python['ai_led_matrix_draw_pixel'] = function (block) {
  481. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  482. var value_coordinate = Blockly.Python.valueToCode(block, 'coordinate', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable.
  483. var code = '_a = _ai_led_matrix.set_led((((' + value_coordinate.substr(3, 1) + '*6)//6)-1)*6+(' + value_coordinate.substr(1, 1) + '-1),(' + value_color + '))\n';
  484. return code;
  485. };
  486. Blockly.Blocks['ai_led_matrix_show_above'] = {
  487. init: function init() {
  488. this.appendDummyInput().appendField(Blockly.Msg.x_led_matrix_show_above_ai);
  489. this.setInputsInline(false);
  490. this.setPreviousStatement(true, null);
  491. this.setNextStatement(true, null);
  492. this.setColour(AILEDCOLOR);
  493. this.setTooltip("");
  494. this.setHelpUrl("");
  495. }
  496. };
  497. Blockly.Python['ai_led_matrix_show_above'] = function (block) {
  498. // TODO: Assemble Python into code variable.
  499. var code = '_ai_led_matrix.display()\n';
  500. return code;
  501. };
  502. Blockly.Blocks['iot_led_matrix_setup'] = {
  503. init: function init() {
  504. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/led_setup.png", 50, 40, {
  505. alt: "*",
  506. flipRtl: "FALSE"
  507. }));
  508. this.appendDummyInput().appendField(Blockly.Msg.x_led_matrix_setup_iot);
  509. this.setInputsInline(false);
  510. this.setPreviousStatement(true, null);
  511. this.setNextStatement(true, null);
  512. this.setColour(AILEDCOLOR);
  513. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Setup_TOOLTIP);
  514. this.setHelpUrl("");
  515. }
  516. };
  517. Blockly.Python['iot_led_matrix_setup'] = function (block) {
  518. Blockly.Python.definitions_['iot_controller_led_setup'] = '' + 'import machine, neopixel, time\n' + '_6x6_led_matrix = neopixel.NeoPixel(machine.Pin(15), 36)\n' + '\n'; // TODO: Assemble Python into code variable.
  519. var code = '';
  520. return code;
  521. };
  522. Blockly.Blocks['iot_led_matrix_xy'] = {
  523. init: function init() {
  524. this.appendValueInput("x").setCheck(null).appendField(Blockly.Msg.x_led_matrix_width_iot_x);
  525. this.appendValueInput("y").setCheck(null).appendField(Blockly.Msg.x_led_matrix_width_iot_y);
  526. this.setInputsInline(true);
  527. this.setOutput(true, null);
  528. this.setColour(AILEDCOLOR);
  529. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_xy_TOOLTIP);
  530. this.setHelpUrl("");
  531. }
  532. };
  533. Blockly.Python['iot_led_matrix_xy'] = function (block) {
  534. var value_x = Blockly.Python.valueToCode(block, 'x', Blockly.Python.ORDER_ATOMIC);
  535. var value_y = Blockly.Python.valueToCode(block, 'y', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable.
  536. var code = value_x + ',' + value_y; // TODO: Change ORDER_NONE to the correct strength.
  537. return [code, Blockly.Python.ORDER_NONE];
  538. };
  539. Blockly.Blocks['iot_led_matrix_wh'] = {
  540. init: function init() {
  541. this.appendValueInput("w").setCheck(null).appendField(Blockly.Msg.x_led_matrix_width_iot);
  542. this.appendValueInput("h").setCheck(null).appendField(Blockly.Msg.x_led_matrix_height_iot);
  543. this.setInputsInline(true);
  544. this.setOutput(true, null);
  545. this.setColour(AILEDCOLOR);
  546. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_wh_TOOLTIP);
  547. this.setHelpUrl("");
  548. }
  549. };
  550. Blockly.Python['iot_led_matrix_wh'] = function (block) {
  551. var value_w = Blockly.Python.valueToCode(block, 'w', Blockly.Python.ORDER_ATOMIC);
  552. var value_h = Blockly.Python.valueToCode(block, 'h', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable.
  553. var code = value_w + ',' + value_h; // TODO: Change ORDER_NONE to the correct strength.
  554. return [code, Blockly.Python.ORDER_NONE];
  555. };
  556. Blockly.Blocks['iot_led_matrix_color_picker'] = {
  557. init: function init() {
  558. this.appendDummyInput().appendField(Blockly.Msg.x_led_matrix_color_iot).appendField(new Blockly.FieldColour("#ff0000"), "color");
  559. this.setInputsInline(true);
  560. this.setOutput(true, null);
  561. this.setColour(AILEDCOLOR);
  562. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Color_Picker_TOOLTIP);
  563. this.setHelpUrl("");
  564. }
  565. };
  566. Blockly.Python['iot_led_matrix_color_picker'] = function (block) {
  567. var color = block.getFieldValue('color');
  568. var d = 0,
  569. e = 0,
  570. f = 0;
  571. try {
  572. 7 == color.length && (d = parseInt(color.substring(1, 3), 16), e = parseInt(color.substring(3, 5), 16), f = parseInt(color.substring(5, 7), 16));
  573. } catch (g) { } // TODO: Assemble Python into code variable.
  574. var code = d + ',' + e + ',' + f; // TODO: Change ORDER_NONE to the correct strength.
  575. return [code, Blockly.Python.ORDER_NONE];
  576. };
  577. Blockly.Blocks['iot_led_matrix_draw_pixel'] = {
  578. init: function init() {
  579. this.appendDummyInput().appendField(Blockly.Msg.x_led_matrix_draw_title_iot);
  580. this.appendDummyInput().appendField(Blockly.Msg.x_led_matrix_draw_pixel_iot);
  581. this.appendValueInput("color").setCheck(null).appendField(Blockly.Msg.x_led_matrix_parameter_color_iot);
  582. this.appendValueInput("coordinate").setCheck(null).appendField(Blockly.Msg.x_led_matrix_parameter_coord_iot);
  583. this.setInputsInline(false);
  584. this.setPreviousStatement(true, null);
  585. this.setNextStatement(true, null);
  586. this.setColour(AILEDCOLOR);
  587. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Draw_Pixel_TOOLTIP);
  588. this.setHelpUrl("");
  589. }
  590. };
  591. Blockly.Python['iot_led_matrix_draw_pixel'] = function (block) {
  592. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  593. var value_coordinate = Blockly.Python.valueToCode(block, 'coordinate', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable.
  594. // _6x6_led_matrix[(((1*6)//6)-1)*6+(1-1)] = (0, 0, 255)
  595. var code = '_6x6_led_matrix[(((' + value_coordinate.substr(3, 1) + '*6)//6)-1)*6+(' + value_coordinate.substr(1, 1) + '-1)] = (' + value_color + ')\n';
  596. return code;
  597. };
  598. Blockly.Blocks['iot_led_matrix_show_above'] = {
  599. init: function init() {
  600. this.appendDummyInput().appendField(Blockly.Msg.x_led_matrix_show_above_iot);
  601. this.setInputsInline(false);
  602. this.setPreviousStatement(true, null);
  603. this.setNextStatement(true, null);
  604. this.setColour(AILEDCOLOR);
  605. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Show_Above_TOOLTIP);
  606. this.setHelpUrl("");
  607. }
  608. };
  609. Blockly.Python['iot_led_matrix_show_above'] = function (block) {
  610. // TODO: Assemble Python into code variable.
  611. var code = '_6x6_led_matrix.write()\n';
  612. return code;
  613. };
  614. /*
  615. _ _____ ____ ____ _ _
  616. | | | ____| _ \ / ___|| |_ _ __(_)_ __
  617. | | | _| | | | | \___ \| __| '__| | '_ \
  618. | |___| |___| |_| | ___) | |_| | | | |_) |
  619. |_____|_____|____/ |____/ \__|_| |_| .__/
  620. |_|
  621. */
  622. var ESP32_LED_STRIP_COLOR = "#3dade2";
  623. Blockly.Blocks['esp32_main_controller_led_strip_setup'] = {
  624. init: function init() {
  625. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/led_strip_setup.png", 45, 45, {
  626. alt: "*",
  627. flipRtl: "FALSE"
  628. }));
  629. this.appendDummyInput().appendField(Blockly.Msg.ledstrip_setup_text).appendField(new Blockly.FieldVariable("STRIP"), "varitem").appendField(Blockly.Msg.ledstrip_setup);
  630. this.appendValueInput("io").setCheck(null).appendField(Blockly.Msg.ledstrip_set_gpio);
  631. this.appendValueInput("count").setCheck(null).appendField(Blockly.Msg.ledstrip_set_total_led);
  632. this.setInputsInline(false);
  633. this.setPreviousStatement(true, null);
  634. this.setNextStatement(true, null);
  635. this.setColour(ESP32_LED_STRIP_COLOR);
  636. var thisBlock = this;
  637. this.setTooltip(function () {
  638. var mode = thisBlock.getFieldValue('type');
  639. var TOOLTIPS = {
  640. 'iot': Blockly.Msg.Esp32_Main_Controller_Led_Strip_Setup_TOOLTIP.replace('%1', Blockly.Msg.ledstrip_setup_text_iot),
  641. 'ai': Blockly.Msg.Esp32_Main_Controller_Led_Strip_Setup_TOOLTIP.replace('%1', Blockly.Msg.ledstrip_setup_text_ai)
  642. };
  643. return TOOLTIPS[mode];
  644. });
  645. this.setHelpUrl("");
  646. }
  647. };
  648. Blockly.Python['esp32_main_controller_led_strip_setup'] = function (block) {
  649. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  650. var value_io = Blockly.Python.valueToCode(block, 'io', Blockly.Python.ORDER_ATOMIC);
  651. var value_count = Blockly.Python.valueToCode(block, 'count', Blockly.Python.ORDER_ATOMIC);
  652. Blockly.Python.definitions_['led_strip_definition'] = 'import machine, neopixel\n';
  653. Blockly.Python.addVariable(variable_name, variable_name + ' = neopixel.NeoPixel(machine.Pin(' + value_io + '), ' + value_count + ')', true); // TODO: Assemble Python into code variable.
  654. var code = '\n';
  655. return code;
  656. };
  657. Blockly.Blocks['esp32_main_controller_led_strip_set'] = {
  658. init: function init() {
  659. this.appendDummyInput().appendField(Blockly.Msg.ledstrip_set_light_up).appendField(new Blockly.FieldVariable("STRIP"), "varitem");
  660. this.appendValueInput("location").setCheck(null).appendField(Blockly.Msg.ledstrip_set_no);
  661. this.appendValueInput("color").setCheck(null).appendField(Blockly.Msg.ledstrip_set_color);
  662. this.setInputsInline(true);
  663. this.setPreviousStatement(true, null);
  664. this.setNextStatement(true, null);
  665. this.setColour(ESP32_LED_STRIP_COLOR);
  666. this.setTooltip("");
  667. this.setHelpUrl("");
  668. }
  669. };
  670. Blockly.Python['esp32_main_controller_led_strip_set'] = function (block) {
  671. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  672. var value_location = Blockly.Python.valueToCode(block, 'location', Blockly.Python.ORDER_ATOMIC);
  673. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable.
  674. if (value_color.charAt(0) != '#') {
  675. var code = '' + 'np[' + value_location + '] = ' + value_color + '\n' + 'np.write()\n' + '\n';
  676. } else if (value_color.charAt(0) == '#') {
  677. var d = 0,
  678. e = 0,
  679. f = 0;
  680. try {
  681. 7 == color.length && (d = parseInt(color.substring(1, 3), 16), e = parseInt(color.substring(3, 5), 16), f = parseInt(color.substring(5, 7), 16));
  682. } catch (g) { }
  683. var code = '' + 'np[' + value_location + '] = (' + d + ',' + e + ',' + f + ')\n' + 'np.write()\n' + '\n';
  684. }
  685. return code;
  686. };
  687. /*
  688. ____ ____ _
  689. / ___| __ _ _ __ ___ ___| _ \ __ _ __| |
  690. | | _ / _` | '_ ` _ \ / _ \ |_) / _` |/ _` |
  691. | |_| | (_| | | | | | | __/ __/ (_| | (_| |
  692. \____|\__,_|_| |_| |_|\___|_| \__,_|\__,_|
  693. */
  694. var GAMEPAD_COLOR = "#183895";
  695. Blockly.Blocks['x_iot_read_gamepad_button_pressed'] = {
  696. init: function init() {
  697. this.appendDummyInput().appendField(Blockly.Msg.x_gamepad_read_button_first_text_iot).appendField(new Blockly.FieldDropdown([["X", "x"], ["Y", "y"], ["A", "a"], ["B", "b"], ["L", "l"], ["R", "r"]]), "type").appendField(Blockly.Msg.x_gamepad_read_button_first_text_pressed_iot);
  698. this.setInputsInline(true);
  699. this.setOutput(true, null);
  700. this.setColour(GAMEPAD_COLOR);
  701. var thisBlock = this;
  702. this.setTooltip(function () {
  703. var mode = thisBlock.getFieldValue('type');
  704. var TOOLTIPS = {
  705. 'x': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "X"),
  706. 'y': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "Y"),
  707. 'a': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "A"),
  708. 'b': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "B"),
  709. 'l': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "L"),
  710. 'r': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "R")
  711. };
  712. return TOOLTIPS[mode];
  713. });
  714. this.setHelpUrl("");
  715. }
  716. };
  717. Blockly.Python['x_iot_read_gamepad_button_pressed'] = function (block) {
  718. var dropdown_type = block.getFieldValue('type');
  719. Blockly.Python.definitions_['gamepad_all_def_iot'] = '' + 'from machine import ADC, Pin\n' + '\n';
  720. Blockly.Python.definitions_['gamepad_button_def_iot'] = '' + '_px = Pin(21, Pin.IN,Pin.PULL_UP) \n' + '_py = Pin(22, Pin.IN,Pin.PULL_UP) \n' + '_pa = Pin(4, Pin.IN,Pin.PULL_UP) \n' + '_pb = Pin(2, Pin.IN,Pin.PULL_UP) \n' + '_pl = Pin(17, Pin.IN,Pin.PULL_UP) \n' + '_pr = Pin(16, Pin.IN,Pin.PULL_UP)\n' + '\n'; // TODO: Assemble Python into code variable.
  721. var code = '_p' + dropdown_type + '.value() == 1'; // TODO: Change ORDER_NONE to the correct strength.
  722. return [code, Blockly.Python.ORDER_NONE];
  723. };
  724. Blockly.Blocks['x_iot_read_gamepad_button_released'] = {
  725. init: function init() {
  726. this.appendDummyInput().appendField(Blockly.Msg.x_gamepad_read_button_first_text_iot).appendField(new Blockly.FieldDropdown([["X", "x"], ["Y", "y"], ["A", "a"], ["B", "b"], ["L", "l"], ["R", "r"]]), "type").appendField(Blockly.Msg.x_gamepad_read_button_first_text_released_iot);
  727. this.setInputsInline(true);
  728. this.setOutput(true, null);
  729. this.setColour(GAMEPAD_COLOR);
  730. var thisBlock = this;
  731. this.setTooltip(function () {
  732. var mode = thisBlock.getFieldValue('type');
  733. var TOOLTIPS = {
  734. 'x': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "X"),
  735. 'y': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "Y"),
  736. 'a': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "A"),
  737. 'b': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "B"),
  738. 'l': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "L"),
  739. 'r': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "R")
  740. };
  741. return TOOLTIPS[mode];
  742. });
  743. this.setHelpUrl("");
  744. }
  745. };
  746. Blockly.Python['x_iot_read_gamepad_button_released'] = function (block) {
  747. var dropdown_type = block.getFieldValue('type');
  748. Blockly.Python.definitions_['gamepad_all_def_iot'] = '' + 'from machine import ADC, Pin\n' + '\n';
  749. Blockly.Python.definitions_['gamepad_button_def_iot'] = '' + '_px = Pin(21, Pin.IN,Pin.PULL_UP) \n' + '_py = Pin(22, Pin.IN,Pin.PULL_UP) \n' + '_pa = Pin(4, Pin.IN,Pin.PULL_UP) \n' + '_pb = Pin(2, Pin.IN,Pin.PULL_UP) \n' + '_pl = Pin(17, Pin.IN,Pin.PULL_UP) \n' + '_pr = Pin(16, Pin.IN,Pin.PULL_UP)\n' + '\n'; // TODO: Assemble Python into code variable.
  750. var code = '_p' + dropdown_type + '.value() == 0'; // TODO: Change ORDER_NONE to the correct strength.
  751. return [code, Blockly.Python.ORDER_NONE];
  752. };
  753. Blockly.Blocks['x_iot_read_gamepad_button_x-r'] = {
  754. init: function init() {
  755. this.appendDummyInput().appendField(Blockly.Msg.x_gamepad_read_button_status_first_text_iot).appendField(new Blockly.FieldDropdown([["X", "x"], ["Y", "y"], ["A", "a"], ["B", "b"], ["L", "l"], ["R", "r"]]), "type").appendField(Blockly.Msg.x_gamepad_read_button_status_after_text_iot);
  756. this.setInputsInline(true);
  757. this.setOutput(true, null);
  758. this.setColour(GAMEPAD_COLOR);
  759. var thisBlock = this;
  760. this.setTooltip(function () {
  761. var mode = thisBlock.getFieldValue('type');
  762. var TOOLTIPS = {
  763. 'x': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "X"),
  764. 'y': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "Y"),
  765. 'a': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "A"),
  766. 'b': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "B"),
  767. 'l': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "L"),
  768. 'r': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "R")
  769. };
  770. return TOOLTIPS[mode];
  771. });
  772. this.setHelpUrl("");
  773. }
  774. };
  775. Blockly.Python['x_iot_read_gamepad_button_x-r'] = function (block) {
  776. var dropdown_type = block.getFieldValue('type');
  777. Blockly.Python.definitions_['gamepad_all_def_iot'] = '' + 'from machine import ADC, Pin\n' + '\n';
  778. Blockly.Python.definitions_['gamepad_button_def_iot'] = '' + '_px = Pin(21, Pin.IN,Pin.PULL_UP) \n' + '_py = Pin(22, Pin.IN,Pin.PULL_UP) \n' + '_pa = Pin(4, Pin.IN,Pin.PULL_UP) \n' + '_pb = Pin(2, Pin.IN,Pin.PULL_UP) \n' + '_pl = Pin(17, Pin.IN,Pin.PULL_UP) \n' + '_pr = Pin(16, Pin.IN,Pin.PULL_UP)\n' + '\n'; // TODO: Assemble Python into code variable.
  779. var code = '_p' + dropdown_type + '.value()'; // TODO: Change ORDER_NONE to the correct strength.
  780. return [code, Blockly.Python.ORDER_NONE];
  781. };
  782. Blockly.Blocks['x_iot_read_gamepad_joystick'] = {
  783. init: function init() {
  784. this.appendDummyInput().appendField(Blockly.Msg.x_gamepad_read_joystick_read_iot).appendField(new Blockly.FieldDropdown([[Blockly.Msg.x_gamepad_read_joystick_read_iot_x, "X"], [Blockly.Msg.x_gamepad_read_joystick_read_iot_y, "Y"]]), "type").appendField(Blockly.Msg.x_gamepad_read_joystick_read_after_iot);
  785. this.setInputsInline(true);
  786. this.setOutput(true, null);
  787. this.setColour(GAMEPAD_COLOR);
  788. this.setTooltip("");
  789. this.setHelpUrl("");
  790. }
  791. };
  792. Blockly.Python['x_iot_read_gamepad_joystick'] = function (block) {
  793. var dropdown_type = block.getFieldValue('type');
  794. Blockly.Python.definitions_['gamepad_all_def_iot'] = '' + 'from machine import ADC, Pin\n' + '\n';
  795. Blockly.Python.definitions_['gamepad_joystick_def_iot'] = '' + '_adcX = machine.ADC(Pin(36))\n' + '_adcY = machine.ADC(Pin(39))\n' + '\n' + '_adcX.atten(ADC.ATTN_11DB)\n' + '_adcY.atten(ADC.ATTN_11DB)\n' + '_adcX.width(ADC.WIDTH_10BIT)\n' + '_adcY.width(ADC.WIDTH_10BIT)\n' + '\n'; // TODO: Assemble Python into code variable.
  796. var code = '_adc' + dropdown_type + '.read()'; // TODO: Change ORDER_NONE to the correct strength.
  797. return [code, Blockly.Python.ORDER_NONE];
  798. };
  799. Blockly.Blocks['x_iot_read_gamepad_joystick_check'] = {
  800. init: function init() {
  801. this.appendDummyInput().appendField(Blockly.Msg.x_gamepad_read_joystick_when_iot).appendField(new Blockly.FieldDropdown([[Blockly.Msg.x_gamepad_read_joystick_when_top_iot, "top"], [Blockly.Msg.x_gamepad_read_joystick_when_bottom_iot, "bottom"], [Blockly.Msg.x_gamepad_read_joystick_when_left_iot, "left"], [Blockly.Msg.x_gamepad_read_joystick_when_right_iot, "right"], [Blockly.Msg.x_gamepad_read_joystick_when_not_iot, "still"]]), "type");
  802. this.setInputsInline(true);
  803. this.setOutput(true, null);
  804. this.setColour(GAMEPAD_COLOR);
  805. this.setTooltip("");
  806. this.setHelpUrl("");
  807. }
  808. };
  809. Blockly.Python['x_iot_read_gamepad_joystick_check'] = function (block) {
  810. var dropdown_type = block.getFieldValue('type');
  811. Blockly.Python.definitions_['gamepad_all_def_iot'] = '' + 'from machine import ADC, Pin\n' + '\n';
  812. Blockly.Python.definitions_['gamepad_joystick_def_iot'] = '' + '_adcX = machine.ADC(Pin(36))\n' + '_adcY = machine.ADC(Pin(39))\n' + '\n' + '_adcX.atten(ADC.ATTN_11DB)\n' + '_adcY.atten(ADC.ATTN_11DB)\n' + '_adcX.width(ADC.WIDTH_10BIT)\n' + '_adcY.width(ADC.WIDTH_10BIT)\n' + '\n';
  813. Blockly.Python.definitions_['gamepad_joystick_check_direction_def'] = '' + 'def _read_direction():\n' + ' if (_adcY > 300) and (_adcY < 500) and (_adcX > 300) and (_adcX < 500):\n' + ' return "still" \n' + ' elif _adcX > 500:\n' + ' return "left"\n' + ' elif _adcX < 300:\n' + ' return "right"\n' + ' elif _adcY > 500:\n' + ' return "top"\n' + ' elif _adcY < 300:\n' + ' return "bottom"\n' + '\n'; // TODO: Assemble Python into code variable.
  814. var code = '_read_direction() == "' + dropdown_type + '"'; // TODO: Change ORDER_NONE to the correct strength.
  815. return [code, Blockly.Python.ORDER_NONE];
  816. };
  817. Blockly.Blocks['x_ai_read_gamepad_button_pressed'] = {
  818. init: function init() {
  819. this.appendDummyInput().appendField(Blockly.Msg.x_gamepad_read_button_first_text_ai).appendField(new Blockly.FieldDropdown([["X", "x"], ["Y", "y"], ["A", "a"], ["B", "b"], ["L", "l"], ["R", "r"]]), "type").appendField(Blockly.Msg.x_gamepad_read_button_first_text_pressed_ai);
  820. this.setInputsInline(true);
  821. this.setOutput(true, null);
  822. this.setColour(GAMEPAD_COLOR);
  823. this.setTooltip("");
  824. this.setHelpUrl("");
  825. }
  826. };
  827. Blockly.Python['x_ai_read_gamepad_button_pressed'] = function (block) {
  828. var dropdown_type = block.getFieldValue('type');
  829. Blockly.Python.definitions_['gamepad_all_def'] = '' + 'from fpioa_manager import *\n' + 'from Maix import FPIOA, GPIO\n' + '\n';
  830. Blockly.Python.definitions_['gamepad_button_def'] = '' + '_gp_side_button = [13, 14]\n' + '_gp_right_button = [29, 30, 17, 15]\n' + 'FPIOA().set_function(_gp_side_button[0],FPIOA.GPIO0)\n' + 'FPIOA().set_function(_gp_side_button[1],FPIOA.GPIO1)\n' + 'FPIOA().set_function(_gp_right_button[0],FPIOA.GPIO2)\n' + 'FPIOA().set_function(_gp_right_button[1],FPIOA.GPIO3)\n' + 'FPIOA().set_function(_gp_right_button[2],FPIOA.GPIO4)\n' + 'FPIOA().set_function(_gp_right_button[3],FPIOA.GPIO5)\n' + '_gp_button_l = GPIO(GPIO.GPIO0,GPIO.IN)\n' + '_gp_button_r = GPIO(GPIO.GPIO1,GPIO.IN)\n' + '_gp_button_x = GPIO(GPIO.GPIO2,GPIO.IN)\n' + '_gp_button_y = GPIO(GPIO.GPIO3,GPIO.IN)\n' + '_gp_button_a = GPIO(GPIO.GPIO4,GPIO.IN)\n' + '_gp_button_b = GPIO(GPIO.GPIO5,GPIO.IN)\n' + '\n'; // TODO: Assemble Python into code variable.
  831. var code = '_gp_button_' + dropdown_type + '.value() == 1'; // TODO: Change ORDER_NONE to the correct strength.
  832. return [code, Blockly.Python.ORDER_NONE];
  833. };
  834. Blockly.Blocks['x_ai_read_gamepad_button_released'] = {
  835. init: function init() {
  836. this.appendDummyInput().appendField(Blockly.Msg.x_gamepad_read_button_first_text_ai).appendField(new Blockly.FieldDropdown([["X", "x"], ["Y", "y"], ["A", "a"], ["B", "b"], ["L", "l"], ["R", "r"]]), "type").appendField(Blockly.Msg.x_gamepad_read_button_first_text_released_ai);
  837. this.setInputsInline(true);
  838. this.setOutput(true, null);
  839. this.setColour(GAMEPAD_COLOR);
  840. this.setTooltip("");
  841. this.setHelpUrl("");
  842. }
  843. };
  844. Blockly.Python['x_ai_read_gamepad_button_released'] = function (block) {
  845. var dropdown_type = block.getFieldValue('type');
  846. Blockly.Python.definitions_['gamepad_all_def'] = '' + 'from fpioa_manager import *\n' + 'from Maix import FPIOA, GPIO\n' + '\n';
  847. Blockly.Python.definitions_['gamepad_button_def'] = '' + '_gp_side_button = [13, 14]\n' + '_gp_right_button = [29, 30, 15, 17]\n' + 'FPIOA().set_function(_gp_side_button[0],FPIOA.GPIO0)\n' + 'FPIOA().set_function(_gp_side_button[1],FPIOA.GPIO1)\n' + 'FPIOA().set_function(_gp_right_button[0],FPIOA.GPIO2)\n' + 'FPIOA().set_function(_gp_right_button[1],FPIOA.GPIO3)\n' + 'FPIOA().set_function(_gp_right_button[2],FPIOA.GPIO4)\n' + 'FPIOA().set_function(_gp_right_button[3],FPIOA.GPIO5)\n' + '_gp_button_l = GPIO(GPIO.GPIO0,GPIO.IN)\n' + '_gp_button_r = GPIO(GPIO.GPIO1,GPIO.IN)\n' + '_gp_button_x = GPIO(GPIO.GPIO2,GPIO.IN)\n' + '_gp_button_y = GPIO(GPIO.GPIO3,GPIO.IN)\n' + '_gp_button_a = GPIO(GPIO.GPIO4,GPIO.IN)\n' + '_gp_button_b = GPIO(GPIO.GPIO5,GPIO.IN)\n' + '\n'; // TODO: Assemble Python into code variable.
  848. var code = '_gp_button_' + dropdown_type + '.value() == 1'; // TODO: Change ORDER_NONE to the correct strength.
  849. return [code, Blockly.Python.ORDER_NONE];
  850. };
  851. Blockly.Blocks['x_ai_read_gamepad_button_x-r'] = {
  852. init: function init() {
  853. this.appendDummyInput().appendField(Blockly.Msg.x_gamepad_read_button_status_first_text_ai).appendField(new Blockly.FieldDropdown([["X", "x"], ["Y", "y"], ["A", "a"], ["B", "b"], ["L", "l"], ["R", "r"]]), "type").appendField(Blockly.Msg.x_gamepad_read_button_status_after_text_ai);
  854. this.setInputsInline(true);
  855. this.setOutput(true, null);
  856. this.setColour(GAMEPAD_COLOR);
  857. this.setTooltip("");
  858. this.setHelpUrl("");
  859. }
  860. };
  861. Blockly.Python['x_ai_read_gamepad_button_x-r'] = function (block) {
  862. var dropdown_type = block.getFieldValue('type');
  863. Blockly.Python.definitions_['gamepad_all_def'] = '' + 'from fpioa_manager import *\n' + 'from Maix import FPIOA, GPIO\n' + '\n';
  864. Blockly.Python.definitions_['gamepad_button_def'] = '' + '_gp_side_button = [13, 14]\n' + '_gp_right_button = [29, 30, 15, 17]\n' + 'FPIOA().set_function(_gp_side_button[0],FPIOA.GPIO0)\n' + 'FPIOA().set_function(_gp_side_button[1],FPIOA.GPIO1)\n' + 'FPIOA().set_function(_gp_right_button[0],FPIOA.GPIO2)\n' + 'FPIOA().set_function(_gp_right_button[1],FPIOA.GPIO3)\n' + 'FPIOA().set_function(_gp_right_button[2],FPIOA.GPIO4)\n' + 'FPIOA().set_function(_gp_right_button[3],FPIOA.GPIO5)\n' + '_gp_button_l = GPIO(GPIO.GPIO0,GPIO.IN)\n' + '_gp_button_r = GPIO(GPIO.GPIO1,GPIO.IN)\n' + '_gp_button_x = GPIO(GPIO.GPIO2,GPIO.IN)\n' + '_gp_button_y = GPIO(GPIO.GPIO3,GPIO.IN)\n' + '_gp_button_a = GPIO(GPIO.GPIO4,GPIO.IN)\n' + '_gp_button_b = GPIO(GPIO.GPIO5,GPIO.IN)\n' + '\n'; // TODO: Assemble Python into code variable.
  865. var code = '_gp_button_' + dropdown_type + '.value()'; // TODO: Change ORDER_NONE to the correct strength.
  866. return [code, Blockly.Python.ORDER_NONE];
  867. };
  868. Blockly.Blocks['x_ai_read_gamepad_joystick'] = {
  869. init: function init() {
  870. this.appendDummyInput().appendField(Blockly.Msg.x_gamepad_read_joystick_read_ai).appendField(new Blockly.FieldDropdown([[Blockly.Msg.x_gamepad_read_joystick_read_iot_x, "0"], [Blockly.Msg.x_gamepad_read_joystick_read_iot_y, "1"]]), "type").appendField(Blockly.Msg.x_gamepad_read_joystick_read_after_ai);
  871. this.setInputsInline(true);
  872. this.setOutput(true, null);
  873. this.setColour(GAMEPAD_COLOR);
  874. this.setTooltip("");
  875. this.setHelpUrl("");
  876. }
  877. };
  878. Blockly.Python['x_ai_read_gamepad_joystick'] = function (block) {
  879. var dropdown_type = block.getFieldValue('type');
  880. Blockly.Python.definitions_['gamepad_joystick_def'] = '' + 'from machine import I2C\n' + 'from ads1x15 import ADS1115\n' + '\n' + '_adc_i2c = I2C(I2C.I2C0, freq=100000, scl=24, sda=23)\n' + '_joystick_adc = ADS1115(_adc_i2c, 0x48, 1)\n' + '\n'; // TODO: Assemble Python into code variable.
  881. var code = 'int(_joystick_adc.read(channel1=' + dropdown_type + ')/25.75)'; // TODO: Change ORDER_NONE to the correct strength.
  882. return [code, Blockly.Python.ORDER_NONE];
  883. };
  884. Blockly.Blocks['x_ai_read_gamepad_joystick_check'] = {
  885. init: function init() {
  886. this.appendDummyInput().appendField(Blockly.Msg.x_gamepad_read_joystick_when_ai).appendField(new Blockly.FieldDropdown([[Blockly.Msg.x_gamepad_read_joystick_when_top_ai, "top"], [Blockly.Msg.x_gamepad_read_joystick_when_bottom_ai, "bottom"], [Blockly.Msg.x_gamepad_read_joystick_when_left_ai, "left"], [Blockly.Msg.x_gamepad_read_joystick_when_right_ai, "right"], [Blockly.Msg.x_gamepad_read_joystick_when_not_ai, "still"]]), "type");
  887. this.setInputsInline(true);
  888. this.setOutput(true, null);
  889. this.setColour(GAMEPAD_COLOR);
  890. this.setTooltip("");
  891. this.setHelpUrl("");
  892. }
  893. };
  894. Blockly.Python['x_ai_read_gamepad_joystick_check'] = function (block) {
  895. var dropdown_type = block.getFieldValue('type');
  896. Blockly.Python.definitions_['gamepad_joystick_def'] = '' + 'from machine import I2C\n' + 'from ads1x15 import ADS1115\n' + '\n' + '_adc_i2c = I2C(I2C.I2C0, freq=100000, scl=24, sda=23)\n' + '_joystick_adc = ADS1115(_adc_i2c, 0x48, 1)\n' + '\n'; // TODO: Assemble Python into code variable.
  897. '\n';
  898. Blockly.Python.definitions_['gamepad_joystick_check_direction_def'] = '' + 'def _read_direction():\n' + ' if (int(_joystick_adc.read(channel1=0)/25.75) > 400) and (int(_joystick_adc.read(channel1=0)/25.75) < 600) and (int(_joystick_adc.read(channel1=1)/25.75) > 400) and (int(_joystick_adc.read(channel1=1)/25.75) < 600):\n' + ' return "still" \n' + ' elif int(_joystick_adc.read(channel1=1)/25.75) > 600:\n' + ' return "left"\n' + ' elif int(_joystick_adc.read(channel1=1)/25.75) < 400:\n' + ' return "right"\n' + ' elif int(_joystick_adc.read(channel1=0)/25.75) > 600:\n' + ' return "top"\n' + ' elif int(_joystick_adc.read(channel1=0)/25.75) < 400:\n' + ' return "bottom"\n' + '\n'; // TODO: Assemble Python into code variable.
  899. var code = '_read_direction() == "' + dropdown_type + '"'; // TODO: Change ORDER_NONE to the correct strength.
  900. return [code, Blockly.Python.ORDER_NONE];
  901. };
  902. /*
  903. __ ___ _____ _____
  904. /\ \ \/ __\ \_ \___/__ \
  905. / \/ /__\//_____ / /\/ _ \ / /\/
  906. / /\ / \/ \_____/\/ /_| (_) / /
  907. \_\ \/\_____/ \____/ \___/\/
  908. Blockly.Python.addVariable('', '', true);
  909. Blockly.Python.definitions_['definition'] = '';
  910. Blockly.Python.addSetup("setup", '');
  911. */
  912. var ESP32_NBIOT_COLOR = "#629978";
  913. Blockly.Blocks['esp32_nbiot_setup'] = {
  914. init: function init() {
  915. var type = getLocalStorage("type");
  916. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/nbiot_esp32_header.png", 110, 40, {
  917. alt: "*",
  918. flipRtl: "FALSE"
  919. }));
  920. type == 1 ? this.appendDummyInput().appendField(new Blockly.FieldDropdown([[Blockly.Msg.nbiot_module_which_module_ai, "ai"], [Blockly.Msg.nbiot_module_which_module_iot, "iot"]]), "module_type") : this.appendDummyInput().appendField(new Blockly.FieldDropdown([[Blockly.Msg.nbiot_module_which_module_iot, "iot"], [Blockly.Msg.nbiot_module_which_module_ai, "ai"]]), "module_type");
  921. this.appendDummyInput().appendField(Blockly.Msg.nbiot_module_init);
  922. this.setPreviousStatement(true, null);
  923. this.setNextStatement(true, null);
  924. this.setColour(ESP32_NBIOT_COLOR);
  925. this.setTooltip("");
  926. this.setHelpUrl("");
  927. }
  928. };
  929. Blockly.Python['esp32_nbiot_setup'] = function (block) {
  930. // TODO: Assemble Python into code variable.
  931. var dropdown_module_type = block.getFieldValue('module_type');
  932. if (dropdown_module_type == "iot") {
  933. Blockly.Python.addVariable('_nbiot_serial_read', '_nbiot_serial_read = ""', true);
  934. Blockly.Python.addVariable('_nbiot_readings', '_nbiot_readings = ""', true);
  935. Blockly.Python.addVariable('_nbiot_power_on_status', '_nbiot_power_on_status = False', true);
  936. Blockly.Python.addVariable('_nbiot_internet_status', '_nbiot_internet_status = False', true);
  937. Blockly.Python.addVariable('_nbiot_internet_ip_address', '_nbiot_internet_ip_address = ""', true);
  938. Blockly.Python.definitions_['import_definition'] = '' + 'from machine import UART\n' + 'from machine import Timer\n' + 'import ubinascii as binascii\n' + 'import time\n' + '\n' + '_nbiot_serial = UART(2,rx=17, tx=16, baudrate=115200, timeout=10)\n' + '\n' + '_nbiot_serial_timer = Timer(1)\n' + '_nbiot_serial_timer.init(period=50, mode=Timer.PERIODIC, callback=lambda t: _read_nbiot_serial(_nbiot_serial))\n' + '\n' + 'def _read_nbiot_serial(uart):\n' + ' global _nbiot_readings, _nbiot_power_on_status, _nbiot_internet_status, _nbiot_internet_ip_address\n' + ' if _nbiot_serial.any():\n' + ' _nbiot_serial_read = _nbiot_serial.readline().decode("utf-8")\n' + ' _nbiot_serial_read = _nbiot_serial_read.strip()\n' + ' _nbiot_readings = _nbiot_serial_read.strip()\n' + '\n' + ' if _nbiot_serial_read == "+CPIN: READY":\n' + ' _nbiot_power_on_status = True\n' + ' print("NB-IoT Module Initiated!\\n")\n' + ' _nbiot_serial.write(b\'AT+CMSYSCTRL=0,2,50,300,100,800\\r\\n\')\n' + '\n' + ' if "+IP" in _nbiot_serial_read:\n' + ' _nbiot_internet_status = True\n' + ' _nbiot_internet_ip_address = _nbiot_serial_read[5:len(_nbiot_serial_read)]\n' + '';
  939. var code = '';
  940. } else if (dropdown_module_type == "ai") {
  941. Blockly.Python.addVariable('_nbiot_serial_read', '_nbiot_serial_read = ""', true);
  942. Blockly.Python.addVariable('_nbiot_readings', '_nbiot_readings = ""', true);
  943. Blockly.Python.addVariable('_nbiot_power_on_status', '_nbiot_power_on_status = False', true);
  944. Blockly.Python.addVariable('_nbiot_internet_status', '_nbiot_internet_status = False', true);
  945. Blockly.Python.addVariable('_nbiot_internet_ip_address', '_nbiot_internet_ip_address = ""', true);
  946. Blockly.Python.definitions_['import_definition'] = '' + 'import machine, time\n' + 'from fpioa_manager import fm\n' + 'from machine import Timer\n' + '\n' + 'fm.register(14,fm.fpioa.UART2_TX)\n' + 'fm.register(13,fm.fpioa.UART2_RX)\n' + '\n' + '_nbiot_serial = machine.UART(machine.UART.UART2,115200, 8, None, 1, timeout=50)\n' + '\n' + 'def _on_timer(timer):\n' + ' _read_nbiot_serial(_nbiot_serial)\n' + '\n' + '_periodic_tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PERIODIC, period=1, callback=_on_timer, arg=_on_timer)\n' + '_periodic_tim.start()\n' + '\n' + 'def _read_nbiot_serial(uart):\n' + ' global _nbiot_readings, _nbiot_power_on_status, _nbiot_internet_status, _nbiot_internet_ip_address\n' + ' if _nbiot_serial.any():\n' + ' _nbiot_serial_read = _nbiot_serial.readline().decode("utf-8")\n' + ' print(_nbiot_serial_read)\n' + ' _nbiot_serial_read = _nbiot_serial_read.strip()\n' + ' _nbiot_readings = _nbiot_serial_read.strip()\n' + '\n' + ' if _nbiot_serial_read == "+CPIN: READY":\n' + ' _nbiot_power_on_status = True\n' + ' print("NB-IoT Module Initiated!\\n")\n' + ' _nbiot_serial.write(b\'AT+CMSYSCTRL=0,2,50,300,100,800\\r\\n\')\n' + '\n' + ' if "+IP" in _nbiot_serial_read:\n' + ' _nbiot_internet_status = True\n' + ' _nbiot_internet_ip_address = _nbiot_serial_read[5:len(_nbiot_serial_read)]\n' + ' print("NB-IoT is Online! Public IP is: " + str(_nbiot_internet_ip_address))\n' + '\n' + '_nbiot_serial_read = ""\n' + '_nbiot_readings = ""\n' + '_nbiot_power_on_status = False\n' + '_nbiot_internet_status = False\n' + '_nbiot_internet_ip_address = ""\n' + '';
  947. var code = '';
  948. }
  949. return code;
  950. };
  951. Blockly.Blocks['esp32_nbiot_get_power_status'] = {
  952. init: function init() {
  953. this.appendDummyInput().appendField(Blockly.Msg.nbiot_module_power);
  954. this.setOutput(true, null);
  955. this.setColour(ESP32_NBIOT_COLOR);
  956. this.setTooltip(Blockly.Msg.esp32_nbiot_get_power_status_TOOLTIP);
  957. this.setHelpUrl("");
  958. }
  959. };
  960. Blockly.Python['esp32_nbiot_get_power_status'] = function (block) {
  961. // TODO: Assemble Python into code variable.
  962. var code = '_nbiot_power_on_status'; // TODO: Change ORDER_NONE to the correct strength.
  963. return [code, Blockly.Python.ORDER_NONE];
  964. };
  965. Blockly.Blocks['esp32_nbiot_get_network_status'] = {
  966. init: function init() {
  967. this.appendDummyInput().appendField(Blockly.Msg.nbiot_module_connectivity);
  968. this.setOutput(true, null);
  969. this.setColour(ESP32_NBIOT_COLOR);
  970. this.setTooltip("");
  971. this.setHelpUrl("");
  972. }
  973. };
  974. Blockly.Python['esp32_nbiot_get_network_status'] = function (block) {
  975. // TODO: Assemble Python into code variable.
  976. var code = '_nbiot_internet_status'; // TODO: Change ORDER_NONE to the correct strength.
  977. return [code, Blockly.Python.ORDER_NONE];
  978. };
  979. Blockly.Blocks['esp32_nbiot_get_network_ip'] = {
  980. init: function init() {
  981. this.appendDummyInput().appendField(Blockly.Msg.nbiot_module_ip);
  982. this.setOutput(true, null);
  983. this.setColour(ESP32_NBIOT_COLOR);
  984. this.setTooltip("");
  985. this.setHelpUrl("");
  986. }
  987. };
  988. Blockly.Python['esp32_nbiot_get_network_ip'] = function (block) {
  989. // TODO: Assemble Python into code variable.
  990. var code = '_nbiot_internet_ip_address'; // TODO: Change ORDER_NONE to the correct strength.
  991. return [code, Blockly.Python.ORDER_NONE];
  992. };
  993. Blockly.Blocks['esp32_nbiot_onenet_init'] = {
  994. init: function init() {
  995. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/nbiot_onenet.png", 200, 60, {
  996. alt: "*",
  997. flipRtl: "FALSE"
  998. }));
  999. this.appendDummyInput().appendField(Blockly.Msg.nbiot_onenet_title);
  1000. this.appendValueInput("product_id").setCheck(null).appendField(Blockly.Msg.nbiot_onenet_connect_productid);
  1001. this.appendValueInput("device_id").setCheck(null).appendField(Blockly.Msg.nbiot_onenet_connect_deviceid);
  1002. this.appendValueInput("device_apikey").setCheck(null).appendField(Blockly.Msg.nbiot_onenet_connect_device_api);
  1003. this.setPreviousStatement(true, null);
  1004. this.setNextStatement(true, null);
  1005. this.setColour(ONENET_BLOCK_COLOR);
  1006. this.setTooltip("");
  1007. this.setHelpUrl("");
  1008. }
  1009. };
  1010. Blockly.Python['esp32_nbiot_onenet_init'] = function (block) {
  1011. var value_product_id = Blockly.Python.valueToCode(block, 'product_id', Blockly.Python.ORDER_ATOMIC);
  1012. var value_device_id = Blockly.Python.valueToCode(block, 'device_id', Blockly.Python.ORDER_ATOMIC);
  1013. var value_device_apikey = Blockly.Python.valueToCode(block, 'device_apikey', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable.
  1014. Blockly.Python.addVariable('_nbiot_onenet_product_id', '', true);
  1015. Blockly.Python.addVariable('_nbiot_onenet_device_id', '', true);
  1016. Blockly.Python.addVariable('_nbiot_onent_device_apikey', '', true);
  1017. Blockly.Python.addVariable('_nbiot_onenet_connection_status', '', true);
  1018. Blockly.Python.addVariable('_nbiot_onenet_read_anything', '', true);
  1019. Blockly.Python.addVariable('_nbiot_onenet_init_state', '', true);
  1020. Blockly.Python.addSetup("setup", '' + '_nbiot_onenet_init_state = False\n' + '_nbiot_onenet_connection_status = False\n' + '_nbiot_onenet_read_anything = False\n' + '_nbiot_onenet_product_id = ' + value_product_id + '\n' + '_nbiot_onenet_device_id = ' + value_device_id + '\n' + '_nbiot_onent_device_apikey = ' + value_device_apikey + '\n' + '\n' + 'def _nbiot_send_onenet_data(data):\n' + ' _nbiot_send_message = data\n' + ' _nbiot_send_data_content = binascii.hexlify(_nbiot_send_message).decode("utf-8")\n' + ' _nbiot_send_header = ",0300" + str(hex(len(_nbiot_send_message)).split(\'x\')[-1])\n' + ' _nbiot_send_at_content = "AT+MQTTPUB=$dp,0,1,0," + str(len(_nbiot_send_message)+len(_nbiot_send_header[1:7].replace("0",""))) + ",0300" + str(hex(len(_nbiot_send_message)).split(\'x\')[-1]) + str(_nbiot_send_data_content)\n' + ' return _nbiot_send_at_content\n' + '\n' + 'def _nbiot_read_onenet_data(raw_response):\n' + ' _nbiot_receive = raw_response\n' + ' if "+MQTTPUBLISH" not in raw_response:\n' + ' return ""\n' + ' elif "+MQTTPUBLISH" in raw_response:\n' + ' _nbiot_occur = -1\n' + ' for i in range(0, 6): _nbiot_occur = _nbiot_receive.find(",", _nbiot_occur + 1)\n' + ' return _nbiot_receive[_nbiot_occur+1:len(_nbiot_receive)]\n' + '\n' + 'def _read_nbiot_onenet(_incoming_data):\n' + ' global _nbiot_onenet_connection_status, _nbiot_onenet_read_anything\n' + '\n' + ' if "+MQTTOPEN: OK" in _incoming_data:\n' + ' _nbiot_onenet_connection_status = True\n' + ' # print("OneNET Connected!")\n' + ' if "+MQTTOPEN: FAIL" in _incoming_data:\n' + ' _nbiot_onenet_connection_status = False\n' + ' # print("OneNET Lost Connection!")\n' + '\n' + ' if "+MQTTPUBLISH" in _incoming_data:\n' + ' _nbiot_onenet_read_anything = True\n' + ' else:\n' + ' _nbiot_onenet_read_anything = False\n' + '\n' + ' if "+MQTTDISC" in _incoming_data:\n' + ' _nbiot_onenet_read_anything = False\n' + ' _nbiot_onenet_configuration = "AT+MQTTCFG=183.230.40.39,6002,"+_nbiot_onenet_device_id+",100,"+_nbiot_onenet_product_id+","+_nbiot_onent_device_apikey+",1,0\\r\\n"\n' + ' print("Enabling OneNET Connection...")\n' + ' _nbiot_serial.write(_nbiot_onenet_configuration.encode())\n' + ' time.sleep_ms(100)\n' + ' _nbiot_serial.write(b\'AT+MQTTOPEN=1,1,0\\r\\n\')\n' + '\n' + 'def _init_onenet_connection():\n' + ' _nbiot_onenet_configuration = "AT+MQTTCFG=183.230.40.39,6002,"+_nbiot_onenet_device_id+",100,"+_nbiot_onenet_product_id+","+_nbiot_onent_device_apikey+",1,0\\r\\n"\n' + ' print("Enabling OneNET Connection...")\n' + ' _nbiot_serial.write(_nbiot_onenet_configuration.encode())\n' + ' time.sleep_ms(100)\n' + ' _nbiot_serial.write(b\'AT+MQTTOPEN=1,1,0\\r\\n\')\n' + ' time.sleep_ms(100)\n' + '');
  1021. var code = '' + 'if (_nbiot_internet_status == True) and (_nbiot_onenet_init_state == False) and (_nbiot_onenet_connection_status == False):\n' + ' _init_onenet_connection()\n' + ' _nbiot_onenet_init_state = True\n' + '\n' + '_read_nbiot_onenet(_nbiot_readings)\n' + '';
  1022. return code;
  1023. };
  1024. Blockly.Blocks['esp32_nbiot_onenet_connection_status'] = {
  1025. init: function init() {
  1026. this.appendDummyInput().appendField(Blockly.Msg.nbiot_onenet_connectivity);
  1027. this.setOutput(true, null);
  1028. this.setColour(ONENET_BLOCK_COLOR);
  1029. this.setTooltip("");
  1030. this.setHelpUrl("");
  1031. }
  1032. };
  1033. Blockly.Python['esp32_nbiot_onenet_connection_status'] = function (block) {
  1034. // TODO: Assemble Python into code variable.
  1035. var code = '_nbiot_onenet_connection_status'; // TODO: Change ORDER_NONE to the correct strength.
  1036. return [code, Blockly.Python.ORDER_NONE];
  1037. };
  1038. Blockly.Blocks['iot_service_nbiot_onenet'] = {
  1039. init: function init() {
  1040. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/nbiot_onenet_send.png", 210, 60, {
  1041. alt: "*",
  1042. flipRtl: "FALSE"
  1043. }));
  1044. this.appendDummyInput().appendField(Blockly.Msg.nbiot_onenet_send_title);
  1045. /*this.appendValueInput("first_input")
  1046. .setCheck(null)
  1047. .appendField("Field 0");*/
  1048. this.itemCount_ = 1;
  1049. this.updateShape_();
  1050. this.setMutator(new Blockly.Mutator(['iot_service_nbiot_onenet_create_with_item']));
  1051. this.setColour(ONENET_BLOCK_COLOR);
  1052. this.setPreviousStatement(true, null);
  1053. this.setNextStatement(true, null);
  1054. this.setTooltip("");
  1055. this.setHelpUrl("");
  1056. },
  1057. mutationToDom: function mutationToDom() {
  1058. var container = document.createElement('mutation');
  1059. container.setAttribute('items', this.itemCount_);
  1060. return container;
  1061. },
  1062. domToMutation: function domToMutation(xmlElement) {
  1063. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  1064. this.updateShape_();
  1065. },
  1066. decompose: function decompose(workspace) {
  1067. var containerBlock = workspace.newBlock('iot_service_nbiot_onenet_create_with_container');
  1068. containerBlock.initSvg();
  1069. var connection = containerBlock.getInput('STACK').connection;
  1070. for (var i = 0; i < this.itemCount_; i++) {
  1071. var itemBlock = workspace.newBlock('iot_service_nbiot_onenet_create_with_item');
  1072. itemBlock.initSvg();
  1073. connection.connect(itemBlock.previousConnection);
  1074. connection = itemBlock.nextConnection;
  1075. }
  1076. return containerBlock;
  1077. },
  1078. compose: function compose(containerBlock) {
  1079. var itemBlock = containerBlock.getInputTargetBlock('STACK'); // Count number of inputs.
  1080. var connections = [];
  1081. while (itemBlock) {
  1082. connections.push(itemBlock.valueConnection_);
  1083. itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock();
  1084. } // Disconnect any children that don't belong.
  1085. for (var i = 0; i < this.itemCount_; i++) {
  1086. var connection = this.getInput('ADD' + i).connection.targetConnection;
  1087. if (connection && connections.indexOf(connection) == -1) {
  1088. connection.disconnect();
  1089. }
  1090. }
  1091. this.itemCount_ = connections.length;
  1092. this.updateShape_(); // Reconnect any child blocks.
  1093. for (var i = 0; i < this.itemCount_; i++) {
  1094. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  1095. }
  1096. },
  1097. saveConnections: function saveConnections(containerBlock) {
  1098. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  1099. var i = 0;
  1100. while (itemBlock) {
  1101. var input = this.getInput('ADD' + i);
  1102. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  1103. i++;
  1104. itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock();
  1105. }
  1106. },
  1107. updateShape_: function updateShape_() {
  1108. for (var i = 0; i < this.itemCount_; i++) {
  1109. if (!this.getInput('ADD' + i)) {
  1110. var input = this.appendValueInput('ADD' + i);
  1111. input.appendField(Blockly.Msg.nbiot_onenet_send_property).appendField(new Blockly.FieldTextInput("Property" + i), "field" + i); //input.appendField("資料 " + (i + 1) + ":");
  1112. //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
  1113. // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
  1114. }
  1115. }
  1116. while (this.getInput('ADD' + i)) {
  1117. this.removeInput('ADD' + i);
  1118. i++;
  1119. }
  1120. }
  1121. };
  1122. Blockly.Blocks['iot_service_nbiot_onenet_create_with_container'] = {
  1123. init: function init() {
  1124. this.setColour(ONENET_BLOCK_COLOR);
  1125. this.appendDummyInput().appendField("Items");
  1126. this.appendStatementInput('STACK');
  1127. this.setTooltip('');
  1128. this.contextMenu = false;
  1129. }
  1130. };
  1131. Blockly.Blocks['iot_service_nbiot_onenet_create_with_item'] = {
  1132. init: function init() {
  1133. this.setColour(ONENET_BLOCK_COLOR);
  1134. this.appendDummyInput().appendField("Field");
  1135. this.setPreviousStatement(true);
  1136. this.setNextStatement(true);
  1137. this.setTooltip('');
  1138. this.contextMenu = false;
  1139. }
  1140. };
  1141. Blockly.Python['iot_service_nbiot_onenet'] = function (block) {
  1142. // var key = Blockly.Arduino.valueToCode(block, 'KEY', Blockly.Arduino.ORDER_ATOMIC).replace(/\"/g, '') || "";
  1143. var item_field = '',
  1144. item_value = '';
  1145. var nbiot_onenet_data = "'{";
  1146. for (var n = 0; n < this.itemCount_; n++) {
  1147. item_field = block.getFieldValue("field" + n);
  1148. item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
  1149. nbiot_onenet_data += '"' + item_field + '":"\' + str(' + item_value + ') + \'",';
  1150. }
  1151. var nbiot_onenet_data_final = nbiot_onenet_data + "}'";
  1152. var code = '' + '_data_to_be_send = ' + nbiot_onenet_data_final.slice(0, -3) + '}\\r\\n\'' + '\n' + '_data_add_rn = _nbiot_send_onenet_data(_data_to_be_send) + "\\r\\n"\n' + '_data_for_at = _data_add_rn.encode()\n' + '_nbiot_serial.write(_data_for_at)\n' + // '_nbiot_serial.write(_nbiot_send_onenet_data(_data_to_be_send).encode())\n' +
  1153. '\n' + '';
  1154. return code;
  1155. };
  1156. Blockly.Blocks['iot_service_nbiot_onenet_read'] = {
  1157. init: function init() {
  1158. this.appendDummyInput().appendField(Blockly.Msg.nbiot_onenet_when_incoming_msg);
  1159. this.appendValueInput("message").setCheck(null);
  1160. this.appendDummyInput().appendField(Blockly.Msg.nbiot_onenet_when_incoming_msg_when);
  1161. this.appendStatementInput("exec").setCheck(null).appendField(Blockly.Msg.nbiot_onenet_when_incoming_msg_exec);
  1162. this.setPreviousStatement(true, null);
  1163. this.setNextStatement(true, null);
  1164. this.setColour(ONENET_BLOCK_COLOR);
  1165. this.setTooltip("");
  1166. this.setHelpUrl("");
  1167. }
  1168. };
  1169. Blockly.Python['iot_service_nbiot_onenet_read'] = function (block) {
  1170. var value_message = Blockly.Python.valueToCode(block, 'message', Blockly.Python.ORDER_ATOMIC);
  1171. var statements_exec = Blockly.Python.statementToCode(block, 'exec'); // TODO: Assemble Python into code variable.
  1172. var code = 'if (_nbiot_read_onenet_data(_nbiot_readings) == ' + value_message + '):\n' + statements_exec + '\n';
  1173. return code;
  1174. };
  1175. Blockly.Blocks['esp32_nbiot_onenet_read_value'] = {
  1176. init: function init() {
  1177. this.appendDummyInput().appendField(Blockly.Msg.nbiot_onenet_receive_command);
  1178. this.setOutput(true, null);
  1179. this.setColour(ONENET_BLOCK_COLOR);
  1180. this.setTooltip("");
  1181. this.setHelpUrl("");
  1182. }
  1183. };
  1184. Blockly.Python['esp32_nbiot_onenet_read_value'] = function (block) {
  1185. // TODO: Assemble Python into code variable.
  1186. var code = '_nbiot_read_onenet_data(_nbiot_readings)'; // TODO: Change ORDER_NONE to the correct strength.
  1187. return [code, Blockly.Python.ORDER_NONE];
  1188. };
  1189. Blockly.Blocks['iot_service_nbiot_cococloud_read'] = {
  1190. init: function init() {
  1191. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/nbiot_cococloud_get.png", 260, 60, {
  1192. alt: "*",
  1193. flipRtl: "FALSE"
  1194. }));
  1195. this.appendDummyInput().appendField(Blockly.Msg.nbiot_cococloud_get_event_title);
  1196. this.appendDummyInput().appendField(Blockly.Msg.nbiot_cococloud_get_event_api).appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_API_KEY"), "t_api");
  1197. this.setPreviousStatement(true, null);
  1198. this.setNextStatement(true, null);
  1199. this.setColour(COCOCLOUD_BLOCK_COLOR);
  1200. this.setTooltip("");
  1201. this.setHelpUrl("");
  1202. }
  1203. };
  1204. Blockly.Python['iot_service_nbiot_cococloud_read'] = function (block) {
  1205. var api = block.getFieldValue('t_api'); // TODO: Assemble Python into code variable.
  1206. var code = '\n';
  1207. return code;
  1208. };
  1209. Blockly.Blocks['iot_service_nbiot_cococloud_read_data'] = {
  1210. init: function init() {
  1211. this.appendDummyInput().appendField(Blockly.Msg.nbiot_cococloud_get_property);
  1212. this.appendValueInput("property").setCheck(null);
  1213. this.appendDummyInput().appendField(Blockly.Msg.nbiot_cococloud_get_data);
  1214. this.setInputsInline(true);
  1215. this.setOutput(true, null);
  1216. this.setColour(COCOCLOUD_BLOCK_COLOR);
  1217. this.setTooltip("");
  1218. this.setHelpUrl("");
  1219. }
  1220. };
  1221. Blockly.Python['iot_service_nbiot_cococloud_read_data'] = function (block) {
  1222. var property = Blockly.Python.valueToCode(block, 'property', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable.
  1223. var code = '\n'; // TODO: Change ORDER_NONE to the correct strength.
  1224. return [code, Blockly.Python.ORDER_NONE];
  1225. };
  1226. Blockly.Blocks['iot_service_nbiot_cococloud'] = {
  1227. init: function init() {
  1228. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/nbiot_cococloud_send.png", 260, 60, {
  1229. alt: "*",
  1230. flipRtl: "FALSE"
  1231. }));
  1232. this.appendDummyInput().appendField(Blockly.Msg.nbiot_cococloud_send_title);
  1233. this.appendDummyInput().appendField(Blockly.Msg.nbiot_cococloud_send_api).appendField(new Blockly.FieldTextInput("ENTET_YOUT_EVENT_API_KEY"), "t_api");
  1234. /*this.appendValueInput("first_input")
  1235. .setCheck(null)
  1236. .appendField("Field 0");*/
  1237. this.itemCount_ = 1;
  1238. this.updateShape_();
  1239. this.setMutator(new Blockly.Mutator(['iot_service_nbiot_cococloud_create_with_item']));
  1240. this.setColour(COCOCLOUD_BLOCK_COLOR);
  1241. this.setPreviousStatement(true, null);
  1242. this.setNextStatement(true, null);
  1243. this.setTooltip(Blockly.Msg.iot_service_nbiot_cococloud_TOOLTIP);
  1244. this.setHelpUrl("");
  1245. },
  1246. mutationToDom: function mutationToDom() {
  1247. var container = document.createElement('mutation');
  1248. container.setAttribute('items', this.itemCount_);
  1249. return container;
  1250. },
  1251. domToMutation: function domToMutation(xmlElement) {
  1252. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  1253. this.updateShape_();
  1254. },
  1255. decompose: function decompose(workspace) {
  1256. var containerBlock = workspace.newBlock('iot_service_nbiot_cococloud_create_with_container');
  1257. containerBlock.initSvg();
  1258. var connection = containerBlock.getInput('STACK').connection;
  1259. for (var i = 0; i < this.itemCount_; i++) {
  1260. var itemBlock = workspace.newBlock('iot_service_nbiot_cococloud_create_with_item');
  1261. itemBlock.initSvg();
  1262. connection.connect(itemBlock.previousConnection);
  1263. connection = itemBlock.nextConnection;
  1264. }
  1265. return containerBlock;
  1266. },
  1267. compose: function compose(containerBlock) {
  1268. var itemBlock = containerBlock.getInputTargetBlock('STACK'); // Count number of inputs.
  1269. var connections = [];
  1270. while (itemBlock) {
  1271. connections.push(itemBlock.valueConnection_);
  1272. itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock();
  1273. } // Disconnect any children that don't belong.
  1274. for (var i = 0; i < this.itemCount_; i++) {
  1275. var connection = this.getInput('ADD' + i).connection.targetConnection;
  1276. if (connection && connections.indexOf(connection) == -1) {
  1277. connection.disconnect();
  1278. }
  1279. }
  1280. this.itemCount_ = connections.length;
  1281. this.updateShape_(); // Reconnect any child blocks.
  1282. for (var i = 0; i < this.itemCount_; i++) {
  1283. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  1284. }
  1285. },
  1286. saveConnections: function saveConnections(containerBlock) {
  1287. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  1288. var i = 0;
  1289. while (itemBlock) {
  1290. var input = this.getInput('ADD' + i);
  1291. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  1292. i++;
  1293. itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock();
  1294. }
  1295. },
  1296. updateShape_: function updateShape_() {
  1297. for (var i = 0; i < this.itemCount_; i++) {
  1298. if (!this.getInput('ADD' + i)) {
  1299. var input = this.appendValueInput('ADD' + i);
  1300. input.appendField(Blockly.Msg.nbiot_cococloud_send_property).appendField(new Blockly.FieldTextInput("Property" + i), "field" + i); //input.appendField("資料 " + (i + 1) + ":");
  1301. //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
  1302. // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
  1303. }
  1304. }
  1305. while (this.getInput('ADD' + i)) {
  1306. this.removeInput('ADD' + i);
  1307. i++;
  1308. }
  1309. }
  1310. };
  1311. Blockly.Blocks['iot_service_nbiot_cococloud_create_with_container'] = {
  1312. init: function init() {
  1313. this.setColour(ESP32_NBIOT_COLOR);
  1314. this.appendDummyInput().appendField("Items");
  1315. this.appendStatementInput('STACK');
  1316. this.setTooltip('');
  1317. this.contextMenu = false;
  1318. }
  1319. };
  1320. Blockly.Blocks['iot_service_nbiot_cococloud_create_with_item'] = {
  1321. init: function init() {
  1322. this.setColour(COCOCLOUD_BLOCK_COLOR);
  1323. this.appendDummyInput().appendField("Field");
  1324. this.setPreviousStatement(true);
  1325. this.setNextStatement(true);
  1326. this.setTooltip('');
  1327. this.contextMenu = false;
  1328. }
  1329. };
  1330. Blockly.Python['iot_service_nbiot_cococloud'] = function (block) {
  1331. var api = block.getFieldValue('t_api');
  1332. Blockly.Python.addVariable('_nbiot_cococloud_send_init_state', '', true);
  1333. Blockly.Python.addVariable('_nbiot_cococloud_json_content', '', true);
  1334. Blockly.Python.definitions_['import'] = '' + '_nbiot_cococloud_send_init_state = False\n' + '\n' + 'def _nbiot_cococloud_send(data):\n' + ' _nbiot_cococloud_send_data = \'AT+HTTPCONTENT=0,\"\'+ data + \'\"\\r\\n\'\n' + ' print(_nbiot_cococloud_send_data)\n' + ' _nbiot_serial.write(b\'AT+HTTPCLOSE=0\\r\\n\')\n' + ' time.sleep_ms(500)\n' + ' _nbiot_serial.write(b\'AT+HTTPCREATE=\"http://api.cocorobo.cn/\"\\r\\n\')\n' + ' time.sleep_ms(500)\n' + ' _nbiot_serial.write(b\'AT+HTTPHEADER=0,\"Content-Type: application/json\\\\r\\\\n\"\\r\\n\')\n' + ' time.sleep_ms(500)\n' + ' _nbiot_serial.write(_nbiot_cococloud_send_data.encode())\n' + ' time.sleep_ms(500)\n' + ' _nbiot_serial.write(b\'AT+HTTPSEND=0,1,\"/iot/data/eventAPIKey/' + api + '\"\\r\\n\')\n' + ' time.sleep_ms(500)\n' + '';
  1335. var item_field = '',
  1336. item_value = '';
  1337. var nbiot_cococloud_data = "'{";
  1338. for (var n = 0; n < this.itemCount_; n++) {
  1339. item_field = block.getFieldValue("field" + n);
  1340. item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
  1341. nbiot_cococloud_data += '"' + item_field + '":"\' + str(' + item_value + ') + \'",';
  1342. }
  1343. var nbiot_cococloud_data_final = nbiot_cococloud_data + "}'";
  1344. var code = '' + '_nbiot_cococloud_json_content = ' + nbiot_cococloud_data_final.slice(0, -3) + '}\'' + '\n' + '_nbiot_cococloud_send(_nbiot_cococloud_json_content)\n' + '';
  1345. return code;
  1346. };