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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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 ENABLED(EEPROM_SETTINGS)
  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
  44. #if ENABLED(ULTRA_LCD) || ENABLED(DEBUG_LEVELING_FEATURE)
  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 unsigned int to string 123 format
  51. char* i8tostr3(const uint8_t i) {
  52. conv[4] = RJDIGIT(i, 100);
  53. conv[5] = RJDIGIT(i, 10);
  54. conv[6] = DIGIMOD(i, 1);
  55. return &conv[4];
  56. }
  57. // Convert signed int to rj string with 123 or -12 format
  58. char* itostr3(int i) {
  59. conv[4] = MINUSOR(i, RJDIGIT(i, 100));
  60. conv[5] = RJDIGIT(i, 10);
  61. conv[6] = DIGIMOD(i, 1);
  62. return &conv[4];
  63. }
  64. // Convert unsigned int to lj string with 123 format
  65. char* itostr3left(const int i) {
  66. char *str = &conv[6];
  67. *str = DIGIMOD(i, 1);
  68. if (i >= 10) {
  69. *(--str) = DIGIMOD(i, 10);
  70. if (i >= 100)
  71. *(--str) = DIGIMOD(i, 100);
  72. }
  73. return str;
  74. }
  75. // Convert signed int to rj string with 1234, _123, -123, _-12, or __-1 format
  76. char* itostr4sign(const int i) {
  77. const bool neg = i < 0;
  78. const int ii = neg ? -i : i;
  79. if (i >= 1000) {
  80. conv[3] = DIGIMOD(ii, 1000);
  81. conv[4] = DIGIMOD(ii, 100);
  82. conv[5] = DIGIMOD(ii, 10);
  83. }
  84. else if (ii >= 100) {
  85. conv[3] = neg ? '-' : ' ';
  86. conv[4] = DIGIMOD(ii, 100);
  87. conv[5] = DIGIMOD(ii, 10);
  88. }
  89. else {
  90. conv[3] = ' ';
  91. conv[4] = ' ';
  92. if (ii >= 10) {
  93. conv[4] = neg ? '-' : ' ';
  94. conv[5] = DIGIMOD(ii, 10);
  95. }
  96. else {
  97. conv[5] = neg ? '-' : ' ';
  98. }
  99. }
  100. conv[6] = DIGIMOD(ii, 1);
  101. return &conv[3];
  102. }
  103. // Convert unsigned float to string with 1.23 format
  104. char* ftostr12ns(const float &f) {
  105. const long i = ((f < 0 ? -f : f) * 1000 + 5) / 10;
  106. conv[3] = DIGIMOD(i, 100);
  107. conv[4] = '.';
  108. conv[5] = DIGIMOD(i, 10);
  109. conv[6] = DIGIMOD(i, 1);
  110. return &conv[3];
  111. }
  112. // Convert signed float to fixed-length string with 023.45 / -23.45 format
  113. char* ftostr32(const float &f) {
  114. long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
  115. conv[1] = MINUSOR(i, DIGIMOD(i, 10000));
  116. conv[2] = DIGIMOD(i, 1000);
  117. conv[3] = DIGIMOD(i, 100);
  118. conv[4] = '.';
  119. conv[5] = DIGIMOD(i, 10);
  120. conv[6] = DIGIMOD(i, 1);
  121. return &conv[1];
  122. }
  123. #if ENABLED(LCD_DECIMAL_SMALL_XY)
  124. // Convert float to rj string with 1234, _123, -123, _-12, 12.3, _1.2, or -1.2 format
  125. char* ftostr4sign(const float &f) {
  126. const int i = (f * 100 + (f < 0 ? -5: 5)) / 10;
  127. if (!WITHIN(i, -99, 999)) return itostr4sign((int)f);
  128. const bool neg = i < 0;
  129. const int ii = neg ? -i : i;
  130. conv[3] = neg ? '-' : (ii >= 100 ? DIGIMOD(ii, 100) : ' ');
  131. conv[4] = DIGIMOD(ii, 10);
  132. conv[5] = '.';
  133. conv[6] = DIGIMOD(ii, 1);
  134. return &conv[3];
  135. }
  136. #endif // LCD_DECIMAL_SMALL_XY
  137. // Convert float to fixed-length string with +123.4 / -123.4 format
  138. char* ftostr41sign(const float &f) {
  139. int i = (f * 100 + (f < 0 ? -5: 5)) / 10;
  140. conv[1] = MINUSOR(i, '+');
  141. conv[2] = DIGIMOD(i, 1000);
  142. conv[3] = DIGIMOD(i, 100);
  143. conv[4] = DIGIMOD(i, 10);
  144. conv[5] = '.';
  145. conv[6] = DIGIMOD(i, 1);
  146. return &conv[1];
  147. }
  148. // Convert signed float to string (6 digit) with -1.234 / _0.000 / +1.234 format
  149. char* ftostr43sign(const float &f, char plus/*=' '*/) {
  150. long i = (f * 10000 + (f < 0 ? -5: 5)) / 10;
  151. conv[1] = i ? MINUSOR(i, plus) : ' ';
  152. conv[2] = DIGIMOD(i, 1000);
  153. conv[3] = '.';
  154. conv[4] = DIGIMOD(i, 100);
  155. conv[5] = DIGIMOD(i, 10);
  156. conv[6] = DIGIMOD(i, 1);
  157. return &conv[1];
  158. }
  159. // Convert unsigned float to rj string with 12345 format
  160. char* ftostr5rj(const float &f) {
  161. const long i = ((f < 0 ? -f : f) * 10 + 5) / 10;
  162. conv[2] = RJDIGIT(i, 10000);
  163. conv[3] = RJDIGIT(i, 1000);
  164. conv[4] = RJDIGIT(i, 100);
  165. conv[5] = RJDIGIT(i, 10);
  166. conv[6] = DIGIMOD(i, 1);
  167. return &conv[2];
  168. }
  169. // Convert signed float to string with +1234.5 format
  170. char* ftostr51sign(const float &f) {
  171. long i = (f * 100 + (f < 0 ? -5: 5)) / 10;
  172. conv[0] = MINUSOR(i, '+');
  173. conv[1] = DIGIMOD(i, 10000);
  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;
  180. }
  181. // Convert signed float to string with +123.45 format
  182. char* ftostr52sign(const float &f) {
  183. long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
  184. conv[0] = MINUSOR(i, '+');
  185. conv[1] = DIGIMOD(i, 10000);
  186. conv[2] = DIGIMOD(i, 1000);
  187. conv[3] = DIGIMOD(i, 100);
  188. conv[4] = '.';
  189. conv[5] = DIGIMOD(i, 10);
  190. conv[6] = DIGIMOD(i, 1);
  191. return conv;
  192. }
  193. // Convert unsigned float to string with 1234.56 format omitting trailing zeros
  194. char* ftostr62rj(const float &f) {
  195. const long i = ((f < 0 ? -f : f) * 1000 + 5) / 10;
  196. conv[0] = RJDIGIT(i, 100000);
  197. conv[1] = RJDIGIT(i, 10000);
  198. conv[2] = RJDIGIT(i, 1000);
  199. conv[3] = DIGIMOD(i, 100);
  200. conv[4] = '.';
  201. conv[5] = DIGIMOD(i, 10);
  202. conv[6] = DIGIMOD(i, 1);
  203. return conv;
  204. }
  205. // Convert signed float to space-padded string with -_23.4_ format
  206. char* ftostr52sp(const float &f) {
  207. long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
  208. uint8_t dig;
  209. conv[1] = MINUSOR(i, RJDIGIT(i, 10000));
  210. conv[2] = RJDIGIT(i, 1000);
  211. conv[3] = DIGIMOD(i, 100);
  212. if ((dig = i % 10)) { // second digit after decimal point?
  213. conv[4] = '.';
  214. conv[5] = DIGIMOD(i, 10);
  215. conv[6] = DIGIT(dig);
  216. }
  217. else {
  218. if ((dig = (i / 10) % 10)) { // first digit after decimal point?
  219. conv[4] = '.';
  220. conv[5] = DIGIT(dig);
  221. }
  222. else // nothing after decimal point
  223. conv[4] = conv[5] = ' ';
  224. conv[6] = ' ';
  225. }
  226. return &conv[1];
  227. }
  228. #endif // ULTRA_LCD
  229. #if ENABLED(DEBUG_LEVELING_FEATURE)
  230. #include "../module/probe.h"
  231. #include "../module/motion.h"
  232. #include "../module/stepper.h"
  233. #include "../feature/bedlevel/bedlevel.h"
  234. void log_machine_info() {
  235. SERIAL_ECHOPGM("Machine Type: ");
  236. #if ENABLED(DELTA)
  237. SERIAL_ECHOLNPGM("Delta");
  238. #elif IS_SCARA
  239. SERIAL_ECHOLNPGM("SCARA");
  240. #elif IS_CORE
  241. SERIAL_ECHOLNPGM("Core");
  242. #else
  243. SERIAL_ECHOLNPGM("Cartesian");
  244. #endif
  245. SERIAL_ECHOPGM("Probe: ");
  246. #if ENABLED(PROBE_MANUALLY)
  247. SERIAL_ECHOLNPGM("PROBE_MANUALLY");
  248. #elif ENABLED(FIX_MOUNTED_PROBE)
  249. SERIAL_ECHOLNPGM("FIX_MOUNTED_PROBE");
  250. #elif ENABLED(BLTOUCH)
  251. SERIAL_ECHOLNPGM("BLTOUCH");
  252. #elif HAS_Z_SERVO_PROBE
  253. SERIAL_ECHOLNPGM("SERVO PROBE");
  254. #elif ENABLED(Z_PROBE_SLED)
  255. SERIAL_ECHOLNPGM("Z_PROBE_SLED");
  256. #elif ENABLED(Z_PROBE_ALLEN_KEY)
  257. SERIAL_ECHOLNPGM("Z_PROBE_ALLEN_KEY");
  258. #else
  259. SERIAL_ECHOLNPGM("NONE");
  260. #endif
  261. #if HAS_BED_PROBE
  262. SERIAL_ECHOPAIR("Probe Offset X:", X_PROBE_OFFSET_FROM_EXTRUDER);
  263. SERIAL_ECHOPAIR(" Y:", Y_PROBE_OFFSET_FROM_EXTRUDER);
  264. SERIAL_ECHOPAIR(" Z:", zprobe_zoffset);
  265. #if X_PROBE_OFFSET_FROM_EXTRUDER > 0
  266. SERIAL_ECHOPGM(" (Right");
  267. #elif X_PROBE_OFFSET_FROM_EXTRUDER < 0
  268. SERIAL_ECHOPGM(" (Left");
  269. #elif Y_PROBE_OFFSET_FROM_EXTRUDER != 0
  270. SERIAL_ECHOPGM(" (Middle");
  271. #else
  272. SERIAL_ECHOPGM(" (Aligned With");
  273. #endif
  274. #if Y_PROBE_OFFSET_FROM_EXTRUDER > 0
  275. SERIAL_ECHOPGM("-Back");
  276. #elif Y_PROBE_OFFSET_FROM_EXTRUDER < 0
  277. SERIAL_ECHOPGM("-Front");
  278. #elif X_PROBE_OFFSET_FROM_EXTRUDER != 0
  279. SERIAL_ECHOPGM("-Center");
  280. #endif
  281. if (zprobe_zoffset < 0)
  282. SERIAL_ECHOPGM(" & Below");
  283. else if (zprobe_zoffset > 0)
  284. SERIAL_ECHOPGM(" & Above");
  285. else
  286. SERIAL_ECHOPGM(" & Same Z as");
  287. SERIAL_ECHOLNPGM(" Nozzle)");
  288. #endif
  289. #if HAS_ABL
  290. SERIAL_ECHOPGM("Auto Bed Leveling: ");
  291. #if ENABLED(AUTO_BED_LEVELING_LINEAR)
  292. SERIAL_ECHOPGM("LINEAR");
  293. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  294. SERIAL_ECHOPGM("BILINEAR");
  295. #elif ENABLED(AUTO_BED_LEVELING_3POINT)
  296. SERIAL_ECHOPGM("3POINT");
  297. #elif ENABLED(AUTO_BED_LEVELING_UBL)
  298. SERIAL_ECHOPGM("UBL");
  299. #endif
  300. if (planner.leveling_active) {
  301. SERIAL_ECHOLNPGM(" (enabled)");
  302. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  303. if (planner.z_fade_height)
  304. SERIAL_ECHOLNPAIR("Z Fade: ", planner.z_fade_height);
  305. #endif
  306. #if ABL_PLANAR
  307. const float diff[XYZ] = {
  308. stepper.get_axis_position_mm(X_AXIS) - current_position[X_AXIS],
  309. stepper.get_axis_position_mm(Y_AXIS) - current_position[Y_AXIS],
  310. stepper.get_axis_position_mm(Z_AXIS) - current_position[Z_AXIS]
  311. };
  312. SERIAL_ECHOPGM("ABL Adjustment X");
  313. if (diff[X_AXIS] > 0) SERIAL_CHAR('+');
  314. SERIAL_ECHO(diff[X_AXIS]);
  315. SERIAL_ECHOPGM(" Y");
  316. if (diff[Y_AXIS] > 0) SERIAL_CHAR('+');
  317. SERIAL_ECHO(diff[Y_AXIS]);
  318. SERIAL_ECHOPGM(" Z");
  319. if (diff[Z_AXIS] > 0) SERIAL_CHAR('+');
  320. SERIAL_ECHO(diff[Z_AXIS]);
  321. #else
  322. #if ENABLED(AUTO_BED_LEVELING_UBL)
  323. SERIAL_ECHOPGM("UBL Adjustment Z");
  324. const float rz = ubl.get_z_correction(current_position[X_AXIS], current_position[Y_AXIS]);
  325. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  326. SERIAL_ECHOPGM("ABL Adjustment Z");
  327. const float rz = bilinear_z_offset(current_position);
  328. #endif
  329. SERIAL_ECHO(ftostr43sign(rz, '+'));
  330. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  331. if (planner.z_fade_height) {
  332. SERIAL_ECHOPAIR(" (", ftostr43sign(rz * planner.fade_scaling_factor_for_z(current_position[Z_AXIS]), '+'));
  333. SERIAL_CHAR(')');
  334. }
  335. #endif
  336. #endif
  337. }
  338. else
  339. SERIAL_ECHOLNPGM(" (disabled)");
  340. SERIAL_EOL();
  341. #elif ENABLED(MESH_BED_LEVELING)
  342. SERIAL_ECHOPGM("Mesh Bed Leveling");
  343. if (planner.leveling_active) {
  344. SERIAL_ECHOLNPGM(" (enabled)");
  345. SERIAL_ECHOPAIR("MBL Adjustment Z", ftostr43sign(mbl.get_z(current_position[X_AXIS], current_position[Y_AXIS], 1.0), '+'));
  346. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  347. if (planner.z_fade_height) {
  348. SERIAL_ECHOPAIR(" (", ftostr43sign(
  349. mbl.get_z(current_position[X_AXIS], current_position[Y_AXIS], planner.fade_scaling_factor_for_z(current_position[Z_AXIS])), '+'
  350. ));
  351. SERIAL_CHAR(')');
  352. }
  353. #endif
  354. }
  355. else
  356. SERIAL_ECHOPGM(" (disabled)");
  357. SERIAL_EOL();
  358. #endif // MESH_BED_LEVELING
  359. }
  360. #endif // DEBUG_LEVELING_FEATURE