From: Joseph Coffland Date: Thu, 31 Dec 2015 03:46:56 +0000 (-0800) Subject: More cleanup X-Git-Url: https://git.buildbotics.com/?a=commitdiff_plain;h=a1160e820c4a8edc72f7db6952fbb0c2bb8bbaec;p=bbctrl-firmware More cleanup --- diff --git a/src/util.c b/src/util.c index b583c83..4836f84 100644 --- a/src/util.c +++ b/src/util.c @@ -46,54 +46,55 @@ float vector[AXES]; // statically allocated global for vector utilities -uint8_t vector_equal(const float a[], const float b[]) -{ - if ((fp_EQ(a[AXIS_X], b[AXIS_X])) && - (fp_EQ(a[AXIS_Y], b[AXIS_Y])) && - (fp_EQ(a[AXIS_Z], b[AXIS_Z])) && - (fp_EQ(a[AXIS_A], b[AXIS_A])) && - (fp_EQ(a[AXIS_B], b[AXIS_B])) && - (fp_EQ(a[AXIS_C], b[AXIS_C]))) { - return true; - } - return false; +uint8_t vector_equal(const float a[], const float b[]) { + if ((fp_EQ(a[AXIS_X], b[AXIS_X])) && + (fp_EQ(a[AXIS_Y], b[AXIS_Y])) && + (fp_EQ(a[AXIS_Z], b[AXIS_Z])) && + (fp_EQ(a[AXIS_A], b[AXIS_A])) && + (fp_EQ(a[AXIS_B], b[AXIS_B])) && + (fp_EQ(a[AXIS_C], b[AXIS_C]))) + return true; + + + return false; } -float get_axis_vector_length(const float a[], const float b[]) -{ - return sqrt(square(a[AXIS_X] - b[AXIS_X] + - square(a[AXIS_Y] - b[AXIS_Y]) + - square(a[AXIS_Z] - b[AXIS_Z]) + - square(a[AXIS_A] - b[AXIS_A]) + - square(a[AXIS_B] - b[AXIS_B]) + - square(a[AXIS_C] - b[AXIS_C]))); + +float get_axis_vector_length(const float a[], const float b[]) { + return sqrt(square(a[AXIS_X] - b[AXIS_X] + + square(a[AXIS_Y] - b[AXIS_Y]) + + square(a[AXIS_Z] - b[AXIS_Z]) + + square(a[AXIS_A] - b[AXIS_A]) + + square(a[AXIS_B] - b[AXIS_B]) + + square(a[AXIS_C] - b[AXIS_C]))); } -float *set_vector(float x, float y, float z, float a, float b, float c) -{ - vector[AXIS_X] = x; - vector[AXIS_Y] = y; - vector[AXIS_Z] = z; - vector[AXIS_A] = a; - vector[AXIS_B] = b; - vector[AXIS_C] = c; - return vector; + +float *set_vector(float x, float y, float z, float a, float b, float c) { + vector[AXIS_X] = x; + vector[AXIS_Y] = y; + vector[AXIS_Z] = z; + vector[AXIS_A] = a; + vector[AXIS_B] = b; + vector[AXIS_C] = c; + return vector; } -float *set_vector_by_axis(float value, uint8_t axis) -{ - clear_vector(vector); - switch (axis) { - case (AXIS_X): vector[AXIS_X] = value; break; - case (AXIS_Y): vector[AXIS_Y] = value; break; - case (AXIS_Z): vector[AXIS_Z] = value; break; - case (AXIS_A): vector[AXIS_A] = value; break; - case (AXIS_B): vector[AXIS_B] = value; break; - case (AXIS_C): vector[AXIS_C] = value; - } - return vector; + +float *set_vector_by_axis(float value, uint8_t axis) { + clear_vector(vector); + switch (axis) { + case (AXIS_X): vector[AXIS_X] = value; break; + case (AXIS_Y): vector[AXIS_Y] = value; break; + case (AXIS_Z): vector[AXIS_Z] = value; break; + case (AXIS_A): vector[AXIS_A] = value; break; + case (AXIS_B): vector[AXIS_B] = value; break; + case (AXIS_C): vector[AXIS_C] = value; + } + return vector; } + /**** Math and other general purpose functions ****/ /* Slightly faster (*) multi-value min and max functions @@ -110,39 +111,37 @@ float *set_vector_by_axis(float value, uint8_t axis) * #define max3(a,b,c) (max(max(a,b),c)) * #define max4(a,b,c,d) (max(max(a,b),max(c,d))) */ - -float min3(float x1, float x2, float x3) -{ - float min = x1; - if (x2 < min) { min = x2;} - if (x3 < min) { return x3;} - return min; +float min3(float x1, float x2, float x3) { + float min = x1; + if (x2 < min) min = x2; + if (x3 < min) return x3; + return min; } -float min4(float x1, float x2, float x3, float x4) -{ - float min = x1; - if (x2 < min) { min = x2;} - if (x3 < min) { min = x3;} - if (x4 < min) { return x4;} - return min; + +float min4(float x1, float x2, float x3, float x4) { + float min = x1; + if (x2 < min) min = x2; + if (x3 < min) min = x3; + if (x4 < min) return x4; + return min; } -float max3(float x1, float x2, float x3) -{ - float max = x1; - if (x2 > max) { max = x2;} - if (x3 > max) { return x3;} - return max; + +float max3(float x1, float x2, float x3) { + float max = x1; + if (x2 > max) max = x2; + if (x3 > max) return x3; + return max; } -float max4(float x1, float x2, float x3, float x4) -{ - float max = x1; - if (x2 > max) { max = x2;} - if (x3 > max) { max = x3;} - if (x4 > max) { return x4;} - return max; + +float max4(float x1, float x2, float x3, float x4) { + float max = x1; + if (x2 > max) max = x2; + if (x3 > max) max = x3; + if (x4 > max) return x4; + return max; } @@ -151,64 +150,63 @@ float max4(float x1, float x2, float x3, float x4) * isnumber() - isdigit that also accepts plus, minus, and decimal point * escape_string() - add escapes to a string - currently for quotes only */ - -uint8_t isnumber(char_t c) -{ - if (c == '.') { return true; } - if (c == '-') { return true; } - if (c == '+') { return true; } - return isdigit(c); +uint8_t isnumber(char_t c) { + if (c == '.') return true; + if (c == '-') return true; + if (c == '+') return true; + return isdigit(c); } -char_t *escape_string(char_t *dst, char_t *src) -{ - char_t c; - char_t *start_dst = dst; - while ((c = *(src++)) != 0) { // 0 - if (c == '"') { *(dst++) = '\\'; } - *(dst++) = c; - } - return start_dst; +char_t *escape_string(char_t *dst, char_t *src) { + char_t c; + char_t *start_dst = dst; + + while ((c = *(src++)) != 0) { // 0 + if (c == '"') *(dst++) = '\\'; + *(dst++) = c; + } + + return start_dst; } + /* * pstr2str() - return an AVR style progmem string as a RAM string. * * This function copies a string from FLASH to a pre-allocated RAM buffer - * see main.c for allocation and max length. */ -char_t *pstr2str(const char *pgm_string) -{ - strncpy_P(global_string_buf, pgm_string, MESSAGE_LEN); - return global_string_buf; +char_t *pstr2str(const char *pgm_string) { + strncpy_P(global_string_buf, pgm_string, MESSAGE_LEN); + return global_string_buf; } + /* * fntoa() - return ASCII string given a float and a decimal precision value * * Returns length of string, less the terminating 0 character */ -char_t fntoa(char_t *str, float n, uint8_t precision) -{ - // handle special cases - if (isnan(n)) { - strcpy(str, "nan"); - return 3; - - } else if (isinf(n)) { - strcpy(str, "inf"); - return 3; - - } else if (precision == 0 ) { return((char_t)sprintf((char *)str, "%0.0f", (double) n)); - } else if (precision == 1 ) { return((char_t)sprintf((char *)str, "%0.1f", (double) n)); - } else if (precision == 2 ) { return((char_t)sprintf((char *)str, "%0.2f", (double) n)); - } else if (precision == 3 ) { return((char_t)sprintf((char *)str, "%0.3f", (double) n)); - } else if (precision == 4 ) { return((char_t)sprintf((char *)str, "%0.4f", (double) n)); - } else if (precision == 5 ) { return((char_t)sprintf((char *)str, "%0.5f", (double) n)); - } else if (precision == 6 ) { return((char_t)sprintf((char *)str, "%0.6f", (double) n)); - } else if (precision == 7 ) { return((char_t)sprintf((char *)str, "%0.7f", (double) n)); - } else { return((char_t)sprintf((char *)str, "%f", (double) n)); } +char_t fntoa(char_t *str, float n, uint8_t precision) { + // handle special cases + if (isnan(n)) { + strcpy(str, "nan"); + return 3; + + } else if (isinf(n)) { + strcpy(str, "inf"); + return 3; + + } else if (precision == 0 ) {return (char_t)sprintf((char *)str, "%0.0f", (double)n); + } else if (precision == 1 ) {return (char_t)sprintf((char *)str, "%0.1f", (double)n); + } else if (precision == 2 ) {return (char_t)sprintf((char *)str, "%0.2f", (double)n); + } else if (precision == 3 ) {return (char_t)sprintf((char *)str, "%0.3f", (double)n); + } else if (precision == 4 ) {return (char_t)sprintf((char *)str, "%0.4f", (double)n); + } else if (precision == 5 ) {return (char_t)sprintf((char *)str, "%0.5f", (double)n); + } else if (precision == 6 ) {return (char_t)sprintf((char *)str, "%0.6f", (double)n); + } else if (precision == 7 ) {return (char_t)sprintf((char *)str, "%0.7f", (double)n); + } else return (char_t)sprintf((char *)str, "%f", (double)n); } /* @@ -221,22 +219,20 @@ char_t fntoa(char_t *str, float n, uint8_t precision) */ #define HASHMASK 9999 -uint16_t compute_checksum(char_t const *string, const uint16_t length) -{ - uint32_t h = 0; - uint16_t len = strlen(string); - if (length != 0) len = min(len, length); - for (uint16_t i=0; i