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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. $(function(){
  15. var marlin_config = '..';
  16. // Extend String
  17. String.prototype.lpad = function(len, chr) {
  18. if (chr === undefined) { chr = ' '; }
  19. var s = this+'', need = len - s.length;
  20. if (need > 0) { s = new Array(need+1).join(chr) + s; }
  21. return s;
  22. };
  23. String.prototype.prePad = function(len, chr) {
  24. return len ? this.lpad(len, chr) : this;
  25. };
  26. String.prototype.zeroPad = function(len) {
  27. return len ? this.prePad(len, '0') : this;
  28. };
  29. /**
  30. * selectField.addOptions takes an array or keyed object
  31. */
  32. $.fn.extend({
  33. addOptions: function(arrObj) {
  34. return this.each(function() {
  35. var sel = $(this);
  36. var isArr = Object.prototype.toString.call(arrObj) == "[object Array]";
  37. $.each(arrObj, function(k, v) {
  38. sel.append( $('<option>',{value:isArr?v:k}).text(v) );
  39. });
  40. });
  41. }
  42. });
  43. // The app is a singleton
  44. var configuratorApp = (function(){
  45. // private variables and functions go here
  46. var self,
  47. pi2 = Math.PI * 2,
  48. $config = $('#config_text'),
  49. $config_adv = $('#config_adv_text'),
  50. boards_list = {},
  51. therms_list = {};
  52. // Return this anonymous object as configuratorApp
  53. return {
  54. my_public_var: 4,
  55. init: function() {
  56. self = this; // a 'this' for use when 'this' is something else
  57. // Read boards.h
  58. boards_list = {};
  59. $.get(marlin_config + "/boards.h", function(txt) {
  60. // Get all the boards and save them into an object
  61. var r, findDef = new RegExp('[ \\t]*#define[ \\t]+(BOARD_[^ \\t]+)[ \\t]+(\\d+)[ \\t]*(//[ \\t]*)?(.+)?', 'mg');
  62. while((r = findDef.exec(txt)) !== null) {
  63. boards_list[r[1]] = r[2].prePad(r[2] < 100 ? (r[2] < 10 ? 5 : 4) : 0, ' ') + " — " + r[4].replace(/\).*/, ')');
  64. }
  65. });
  66. // Read Configuration.h
  67. $.get(marlin_config+"/Configuration.h", function(txt) {
  68. $config.text(txt);
  69. });
  70. // Read Configuration.h
  71. $.get(marlin_config+"/Configuration_adv.h", function(txt) {
  72. $config_adv.text(txt);
  73. self.setupConfigForm();
  74. });
  75. },
  76. setupConfigForm: function() {
  77. // Modify form fields and make the form responsive.
  78. // As values change on the form, we could update the
  79. // contents of text areas containing the configs, for
  80. // example.
  81. // while(!$config_adv.text() == null) {}
  82. // while(!$config.text() == null) {}
  83. // Go through all form items with names
  84. $('#config_form').find('[name]').each(function() {
  85. // Set its id to its name
  86. var name = $(this).attr('name');
  87. $(this).attr({id: name});
  88. // Attach its label sibling
  89. var $label = $(this).prev();
  90. if ($label[0].tagName == 'LABEL') {
  91. $label.attr('for',name);
  92. }
  93. });
  94. // Get all 'switchable' class items and add a checkbox
  95. $('#config_form .switchable').each(function(){
  96. $(this).after(
  97. $('<input>',{type:'checkbox',value:'1',class:'enabler'}).prop('checked',true)
  98. .attr('id',this.id + '-switch')
  99. .change(self.handleSwitch)
  100. );
  101. });
  102. /**
  103. * For now I'm manually creating these references
  104. * but I should be able to parse Configuration.h
  105. * and iterate the #defines.
  106. *
  107. * For any #ifdef blocks I can create field groups
  108. * which can be dimmed together when the option
  109. * is disabled.
  110. *
  111. * Then we only need to specify exceptions to
  112. * standard behavior, (which is to add a text field)
  113. */
  114. $('#SERIAL_PORT').addOptions([0,1,2,3,4,5,6,7]);
  115. this.initField('SERIAL_PORT');
  116. $('#BAUDRATE').addOptions([2400,9600,19200,38400,57600,115200,250000]);
  117. this.initField('BAUDRATE');
  118. this.initField('BTENABLED');
  119. $('#MOTHERBOARD').addOptions(boards_list);
  120. this.initField('MOTHERBOARD');
  121. this.initField('CUSTOM_MENDEL_NAME');
  122. this.initField('MACHINE_UUID');
  123. $('#EXTRUDERS').addOptions([1,2,3,4]);
  124. this.initField('EXTRUDERS');
  125. $('#POWER_SUPPLY').addOptions({'1':'ATX','2':'Xbox 360'});
  126. this.initField('POWER_SUPPLY');
  127. this.initField('PS_DEFAULT_OFF');
  128. /*
  129. $('#serial_stepper').jstepper({
  130. min: 0,
  131. max: 3,
  132. val: $('#SERIAL_PORT').val(),
  133. arrowWidth: '18px',
  134. arrowHeight: '15px',
  135. color: '#FFF',
  136. acolor: '#F70',
  137. hcolor: '#FF0',
  138. id: 'select-me',
  139. stepperClass: 'inner',
  140. textStyle: {width:'1.5em',fontSize:'120%',textAlign:'center'},
  141. // onChange: function(v) { },
  142. });
  143. */
  144. // prettyPrint();
  145. },
  146. initField: function(name) {
  147. var $elm = $('#'+name), isText = $elm.attr('type') == 'text';
  148. this.setFieldFromDefine(name);
  149. isText ? $elm.bind('input', this.handleChange) : $elm.change(this.handleChange)
  150. },
  151. handleChange: function(e) {
  152. self.updateDefineForField(e.target);
  153. },
  154. handleSwitch: function(e) {
  155. var $elm = $(e.target), $prev = $elm.prev();
  156. var on = $elm.prop('checked') || false;
  157. $prev.attr('disabled', !on);
  158. self.setDefineEnabled($prev[0], on);
  159. },
  160. setDefineEnabled: function(elm, val) {
  161. var $elm = $(elm);
  162. // console.log("Enable: " + elm.id + " = " + val);
  163. var txt = $config.text();
  164. var findDef = new RegExp('^[ \\t]*(//[ \\t]*)?(#define[ \\t]+' + elm.id + '([ \\t].*)?)$', 'm');
  165. txt = txt.replace(findDef, val ? '$2': '//$2');
  166. // Now set the text in the field
  167. $config.text(txt);
  168. },
  169. updateDefineForField: function(elm) {
  170. var $elm = $(elm),
  171. isCheck = $elm.attr('type') == 'checkbox',
  172. val = isCheck ? $elm.prop('checked') : $elm.val();
  173. // console.log("Set: " + elm.id + " = " + val);
  174. var txt = $config.text();
  175. if (isCheck) {
  176. var findDef = new RegExp('^([ \\t]*)(//[ \\t]*)?(#define[ \\t]+' + elm.id + '([ \\t].*)?)$', 'm');
  177. txt = txt.replace(findDef, val ? '$1$3': '$1//$3');
  178. }
  179. else {
  180. // Match the define name, 1 = "#define name ", 3=value, 4=comment
  181. var findDef = new RegExp('^([ \\t]*(//)?[ \\t]*#define[ \\t]+' + elm.id + '[ \\t]+)(.*)([ \\t]*(//)?.*)$', 'm');
  182. if ($elm.hasClass('quote')) val = '"' + val + '"'
  183. txt = txt.replace(findDef, '$1!!REGEXP!!$4').replace('!!REGEXP!!', val);
  184. }
  185. // Now set the text in the field
  186. $config.text(txt);
  187. // $config.scrollTo(500);
  188. },
  189. setFieldFromDefine: function(name, adv) {
  190. var $elm = $('#'+name), elm = $elm[0];
  191. var isCheck = $elm.attr('type') == 'checkbox';
  192. var val = this.defineValue(name, adv);
  193. isCheck ? $elm.prop('checked', val) : $elm.val("" + val);
  194. // If the item has a checkbox then set enabled state too
  195. var $cb = $('#'+name+'-switch');
  196. if ($cb.length) {
  197. var on = self.defineIsEnabled(name,adv);
  198. $elm.attr('disabled', !on);
  199. $cb.prop('checked', on);
  200. }
  201. },
  202. defineValue: function(name, adv) {
  203. var $elm = $('#'+name), elm = $elm[0];
  204. var $c = adv ? $config_adv : $config;
  205. if ($elm.attr('type') == 'checkbox') {
  206. // maybe spaces, maybe comment, #define, spaces, name, maybe a comment
  207. var findDef = new RegExp('^[ \\t]*(//[ \\t]*)?(#define[ \\t]+' + elm.id + '([ \\t]*(//)?.*)?)$', 'm');
  208. var result = findDef.exec($c.text());
  209. return (result ? result[1] == undefined || result[1].trim() != '//' : false);
  210. }
  211. else {
  212. // maybe spaces, maybe comment, #define, spaces, name, one or more spaces, value, maybe a comment
  213. var findDef = new RegExp('^([ \\t]*(//[ \\t]*)?#define[ \\t]+' + elm.id + '[ \\t]+)(.*)([ \\t]*(//)?.*)$', 'm');
  214. var result = findDef.exec($c.text());
  215. if (result !== null) {
  216. var val = result[3];
  217. if (val.search(/^".*"$/) >= 0) $elm.addClass('quote');
  218. if ($elm.hasClass('quote')) val = val.replace(/^"(.*)"$/, '$1');
  219. return val;
  220. }
  221. return 'fail';
  222. }
  223. },
  224. defineIsEnabled: function(name, adv) {
  225. var $elm = $('#'+name);
  226. var $c = adv ? $config_adv : $config;
  227. var findDef = new RegExp('^[ \\t]*(//[ \\t]*)?(#define[ \\t]+' + name + '([ \\t]*(//)?.*)?)$', 'm');
  228. var result = findDef.exec($c.text());
  229. return (result ? result[1].trim() != '//' : false);
  230. },
  231. logOnce: function(o) {
  232. if (typeof o.didLogThisObject === 'undefined') {
  233. console.log(o);
  234. o.didLogThisObject = true;
  235. }
  236. },
  237. EOF: null
  238. };
  239. })();
  240. // Typically the app would be in its own file, but this would be here
  241. configuratorApp.init();
  242. });