|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- /* Teensyduino Core Library
- * http://www.pjrc.com/teensy/
- * Copyright (c) 2013 PJRC.COM, LLC.
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sublicense, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- *
- * 1. The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- * 2. If the Software is incorporated into a build system that allows
- * selection among a list of target devices, then similar target
- * devices manufactured by PJRC.COM must be included in the list of
- * target devices and selectable in the same manner.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
- * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
- * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- #include "avr_functions.h"
- #include <string.h>
- #include <stdlib.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;
- }
- }
-
- char * dtostrf(float val, int width, unsigned int precision, char *buf)
- {
- int decpt, sign, reqd, pad;
- const char *s, *e;
- char *p;
-
- s = fcvtf(val, precision, &decpt, &sign);
- if (precision == 0 && decpt == 0) {
- s = (*s < '5') ? "0" : "1";
- reqd = 1;
- } else {
- reqd = strlen(s);
- if (reqd > decpt) reqd++;
- if (decpt == 0) reqd++;
- }
- 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++ = '.';
- e++;
- 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;
-
- //char format[20];
- //sprintf(format, "%%%d.%df", width, precision);
- //sprintf(buf, format, val);
- return buf;
- }
-
|