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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 <https://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. #define INTFLOAT(V,N) (((V) * 10 * pow(10, N) + ((V) < 0 ? -5: 5)) / 10) // pow10?
  31. #define UINTFLOAT(V,N) INTFLOAT((V) < 0 ? -(V) : (V), N)
  32. // Format uint8_t (0-100) as rj string with 123% / _12% / __1% format
  33. const char* pcttostrpctrj(const uint8_t i) {
  34. conv[3] = RJDIGIT(i, 100);
  35. conv[4] = RJDIGIT(i, 10);
  36. conv[5] = DIGIMOD(i, 1);
  37. conv[6] = '%';
  38. return &conv[3];
  39. }
  40. // Convert uint8_t (0-255) to a percentage, format as above
  41. const char* ui8tostr4pctrj(const uint8_t i) {
  42. return pcttostrpctrj(ui8_to_percent(i));
  43. }
  44. // Convert unsigned 8bit int to string 123 format
  45. const char* ui8tostr3rj(const uint8_t i) {
  46. conv[4] = RJDIGIT(i, 100);
  47. conv[5] = RJDIGIT(i, 10);
  48. conv[6] = DIGIMOD(i, 1);
  49. return &conv[4];
  50. }
  51. // Convert uint8_t to string with 12 format
  52. const char* ui8tostr2(const uint8_t i) {
  53. conv[5] = DIGIMOD(i, 10);
  54. conv[6] = DIGIMOD(i, 1);
  55. return &conv[5];
  56. }
  57. // Convert signed 8bit int to rj string with 123 or -12 format
  58. const char* i8tostr3rj(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. #if HAS_PRINT_PROGRESS_PERMYRIAD
  66. // Convert unsigned 16-bit permyriad to percent with 100 / 23.4 / 3.45 format
  67. const char* permyriadtostr4(const uint16_t xx) {
  68. if (xx >= 10000)
  69. return " 100"; // space to keep 4-width alignment
  70. else if (xx >= 1000) {
  71. conv[3] = DIGIMOD(xx, 1000);
  72. conv[4] = DIGIMOD(xx, 100);
  73. conv[5] = '.';
  74. conv[6] = DIGIMOD(xx, 10);
  75. return &conv[3];
  76. }
  77. else if (xx % 100 == 0) {
  78. conv[4] = ' ';
  79. conv[5] = RJDIGIT(xx, 1000);
  80. conv[6] = DIGIMOD(xx, 100);
  81. return &conv[4];
  82. }
  83. else {
  84. conv[3] = DIGIMOD(xx, 100);
  85. conv[4] = '.';
  86. conv[5] = DIGIMOD(xx, 10);
  87. conv[6] = RJDIGIT(xx, 1);
  88. return &conv[3];
  89. }
  90. }
  91. #endif
  92. // Convert unsigned 16bit int to string 12345 format
  93. const char* ui16tostr5rj(const uint16_t xx) {
  94. conv[2] = RJDIGIT(xx, 10000);
  95. conv[3] = RJDIGIT(xx, 1000);
  96. conv[4] = RJDIGIT(xx, 100);
  97. conv[5] = RJDIGIT(xx, 10);
  98. conv[6] = DIGIMOD(xx, 1);
  99. return &conv[2];
  100. }
  101. // Convert unsigned 16bit int to string 1234 format
  102. const char* ui16tostr4rj(const uint16_t xx) {
  103. conv[3] = RJDIGIT(xx, 1000);
  104. conv[4] = RJDIGIT(xx, 100);
  105. conv[5] = RJDIGIT(xx, 10);
  106. conv[6] = DIGIMOD(xx, 1);
  107. return &conv[3];
  108. }
  109. // Convert unsigned 16bit int to string 123 format
  110. const char* ui16tostr3rj(const uint16_t xx) {
  111. conv[4] = RJDIGIT(xx, 100);
  112. conv[5] = RJDIGIT(xx, 10);
  113. conv[6] = DIGIMOD(xx, 1);
  114. return &conv[4];
  115. }
  116. // Convert signed 16bit int to rj string with 123 or -12 format
  117. const char* i16tostr3rj(const int16_t x) {
  118. int xx = x;
  119. conv[4] = MINUSOR(xx, RJDIGIT(xx, 100));
  120. conv[5] = RJDIGIT(xx, 10);
  121. conv[6] = DIGIMOD(xx, 1);
  122. return &conv[4];
  123. }
  124. // Convert unsigned 16bit int to lj string with 123 format
  125. const char* i16tostr3left(const int16_t i) {
  126. char *str = &conv[6];
  127. *str = DIGIMOD(i, 1);
  128. if (i >= 10) {
  129. *(--str) = DIGIMOD(i, 10);
  130. if (i >= 100)
  131. *(--str) = DIGIMOD(i, 100);
  132. }
  133. return str;
  134. }
  135. // Convert signed 16bit int to rj string with 1234, _123, -123, _-12, or __-1 format
  136. const char* i16tostr4signrj(const int16_t i) {
  137. const bool neg = i < 0;
  138. const int ii = neg ? -i : i;
  139. if (i >= 1000) {
  140. conv[3] = DIGIMOD(ii, 1000);
  141. conv[4] = DIGIMOD(ii, 100);
  142. conv[5] = DIGIMOD(ii, 10);
  143. }
  144. else if (ii >= 100) {
  145. conv[3] = neg ? '-' : ' ';
  146. conv[4] = DIGIMOD(ii, 100);
  147. conv[5] = DIGIMOD(ii, 10);
  148. }
  149. else {
  150. conv[3] = ' ';
  151. conv[4] = ' ';
  152. if (ii >= 10) {
  153. conv[4] = neg ? '-' : ' ';
  154. conv[5] = DIGIMOD(ii, 10);
  155. }
  156. else {
  157. conv[5] = neg ? '-' : ' ';
  158. }
  159. }
  160. conv[6] = DIGIMOD(ii, 1);
  161. return &conv[3];
  162. }
  163. // Convert unsigned float to string with 1.1 format
  164. const char* ftostr11ns(const_float_t f) {
  165. const long i = UINTFLOAT(f, 1);
  166. conv[4] = DIGIMOD(i, 10);
  167. conv[5] = '.';
  168. conv[6] = DIGIMOD(i, 1);
  169. return &conv[4];
  170. }
  171. // Convert unsigned float to string with 1.23 format
  172. const char* ftostr12ns(const_float_t f) {
  173. const long i = UINTFLOAT(f, 2);
  174. conv[3] = DIGIMOD(i, 100);
  175. conv[4] = '.';
  176. conv[5] = DIGIMOD(i, 10);
  177. conv[6] = DIGIMOD(i, 1);
  178. return &conv[3];
  179. }
  180. // Convert unsigned float to string with 12.3 format
  181. const char* ftostr31ns(const_float_t f) {
  182. const long i = UINTFLOAT(f, 1);
  183. conv[3] = DIGIMOD(i, 100);
  184. conv[4] = DIGIMOD(i, 10);
  185. conv[5] = '.';
  186. conv[6] = DIGIMOD(i, 1);
  187. return &conv[3];
  188. }
  189. // Convert unsigned float to string with 123.4 format
  190. const char* ftostr41ns(const_float_t f) {
  191. const long i = UINTFLOAT(f, 1);
  192. conv[2] = DIGIMOD(i, 1000);
  193. conv[3] = DIGIMOD(i, 100);
  194. conv[4] = DIGIMOD(i, 10);
  195. conv[5] = '.';
  196. conv[6] = DIGIMOD(i, 1);
  197. return &conv[2];
  198. }
  199. // Convert signed float to fixed-length string with 12.34 / _2.34 / -2.34 or -23.45 / 123.45 format
  200. const char* ftostr42_52(const_float_t f) {
  201. if (f <= -10 || f >= 100) return ftostr52(f); // -23.45 / 123.45
  202. long i = INTFLOAT(f, 2);
  203. conv[2] = (f >= 0 && f < 10) ? ' ' : MINUSOR(i, DIGIMOD(i, 1000));
  204. conv[3] = DIGIMOD(i, 100);
  205. conv[4] = '.';
  206. conv[5] = DIGIMOD(i, 10);
  207. conv[6] = DIGIMOD(i, 1);
  208. return &conv[2];
  209. }
  210. // Convert signed float to fixed-length string with 023.45 / -23.45 format
  211. const char* ftostr52(const_float_t f) {
  212. long i = INTFLOAT(f, 2);
  213. conv[1] = MINUSOR(i, DIGIMOD(i, 10000));
  214. conv[2] = DIGIMOD(i, 1000);
  215. conv[3] = DIGIMOD(i, 100);
  216. conv[4] = '.';
  217. conv[5] = DIGIMOD(i, 10);
  218. conv[6] = DIGIMOD(i, 1);
  219. return &conv[1];
  220. }
  221. // Convert signed float to fixed-length string with 12.345 / _2.345 / -2.345 or -23.45 / 123.45 format
  222. const char* ftostr53_63(const_float_t f) {
  223. if (f <= -10 || f >= 100) return ftostr63(f); // -23.456 / 123.456
  224. long i = INTFLOAT(f, 3);
  225. conv[1] = (f >= 0 && f < 10) ? ' ' : MINUSOR(i, DIGIMOD(i, 10000));
  226. conv[2] = DIGIMOD(i, 1000);
  227. conv[3] = '.';
  228. conv[4] = DIGIMOD(i, 100);
  229. conv[5] = DIGIMOD(i, 10);
  230. conv[6] = DIGIMOD(i, 1);
  231. return &conv[1];
  232. }
  233. // Convert signed float to fixed-length string with 023.456 / -23.456 format
  234. const char* ftostr63(const_float_t f) {
  235. long i = INTFLOAT(f, 3);
  236. conv[0] = MINUSOR(i, DIGIMOD(i, 100000));
  237. conv[1] = DIGIMOD(i, 10000);
  238. conv[2] = DIGIMOD(i, 1000);
  239. conv[3] = '.';
  240. conv[4] = DIGIMOD(i, 100);
  241. conv[5] = DIGIMOD(i, 10);
  242. conv[6] = DIGIMOD(i, 1);
  243. return &conv[0];
  244. }
  245. #if ENABLED(LCD_DECIMAL_SMALL_XY)
  246. // Convert float to rj string with 1234, _123, -123, _-12, 12.3, _1.2, or -1.2 format
  247. const char* ftostr4sign(const_float_t f) {
  248. const int i = INTFLOAT(f, 1);
  249. if (!WITHIN(i, -99, 999)) return i16tostr4signrj((int)f);
  250. const bool neg = i < 0;
  251. const int ii = neg ? -i : i;
  252. conv[3] = neg ? '-' : (ii >= 100 ? DIGIMOD(ii, 100) : ' ');
  253. conv[4] = DIGIMOD(ii, 10);
  254. conv[5] = '.';
  255. conv[6] = DIGIMOD(ii, 1);
  256. return &conv[3];
  257. }
  258. #endif
  259. // Convert float to fixed-length string with +12.3 / -12.3 format
  260. const char* ftostr31sign(const_float_t f) {
  261. int i = INTFLOAT(f, 1);
  262. conv[2] = MINUSOR(i, '+');
  263. conv[3] = DIGIMOD(i, 100);
  264. conv[4] = DIGIMOD(i, 10);
  265. conv[5] = '.';
  266. conv[6] = DIGIMOD(i, 1);
  267. return &conv[2];
  268. }
  269. // Convert float to fixed-length string with +123.4 / -123.4 format
  270. const char* ftostr41sign(const_float_t f) {
  271. int i = INTFLOAT(f, 1);
  272. conv[1] = MINUSOR(i, '+');
  273. conv[2] = DIGIMOD(i, 1000);
  274. conv[3] = DIGIMOD(i, 100);
  275. conv[4] = DIGIMOD(i, 10);
  276. conv[5] = '.';
  277. conv[6] = DIGIMOD(i, 1);
  278. return &conv[1];
  279. }
  280. // Convert signed float to string (6 digit) with -1.234 / _0.000 / +1.234 format
  281. const char* ftostr43sign(const_float_t f, char plus/*=' '*/) {
  282. long i = INTFLOAT(f, 3);
  283. conv[1] = i ? MINUSOR(i, plus) : ' ';
  284. conv[2] = DIGIMOD(i, 1000);
  285. conv[3] = '.';
  286. conv[4] = DIGIMOD(i, 100);
  287. conv[5] = DIGIMOD(i, 10);
  288. conv[6] = DIGIMOD(i, 1);
  289. return &conv[1];
  290. }
  291. // Convert signed float to string (5 digit) with -1.2345 / _0.0000 / +1.2345 format
  292. const char* ftostr54sign(const_float_t f, char plus/*=' '*/) {
  293. long i = INTFLOAT(f, 4);
  294. conv[0] = i ? MINUSOR(i, plus) : ' ';
  295. conv[1] = DIGIMOD(i, 10000);
  296. conv[2] = '.';
  297. conv[3] = DIGIMOD(i, 1000);
  298. conv[4] = DIGIMOD(i, 100);
  299. conv[5] = DIGIMOD(i, 10);
  300. conv[6] = DIGIMOD(i, 1);
  301. return &conv[0];
  302. }
  303. // Convert unsigned float to rj string with 12345 format
  304. const char* ftostr5rj(const_float_t f) {
  305. const long i = UINTFLOAT(f, 0);
  306. return ui16tostr5rj(i);
  307. }
  308. // Convert signed float to string with +1234.5 format
  309. const char* ftostr51sign(const_float_t f) {
  310. long i = INTFLOAT(f, 1);
  311. conv[0] = MINUSOR(i, '+');
  312. conv[1] = DIGIMOD(i, 10000);
  313. conv[2] = DIGIMOD(i, 1000);
  314. conv[3] = DIGIMOD(i, 100);
  315. conv[4] = DIGIMOD(i, 10);
  316. conv[5] = '.';
  317. conv[6] = DIGIMOD(i, 1);
  318. return conv;
  319. }
  320. // Convert signed float to string with +123.45 format
  321. const char* ftostr52sign(const_float_t f) {
  322. long i = INTFLOAT(f, 2);
  323. conv[0] = MINUSOR(i, '+');
  324. conv[1] = DIGIMOD(i, 10000);
  325. conv[2] = DIGIMOD(i, 1000);
  326. conv[3] = DIGIMOD(i, 100);
  327. conv[4] = '.';
  328. conv[5] = DIGIMOD(i, 10);
  329. conv[6] = DIGIMOD(i, 1);
  330. return conv;
  331. }
  332. // Convert signed float to string with +12.345 format
  333. const char* ftostr53sign(const_float_t f) {
  334. long i = INTFLOAT(f, 3);
  335. conv[0] = MINUSOR(i, '+');
  336. conv[1] = DIGIMOD(i, 10000);
  337. conv[2] = DIGIMOD(i, 1000);
  338. conv[3] = '.';
  339. conv[4] = DIGIMOD(i, 100);
  340. conv[5] = DIGIMOD(i, 10);
  341. conv[6] = DIGIMOD(i, 1);
  342. return conv;
  343. }
  344. // Convert unsigned float to string with ____5.6, ___45.6, __345.6, _2345.6, 12345.6 format
  345. const char* ftostr61rj(const_float_t f) {
  346. const long i = UINTFLOAT(f, 1);
  347. conv[0] = RJDIGIT(i, 100000);
  348. conv[1] = RJDIGIT(i, 10000);
  349. conv[2] = RJDIGIT(i, 1000);
  350. conv[3] = RJDIGIT(i, 100);
  351. conv[4] = DIGIMOD(i, 10);
  352. conv[5] = '.';
  353. conv[6] = DIGIMOD(i, 1);
  354. return conv;
  355. }
  356. // Convert signed float to space-padded string with -_23.4_ format
  357. const char* ftostr52sp(const_float_t f) {
  358. long i = INTFLOAT(f, 2);
  359. uint8_t dig;
  360. conv[0] = MINUSOR(i, ' ');
  361. conv[1] = RJDIGIT(i, 10000);
  362. conv[2] = RJDIGIT(i, 1000);
  363. conv[3] = DIGIMOD(i, 100);
  364. if ((dig = i % 10)) { // second digit after decimal point?
  365. conv[4] = '.';
  366. conv[5] = DIGIMOD(i, 10);
  367. conv[6] = DIGIT(dig);
  368. }
  369. else {
  370. if ((dig = (i / 10) % 10)) { // first digit after decimal point?
  371. conv[4] = '.';
  372. conv[5] = DIGIT(dig);
  373. }
  374. else // nothing after decimal point
  375. conv[4] = conv[5] = ' ';
  376. conv[6] = ' ';
  377. }
  378. return conv;
  379. }