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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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;
  34. if (argc > 0) {
  35. src_file = src_name = argv[0];
  36. dst_file = argv[argc > 1 ? 1 : 0];
  37. }
  38. // Read from file or STDIN until it terminates
  39. const filtered = process_text(fs.readFileSync(src_file).toString());
  40. if (dst_file)
  41. fs.writeFileSync(dst_file, filtered);
  42. else
  43. console.log(filtered);
  44. // Find the pin pattern so non-pin defines can be skipped
  45. function get_pin_pattern(txt) {
  46. var r, m = 0, match_count = [ 0, 0, 0 ];
  47. definePatt.lastIndex = 0;
  48. while ((r = definePatt.exec(txt)) !== null) {
  49. let ind = -1;
  50. if (mexpr.some((p) => {
  51. ind++;
  52. const didmatch = r[2].match(p);
  53. return r[2].match(p);
  54. }) ) {
  55. const m = ++match_count[ind];
  56. if (m >= 10) {
  57. return { match: mpatt[ind], pad:ppad[ind] };
  58. }
  59. }
  60. }
  61. return null;
  62. }
  63. function process_text(txt) {
  64. if (!txt.length) return '(no text)';
  65. const patt = get_pin_pattern(txt);
  66. if (!patt) return txt;
  67. const pindefPatt = new RegExp(`^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s+(${patt.match})\\s*(//.*)?$`),
  68. noPinPatt = new RegExp(`^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s+(-1)\\s*(//.*)?$`),
  69. skipPatt = new RegExp('^(\\s*(//)?#define)\\s+(AT90USB|USBCON|BOARD_.+|.+_MACHINE_NAME|.+_SERIAL)\\s+(.+)\\s*(//.*)?$'),
  70. aliasPatt = new RegExp('^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s+([A-Z_][A-Z0-9_()]+)\\s*(//.*)?$'),
  71. switchPatt = new RegExp('^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s*(//.*)?$'),
  72. undefPatt = new RegExp('^(\\s*(//)?#undef)\\s+([A-Z_][A-Z0-9_]+)\\s*(//.*)?$'),
  73. defPatt = new RegExp('^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s+(\\w+)\\s*(//.*)?$'),
  74. condPatt = new RegExp('^(\\s*(//)?#(if|ifn?def|else|elif)(\\s+\\S+)*)\\s+(//.*)$'),
  75. commPatt = new RegExp('^\\s{20,}(//.*)?$');
  76. const col_value_lj = col_comment - patt.pad - 2;
  77. var r, out = '', check_comment_next = false;
  78. txt.split('\n').forEach((line) => {
  79. if (check_comment_next)
  80. check_comment_next = ((r = commPatt.exec(line)) !== null);
  81. if (check_comment_next)
  82. // Comments in column 45
  83. line = ''.rpad(col_comment) + r[1];
  84. else if ((r = pindefPatt.exec(line)) !== null) {
  85. //
  86. // #define MY_PIN [pin]
  87. //
  88. const pinnum = r[4].charAt(0) == 'P' ? r[4] : r[4].lpad(patt.pad);
  89. line = r[1] + ' ' + r[3];
  90. line = line.rpad(col_value_lj) + pinnum;
  91. if (r[5]) line = line.rpad(col_comment) + r[5];
  92. }
  93. else if ((r = noPinPatt.exec(line)) !== null) {
  94. //
  95. // #define MY_PIN -1
  96. //
  97. line = r[1] + ' ' + r[3];
  98. line = line.rpad(col_value_lj) + '-1';
  99. if (r[5]) line = line.rpad(col_comment) + r[5];
  100. }
  101. else if ((r = skipPatt.exec(line)) !== null) {
  102. }
  103. else if ((r = aliasPatt.exec(line)) !== null) {
  104. line = r[1] + ' ' + r[3];
  105. line += r[4].lpad(col_value_rj + 1 - line.length);
  106. if (r[5]) line = line.rpad(col_comment) + r[5];
  107. }
  108. else if ((r = switchPatt.exec(line)) !== null) {
  109. line = r[1] + ' ' + r[3];
  110. if (r[4]) line = line.rpad(col_comment) + r[4];
  111. check_comment_next = true;
  112. }
  113. else if ((r = defPatt.exec(line)) !== null) {
  114. line = r[1] + ' ' + r[3] + ' ' + r[4];
  115. if (r[5]) line = line.rpad(col_comment) + r[5];
  116. }
  117. else if ((r = undefPatt.exec(line)) !== null) {
  118. line = r[1] + ' ' + r[3];
  119. if (r[4]) line = line.rpad(col_comment) + r[4];
  120. }
  121. else if ((r = condPatt.exec(line)) !== null) {
  122. line = r[1].rpad(col_comment) + r[5];
  123. check_comment_next = true;
  124. }
  125. out += line + '\n';
  126. });
  127. return out.replace(/\n\n+/g, '\n\n').replace(/\n\n$/g, '\n');
  128. }