diff --git a/print.c b/print.c index f9da5ab..7aa710c 100755 --- a/print.c +++ b/print.c @@ -27,6 +27,7 @@ #include #include "config.h" #include "serial.h" +#include "settings.h" void printString(const char *s) { @@ -88,12 +89,12 @@ static void print_uint32_base10(unsigned long n) } while (n > 0) { - buf[i++] = n % 10; + buf[i++] = n % 10 + '0'; n /= 10; } - + for (; i > 0; i--) - serial_write('0' + buf[i - 1]); + serial_write(buf[i-1]); } void printInteger(long n) @@ -105,27 +106,43 @@ void printInteger(long n) print_uint32_base10(n); } -void printFloat(double n) +// Convert float to string by immediately converting to a long integer, which contains +// more digits than a float. Number of decimal places, which are tracked by a counter, +// may be set by the user. The integer is then efficiently converted to a string. +void printFloat(float n) { if (n < 0) { serial_write('-'); n = -n; } - n += 0.5/DECIMAL_MULTIPLIER; // Add rounding factor - - long integer_part; - integer_part = (int)n; - print_uint32_base10(integer_part); - - serial_write('.'); - - n -= integer_part; - int decimals = DECIMAL_PLACES; - uint8_t decimal_part; - while(decimals-- > 0) { - n *= 10; - decimal_part = (int) n; - serial_write('0'+decimal_part); - n -= decimal_part; + + uint8_t decimals = settings.decimal_places; + while (decimals >= 2) { // Quickly convert values expected to be E0 to E-4. + n *= 100; + decimals -= 2; } + if (decimals) { n *= 10; } + n += 0.5; // Add rounding factor. Ensures carryover through entire value. + + // Generate digits backwards and store in string. + unsigned char buf[10]; + uint8_t i = 0; + uint32_t a = (long)n; + buf[settings.decimal_places] = '.'; // Place decimal point, even if decimal places are zero. + while(a > 0) { + if (i == settings.decimal_places) { i++; } // Skip decimal point location + buf[i++] = (a % 10) + '0'; // Get digit + a /= 10; + } + while (i < settings.decimal_places) { + buf[i++] = '0'; // Fill in zeros to decimal point for (n < 1) + } + if (i == settings.decimal_places) { // Fill in leading zero, if needed. + i++; + buf[i++] = '0'; + } + + // Print the generated string. + for (; i > 0; i--) + serial_write(buf[i-1]); } diff --git a/settings.c b/settings.c index 86e4439..92c8226 100755 --- a/settings.c +++ b/settings.c @@ -77,6 +77,7 @@ typedef struct { // #define DEFAULT_AUTO_START 1 // true // #define DEFAULT_INCHES_MODE 1 // true // #define DEFAULT_BLOCK_DELETE 0 // false +#define DEFAULT_DECIMAL_PLACES 3 void settings_reset(bool reset_all) { // Reset all settings or only the migration settings to the new version. @@ -101,6 +102,7 @@ void settings_reset(bool reset_all) { settings.homing_seek_rate = DEFAULT_HOMING_RAPID_FEEDRATE; settings.homing_debounce_delay = DEFAULT_HOMING_DEBOUNCE_DELAY; settings.stepper_idle_lock_time = DEFAULT_STEPPER_IDLE_LOCK_TIME; + settings.decimal_places = DEFAULT_DECIMAL_PLACES; } void settings_dump() { @@ -120,7 +122,8 @@ void settings_dump() { printPgmString(PSTR(" (mm/min homing feed rate)\r\n$12 = ")); printFloat(settings.homing_seek_rate); printPgmString(PSTR(" (mm/min homing seek rate)\r\n$13 = ")); printInteger(settings.homing_debounce_delay); printPgmString(PSTR(" (milliseconds homing debounce delay)\r\n$14 = ")); printInteger(settings.stepper_idle_lock_time); - printPgmString(PSTR(" (milliseconds stepper idle lock time)")); + printPgmString(PSTR(" (milliseconds stepper idle lock time)\r\n$15 = ")); printInteger(settings.decimal_places); + printPgmString(PSTR(" (float decimal places)")); printPgmString(PSTR("\r\n'$x=value' to set parameter or just '$' to dump current settings\r\n")); } @@ -235,6 +238,7 @@ void settings_store_setting(int parameter, float value) { case 12: settings.homing_seek_rate = value; break; case 13: settings.homing_debounce_delay = round(value); break; case 14: settings.stepper_idle_lock_time = round(value); break; + case 15: settings.decimal_places = round(value); break; default: printPgmString(PSTR("Unknown parameter\r\n")); return; diff --git a/settings.h b/settings.h index f20f015..cb15bd6 100755 --- a/settings.h +++ b/settings.h @@ -29,7 +29,7 @@ // Version of the EEPROM data. Will be used to migrate existing data from older versions of Grbl // when firmware is upgraded. Always stored in byte 0 of eeprom -#define SETTINGS_VERSION 51 +#define SETTINGS_VERSION 52 // Define bit flag masks in settings.flag. #define FLAG_BIT_HOMING_ENABLE bit(0) @@ -52,6 +52,7 @@ typedef struct { float homing_seek_rate; uint16_t homing_debounce_delay; uint8_t stepper_idle_lock_time; + uint8_t decimal_places; } settings_t; extern settings_t settings;