|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
-
-
- #include "avr_functions.h"
- #include <string.h>
- #include <stdlib.h>
- #include <math.h>
-
-
- char * ultoa(unsigned long val, char *buf, int radix)
- {
- unsigned digit;
- int i=0, j;
- char t;
-
- while (1) {
- digit = val % radix;
- buf[i] = ((digit < 10) ? '0' + digit : 'A' + digit - 10);
- val /= radix;
- if (val == 0) break;
- i++;
- }
- buf[i + 1] = 0;
- for (j=0; j < i; j++, i--) {
- t = buf[j];
- buf[j] = buf[i];
- buf[i] = t;
- }
- return buf;
- }
-
- char * ltoa(long val, char *buf, int radix)
- {
- if (val >= 0) {
- return ultoa(val, buf, radix);
- } else {
- buf[0] = '-';
- ultoa(-val, buf + 1, radix);
- return buf;
- }
- }
-
- #define DTOA_UPPER 0x04
-
- char * fcvtf(float, int, int *, int *);
- int isnanf (float x);
- int isinff (float x);
-
- char * dtostrf(float val, int width, unsigned int precision, char *buf)
- {
- int decpt, sign, reqd, pad;
- const char *s, *e;
- char *p;
-
- int awidth = abs(width);
- if (isnanf(val)) {
- int ndigs = (val<0) ? 4 : 3;
- awidth = (awidth > ndigs) ? awidth - ndigs : 0;
- if (width<0) {
- while (awidth) {
- *buf++ = ' ';
- awidth--;
- }
- }
- if (copysignf(1.0f, val)<0) *buf++ = '-';
- if (DTOA_UPPER) {
- *buf++ = 'N'; *buf++ = 'A'; *buf++ = 'N';
- } else {
- *buf++ = 'n'; *buf++ = 'a'; *buf++ = 'n';
- }
- while (awidth) {
- *buf++ = ' ';
- awidth--;
- }
- *buf = 0;
- return buf;
- }
- if (isinff(val)) {
- int ndigs = (val<0) ? 4 : 3;
- awidth = (awidth > ndigs) ? awidth - ndigs : 0;
- if (width<0) {
- while (awidth) {
- *buf++ = ' ';
- awidth--;
- }
- }
- if (val<0) *buf++ = '-';
- if (DTOA_UPPER) {
- *buf++ = 'I'; *buf++ = 'N'; *buf++ = 'F';
- } else {
- *buf++ = 'i'; *buf++ = 'n'; *buf++ = 'f';
- }
- while (awidth) {
- *buf++ = ' ';
- awidth--;
- }
- *buf = 0;
- return buf;
- }
-
- s = fcvtf(val, precision, &decpt, &sign);
-
-
- if (precision == 0 && decpt == 0) {
-
- s = (*s < '5') ? "0" : "1";
- decpt++;
- }
-
-
- if (-decpt > (int)precision) {
- s = "0";
- decpt = -precision;
- }
-
- reqd = strlen(s);
-
-
- if (reqd > decpt) reqd++;
-
-
- if (decpt == 0) reqd++;
-
-
- if (decpt < 0 && precision > 0) {
-
- reqd = precision + 2;
-
- if (strlen(s) > precision + decpt) {
-
-
-
-
- int newPrecision = precision;
- int newDecimalPoint;
-
-
- while (newPrecision > 0) {
- val *= 10.0;
- newPrecision--;
- }
-
-
- s = fcvtf(val, newPrecision, &newDecimalPoint, &sign);
-
-
- if (newDecimalPoint - decpt == precision + 1) decpt++;
- }
- }
-
-
- if (sign) reqd++;
-
- p = buf;
- e = p + reqd;
- pad = width - reqd;
- if (pad > 0) {
- e += pad;
- while (pad-- > 0) *p++ = ' ';
- }
- if (sign) *p++ = '-';
- if (decpt == 0 && precision > 0) {
- *p++ = '0';
- *p++ = '.';
- }
- else if (decpt < 0 && precision > 0) {
- *p++ = '0';
- *p++ = '.';
-
- while ( decpt < 0 ) {
- decpt++;
- *p++ = '0';
- }
- }
-
- while (p < e) {
- *p++ = *s++;
- if (p == e) break;
- if (--decpt == 0) *p++ = '.';
- }
- if (width < 0) {
- pad = (reqd + width) * -1;
- while (pad-- > 0) *p++ = ' ';
- }
- *p = 0;
-
-
-
-
- return buf;
- }
-
|