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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 "Marlin.h"
  23. #include "utility.h"
  24. #include "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 with 12 format
  51. char* itostr2(const uint8_t& xx) {
  52. conv[5] = DIGIMOD(xx, 10);
  53. conv[6] = DIGIMOD(xx, 1);
  54. return &conv[5];
  55. }
  56. // Convert signed int to rj string with 123 or -12 format
  57. char* itostr3(const int& x) {
  58. int xx = x;
  59. conv[4] = MINUSOR(xx, RJDIGIT(xx, 100));
  60. conv[5] = RJDIGIT(xx, 10);
  61. conv[6] = DIGIMOD(xx, 1);
  62. return &conv[4];
  63. }
  64. // Convert unsigned int to lj string with 123 format
  65. char* itostr3left(const int& xx) {
  66. char *str = &conv[6];
  67. *str = DIGIMOD(xx, 1);
  68. if (xx >= 10) {
  69. *(--str) = DIGIMOD(xx, 10);
  70. if (xx >= 100)
  71. *(--str) = DIGIMOD(xx, 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& x) {
  77. const bool neg = x < 0;
  78. const int xx = neg ? -x : x;
  79. if (x >= 1000) {
  80. conv[3] = DIGIMOD(xx, 1000);
  81. conv[4] = DIGIMOD(xx, 100);
  82. conv[5] = DIGIMOD(xx, 10);
  83. }
  84. else {
  85. if (xx >= 100) {
  86. conv[3] = neg ? '-' : ' ';
  87. conv[4] = DIGIMOD(xx, 100);
  88. conv[5] = DIGIMOD(xx, 10);
  89. }
  90. else {
  91. conv[3] = ' ';
  92. conv[4] = ' ';
  93. if (xx >= 10) {
  94. conv[4] = neg ? '-' : ' ';
  95. conv[5] = DIGIMOD(xx, 10);
  96. }
  97. else {
  98. conv[5] = neg ? '-' : ' ';
  99. }
  100. }
  101. }
  102. conv[6] = DIGIMOD(xx, 1);
  103. return &conv[3];
  104. }
  105. // Convert unsigned float to string with 1.23 format
  106. char* ftostr12ns(const float& x) {
  107. const long xx = (x < 0 ? -x : x) * 100;
  108. conv[3] = DIGIMOD(xx, 100);
  109. conv[4] = '.';
  110. conv[5] = DIGIMOD(xx, 10);
  111. conv[6] = DIGIMOD(xx, 1);
  112. return &conv[3];
  113. }
  114. // Convert signed float to fixed-length string with 023.45 / -23.45 format
  115. char *ftostr32(const float& x) {
  116. long xx = x * 100;
  117. conv[1] = MINUSOR(xx, DIGIMOD(xx, 10000));
  118. conv[2] = DIGIMOD(xx, 1000);
  119. conv[3] = DIGIMOD(xx, 100);
  120. conv[4] = '.';
  121. conv[5] = DIGIMOD(xx, 10);
  122. conv[6] = DIGIMOD(xx, 1);
  123. return &conv[1];
  124. }
  125. #if ENABLED(LCD_DECIMAL_SMALL_XY)
  126. // Convert float to rj string with 1234, _123, -123, _-12, 12.3, _1.2, or -1.2 format
  127. char *ftostr4sign(const float& fx) {
  128. const int x = fx * 10;
  129. if (!WITHIN(x, -99, 999)) return itostr4sign((int)fx);
  130. const bool neg = x < 0;
  131. const int xx = neg ? -x : x;
  132. conv[3] = neg ? '-' : (xx >= 100 ? DIGIMOD(xx, 100) : ' ');
  133. conv[4] = DIGIMOD(xx, 10);
  134. conv[5] = '.';
  135. conv[6] = DIGIMOD(xx, 1);
  136. return &conv[3];
  137. }
  138. #endif // LCD_DECIMAL_SMALL_XY
  139. // Convert float to fixed-length string with +123.4 / -123.4 format
  140. char* ftostr41sign(const float& x) {
  141. int xx = x * 10;
  142. conv[1] = MINUSOR(xx, '+');
  143. conv[2] = DIGIMOD(xx, 1000);
  144. conv[3] = DIGIMOD(xx, 100);
  145. conv[4] = DIGIMOD(xx, 10);
  146. conv[5] = '.';
  147. conv[6] = DIGIMOD(xx, 1);
  148. return &conv[1];
  149. }
  150. // Convert signed float to string (6 digit) with -1.234 / _0.000 / +1.234 format
  151. char* ftostr43sign(const float& x, char plus/*=' '*/) {
  152. long xx = x * 1000;
  153. conv[1] = xx ? MINUSOR(xx, plus) : ' ';
  154. conv[2] = DIGIMOD(xx, 1000);
  155. conv[3] = '.';
  156. conv[4] = DIGIMOD(xx, 100);
  157. conv[5] = DIGIMOD(xx, 10);
  158. conv[6] = DIGIMOD(xx, 1);
  159. return &conv[1];
  160. }
  161. // Convert unsigned float to rj string with 12345 format
  162. char* ftostr5rj(const float& x) {
  163. const long xx = x < 0 ? -x : x;
  164. conv[2] = RJDIGIT(xx, 10000);
  165. conv[3] = RJDIGIT(xx, 1000);
  166. conv[4] = RJDIGIT(xx, 100);
  167. conv[5] = RJDIGIT(xx, 10);
  168. conv[6] = DIGIMOD(xx, 1);
  169. return &conv[2];
  170. }
  171. // Convert signed float to string with +1234.5 format
  172. char* ftostr51sign(const float& x) {
  173. long xx = x * 10;
  174. conv[0] = MINUSOR(xx, '+');
  175. conv[1] = DIGIMOD(xx, 10000);
  176. conv[2] = DIGIMOD(xx, 1000);
  177. conv[3] = DIGIMOD(xx, 100);
  178. conv[4] = DIGIMOD(xx, 10);
  179. conv[5] = '.';
  180. conv[6] = DIGIMOD(xx, 1);
  181. return conv;
  182. }
  183. // Convert signed float to string with +123.45 format
  184. char* ftostr52sign(const float& x) {
  185. long xx = x * 100;
  186. conv[0] = MINUSOR(xx, '+');
  187. conv[1] = DIGIMOD(xx, 10000);
  188. conv[2] = DIGIMOD(xx, 1000);
  189. conv[3] = DIGIMOD(xx, 100);
  190. conv[4] = '.';
  191. conv[5] = DIGIMOD(xx, 10);
  192. conv[6] = DIGIMOD(xx, 1);
  193. return conv;
  194. }
  195. // Convert unsigned float to string with 1234.56 format omitting trailing zeros
  196. char* ftostr62rj(const float& x) {
  197. const long xx = (x < 0 ? -x : x) * 100;
  198. conv[0] = RJDIGIT(xx, 100000);
  199. conv[1] = RJDIGIT(xx, 10000);
  200. conv[2] = RJDIGIT(xx, 1000);
  201. conv[3] = DIGIMOD(xx, 100);
  202. conv[4] = '.';
  203. conv[5] = DIGIMOD(xx, 10);
  204. conv[6] = DIGIMOD(xx, 1);
  205. return conv;
  206. }
  207. // Convert signed float to space-padded string with -_23.4_ format
  208. char* ftostr52sp(const float& x) {
  209. long xx = x * 100;
  210. uint8_t dig;
  211. conv[1] = MINUSOR(xx, RJDIGIT(xx, 10000));
  212. conv[2] = RJDIGIT(xx, 1000);
  213. conv[3] = DIGIMOD(xx, 100);
  214. if ((dig = xx % 10)) { // second digit after decimal point?
  215. conv[4] = '.';
  216. conv[5] = DIGIMOD(xx, 10);
  217. conv[6] = DIGIT(dig);
  218. }
  219. else {
  220. if ((dig = (xx / 10) % 10)) { // first digit after decimal point?
  221. conv[4] = '.';
  222. conv[5] = DIGIT(dig);
  223. }
  224. else // nothing after decimal point
  225. conv[4] = conv[5] = ' ';
  226. conv[6] = ' ';
  227. }
  228. return &conv[1];
  229. }
  230. #endif // ULTRA_LCD