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 15KB

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