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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 unsigned float to string with 12.3 format
  162. const char* ftostr31ns(const float &f) {
  163. const long i = ((f < 0 ? -f : f) * 100 + 5) / 10;
  164. conv[3] = DIGIMOD(i, 100);
  165. conv[4] = DIGIMOD(i, 10);
  166. conv[5] = '.';
  167. conv[6] = DIGIMOD(i, 1);
  168. return &conv[3];
  169. }
  170. // Convert unsigned float to string with 123.4 format
  171. const char* ftostr41ns(const float &f) {
  172. const long i = ((f < 0 ? -f : f) * 100 + 5) / 10;
  173. conv[2] = DIGIMOD(i, 1000);
  174. conv[3] = DIGIMOD(i, 100);
  175. conv[4] = DIGIMOD(i, 10);
  176. conv[5] = '.';
  177. conv[6] = DIGIMOD(i, 1);
  178. return &conv[2];
  179. }
  180. // Convert signed float to fixed-length string with 12.34 / _2.34 / -2.34 or -23.45 / 123.45 format
  181. const char* ftostr42_52(const float &f) {
  182. if (f <= -10 || f >= 100) return ftostr52(f); // -23.45 / 123.45
  183. long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
  184. conv[2] = (f >= 0 && f < 10) ? ' ' : MINUSOR(i, DIGIMOD(i, 1000));
  185. conv[3] = DIGIMOD(i, 100);
  186. conv[4] = '.';
  187. conv[5] = DIGIMOD(i, 10);
  188. conv[6] = DIGIMOD(i, 1);
  189. return &conv[2];
  190. }
  191. // Convert signed float to fixed-length string with 023.45 / -23.45 format
  192. const char* ftostr52(const float &f) {
  193. long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
  194. conv[1] = MINUSOR(i, DIGIMOD(i, 10000));
  195. conv[2] = DIGIMOD(i, 1000);
  196. conv[3] = DIGIMOD(i, 100);
  197. conv[4] = '.';
  198. conv[5] = DIGIMOD(i, 10);
  199. conv[6] = DIGIMOD(i, 1);
  200. return &conv[1];
  201. }
  202. // Convert signed float to fixed-length string with 12.345 / _2.345 / -2.345 or -23.45 / 123.45 format
  203. const char* ftostr53_63(const float &f) {
  204. if (f <= -10 || f >= 100) return ftostr63(f); // -23.456 / 123.456
  205. long i = (f * 10000 + (f < 0 ? -5: 5)) / 10;
  206. conv[1] = (f >= 0 && f < 10) ? ' ' : MINUSOR(i, DIGIMOD(i, 10000));
  207. conv[2] = DIGIMOD(i, 1000);
  208. conv[3] = '.';
  209. conv[4] = DIGIMOD(i, 100);
  210. conv[5] = DIGIMOD(i, 10);
  211. conv[6] = DIGIMOD(i, 1);
  212. return &conv[1];
  213. }
  214. // Convert signed float to fixed-length string with 023.456 / -23.456 format
  215. const char* ftostr63(const float &f) {
  216. long i = (f * 10000 + (f < 0 ? -5: 5)) / 10;
  217. conv[0] = MINUSOR(i, DIGIMOD(i, 100000));
  218. conv[1] = DIGIMOD(i, 10000);
  219. conv[2] = DIGIMOD(i, 1000);
  220. conv[3] = '.';
  221. conv[4] = DIGIMOD(i, 100);
  222. conv[5] = DIGIMOD(i, 10);
  223. conv[6] = DIGIMOD(i, 1);
  224. return &conv[0];
  225. }
  226. #if ENABLED(LCD_DECIMAL_SMALL_XY)
  227. // Convert float to rj string with 1234, _123, -123, _-12, 12.3, _1.2, or -1.2 format
  228. const char* ftostr4sign(const float &f) {
  229. const int i = (f * 100 + (f < 0 ? -5: 5)) / 10;
  230. if (!WITHIN(i, -99, 999)) return i16tostr4signrj((int)f);
  231. const bool neg = i < 0;
  232. const int ii = neg ? -i : i;
  233. conv[3] = neg ? '-' : (ii >= 100 ? DIGIMOD(ii, 100) : ' ');
  234. conv[4] = DIGIMOD(ii, 10);
  235. conv[5] = '.';
  236. conv[6] = DIGIMOD(ii, 1);
  237. return &conv[3];
  238. }
  239. #endif
  240. // Convert float to fixed-length string with +123.4 / -123.4 format
  241. const char* ftostr41sign(const float &f) {
  242. int i = (f * 100 + (f < 0 ? -5: 5)) / 10;
  243. conv[1] = MINUSOR(i, '+');
  244. conv[2] = DIGIMOD(i, 1000);
  245. conv[3] = DIGIMOD(i, 100);
  246. conv[4] = DIGIMOD(i, 10);
  247. conv[5] = '.';
  248. conv[6] = DIGIMOD(i, 1);
  249. return &conv[1];
  250. }
  251. // Convert signed float to string (6 digit) with -1.234 / _0.000 / +1.234 format
  252. const char* ftostr43sign(const float &f, char plus/*=' '*/) {
  253. long i = (f * 10000 + (f < 0 ? -5: 5)) / 10;
  254. conv[1] = i ? MINUSOR(i, plus) : ' ';
  255. conv[2] = DIGIMOD(i, 1000);
  256. conv[3] = '.';
  257. conv[4] = DIGIMOD(i, 100);
  258. conv[5] = DIGIMOD(i, 10);
  259. conv[6] = DIGIMOD(i, 1);
  260. return &conv[1];
  261. }
  262. // Convert signed float to string (5 digit) with -1.2345 / _0.0000 / +1.2345 format
  263. const char* ftostr54sign(const float &f, char plus/*=' '*/) {
  264. long i = (f * 100000 + (f < 0 ? -5: 5)) / 10;
  265. conv[0] = i ? MINUSOR(i, plus) : ' ';
  266. conv[1] = DIGIMOD(i, 10000);
  267. conv[2] = '.';
  268. conv[3] = DIGIMOD(i, 1000);
  269. conv[4] = DIGIMOD(i, 100);
  270. conv[5] = DIGIMOD(i, 10);
  271. conv[6] = DIGIMOD(i, 1);
  272. return &conv[0];
  273. }
  274. // Convert unsigned float to rj string with 12345 format
  275. const char* ftostr5rj(const float &f) {
  276. const long i = ((f < 0 ? -f : f) * 10 + 5) / 10;
  277. return ui16tostr5rj(i);
  278. }
  279. // Convert signed float to string with +1234.5 format
  280. const char* ftostr51sign(const float &f) {
  281. long i = (f * 100 + (f < 0 ? -5: 5)) / 10;
  282. conv[0] = MINUSOR(i, '+');
  283. conv[1] = DIGIMOD(i, 10000);
  284. conv[2] = DIGIMOD(i, 1000);
  285. conv[3] = DIGIMOD(i, 100);
  286. conv[4] = DIGIMOD(i, 10);
  287. conv[5] = '.';
  288. conv[6] = DIGIMOD(i, 1);
  289. return conv;
  290. }
  291. // Convert signed float to string with +123.45 format
  292. const char* ftostr52sign(const float &f) {
  293. long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
  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] = '.';
  299. conv[5] = DIGIMOD(i, 10);
  300. conv[6] = DIGIMOD(i, 1);
  301. return conv;
  302. }
  303. // Convert signed float to string with +12.345 format
  304. const char* ftostr53sign(const float &f) {
  305. long i = (f * 10000 + (f < 0 ? -5: 5)) / 10;
  306. conv[0] = MINUSOR(i, '+');
  307. conv[1] = DIGIMOD(i, 10000);
  308. conv[2] = DIGIMOD(i, 1000);
  309. conv[3] = '.';
  310. conv[4] = DIGIMOD(i, 100);
  311. conv[5] = DIGIMOD(i, 10);
  312. conv[6] = DIGIMOD(i, 1);
  313. return conv;
  314. }
  315. // Convert unsigned float to string with ____4.5, __34.5, _234.5, 1234.5 format
  316. const char* ftostr51rj(const float &f) {
  317. const long i = ((f < 0 ? -f : f) * 100 + 5) / 10;
  318. conv[0] = ' ';
  319. conv[1] = RJDIGIT(i, 10000);
  320. conv[2] = RJDIGIT(i, 1000);
  321. conv[3] = RJDIGIT(i, 100);
  322. conv[4] = DIGIMOD(i, 10);
  323. conv[5] = '.';
  324. conv[6] = DIGIMOD(i, 1);
  325. return conv;
  326. }
  327. // Convert signed float to space-padded string with -_23.4_ format
  328. const char* ftostr52sp(const float &f) {
  329. long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
  330. uint8_t dig;
  331. conv[0] = MINUSOR(i, ' ');
  332. conv[1] = RJDIGIT(i, 10000);
  333. conv[2] = RJDIGIT(i, 1000);
  334. conv[3] = DIGIMOD(i, 100);
  335. if ((dig = i % 10)) { // second digit after decimal point?
  336. conv[4] = '.';
  337. conv[5] = DIGIMOD(i, 10);
  338. conv[6] = DIGIT(dig);
  339. }
  340. else {
  341. if ((dig = (i / 10) % 10)) { // first digit after decimal point?
  342. conv[4] = '.';
  343. conv[5] = DIGIT(dig);
  344. }
  345. else // nothing after decimal point
  346. conv[4] = conv[5] = ' ';
  347. conv[6] = ' ';
  348. }
  349. return conv;
  350. }