You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 satır
7.3KB

  1. /** Modified from original Node-Red source, for audio system visualization
  2. * vim: set ts=4:
  3. * Copyright 2013 IBM Corp.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. **/
  17. var RED = (function() {
  18. $('#btn-keyboard-shortcuts').click(function(){showHelp();});
  19. function hideDropTarget() {
  20. $("#dropTarget").hide();
  21. RED.keyboard.remove(/* ESCAPE */ 27);
  22. }
  23. $('#chart').on("dragenter",function(event) {
  24. if ($.inArray("text/plain",event.originalEvent.dataTransfer.types) != -1) {
  25. $("#dropTarget").css({display:'table'});
  26. RED.keyboard.add(/* ESCAPE */ 27,hideDropTarget);
  27. }
  28. });
  29. $('#dropTarget').on("dragover",function(event) {
  30. if ($.inArray("text/plain",event.originalEvent.dataTransfer.types) != -1) {
  31. event.preventDefault();
  32. }
  33. })
  34. .on("dragleave",function(event) {
  35. hideDropTarget();
  36. })
  37. .on("drop",function(event) {
  38. var data = event.originalEvent.dataTransfer.getData("text/plain");
  39. hideDropTarget();
  40. RED.view.importNodes(data);
  41. event.preventDefault();
  42. });
  43. function save(force) {
  44. RED.storage.update();
  45. if (1) {
  46. var nns = RED.nodes.createCompleteNodeSet();
  47. // sort by horizontal position, plus slight vertical position,
  48. // for well defined update order that follows signal flow
  49. nns.sort(function(a,b){ return (a.x + a.y/250) - (b.x + b.y/250); });
  50. //console.log(JSON.stringify(nns));
  51. var cpp = "#include <Audio.h>\n#include <Wire.h>\n"
  52. + "#include <SPI.h>\n#include <SD.h>\n#include <SerialFlash.h>\n\n"
  53. + "// GUItool: begin automatically generated code\n";
  54. // generate code for all audio processing nodes
  55. for (var i=0; i<nns.length; i++) {
  56. var n = nns[i];
  57. var node = RED.nodes.node(n.id);
  58. if (node && (node.outputs > 0 || node._def.inputs > 0)) {
  59. cpp += n.type + " ";
  60. for (var j=n.type.length; j<24; j++) cpp += " ";
  61. var name = (n.name ? n.name : n.id);
  62. name = name.replace(" ", "_").replace("+", "_").replace("-", "_");
  63. cpp += name + "; ";
  64. for (var j=n.id.length; j<14; j++) cpp += " ";
  65. cpp += "//xy=" + n.x + "," + n.y + "\n";
  66. }
  67. }
  68. // generate code for all connections (aka wires or links)
  69. var cordcount = 1;
  70. for (var i=0; i<nns.length; i++) {
  71. var n = nns[i];
  72. if (n.wires) {
  73. for (var j=0; j<n.wires.length; j++) {
  74. var wires = n.wires[j];
  75. if (!wires) continue;
  76. for (var k=0; k<wires.length; k++) {
  77. var wire = n.wires[j][k];
  78. if (wire) {
  79. var parts = wire.split(":");
  80. if (parts.length == 2) {
  81. cpp += "AudioConnection patchCord" + cordcount + "(";
  82. var src = RED.nodes.node(n.id);
  83. var dst = RED.nodes.node(parts[0]);
  84. if (j == 0 && parts[1] == 0 && src && src.outputs == 1 && dst && dst._def.inputs == 1) {
  85. cpp += n.id + ", " + parts[0];
  86. } else {
  87. cpp += n.id + ", " + j + ", " + parts[0] + ", " + parts[1];
  88. }
  89. cpp += ");\n";
  90. cordcount++;
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. // generate code for all control nodes (no inputs or outputs)
  98. for (var i=0; i<nns.length; i++) {
  99. var n = nns[i];
  100. var node = RED.nodes.node(n.id);
  101. if (node && node.outputs == 0 && node._def.inputs == 0) {
  102. cpp += n.type + " ";
  103. for (var j=n.type.length; j<24; j++) cpp += " ";
  104. cpp += n.id + "; ";
  105. for (var j=n.id.length; j<14; j++) cpp += " ";
  106. cpp += "//xy=" + n.x + "," + n.y + "\n";
  107. }
  108. }
  109. cpp += "// GUItool: end automatically generated code\n";
  110. //console.log(cpp);
  111. RED.view.state(RED.state.EXPORT);
  112. RED.view.getForm('dialog-form', 'export-clipboard-dialog', function (d, f) {
  113. $("#node-input-export").val(cpp).focus(function() {
  114. var textarea = $(this);
  115. textarea.select();
  116. textarea.mouseup(function() {
  117. textarea.unbind("mouseup");
  118. return false;
  119. });
  120. }).focus();
  121. $( "#dialog" ).dialog("option","title","Export to Arduino").dialog( "open" );
  122. });
  123. //RED.view.dirty(false);
  124. }
  125. }
  126. $('#btn-deploy').click(function() { save(); });
  127. $( "#node-dialog-confirm-deploy" ).dialog({
  128. title: "Confirm deploy",
  129. modal: true,
  130. autoOpen: false,
  131. width: 530,
  132. height: 230,
  133. buttons: [
  134. {
  135. text: "Confirm deploy",
  136. click: function() {
  137. save(true);
  138. $( this ).dialog( "close" );
  139. }
  140. },
  141. {
  142. text: "Cancel",
  143. click: function() {
  144. $( this ).dialog( "close" );
  145. }
  146. }
  147. ]
  148. });
  149. // from http://css-tricks.com/snippets/javascript/get-url-variables/
  150. function getQueryVariable(variable) {
  151. var query = window.location.search.substring(1);
  152. var vars = query.split("&");
  153. for (var i=0;i<vars.length;i++) {
  154. var pair = vars[i].split("=");
  155. if(pair[0] == variable){return pair[1];}
  156. }
  157. return(false);
  158. }
  159. function loadNodes() {
  160. $(".palette-scroll").show();
  161. $("#palette-search").show();
  162. RED.storage.load();
  163. RED.view.redraw();
  164. setTimeout(function() {
  165. $("#btn-deploy").removeClass("disabled").addClass("btn-danger");
  166. $("#btn-import").removeClass("disabled").addClass("btn-success");
  167. }, 1500);
  168. $('#btn-deploy').click(function() { save(); });
  169. // if the query string has ?info=className, populate info tab
  170. var info = getQueryVariable("info");
  171. if (info) {
  172. RED.sidebar.info.setHelpContent('', info);
  173. }
  174. }
  175. $('#btn-node-status').click(function() {toggleStatus();});
  176. var statusEnabled = false;
  177. function toggleStatus() {
  178. var btnStatus = $("#btn-node-status");
  179. statusEnabled = btnStatus.toggleClass("active").hasClass("active");
  180. RED.view.status(statusEnabled);
  181. }
  182. function showHelp() {
  183. var dialog = $('#node-help');
  184. //$("#node-help").draggable({
  185. // handle: ".modal-header"
  186. //});
  187. dialog.on('show',function() {
  188. RED.keyboard.disable();
  189. });
  190. dialog.on('hidden',function() {
  191. RED.keyboard.enable();
  192. });
  193. dialog.modal();
  194. }
  195. $(function() {
  196. $(".palette-spinner").show();
  197. // server test switched off - test purposes only
  198. var patt = new RegExp(/^[http|https]/);
  199. var server = false && patt.test(location.protocol);
  200. if (!server) {
  201. var data = $.parseJSON($("script[data-container-name|='NodeDefinitions']").html());
  202. var nodes = data["nodes"];
  203. $.each(nodes, function (key, val) {
  204. RED.nodes.registerType(val["type"], val["data"]);
  205. });
  206. RED.keyboard.add(/* ? */ 191, {shift: true}, function () {
  207. showHelp();
  208. d3.event.preventDefault();
  209. });
  210. loadNodes();
  211. $(".palette-spinner").hide();
  212. } else {
  213. $.ajaxSetup({beforeSend: function(xhr){
  214. if (xhr.overrideMimeType) {
  215. xhr.overrideMimeType("application/json");
  216. }
  217. }});
  218. $.getJSON( "resources/nodes_def.json", function( data ) {
  219. var nodes = data["nodes"];
  220. $.each(nodes, function(key, val) {
  221. RED.nodes.registerType(val["type"], val["data"]);
  222. });
  223. RED.keyboard.add(/* ? */ 191,{shift:true},function(){showHelp();d3.event.preventDefault();});
  224. loadNodes();
  225. $(".palette-spinner").hide();
  226. })
  227. }
  228. });
  229. return {
  230. };
  231. })();