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

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