My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

utility.cpp 7.0KB

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