'use strict'; goog.provide('Blockly.Python.screen'); goog.require('Blockly.Python'); // "void variableTo565(string color){\n"+ // " var result;\n"+ // " var regexp = /(\w+),(\w+),(\w+)/g;\n"+ // " var result = regexp.exec(color);\n" // " var rest = (result[1]>> 3) * 256 * 8 + (result[2] >> 2) * 32 + (result[3] >> 3);\n"+ // " rest = rest.toString(16);\n"+ // " if (rest.length < 4) {\n"+ // " while (rest.length < 4) {\n"+ // "rest = '0' + rest;\n"+ // " }\n"+ // " }\n"+ // " return result ? '0x' + rest : null;\n"+ // "}\n"; Blockly.Python['lcd_setup'] = function (block) { Blockly.Python.addInclude('Aadafruit_GFX', '#include '); Blockly.Python.addInclude('Adafruit_SSD1351', '#include '); Blockly.Python.addInclude('SPI', '#include '); Blockly.Python.addDeclaration('SSD1351_CS', '#define CS 8'); Blockly.Python.addDeclaration('SSD1351_DC', '#define DC 5'); Blockly.Python.addDeclaration('SSD1351_RST', '#define RST 6'); Blockly.Python.addDeclaration('SSD1351_decl', 'Adafruit_SSD1351 tft = Adafruit_SSD1351(CS, DC, RST);'); Blockly.Python.addSetup('SSD1351_begin', 'tft.begin();\n tft.fillScreen(0x0000);\n', false); return ''; }; Blockly.Python['lcd_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['lcd_set_position'] = function (block) { // var reg = /^?[0-9]\d*$/ 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 = posConvert(pos_x, pos_y); // var code = pos_x + "," + pos_y; return [code, Blockly.Python.ORDER_ATOMIC]; }; Blockly.Python['lcd_drawline'] = function (block) { var drawlinecolor = sharpTo565( Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC)); var drawlinestart = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC); var drawlineend = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC); let code = "tft.drawLine(" + drawlinestart + ", " + drawlineend + ", " + drawlinecolor + ");\n" return code; }; Blockly.Python['lcd_drawrect'] = function (block) { var drawrectcolor = sharpTo565( Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC)); var drawrectsize = Blockly.Python.valueToCode(block, 'SIZE', Blockly.Python.ORDER_ATOMIC); var drawrectoption = block.getFieldValue('rect_type'); var drawrectcoord = Blockly.Python.valueToCode(block, 'POS', Blockly.Python.ORDER_ATOMIC); var code = 'tft.' + drawrectoption + 'Rect(' + drawrectcoord + ', ' + drawrectsize + ', ' + drawrectcolor + ');\n'; return code; }; Blockly.Python['lcd_drawcircle'] = function (block) { // var drawcirclecolor = sharpTo565( // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC)); var circletype = block.getFieldValue('circle_type'); var position = Blockly.Python.valueToCode(block, 'POS', Blockly.Python.ORDER_ATOMIC); var radius = Blockly.Python.valueToCode(block, 'RADIUS', Blockly.Python.ORDER_ATOMIC); var code = Blockly.Python['lcd_color'](block); code = 'tft.' + circletype + 'Circle(' + position + ', ' + radius + ', ' + code + ');\n'; return code; }; Blockly.Python['lcd_drawtriangle'] = function (block) { var tri_color = sharpTo565( Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC)); var triangleType = block.getFieldValue('triangle_type'); var pos_a = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC); var pos_b = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC); var pos_c = Blockly.Python.valueToCode(block, 'POSC', Blockly.Python.ORDER_ATOMIC); var code = 'tft.' + triangleType + 'Triangle(' + pos_a + ', ' + pos_b + ', ' + pos_c + ',' + tri_color + ');\n'; return code; }; Blockly.Python['lcd_text'] = function (block) { var text_input = Blockly.Python.valueToCode(block, 'INPUT', Blockly.Python.ORDER_ATOMIC); var text_coord = Blockly.Python.valueToCode(block, 'POS', Blockly.Python.ORDER_ATOMIC); var text_color = sharpTo565( Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC)); var text_size = Blockly.Python.valueToCode(block, 'SIZE', Blockly.Python.ORDER_ATOMIC); var text_type = block.getFieldValue('print_type'); var code = 'tft.setCursor(' + text_coord + ');\n' + 'tft.setTextColor(' + text_color + ');\n' + 'tft.setTextSize(' + text_size + ');\n' + 'tft.' + text_type + '(' + text_input + ');\n'; return code; }; Blockly.Python['lcd_clear'] = function (block) { var code = 'tft.fillScreen(0x0000);\n'; return code; }; Blockly.Python['lcd_rotation'] = function (block) { var degree = block.getFieldValue("DEGREE"); var code = "tft.fillScreen(0x0000);\ntft.setRotation(" + degree + ");\n"; // Blockly.Python.addSetup("setRotation", code); return code; } Blockly.Python['lcd_set_color'] = function (block) { var code = block.getFieldValue('COLOR'); return [code, Blockly.Python.ORDER_ATOMIC]; }; Blockly.Python['lcd_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]; }; /** add on 14:25-09/01/2019 by QR * convert position from origin(x,y) to (x,128-y) */ function posConvert(x, y) { // var reg = /^?[0-9]\d*$/; // if (reg.test(y)) y = 128 - parseInt(y, 10) // else y = "128 - " + y; return x + ", 128 - " + y; } // #end