My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

configurator.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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 = '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. String.prototype.regEsc = function() {
  31. return this.replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&");
  32. }
  33. /**
  34. * selectField.addOptions takes an array or keyed object
  35. */
  36. $.fn.extend({
  37. addOptions: function(arrObj) {
  38. return this.each(function() {
  39. var sel = $(this);
  40. var isArr = Object.prototype.toString.call(arrObj) == "[object Array]";
  41. $.each(arrObj, function(k, v) {
  42. sel.append( $('<option>',{value:isArr?v:k}).text(v) );
  43. });
  44. });
  45. }
  46. });
  47. // The app is a singleton
  48. var configuratorApp = (function(){
  49. // private variables and functions go here
  50. var self,
  51. pi2 = Math.PI * 2,
  52. boards_file = 'boards.h',
  53. config_file = 'Configuration.h',
  54. config_adv_file = 'Configuration_adv.h',
  55. $config = $('#config_text'),
  56. $config_adv = $('#config_adv_text'),
  57. boards_list = {},
  58. therms_list = {};
  59. // Return this anonymous object as configuratorApp
  60. return {
  61. my_public_var: 4,
  62. logging: 1,
  63. init: function() {
  64. self = this; // a 'this' for use when 'this' is something else
  65. // Make tabs for the fieldsets
  66. var $fset = $('#config_form fieldset');
  67. var $tabs = $('<ul>',{class:'tabs'}), ind = 1;
  68. $('#config_form fieldset').each(function(){
  69. var tabID = 'TAB'+ind;
  70. $(this).addClass(tabID);
  71. var $leg = $(this).find('legend');
  72. var $link = $('<a>',{href:'#'+ind,id:tabID}).text($leg.text());
  73. $tabs.append($('<li>').append($link));
  74. $link.click(function(e){
  75. e.preventDefault;
  76. var ind = this.id;
  77. $tabs.find('.active').removeClass('active');
  78. $(this).addClass('active');
  79. $fset.hide();
  80. $fset.filter('.'+this.id).show();
  81. return false;
  82. });
  83. ind++;
  84. });
  85. $tabs.appendTo('#tabs');
  86. $('<br>',{class:'clear'}).appendTo('#tabs');
  87. $tabs.find('a:first').trigger('click');
  88. // Make a droppable file uploader
  89. var $uploader = $('#file-upload');
  90. var fileUploader = new BinaryFileUploader({
  91. element: $uploader[0],
  92. onFileLoad: function(file) { self.handleFileLoad(file, $uploader); }
  93. });
  94. if (!fileUploader.hasFileUploaderSupport()) alert('Your browser doesn\'t support the file reading API');
  95. // Read boards.h
  96. boards_list = {};
  97. var errFunc = function(jqXHR, textStatus, errorThrown) {
  98. alert('Failed to load '+this.url+'. Try the file field.');
  99. };
  100. $.ajax({
  101. url: marlin_config+'/'+boards_file,
  102. type: 'GET',
  103. async: false,
  104. cache: false,
  105. success: function(txt) {
  106. // Get all the boards and save them into an object
  107. self.initBoardsFromText(txt);
  108. },
  109. error: errFunc
  110. });
  111. // Read Configuration.h
  112. $.ajax({
  113. url: marlin_config+'/'+config_file,
  114. type: 'GET',
  115. async: false,
  116. cache: false,
  117. success: function(txt) {
  118. // File contents into the textarea
  119. $config.text(txt);
  120. // Get thermistor info too
  121. self.initThermistorsFromText(txt);
  122. },
  123. error: errFunc
  124. });
  125. // Read Configuration.h
  126. $.ajax({
  127. url: marlin_config+'/'+config_adv_file,
  128. type: 'GET',
  129. async: false,
  130. cache: false,
  131. success: function(txt) {
  132. // File contents into the textarea
  133. $config_adv.text(txt);
  134. self.setupConfigForm();
  135. },
  136. error: errFunc
  137. });
  138. },
  139. initBoardsFromText: function(txt) {
  140. boards_list = {};
  141. var r, findDef = new RegExp('[ \\t]*#define[ \\t]+(BOARD_[\\w_]+)[ \\t]+(\\d+)[ \\t]*(//[ \\t]*)?(.+)?', 'gm');
  142. while((r = findDef.exec(txt)) !== null) {
  143. boards_list[r[1]] = r[2].prePad(3, '  ') + " — " + r[4].replace(/\).*/, ')');
  144. }
  145. },
  146. initThermistorsFromText: function(txt) {
  147. // Get all the thermistors and save them into an object
  148. var r, s, findDef = new RegExp('(//.*\n)+\\s+(#define[ \\t]+TEMP_SENSOR_0)', 'g');
  149. r = findDef.exec(txt);
  150. findDef = new RegExp('^//[ \\t]*([-\\d]+)[ \\t]+is[ \\t]+(.*)[ \\t]*$', 'gm');
  151. while((s = findDef.exec(r[0])) !== null) {
  152. therms_list[s[1]] = s[1].prePad(4, '  ') + " — " + s[2];
  153. }
  154. },
  155. handleFileLoad: function(file, $uploader) {
  156. file += '';
  157. var filename = $uploader.val().replace(/.*[\/\\](.*)$/, '$1');
  158. switch(filename) {
  159. case config_file:
  160. $config.text(file);
  161. this.initThermistorsFromText(file);
  162. this.refreshConfigForm();
  163. break;
  164. case config_adv_file:
  165. $config_adv.text(file);
  166. this.refreshConfigForm();
  167. break;
  168. case boards_file:
  169. this.initBoardsFromText(file);
  170. $('#MOTHERBOARD').html('').addOptions(boards_list);
  171. this.initField('MOTHERBOARD');
  172. break;
  173. default:
  174. this.log("Can't parse "+filename, 1);
  175. break;
  176. }
  177. },
  178. setupConfigForm: function() {
  179. // Modify form fields and make the form responsive.
  180. // As values change on the form, we could update the
  181. // contents of text areas containing the configs, for
  182. // example.
  183. // while(!$config_adv.text() == null) {}
  184. // while(!$config.text() == null) {}
  185. // Go through all form items with names
  186. $('#config_form').find('[name]').each(function() {
  187. // Set its id to its name
  188. var name = $(this).attr('name');
  189. $(this).attr({id: name});
  190. // Attach its label sibling
  191. var $label = $(this).prev();
  192. if ($label[0].tagName == 'LABEL') {
  193. $label.attr('for',name);
  194. }
  195. });
  196. // Get all 'switchable' class items and add a checkbox
  197. $('#config_form .switchable').each(function(){
  198. $(this).after(
  199. $('<input>',{type:'checkbox',value:'1',class:'enabler'}).prop('checked',true)
  200. .attr('id',this.id + '-switch')
  201. .change(self.handleSwitch)
  202. );
  203. });
  204. $('#SERIAL_PORT').addOptions([0,1,2,3,4,5,6,7]);
  205. $('#BAUDRATE').addOptions([2400,9600,19200,38400,57600,115200,250000]);
  206. $('#MOTHERBOARD').addOptions(boards_list);
  207. $('#EXTRUDERS').addOptions([1,2,3,4]);
  208. $('#POWER_SUPPLY').addOptions({'1':'ATX','2':'Xbox 360'});
  209. this.refreshConfigForm();
  210. },
  211. refreshConfigForm: function() {
  212. /**
  213. * For now I'm manually creating these references
  214. * but I should be able to parse Configuration.h
  215. * and iterate the #defines.
  216. *
  217. * For any #ifdef blocks I can create field groups
  218. * which can be dimmed together when the option
  219. * is disabled.
  220. *
  221. * Then we only need to specify exceptions to
  222. * standard behavior, (which is to add a text field)
  223. */
  224. this.initField('SERIAL_PORT');
  225. this.initField('BAUDRATE');
  226. this.initField('BTENABLED');
  227. this.initField('MOTHERBOARD');
  228. this.initField('CUSTOM_MENDEL_NAME');
  229. this.initField('MACHINE_UUID');
  230. this.initField('EXTRUDERS');
  231. this.initField('POWER_SUPPLY');
  232. this.initField('PS_DEFAULT_OFF');
  233. $('#TEMP_SENSOR_0, #TEMP_SENSOR_1, #TEMP_SENSOR_2, #TEMP_SENSOR_BED').html('').addOptions(therms_list);
  234. this.initField('TEMP_SENSOR_0');
  235. this.initField('TEMP_SENSOR_1');
  236. this.initField('TEMP_SENSOR_2');
  237. this.initField('TEMP_SENSOR_BED');
  238. this.initField('TEMP_SENSOR_1_AS_REDUNDANT');
  239. this.initField('MAX_REDUNDANT_TEMP_SENSOR_DIFF');
  240. this.initField('TEMP_RESIDENCY_TIME');
  241. $('#serial_stepper').jstepper({
  242. min: 0,
  243. max: 7,
  244. val: $('#SERIAL_PORT').val(),
  245. arrowWidth: '18px',
  246. arrowHeight: '15px',
  247. color: '#FFF',
  248. acolor: '#F70',
  249. hcolor: '#FF0',
  250. id: 'select-me',
  251. textStyle: {width:'1.5em',fontSize:'120%',textAlign:'center'},
  252. onChange: function(v) { $('#SERIAL_PORT').val(v).trigger('change'); }
  253. });
  254. // prettyPrint();
  255. },
  256. /**
  257. * initField - make a field responsive and get info
  258. * about its configuration file define
  259. */
  260. initField: function(name) {
  261. var $elm = $('#'+name), elm = $elm[0];
  262. if (elm.configInfo === undefined) {
  263. elm.configInfo = this.getDefineInfo(name);
  264. $elm.on($elm.attr('type') == 'text' ? 'input' : 'change', this.handleChange);
  265. }
  266. this.setFieldFromDefine(name);
  267. },
  268. handleChange: function(e) {
  269. self.updateDefineForField(e.target.id);
  270. },
  271. handleSwitch: function(e) {
  272. var $elm = $(e.target), $prev = $elm.prev();
  273. var on = $elm.prop('checked') || false;
  274. $prev.attr('disabled', !on);
  275. self.setDefineEnabled($prev[0].id, on);
  276. },
  277. setDefineEnabled: function(name, val) {
  278. this.log('setDefineEnabled:'+name,3);
  279. var $elm = $('#'+name), elm = $elm[0], inf = elm.configInfo;
  280. var $c = $config; // for now
  281. var txt = $c.text();
  282. var slash = val ? '' : '//';
  283. var newline = inf.line
  284. .replace(/^([ \t]*)(\/\/)([ \t]*)/, '$1$3') // remove slashes
  285. .replace(inf.pre+inf.define, inf.pre+slash+inf.define); // add them back
  286. txt = txt.replace(inf.line, newline);
  287. inf.line = newline;
  288. this.log(newline, 2);
  289. $c.text(txt);
  290. },
  291. defineIsEnabled: function(name, adv) {
  292. this.log('defineIsEnabled:'+name,4);
  293. var $elm = $('#'+name), elm = $elm[0];
  294. var $c = adv ? $config_adv : $config;
  295. var result = elm.configInfo.regex.exec($c.text());
  296. this.log(result,2);
  297. var on = result !== null ? result[1].trim() != '//' : true;
  298. this.log(name + ' = ' + on, 4);
  299. return on;
  300. },
  301. updateDefineForField: function(name, adv) {
  302. this.log('updateDefineForField:'+name,4);
  303. var $elm = $('#'+name), elm = $elm[0], inf = elm.configInfo;
  304. var $c = adv ? $config_adv : $config;
  305. var txt = $c.text();
  306. // var result = inf.repl.exec(txt);
  307. // this.log(result, 2);
  308. var isCheck = $elm.attr('type') == 'checkbox',
  309. val = isCheck ? $elm.prop('checked') : $elm.val();
  310. var newline;
  311. switch(inf.type) {
  312. case 'switch':
  313. var slash = val ? '' : '//';
  314. newline = (inf.pre + slash + inf.define + inf.post);
  315. break;
  316. case 'quoted':
  317. if (isCheck) {
  318. this.log(name + ' should not be a checkbox', 1);
  319. var slash = val ? '' : '//';
  320. newline = (inf.pre + slash + inf.define + '"'+val+'"' + inf.post);
  321. }
  322. else {
  323. newline = inf.pre + inf.define + '"'+val+'"' + inf.post;
  324. }
  325. break;
  326. case 'plain':
  327. if (isCheck) {
  328. this.log(name + ' should not be a checkbox', 1);
  329. var slash = val ? '' : '//';
  330. newline = (inf.pre + slash + inf.define + val + inf.post);
  331. }
  332. else {
  333. newline = inf.pre + inf.define + val + inf.post;
  334. }
  335. break;
  336. }
  337. txt = txt.replace(inf.line, newline);
  338. inf.line = newline;
  339. this.log(newline, 2);
  340. $c.text(txt);
  341. },
  342. setFieldFromDefine: function(name, adv) {
  343. var $elm = $('#'+name), elm = $elm[0];
  344. var isCheck = $elm.attr('type') == 'checkbox';
  345. var val = this.defineValue(name, adv);
  346. this.log('setFieldFromDefine:' + name + ' to ' + val, 4);
  347. isCheck ? $elm.prop('checked', val) : $elm.val("" + val);
  348. // If the item has a checkbox then set enabled state too
  349. var $cb = $('#'+name+'-switch');
  350. if ($cb.length) {
  351. var on = self.defineIsEnabled(name,adv);
  352. $elm.attr('disabled', !on);
  353. $cb.prop('checked', on);
  354. }
  355. },
  356. getDefineInfo: function(name, adv) {
  357. this.log('getDefineInfo:'+name,4);
  358. var $elm = $('#'+name), elm = $elm[0];
  359. var $c = adv ? $config_adv : $config;
  360. // a switch line with no value
  361. var findDef = new RegExp('^(.*//)?(.*#define[ \\t]+' + elm.id + ')([ \\t]*/[*/].*)?$', 'm');
  362. var result = findDef.exec($c.text());
  363. if (result !== null) {
  364. var info = {
  365. type: 'switch',
  366. line: result[0], // whole line
  367. pre: result[1] === undefined ? '' : result[1].replace('//',''),
  368. define: result[2],
  369. post: result[3] === undefined ? '' : result[3]
  370. };
  371. info.regex = new RegExp('(.*//)?(.*' + info.define.regEsc() + info.post.regEsc() + ')', 'm');
  372. info.repl = info.regex;
  373. this.log(info,2);
  374. return info;
  375. }
  376. // a define with quotes
  377. findDef = new RegExp('^(.*//)?(.*#define[ \\t]+' + elm.id + '[ \\t]+)("[^"]*")([ \\t]*/[*/].*)?$', 'm');
  378. result = findDef.exec($c.text());
  379. if (result !== null) {
  380. var info = {
  381. type: 'quoted',
  382. line: result[0],
  383. pre: result[1] === undefined ? '' : result[1].replace('//',''),
  384. define: result[2],
  385. post: result[4] === undefined ? '' : result[4]
  386. };
  387. info.regex = new RegExp('(.*//)?.*' + info.define.regEsc() + '"([^"]*)"' + info.post.regEsc(), 'm');
  388. info.repl = new RegExp('((.*//)?.*' + info.define.regEsc() + '")[^"]*("' + info.post.regEsc() + ')', 'm');
  389. this.log(info,2);
  390. return info;
  391. }
  392. // a define with no quotes
  393. findDef = new RegExp('^(.*//)?(.*#define[ \\t]+' + elm.id + '[ \\t]+)(\\S*)([ \\t]*/[*/].*)?$', 'm');
  394. result = findDef.exec($c.text());
  395. if (result !== null) {
  396. var info = {
  397. type: 'plain',
  398. line: result[0],
  399. pre: result[1] === undefined ? '' : result[1].replace('//',''),
  400. define: result[2],
  401. post: result[4] === undefined ? '' : result[4]
  402. };
  403. info.regex = new RegExp('(.*//)?.*' + info.define.regEsc() + '(\\S*)' + info.post.regEsc(), 'm');
  404. info.repl = new RegExp('((.*//)?.*' + info.define.regEsc() + ')\\S*(' + info.post.regEsc() + ')', 'm');
  405. this.log(info,2);
  406. return info;
  407. }
  408. return null;
  409. },
  410. defineValue: function(name, adv) {
  411. this.log('defineValue:'+name,4);
  412. var $elm = $('#'+name), elm = $elm[0];
  413. var $c = adv ? $config_adv : $config;
  414. var inf = elm.configInfo;
  415. var result = inf.regex.exec($c.text());
  416. this.log(result,2);
  417. switch(inf.type) {
  418. case 'switch': return result[1] != '//';
  419. case 'quoted': return result[2];
  420. case 'plain': return result[2];
  421. }
  422. },
  423. log: function(o,l) {
  424. if (l === undefined) l = 0;
  425. if (this.logging>=l*1) console.log(o);
  426. },
  427. logOnce: function(o) {
  428. if (typeof o.didLogThisObject === 'undefined') {
  429. this.log(o);
  430. o.didLogThisObject = true;
  431. }
  432. },
  433. EOF: null
  434. };
  435. })();
  436. // Typically the app would be in its own file, but this would be here
  437. configuratorApp.init();
  438. });