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.

MarlinSerial.cpp 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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. /**
  23. * MarlinSerial.cpp - Hardware serial library for Wiring
  24. * Copyright (c) 2006 Nicholas Zambetti. All right reserved.
  25. *
  26. * Modified 23 November 2006 by David A. Mellis
  27. * Modified 28 September 2010 by Mark Sproul
  28. * Modified 14 February 2016 by Andreas Hardtung (added tx buffer)
  29. */
  30. #include "MarlinSerial.h"
  31. #include "Marlin.h"
  32. // Disable HardwareSerial.cpp to support chips without a UART (Attiny, etc.)
  33. #if !defined(USBCON) && (defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H))
  34. #if UART_PRESENT(SERIAL_PORT)
  35. ring_buffer_r rx_buffer = { { 0 }, 0, 0 };
  36. #if TX_BUFFER_SIZE > 0
  37. ring_buffer_t tx_buffer = { { 0 }, 0, 0 };
  38. static bool _written;
  39. #endif
  40. #endif
  41. #if ENABLED(SERIAL_XON_XOFF)
  42. uint8_t xon_xoff_state = XON_XOFF_CHAR_SENT | XON_CHAR;
  43. #endif
  44. #if ENABLED(SERIAL_STATS_DROPPED_RX)
  45. uint8_t rx_dropped_bytes = 0;
  46. #endif
  47. #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
  48. ring_buffer_pos_t rx_max_enqueued = 0;
  49. #endif
  50. #if ENABLED(EMERGENCY_PARSER)
  51. #include "stepper.h"
  52. #include "language.h"
  53. // Currently looking for: M108, M112, M410
  54. // If you alter the parser please don't forget to update the capabilities in Conditionals_post.h
  55. FORCE_INLINE void emergency_parser(const unsigned char c) {
  56. static e_parser_state state = state_RESET;
  57. switch (state) {
  58. case state_RESET:
  59. switch (c) {
  60. case ' ': break;
  61. case 'N': state = state_N; break;
  62. case 'M': state = state_M; break;
  63. default: state = state_IGNORE;
  64. }
  65. break;
  66. case state_N:
  67. switch (c) {
  68. case '0': case '1': case '2':
  69. case '3': case '4': case '5':
  70. case '6': case '7': case '8':
  71. case '9': case '-': case ' ': break;
  72. case 'M': state = state_M; break;
  73. default: state = state_IGNORE;
  74. }
  75. break;
  76. case state_M:
  77. switch (c) {
  78. case ' ': break;
  79. case '1': state = state_M1; break;
  80. case '4': state = state_M4; break;
  81. default: state = state_IGNORE;
  82. }
  83. break;
  84. case state_M1:
  85. switch (c) {
  86. case '0': state = state_M10; break;
  87. case '1': state = state_M11; break;
  88. default: state = state_IGNORE;
  89. }
  90. break;
  91. case state_M10:
  92. state = (c == '8') ? state_M108 : state_IGNORE;
  93. break;
  94. case state_M11:
  95. state = (c == '2') ? state_M112 : state_IGNORE;
  96. break;
  97. case state_M4:
  98. state = (c == '1') ? state_M41 : state_IGNORE;
  99. break;
  100. case state_M41:
  101. state = (c == '0') ? state_M410 : state_IGNORE;
  102. break;
  103. case state_IGNORE:
  104. if (c == '\n') state = state_RESET;
  105. break;
  106. default:
  107. if (c == '\n') {
  108. switch (state) {
  109. case state_M108:
  110. wait_for_user = wait_for_heatup = false;
  111. break;
  112. case state_M112:
  113. kill(PSTR(MSG_KILLED));
  114. break;
  115. case state_M410:
  116. quickstop_stepper();
  117. break;
  118. default:
  119. break;
  120. }
  121. state = state_RESET;
  122. }
  123. }
  124. }
  125. #endif // EMERGENCY_PARSER
  126. FORCE_INLINE void store_rxd_char() {
  127. const ring_buffer_pos_t h = rx_buffer.head,
  128. i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
  129. // if we should be storing the received character into the location
  130. // just before the tail (meaning that the head would advance to the
  131. // current location of the tail), we're about to overflow the buffer
  132. // and so we don't write the character or advance the head.
  133. if (i != rx_buffer.tail) {
  134. rx_buffer.buffer[h] = M_UDRx;
  135. rx_buffer.head = i;
  136. }
  137. else {
  138. (void)M_UDRx;
  139. #if ENABLED(SERIAL_STATS_DROPPED_RX)
  140. if (!++rx_dropped_bytes)
  141. ++rx_dropped_bytes;
  142. #endif
  143. }
  144. #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
  145. {
  146. // calculate count of bytes stored into the RX buffer
  147. ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(rx_buffer.head - rx_buffer.tail) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
  148. // Keep track of the maximum count of enqueued bytes
  149. if (rx_max_enqueued < rx_count)
  150. rx_max_enqueued = rx_count;
  151. }
  152. #endif
  153. #if ENABLED(SERIAL_XON_XOFF)
  154. // for high speed transfers, we can use XON/XOFF protocol to do
  155. // software handshake and avoid overruns.
  156. if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XON_CHAR) {
  157. // calculate count of bytes stored into the RX buffer
  158. ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(rx_buffer.head - rx_buffer.tail) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
  159. // if we are above 12.5% of RX buffer capacity, send XOFF before
  160. // we run out of RX buffer space .. We need 325 bytes @ 250kbits/s to
  161. // let the host react and stop sending bytes. This translates to 13mS
  162. // propagation time.
  163. if (rx_count >= (RX_BUFFER_SIZE/8)) {
  164. // If TX interrupts are disabled and data register is empty,
  165. // just write the byte to the data register and be done. This
  166. // shortcut helps significantly improve the effective datarate
  167. // at high (>500kbit/s) bitrates, where interrupt overhead
  168. // becomes a slowdown.
  169. if (!TEST(M_UCSRxB, M_UDRIEx) && TEST(M_UCSRxA, M_UDREx)) {
  170. // Send an XOFF character
  171. M_UDRx = XOFF_CHAR;
  172. // clear the TXC bit -- "can be cleared by writing a one to its bit
  173. // location". This makes sure flush() won't return until the bytes
  174. // actually got written
  175. SBI(M_UCSRxA, M_TXCx);
  176. // And remember we already sent it
  177. xon_xoff_state = XOFF_CHAR | XON_XOFF_CHAR_SENT;
  178. } else {
  179. // TX interrupts disabled, but buffer still not empty ... or
  180. // TX interrupts enabled. Reenable TX ints and schedule XOFF
  181. // character to be sent
  182. #if TX_BUFFER_SIZE > 0
  183. SBI(M_UCSRxB, M_UDRIEx);
  184. xon_xoff_state = XOFF_CHAR;
  185. #else
  186. // We are not using TX interrupts, we will have to send this manually
  187. while (!TEST(M_UCSRxA, M_UDREx))
  188. ;
  189. M_UDRx = XOFF_CHAR;
  190. // And remember we already sent it
  191. xon_xoff_state = XOFF_CHAR | XON_XOFF_CHAR_SENT;
  192. #endif
  193. }
  194. }
  195. }
  196. #endif
  197. #if ENABLED(EMERGENCY_PARSER)
  198. emergency_parser(c);
  199. #endif
  200. }
  201. #if TX_BUFFER_SIZE > 0
  202. FORCE_INLINE void _tx_udr_empty_irq(void) {
  203. // If interrupts are enabled, there must be more data in the output
  204. // buffer.
  205. #if ENABLED(SERIAL_XON_XOFF)
  206. // If we must do a priority insertion of an XON/XOFF char,
  207. // do it now
  208. uint8_t state = xon_xoff_state;
  209. if (!(state & XON_XOFF_CHAR_SENT)) {
  210. M_UDRx = state & XON_XOFF_CHAR_MASK;
  211. xon_xoff_state = state | XON_XOFF_CHAR_SENT;
  212. } else {
  213. #endif
  214. // Send the next byte
  215. const uint8_t t = tx_buffer.tail,
  216. c = tx_buffer.buffer[t];
  217. tx_buffer.tail = (t + 1) & (TX_BUFFER_SIZE - 1);
  218. M_UDRx = c;
  219. #if ENABLED(SERIAL_XON_XOFF)
  220. }
  221. #endif
  222. // clear the TXC bit -- "can be cleared by writing a one to its bit
  223. // location". This makes sure flush() won't return until the bytes
  224. // actually got written
  225. SBI(M_UCSRxA, M_TXCx);
  226. if (tx_buffer.head == tx_buffer.tail) {
  227. // Buffer empty, so disable interrupts
  228. CBI(M_UCSRxB, M_UDRIEx);
  229. }
  230. }
  231. #ifdef M_USARTx_UDRE_vect
  232. ISR(M_USARTx_UDRE_vect) {
  233. _tx_udr_empty_irq();
  234. }
  235. #endif
  236. #endif // TX_BUFFER_SIZE
  237. #ifdef M_USARTx_RX_vect
  238. ISR(M_USARTx_RX_vect) {
  239. store_rxd_char();
  240. }
  241. #endif
  242. // Public Methods
  243. void MarlinSerial::begin(const long baud) {
  244. uint16_t baud_setting;
  245. bool useU2X = true;
  246. #if F_CPU == 16000000UL && SERIAL_PORT == 0
  247. // hard-coded exception for compatibility with the bootloader shipped
  248. // with the Duemilanove and previous boards and the firmware on the 8U2
  249. // on the Uno and Mega 2560.
  250. if (baud == 57600) useU2X = false;
  251. #endif
  252. if (useU2X) {
  253. M_UCSRxA = _BV(M_U2Xx);
  254. baud_setting = (F_CPU / 4 / baud - 1) / 2;
  255. }
  256. else {
  257. M_UCSRxA = 0;
  258. baud_setting = (F_CPU / 8 / baud - 1) / 2;
  259. }
  260. // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
  261. M_UBRRxH = baud_setting >> 8;
  262. M_UBRRxL = baud_setting;
  263. SBI(M_UCSRxB, M_RXENx);
  264. SBI(M_UCSRxB, M_TXENx);
  265. SBI(M_UCSRxB, M_RXCIEx);
  266. #if TX_BUFFER_SIZE > 0
  267. CBI(M_UCSRxB, M_UDRIEx);
  268. _written = false;
  269. #endif
  270. }
  271. void MarlinSerial::end() {
  272. CBI(M_UCSRxB, M_RXENx);
  273. CBI(M_UCSRxB, M_TXENx);
  274. CBI(M_UCSRxB, M_RXCIEx);
  275. CBI(M_UCSRxB, M_UDRIEx);
  276. }
  277. void MarlinSerial::checkRx(void) {
  278. if (TEST(M_UCSRxA, M_RXCx)) {
  279. CRITICAL_SECTION_START;
  280. store_rxd_char();
  281. CRITICAL_SECTION_END;
  282. }
  283. }
  284. int MarlinSerial::peek(void) {
  285. CRITICAL_SECTION_START;
  286. const int v = rx_buffer.head == rx_buffer.tail ? -1 : rx_buffer.buffer[rx_buffer.tail];
  287. CRITICAL_SECTION_END;
  288. return v;
  289. }
  290. int MarlinSerial::read(void) {
  291. int v;
  292. CRITICAL_SECTION_START;
  293. const ring_buffer_pos_t t = rx_buffer.tail;
  294. if (rx_buffer.head == t)
  295. v = -1;
  296. else {
  297. v = rx_buffer.buffer[t];
  298. rx_buffer.tail = (ring_buffer_pos_t)(t + 1) & (RX_BUFFER_SIZE - 1);
  299. #if ENABLED(SERIAL_XON_XOFF)
  300. // for high speed transfers, we can use XON/XOFF protocol to do
  301. // software handshake and avoid overruns.
  302. if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XOFF_CHAR) {
  303. // calculate count of bytes stored into the RX buffer
  304. ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(rx_buffer.head - rx_buffer.tail) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
  305. // if we are below 10% of RX buffer capacity, send XON before
  306. // we run out of RX buffer bytes
  307. if (rx_count < (RX_BUFFER_SIZE/10)) {
  308. // Send an XON character
  309. xon_xoff_state = XON_CHAR | XON_XOFF_CHAR_SENT;
  310. // End critical section
  311. CRITICAL_SECTION_END;
  312. // Transmit the XON character
  313. writeNoHandshake(XON_CHAR);
  314. // Done
  315. return v;
  316. }
  317. }
  318. #endif
  319. }
  320. CRITICAL_SECTION_END;
  321. return v;
  322. }
  323. ring_buffer_pos_t MarlinSerial::available(void) {
  324. CRITICAL_SECTION_START;
  325. const ring_buffer_pos_t h = rx_buffer.head,
  326. t = rx_buffer.tail;
  327. CRITICAL_SECTION_END;
  328. return (ring_buffer_pos_t)(RX_BUFFER_SIZE + h - t) & (RX_BUFFER_SIZE - 1);
  329. }
  330. void MarlinSerial::flush(void) {
  331. // RX
  332. // don't reverse this or there may be problems if the RX interrupt
  333. // occurs after reading the value of rx_buffer_head but before writing
  334. // the value to rx_buffer_tail; the previous value of rx_buffer_head
  335. // may be written to rx_buffer_tail, making it appear as if the buffer
  336. // were full, not empty.
  337. CRITICAL_SECTION_START;
  338. rx_buffer.head = rx_buffer.tail;
  339. CRITICAL_SECTION_END;
  340. #if ENABLED(SERIAL_XON_XOFF)
  341. // for high speed transfers, we can use XON/XOFF protocol to do
  342. // software handshake and avoid overruns.
  343. if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XOFF_CHAR) {
  344. // Send an XON character
  345. xon_xoff_state = XON_CHAR | XON_XOFF_CHAR_SENT;
  346. // Transmit the XON character
  347. writeNoHandshake(XON_CHAR);
  348. }
  349. #endif
  350. }
  351. #if TX_BUFFER_SIZE > 0
  352. uint8_t MarlinSerial::availableForWrite(void) {
  353. CRITICAL_SECTION_START;
  354. const uint8_t h = tx_buffer.head,
  355. t = tx_buffer.tail;
  356. CRITICAL_SECTION_END;
  357. return (uint8_t)(TX_BUFFER_SIZE + h - t) & (TX_BUFFER_SIZE - 1);
  358. }
  359. void MarlinSerial::write(const uint8_t c) {
  360. #if ENABLED(SERIAL_XON_XOFF)
  361. uint8_t state = xon_xoff_state;
  362. if (!(state & XON_XOFF_CHAR_SENT)) {
  363. // 2 characters to send: The XON/XOFF character and the user
  364. // specified char.
  365. writeNoHandshake(state & XON_XOFF_CHAR_MASK);
  366. xon_xoff_state = state | XON_XOFF_CHAR_SENT;
  367. }
  368. #endif
  369. writeNoHandshake(c);
  370. }
  371. void MarlinSerial::writeNoHandshake(uint8_t c) {
  372. _written = true;
  373. CRITICAL_SECTION_START;
  374. bool emty = (tx_buffer.head == tx_buffer.tail);
  375. CRITICAL_SECTION_END;
  376. // If the buffer and the data register is empty, just write the byte
  377. // to the data register and be done. This shortcut helps
  378. // significantly improve the effective datarate at high (>
  379. // 500kbit/s) bitrates, where interrupt overhead becomes a slowdown.
  380. if (emty && TEST(M_UCSRxA, M_UDREx)) {
  381. CRITICAL_SECTION_START;
  382. M_UDRx = c;
  383. SBI(M_UCSRxA, M_TXCx);
  384. CRITICAL_SECTION_END;
  385. return;
  386. }
  387. const uint8_t i = (tx_buffer.head + 1) & (TX_BUFFER_SIZE - 1);
  388. // If the output buffer is full, there's nothing for it other than to
  389. // wait for the interrupt handler to empty it a bit
  390. while (i == tx_buffer.tail) {
  391. if (!TEST(SREG, SREG_I)) {
  392. // Interrupts are disabled, so we'll have to poll the data
  393. // register empty flag ourselves. If it is set, pretend an
  394. // interrupt has happened and call the handler to free up
  395. // space for us.
  396. if (TEST(M_UCSRxA, M_UDREx))
  397. _tx_udr_empty_irq();
  398. }
  399. else {
  400. // nop, the interrupt handler will free up space for us
  401. }
  402. }
  403. tx_buffer.buffer[tx_buffer.head] = c;
  404. { CRITICAL_SECTION_START;
  405. tx_buffer.head = i;
  406. SBI(M_UCSRxB, M_UDRIEx);
  407. CRITICAL_SECTION_END;
  408. }
  409. return;
  410. }
  411. void MarlinSerial::flushTX(void) {
  412. // TX
  413. // If we have never written a byte, no need to flush. This special
  414. // case is needed since there is no way to force the TXC (transmit
  415. // complete) bit to 1 during initialization
  416. if (!_written)
  417. return;
  418. while (TEST(M_UCSRxB, M_UDRIEx) || !TEST(M_UCSRxA, M_TXCx)) {
  419. if (!TEST(SREG, SREG_I) && TEST(M_UCSRxB, M_UDRIEx))
  420. // Interrupts are globally disabled, but the DR empty
  421. // interrupt should be enabled, so poll the DR empty flag to
  422. // prevent deadlock
  423. if (TEST(M_UCSRxA, M_UDREx))
  424. _tx_udr_empty_irq();
  425. }
  426. // If we get here, nothing is queued anymore (DRIE is disabled) and
  427. // the hardware finished tranmission (TXC is set).
  428. }
  429. #else
  430. void MarlinSerial::write(uint8_t c) {
  431. #if ENABLED(SERIAL_XON_XOFF)
  432. // If we must do a priority insertion of an XON/XOFF char, do it now
  433. uint8_t state = xon_xoff_state;
  434. if (!(state & XON_XOFF_CHAR_SENT)) {
  435. writeNoHandshake(state & XON_XOFF_CHAR_MASK);
  436. xon_xoff_state = state | XON_XOFF_CHAR_SENT;
  437. }
  438. #endif
  439. writeNoHandshake(c);
  440. }
  441. void MarlinSerial::writeNoHandshake(uint8_t c) {
  442. while (!TEST(M_UCSRxA, M_UDREx))
  443. ;
  444. M_UDRx = c;
  445. }
  446. #endif
  447. // end NEW
  448. /// imports from print.h
  449. void MarlinSerial::print(char c, int base) {
  450. print((long)c, base);
  451. }
  452. void MarlinSerial::print(unsigned char b, int base) {
  453. print((unsigned long)b, base);
  454. }
  455. void MarlinSerial::print(int n, int base) {
  456. print((long)n, base);
  457. }
  458. void MarlinSerial::print(unsigned int n, int base) {
  459. print((unsigned long)n, base);
  460. }
  461. void MarlinSerial::print(long n, int base) {
  462. if (base == 0)
  463. write(n);
  464. else if (base == 10) {
  465. if (n < 0) {
  466. print('-');
  467. n = -n;
  468. }
  469. printNumber(n, 10);
  470. }
  471. else
  472. printNumber(n, base);
  473. }
  474. void MarlinSerial::print(unsigned long n, int base) {
  475. if (base == 0) write(n);
  476. else printNumber(n, base);
  477. }
  478. void MarlinSerial::print(double n, int digits) {
  479. printFloat(n, digits);
  480. }
  481. void MarlinSerial::println(void) {
  482. print('\r');
  483. print('\n');
  484. }
  485. void MarlinSerial::println(const String& s) {
  486. print(s);
  487. println();
  488. }
  489. void MarlinSerial::println(const char c[]) {
  490. print(c);
  491. println();
  492. }
  493. void MarlinSerial::println(char c, int base) {
  494. print(c, base);
  495. println();
  496. }
  497. void MarlinSerial::println(unsigned char b, int base) {
  498. print(b, base);
  499. println();
  500. }
  501. void MarlinSerial::println(int n, int base) {
  502. print(n, base);
  503. println();
  504. }
  505. void MarlinSerial::println(unsigned int n, int base) {
  506. print(n, base);
  507. println();
  508. }
  509. void MarlinSerial::println(long n, int base) {
  510. print(n, base);
  511. println();
  512. }
  513. void MarlinSerial::println(unsigned long n, int base) {
  514. print(n, base);
  515. println();
  516. }
  517. void MarlinSerial::println(double n, int digits) {
  518. print(n, digits);
  519. println();
  520. }
  521. // Private Methods
  522. void MarlinSerial::printNumber(unsigned long n, uint8_t base) {
  523. if (n) {
  524. unsigned char buf[8 * sizeof(long)]; // Enough space for base 2
  525. int8_t i = 0;
  526. while (n) {
  527. buf[i++] = n % base;
  528. n /= base;
  529. }
  530. while (i--)
  531. print((char)(buf[i] + (buf[i] < 10 ? '0' : 'A' - 10)));
  532. }
  533. else
  534. print('0');
  535. }
  536. void MarlinSerial::printFloat(double number, uint8_t digits) {
  537. // Handle negative numbers
  538. if (number < 0.0) {
  539. print('-');
  540. number = -number;
  541. }
  542. // Round correctly so that print(1.999, 2) prints as "2.00"
  543. double rounding = 0.5;
  544. for (uint8_t i = 0; i < digits; ++i)
  545. rounding *= 0.1;
  546. number += rounding;
  547. // Extract the integer part of the number and print it
  548. unsigned long int_part = (unsigned long)number;
  549. double remainder = number - (double)int_part;
  550. print(int_part);
  551. // Print the decimal point, but only if there are digits beyond
  552. if (digits) {
  553. print('.');
  554. // Extract digits from the remainder one at a time
  555. while (digits--) {
  556. remainder *= 10.0;
  557. int toPrint = int(remainder);
  558. print(toPrint);
  559. remainder -= toPrint;
  560. }
  561. }
  562. }
  563. // Preinstantiate
  564. MarlinSerial customizedSerial;
  565. #endif // !USBCON && (UBRRH || UBRR0H || UBRR1H || UBRR2H || UBRR3H)
  566. // For AT90USB targets use the UART for BT interfacing
  567. #if defined(USBCON) && ENABLED(BLUETOOTH)
  568. HardwareSerial bluetoothSerial;
  569. #endif