music_old.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @license Licensed under the Apache License, Version 2.0 (the "License"):
  3. * http://www.apache.org/licenses/LICENSE-2.0
  4. */
  5. /**
  6. * @fileoverview Code generator for the test 2 blocks.
  7. */
  8. /*'use strict';
  9. goog.provide('Blockly.Python.music');
  10. goog.require('Blockly.Python');
  11. Blockly.Python['music_setup'] = function(block) {
  12. var decl = '#include <SPI.h>\n' +
  13. '#include <SD.h>\n' +
  14. '#include <MusicPlayer.h>\n' +
  15. '#include <Wire.h>\n' +
  16. 'MusicPlayer player;\n';
  17. Blockly.Python.addDeclaration('music_setup_decl', decl);
  18. var setup = 'Serial.begin(9600);\n'+
  19. 'player.beginMidi();\n';
  20. Blockly.Python.addSetup('music_setup', setup);
  21. var func = 'void midiSendByte(char data)\n' +
  22. '{\n' +
  23. ' SPI.transfer(0x00);\n' +
  24. ' SPI.transfer(data);\n' +
  25. '}\n' +
  26. 'void midiWriteData(char cmd, char high, char low)\n' +
  27. '{\n' +
  28. ' while(!digitalRead(VS_DREQ));\n' +
  29. ' digitalWrite(VS_XDCS, LOW);\n' +
  30. ' midiSendByte(cmd);\n' +
  31. ' if((cmd & 0xF0) <= 0xB0 || (cmd & 0xF0) >= 0xE0)\n' +
  32. ' {\n' +
  33. ' midiSendByte(high);\n' +
  34. ' midiSendByte(low);\n' +
  35. ' }\n' +
  36. ' else\n' +
  37. ' {\n' +
  38. ' midiSendByte(high);\n' +
  39. ' }\n' +
  40. ' digitalWrite(VS_XDCS, HIGH);\n' +
  41. '}\n' +
  42. 'void midiNoteOff(char channel, char note, char rate)\n' +
  43. '{\n' +
  44. ' midiWriteData((0x80 | channel), note, rate);\n' +
  45. '}\n' +
  46. 'void midiNoteOn(char channel, char note, char rate)\n' +
  47. '{\n' +
  48. ' midiWriteData((0x90 | channel), note, rate);\n' +
  49. '}\n';
  50. // Blockly.Python.addFunction('func', func);
  51. return "";
  52. };
  53. var original_beat;
  54. Blockly.Python['music_set_beat'] = function(block) {
  55. var dropdown_beat_option = block.getFieldValue('beat_option');
  56. Blockly.Python.addDeclaration('beat', 'float beat;');
  57. original_beat = dropdown_beat_option;
  58. var code = 'beat = '+dropdown_beat_option;
  59. return [code, Blockly.Python.ORDER_NONE];
  60. };
  61. Blockly.Python['music_set_bpm'] = function(block) {
  62. var dropdown_bpm = block.getFieldValue('bpm');
  63. Blockly.Python.addDeclaration('bpm', 'int bpm;');
  64. var code = 'bpm = '+dropdown_beat_option;
  65. return [code, Blockly.Python.ORDER_NONE];
  66. };
  67. Blockly.Python['music_instrument'] = function(block) {
  68. var dropdown_instru = block.getFieldValue('instru');
  69. var code = 'midiWriteData(0xC0, '+dropdown_instru+', 0)';
  70. return [code, Blockly.Python.ORDER_NONE];
  71. };
  72. Blockly.Python['music_rest'] = function(block) {
  73. var text_rest_beat = block.getFieldValue('rest_beat');
  74. // TODO: Assemble JavaScript into code variable.
  75. var code = '...;\n';
  76. return code;
  77. };
  78. Blockly.Python['music_volume'] = function(block) {
  79. var number_music_volume = block.getFieldValue('music_volume');
  80. // TODO: Assemble JavaScript into code variable.
  81. var code = 'midiWriteData(0xB0, 0x07, '+number_music_volume+');\n';
  82. return code;
  83. };
  84. Blockly.Python['music_customize_melody'] = function(block) {
  85. var value_instru = Blockly.Python.valueToCode(block, 'instru', Blockly.Python.ORDER_ATOMIC);
  86. var text_melody_name = block.getFieldValue('melody_name');
  87. var statements_composition = Blockly.Python.statementToCode(block, 'composition');
  88. var code = '...;\n';
  89. return code;
  90. };
  91. Blockly.Python['music_change_tempo'] = function(block) {
  92. var dropdown_beat = block.getFieldValue('beat');
  93. var statements_name = Blockly.Python.statementToCode(block, 'melody');
  94. // TODO: Assemble JavaScript into code variable.
  95. var code = 'beat = '+dropdown_beat+';\n'
  96. + statements_name
  97. + 'beat = '+original_beat+';\n';
  98. return code;
  99. };
  100. Blockly.Python['music_rest'] = function(block) {
  101. var text_name = block.getFieldValue('beat');
  102. var code = ' midiNoteOff(0, 75, 127);\n' +
  103. 'delay(1000*60 / bpm * '+text_name+');\n';
  104. return code;
  105. };
  106. Blockly.Python['music_combination'] = function(block) {
  107. var value_instrument = Blockly.Python.valueToCode(block, 'instrument', Blockly.Python.ORDER_ATOMIC);
  108. var value_note = Blockly.Python.valueToCode(block, 'note', Blockly.Python.ORDER_ATOMIC);
  109. var value_beat = Blockly.Python.valueToCode(block, 'beat', Blockly.Python.ORDER_ATOMIC);
  110. // TODO: Assemble JavaScript into code variable.
  111. var code = '...;\n';
  112. return code;
  113. };
  114. */