My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

configurator.js 9.6KB

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