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

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