gnea\grbl-Mega  1.0f
Source Code Documentation ( Internal Workings )
print.c
Go to the documentation of this file.
1 /*
2  print.c - Functions for formatting output strings
3  Part of Grbl
4 
5  Copyright (c) 2011-2016 Sungeun K. Jeon for Gnea Research LLC
6  Copyright (c) 2009-2011 Simen Svale Skogsrud
7 
8  Grbl 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  Grbl 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 Grbl. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "grbl.h"
23 
24 
25 void printString(const char *s)
26 {
27  while (*s)
28  serial_write(*s++);
29 }
30 
32 void printPgmString(const char *s)
33 {
34  char c;
35  while ((c = pgm_read_byte_near(s++)))
36  serial_write(c);
37 }
38 
40 // {
41 // unsigned char buf[8 * sizeof(long)]; //!< Assumes 8-bit chars.
42 // unsigned long i = 0;
43 //
44 // if (n == 0) {
45 // serial_write('0');
46 // return;
47 // }
48 //
49 // while (n > 0) {
50 // buf[i++] = n % base;
51 // n /= base;
52 // }
53 //
54 // for (; i > 0; i--)
55 // serial_write(buf[i - 1] < 10 ?
56 // '0' + buf[i - 1] :
57 // 'A' + buf[i - 1] - 10);
58 // }
59 
61 void print_uint8_base10(uint8_t n)
62 {
63  uint8_t digit_a = 0;
64  uint8_t digit_b = 0;
65  if (n >= 100) {
66  digit_a = '0' + n % 10;
67  n /= 10;
68  }
69  if (n >= 10) {
70  digit_b = '0' + n % 10;
71  n /= 10;
72  }
73  serial_write('0' + n);
74  if (digit_b) { serial_write(digit_b); }
75  if (digit_a) { serial_write(digit_a); }
76 }
77 
79 void print_uint8_base2_ndigit(uint8_t n, uint8_t digits) {
80  unsigned char buf[digits];
81  uint8_t i = 0;
82 
83  for (; i < digits; i++) {
84  buf[i] = n % 2 ;
85  n /= 2;
86  }
87 
88  for (; i > 0; i--)
89  serial_write('0' + buf[i - 1]);
90 }
91 
92 
93 void print_uint32_base10(uint32_t n)
94 {
95  if (n == 0) {
96  serial_write('0');
97  return;
98  }
99 
100  unsigned char buf[10];
101  uint8_t i = 0;
102 
103  while (n > 0) {
104  buf[i++] = n % 10;
105  n /= 10;
106  }
107 
108  for (; i > 0; i--)
109  serial_write('0' + buf[i-1]);
110 }
111 
112 
113 void printInteger(long n)
114 {
115  if (n < 0) {
116  serial_write('-');
118  } else {
120  }
121 }
122 
124 // more digits than a float. Number of decimal places, which are tracked by a counter,
125 // may be set by the user. The integer is then efficiently converted to a string.
126 //
128 // techniques are actually just slightly slower. Found this out the hard way.
129 void printFloat(float n, uint8_t decimal_places)
130 {
131  if (n < 0) {
132  serial_write('-');
133  n = -n;
134  }
135 
136  uint8_t decimals = decimal_places;
137  while (decimals >= 2) {
138  n *= 100;
139  decimals -= 2;
140  }
141  if (decimals) { n *= 10; }
142  n += 0.5;
143 
144 // Generate digits backwards and store in string.
145  unsigned char buf[13];
146  uint8_t i = 0;
147  uint32_t a = (long)n;
148  while(a > 0) {
149  buf[i++] = (a % 10) + '0';
150  a /= 10;
151  }
152  while (i < decimal_places) {
153  buf[i++] = '0';
154  }
155  if (i == decimal_places) {
156  buf[i++] = '0';
157  }
158 
159 // Print the generated string.
160  for (; i > 0; i--) {
161  if (i == decimal_places) { serial_write('.'); }
162  serial_write(buf[i-1]);
163  }
164 }
165 
167 // in the config.h.
168 // - CoordValue: Handles all position or coordinate values in inches or mm reporting.
169 // - RateValue: Handles feed rate and current velocity in inches or mm reporting.
170 void printFloat_CoordValue(float n) {
173  } else {
175  }
176 }
177 
178 void printFloat_RateValue(float n) {
181  } else {
183  }
184 }
186 //
188 // void printFreeMemory()
189 // {
190 // extern int __heap_start, *__brkval;
191 // uint16_t free; //!< Up to 64k values.
192 // free = (int) &free - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
193 // printInteger((int32_t)free);
194 // printString(" ");
195 // }
#define INCH_PER_MM
Definition: nuts_bolts.h:44
#define N_DECIMAL_RATEVALUE_MM
Rate or velocity value in mm/min.
Definition: config.h:137
#define N_DECIMAL_COORDVALUE_MM
Coordinate or position value in mm.
Definition: config.h:135
void serial_write(uint8_t data)
Writes one byte to the TX serial buffer. Called by main program.
Definition: serial.c:84
uint8_t flags
Contains default boolean settings.
Definition: settings.h:92
#define BITFLAG_REPORT_INCHES
NOTE: Check settings_reset() when moving to next version.
Definition: settings.h:35
#define N_DECIMAL_COORDVALUE_INCH
Number of floating decimal points printed by Grbl for certain value types. These settings are...
Definition: config.h:134
settings_t settings
Definition: settings.c:24
#define N_DECIMAL_RATEVALUE_INCH
Rate or velocity value in in/min.
Definition: config.h:136
#define bit_istrue(x, mask)
Definition: nuts_bolts.h:60