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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 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. char* ui8tostr4pct(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. char* ui8tostr3(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. char* i8tostr3(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. // Convert unsigned 16bit int to string 12345 format
  55. char* ui16tostr5(const uint16_t xx) {
  56. conv[2] = RJDIGIT(xx, 10000);
  57. conv[3] = RJDIGIT(xx, 1000);
  58. conv[4] = RJDIGIT(xx, 100);
  59. conv[5] = RJDIGIT(xx, 10);
  60. conv[6] = DIGIMOD(xx, 1);
  61. return &conv[2];
  62. }
  63. // Convert unsigned 16bit int to string 1234 format
  64. char* ui16tostr4(const uint16_t xx) {
  65. conv[3] = RJDIGIT(xx, 1000);
  66. conv[4] = RJDIGIT(xx, 100);
  67. conv[5] = RJDIGIT(xx, 10);
  68. conv[6] = DIGIMOD(xx, 1);
  69. return &conv[3];
  70. }
  71. // Convert unsigned 16bit int to string 123 format
  72. char* ui16tostr3(const uint16_t xx) {
  73. conv[4] = RJDIGIT(xx, 100);
  74. conv[5] = RJDIGIT(xx, 10);
  75. conv[6] = DIGIMOD(xx, 1);
  76. return &conv[4];
  77. }
  78. // Convert signed 16bit int to rj string with 123 or -12 format
  79. char* i16tostr3(const int16_t x) {
  80. int xx = x;
  81. conv[4] = MINUSOR(xx, RJDIGIT(xx, 100));
  82. conv[5] = RJDIGIT(xx, 10);
  83. conv[6] = DIGIMOD(xx, 1);
  84. return &conv[4];
  85. }
  86. // Convert unsigned 16bit int to lj string with 123 format
  87. char* i16tostr3left(const int16_t i) {
  88. char *str = &conv[6];
  89. *str = DIGIMOD(i, 1);
  90. if (i >= 10) {
  91. *(--str) = DIGIMOD(i, 10);
  92. if (i >= 100)
  93. *(--str) = DIGIMOD(i, 100);
  94. }
  95. return str;
  96. }
  97. // Convert signed 16bit int to rj string with 1234, _123, -123, _-12, or __-1 format
  98. char* i16tostr4sign(const int16_t i) {
  99. const bool neg = i < 0;
  100. const int ii = neg ? -i : i;
  101. if (i >= 1000) {
  102. conv[3] = DIGIMOD(ii, 1000);
  103. conv[4] = DIGIMOD(ii, 100);
  104. conv[5] = DIGIMOD(ii, 10);
  105. }
  106. else if (ii >= 100) {
  107. conv[3] = neg ? '-' : ' ';
  108. conv[4] = DIGIMOD(ii, 100);
  109. conv[5] = DIGIMOD(ii, 10);
  110. }
  111. else {
  112. conv[3] = ' ';
  113. conv[4] = ' ';
  114. if (ii >= 10) {
  115. conv[4] = neg ? '-' : ' ';
  116. conv[5] = DIGIMOD(ii, 10);
  117. }
  118. else {
  119. conv[5] = neg ? '-' : ' ';
  120. }
  121. }
  122. conv[6] = DIGIMOD(ii, 1);
  123. return &conv[3];
  124. }
  125. // Convert unsigned float to string with 1.23 format
  126. char* ftostr12ns(const float &f) {
  127. const long i = ((f < 0 ? -f : f) * 1000 + 5) / 10;
  128. conv[3] = DIGIMOD(i, 100);
  129. conv[4] = '.';
  130. conv[5] = DIGIMOD(i, 10);
  131. conv[6] = DIGIMOD(i, 1);
  132. return &conv[3];
  133. }
  134. // Convert signed float to fixed-length string with 12.34 / -2.34 format or 123.45 / -23.45 format
  135. char* ftostr42_52(const float &f) {
  136. if (f <= -10 || f >= 100) return ftostr52(f); // need more digits
  137. long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
  138. conv[2] = (f >= 0 && f < 10) ? ' ' : MINUSOR(i, DIGIMOD(i, 1000));
  139. conv[3] = DIGIMOD(i, 100);
  140. conv[4] = '.';
  141. conv[5] = DIGIMOD(i, 10);
  142. conv[6] = DIGIMOD(i, 1);
  143. return &conv[2];
  144. }
  145. // Convert signed float to fixed-length string with 023.45 / -23.45 format
  146. char* ftostr52(const float &f) {
  147. long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
  148. conv[1] = MINUSOR(i, DIGIMOD(i, 10000));
  149. conv[2] = DIGIMOD(i, 1000);
  150. conv[3] = DIGIMOD(i, 100);
  151. conv[4] = '.';
  152. conv[5] = DIGIMOD(i, 10);
  153. conv[6] = DIGIMOD(i, 1);
  154. return &conv[1];
  155. }
  156. #if ENABLED(LCD_DECIMAL_SMALL_XY)
  157. // Convert float to rj string with 1234, _123, -123, _-12, 12.3, _1.2, or -1.2 format
  158. char* ftostr4sign(const float &f) {
  159. const int i = (f * 100 + (f < 0 ? -5: 5)) / 10;
  160. if (!WITHIN(i, -99, 999)) return i16tostr4sign((int)f);
  161. const bool neg = i < 0;
  162. const int ii = neg ? -i : i;
  163. conv[3] = neg ? '-' : (ii >= 100 ? DIGIMOD(ii, 100) : ' ');
  164. conv[4] = DIGIMOD(ii, 10);
  165. conv[5] = '.';
  166. conv[6] = DIGIMOD(ii, 1);
  167. return &conv[3];
  168. }
  169. #endif
  170. // Convert float to fixed-length string with +123.4 / -123.4 format
  171. char* ftostr41sign(const float &f) {
  172. int i = (f * 100 + (f < 0 ? -5: 5)) / 10;
  173. conv[1] = MINUSOR(i, '+');
  174. conv[2] = DIGIMOD(i, 1000);
  175. conv[3] = DIGIMOD(i, 100);
  176. conv[4] = DIGIMOD(i, 10);
  177. conv[5] = '.';
  178. conv[6] = DIGIMOD(i, 1);
  179. return &conv[1];
  180. }
  181. // Convert signed float to string (6 digit) with -1.234 / _0.000 / +1.234 format
  182. char* ftostr43sign(const float &f, char plus/*=' '*/) {
  183. long i = (f * 10000 + (f < 0 ? -5: 5)) / 10;
  184. conv[1] = i ? MINUSOR(i, plus) : ' ';
  185. conv[2] = DIGIMOD(i, 1000);
  186. conv[3] = '.';
  187. conv[4] = DIGIMOD(i, 100);
  188. conv[5] = DIGIMOD(i, 10);
  189. conv[6] = DIGIMOD(i, 1);
  190. return &conv[1];
  191. }
  192. // Convert signed float to string (5 digit) with -1.2345 / _0.0000 / +1.2345 format
  193. char* ftostr54sign(const float &f, char plus/*=' '*/) {
  194. long i = (f * 100000 + (f < 0 ? -5: 5)) / 10;
  195. conv[0] = i ? MINUSOR(i, plus) : ' ';
  196. conv[1] = DIGIMOD(i, 10000);
  197. conv[2] = '.';
  198. conv[3] = DIGIMOD(i, 1000);
  199. conv[4] = DIGIMOD(i, 100);
  200. conv[5] = DIGIMOD(i, 10);
  201. conv[6] = DIGIMOD(i, 1);
  202. return &conv[0];
  203. }
  204. // Convert unsigned float to rj string with 12345 format
  205. char* ftostr5rj(const float &f) {
  206. const long i = ((f < 0 ? -f : f) * 10 + 5) / 10;
  207. return ui16tostr5(i);
  208. }
  209. // Convert signed float to string with +1234.5 format
  210. char* ftostr51sign(const float &f) {
  211. long i = (f * 100 + (f < 0 ? -5: 5)) / 10;
  212. conv[0] = MINUSOR(i, '+');
  213. conv[1] = DIGIMOD(i, 10000);
  214. conv[2] = DIGIMOD(i, 1000);
  215. conv[3] = DIGIMOD(i, 100);
  216. conv[4] = DIGIMOD(i, 10);
  217. conv[5] = '.';
  218. conv[6] = DIGIMOD(i, 1);
  219. return conv;
  220. }
  221. // Convert signed float to string with +123.45 format
  222. char* ftostr52sign(const float &f) {
  223. long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
  224. conv[0] = MINUSOR(i, '+');
  225. conv[1] = DIGIMOD(i, 10000);
  226. conv[2] = DIGIMOD(i, 1000);
  227. conv[3] = DIGIMOD(i, 100);
  228. conv[4] = '.';
  229. conv[5] = DIGIMOD(i, 10);
  230. conv[6] = DIGIMOD(i, 1);
  231. return conv;
  232. }
  233. // Convert unsigned float to string with 1234.5 format omitting trailing zeros
  234. char* ftostr51rj(const float &f) {
  235. const long i = ((f < 0 ? -f : f) * 100 + 5) / 10;
  236. conv[0] = ' ';
  237. conv[1] = RJDIGIT(i, 10000);
  238. conv[2] = RJDIGIT(i, 1000);
  239. conv[3] = RJDIGIT(i, 100);
  240. conv[4] = DIGIMOD(i, 10);
  241. conv[5] = '.';
  242. conv[6] = DIGIMOD(i, 1);
  243. return conv;
  244. }
  245. // Convert signed float to space-padded string with -_23.4_ format
  246. char* ftostr52sp(const float &f) {
  247. long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
  248. uint8_t dig;
  249. conv[0] = MINUSOR(i, ' ');
  250. conv[1] = RJDIGIT(i, 10000);
  251. conv[2] = RJDIGIT(i, 1000);
  252. conv[3] = DIGIMOD(i, 100);
  253. if ((dig = i % 10)) { // second digit after decimal point?
  254. conv[4] = '.';
  255. conv[5] = DIGIMOD(i, 10);
  256. conv[6] = DIGIT(dig);
  257. }
  258. else {
  259. if ((dig = (i / 10) % 10)) { // first digit after decimal point?
  260. conv[4] = '.';
  261. conv[5] = DIGIT(dig);
  262. }
  263. else // nothing after decimal point
  264. conv[4] = conv[5] = ' ';
  265. conv[6] = ' ';
  266. }
  267. return conv;
  268. }