Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

280 lines
7.9KB

  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 make_name(n) {
  44. var name = (n.name ? n.name : n.id);
  45. name = name.replace(" ", "_").replace("+", "_").replace("-", "_");
  46. return name
  47. }
  48. function save(force) {
  49. RED.storage.update();
  50. if (RED.nodes.hasIO()) {
  51. var nns = RED.nodes.createCompleteNodeSet();
  52. // sort by horizontal position, plus slight vertical position,
  53. // for well defined update order that follows signal flow
  54. nns.sort(function(a,b){ return (a.x + a.y/250) - (b.x + b.y/250); });
  55. //console.log(JSON.stringify(nns));
  56. var cpp = "#include <Audio.h>\n#include <Wire.h>\n"
  57. + "#include <SPI.h>\n#include <SD.h>\n#include <SerialFlash.h>\n\n"
  58. + "// GUItool: begin automatically generated code\n";
  59. // generate code for all audio processing nodes
  60. for (var i=0; i<nns.length; i++) {
  61. var n = nns[i];
  62. var node = RED.nodes.node(n.id);
  63. if (node && (node.outputs > 0 || node._def.inputs > 0)) {
  64. cpp += n.type + " ";
  65. for (var j=n.type.length; j<24; j++) cpp += " ";
  66. var name = make_name(n)
  67. cpp += name + "; ";
  68. for (var j=n.id.length; j<14; j++) cpp += " ";
  69. cpp += "//xy=" + n.x + "," + n.y + "\n";
  70. }
  71. }
  72. // generate code for all connections (aka wires or links)
  73. var cordcount = 1;
  74. for (var i=0; i<nns.length; i++) {
  75. var n = nns[i];
  76. if (n.wires) {
  77. for (var j=0; j<n.wires.length; j++) {
  78. var wires = n.wires[j];
  79. if (!wires) continue;
  80. for (var k=0; k<wires.length; k++) {
  81. var wire = n.wires[j][k];
  82. if (wire) {
  83. var parts = wire.split(":");
  84. if (parts.length == 2) {
  85. cpp += "AudioConnection patchCord" + cordcount + "(";
  86. var src = RED.nodes.node(n.id);
  87. var dst = RED.nodes.node(parts[0]);
  88. var src_name = make_name(src);
  89. var dst_name = make_name(dst);
  90. if (j == 0 && parts[1] == 0 && src && src.outputs == 1 && dst && dst._def.inputs == 1) {
  91. cpp += src_name + ", " + dst_name;
  92. } else {
  93. cpp += src_name + ", " + j + ", " + dst_name + ", " + parts[1];
  94. }
  95. cpp += ");\n";
  96. cordcount++;
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. // generate code for all control nodes (no inputs or outputs)
  104. for (var i=0; i<nns.length; i++) {
  105. var n = nns[i];
  106. var node = RED.nodes.node(n.id);
  107. if (node && node.outputs == 0 && node._def.inputs == 0) {
  108. cpp += n.type + " ";
  109. for (var j=n.type.length; j<24; j++) cpp += " ";
  110. cpp += n.id + "; ";
  111. for (var j=n.id.length; j<14; j++) cpp += " ";
  112. cpp += "//xy=" + n.x + "," + n.y + "\n";
  113. }
  114. }
  115. cpp += "// GUItool: end automatically generated code\n";
  116. //console.log(cpp);
  117. RED.view.state(RED.state.EXPORT);
  118. RED.view.getForm('dialog-form', 'export-clipboard-dialog', function (d, f) {
  119. $("#node-input-export").val(cpp).focus(function() {
  120. var textarea = $(this);
  121. textarea.select();
  122. textarea.mouseup(function() {
  123. textarea.unbind("mouseup");
  124. return false;
  125. });
  126. }).focus();
  127. $( "#dialog" ).dialog("option","title","Export to Arduino").dialog( "open" );
  128. });
  129. //RED.view.dirty(false);
  130. } else {
  131. $( "#node-dialog-error-deploy" ).dialog({
  132. title: "Error exporting data to Arduino IDE",
  133. modal: true,
  134. autoOpen: false,
  135. width: 410,
  136. height: 245,
  137. buttons: [{
  138. text: "Ok",
  139. click: function() {
  140. $( this ).dialog( "close" );
  141. }
  142. }]
  143. }).dialog("open");
  144. }
  145. }
  146. $('#btn-deploy').click(function() { save(); });
  147. $( "#node-dialog-confirm-deploy" ).dialog({
  148. title: "Confirm deploy",
  149. modal: true,
  150. autoOpen: false,
  151. width: 530,
  152. height: 230,
  153. buttons: [
  154. {
  155. text: "Confirm deploy",
  156. click: function() {
  157. save(true);
  158. $( this ).dialog( "close" );
  159. }
  160. },
  161. {
  162. text: "Cancel",
  163. click: function() {
  164. $( this ).dialog( "close" );
  165. }
  166. }
  167. ]
  168. });
  169. // from http://css-tricks.com/snippets/javascript/get-url-variables/
  170. function getQueryVariable(variable) {
  171. var query = window.location.search.substring(1);
  172. var vars = query.split("&");
  173. for (var i=0;i<vars.length;i++) {
  174. var pair = vars[i].split("=");
  175. if(pair[0] == variable){return pair[1];}
  176. }
  177. return(false);
  178. }
  179. function loadNodes() {
  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. $("#btn-import").removeClass("disabled").addClass("btn-success");
  187. }, 1500);
  188. $('#btn-deploy').click(function() { save(); });
  189. // if the query string has ?info=className, populate info tab
  190. var info = getQueryVariable("info");
  191. if (info) {
  192. RED.sidebar.info.setHelpContent('', info);
  193. }
  194. }
  195. $('#btn-node-status').click(function() {toggleStatus();});
  196. var statusEnabled = false;
  197. function toggleStatus() {
  198. var btnStatus = $("#btn-node-status");
  199. statusEnabled = btnStatus.toggleClass("active").hasClass("active");
  200. RED.view.status(statusEnabled);
  201. }
  202. function showHelp() {
  203. var dialog = $('#node-help');
  204. //$("#node-help").draggable({
  205. // handle: ".modal-header"
  206. //});
  207. dialog.on('show',function() {
  208. RED.keyboard.disable();
  209. });
  210. dialog.on('hidden',function() {
  211. RED.keyboard.enable();
  212. });
  213. dialog.modal();
  214. }
  215. $(function() {
  216. $(".palette-spinner").show();
  217. // server test switched off - test purposes only
  218. var patt = new RegExp(/^[http|https]/);
  219. var server = false && patt.test(location.protocol);
  220. if (!server) {
  221. var metaData = $.parseJSON($("script[data-container-name|='InputOutputCompatibilityMetadata']").html());
  222. requirements = metaData["requirements"];
  223. var data = $.parseJSON($("script[data-container-name|='NodeDefinitions']").html());
  224. var nodes = data["nodes"];
  225. $.each(nodes, function (key, val) {
  226. RED.nodes.registerType(val["type"], val["data"]);
  227. });
  228. RED.keyboard.add(/* ? */ 191, {shift: true}, function () {
  229. showHelp();
  230. d3.event.preventDefault();
  231. });
  232. loadNodes();
  233. $(".palette-spinner").hide();
  234. } else {
  235. $.ajaxSetup({beforeSend: function(xhr){
  236. if (xhr.overrideMimeType) {
  237. xhr.overrideMimeType("application/json");
  238. }
  239. }});
  240. $.getJSON( "resources/nodes_def.json", function( data ) {
  241. var nodes = data["nodes"];
  242. $.each(nodes, function(key, val) {
  243. RED.nodes.registerType(val["type"], val["data"]);
  244. });
  245. RED.keyboard.add(/* ? */ 191,{shift:true},function(){showHelp();d3.event.preventDefault();});
  246. loadNodes();
  247. $(".palette-spinner").hide();
  248. })
  249. }
  250. });
  251. return {
  252. };
  253. })();