touch.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use strict';
  2. goog.provide('Blockly.Python.touch');
  3. goog.require('Blockly.Python');
  4. Blockly.Python['touch_setup'] = function(block) {
  5. var wireh = '#include <Wire.h>\n';
  6. Blockly.Python.addDeclaration('wire.h', wireh);
  7. var decl = '#include "Adafruit_MPR121.h"\n' +
  8. 'Adafruit_MPR121 cap = Adafruit_MPR121();\n' +
  9. 'uint16_t lasttouched = 0;\n' +
  10. 'uint16_t currtouched = 0;\n';
  11. Blockly.Python.addDeclaration('touch_sensor_decl', decl);
  12. var setup = 'Serial.begin(9600);\n' +
  13. 'Serial.println("Adafruit MPR121 Capacitive Touch sensor test");\n' +
  14. ' if (!cap.begin(0x5A)) {\n' +
  15. ' Serial.println("MPR121 not found, check wiring?");\n' +
  16. ' while (1);\n }\n' +
  17. ' Serial.println("MPR121 found!");\n';
  18. Blockly.Python.addSetup('music_keyboard_setup', setup);
  19. return '';
  20. };
  21. Blockly.Python['touch_keyboardset'] = function(block) {
  22. var variable_name = "i_";
  23. var codePre = "currtouched = cap.touched();\n" +
  24. "for (uint8_t " + variable_name + "=0; " + variable_name + "<12; " + variable_name + "++) {\n" +
  25. " if ((currtouched & _BV(" + variable_name + ")) && !(lasttouched & _BV(" + variable_name + ")) ) {\n";
  26. var code = " switch (" + variable_name + ") {\n";
  27. var argument, branch;
  28. for (var n = 0; n <= block.caseCount_; n++) {
  29. argument = block.getFieldValue('keyboard' + n) || n;
  30. branch = Blockly.Python.statementToCode(block, 'DO' + n);
  31. code += " case " + argument + ":\n " + branch + " break;\n";
  32. }
  33. return codePre + code + ' }\n' + 'lasttouched = currtouched;\n' + ' }\n' + '}\n';
  34. }
  35. Blockly.Python['touch_keyboardset_loose'] = function(block) {
  36. var variable_name = "i_";
  37. var codePre = "for (uint8_t " + variable_name + "=0; " + variable_name + "<12; " + variable_name + "++) {\n" +
  38. " if (!(currtouched & _BV(" + variable_name + ")) && (lasttouched & _BV(" + variable_name + ")) ) {\n";
  39. var code = " switch (" + variable_name + ") {\n";
  40. var argument, branch;
  41. for (var n = 0; n <= block.caseCount_; n++) {
  42. argument = block.getFieldValue('keyboard' + n) || n;
  43. branch = Blockly.Python.statementToCode(block, 'DO' + n);
  44. code += " case " + argument + ":\n " + branch + " break;\n";
  45. }
  46. return codePre + code + ' }\n' + 'lasttouched = 0;\n' + ' }\n' + '}\n';
  47. }
  48. /* new version touch */
  49. Blockly.Python['touch_setup_1'] = function(block) {
  50. var wireh = '#include <Wire.h>\n';
  51. Blockly.Python.addDeclaration('wire.h', wireh);
  52. var decl = '#include "Adafruit_MPR121.h"\n' +
  53. 'Adafruit_MPR121 cap = Adafruit_MPR121();\n' +
  54. 'uint16_t lasttouched = 0;\n' +
  55. 'uint16_t currtouched = 0;\n';
  56. Blockly.Python.addDeclaration('touch_sensor_decl', decl);
  57. var setup = 'Serial.println("Adafruit MPR121 Capacitive Touch sensor test");\n' +
  58. ' if (!cap.begin(0x5A)) {\n' +
  59. ' Serial.println("MPR121 not found, check wiring?");\n' +
  60. ' while (1);\n }\n' +
  61. ' Serial.println("MPR121 found!");\n';
  62. Blockly.Python.addSetup('music_keyboard_setup', setup);
  63. var code = "currtouched = cap.touched();\n"
  64. return code;
  65. };
  66. Blockly.Python['touch_each'] = function(block) {
  67. var key = block.getFieldValue("keyboard");
  68. var branch0 = Blockly.Python.statementToCode(block, 'DO0');
  69. var branch1 = Blockly.Python.statementToCode(block, 'DO1');
  70. var code = "if ((currtouched & _BV(" + key + "))&& !(lasttouched & _BV(" + key + "))){\n" + branch0 +
  71. " lasttouched = currtouched;\n" +
  72. "}\n" +
  73. "if ((lasttouched & _BV(" + key + ")) && !(currtouched & _BV(" + key + ")) ) {\n" + branch1 +
  74. " lasttouched = 0;\n" +
  75. "}\n";
  76. return code;
  77. }