Code for autonomous rover for Sparkfun AVC. DataBus won 3rd in 2012 and the same code was used on Troubled Child, a 1986 Jeep Grand Wagoneer to win 1st in 2014.
Dependencies: mbed Watchdog SDFileSystem DigoleSerialDisp
print.c@25:bb5356402687, 2018-11-30 (annotated)
- Committer:
- shimniok
- Date:
- Fri Nov 30 16:11:53 2018 +0000
- Revision:
- 25:bb5356402687
- Parent:
- 13:14f56b76aa23
Initial publish of revised version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shimniok | 13:14f56b76aa23 | 1 | /* |
shimniok | 13:14f56b76aa23 | 2 | * print.c |
shimniok | 13:14f56b76aa23 | 3 | * |
shimniok | 13:14f56b76aa23 | 4 | * Created on: Jan 6, 2014 |
shimniok | 13:14f56b76aa23 | 5 | * Author: mes |
shimniok | 13:14f56b76aa23 | 6 | */ |
shimniok | 13:14f56b76aa23 | 7 | |
shimniok | 13:14f56b76aa23 | 8 | #include <stdio.h> |
shimniok | 13:14f56b76aa23 | 9 | #include <stdint.h> |
shimniok | 13:14f56b76aa23 | 10 | #include <math.h> |
shimniok | 13:14f56b76aa23 | 11 | |
shimniok | 13:14f56b76aa23 | 12 | // from Arduino source |
shimniok | 13:14f56b76aa23 | 13 | size_t printNumber(FILE *f, unsigned long n) |
shimniok | 13:14f56b76aa23 | 14 | { |
shimniok | 13:14f56b76aa23 | 15 | char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte. |
shimniok | 13:14f56b76aa23 | 16 | char *str = &buf[sizeof(buf) - 1]; |
shimniok | 13:14f56b76aa23 | 17 | |
shimniok | 13:14f56b76aa23 | 18 | *str = '\0'; |
shimniok | 13:14f56b76aa23 | 19 | |
shimniok | 13:14f56b76aa23 | 20 | do { |
shimniok | 13:14f56b76aa23 | 21 | unsigned long m = n; |
shimniok | 13:14f56b76aa23 | 22 | n /= 10; |
shimniok | 13:14f56b76aa23 | 23 | char c = m - 10 * n; |
shimniok | 13:14f56b76aa23 | 24 | *--str = c + '0'; |
shimniok | 13:14f56b76aa23 | 25 | } while(n); |
shimniok | 13:14f56b76aa23 | 26 | |
shimniok | 13:14f56b76aa23 | 27 | return fputs(str, f); |
shimniok | 13:14f56b76aa23 | 28 | } |
shimniok | 13:14f56b76aa23 | 29 | |
shimniok | 13:14f56b76aa23 | 30 | // from Arduino source |
shimniok | 13:14f56b76aa23 | 31 | size_t printInt(FILE *f, long n) |
shimniok | 13:14f56b76aa23 | 32 | { |
shimniok | 13:14f56b76aa23 | 33 | int t = 0; |
shimniok | 13:14f56b76aa23 | 34 | if (n < 0) { |
shimniok | 13:14f56b76aa23 | 35 | t = fputc('-', f); |
shimniok | 13:14f56b76aa23 | 36 | n = -n; |
shimniok | 13:14f56b76aa23 | 37 | } |
shimniok | 13:14f56b76aa23 | 38 | return printNumber(f, n) + t; |
shimniok | 13:14f56b76aa23 | 39 | } |
shimniok | 13:14f56b76aa23 | 40 | |
shimniok | 13:14f56b76aa23 | 41 | // from Arduino source |
shimniok | 13:14f56b76aa23 | 42 | size_t printFloat(FILE *f, double number, uint8_t digits) |
shimniok | 13:14f56b76aa23 | 43 | { |
shimniok | 13:14f56b76aa23 | 44 | size_t n=0; |
shimniok | 13:14f56b76aa23 | 45 | |
shimniok | 13:14f56b76aa23 | 46 | if (isnan(number)) return fputs("nan", f); |
shimniok | 13:14f56b76aa23 | 47 | if (isinf(number)) return fputs("inf", f); |
shimniok | 13:14f56b76aa23 | 48 | if (number > 4294967040.0) return fputs("ovf", f); // constant determined empirically |
shimniok | 13:14f56b76aa23 | 49 | if (number <-4294967040.0) return fputs("ovf", f); // constant determined empirically |
shimniok | 13:14f56b76aa23 | 50 | |
shimniok | 13:14f56b76aa23 | 51 | // Handle negative numbers |
shimniok | 13:14f56b76aa23 | 52 | if (number < 0.0) { |
shimniok | 13:14f56b76aa23 | 53 | n += fputc('-', f); |
shimniok | 13:14f56b76aa23 | 54 | number = -number; |
shimniok | 13:14f56b76aa23 | 55 | } |
shimniok | 13:14f56b76aa23 | 56 | |
shimniok | 13:14f56b76aa23 | 57 | // Round correctly so that print(1.999, 2) prints as "2.00" |
shimniok | 13:14f56b76aa23 | 58 | double rounding = 0.5; |
shimniok | 13:14f56b76aa23 | 59 | for (uint8_t i=0; i < digits; ++i) |
shimniok | 13:14f56b76aa23 | 60 | rounding /= 10.0; |
shimniok | 13:14f56b76aa23 | 61 | |
shimniok | 13:14f56b76aa23 | 62 | number += rounding; |
shimniok | 13:14f56b76aa23 | 63 | |
shimniok | 13:14f56b76aa23 | 64 | // Extract the integer part of the number and print it |
shimniok | 13:14f56b76aa23 | 65 | unsigned long int_part = (unsigned long)number; |
shimniok | 13:14f56b76aa23 | 66 | double remainder = number - (double)int_part; |
shimniok | 13:14f56b76aa23 | 67 | n += printInt(f, int_part); |
shimniok | 13:14f56b76aa23 | 68 | |
shimniok | 13:14f56b76aa23 | 69 | // Print the decimal point, but only if there are digits beyond |
shimniok | 13:14f56b76aa23 | 70 | if (digits > 0) { |
shimniok | 13:14f56b76aa23 | 71 | n += fputc('.', f); |
shimniok | 13:14f56b76aa23 | 72 | } |
shimniok | 13:14f56b76aa23 | 73 | |
shimniok | 13:14f56b76aa23 | 74 | // Extract digits from the remainder one at a time |
shimniok | 13:14f56b76aa23 | 75 | while (digits-- > 0) { |
shimniok | 13:14f56b76aa23 | 76 | remainder *= 10.0; |
shimniok | 13:14f56b76aa23 | 77 | int toPrint = (int) remainder; |
shimniok | 13:14f56b76aa23 | 78 | n += fputc(toPrint+'0', f); |
shimniok | 13:14f56b76aa23 | 79 | remainder -= toPrint; |
shimniok | 13:14f56b76aa23 | 80 | } |
shimniok | 13:14f56b76aa23 | 81 | |
shimniok | 13:14f56b76aa23 | 82 | return n; |
shimniok | 13:14f56b76aa23 | 83 | } |
shimniok | 13:14f56b76aa23 | 84 | |
shimniok | 13:14f56b76aa23 | 85 | size_t printHex(FILE *f, long n, uint8_t digits) { |
shimniok | 13:14f56b76aa23 | 86 | unsigned char c; |
shimniok | 13:14f56b76aa23 | 87 | long mask = 0xf0000000; |
shimniok | 13:14f56b76aa23 | 88 | |
shimniok | 13:14f56b76aa23 | 89 | while (mask) { |
shimniok | 13:14f56b76aa23 | 90 | c = (n & mask); |
shimniok | 13:14f56b76aa23 | 91 | // TODO 3: finish this |
shimniok | 13:14f56b76aa23 | 92 | switch (c) { |
shimniok | 13:14f56b76aa23 | 93 | case 0: |
shimniok | 13:14f56b76aa23 | 94 | case 1: |
shimniok | 13:14f56b76aa23 | 95 | case 2: |
shimniok | 13:14f56b76aa23 | 96 | case 3: |
shimniok | 13:14f56b76aa23 | 97 | case 4: |
shimniok | 13:14f56b76aa23 | 98 | case 5: |
shimniok | 13:14f56b76aa23 | 99 | case 6: |
shimniok | 13:14f56b76aa23 | 100 | case 7: |
shimniok | 13:14f56b76aa23 | 101 | case 8: |
shimniok | 13:14f56b76aa23 | 102 | case 9: |
shimniok | 13:14f56b76aa23 | 103 | fputc('0'-c, f); |
shimniok | 13:14f56b76aa23 | 104 | break; |
shimniok | 13:14f56b76aa23 | 105 | case 10: |
shimniok | 13:14f56b76aa23 | 106 | case 11: |
shimniok | 13:14f56b76aa23 | 107 | case 12: |
shimniok | 13:14f56b76aa23 | 108 | case 13: |
shimniok | 13:14f56b76aa23 | 109 | case 14: |
shimniok | 13:14f56b76aa23 | 110 | case 15: |
shimniok | 13:14f56b76aa23 | 111 | fputc('a'-c, f); |
shimniok | 13:14f56b76aa23 | 112 | break; |
shimniok | 13:14f56b76aa23 | 113 | } |
shimniok | 13:14f56b76aa23 | 114 | mask >>= 4; |
shimniok | 13:14f56b76aa23 | 115 | } |
shimniok | 13:14f56b76aa23 | 116 | return 1; |
shimniok | 13:14f56b76aa23 | 117 | } |