screen.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. function sharpTo565(a) {
  18. var b = "#" == a[0] ? (a = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(a)) ? { r: parseInt(a[1], 16), g: parseInt(a[2], 16), b: parseInt(a[3], 16) } : null : (a = /([0-9]{0,3}),([0-9]{0,3}),([0-9]{0,3})/g.exec(a)) ? { r: a[1], g: a[2], b: a[3] } : null;
  19. b = 2048 * (b.r >> 3) + 32 * (b.g >> 2) + (b.b >> 3);
  20. b = b.toString(16);
  21. if (4 > b.length)
  22. for (; 4 > b.length;)b = "0" + b; return a ? "0x" + b : null
  23. }
  24. Blockly.Python['lcd_setup'] = function (block) {
  25. Blockly.Python.addInclude('Aadafruit_GFX', '#include <Adafruit_GFX.h>');
  26. Blockly.Python.addInclude('Adafruit_SSD1351', '#include <Adafruit_SSD1351.h>');
  27. Blockly.Python.addInclude('SPI', '#include <SPI.h>');
  28. Blockly.Python.addDeclaration('SSD1351_CS', '#define CS 8');
  29. Blockly.Python.addDeclaration('SSD1351_DC', '#define DC 5');
  30. Blockly.Python.addDeclaration('SSD1351_RST', '#define RST 6');
  31. Blockly.Python.addDeclaration('SSD1351_decl', 'Adafruit_SSD1351 tft = Adafruit_SSD1351(CS, DC, RST);');
  32. Blockly.Python.addSetup('SSD1351_begin', 'tft.begin()\n tft.fillScreen(0x0000)\n', false);
  33. return '';
  34. };
  35. Blockly.Python['lcd_set_width_height'] = function (block) {
  36. var width = Blockly.Python.valueToCode(block, 'WIDTH', Blockly.Python.ORDER_ATOMIC);
  37. var height = Blockly.Python.valueToCode(block, 'HEIGHT', Blockly.Python.ORDER_ATOMIC);
  38. var code = width + ', ' + height;
  39. return [code, Blockly.Python.ORDER_ATOMIC];
  40. };
  41. Blockly.Python['lcd_set_position'] = function (block) {
  42. // var reg = /^?[0-9]\d*$/
  43. var pos_x = Blockly.Python.valueToCode(block, 'POSX', Blockly.Python.ORDER_ATOMIC);
  44. var pos_y = Blockly.Python.valueToCode(block, 'POSY', Blockly.Python.ORDER_ATOMIC);
  45. //var code = posConvert(pos_x, pos_y);
  46. var code = pos_x + "," + pos_y;
  47. return [code, Blockly.Python.ORDER_ATOMIC];
  48. };
  49. Blockly.Python['lcd_drawline'] = function (block) {
  50. // var drawlinecolor = sharpTo565(
  51. // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
  52. var drawlinestart = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  53. var drawlineend = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
  54. var code = Blockly.Python['lcd_color'](block);
  55. code = "tft.line((" + drawlinestart + "), (" + drawlineend + "), " + code + ")\n"
  56. return code;
  57. };
  58. Blockly.Python['lcd_drawlinelen'] = function (block) {
  59. // var drawlinecolor = sharpTo565(
  60. // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
  61. var _type = block.getFieldValue("rect_type");
  62. var drawlinestart = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  63. var drawlineend = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
  64. var code = Blockly.Python['lcd_color'](block);
  65. code += "tft." + _type + "((" + drawlinestart + "), " + drawlineend + ", FS_COLOR)\n"
  66. return code;
  67. };
  68. Blockly.Python['lcd_drawvhline'] = function (block) {
  69. // var drawlinecolor = sharpTo565(
  70. // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
  71. var _type = block.getFieldValue("rect_type");
  72. var drawlinestart = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  73. var drawlineend = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
  74. var code = Blockly.Python['lcd_color'](block);
  75. code = "tft." + _type + "((" + drawlinestart + "), (" + drawlineend + "), " + code + ")\n"
  76. return code;
  77. };
  78. Blockly.Python['ai_lcd_drawvhtext'] = function (block) {
  79. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  80. var bcolor = Blockly.Python.valueToCode(block, 'BCOLOR', Blockly.Python.ORDER_ATOMIC);
  81. var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
  82. var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  83. //var size = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
  84. var _code = "lcd.draw_string(" + xy + ", " + content + ", lcd." + color + ", lcd." + bcolor + ")";//, scale=" + size + "
  85. return [_code, Blockly.Python.ORDER_ATOMIC]
  86. };
  87. Blockly.Python['lcd_set_width'] = function (block) {
  88. // var reg = /^?[0-9]\d*$/
  89. var pos_w = Blockly.Python.valueToCode(block, 'Width', Blockly.Python.ORDER_ATOMIC);
  90. return [pos_w, Blockly.Python.ORDER_ATOMIC];
  91. };
  92. Blockly.Python['lcd_set_Scale'] = function (block) {
  93. // var reg = /^?[0-9]\d*$/
  94. var pos_w = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
  95. return [pos_w, Blockly.Python.ORDER_ATOMIC];
  96. };
  97. Blockly.Python['lcd_drawrect'] = function (block) {
  98. // var drawrectcolor = sharpTo565(
  99. // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
  100. var drawrectcoord = Blockly.Python.valueToCode(block, 'POS', Blockly.Python.ORDER_ATOMIC);
  101. var code = Blockly.Python['lcd_color'](block);
  102. code = 'tft.pixel((' + drawrectcoord + '), ' + code + ')\n';
  103. return code;
  104. };
  105. Blockly.Python['lcd_drawcircle'] = function (block) {
  106. // var drawcirclecolor = sharpTo565(
  107. // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
  108. var circletype = block.getFieldValue('circle_type');
  109. var position = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  110. var radius = Blockly.Python.valueToCode(block, 'WIDTH', Blockly.Python.ORDER_ATOMIC);
  111. var code = Blockly.Python['lcd_color'](block);
  112. code = 'tft.' + circletype + '((' + position + '), ' + radius + ', ' + code + ')\n';
  113. return code;
  114. };
  115. Blockly.Python['lcd_drawqrcode'] = function (block) {
  116. var position = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  117. var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
  118. var width = Blockly.Python.valueToCode(block, 'WIDTH', Blockly.Python.ORDER_ATOMIC);
  119. var code = 'tft.drawqrcode(' + position + ', ' + content + ', ' + width + ')\n';
  120. return code;
  121. };
  122. Blockly.Python['lcd_drawtext'] = function (block) {
  123. // var drawcirclecolor = sharpTo565(
  124. // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
  125. var position = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  126. var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
  127. var tcontent = block.getFieldValue('TextContnet');
  128. var box = block.getFieldValue('TextCheckBox') == "TRUE" ? "True" : "False";
  129. var code = Blockly.Python['lcd_color'](block);
  130. code = 'tft.text((' + position + '), ' + content + ', ' + code + ', sysfont, ' + tcontent + ', nowrap=' + box + ')\n';
  131. return code;
  132. };
  133. Blockly.Python['lcd_drawtriangle'] = function (block) {
  134. var tri_color = sharpTo565(
  135. Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
  136. var triangleType = block.getFieldValue('triangle_type');
  137. var pos_a = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  138. var pos_b = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
  139. var pos_c = Blockly.Python.valueToCode(block, 'POSC', Blockly.Python.ORDER_ATOMIC);
  140. var code = 'tft.' + triangleType + 'Triangle(' + pos_a + ', ' + pos_b + ', ' + pos_c + ',' + tri_color + ')\n';
  141. return code;
  142. };
  143. Blockly.Python['lcd_text'] = function (block) {
  144. var text_input = Blockly.Python.valueToCode(block, 'INPUT', Blockly.Python.ORDER_ATOMIC);
  145. var text_coord = Blockly.Python.valueToCode(block, 'POS', Blockly.Python.ORDER_ATOMIC);
  146. var text_color = sharpTo565(
  147. Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
  148. var text_size = Blockly.Python.valueToCode(block, 'SIZE', Blockly.Python.ORDER_ATOMIC);
  149. var text_type = block.getFieldValue('print_type');
  150. var code = 'tft.setCursor(' + text_coord + ')\n' +
  151. 'tft.setTextColor(' + text_color + ')\n' +
  152. 'tft.setTextSize(' + text_size + ')\n' +
  153. 'tft.' + text_type + '(' + text_input + ')\n';
  154. return code;
  155. };
  156. Blockly.Python['lcd_clear'] = function (block) {
  157. var code = 'tft.fillScreen(0x0000)\n';
  158. return code;
  159. };
  160. Blockly.Python['lcd_rotation'] = function (block) {
  161. var degree = block.getFieldValue("DEGREE");
  162. var code = "tft.rotation(" + degree + ")\n";
  163. // Blockly.Python.addSetup("setRotation", code);
  164. return code;
  165. }
  166. Blockly.Python['lcd_set_color'] = function (block) {
  167. var code = block.getFieldValue('COLOR');
  168. return [code, Blockly.Python.ORDER_ATOMIC];
  169. };
  170. Blockly.Python['lcd_set_bool'] = function (block) {
  171. var code = block.getFieldValue('BOOL');
  172. return [code, Blockly.Python.ORDER_ATOMIC];
  173. };
  174. Blockly.Python['lcd_set_colortext'] = function (block) {
  175. var code = block.getFieldValue('COLOR');
  176. //Blockly.JavaScript.valueToCode(block, 'NAME', Blockly.JavaScript.ORDER_ATOMIC);
  177. return [code, Blockly.Python.ORDER_ATOMIC];
  178. };
  179. Blockly.Python['lcd_set_colorRGB'] = function (block) {
  180. var r = Blockly.Python.valueToCode(block, 'RED_VAL', Blockly.Python.ORDER_ATOMIC);
  181. var g = Blockly.Python.valueToCode(block, 'GREEN_VAL', Blockly.Python.ORDER_ATOMIC);
  182. var b = Blockly.Python.valueToCode(block, 'BLUE_VAL', Blockly.Python.ORDER_ATOMIC);
  183. var code = '(' + r + ',' + g + ',' + b + ')';
  184. return [code, Blockly.Python.ORDER_ATOMIC];
  185. };
  186. /** add on 14:25-09/01/2019 by QR
  187. * convert position from origin(x,y) to (x,128-y)
  188. */
  189. function posConvert(x, y) {
  190. var reg = /^?[0-9]\d*$/;
  191. // if (reg.test(y)) y = 128 - parseInt(y, 10)
  192. // else y = "128 - " + y;
  193. return x + ", 128 - " + y;
  194. }
  195. // #end