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.

pinsformat.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/usr/bin/env node
  2. //
  3. // Formatter script for pins_MYPINS.h files
  4. //
  5. // Usage: mffmt [infile] [outfile]
  6. //
  7. // With no parameters convert STDIN to STDOUT
  8. //
  9. const fs = require("fs");
  10. // String lpad / rpad
  11. String.prototype.lpad = function(len, chr) {
  12. if (!len) return this;
  13. if (chr === undefined) chr = ' ';
  14. var s = this+'', need = len - s.length;
  15. if (need > 0) s = new Array(need+1).join(chr) + s;
  16. return s;
  17. };
  18. String.prototype.rpad = function(len, chr) {
  19. if (!len) return this;
  20. if (chr === undefined) chr = ' ';
  21. var s = this+'', need = len - s.length;
  22. if (need > 0) s += new Array(need+1).join(chr);
  23. return s;
  24. };
  25. const mpatt = [ '-?\\d+', 'P[A-I]\\d+', 'P\\d_\\d+' ],
  26. definePatt = new RegExp(`^\\s*(//)?#define\\s+[A-Z_][A-Z0-9_]+\\s+(${mpatt[0]}|${mpatt[1]}|${mpatt[2]})\\s*(//.*)?$`, 'gm'),
  27. ppad = [ 3, 4, 5 ],
  28. col_comment = 50,
  29. col_value_rj = col_comment - 3;
  30. var mexpr = [];
  31. for (let m of mpatt) mexpr.push(new RegExp('^' + m + '$'));
  32. const argv = process.argv.slice(2), argc = argv.length;
  33. var src_file = 0, src_name = 'STDIN', dst_file, do_log = false;
  34. if (argc > 0) {
  35. let ind = 0;
  36. if (argv[0] == '-v') { do_log = true; ind++; }
  37. dst_file = src_file = src_name = argv[ind++];
  38. if (ind < argc) dst_file = argv[ind];
  39. }
  40. // Read from file or STDIN until it terminates
  41. const filtered = process_text(fs.readFileSync(src_file).toString());
  42. if (dst_file)
  43. fs.writeFileSync(dst_file, filtered);
  44. else
  45. console.log(filtered);
  46. // Find the pin pattern so non-pin defines can be skipped
  47. function get_pin_pattern(txt) {
  48. var r, m = 0, match_count = [ 0, 0, 0 ];
  49. definePatt.lastIndex = 0;
  50. while ((r = definePatt.exec(txt)) !== null) {
  51. let ind = -1;
  52. if (mexpr.some((p) => {
  53. ind++;
  54. const didmatch = r[2].match(p);
  55. return r[2].match(p);
  56. }) ) {
  57. const m = ++match_count[ind];
  58. if (m >= 10) {
  59. return { match: mpatt[ind], pad:ppad[ind] };
  60. }
  61. }
  62. }
  63. return null;
  64. }
  65. function process_text(txt) {
  66. if (!txt.length) return '(no text)';
  67. const patt = get_pin_pattern(txt);
  68. if (!patt) return txt;
  69. const pindefPatt = new RegExp(`^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s+(${patt.match})\\s*(//.*)?$`),
  70. noPinPatt = new RegExp(`^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s+(-1)\\s*(//.*)?$`),
  71. skipPatt = new RegExp('^(\\s*(//)?#define)\\s+(AT90USB|USBCON|BOARD_.+|.+_MACHINE_NAME|.+_SERIAL)\\s+(.+)\\s*(//.*)?$'),
  72. aliasPatt = new RegExp('^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s+([A-Z_][A-Z0-9_()]+)\\s*(//.*)?$'),
  73. switchPatt = new RegExp('^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s*(//.*)?$'),
  74. undefPatt = new RegExp('^(\\s*(//)?#undef)\\s+([A-Z_][A-Z0-9_]+)\\s*(//.*)?$'),
  75. defPatt = new RegExp('^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s+([-_\\w]+)\\s*(//.*)?$'),
  76. condPatt = new RegExp('^(\\s*(//)?#(if|ifn?def|else|elif)(\\s+\\S+)*)\\s+(//.*)$'),
  77. commPatt = new RegExp('^\\s{20,}(//.*)?$');
  78. const col_value_lj = col_comment - patt.pad - 2;
  79. var r, out = '', check_comment_next = false;
  80. txt.split('\n').forEach((line) => {
  81. if (check_comment_next)
  82. check_comment_next = ((r = commPatt.exec(line)) !== null);
  83. if (check_comment_next)
  84. // Comments in column 45
  85. line = ''.rpad(col_comment) + r[1];
  86. else if ((r = pindefPatt.exec(line)) !== null) {
  87. //
  88. // #define MY_PIN [pin]
  89. //
  90. if (do_log) console.log("pin:", line);
  91. const pinnum = r[4].charAt(0) == 'P' ? r[4] : r[4].lpad(patt.pad);
  92. line = r[1] + ' ' + r[3];
  93. line = line.rpad(col_value_lj) + pinnum;
  94. if (r[5]) line = line.rpad(col_comment) + r[5];
  95. }
  96. else if ((r = noPinPatt.exec(line)) !== null) {
  97. //
  98. // #define MY_PIN -1
  99. //
  100. if (do_log) console.log("pin -1:", line);
  101. line = r[1] + ' ' + r[3];
  102. line = line.rpad(col_value_lj) + '-1';
  103. if (r[5]) line = line.rpad(col_comment) + r[5];
  104. }
  105. else if ((r = skipPatt.exec(line)) !== null) {
  106. //
  107. // #define SKIP_ME
  108. //
  109. if (do_log) console.log("skip:", line);
  110. }
  111. else if ((r = aliasPatt.exec(line)) !== null) {
  112. //
  113. // #define ALIAS OTHER
  114. //
  115. if (do_log) console.log("alias:", line);
  116. line = r[1] + ' ' + r[3];
  117. line += r[4].lpad(col_value_rj + 1 - line.length);
  118. if (r[5]) line = line.rpad(col_comment) + r[5];
  119. }
  120. else if ((r = switchPatt.exec(line)) !== null) {
  121. //
  122. // #define SWITCH
  123. //
  124. if (do_log) console.log("switch:", line);
  125. line = r[1] + ' ' + r[3];
  126. if (r[4]) line = line.rpad(col_comment) + r[4];
  127. check_comment_next = true;
  128. }
  129. else if ((r = defPatt.exec(line)) !== null) {
  130. //
  131. // #define ...
  132. //
  133. if (do_log) console.log("def:", line);
  134. line = r[1] + ' ' + r[3] + ' ';
  135. line += r[4].lpad(col_value_rj + 1 - line.length);
  136. if (r[5]) line = line.rpad(col_comment - 1) + ' ' + r[5];
  137. }
  138. else if ((r = undefPatt.exec(line)) !== null) {
  139. //
  140. // #undef ...
  141. //
  142. if (do_log) console.log("undef:", line);
  143. line = r[1] + ' ' + r[3];
  144. if (r[4]) line = line.rpad(col_comment) + r[4];
  145. }
  146. else if ((r = condPatt.exec(line)) !== null) {
  147. //
  148. // #if ...
  149. //
  150. if (do_log) console.log("cond:", line);
  151. line = r[1].rpad(col_comment) + r[5];
  152. check_comment_next = true;
  153. }
  154. out += line + '\n';
  155. });
  156. return out.replace(/\n\n+/g, '\n\n').replace(/\n\n$/g, '\n');
  157. }