My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

MarlinSerial.cpp 19KB

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