My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

utility.cpp 13KB

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