Luigi Santivetti | 69972f9 | 2019-11-12 22:55:40 +0000 | [diff] [blame^] | 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 | |
| 31 | |
| 32 | // Print a string stored in PGM-memory |
| 33 | void printPgmString(const char *s) |
| 34 | { |
| 35 | char c; |
| 36 | while ((c = pgm_read_byte_near(s++))) |
| 37 | serial_write(c); |
| 38 | } |
| 39 | |
| 40 | |
| 41 | // void printIntegerInBase(unsigned long n, unsigned long base) |
| 42 | // { |
| 43 | // unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars. |
| 44 | // unsigned long i = 0; |
| 45 | // |
| 46 | // if (n == 0) { |
| 47 | // serial_write('0'); |
| 48 | // return; |
| 49 | // } |
| 50 | // |
| 51 | // while (n > 0) { |
| 52 | // buf[i++] = n % base; |
| 53 | // n /= base; |
| 54 | // } |
| 55 | // |
| 56 | // for (; i > 0; i--) |
| 57 | // serial_write(buf[i - 1] < 10 ? |
| 58 | // '0' + buf[i - 1] : |
| 59 | // 'A' + buf[i - 1] - 10); |
| 60 | // } |
| 61 | |
| 62 | |
| 63 | // Prints an uint8 variable in base 10. |
| 64 | void print_uint8_base10(uint8_t n) |
| 65 | { |
| 66 | uint8_t digit_a = 0; |
| 67 | uint8_t digit_b = 0; |
| 68 | if (n >= 100) { // 100-255 |
| 69 | digit_a = '0' + n % 10; |
| 70 | n /= 10; |
| 71 | } |
| 72 | if (n >= 10) { // 10-99 |
| 73 | digit_b = '0' + n % 10; |
| 74 | n /= 10; |
| 75 | } |
| 76 | serial_write('0' + n); |
| 77 | if (digit_b) { serial_write(digit_b); } |
| 78 | if (digit_a) { serial_write(digit_a); } |
| 79 | } |
| 80 | |
| 81 | |
| 82 | // Prints an uint8 variable in base 2 with desired number of desired digits. |
| 83 | void print_uint8_base2_ndigit(uint8_t n, uint8_t digits) { |
| 84 | unsigned char buf[digits]; |
| 85 | uint8_t i = 0; |
| 86 | |
| 87 | for (; i < digits; i++) { |
| 88 | buf[i] = n % 2 ; |
| 89 | n /= 2; |
| 90 | } |
| 91 | |
| 92 | for (; i > 0; i--) |
| 93 | serial_write('0' + buf[i - 1]); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | void print_uint32_base10(uint32_t n) |
| 98 | { |
| 99 | if (n == 0) { |
| 100 | serial_write('0'); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | unsigned char buf[10]; |
| 105 | uint8_t i = 0; |
| 106 | |
| 107 | while (n > 0) { |
| 108 | buf[i++] = n % 10; |
| 109 | n /= 10; |
| 110 | } |
| 111 | |
| 112 | for (; i > 0; i--) |
| 113 | serial_write('0' + buf[i-1]); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | void printInteger(long n) |
| 118 | { |
| 119 | if (n < 0) { |
| 120 | serial_write('-'); |
| 121 | print_uint32_base10(-n); |
| 122 | } else { |
| 123 | print_uint32_base10(n); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | |
| 128 | // Convert float to string by immediately converting to a long integer, which contains |
| 129 | // more digits than a float. Number of decimal places, which are tracked by a counter, |
| 130 | // may be set by the user. The integer is then efficiently converted to a string. |
| 131 | // NOTE: AVR '%' and '/' integer operations are very efficient. Bitshifting speed-up |
| 132 | // techniques are actually just slightly slower. Found this out the hard way. |
| 133 | void printFloat(float n, uint8_t decimal_places) |
| 134 | { |
| 135 | if (n < 0) { |
| 136 | serial_write('-'); |
| 137 | n = -n; |
| 138 | } |
| 139 | |
| 140 | uint8_t decimals = decimal_places; |
| 141 | while (decimals >= 2) { // Quickly convert values expected to be E0 to E-4. |
| 142 | n *= 100; |
| 143 | decimals -= 2; |
| 144 | } |
| 145 | if (decimals) { n *= 10; } |
| 146 | n += 0.5; // Add rounding factor. Ensures carryover through entire value. |
| 147 | |
| 148 | // Generate digits backwards and store in string. |
| 149 | unsigned char buf[13]; |
| 150 | uint8_t i = 0; |
| 151 | uint32_t a = (long)n; |
| 152 | while(a > 0) { |
| 153 | buf[i++] = (a % 10) + '0'; // Get digit |
| 154 | a /= 10; |
| 155 | } |
| 156 | while (i < decimal_places) { |
| 157 | buf[i++] = '0'; // Fill in zeros to decimal point for (n < 1) |
| 158 | } |
| 159 | if (i == decimal_places) { // Fill in leading zero, if needed. |
| 160 | buf[i++] = '0'; |
| 161 | } |
| 162 | |
| 163 | // Print the generated string. |
| 164 | for (; i > 0; i--) { |
| 165 | if (i == decimal_places) { serial_write('.'); } // Insert decimal point in right place. |
| 166 | serial_write(buf[i-1]); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | |
| 171 | // Floating value printing handlers for special variables types used in Grbl and are defined |
| 172 | // in the config.h. |
| 173 | // - CoordValue: Handles all position or coordinate values in inches or mm reporting. |
| 174 | // - RateValue: Handles feed rate and current velocity in inches or mm reporting. |
| 175 | void printFloat_CoordValue(float n) { |
| 176 | if (bit_istrue(settings.flags,BITFLAG_REPORT_INCHES)) { |
| 177 | printFloat(n*INCH_PER_MM,N_DECIMAL_COORDVALUE_INCH); |
| 178 | } else { |
| 179 | printFloat(n,N_DECIMAL_COORDVALUE_MM); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | void printFloat_RateValue(float n) { |
| 184 | if (bit_istrue(settings.flags,BITFLAG_REPORT_INCHES)) { |
| 185 | printFloat(n*INCH_PER_MM,N_DECIMAL_RATEVALUE_INCH); |
| 186 | } else { |
| 187 | printFloat(n,N_DECIMAL_RATEVALUE_MM); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // Debug tool to print free memory in bytes at the called point. |
| 192 | // NOTE: Keep commented unless using. Part of this function always gets compiled in. |
| 193 | // void printFreeMemory() |
| 194 | // { |
| 195 | // extern int __heap_start, *__brkval; |
| 196 | // uint16_t free; // Up to 64k values. |
| 197 | // free = (int) &free - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); |
| 198 | // printInteger((int32_t)free); |
| 199 | // printString(" "); |
| 200 | // } |