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

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