My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

configurator.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /**
  2. * configurator.js
  3. *
  4. * Marlin Configuration Utility
  5. * - Web form for entering configuration options
  6. * - A reprap calculator to calculate movement values
  7. * - Uses HTML5 to generate downloadables in Javascript
  8. * - Reads and parses standard configuration files from local folders
  9. *
  10. * Supporting functions
  11. * - Parser to read Marlin Configuration.h and Configuration_adv.h files
  12. * - Utilities to replace values in configuration files
  13. */
  14. "use strict";
  15. $(function(){
  16. var marlin_config = '..';
  17. // Extend String
  18. String.prototype.lpad = function(len, chr) {
  19. if (chr === undefined) { chr = ' '; }
  20. var s = this+'', need = len - s.length;
  21. if (need > 0) { s = new Array(need+1).join(chr) + s; }
  22. return s;
  23. };
  24. String.prototype.prePad = function(len, chr) {
  25. return len ? this.lpad(len, chr) : this;
  26. };
  27. String.prototype.zeroPad = function(len) {
  28. return len ? this.prePad(len, '0') : this;
  29. };
  30. /**
  31. * selectField.addOptions takes an array or keyed object
  32. */
  33. $.fn.extend({
  34. addOptions: function(arrObj) {
  35. return this.each(function() {
  36. var sel = $(this);
  37. var isArr = Object.prototype.toString.call(arrObj) == "[object Array]";
  38. $.each(arrObj, function(k, v) {
  39. sel.append( $('<option>',{value:isArr?v:k}).text(v) );
  40. });
  41. });
  42. }
  43. });
  44. // The app is a singleton
  45. var configuratorApp = (function(){
  46. // private variables and functions go here
  47. var self,
  48. pi2 = Math.PI * 2,
  49. boards_file = 'boards.h',
  50. config_file = 'Configuration.h',
  51. config_adv_file = 'Configuration_adv.h',
  52. $config = $('#config_text'),
  53. $config_adv = $('#config_adv_text'),
  54. boards_list = {},
  55. therms_list = {};
  56. // Return this anonymous object as configuratorApp
  57. return {
  58. my_public_var: 4,
  59. init: function() {
  60. self = this; // a 'this' for use when 'this' is something else
  61. // Make a droppable file uploader
  62. var $uploader = $('#file-upload');
  63. var fileUploader = new BinaryFileUploader({
  64. element: $uploader[0],
  65. onFileLoad: function(file) { console.log(this); self.handleFileLoad(file, $uploader); }
  66. });
  67. if (!fileUploader.hasFileUploaderSupport()) alert('Your browser doesn\'t support the file reading API');
  68. // Read boards.h
  69. boards_list = {};
  70. var errFunc = function(jqXHR, textStatus, errorThrown) {
  71. alert('Failed to load '+this.url+'. Try the file field.');
  72. };
  73. $.ajax({
  74. url: marlin_config+'/'+boards_file,
  75. type: 'GET',
  76. async: false,
  77. cache: false,
  78. success: function(txt) {
  79. // Get all the boards and save them into an object
  80. self.initBoardsFromText(txt);
  81. },
  82. error: errFunc
  83. });
  84. // Read Configuration.h
  85. $.ajax({
  86. url: marlin_config+'/'+config_file,
  87. type: 'GET',
  88. async: false,
  89. cache: false,
  90. success: function(txt) {
  91. // File contents into the textarea
  92. $config.text(txt);
  93. self.initThermistorsFromText(txt);
  94. },
  95. error: errFunc
  96. });
  97. // Read Configuration.h
  98. $.ajax({
  99. url: marlin_config+'/'+config_adv_file,
  100. type: 'GET',
  101. async: false,
  102. cache: false,
  103. success: function(txt) {
  104. // File contents into the textarea
  105. $config_adv.text(txt);
  106. self.setupConfigForm();
  107. },
  108. error: errFunc
  109. });
  110. },
  111. initBoardsFromText: function(txt) {
  112. boards_list = {};
  113. var r, findDef = new RegExp('[ \\t]*#define[ \\t]+(BOARD_[^ \\t]+)[ \\t]+(\\d+)[ \\t]*(//[ \\t]*)?(.+)?', 'gm');
  114. while((r = findDef.exec(txt)) !== null) {
  115. boards_list[r[1]] = r[2].prePad(3, '  ') + " — " + r[4].replace(/\).*/, ')');
  116. }
  117. },
  118. initThermistorsFromText: function(txt) {
  119. // Get all the thermistors and save them into an object
  120. var r, s, findDef = new RegExp('(//.*\n)+\\s+(#define[ \\t]+TEMP_SENSOR_0)', 'g');
  121. r = findDef.exec(txt);
  122. findDef = new RegExp('^//[ \\t]*([-\\d]+)[ \\t]+is[ \\t]+(.*)[ \\t]*$', 'gm');
  123. while((s = findDef.exec(r[0])) !== null) {
  124. therms_list[s[1]] = s[1].prePad(4, '  ') + " — " + s[2];
  125. }
  126. },
  127. handleFileLoad: function(file, $uploader) {
  128. file += '';
  129. var filename = $uploader.val().replace(/.*[\/\\](.*)$/, '$1');
  130. switch(filename) {
  131. case config_file:
  132. $config.text(file);
  133. this.initThermistorsFromText(file);
  134. this.refreshConfigForm();
  135. break;
  136. case config_adv_file:
  137. $config_adv.text(file);
  138. this.refreshConfigForm();
  139. break;
  140. case boards_file:
  141. this.initBoardsFromText(file);
  142. $('#MOTHERBOARD').html('').addOptions(boards_list);
  143. this.initField('MOTHERBOARD');
  144. break;
  145. default:
  146. console.log("Can't parse "+filename);
  147. break;
  148. }
  149. },
  150. setupConfigForm: function() {
  151. // Modify form fields and make the form responsive.
  152. // As values change on the form, we could update the
  153. // contents of text areas containing the configs, for
  154. // example.
  155. // while(!$config_adv.text() == null) {}
  156. // while(!$config.text() == null) {}
  157. // Go through all form items with names
  158. $('#config_form').find('[name]').each(function() {
  159. // Set its id to its name
  160. var name = $(this).attr('name');
  161. $(this).attr({id: name});
  162. // Attach its label sibling
  163. var $label = $(this).prev();
  164. if ($label[0].tagName == 'LABEL') {
  165. $label.attr('for',name);
  166. }
  167. });
  168. // Get all 'switchable' class items and add a checkbox
  169. $('#config_form .switchable').each(function(){
  170. $(this).after(
  171. $('<input>',{type:'checkbox',value:'1',class:'enabler'}).prop('checked',true)
  172. .attr('id',this.id + '-switch')
  173. .change(self.handleSwitch)
  174. );
  175. });
  176. $('#SERIAL_PORT').addOptions([0,1,2,3,4,5,6,7]);
  177. $('#BAUDRATE').addOptions([2400,9600,19200,38400,57600,115200,250000]);
  178. $('#MOTHERBOARD').addOptions(boards_list);
  179. $('#EXTRUDERS').addOptions([1,2,3,4]);
  180. $('#POWER_SUPPLY').addOptions({'1':'ATX','2':'Xbox 360'});
  181. this.refreshConfigForm();
  182. },
  183. refreshConfigForm: function() {
  184. /**
  185. * For now I'm manually creating these references
  186. * but I should be able to parse Configuration.h
  187. * and iterate the #defines.
  188. *
  189. * For any #ifdef blocks I can create field groups
  190. * which can be dimmed together when the option
  191. * is disabled.
  192. *
  193. * Then we only need to specify exceptions to
  194. * standard behavior, (which is to add a text field)
  195. */
  196. this.initField('SERIAL_PORT');
  197. this.initField('BAUDRATE');
  198. this.initField('BTENABLED');
  199. this.initField('MOTHERBOARD');
  200. this.initField('CUSTOM_MENDEL_NAME');
  201. this.initField('MACHINE_UUID');
  202. this.initField('EXTRUDERS');
  203. this.initField('POWER_SUPPLY');
  204. this.initField('PS_DEFAULT_OFF');
  205. $('#TEMP_SENSOR_0, #TEMP_SENSOR_1, #TEMP_SENSOR_2, #TEMP_SENSOR_BED').html('').addOptions(therms_list);
  206. this.initField('TEMP_SENSOR_0');
  207. this.initField('TEMP_SENSOR_1');
  208. this.initField('TEMP_SENSOR_2');
  209. this.initField('TEMP_SENSOR_BED');
  210. /*
  211. $('#serial_stepper').jstepper({
  212. min: 0,
  213. max: 3,
  214. val: $('#SERIAL_PORT').val(),
  215. arrowWidth: '18px',
  216. arrowHeight: '15px',
  217. color: '#FFF',
  218. acolor: '#F70',
  219. hcolor: '#FF0',
  220. id: 'select-me',
  221. stepperClass: 'inner',
  222. textStyle: {width:'1.5em',fontSize:'120%',textAlign:'center'},
  223. // onChange: function(v) { },
  224. });
  225. */
  226. // prettyPrint();
  227. },
  228. initField: function(name) {
  229. var $elm = $('#'+name), isText = $elm.attr('type') == 'text';
  230. this.setFieldFromDefine(name);
  231. isText ? $elm.bind('input', this.handleChange) : $elm.change(this.handleChange)
  232. },
  233. handleChange: function(e) {
  234. self.updateDefineForField(e.target);
  235. },
  236. handleSwitch: function(e) {
  237. var $elm = $(e.target), $prev = $elm.prev();
  238. var on = $elm.prop('checked') || false;
  239. $prev.attr('disabled', !on);
  240. self.setDefineEnabled($prev[0], on);
  241. },
  242. setDefineEnabled: function(elm, val) {
  243. var $elm = $(elm);
  244. // console.log("Enable: " + elm.id + " = " + val);
  245. var txt = $config.text();
  246. var findDef = new RegExp('^[ \\t]*(//[ \\t]*)?(#define[ \\t]+' + elm.id + '([ \\t].*)?)$', 'm');
  247. txt = txt.replace(findDef, val ? '$2': '//$2');
  248. // Now set the text in the field
  249. $config.text(txt);
  250. },
  251. updateDefineForField: function(elm) {
  252. var $elm = $(elm),
  253. isCheck = $elm.attr('type') == 'checkbox',
  254. val = isCheck ? $elm.prop('checked') : $elm.val();
  255. // console.log("Set: " + elm.id + " = " + val);
  256. var txt = $config.text();
  257. if (isCheck) {
  258. var findDef = new RegExp('^([ \\t]*)(//[ \\t]*)?(#define[ \\t]+' + elm.id + '([ \\t].*)?)$', 'm');
  259. txt = txt.replace(findDef, val ? '$1$3': '$1//$3');
  260. }
  261. else {
  262. // Match the define name, 1 = "#define name ", 3=value, 4=comment
  263. var findDef = new RegExp('^([ \\t]*(//)?[ \\t]*#define[ \\t]+' + elm.id + '[ \\t]+)(.*)([ \\t]*(//)?.*)$', 'm');
  264. if ($elm.hasClass('quote')) val = '"' + val + '"'
  265. txt = txt.replace(findDef, '$1!!REGEXP!!$4').replace('!!REGEXP!!', val);
  266. }
  267. // Now set the text in the field
  268. $config.text(txt);
  269. },
  270. setFieldFromDefine: function(name, adv) {
  271. var $elm = $('#'+name), elm = $elm[0];
  272. var isCheck = $elm.attr('type') == 'checkbox';
  273. var val = this.defineValue(name, adv);
  274. isCheck ? $elm.prop('checked', val) : $elm.val("" + val);
  275. // If the item has a checkbox then set enabled state too
  276. var $cb = $('#'+name+'-switch');
  277. if ($cb.length) {
  278. var on = self.defineIsEnabled(name,adv);
  279. $elm.attr('disabled', !on);
  280. $cb.prop('checked', on);
  281. }
  282. },
  283. defineValue: function(name, adv) {
  284. var $elm = $('#'+name), elm = $elm[0];
  285. var $c = adv ? $config_adv : $config;
  286. if ($elm.attr('type') == 'checkbox') {
  287. // maybe spaces, maybe comment, #define, spaces, name, maybe a comment
  288. var findDef = new RegExp('^[ \\t]*(//[ \\t]*)?(#define[ \\t]+' + elm.id + '([ \\t]*(//)?.*)?)$', 'm');
  289. var result = findDef.exec($c.text());
  290. return (result ? result[1] == undefined || result[1].trim() != '//' : false);
  291. }
  292. else {
  293. // maybe spaces, maybe comment, #define, spaces, name, one or more spaces, value, maybe a comment
  294. var findDef = new RegExp('^([ \\t]*(//[ \\t]*)?#define[ \\t]+' + elm.id + '[ \\t]+)(.*)([ \\t]*(//)?.*)$', 'm');
  295. var result = findDef.exec($c.text());
  296. if (result !== null) {
  297. var val = result[3];
  298. if (val.search(/^".*"$/) >= 0) $elm.addClass('quote');
  299. if ($elm.hasClass('quote')) val = val.replace(/^"(.*)"$/, '$1');
  300. return val;
  301. }
  302. return 'fail';
  303. }
  304. },
  305. defineIsEnabled: function(name, adv) {
  306. var $elm = $('#'+name);
  307. var $c = adv ? $config_adv : $config;
  308. var findDef = new RegExp('^[ \\t]*(//[ \\t]*)?(#define[ \\t]+' + name + '([ \\t]*(//)?.*)?)$', 'm');
  309. var result = findDef.exec($c.text());
  310. return (result ? result[1].trim() != '//' : false);
  311. },
  312. logOnce: function(o) {
  313. if (typeof o.didLogThisObject === 'undefined') {
  314. console.log(o);
  315. o.didLogThisObject = true;
  316. }
  317. },
  318. EOF: null
  319. };
  320. })();
  321. // Typically the app would be in its own file, but this would be here
  322. configuratorApp.init();
  323. });