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.

229 line
6.4KB

  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. cpp += n.id + "; ";
  62. for (var j=n.id.length; j<14; j++) cpp += " ";
  63. cpp += "//xy=" + n.x + "," + n.y + "\n";
  64. }
  65. }
  66. // generate code for all connections (aka wires or links)
  67. var cordcount = 1;
  68. for (var i=0; i<nns.length; i++) {
  69. var n = nns[i];
  70. if (n.wires) {
  71. for (var j=0; j<n.wires.length; j++) {
  72. var wires = n.wires[j];
  73. if (!wires) continue;
  74. for (var k=0; k<wires.length; k++) {
  75. var wire = n.wires[j][k];
  76. if (wire) {
  77. var parts = wire.split(":");
  78. if (parts.length == 2) {
  79. cpp += "AudioConnection patchCord" + cordcount + "(";
  80. var src = RED.nodes.node(n.id);
  81. var dst = RED.nodes.node(parts[0]);
  82. if (j == 0 && parts[1] == 0 && src && src.outputs == 1 && dst && dst._def.inputs == 1) {
  83. cpp += n.id + ", " + parts[0];
  84. } else {
  85. cpp += n.id + ", " + j + ", " + parts[0] + ", " + parts[1];
  86. }
  87. cpp += ");\n";
  88. cordcount++;
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }
  95. // generate code for all control nodes (no inputs or outputs)
  96. for (var i=0; i<nns.length; i++) {
  97. var n = nns[i];
  98. var node = RED.nodes.node(n.id);
  99. if (node && node.outputs == 0 && node._def.inputs == 0) {
  100. cpp += n.type + " ";
  101. for (var j=n.type.length; j<24; j++) cpp += " ";
  102. cpp += n.id + "; ";
  103. for (var j=n.id.length; j<14; j++) cpp += " ";
  104. cpp += "//xy=" + n.x + "," + n.y + "\n";
  105. }
  106. }
  107. cpp += "// GUItool: end automatically generated code\n";
  108. //console.log(cpp);
  109. RED.view.state(RED.state.EXPORT);
  110. $("#dialog-form").html($("script[data-template-name='export-clipboard-dialog']").html());
  111. $("#node-input-export").val(cpp);
  112. $("#node-input-export").focus(function() {
  113. var textarea = $(this);
  114. textarea.select();
  115. textarea.mouseup(function() {
  116. textarea.unbind("mouseup");
  117. return false;
  118. });
  119. });
  120. $( "#dialog" ).dialog("option","title","Export to Arduino").dialog( "open" );
  121. $("#node-input-export").focus();
  122. //RED.view.dirty(false);
  123. }
  124. }
  125. $('#btn-deploy').click(function() { save(); });
  126. $( "#node-dialog-confirm-deploy" ).dialog({
  127. title: "Confirm deploy",
  128. modal: true,
  129. autoOpen: false,
  130. width: 530,
  131. height: 230,
  132. buttons: [
  133. {
  134. text: "Confirm deploy",
  135. click: function() {
  136. save(true);
  137. $( this ).dialog( "close" );
  138. }
  139. },
  140. {
  141. text: "Cancel",
  142. click: function() {
  143. $( this ).dialog( "close" );
  144. }
  145. }
  146. ]
  147. });
  148. // from http://css-tricks.com/snippets/javascript/get-url-variables/
  149. function getQueryVariable(variable) {
  150. var query = window.location.search.substring(1);
  151. var vars = query.split("&");
  152. for (var i=0;i<vars.length;i++) {
  153. var pair = vars[i].split("=");
  154. if(pair[0] == variable){return pair[1];}
  155. }
  156. return(false);
  157. }
  158. function loadNodes() {
  159. //$.get('list.html', function(data) {
  160. //$("body").append(data);
  161. //setTimeout(function() {
  162. $(".palette-spinner").hide();
  163. $(".palette-scroll").show();
  164. $("#palette-search").show();
  165. RED.storage.load();
  166. RED.view.redraw();
  167. setTimeout(function() {
  168. $("#btn-deploy").removeClass("disabled").addClass("btn-danger");
  169. }, 1500);
  170. $('#btn-deploy').click(function() { save(); });
  171. // if the query string has ?info=className, populate info tab
  172. var info = getQueryVariable("info");
  173. if (info) {
  174. $("#tab-info").html('<div class="node-help">'
  175. +($("script[data-help-name|='"+info+"']").html()||"")+"</div>");
  176. }
  177. //}, 100);
  178. //}, "html");
  179. }
  180. $('#btn-node-status').click(function() {toggleStatus();});
  181. var statusEnabled = false;
  182. function toggleStatus() {
  183. var btnStatus = $("#btn-node-status");
  184. statusEnabled = btnStatus.toggleClass("active").hasClass("active");
  185. RED.view.status(statusEnabled);
  186. }
  187. function showHelp() {
  188. var dialog = $('#node-help');
  189. //$("#node-help").draggable({
  190. // handle: ".modal-header"
  191. //});
  192. dialog.on('show',function() {
  193. RED.keyboard.disable();
  194. });
  195. dialog.on('hidden',function() {
  196. RED.keyboard.enable();
  197. });
  198. dialog.modal();
  199. }
  200. $(function() {
  201. RED.keyboard.add(/* ? */ 191,{shift:true},function(){showHelp();d3.event.preventDefault();});
  202. loadNodes();
  203. });
  204. return {
  205. };
  206. })();