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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. $('#serial_stepper').jstepper({
  211. min: 0,
  212. max: 7,
  213. val: $('#SERIAL_PORT').val(),
  214. arrowWidth: '18px',
  215. arrowHeight: '15px',
  216. color: '#FFF',
  217. acolor: '#F70',
  218. hcolor: '#FF0',
  219. id: 'select-me',
  220. textStyle: {width:'1.5em',fontSize:'120%',textAlign:'center'},
  221. onChange: function(v) { $('#SERIAL_PORT').val(v).trigger('change'); }
  222. });
  223. // prettyPrint();
  224. },
  225. initField: function(name) {
  226. var $elm = $('#'+name), isText = $elm.attr('type') == 'text';
  227. this.setFieldFromDefine(name);
  228. isText ? $elm.bind('input', this.handleChange) : $elm.change(this.handleChange)
  229. },
  230. handleChange: function(e) {
  231. self.updateDefineForField(e.target);
  232. },
  233. handleSwitch: function(e) {
  234. var $elm = $(e.target), $prev = $elm.prev();
  235. var on = $elm.prop('checked') || false;
  236. $prev.attr('disabled', !on);
  237. self.setDefineEnabled($prev[0], on);
  238. },
  239. setDefineEnabled: function(elm, val) {
  240. var $elm = $(elm);
  241. // console.log("Enable: " + elm.id + " = " + val);
  242. var txt = $config.text();
  243. var findDef = new RegExp('^[ \\t]*(//[ \\t]*)?(#define[ \\t]+' + elm.id + '([ \\t].*)?)$', 'm');
  244. txt = txt.replace(findDef, val ? '$2': '//$2');
  245. // Now set the text in the field
  246. $config.text(txt);
  247. },
  248. updateDefineForField: function(elm) {
  249. var $elm = $(elm),
  250. isCheck = $elm.attr('type') == 'checkbox',
  251. val = isCheck ? $elm.prop('checked') : $elm.val();
  252. // console.log("Set: " + elm.id + " = " + val);
  253. var txt = $config.text();
  254. if (isCheck) {
  255. var findDef = new RegExp('^([ \\t]*)(//[ \\t]*)?(#define[ \\t]+' + elm.id + '([ \\t].*)?)$', 'm');
  256. txt = txt.replace(findDef, val ? '$1$3': '$1//$3');
  257. }
  258. else {
  259. // Match the define name, 1 = "#define name ", 3=value, 4=comment
  260. var findDef = new RegExp('^([ \\t]*(//)?[ \\t]*#define[ \\t]+' + elm.id + '[ \\t]+)(.*)([ \\t]*(//)?.*)$', 'm');
  261. if ($elm.hasClass('quote')) val = '"' + val + '"'
  262. txt = txt.replace(findDef, '$1!!REGEXP!!$4').replace('!!REGEXP!!', val);
  263. }
  264. // Now set the text in the field
  265. $config.text(txt);
  266. },
  267. setFieldFromDefine: function(name, adv) {
  268. var $elm = $('#'+name), elm = $elm[0];
  269. var isCheck = $elm.attr('type') == 'checkbox';
  270. var val = this.defineValue(name, adv);
  271. isCheck ? $elm.prop('checked', val) : $elm.val("" + val);
  272. // If the item has a checkbox then set enabled state too
  273. var $cb = $('#'+name+'-switch');
  274. if ($cb.length) {
  275. var on = self.defineIsEnabled(name,adv);
  276. $elm.attr('disabled', !on);
  277. $cb.prop('checked', on);
  278. }
  279. },
  280. defineValue: function(name, adv) {
  281. var $elm = $('#'+name), elm = $elm[0];
  282. var $c = adv ? $config_adv : $config;
  283. if ($elm.attr('type') == 'checkbox') {
  284. // maybe spaces, maybe comment, #define, spaces, name, maybe a comment
  285. var findDef = new RegExp('^[ \\t]*(//[ \\t]*)?(#define[ \\t]+' + elm.id + '([ \\t]*(//)?.*)?)$', 'm');
  286. var result = findDef.exec($c.text());
  287. return (result ? result[1] == undefined || result[1].trim() != '//' : false);
  288. }
  289. else {
  290. // maybe spaces, maybe comment, #define, spaces, name, one or more spaces, value, maybe a comment
  291. var findDef = new RegExp('^([ \\t]*(//[ \\t]*)?#define[ \\t]+' + elm.id + '[ \\t]+)(.*)([ \\t]*(//)?.*)$', 'm');
  292. var result = findDef.exec($c.text());
  293. if (result !== null) {
  294. var val = result[3];
  295. if (val.search(/^".*"$/) >= 0) $elm.addClass('quote');
  296. if ($elm.hasClass('quote')) val = val.replace(/^"(.*)"$/, '$1');
  297. return val;
  298. }
  299. return 'fail';
  300. }
  301. },
  302. defineIsEnabled: function(name, adv) {
  303. var $elm = $('#'+name);
  304. var $c = adv ? $config_adv : $config;
  305. var findDef = new RegExp('^[ \\t]*(//[ \\t]*)?(#define[ \\t]+' + name + '([ \\t]*(//)?.*)?)$', 'm');
  306. var result = findDef.exec($c.text());
  307. return (result ? result[1].trim() != '//' : false);
  308. },
  309. logOnce: function(o) {
  310. if (typeof o.didLogThisObject === 'undefined') {
  311. console.log(o);
  312. o.didLogThisObject = true;
  313. }
  314. },
  315. EOF: null
  316. };
  317. })();
  318. // Typically the app would be in its own file, but this would be here
  319. configuratorApp.init();
  320. });