Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

9 лет назад
8 лет назад
8 лет назад
9 лет назад
9 лет назад
9 лет назад
9 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #include "avr_functions.h"
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <math.h>
  34. char * ultoa(unsigned long val, char *buf, int radix)
  35. {
  36. unsigned digit;
  37. int i=0, j;
  38. char t;
  39. while (1) {
  40. digit = val % radix;
  41. buf[i] = ((digit < 10) ? '0' + digit : 'A' + digit - 10);
  42. val /= radix;
  43. if (val == 0) break;
  44. i++;
  45. }
  46. buf[i + 1] = 0;
  47. for (j=0; j < i; j++, i--) {
  48. t = buf[j];
  49. buf[j] = buf[i];
  50. buf[i] = t;
  51. }
  52. return buf;
  53. }
  54. char * ltoa(long val, char *buf, int radix)
  55. {
  56. if (val >= 0) {
  57. return ultoa(val, buf, radix);
  58. } else {
  59. buf[0] = '-';
  60. ultoa(-val, buf + 1, radix);
  61. return buf;
  62. }
  63. }
  64. #define DTOA_UPPER 0x04
  65. char * fcvtf(float, int, int *, int *);
  66. int isnanf (float x);
  67. int isinff (float x);
  68. char * dtostrf(float val, int width, unsigned int precision, char *buf)
  69. {
  70. int decpt, sign, reqd, pad;
  71. const char *s, *e;
  72. char *p;
  73. int awidth = abs(width);
  74. if (isnanf(val)) {
  75. int ndigs = (val<0) ? 4 : 3;
  76. awidth = (awidth > ndigs) ? awidth - ndigs : 0;
  77. if (width<0) {
  78. while (awidth) {
  79. *buf++ = ' ';
  80. awidth--;
  81. }
  82. }
  83. if (copysignf(1.0f, val)<0) *buf++ = '-';
  84. if (DTOA_UPPER) {
  85. *buf++ = 'N'; *buf++ = 'A'; *buf++ = 'N';
  86. } else {
  87. *buf++ = 'n'; *buf++ = 'a'; *buf++ = 'n';
  88. }
  89. while (awidth) {
  90. *buf++ = ' ';
  91. awidth--;
  92. }
  93. *buf = 0;
  94. return buf;
  95. }
  96. if (isinff(val)) {
  97. int ndigs = (val<0) ? 4 : 3;
  98. awidth = (awidth > ndigs) ? awidth - ndigs : 0;
  99. if (width<0) {
  100. while (awidth) {
  101. *buf++ = ' ';
  102. awidth--;
  103. }
  104. }
  105. if (val<0) *buf++ = '-';
  106. if (DTOA_UPPER) {
  107. *buf++ = 'I'; *buf++ = 'N'; *buf++ = 'F';
  108. } else {
  109. *buf++ = 'i'; *buf++ = 'n'; *buf++ = 'f';
  110. }
  111. while (awidth) {
  112. *buf++ = ' ';
  113. awidth--;
  114. }
  115. *buf = 0;
  116. return buf;
  117. }
  118. s = fcvtf(val, precision, &decpt, &sign);
  119. if (precision == 0 && decpt == 0) {
  120. s = (*s < '5') ? "0" : "1";
  121. reqd = 1;
  122. } else {
  123. reqd = strlen(s);
  124. if (reqd > decpt) reqd++;
  125. if (decpt == 0) reqd++;
  126. }
  127. if (sign) reqd++;
  128. p = buf;
  129. e = p + reqd;
  130. pad = width - reqd;
  131. if (pad > 0) {
  132. e += pad;
  133. while (pad-- > 0) *p++ = ' ';
  134. }
  135. if (sign) *p++ = '-';
  136. if (decpt == 0 && precision > 0) {
  137. *p++ = '0';
  138. *p++ = '.';
  139. }
  140. else if (decpt < 0 && precision > 0) {
  141. *p++ = '0';
  142. *p++ = '.';
  143. e++;
  144. while ( decpt < 0 ) {
  145. decpt++;
  146. *p++ = '0';
  147. }
  148. }
  149. while (p < e) {
  150. *p++ = *s++;
  151. if (p == e) break;
  152. if (--decpt == 0) *p++ = '.';
  153. }
  154. if (width < 0) {
  155. pad = (reqd + width) * -1;
  156. while (pad-- > 0) *p++ = ' ';
  157. }
  158. *p = 0;
  159. //char format[20];
  160. //sprintf(format, "%%%d.%df", width, precision);
  161. //sprintf(buf, format, val);
  162. return buf;
  163. }