MarlinSerial.cpp

Sat, 07 Nov 2015 13:23:07 +0100

author
mbayer
date
Sat, 07 Nov 2015 13:23:07 +0100
changeset 0
2c8ba1964db7
permissions
-rw-r--r--

Initial code from reprappro Marlin repository

0
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1 /*
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
2 HardwareSerial.cpp - Hardware serial library for Wiring
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
3 Copyright (c) 2006 Nicholas Zambetti. All right reserved.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
4
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
6 modify it under the terms of the GNU Lesser General Public
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
7 License as published by the Free Software Foundation; either
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
9
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
13 Lesser General Public License for more details.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
14
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
15 You should have received a copy of the GNU Lesser General Public
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
16 License along with this library; if not, write to the Free Software
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
18
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
19 Modified 23 November 2006 by David A. Mellis
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
20 Modified 28 September 2010 by Mark Sproul
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
21 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
22
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
23
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
24
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
25 #include "Marlin.h"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
26
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
27 #ifndef REPRAPPRO_MULTIMATERIALS
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
28
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
29 #include "MarlinSerial.h"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
30
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
31 #if MOTHERBOARD != 8 // !teensylu
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
32 // this next line disables the entire HardwareSerial.cpp,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
33 // this is so I can support Attiny series and any other chip without a uart
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
34 #if defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
35
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
36 #if defined(UBRRH) || defined(UBRR0H)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
37 ring_buffer rx_buffer = { { 0 }, 0, 0 };
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
38 #endif
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
39
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
40 FORCE_INLINE void store_char(unsigned char c)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
41 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
42 int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
43
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
44 // if we should be storing the received character into the location
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
45 // just before the tail (meaning that the head would advance to the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
46 // current location of the tail), we're about to overflow the buffer
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
47 // and so we don't write the character or advance the head.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
48 if (i != rx_buffer.tail) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
49 rx_buffer.buffer[rx_buffer.head] = c;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
50 rx_buffer.head = i;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
51 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
52 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
53
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
54
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
55 //#elif defined(SIG_USART_RECV)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
56 #if defined(USART0_RX_vect)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
57 // fixed by Mark Sproul this is on the 644/644p
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
58 //SIGNAL(SIG_USART_RECV)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
59 SIGNAL(USART0_RX_vect)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
60 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
61 #if defined(UDR0)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
62 unsigned char c = UDR0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
63 #elif defined(UDR)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
64 unsigned char c = UDR; // atmega8, atmega32
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
65 #else
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
66 #error UDR not defined
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
67 #endif
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
68 store_char(c);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
69 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
70 #endif
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
71
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
72 // Constructors ////////////////////////////////////////////////////////////////
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
73
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
74 MarlinSerial::MarlinSerial()
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
75 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
76
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
77 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
78
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
79 // Public Methods //////////////////////////////////////////////////////////////
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
80
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
81 void MarlinSerial::begin(long baud)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
82 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
83 uint16_t baud_setting;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
84 bool useU2X0 = true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
85
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
86 #if F_CPU == 16000000UL
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
87 // hardcoded exception for compatibility with the bootloader shipped
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
88 // with the Duemilanove and previous boards and the firmware on the 8U2
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
89 // on the Uno and Mega 2560.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
90 if (baud == 57600) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
91 useU2X0 = false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
92 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
93 #endif
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
94
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
95 if (useU2X0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
96 UCSR0A = 1 << U2X0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
97 baud_setting = (F_CPU / 4 / baud - 1) / 2;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
98 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
99 UCSR0A = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
100 baud_setting = (F_CPU / 8 / baud - 1) / 2;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
101 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
102
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
103 // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
104 UBRR0H = baud_setting >> 8;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
105 UBRR0L = baud_setting;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
106
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
107 sbi(UCSR0B, RXEN0);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
108 sbi(UCSR0B, TXEN0);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
109 sbi(UCSR0B, RXCIE0);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
110 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
111
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
112 void MarlinSerial::end()
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
113 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
114 cbi(UCSR0B, RXEN0);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
115 cbi(UCSR0B, TXEN0);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
116 cbi(UCSR0B, RXCIE0);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
117 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
118
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
119
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
120
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
121 int MarlinSerial::peek(void)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
122 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
123 if (rx_buffer.head == rx_buffer.tail) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
124 return -1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
125 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
126 return rx_buffer.buffer[rx_buffer.tail];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
127 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
128 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
129
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
130 int MarlinSerial::read(void)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
131 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
132 // if the head isn't ahead of the tail, we don't have any characters
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
133 if (rx_buffer.head == rx_buffer.tail) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
134 return -1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
135 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
136 unsigned char c = rx_buffer.buffer[rx_buffer.tail];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
137 rx_buffer.tail = (unsigned int)(rx_buffer.tail + 1) % RX_BUFFER_SIZE;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
138 return c;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
139 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
140 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
141
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
142 void MarlinSerial::flush()
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
143 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
144 // don't reverse this or there may be problems if the RX interrupt
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
145 // occurs after reading the value of rx_buffer_head but before writing
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
146 // the value to rx_buffer_tail; the previous value of rx_buffer_head
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
147 // may be written to rx_buffer_tail, making it appear as if the buffer
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
148 // don't reverse this or there may be problems if the RX interrupt
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
149 // occurs after reading the value of rx_buffer_head but before writing
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
150 // the value to rx_buffer_tail; the previous value of rx_buffer_head
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
151 // may be written to rx_buffer_tail, making it appear as if the buffer
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
152 // were full, not empty.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
153 rx_buffer.head = rx_buffer.tail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
154 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
155
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
156
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
157
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
158
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
159 /// imports from print.h
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
160
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
161
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
162
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
163
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
164 void MarlinSerial::print(char c, int base)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
165 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
166 print((long) c, base);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
167 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
168
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
169 void MarlinSerial::print(unsigned char b, int base)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
170 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
171 print((unsigned long) b, base);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
172 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
173
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
174 void MarlinSerial::print(int n, int base)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
175 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
176 print((long) n, base);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
177 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
178
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
179 void MarlinSerial::print(unsigned int n, int base)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
180 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
181 print((unsigned long) n, base);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
182 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
183
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
184 void MarlinSerial::print(long n, int base)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
185 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
186 if (base == 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
187 write(n);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
188 } else if (base == 10) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
189 if (n < 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
190 print('-');
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
191 n = -n;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
192 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
193 printNumber(n, 10);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
194 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
195 printNumber(n, base);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
196 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
197 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
198
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
199 void MarlinSerial::print(unsigned long n, int base)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
200 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
201 if (base == 0) write(n);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
202 else printNumber(n, base);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
203 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
204
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
205 void MarlinSerial::print(double n, int digits)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
206 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
207 printFloat(n, digits);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
208 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
209
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
210 void MarlinSerial::println(void)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
211 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
212 print('\r');
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
213 print('\n');
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
214 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
215
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
216 void MarlinSerial::println(const String &s)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
217 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
218 print(s);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
219 println();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
220 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
221
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
222 void MarlinSerial::println(const char c[])
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
223 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
224 print(c);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
225 println();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
226 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
227
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
228 void MarlinSerial::println(char c, int base)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
229 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
230 print(c, base);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
231 println();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
232 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
233
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
234 void MarlinSerial::println(unsigned char b, int base)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
235 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
236 print(b, base);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
237 println();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
238 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
239
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
240 void MarlinSerial::println(int n, int base)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
241 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
242 print(n, base);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
243 println();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
244 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
245
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
246 void MarlinSerial::println(unsigned int n, int base)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
247 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
248 print(n, base);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
249 println();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
250 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
251
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
252 void MarlinSerial::println(long n, int base)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
253 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
254 print(n, base);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
255 println();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
256 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
257
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
258 void MarlinSerial::println(unsigned long n, int base)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
259 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
260 print(n, base);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
261 println();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
262 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
263
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
264 void MarlinSerial::println(double n, int digits)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
265 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
266 print(n, digits);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
267 println();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
268 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
269
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
270 // Private Methods /////////////////////////////////////////////////////////////
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
271
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
272 void MarlinSerial::printNumber(unsigned long n, uint8_t base)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
273 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
274 unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
275 unsigned long i = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
276
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
277 if (n == 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
278 print('0');
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
279 return;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
280 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
281
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
282 while (n > 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
283 buf[i++] = n % base;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
284 n /= base;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
285 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
286
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
287 for (; i > 0; i--)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
288 print((char) (buf[i - 1] < 10 ?
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
289 '0' + buf[i - 1] :
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
290 'A' + buf[i - 1] - 10));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
291 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
292
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
293 void MarlinSerial::printFloat(double number, uint8_t digits)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
294 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
295 // Handle negative numbers
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
296 if (number < 0.0)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
297 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
298 print('-');
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
299 number = -number;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
300 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
301
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
302 // Round correctly so that print(1.999, 2) prints as "2.00"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
303 double rounding = 0.5;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
304 for (uint8_t i=0; i<digits; ++i)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
305 rounding /= 10.0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
306
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
307 number += rounding;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
308
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
309 // Extract the integer part of the number and print it
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
310 unsigned long int_part = (unsigned long)number;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
311 double remainder = number - (double)int_part;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
312 print(int_part);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
313
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
314 // Print the decimal point, but only if there are digits beyond
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
315 if (digits > 0)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
316 print(".");
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
317
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
318 // Extract digits from the remainder one at a time
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
319 while (digits-- > 0)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
320 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
321 remainder *= 10.0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
322 int toPrint = int(remainder);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
323 print(toPrint);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
324 remainder -= toPrint;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
325 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
326 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
327 // Preinstantiate Objects //////////////////////////////////////////////////////
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
328
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
329
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
330 MarlinSerial MSerial;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
331
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
332 MarlinSerial MSerial1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
333
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
334 #endif // whole file
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
335 #endif //teensylu
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
336
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
337 #endif REPRAPPRO_MULTIMATERIALS
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
338

mercurial