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.

utility.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "utility.h"
  23. #include "../Marlin.h"
  24. #include "../module/temperature.h"
  25. void safe_delay(millis_t ms) {
  26. while (ms > 50) {
  27. ms -= 50;
  28. delay(50);
  29. thermalManager.manage_heater();
  30. }
  31. delay(ms);
  32. thermalManager.manage_heater(); // This keeps us safe if too many small safe_delay() calls are made
  33. }
  34. #if EITHER(EEPROM_SETTINGS, SD_FIRMWARE_UPDATE)
  35. void crc16(uint16_t *crc, const void * const data, uint16_t cnt) {
  36. uint8_t *ptr = (uint8_t *)data;
  37. while (cnt--) {
  38. *crc = (uint16_t)(*crc ^ (uint16_t)(((uint16_t)*ptr++) << 8));
  39. for (uint8_t i = 0; i < 8; i++)
  40. *crc = (uint16_t)((*crc & 0x8000) ? ((uint16_t)(*crc << 1) ^ 0x1021) : (*crc << 1));
  41. }
  42. }
  43. #endif // EEPROM_SETTINGS || SD_FIRMWARE_UPDATE
  44. #if ANY(ULTRA_LCD, DEBUG_LEVELING_FEATURE, EXTENSIBLE_UI)
  45. char conv[8] = { 0 };
  46. #define DIGIT(n) ('0' + (n))
  47. #define DIGIMOD(n, f) DIGIT((n)/(f) % 10)
  48. #define RJDIGIT(n, f) ((n) >= (f) ? DIGIMOD(n, f) : ' ')
  49. #define MINUSOR(n, alt) (n >= 0 ? (alt) : (n = -n, '-'))
  50. // Convert a full-range unsigned 8bit int to a percentage
  51. char* ui8tostr4pct(const uint8_t i) {
  52. const uint8_t n = ui8_to_percent(i);
  53. conv[3] = RJDIGIT(n, 100);
  54. conv[4] = RJDIGIT(n, 10);
  55. conv[5] = DIGIMOD(n, 1);
  56. conv[6] = '%';
  57. return &conv[3];
  58. }
  59. // Convert unsigned 8bit int to string 123 format
  60. char* ui8tostr3(const uint8_t i) {
  61. conv[4] = RJDIGIT(i, 100);
  62. conv[5] = RJDIGIT(i, 10);
  63. conv[6] = DIGIMOD(i, 1);
  64. return &conv[4];
  65. }
  66. // Convert signed 8bit int to rj string with 123 or -12 format
  67. char* i8tostr3(const int8_t x) {
  68. int xx = x;
  69. conv[4] = MINUSOR(xx, RJDIGIT(xx, 100));
  70. conv[5] = RJDIGIT(xx, 10);
  71. conv[6] = DIGIMOD(xx, 1);
  72. return &conv[4];
  73. }
  74. // Convert unsigned 16bit int to string 123 format
  75. char* ui16tostr3(const uint16_t xx) {
  76. conv[4] = RJDIGIT(xx, 100);
  77. conv[5] = RJDIGIT(xx, 10);
  78. conv[6] = DIGIMOD(xx, 1);
  79. return &conv[4];
  80. }
  81. // Convert unsigned 16bit int to string 1234 format
  82. char* ui16tostr4(const uint16_t xx) {
  83. conv[3] = RJDIGIT(xx, 1000);
  84. conv[4] = RJDIGIT(xx, 100);
  85. conv[5] = RJDIGIT(xx, 10);
  86. conv[6] = DIGIMOD(xx, 1);
  87. return &conv[3];
  88. }
  89. // Convert signed 16bit int to rj string with 123 or -12 format
  90. char* i16tostr3(const int16_t x) {
  91. int xx = x;
  92. conv[4] = MINUSOR(xx, RJDIGIT(xx, 100));
  93. conv[5] = RJDIGIT(xx, 10);
  94. conv[6] = DIGIMOD(xx, 1);
  95. return &conv[4];
  96. }
  97. // Convert unsigned 16bit int to lj string with 123 format
  98. char* i16tostr3left(const int16_t i) {
  99. char *str = &conv[6];
  100. *str = DIGIMOD(i, 1);
  101. if (i >= 10) {
  102. *(--str) = DIGIMOD(i, 10);
  103. if (i >= 100)
  104. *(--str) = DIGIMOD(i, 100);
  105. }
  106. return str;
  107. }
  108. // Convert signed 16bit int to rj string with 1234, _123, -123, _-12, or __-1 format
  109. char* i16tostr4sign(const int16_t i) {
  110. const bool neg = i < 0;
  111. const int ii = neg ? -i : i;
  112. if (i >= 1000) {
  113. conv[3] = DIGIMOD(ii, 1000);
  114. conv[4] = DIGIMOD(ii, 100);
  115. conv[5] = DIGIMOD(ii, 10);
  116. }
  117. else if (ii >= 100) {
  118. conv[3] = neg ? '-' : ' ';
  119. conv[4] = DIGIMOD(ii, 100);
  120. conv[5] = DIGIMOD(ii, 10);
  121. }
  122. else {
  123. conv[3] = ' ';
  124. conv[4] = ' ';
  125. if (ii >= 10) {
  126. conv[4] = neg ? '-' : ' ';
  127. conv[5] = DIGIMOD(ii, 10);
  128. }
  129. else {
  130. conv[5] = neg ? '-' : ' ';
  131. }
  132. }
  133. conv[6] = DIGIMOD(ii, 1);
  134. return &conv[3];
  135. }
  136. // Convert unsigned float to string with 1.23 format
  137. char* ftostr12ns(const float &f) {
  138. const long i = ((f < 0 ? -f : f) * 1000 + 5) / 10;
  139. conv[3] = DIGIMOD(i, 100);
  140. conv[4] = '.';
  141. conv[5] = DIGIMOD(i, 10);
  142. conv[6] = DIGIMOD(i, 1);
  143. return &conv[3];
  144. }
  145. // Convert signed float to fixed-length string with 023.45 / -23.45 format
  146. char* ftostr52(const float &f) {
  147. long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
  148. conv[1] = MINUSOR(i, DIGIMOD(i, 10000));
  149. conv[2] = DIGIMOD(i, 1000);
  150. conv[3] = DIGIMOD(i, 100);
  151. conv[4] = '.';
  152. conv[5] = DIGIMOD(i, 10);
  153. conv[6] = DIGIMOD(i, 1);
  154. return &conv[1];
  155. }
  156. #if ENABLED(LCD_DECIMAL_SMALL_XY)
  157. // Convert float to rj string with 1234, _123, -123, _-12, 12.3, _1.2, or -1.2 format
  158. char* ftostr4sign(const float &f) {
  159. const int i = (f * 100 + (f < 0 ? -5: 5)) / 10;
  160. if (!WITHIN(i, -99, 999)) return i16tostr4sign((int)f);
  161. const bool neg = i < 0;
  162. const int ii = neg ? -i : i;
  163. conv[3] = neg ? '-' : (ii >= 100 ? DIGIMOD(ii, 100) : ' ');
  164. conv[4] = DIGIMOD(ii, 10);
  165. conv[5] = '.';
  166. conv[6] = DIGIMOD(ii, 1);
  167. return &conv[3];
  168. }
  169. #endif // LCD_DECIMAL_SMALL_XY
  170. // Convert float to fixed-length string with +123.4 / -123.4 format
  171. char* ftostr41sign(const float &f) {
  172. int i = (f * 100 + (f < 0 ? -5: 5)) / 10;
  173. conv[1] = MINUSOR(i, '+');
  174. conv[2] = DIGIMOD(i, 1000);
  175. conv[3] = DIGIMOD(i, 100);
  176. conv[4] = DIGIMOD(i, 10);
  177. conv[5] = '.';
  178. conv[6] = DIGIMOD(i, 1);
  179. return &conv[1];
  180. }
  181. // Convert signed float to string (6 digit) with -1.234 / _0.000 / +1.234 format
  182. char* ftostr43sign(const float &f, char plus/*=' '*/) {
  183. long i = (f * 10000 + (f < 0 ? -5: 5)) / 10;
  184. conv[1] = i ? MINUSOR(i, plus) : ' ';
  185. conv[2] = DIGIMOD(i, 1000);
  186. conv[3] = '.';
  187. conv[4] = DIGIMOD(i, 100);
  188. conv[5] = DIGIMOD(i, 10);
  189. conv[6] = DIGIMOD(i, 1);
  190. return &conv[1];
  191. }
  192. // Convert signed float to string (5 digit) with -1.2345 / _0.0000 / +1.2345 format
  193. char* ftostr54sign(const float &f, char plus/*=' '*/) {
  194. long i = (f * 100000 + (f < 0 ? -5: 5)) / 10;
  195. conv[0] = i ? MINUSOR(i, plus) : ' ';
  196. conv[1] = DIGIMOD(i, 10000);
  197. conv[2] = '.';
  198. conv[3] = DIGIMOD(i, 1000);
  199. conv[4] = DIGIMOD(i, 100);
  200. conv[5] = DIGIMOD(i, 10);
  201. conv[6] = DIGIMOD(i, 1);
  202. return &conv[0];
  203. }
  204. // Convert unsigned float to rj string with 12345 format
  205. char* ftostr5rj(const float &f) {
  206. const long i = ((f < 0 ? -f : f) * 10 + 5) / 10;
  207. conv[2] = RJDIGIT(i, 10000);
  208. conv[3] = RJDIGIT(i, 1000);
  209. conv[4] = RJDIGIT(i, 100);
  210. conv[5] = RJDIGIT(i, 10);
  211. conv[6] = DIGIMOD(i, 1);
  212. return &conv[2];
  213. }
  214. // Convert signed float to string with +1234.5 format
  215. char* ftostr51sign(const float &f) {
  216. long i = (f * 100 + (f < 0 ? -5: 5)) / 10;
  217. conv[0] = MINUSOR(i, '+');
  218. conv[1] = DIGIMOD(i, 10000);
  219. conv[2] = DIGIMOD(i, 1000);
  220. conv[3] = DIGIMOD(i, 100);
  221. conv[4] = DIGIMOD(i, 10);
  222. conv[5] = '.';
  223. conv[6] = DIGIMOD(i, 1);
  224. return conv;
  225. }
  226. // Convert signed float to string with +123.45 format
  227. char* ftostr52sign(const float &f) {
  228. long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
  229. conv[0] = MINUSOR(i, '+');
  230. conv[1] = DIGIMOD(i, 10000);
  231. conv[2] = DIGIMOD(i, 1000);
  232. conv[3] = DIGIMOD(i, 100);
  233. conv[4] = '.';
  234. conv[5] = DIGIMOD(i, 10);
  235. conv[6] = DIGIMOD(i, 1);
  236. return conv;
  237. }
  238. // Convert unsigned float to string with 1234.5 format omitting trailing zeros
  239. char* ftostr51rj(const float &f) {
  240. const long i = ((f < 0 ? -f : f) * 100 + 5) / 10;
  241. conv[0] = ' ';
  242. conv[1] = RJDIGIT(i, 10000);
  243. conv[2] = RJDIGIT(i, 1000);
  244. conv[3] = RJDIGIT(i, 100);
  245. conv[4] = DIGIMOD(i, 10);
  246. conv[5] = '.';
  247. conv[6] = DIGIMOD(i, 1);
  248. return conv;
  249. }
  250. // Convert signed float to space-padded string with -_23.4_ format
  251. char* ftostr52sp(const float &f) {
  252. long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
  253. uint8_t dig;
  254. conv[0] = MINUSOR(i, ' ');
  255. conv[1] = RJDIGIT(i, 10000);
  256. conv[2] = RJDIGIT(i, 1000);
  257. conv[3] = DIGIMOD(i, 100);
  258. if ((dig = i % 10)) { // second digit after decimal point?
  259. conv[4] = '.';
  260. conv[5] = DIGIMOD(i, 10);
  261. conv[6] = DIGIT(dig);
  262. }
  263. else {
  264. if ((dig = (i / 10) % 10)) { // first digit after decimal point?
  265. conv[4] = '.';
  266. conv[5] = DIGIT(dig);
  267. }
  268. else // nothing after decimal point
  269. conv[4] = conv[5] = ' ';
  270. conv[6] = ' ';
  271. }
  272. return conv;
  273. }
  274. #endif // ULTRA_LCD
  275. #if ENABLED(DEBUG_LEVELING_FEATURE)
  276. #include "../module/probe.h"
  277. #include "../module/motion.h"
  278. #include "../module/stepper.h"
  279. #include "../feature/bedlevel/bedlevel.h"
  280. void log_machine_info() {
  281. SERIAL_ECHOLNPGM("Machine Type: "
  282. #if ENABLED(DELTA)
  283. "Delta"
  284. #elif IS_SCARA
  285. "SCARA"
  286. #elif IS_CORE
  287. "Core"
  288. #else
  289. "Cartesian"
  290. #endif
  291. );
  292. SERIAL_ECHOLNPGM("Probe: "
  293. #if ENABLED(PROBE_MANUALLY)
  294. "PROBE_MANUALLY"
  295. #elif ENABLED(FIX_MOUNTED_PROBE)
  296. "FIX_MOUNTED_PROBE"
  297. #elif ENABLED(BLTOUCH)
  298. "BLTOUCH"
  299. #elif HAS_Z_SERVO_PROBE
  300. "SERVO PROBE"
  301. #elif ENABLED(Z_PROBE_SLED)
  302. "Z_PROBE_SLED"
  303. #elif ENABLED(Z_PROBE_ALLEN_KEY)
  304. "Z_PROBE_ALLEN_KEY"
  305. #else
  306. "NONE"
  307. #endif
  308. );
  309. #if HAS_BED_PROBE
  310. SERIAL_ECHOPAIR(
  311. "Probe Offset X:" STRINGIFY(X_PROBE_OFFSET_FROM_EXTRUDER)
  312. " Y:" STRINGIFY(Y_PROBE_OFFSET_FROM_EXTRUDER)
  313. " Z:", zprobe_zoffset
  314. );
  315. if ((X_PROBE_OFFSET_FROM_EXTRUDER) > 0)
  316. SERIAL_ECHOPGM(" (Right");
  317. else if ((X_PROBE_OFFSET_FROM_EXTRUDER) < 0)
  318. SERIAL_ECHOPGM(" (Left");
  319. else if ((Y_PROBE_OFFSET_FROM_EXTRUDER) != 0)
  320. SERIAL_ECHOPGM(" (Middle");
  321. else
  322. SERIAL_ECHOPGM(" (Aligned With");
  323. if ((Y_PROBE_OFFSET_FROM_EXTRUDER) > 0) {
  324. #if IS_SCARA
  325. SERIAL_ECHOPGM("-Distal");
  326. #else
  327. SERIAL_ECHOPGM("-Back");
  328. #endif
  329. }
  330. else if ((Y_PROBE_OFFSET_FROM_EXTRUDER) < 0) {
  331. #if IS_SCARA
  332. SERIAL_ECHOPGM("-Proximal");
  333. #else
  334. SERIAL_ECHOPGM("-Front");
  335. #endif
  336. }
  337. else if ((X_PROBE_OFFSET_FROM_EXTRUDER) != 0)
  338. SERIAL_ECHOPGM("-Center");
  339. if (zprobe_zoffset < 0)
  340. SERIAL_ECHOPGM(" & Below");
  341. else if (zprobe_zoffset > 0)
  342. SERIAL_ECHOPGM(" & Above");
  343. else
  344. SERIAL_ECHOPGM(" & Same Z as");
  345. SERIAL_ECHOLNPGM(" Nozzle)");
  346. #endif
  347. #if HAS_ABL_OR_UBL
  348. SERIAL_ECHOLNPGM("Auto Bed Leveling: "
  349. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  350. "LINEAR"
  351. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  352. "BILINEAR"
  353. #elif ENABLED(AUTO_BED_LEVELING_3POINT)
  354. "3POINT"
  355. #elif ENABLED(AUTO_BED_LEVELING_UBL)
  356. "UBL"
  357. #endif
  358. );
  359. if (planner.leveling_active) {
  360. SERIAL_ECHOLNPGM(" (enabled)");
  361. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  362. if (planner.z_fade_height)
  363. SERIAL_ECHOLNPAIR("Z Fade: ", planner.z_fade_height);
  364. #endif
  365. #if ABL_PLANAR
  366. const float diff[XYZ] = {
  367. planner.get_axis_position_mm(X_AXIS) - current_position[X_AXIS],
  368. planner.get_axis_position_mm(Y_AXIS) - current_position[Y_AXIS],
  369. planner.get_axis_position_mm(Z_AXIS) - current_position[Z_AXIS]
  370. };
  371. SERIAL_ECHOPGM("ABL Adjustment X");
  372. if (diff[X_AXIS] > 0) SERIAL_CHAR('+');
  373. SERIAL_ECHO(diff[X_AXIS]);
  374. SERIAL_ECHOPGM(" Y");
  375. if (diff[Y_AXIS] > 0) SERIAL_CHAR('+');
  376. SERIAL_ECHO(diff[Y_AXIS]);
  377. SERIAL_ECHOPGM(" Z");
  378. if (diff[Z_AXIS] > 0) SERIAL_CHAR('+');
  379. SERIAL_ECHO(diff[Z_AXIS]);
  380. #else
  381. #if ENABLED(AUTO_BED_LEVELING_UBL)
  382. SERIAL_ECHOPGM("UBL Adjustment Z");
  383. const float rz = ubl.get_z_correction(current_position[X_AXIS], current_position[Y_AXIS]);
  384. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  385. SERIAL_ECHOPGM("ABL Adjustment Z");
  386. const float rz = bilinear_z_offset(current_position);
  387. #endif
  388. SERIAL_ECHO(ftostr43sign(rz, '+'));
  389. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  390. if (planner.z_fade_height) {
  391. SERIAL_ECHOPAIR(" (", ftostr43sign(rz * planner.fade_scaling_factor_for_z(current_position[Z_AXIS]), '+'));
  392. SERIAL_CHAR(')');
  393. }
  394. #endif
  395. #endif
  396. }
  397. else
  398. SERIAL_ECHOLNPGM(" (disabled)");
  399. SERIAL_EOL();
  400. #elif ENABLED(MESH_BED_LEVELING)
  401. SERIAL_ECHOPGM("Mesh Bed Leveling");
  402. if (planner.leveling_active) {
  403. SERIAL_ECHOLNPGM(" (enabled)");
  404. SERIAL_ECHOPAIR("MBL Adjustment Z", ftostr43sign(mbl.get_z(current_position[X_AXIS], current_position[Y_AXIS]
  405. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  406. , 1.0
  407. #endif
  408. ), '+'));
  409. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  410. if (planner.z_fade_height) {
  411. SERIAL_ECHOPAIR(" (", ftostr43sign(
  412. mbl.get_z(current_position[X_AXIS], current_position[Y_AXIS], planner.fade_scaling_factor_for_z(current_position[Z_AXIS])), '+'
  413. ));
  414. SERIAL_CHAR(')');
  415. }
  416. #endif
  417. }
  418. else
  419. SERIAL_ECHOPGM(" (disabled)");
  420. SERIAL_EOL();
  421. #endif // MESH_BED_LEVELING
  422. }
  423. #endif // DEBUG_LEVELING_FEATURE