gcode_parse.c: make all usages of decfloat_to_int()...

... to divide either by 1 or 1000. Accordingly, a flag
can be used instead of the actual value, shrinking code
size by 70 bytes.
This commit is contained in:
Markus Hitter 2011-04-10 22:57:19 +02:00
parent 26b9a7df54
commit f41dd9c098
1 changed files with 19 additions and 18 deletions

View File

@ -85,13 +85,14 @@ const int32_t rounding[DECFLOAT_EXP_MAX] = {0, 5, 50, 500};
/// convert a floating point input value into an integer with appropriate scaling. /// convert a floating point input value into an integer with appropriate scaling.
/// \param *df pointer to floating point structure that holds fp value to convert /// \param *df pointer to floating point structure that holds fp value to convert
/// \param multiplicand multiply by this amount during conversion to integer /// \param multiplicand multiply by this amount during conversion to integer
/// \param denominator divide by this amount during conversion to integer /// \param divide_by_1000 divide by 1000 during conversion to integer
/// ///
/// lots of work has been done in exploring this function's limitations in terms of overflow and rounding /// lots of work has been done in exploring this function's limitations in terms of overflow and rounding
/// this work may not be finished /// this work may not be finished
static int32_t decfloat_to_int(decfloat *df, uint32_t multiplicand, uint32_t denominator) { static int32_t decfloat_to_int(decfloat *df, uint32_t multiplicand, uint8_t divide_by_1000) {
uint32_t r = df->mantissa; uint32_t r = df->mantissa;
uint8_t e = df->exponent; uint8_t e = df->exponent;
uint32_t denominator = divide_by_1000 ? 1000 : 1;
// e=1 means we've seen a decimal point but no digits after it, and e=2 means we've seen a decimal point with one digit so it's too high by one if not zero // e=1 means we've seen a decimal point but no digits after it, and e=2 means we've seen a decimal point with one digit so it's too high by one if not zero
if (e) if (e)
@ -139,42 +140,42 @@ void gcode_parse_char(uint8_t c) {
break; break;
case 'X': case 'X':
if (next_target.option_inches) if (next_target.option_inches)
next_target.target.X = decfloat_to_int(&read_digit, STEPS_PER_IN_X, 1); next_target.target.X = decfloat_to_int(&read_digit, STEPS_PER_IN_X, 0);
else else
next_target.target.X = decfloat_to_int(&read_digit, STEPS_PER_M_X, 1000); next_target.target.X = decfloat_to_int(&read_digit, STEPS_PER_M_X, 1);
if (debug_flags & DEBUG_ECHO) if (debug_flags & DEBUG_ECHO)
serwrite_int32(next_target.target.X); serwrite_int32(next_target.target.X);
break; break;
case 'Y': case 'Y':
if (next_target.option_inches) if (next_target.option_inches)
next_target.target.Y = decfloat_to_int(&read_digit, STEPS_PER_IN_Y, 1); next_target.target.Y = decfloat_to_int(&read_digit, STEPS_PER_IN_Y, 0);
else else
next_target.target.Y = decfloat_to_int(&read_digit, STEPS_PER_M_Y, 1000); next_target.target.Y = decfloat_to_int(&read_digit, STEPS_PER_M_Y, 1);
if (debug_flags & DEBUG_ECHO) if (debug_flags & DEBUG_ECHO)
serwrite_int32(next_target.target.Y); serwrite_int32(next_target.target.Y);
break; break;
case 'Z': case 'Z':
if (next_target.option_inches) if (next_target.option_inches)
next_target.target.Z = decfloat_to_int(&read_digit, STEPS_PER_IN_Z, 1); next_target.target.Z = decfloat_to_int(&read_digit, STEPS_PER_IN_Z, 0);
else else
next_target.target.Z = decfloat_to_int(&read_digit, STEPS_PER_M_Z, 1000); next_target.target.Z = decfloat_to_int(&read_digit, STEPS_PER_M_Z, 1);
if (debug_flags & DEBUG_ECHO) if (debug_flags & DEBUG_ECHO)
serwrite_int32(next_target.target.Z); serwrite_int32(next_target.target.Z);
break; break;
case 'E': case 'E':
if (next_target.option_inches) if (next_target.option_inches)
next_target.target.E = decfloat_to_int(&read_digit, STEPS_PER_IN_E, 1); next_target.target.E = decfloat_to_int(&read_digit, STEPS_PER_IN_E, 0);
else else
next_target.target.E = decfloat_to_int(&read_digit, STEPS_PER_M_E, 1000); next_target.target.E = decfloat_to_int(&read_digit, STEPS_PER_M_E, 1);
if (debug_flags & DEBUG_ECHO) if (debug_flags & DEBUG_ECHO)
serwrite_uint32(next_target.target.E); serwrite_uint32(next_target.target.E);
break; break;
case 'F': case 'F':
// just use raw integer, we need move distance and n_steps to convert it to a useful value, so wait until we have those to convert it // just use raw integer, we need move distance and n_steps to convert it to a useful value, so wait until we have those to convert it
if (next_target.option_inches) if (next_target.option_inches)
next_target.target.F = decfloat_to_int(&read_digit, 254, 10); next_target.target.F = decfloat_to_int(&read_digit, 25400, 1);
else else
next_target.target.F = decfloat_to_int(&read_digit, 1, 1); next_target.target.F = decfloat_to_int(&read_digit, 1, 0);
if (debug_flags & DEBUG_ECHO) if (debug_flags & DEBUG_ECHO)
serwrite_uint32(next_target.target.F); serwrite_uint32(next_target.target.F);
break; break;
@ -183,17 +184,17 @@ void gcode_parse_char(uint8_t c) {
// cosmetically this should be done in the temperature section, // cosmetically this should be done in the temperature section,
// but it takes less code, less memory and loses no precision if we do it here instead // but it takes less code, less memory and loses no precision if we do it here instead
if ((next_target.M == 104) || (next_target.M == 109) || (next_target.M == 140)) if ((next_target.M == 104) || (next_target.M == 109) || (next_target.M == 140))
next_target.S = decfloat_to_int(&read_digit, 4, 1); next_target.S = decfloat_to_int(&read_digit, 4, 0);
// if this is heater PID stuff, multiply by PID_SCALE because we divide by PID_SCALE later on // if this is heater PID stuff, multiply by PID_SCALE because we divide by PID_SCALE later on
else if ((next_target.M >= 130) && (next_target.M <= 132)) else if ((next_target.M >= 130) && (next_target.M <= 132))
next_target.S = decfloat_to_int(&read_digit, PID_SCALE, 1); next_target.S = decfloat_to_int(&read_digit, PID_SCALE, 0);
else else
next_target.S = decfloat_to_int(&read_digit, 1, 1); next_target.S = decfloat_to_int(&read_digit, 1, 0);
if (debug_flags & DEBUG_ECHO) if (debug_flags & DEBUG_ECHO)
serwrite_uint16(next_target.S); serwrite_uint16(next_target.S);
break; break;
case 'P': case 'P':
next_target.P = decfloat_to_int(&read_digit, 1, 1); next_target.P = decfloat_to_int(&read_digit, 1, 0);
if (debug_flags & DEBUG_ECHO) if (debug_flags & DEBUG_ECHO)
serwrite_uint16(next_target.P); serwrite_uint16(next_target.P);
break; break;
@ -203,12 +204,12 @@ void gcode_parse_char(uint8_t c) {
serwrite_uint8(next_target.T); serwrite_uint8(next_target.T);
break; break;
case 'N': case 'N':
next_target.N = decfloat_to_int(&read_digit, 1, 1); next_target.N = decfloat_to_int(&read_digit, 1, 0);
if (debug_flags & DEBUG_ECHO) if (debug_flags & DEBUG_ECHO)
serwrite_uint32(next_target.N); serwrite_uint32(next_target.N);
break; break;
case '*': case '*':
next_target.checksum_read = decfloat_to_int(&read_digit, 1, 1); next_target.checksum_read = decfloat_to_int(&read_digit, 1, 0);
if (debug_flags & DEBUG_ECHO) if (debug_flags & DEBUG_ECHO)
serwrite_uint8(next_target.checksum_read); serwrite_uint8(next_target.checksum_read);
break; break;