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.

238 lines
6.6KB

  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 (RED.view.dirty()) {
  46. if (!force) {
  47. var invalid = false;
  48. var unknownNodes = [];
  49. RED.nodes.eachNode(function(node) {
  50. invalid = invalid || !node.valid;
  51. if (node.type === "unknown") {
  52. if (unknownNodes.indexOf(node.name) == -1) {
  53. unknownNodes.push(node.name);
  54. }
  55. invalid = true;
  56. }
  57. });
  58. if (invalid) {
  59. if (unknownNodes.length > 0) {
  60. $( "#node-dialog-confirm-deploy-config" ).hide();
  61. $( "#node-dialog-confirm-deploy-unknown" ).show();
  62. var list = "<li>"+unknownNodes.join("</li><li>")+"</li>";
  63. $( "#node-dialog-confirm-deploy-unknown-list" ).html(list);
  64. } else {
  65. $( "#node-dialog-confirm-deploy-config" ).show();
  66. $( "#node-dialog-confirm-deploy-unknown" ).hide();
  67. }
  68. $( "#node-dialog-confirm-deploy" ).dialog( "open" );
  69. return;
  70. }
  71. }
  72. var nns = RED.nodes.createCompleteNodeSet();
  73. // sort by horizontal position, plus slight vertical position,
  74. // for well defined update order that follows signal flow
  75. nns.sort(function(a,b){ return (a.x + a.y/250) - (b.x + b.y/250); });
  76. //console.log(JSON.stringify(nns));
  77. var cpp = "#include <Audio.h>\n#include <Wire.h>\n"
  78. + "#include <SPI.h>\n#include <SD.h>\n\n"
  79. + "// GUItool: begin automatically generated code\n";
  80. // generate code for all audio processing nodes
  81. for (var i=0; i<nns.length; i++) {
  82. var n = nns[i];
  83. var node = RED.nodes.node(n.id);
  84. if (node && (node.outputs > 0 || node._def.inputs > 0)) {
  85. cpp += n.type + " ";
  86. for (var j=n.type.length; j<24; j++) cpp += " ";
  87. cpp += n.id + "; ";
  88. for (var j=n.id.length; j<14; j++) cpp += " ";
  89. cpp += "//xy=" + n.x + "," + n.y + "\n";
  90. }
  91. }
  92. // generate code for all connections (aka wires or links)
  93. var cordcount = 1;
  94. for (var i=0; i<nns.length; i++) {
  95. var n = nns[i];
  96. if (n.wires) {
  97. for (var j=0; j<n.wires.length; j++) {
  98. var wires = n.wires[j];
  99. if (!wires) continue;
  100. for (var k=0; k<wires.length; k++) {
  101. var wire = n.wires[j][k];
  102. if (wire) {
  103. var parts = wire.split(":");
  104. if (parts.length == 2) {
  105. cpp += "AudioConnection patchCord" + cordcount + "(";
  106. var src = RED.nodes.node(n.id);
  107. var dst = RED.nodes.node(parts[0]);
  108. if (j == 0 && parts[1] == 0 && src && src.outputs == 1 && dst && dst._def.inputs == 1) {
  109. cpp += n.id + ", " + parts[0];
  110. } else {
  111. cpp += n.id + ", " + j + ", " + parts[0] + ", " + parts[1];
  112. }
  113. cpp += ");\n";
  114. cordcount++;
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. // generate code for all control nodes (no inputs or outputs)
  122. for (var i=0; i<nns.length; i++) {
  123. var n = nns[i];
  124. var node = RED.nodes.node(n.id);
  125. if (node && node.outputs == 0 && node._def.inputs == 0) {
  126. cpp += n.type + " ";
  127. for (var j=n.type.length; j<24; j++) cpp += " ";
  128. cpp += n.id + "; ";
  129. for (var j=n.id.length; j<14; j++) cpp += " ";
  130. cpp += "//xy=" + n.x + "," + n.y + "\n";
  131. }
  132. }
  133. cpp += "// GUItool: end automatically generated code\n";
  134. //console.log(cpp);
  135. RED.view.state(RED.state.EXPORT);
  136. $("#dialog-form").html($("script[data-template-name='export-clipboard-dialog']").html());
  137. $("#node-input-export").val(cpp);
  138. $("#node-input-export").focus(function() {
  139. var textarea = $(this);
  140. textarea.select();
  141. textarea.mouseup(function() {
  142. textarea.unbind("mouseup");
  143. return false;
  144. });
  145. });
  146. $( "#dialog" ).dialog("option","title","Export to Arduino").dialog( "open" );
  147. $("#node-input-export").focus();
  148. //RED.view.dirty(false);
  149. }
  150. }
  151. $('#btn-deploy').click(function() { save(); });
  152. $( "#node-dialog-confirm-deploy" ).dialog({
  153. title: "Confirm deploy",
  154. modal: true,
  155. autoOpen: false,
  156. width: 530,
  157. height: 230,
  158. buttons: [
  159. {
  160. text: "Confirm deploy",
  161. click: function() {
  162. save(true);
  163. $( this ).dialog( "close" );
  164. }
  165. },
  166. {
  167. text: "Cancel",
  168. click: function() {
  169. $( this ).dialog( "close" );
  170. }
  171. }
  172. ]
  173. });
  174. function loadNodes() {
  175. console.log("loadNodes");
  176. $.get('list.html', function(data) {
  177. console.log("loadNodes complete");
  178. $("body").append(data);
  179. $(".palette-spinner").hide();
  180. $(".palette-scroll").show();
  181. $("#palette-search").show();
  182. RED.storage.load();
  183. RED.view.redraw();
  184. setTimeout(function() {
  185. $("#btn-deploy").removeClass("disabled").addClass("btn-danger");
  186. }, 1500);
  187. }, "html");
  188. }
  189. $('#btn-node-status').click(function() {toggleStatus();});
  190. var statusEnabled = false;
  191. function toggleStatus() {
  192. var btnStatus = $("#btn-node-status");
  193. statusEnabled = btnStatus.toggleClass("active").hasClass("active");
  194. RED.view.status(statusEnabled);
  195. }
  196. function showHelp() {
  197. var dialog = $('#node-help');
  198. //$("#node-help").draggable({
  199. // handle: ".modal-header"
  200. //});
  201. dialog.on('show',function() {
  202. RED.keyboard.disable();
  203. });
  204. dialog.on('hidden',function() {
  205. RED.keyboard.enable();
  206. });
  207. dialog.modal();
  208. }
  209. $(function() {
  210. RED.keyboard.add(/* ? */ 191,{shift:true},function(){showHelp();d3.event.preventDefault();});
  211. loadNodes();
  212. });
  213. return {
  214. };
  215. })();