123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- /**
- * @license Licensed under the Apache License, Version 2.0 (the "License"):
- * http://www.apache.org/licenses/LICENSE-2.0
- */
- /**
- * @fileoverview Code generator for the test 2 blocks.
- */
- 'use strict';
- goog.provide('Blockly.Python.led');
- goog.require('Blockly.Python');
- var globalBrightness;
- Blockly.Python['led_neopixel_setup'] = function(block) {
- Blockly.Python.addInclude('Aadafruit_GFX', '#include <Adafruit_GFX.h>');
- Blockly.Python.addInclude('Adafruit_NeoMatrix', '#include <Adafruit_NeoMatrix.h>');
- Blockly.Python.addInclude('Adafruit_NeoPixel', '#include <Adafruit_NeoPixel.h>');
- var number_neopixel_initpin = block.getFieldValue('neopixel_initpin');
- var number_neopixel_initheight = block.getFieldValue('neopixel_initheight');
- var number_neopixel_initwidth = block.getFieldValue('neopixel_initwidth');
- var number_neopixel_initbright = block.getFieldValue('neopixel_initbright');
- Blockly.Python.addDeclaration('ledDeclA' + number_neopixel_initpin, '#ifndef PSTR\n' + ' #define PSTR\n' + '#endif\n', false);
- Blockly.Python.addDeclaration('ledDeclB' + number_neopixel_initpin, '#define\t' + 'PIN\t' + number_neopixel_initpin, false);
- Blockly.Python.addDeclaration('ledDeclC' + number_neopixel_initpin, 'Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(5,5,' + number_neopixel_initwidth + ',' + number_neopixel_initheight + ', PIN,NEO_TILE_TOP + NEO_TILE_LEFT + NEO_TILE_COLUMNS + NEO_TILE_PROGRESSIVE + NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_ROWS + NEO_MATRIX_PROGRESSIVE,NEO_GRB + NEO_KHZ800);');
- Blockly.Python.addSetup('ledSetup' + number_neopixel_initpin, 'matrix.begin();\n' + ' matrix.setBrightness(' + number_neopixel_initbright + ');\n');
-
- globalBrightness = number_neopixel_initbright;
- var code = '';
- return code;
- };
- Blockly.Python['led_setrotation'] = function(block) {
- var dropdown_led_setrotation_option = block.getFieldValue('led_setrotation_option');
- var code = 'matrix.setRotation(' + dropdown_led_setrotation_option + ');\n';
- return code;
- };
- Blockly.Python['led_setbright'] = function(block) {
- var bright_value = Blockly.Python.valueToCode(block, 'BRIGHTNESS', Blockly.Python.ORDER_ATOMIC);
- var code = 'matrix.setBrightness(' + bright_value + ');\n';
- return code;
- }
- Blockly.Python['led_set_width_height'] = function(block) {
- var width = Blockly.Python.valueToCode(block, 'WIDTH', Blockly.Python.ORDER_ATOMIC);
- var height = Blockly.Python.valueToCode(block, 'HEIGHT', Blockly.Python.ORDER_ATOMIC);
- var code = width + ', ' + height;
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- Blockly.Python['led_set_position'] = function(block) {
- var pos_x = Blockly.Python.valueToCode(block, 'POSX', Blockly.Python.ORDER_ATOMIC);
- var pos_y = Blockly.Python.valueToCode(block, 'POSY', Blockly.Python.ORDER_ATOMIC);
- var code = pos_x + ', ' + pos_y;
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- Blockly.Python['led_drawpixel'] = function(block) {
- var color;
- var color_val = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- if (color_val[0] == "#") {
- color = hexToRgb(color_val);
- } else {
- color = color_val;
- }
- var position = Blockly.Python.valueToCode(block, 'POS', Blockly.Python.ORDER_ATOMIC);
- var code = 'matrix.drawPixel(' + position + ',matrix.Color' + color + ');\n';
- return code;
- };
- Blockly.Python['led_drawrect'] = function(block) {
- //var color = hexToRgb(Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
- var color;
- var color_val = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- if (color_val[0] == "#") {
- color = hexToRgb(color_val);
- } else {
- color = color_val;
- }
- var size = Blockly.Python.valueToCode(block, 'SIZE', Blockly.Python.ORDER_ATOMIC);
- var type = block.getFieldValue('rect_type');
- var position = Blockly.Python.valueToCode(block, 'POS', Blockly.Python.ORDER_ATOMIC);
- var code = 'matrix.' + type + 'Rect(' + position + ',' + size + ',matrix.Color' + color + ');\n';
- return code;
- };
- Blockly.Python['led_drawline'] = function(block) {
- //var color = hexToRgb(Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
- var color;
- var color_val = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- if (color_val[0] == "#") {
- color = hexToRgb(color_val);
- } else {
- color = color_val;
- }
- var type = Blockly.Python.valueToCode(block, 'TYPE', Blockly.Python.ORDER_ATOMIC);
- var positionA = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var positionB = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
- var code = 'matrix.drawLine(' + positionA + ',' + positionB + ',' + 'matrix.Color' + color + ');\n';
- return code;
- };
- Blockly.Python['led_showall'] = function(block) {
- var code = "matrix.show();\n" + "delayMicroseconds(500);\n" + "matrix.setBrightness(" + globalBrightness + ");\n";
- return code;
- };
- Blockly.Python['led_text'] = function(block) {
- var text_x_val = Number(block.getFieldValue('x_val'));
- var text_y_val = Number(block.getFieldValue('y_val'));
- var colour_text_color = hexToRgb(Blockly.Python.valueToCode(block, 'text_color', Blockly.Python.ORDER_ATOMIC));
- var colour_back_color = hexToRgb(Blockly.Python.valueToCode(block, 'back_color', Blockly.Python.ORDER_ATOMIC));
- var number_text_size = block.getFieldValue('text_size');
- var text_color = 'matrix.Color' + colour_text_color;
- var back_color = 'matrix.Color' + colour_back_color;
- var code = 'matrix.setCursor(' + text_x_val + ', ' + text_y_val + ');\n' +
- 'matrix.setTextColor(' + text_color + ', ' + back_color + ');\n' +
- 'matrix.setTextSize(' + number_text_size + ');\n' +
- 'matrix.setTextWrap(false);\n';
- return code;
- };
- Blockly.Python['led_clear'] = function(block) {
- Blockly.Python.addFunction("led_clear", "void ledClear() {\n" +
- ' matrix.fillScreen(0);\n' +
- ' matrix.show();\n' +
- ' delay(1);\n' +
- '}\n');
- var code = 'ledClear();\n';
- return code;
- };
- Blockly.Python['led_scrolling_text'] = function(block) {
- var text_led_scrolling_speed = block.getFieldValue('led_scrolling_speed');
- var colour_led_scrolling_color = hexToRgb(Blockly.Python.valueToCode(block, 'led_scrolling_color', Blockly.Python.ORDER_ATOMIC));
- Blockly.Python.addDeclarataddDion('led_scrolling_decl', 'int x = tiledMatrix.width() ;');
- var code = 'matrix.setTextWrap(false);\n' +
- 'matrix.setTextColor(matrix.Color' + colour_led_scrolling_color + ');\n' +
- 'matrix.setCursor(x, 0);\n' +
- 'int len = text.length()*6;\n' +
- 'len *= (-1);\n' +
- '//every word takes about 6 pixel wide\n' +
- 'if(--x < len) {\n' +
- 'x = matrix.width();\n' +
- '}\n' +
- 'matrix.show();\n' +
- 'delay(' + text_led_scrolling_speed + ');\n';
- return code;
- };
- Blockly.Python['led_pattern_5x5'] = function(block) {
- let pattern_5x5 = 'void setPattern(uint16_t patternMatrix[5][5]) {\n' +
- ' for (int row=0; row<5; row++) {\n' +
- ' for (int col=0; col<5; col++) {\n' +
- ' matrix.drawPixel(row, col, patternMatrix[col][row]);\n }\n }\n}\n';
- Blockly.Python.addFunction('pattern_5x5', pattern_5x5);
- let panel_init = 'uint16_t pattern[5][5] =\n{\n' +
- ' { matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0), },\n' +
- ' { matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0), },\n' +
- ' { matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0), },\n' +
- ' { matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0), },\n' +
- ' { matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0), }\n};\n';
- Blockly.Python.addDeclaration('pattern_5x5_dec', panel_init);
- let code = '';
- for (var i = 0; i < 5; i++) {
- for (var j = 0; j < 5; j++) {
- let color = "matrix.Color" + hexToRgb(block.getFieldValue(i + "_" + j));
- if (color === "matrix.Color(59, 85, 135)") {
- color = "matrix.Color(0, 0, 0)";
- }
- code += 'pattern[4-' + i + '][4-' + j + '] = ' + color + '; ';
- }
- code += '\n'
- }
- code += 'setPattern(pattern);\n';
- return code;
- };
- Blockly.Python['led_drawWord_5x5'] = function(block) {
- var word = Blockly.Python.valueToCode(block, 'WORD', Blockly.Python.ORDER_ATOMIC);
- var color;
- var color_val = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- if (color_val[0] == "#") {
- color = hexToRgb(color_val);
- } else {
- color = color_val;
- }
- Blockly.Python.addInclude("myfont_5x5", "#include <Fonts/MyFont5pt7b.h>");
- Blockly.Python.addSetup("setTextWrap_false", "matrix.setTextWrap(false);");
- Blockly.Python.addSetup("font_5x5", "matrix.setFont(&MyFont5pt7b);\n");
- var code = 'matrix.setTextColor(matrix.Color' + color + ');\n' +
- 'matrix.setCursor(0, 4);\n' +
- 'matrix.print(F(' + word + '));\n';
- return code;
- };
- Blockly.Python['led_text_input'] = function(block) {
- var scroll = block.getFieldValue("scroll");
- var font = block.getFieldValue("font");
- var text = Blockly.Python.valueToCode(block, 'TEXT', Blockly.Python.ORDER_ATOMIC);
- //正则表达式判断拼接的积木是 text积木还是 variable积木
- var isString = /"/g.test(text)
- var color;
- var color_val = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- if (color_val[0] == "#") {
- color = hexToRgb(color_val);
- } else {
- color = color_val;
- }
- var position = Blockly.Python.valueToCode(block, 'POS', Blockly.Python.ORDER_ATOMIC);
- var position_x = parseInt(position.split(',')[0]);
- var position_y = parseInt(position.split(',')[1]);
- var code = 'matrix.setTextColor(matrix.Color' + color + ');\n';
- Blockly.Python.addSetup("setTextWrap_false", "matrix.setTextWrap(false);");
- if (font == "1") {
- Blockly.Python.addInclude("myfont_5x5", "#include <Fonts/MyFont5pt7b.h>");
- Blockly.Python.addSetup("myfont_setup", "matrix.setFont(&MyFont5pt7b);\n");
- position_y += 4;
- Blockly.Python.addDeclaration("pixelPerChar_5", "int pixelPerChar = 5;")
- } else if (font == "3") {
- Blockly.Python.addInclude("FreeSansBold9pt7b", "#include <Fonts/FreeSansBold9pt7b.h>");
- Blockly.Python.addSetup("FreeSansBold9pt7b", "matrix.setFont(&FreeSansBold9pt7b);\n");
- position_y += 12;
- Blockly.Python.addDeclaration("pixelPerChar_9", "int pixelPerChar = 9;")
- } else {
- Blockly.Python.addDeclaration("pixelPerChar_5", "int pixelPerChar = 5;")
- }
- if (scroll == "1") {
- Blockly.Python.addDeclaration("scroll_" + position_x + "_" + position_y, "int scroll_" + position_x + "_" + position_y + " = matrix.width();");
- Blockly.Python.addDeclaration("maxDisplacement_" + position_x + "_" + position_y, "int maxDisplacement_" + position_x + "_" + position_y + ";");
- code += 'matrix.fillScreen(0);\n' +
- 'matrix.setRotation(2);\n'+
- 'matrix.setCursor(scroll_' + position_x + '_' + position_y + '+' + position_x + ', ' + position_y + ');\n ';
- if (isString) { // text积木处理
- code += 'matrix.print(F(' + text + '));\n' +
- "maxDisplacement_" + position_x + "_" + position_y + " = strlen(" + text + ")* pixelPerChar + matrix.width();\n";
- } else { // variable积木处理
- code += 'matrix.print(' + text + ');\n' +
- "maxDisplacement_" + position_x + "_" + position_y + " = " + text + ".length()* pixelPerChar + matrix.width();\n";
- }
- code += 'if(--scroll_' + position_x + '_' + position_y + ' < -maxDisplacement_' + position_x + '_' + position_y + ') {\n' +
- 'scroll_' + position_x + '_' + position_y + ' = matrix.width();\n' +
- '}\n';
- } else {
- code += 'matrix.setCursor(' + position_x + ',' + position_y + ');\n';
- if (isString) // text积木处理
- code += 'matrix.print(F(' + text + '));\n';
- else // variable积木处理
- code += 'matrix.print(' + text + ');\n';
- }
- return code;
- };
- Blockly.Python['led_draw_emotion'] = function(block) {
- let pattern_5x5 = 'void setPattern(uint16_t patternMatrix[5][5]) {\n' +
- ' for (int row=0; row<5; row++) {\n' +
- ' for (int col=0; col<5; col++) {\n' +
- ' matrix.drawPixel(row, col, patternMatrix[col][row]);\n }\n }\n}\n';
- Blockly.Python.addFunction('pattern_5x5', pattern_5x5);
- let panel_init = 'uint16_t pattern[5][5] =\n{\n' +
- ' { matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0), },\n' +
- ' { matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0), },\n' +
- ' { matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0), },\n' +
- ' { matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0), },\n' +
- ' { matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0),matrix.Color(0, 0, 0), }\n};\n';
- Blockly.Python.addDeclaration('pattern_5x5_dec', panel_init);
- var facetype = block.getFieldValue("face");
- block.updateImage_();
- var color;
- var color_val = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- if (color_val[0] == "#") {
- color = hexToRgb(color_val);
- } else {
- color = color_val;
- }
- var code = "";
- switch (facetype) {
- case "1":
- code = "pattern[0][0] = matrix.Color(0, 0, 0); pattern[0][1] = matrix.Color(0, 0, 0); pattern[0][2] = matrix.Color(0, 0, 0); pattern[0][3] = matrix.Color(0, 0, 0); pattern[0][4] = matrix.Color(0, 0, 0);\n" +
- "pattern[1][0] = matrix.Color(0, 0, 0); pattern[1][1] = matrix.Color" + color + "; pattern[1][2] = matrix.Color(0, 0, 0); pattern[1][3] = matrix.Color" + color + "; pattern[1][4] = matrix.Color(0, 0, 0);\n" +
- "pattern[2][0] = matrix.Color(0, 0, 0); pattern[2][1] = matrix.Color(0, 0, 0); pattern[2][2] = matrix.Color(0, 0, 0); pattern[2][3] = matrix.Color(0, 0, 0); pattern[2][4] = matrix.Color(0, 0, 0);\n" +
- "pattern[3][0] = matrix.Color" + color + "; pattern[3][1] = matrix.Color(0, 0, 0); pattern[3][2] = matrix.Color(0, 0, 0); pattern[3][3] = matrix.Color(0, 0, 0); pattern[3][4] = matrix.Color" + color + ";\n" +
- "pattern[4][0] = matrix.Color(0, 0, 0); pattern[4][1] = matrix.Color" + color + "; pattern[4][2] = matrix.Color" + color + "; pattern[4][3] = matrix.Color" + color + "; pattern[4][4] = matrix.Color(0, 0, 0);\n" +
- "setPattern(pattern);\n"
- break;
- case "2":
- code = "pattern[0][0] = matrix.Color(0, 0, 0); pattern[0][1] = matrix.Color(0, 0, 0); pattern[0][2] = matrix.Color(0, 0, 0); pattern[0][3] = matrix.Color(0, 0, 0); pattern[0][4] = matrix.Color(0, 0, 0);\n" +
- "pattern[1][0] = matrix.Color" + color + "; pattern[1][1] = matrix.Color" + color + "; pattern[1][2] = matrix.Color(0, 0, 0); pattern[1][3] = matrix.Color" + color + "; pattern[1][4] = matrix.Color" + color + ";\n" +
- "pattern[2][0] = matrix.Color(0, 0, 0); pattern[2][1] = matrix.Color(0, 0, 0); pattern[2][2] = matrix.Color(0, 0, 0); pattern[2][3] = matrix.Color(0, 0, 0); pattern[2][4] = matrix.Color(0, 0, 0);\n" +
- "pattern[3][0] = matrix.Color(0, 0, 0); pattern[3][1] = matrix.Color" + color + "; pattern[3][2] = matrix.Color" + color + "; pattern[3][3] = matrix.Color" + color + "; pattern[3][4] = matrix.Color(0, 0, 0);\n" +
- "pattern[4][0] = matrix.Color(0, 0, 0); pattern[4][1] = matrix.Color(0, 0, 0); pattern[4][2] = matrix.Color(0, 0, 0); pattern[4][3] = matrix.Color(0, 0, 0); pattern[4][4] = matrix.Color(0, 0, 0);\n" +
- "setPattern(pattern);\n"
- break;
- case "3":
- code = "pattern[0][0] = matrix.Color(0, 0, 0); pattern[0][1] = matrix.Color(0, 0, 0); pattern[0][2] = matrix.Color(0, 0, 0); pattern[0][3] = matrix.Color(0, 0, 0); pattern[0][4] = matrix.Color(0, 0, 0);\n" +
- "pattern[1][0] = matrix.Color(0, 0, 0); pattern[1][1] = matrix.Color" + color + "; pattern[1][2] = matrix.Color(0, 0, 0); pattern[1][3] = matrix.Color" + color + "; pattern[1][4] = matrix.Color(0, 0, 0);\n" +
- "pattern[2][0] = matrix.Color(0, 0, 0); pattern[2][1] = matrix.Color(0, 0, 0); pattern[2][2] = matrix.Color(0, 0, 0); pattern[2][3] = matrix.Color(0, 0, 0); pattern[2][4] = matrix.Color(0, 0, 0);\n" +
- "pattern[3][0] = matrix.Color(0, 0, 0); pattern[3][1] = matrix.Color" + color + "; pattern[3][2] = matrix.Color" + color + "; pattern[3][3] = matrix.Color" + color + "; pattern[3][4] = matrix.Color(0, 0, 0);\n" +
- "pattern[4][0] = matrix.Color" + color + "; pattern[4][1] = matrix.Color(0, 0, 0); pattern[4][2] = matrix.Color(0, 0, 0); pattern[4][3] = matrix.Color(0, 0, 0); pattern[4][4] = matrix.Color" + color + ";\n" +
- "setPattern(pattern);\n"
- break;
- }
- return code;
- };
- Blockly.Python['led_draw_strip'] = function(block) {
- var color;
- var color_val = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- if (color_val[0] == "#") {
- color = hexToRgb(color_val);
- } else {
- color = color_val;
- }
- var location = Blockly.Python.valueToCode(block, 'LOC', Blockly.Python.ORDER_ATOMIC);
- var position = location + " % 5 , " + location + " / 5";
- var code = 'matrix.drawPixel(' + position + ',matrix.Color' + color + ');\n';
- return code;
- };
- Blockly.Python['led_set_color'] = function(block) {
- var code = block.getFieldValue('COLOR');
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- Blockly.Python['led_set_colorRGB'] = function(block) {
- var r = Blockly.Python.valueToCode(block, 'RED_VAL', Blockly.Python.ORDER_ATOMIC);
- var g = Blockly.Python.valueToCode(block, 'GREEN_VAL', Blockly.Python.ORDER_ATOMIC);
- var b = Blockly.Python.valueToCode(block, 'BLUE_VAL', Blockly.Python.ORDER_ATOMIC);
- var code = '(' + r + ',' + g + ',' + b + ')';
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
|