screen.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. 'use strict';
  2. goog.provide('Blockly.Python.screen');
  3. goog.require('Blockly.Python');
  4. // "void variableTo565(string color){\n"+
  5. // " var result;\n"+
  6. // " var regexp = /(\w+),(\w+),(\w+)/g;\n"+
  7. // " var result = regexp.exec(color);\n"
  8. // " var rest = (result[1]>> 3) * 256 * 8 + (result[2] >> 2) * 32 + (result[3] >> 3);\n"+
  9. // " rest = rest.toString(16);\n"+
  10. // " if (rest.length < 4) {\n"+
  11. // " while (rest.length < 4) {\n"+
  12. // "rest = '0' + rest;\n"+
  13. // " }\n"+
  14. // " }\n"+
  15. // " return result ? '0x' + rest : null;\n"+
  16. // "}\n";
  17. Blockly.Python['lcd_setup'] = function (block) {
  18. Blockly.Python.addInclude('Aadafruit_GFX', '#include <Adafruit_GFX.h>');
  19. Blockly.Python.addInclude('Adafruit_SSD1351', '#include <Adafruit_SSD1351.h>');
  20. Blockly.Python.addInclude('SPI', '#include <SPI.h>');
  21. Blockly.Python.addDeclaration('SSD1351_CS', '#define CS 8');
  22. Blockly.Python.addDeclaration('SSD1351_DC', '#define DC 5');
  23. Blockly.Python.addDeclaration('SSD1351_RST', '#define RST 6');
  24. Blockly.Python.addDeclaration('SSD1351_decl', 'Adafruit_SSD1351 tft = Adafruit_SSD1351(CS, DC, RST);');
  25. Blockly.Python.addSetup('SSD1351_begin', 'tft.begin();\n tft.fillScreen(0x0000);\n', false);
  26. return '';
  27. };
  28. Blockly.Python['lcd_set_width_height'] = function (block) {
  29. var width = Blockly.Python.valueToCode(block, 'WIDTH', Blockly.Python.ORDER_ATOMIC);
  30. var height = Blockly.Python.valueToCode(block, 'HEIGHT', Blockly.Python.ORDER_ATOMIC);
  31. var code = width + ', ' + height;
  32. return [code, Blockly.Python.ORDER_ATOMIC];
  33. };
  34. Blockly.Python['lcd_set_position'] = function (block) {
  35. // var reg = /^?[0-9]\d*$/
  36. var pos_x = Blockly.Python.valueToCode(block, 'POSX', Blockly.Python.ORDER_ATOMIC);
  37. var pos_y = Blockly.Python.valueToCode(block, 'POSY', Blockly.Python.ORDER_ATOMIC);
  38. var code = posConvert(pos_x, pos_y);
  39. // var code = pos_x + "," + pos_y;
  40. return [code, Blockly.Python.ORDER_ATOMIC];
  41. };
  42. Blockly.Python['lcd_drawline'] = function (block) {
  43. var drawlinecolor = sharpTo565(
  44. Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
  45. var drawlinestart = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  46. var drawlineend = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
  47. let code = "tft.drawLine(" + drawlinestart + ", " + drawlineend + ", " + drawlinecolor + ");\n"
  48. return code;
  49. };
  50. Blockly.Python['lcd_drawrect'] = function (block) {
  51. var drawrectcolor = sharpTo565(
  52. Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
  53. var drawrectsize = Blockly.Python.valueToCode(block, 'SIZE', Blockly.Python.ORDER_ATOMIC);
  54. var drawrectoption = block.getFieldValue('rect_type');
  55. var drawrectcoord = Blockly.Python.valueToCode(block, 'POS', Blockly.Python.ORDER_ATOMIC);
  56. var code = 'tft.' + drawrectoption + 'Rect(' + drawrectcoord + ', ' + drawrectsize + ', ' + drawrectcolor + ');\n';
  57. return code;
  58. };
  59. Blockly.Python['lcd_drawcircle'] = function (block) {
  60. // var drawcirclecolor = sharpTo565(
  61. // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
  62. var circletype = block.getFieldValue('circle_type');
  63. var position = Blockly.Python.valueToCode(block, 'POS', Blockly.Python.ORDER_ATOMIC);
  64. var radius = Blockly.Python.valueToCode(block, 'RADIUS', Blockly.Python.ORDER_ATOMIC);
  65. var code = Blockly.Python['lcd_color'](block);
  66. code = 'tft.' + circletype + 'Circle(' + position + ', ' + radius + ', ' + code + ');\n';
  67. return code;
  68. };
  69. Blockly.Python['lcd_drawtriangle'] = function (block) {
  70. var tri_color = sharpTo565(
  71. Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
  72. var triangleType = block.getFieldValue('triangle_type');
  73. var pos_a = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  74. var pos_b = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
  75. var pos_c = Blockly.Python.valueToCode(block, 'POSC', Blockly.Python.ORDER_ATOMIC);
  76. var code = 'tft.' + triangleType + 'Triangle(' + pos_a + ', ' + pos_b + ', ' + pos_c + ',' + tri_color + ');\n';
  77. return code;
  78. };
  79. Blockly.Python['lcd_text'] = function (block) {
  80. var text_input = Blockly.Python.valueToCode(block, 'INPUT', Blockly.Python.ORDER_ATOMIC);
  81. var text_coord = Blockly.Python.valueToCode(block, 'POS', Blockly.Python.ORDER_ATOMIC);
  82. var text_color = sharpTo565(
  83. Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
  84. var text_size = Blockly.Python.valueToCode(block, 'SIZE', Blockly.Python.ORDER_ATOMIC);
  85. var text_type = block.getFieldValue('print_type');
  86. var code = 'tft.setCursor(' + text_coord + ');\n' +
  87. 'tft.setTextColor(' + text_color + ');\n' +
  88. 'tft.setTextSize(' + text_size + ');\n' +
  89. 'tft.' + text_type + '(' + text_input + ');\n';
  90. return code;
  91. };
  92. Blockly.Python['lcd_clear'] = function (block) {
  93. var code = 'tft.fillScreen(0x0000);\n';
  94. return code;
  95. };
  96. Blockly.Python['lcd_rotation'] = function (block) {
  97. var degree = block.getFieldValue("DEGREE");
  98. var code = "tft.fillScreen(0x0000);\ntft.setRotation(" + degree + ");\n";
  99. // Blockly.Python.addSetup("setRotation", code);
  100. return code;
  101. }
  102. Blockly.Python['lcd_set_color'] = function (block) {
  103. var code = block.getFieldValue('COLOR');
  104. return [code, Blockly.Python.ORDER_ATOMIC];
  105. };
  106. Blockly.Python['lcd_set_colorRGB'] = function (block) {
  107. var r = Blockly.Python.valueToCode(block, 'RED_VAL', Blockly.Python.ORDER_ATOMIC);
  108. var g = Blockly.Python.valueToCode(block, 'GREEN_VAL', Blockly.Python.ORDER_ATOMIC);
  109. var b = Blockly.Python.valueToCode(block, 'BLUE_VAL', Blockly.Python.ORDER_ATOMIC);
  110. var code = '(' + r + ',' + g + ',' + b + ')';
  111. return [code, Blockly.Python.ORDER_ATOMIC];
  112. };
  113. /** add on 14:25-09/01/2019 by QR
  114. * convert position from origin(x,y) to (x,128-y)
  115. */
  116. function posConvert(x, y) {
  117. var reg = /^?[0-9]\d*$/;
  118. // if (reg.test(y)) y = 128 - parseInt(y, 10)
  119. // else y = "128 - " + y;
  120. return x + ", 128 - " + y;
  121. }
  122. // #end