music.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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['MusicMidiSetup'] = function(block) {
  12. Blockly.Python.addInclude('SPI', '#include <SPI.h>');
  13. Blockly.Python.addInclude('SD', '#include <SD.h>');
  14. Blockly.Python.addInclude('MusicPlayer', '#include <MusicPlayer.h>\n');
  15. var setup = 'player.beginInMidiFmt();\n' +
  16. ' player.midiSetInit(' + block.getFieldValue('INSTRUMENT') + ', ' + block.getFieldValue('VOLUME') + ');\n';
  17. Blockly.Python.addSetup('music_setup', setup);
  18. var code = '';
  19. return code;
  20. };
  21. Blockly.Python['MusicSetVolume'] = function(block) {
  22. var musicVolume = block.getFieldValue('VOLUME');
  23. var code = 'player.setVolume(' + musicVolume + ');\n';
  24. return code;
  25. };
  26. Blockly.Python['MusicSetInstrument'] = function(block) {
  27. var musicInstrument = block.getFieldValue('INSTRUMENT');
  28. var code = 'player.midiSetInstrument(' + musicInstrument + ');\n';
  29. return code;
  30. };
  31. Blockly.Python['MusicNote'] = function(block) {
  32. var musicNote = block.getFieldValue('NOTE');
  33. var code = musicNote;
  34. return [code, Blockly.Python.ORDER_ATOMIC];
  35. };
  36. Blockly.Python['MusicBeat'] = function(block) {
  37. var musicBeat = block.getFieldValue('BEAT');
  38. var code = musicBeat;
  39. return [code, Blockly.Python.ORDER_ATOMIC];
  40. };
  41. Blockly.Python['MusicPlayTone'] = function(block) {
  42. var musicNote = Blockly.Python.valueToCode(block, 'NOTE', Blockly.Python.ORDER_ATOMIC);
  43. var musicDuration = Blockly.Python.valueToCode(block, 'DURATION', Blockly.Python.ORDER_ATOMIC);
  44. var code = 'player.midiPlay(' + musicNote + ', ' + musicDuration + ');\n';
  45. return code;
  46. };
  47. Blockly.Python['MusicRest'] = function(block) {
  48. var musicBeat = Blockly.Python.valueToCode(block, 'BEAT', Blockly.Python.ORDER_ATOMIC);
  49. var code = 'delay(' + musicBeat + ');\n';
  50. return code;
  51. };
  52. Blockly.Python['Music_mp3_setup'] = function(block) {
  53. Blockly.Python.addInclude('SPI', '#include <SPI.h>');
  54. Blockly.Python.addInclude('SD', '#include <SD.h>');
  55. // Blockly.Python.addInclude('arduino', '#include <arduino.h>');
  56. Blockly.Python.addInclude('MusicPlayer', '#include <MusicPlayer.h>\n');
  57. var vol = block.getFieldValue("SETVOL") || 50;
  58. Blockly.Python.addDeclaration("mp3_dec","int _musicPlayerVolume = "+ vol+ ";\n"+
  59. "int _musicVolumeMappingRead, _musicVolumeMappingSet;\n")
  60. Blockly.Python.addSetup("mp3_setup", " Serial.begin(9600);\n" +
  61. " delay(2000);\n" +
  62. " player.keyDisable(); //keys disable first;\n" +
  63. // " player.digitalControlEnable();//enable to scan the 0-7\n" +
  64. // " player.analogControlEnable(); //enable to scan the A4/A5\n" +
  65. " player.begin(); //will initialize the hardware and set default mode to be normal.\n" +
  66. " player.setVolume(_musicPlayerVolume);\n");
  67. Blockly.Python.addFunction("func_musicPlaySong", "/*\n"+
  68. "* Music Function: Music Set Play Song\n"+
  69. "*/\n"+
  70. "void _musicPlaySong(String songname) {\n"+
  71. " int songNameLength = songname.length() + 1;\n"+
  72. " char songNameBuf[songNameLength];\n"+
  73. " songname.toCharArray(songNameBuf, songNameLength);\n"+
  74. "\n"+
  75. " delay(50);\n"+
  76. " player.opPause();\n"+
  77. " player.setPlayMode(PM_REPEAT_ONE);\n"+
  78. " player.playOne(songNameBuf);\n"+
  79. " player.play();\n"+
  80. " delay(50);\n"+
  81. "}")
  82. Blockly.Python.addFunction("func__musicOperation", "/* \n"+
  83. " * Music Function: Music Operation\n"+
  84. " */\n"+
  85. "\n"+
  86. "void _musicOperation(int operation) {\n"+
  87. " _musicVolumeMappingRead = map(_musicPlayerVolume, 110, 10, 0, 10);\n"+
  88. "\n"+
  89. " if (operation == 0) { // 0: Play song\n"+
  90. " delay(100);\n"+
  91. " player.opResume();\n"+
  92. " } else if (operation == 1) { // 1: Pause song\n"+
  93. " delay(100);\n"+
  94. " player.opPause();\n"+
  95. " } else if (operation == 2) { // 2: Previous song\n"+
  96. " delay(100);\n"+
  97. " for (int a = 0; a < 1; a++) {\n"+
  98. " player.opPreviousSong();\n"+
  99. " player.play();\n"+
  100. " }\n"+
  101. " } else if (operation == 3) { // 3: Next song\n"+
  102. " delay(100);\n"+
  103. " for (int a = 0; a < 1; a++) {\n"+
  104. " player.opNextSong();\n"+
  105. " player.play();\n"+
  106. " }\n"+
  107. " } else if (operation == 4) { // 4: Volume Up\n"+
  108. "\n"+
  109. " _musicPlayerVolume = _musicPlayerVolume - 10;\n"+
  110. " if ( _musicPlayerVolume <= 10 ) {\n"+
  111. " _musicPlayerVolume = 10;\n"+
  112. " }\n"+
  113. " delay(100);\n"+
  114. " player.setVolume(_musicPlayerVolume);\n"+
  115. " } else if (operation == 5) { // 5: Volume Down\n"+
  116. "\n"+
  117. " _musicPlayerVolume = _musicPlayerVolume + 10;\n"+
  118. " if ( _musicPlayerVolume >= 110 ) {\n"+
  119. " _musicPlayerVolume = 110;\n"+
  120. " }\n"+
  121. " delay(100);\n"+
  122. " player.setVolume(_musicPlayerVolume);\n"+
  123. " } else if (operation == 6) { // 6: Play all song\n"+
  124. " delay(100);\n"+
  125. " player.setPlayMode(PM_REPEAT_LIST);\n"+
  126. " player.scanAndPlayAll();\n"+
  127. " player.play();\n"+
  128. " delay(100);\n"+
  129. " player.play();\n"+
  130. " }\n"+
  131. "}\n")
  132. Blockly.Python.addFunction("func_musicSetVolume","/* \n"+
  133. " * Music Function: Music Set Volume\n"+
  134. " */\n"+
  135. "\n"+
  136. "void _musicSetVolume(int volume) {\n"+
  137. " _musicVolumeMappingSet = map(volume, 0, 10, 110, 10);\n"+
  138. " player.setVolume(_musicVolumeMappingSet);\n"+
  139. "\n"+
  140. " _musicPlayerVolume = _musicVolumeMappingSet;\n"+
  141. "}\n")
  142. return "";
  143. }
  144. Blockly.Python['Music_mp3_playOneWithName'] = function(block) {
  145. var music_name = block.getFieldValue("MUSIC_NAME");
  146. var code = "_musicPlaySong(\"" + music_name + "\");\n";
  147. return code;
  148. }
  149. Blockly.Python['Music_mp3_playAllMusic'] = function(block) {
  150. // var type = block.getFieldValue("TYPE");
  151. Blockly.Python.addSetup("playMode", "player.setPlayMode(PM_NORMAL_PLAY);\n ");
  152. var code = "player.scanAndPlayAll();\n" +
  153. "player.play();\n";
  154. return code;
  155. }
  156. Blockly.Python['Music_mp3_playSetting'] = function(block) {
  157. var setting = block.getFieldValue("SETTING");
  158. // switch (setting) {
  159. // case "pause":
  160. // code = "player.opPause();\n";
  161. // break;
  162. // case "resume":
  163. // code = "player.opResume();\n";
  164. // break;
  165. // case "stop":
  166. // code = "player.opStop();\n";
  167. // break;
  168. // case "next":
  169. // code = "player.opNextSong();\n";
  170. // break;
  171. // case "pre":
  172. // code = "player.opPreviousSong();\n";
  173. // break;
  174. // default:
  175. // break;
  176. // }
  177. var code = "_musicOperation("+setting+");\n"
  178. return code;
  179. }
  180. Blockly.Python['Music_mp3_getCurrentVolume'] = function(block) {
  181. var code = "_musicVolumeMappingRead";
  182. return [code, Blockly.Python.ORDER_ATOMIC];
  183. }
  184. Blockly.Python['Music_mp3_setCurrentVolume'] = function(block) {
  185. var vol_level = block.getFieldValue("VOLLEVEL")
  186. var code = "_musicSetVolume("+vol_level+");\n";
  187. return code;
  188. }