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

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