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

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