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.

numtostr.cpp 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 "numtostr.h"
  23. #include "../inc/MarlinConfigPre.h"
  24. #include "../core/utility.h"
  25. char conv[8] = { 0 };
  26. #define DIGIT(n) ('0' + (n))
  27. #define DIGIMOD(n, f) DIGIT((n)/(f) % 10)
  28. #define RJDIGIT(n, f) ((n) >= (f) ? DIGIMOD(n, f) : ' ')
  29. #define MINUSOR(n, alt) (n >= 0 ? (alt) : (n = -n, '-'))
  30. // Convert a full-range unsigned 8bit int to a percentage
  31. const char* ui8tostr4pctrj(const uint8_t i) {
  32. const uint8_t n = ui8_to_percent(i);
  33. conv[3] = RJDIGIT(n, 100);
  34. conv[4] = RJDIGIT(n, 10);
  35. conv[5] = DIGIMOD(n, 1);
  36. conv[6] = '%';
  37. return &conv[3];
  38. }
  39. // Convert unsigned 8bit int to string 123 format
  40. const char* ui8tostr3rj(const uint8_t i) {
  41. conv[4] = RJDIGIT(i, 100);
  42. conv[5] = RJDIGIT(i, 10);
  43. conv[6] = DIGIMOD(i, 1);
  44. return &conv[4];
  45. }
  46. // Convert signed 8bit int to rj string with 123 or -12 format
  47. const char* i8tostr3rj(const int8_t x) {
  48. int xx = x;
  49. conv[4] = MINUSOR(xx, RJDIGIT(xx, 100));
  50. conv[5] = RJDIGIT(xx, 10);
  51. conv[6] = DIGIMOD(xx, 1);
  52. return &conv[4];
  53. }
  54. #if HAS_PRINT_PROGRESS_PERMYRIAD
  55. // Convert unsigned 16-bit permyriad to percent with 100 / 23 / 23.4 / 3.45 format
  56. const char* permyriadtostr4(const uint16_t xx) {
  57. if (xx >= 10000)
  58. return "100";
  59. else if (xx >= 1000) {
  60. conv[3] = DIGIMOD(xx, 1000);
  61. conv[4] = DIGIMOD(xx, 100);
  62. conv[5] = '.';
  63. conv[6] = DIGIMOD(xx, 10);
  64. return &conv[3];
  65. }
  66. else if (xx % 100 == 0) {
  67. conv[4] = ' ';
  68. conv[5] = RJDIGIT(xx, 1000);
  69. conv[6] = DIGIMOD(xx, 100);
  70. return &conv[4];
  71. }
  72. else {
  73. conv[3] = DIGIMOD(xx, 100);
  74. conv[4] = '.';
  75. conv[5] = DIGIMOD(xx, 10);
  76. conv[6] = RJDIGIT(xx, 1);
  77. return &conv[3];
  78. }
  79. }
  80. #endif
  81. // Convert unsigned 16bit int to string 12345 format
  82. const char* ui16tostr5rj(const uint16_t xx) {
  83. conv[2] = RJDIGIT(xx, 10000);
  84. conv[3] = RJDIGIT(xx, 1000);
  85. conv[4] = RJDIGIT(xx, 100);
  86. conv[5] = RJDIGIT(xx, 10);
  87. conv[6] = DIGIMOD(xx, 1);
  88. return &conv[2];
  89. }
  90. // Convert unsigned 16bit int to string 1234 format
  91. const char* ui16tostr4rj(const uint16_t xx) {
  92. conv[3] = RJDIGIT(xx, 1000);
  93. conv[4] = RJDIGIT(xx, 100);
  94. conv[5] = RJDIGIT(xx, 10);
  95. conv[6] = DIGIMOD(xx, 1);
  96. return &conv[3];
  97. }
  98. // Convert unsigned 16bit int to string 123 format
  99. const char* ui16tostr3rj(const uint16_t xx) {
  100. conv[4] = RJDIGIT(xx, 100);
  101. conv[5] = RJDIGIT(xx, 10);
  102. conv[6] = DIGIMOD(xx, 1);
  103. return &conv[4];
  104. }
  105. // Convert signed 16bit int to rj string with 123 or -12 format
  106. const char* i16tostr3rj(const int16_t x) {
  107. int xx = x;
  108. conv[4] = MINUSOR(xx, RJDIGIT(xx, 100));
  109. conv[5] = RJDIGIT(xx, 10);
  110. conv[6] = DIGIMOD(xx, 1);
  111. return &conv[4];
  112. }
  113. // Convert unsigned 16bit int to lj string with 123 format
  114. const char* i16tostr3left(const int16_t i) {
  115. char *str = &conv[6];
  116. *str = DIGIMOD(i, 1);
  117. if (i >= 10) {
  118. *(--str) = DIGIMOD(i, 10);
  119. if (i >= 100)
  120. *(--str) = DIGIMOD(i, 100);
  121. }
  122. return str;
  123. }
  124. // Convert signed 16bit int to rj string with 1234, _123, -123, _-12, or __-1 format
  125. const char* i16tostr4signrj(const int16_t i) {
  126. const bool neg = i < 0;
  127. const int ii = neg ? -i : i;
  128. if (i >= 1000) {
  129. conv[3] = DIGIMOD(ii, 1000);
  130. conv[4] = DIGIMOD(ii, 100);
  131. conv[5] = DIGIMOD(ii, 10);
  132. }
  133. else if (ii >= 100) {
  134. conv[3] = neg ? '-' : ' ';
  135. conv[4] = DIGIMOD(ii, 100);
  136. conv[5] = DIGIMOD(ii, 10);
  137. }
  138. else {
  139. conv[3] = ' ';
  140. conv[4] = ' ';
  141. if (ii >= 10) {
  142. conv[4] = neg ? '-' : ' ';
  143. conv[5] = DIGIMOD(ii, 10);
  144. }
  145. else {
  146. conv[5] = neg ? '-' : ' ';
  147. }
  148. }
  149. conv[6] = DIGIMOD(ii, 1);
  150. return &conv[3];
  151. }
  152. // Convert unsigned float to string with 1.23 format
  153. const char* ftostr12ns(const float &f) {
  154. const long i = ((f < 0 ? -f : f) * 1000 + 5) / 10;
  155. conv[3] = DIGIMOD(i, 100);
  156. conv[4] = '.';
  157. conv[5] = DIGIMOD(i, 10);
  158. conv[6] = DIGIMOD(i, 1);
  159. return &conv[3];
  160. }
  161. // Convert signed float to fixed-length string with 12.34 / -2.34 or 023.45 / -23.45 format
  162. const char* ftostr42_52(const float &f) {
  163. if (f <= -10 || f >= 100) return ftostr52(f); // need more digits
  164. long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
  165. conv[2] = (f >= 0 && f < 10) ? ' ' : MINUSOR(i, DIGIMOD(i, 1000));
  166. conv[3] = DIGIMOD(i, 100);
  167. conv[4] = '.';
  168. conv[5] = DIGIMOD(i, 10);
  169. conv[6] = DIGIMOD(i, 1);
  170. return &conv[2];
  171. }
  172. // Convert signed float to fixed-length string with 023.45 / -23.45 format
  173. const char* ftostr52(const float &f) {
  174. long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
  175. conv[1] = MINUSOR(i, DIGIMOD(i, 10000));
  176. conv[2] = DIGIMOD(i, 1000);
  177. conv[3] = DIGIMOD(i, 100);
  178. conv[4] = '.';
  179. conv[5] = DIGIMOD(i, 10);
  180. conv[6] = DIGIMOD(i, 1);
  181. return &conv[1];
  182. }
  183. // Convert signed float to fixed-length string with 12.345 / -2.345 or 023.456 / -23.456 format
  184. const char* ftostr43_53(const float &f) {
  185. if (f <= -10 || f >= 100) return ftostr53(f); // need more digits
  186. long i = (f * 10000 + (f < 0 ? -5: 5)) / 10;
  187. conv[1] = (f >= 0 && f < 10) ? ' ' : MINUSOR(i, DIGIMOD(i, 10000));
  188. conv[2] = DIGIMOD(i, 1000);
  189. conv[3] = '.';
  190. conv[4] = DIGIMOD(i, 100);
  191. conv[5] = DIGIMOD(i, 10);
  192. conv[6] = DIGIMOD(i, 1);
  193. return &conv[1];
  194. }
  195. // Convert signed float to fixed-length string with 023.456 / -23.456 format
  196. const char* ftostr53(const float &f) {
  197. long i = (f * 10000 + (f < 0 ? -5: 5)) / 10;
  198. conv[0] = MINUSOR(i, DIGIMOD(i, 100000));
  199. conv[1] = DIGIMOD(i, 10000);
  200. conv[2] = DIGIMOD(i, 1000);
  201. conv[3] = '.';
  202. conv[4] = DIGIMOD(i, 100);
  203. conv[5] = DIGIMOD(i, 10);
  204. conv[6] = DIGIMOD(i, 1);
  205. return &conv[0];
  206. }
  207. #if ENABLED(LCD_DECIMAL_SMALL_XY)
  208. // Convert float to rj string with 1234, _123, -123, _-12, 12.3, _1.2, or -1.2 format
  209. const char* ftostr4sign(const float &f) {
  210. const int i = (f * 100 + (f < 0 ? -5: 5)) / 10;
  211. if (!WITHIN(i, -99, 999)) return i16tostr4signrj((int)f);
  212. const bool neg = i < 0;
  213. const int ii = neg ? -i : i;
  214. conv[3] = neg ? '-' : (ii >= 100 ? DIGIMOD(ii, 100) : ' ');
  215. conv[4] = DIGIMOD(ii, 10);
  216. conv[5] = '.';
  217. conv[6] = DIGIMOD(ii, 1);
  218. return &conv[3];
  219. }
  220. #endif
  221. // Convert float to fixed-length string with +123.4 / -123.4 format
  222. const char* ftostr41sign(const float &f) {
  223. int i = (f * 100 + (f < 0 ? -5: 5)) / 10;
  224. conv[1] = MINUSOR(i, '+');
  225. conv[2] = DIGIMOD(i, 1000);
  226. conv[3] = DIGIMOD(i, 100);
  227. conv[4] = DIGIMOD(i, 10);
  228. conv[5] = '.';
  229. conv[6] = DIGIMOD(i, 1);
  230. return &conv[1];
  231. }
  232. // Convert signed float to string (6 digit) with -1.234 / _0.000 / +1.234 format
  233. const char* ftostr43sign(const float &f, char plus/*=' '*/) {
  234. long i = (f * 10000 + (f < 0 ? -5: 5)) / 10;
  235. conv[1] = i ? MINUSOR(i, plus) : ' ';
  236. conv[2] = DIGIMOD(i, 1000);
  237. conv[3] = '.';
  238. conv[4] = DIGIMOD(i, 100);
  239. conv[5] = DIGIMOD(i, 10);
  240. conv[6] = DIGIMOD(i, 1);
  241. return &conv[1];
  242. }
  243. // Convert signed float to string (5 digit) with -1.2345 / _0.0000 / +1.2345 format
  244. const char* ftostr54sign(const float &f, char plus/*=' '*/) {
  245. long i = (f * 100000 + (f < 0 ? -5: 5)) / 10;
  246. conv[0] = i ? MINUSOR(i, plus) : ' ';
  247. conv[1] = DIGIMOD(i, 10000);
  248. conv[2] = '.';
  249. conv[3] = DIGIMOD(i, 1000);
  250. conv[4] = DIGIMOD(i, 100);
  251. conv[5] = DIGIMOD(i, 10);
  252. conv[6] = DIGIMOD(i, 1);
  253. return &conv[0];
  254. }
  255. // Convert unsigned float to rj string with 12345 format
  256. const char* ftostr5rj(const float &f) {
  257. const long i = ((f < 0 ? -f : f) * 10 + 5) / 10;
  258. return ui16tostr5rj(i);
  259. }
  260. // Convert signed float to string with +1234.5 format
  261. const char* ftostr51sign(const float &f) {
  262. long i = (f * 100 + (f < 0 ? -5: 5)) / 10;
  263. conv[0] = MINUSOR(i, '+');
  264. conv[1] = DIGIMOD(i, 10000);
  265. conv[2] = DIGIMOD(i, 1000);
  266. conv[3] = DIGIMOD(i, 100);
  267. conv[4] = DIGIMOD(i, 10);
  268. conv[5] = '.';
  269. conv[6] = DIGIMOD(i, 1);
  270. return conv;
  271. }
  272. // Convert signed float to string with +123.45 format
  273. const char* ftostr52sign(const float &f) {
  274. long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
  275. conv[0] = MINUSOR(i, '+');
  276. conv[1] = DIGIMOD(i, 10000);
  277. conv[2] = DIGIMOD(i, 1000);
  278. conv[3] = DIGIMOD(i, 100);
  279. conv[4] = '.';
  280. conv[5] = DIGIMOD(i, 10);
  281. conv[6] = DIGIMOD(i, 1);
  282. return conv;
  283. }
  284. // Convert unsigned float to string with ____4.5, __34.5, _234.5, 1234.5 format
  285. const char* ftostr51rj(const float &f) {
  286. const long i = ((f < 0 ? -f : f) * 100 + 5) / 10;
  287. conv[0] = ' ';
  288. conv[1] = RJDIGIT(i, 10000);
  289. conv[2] = RJDIGIT(i, 1000);
  290. conv[3] = RJDIGIT(i, 100);
  291. conv[4] = DIGIMOD(i, 10);
  292. conv[5] = '.';
  293. conv[6] = DIGIMOD(i, 1);
  294. return conv;
  295. }
  296. // Convert signed float to space-padded string with -_23.4_ format
  297. const char* ftostr52sp(const float &f) {
  298. long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
  299. uint8_t dig;
  300. conv[0] = MINUSOR(i, ' ');
  301. conv[1] = RJDIGIT(i, 10000);
  302. conv[2] = RJDIGIT(i, 1000);
  303. conv[3] = DIGIMOD(i, 100);
  304. if ((dig = i % 10)) { // second digit after decimal point?
  305. conv[4] = '.';
  306. conv[5] = DIGIMOD(i, 10);
  307. conv[6] = DIGIT(dig);
  308. }
  309. else {
  310. if ((dig = (i / 10) % 10)) { // first digit after decimal point?
  311. conv[4] = '.';
  312. conv[5] = DIGIT(dig);
  313. }
  314. else // nothing after decimal point
  315. conv[4] = conv[5] = ' ';
  316. conv[6] = ' ';
  317. }
  318. return conv;
  319. }