Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TFT_Touch_botao_v1 TFT_Touch_exemplo5_git_touch TESTE_1 TFT_Touch_exemplo6_git_touch_button_3_
avr/dtostrf.c@2:81364824d56a, 2021-05-11 (annotated)
- Committer:
- davidprentice
- Date:
- Tue May 11 16:24:13 2021 +0000
- Revision:
- 2:81364824d56a
- Parent:
- 0:5952bbaff1c6
add includes, typedef byte
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| davidprentice | 0:5952bbaff1c6 | 1 | /* |
| davidprentice | 0:5952bbaff1c6 | 2 | dtostrf - Emulation for dtostrf function from avr-libc |
| davidprentice | 0:5952bbaff1c6 | 3 | Copyright (c) 2013 Arduino. All rights reserved. |
| davidprentice | 0:5952bbaff1c6 | 4 | Written by Cristian Maglie <c.maglie@arduino.cc> |
| davidprentice | 0:5952bbaff1c6 | 5 | |
| davidprentice | 0:5952bbaff1c6 | 6 | This library is free software; you can redistribute it and/or |
| davidprentice | 0:5952bbaff1c6 | 7 | modify it under the terms of the GNU Lesser General Public |
| davidprentice | 0:5952bbaff1c6 | 8 | License as published by the Free Software Foundation; either |
| davidprentice | 0:5952bbaff1c6 | 9 | version 2.1 of the License, or (at your option) any later version. |
| davidprentice | 0:5952bbaff1c6 | 10 | |
| davidprentice | 0:5952bbaff1c6 | 11 | This library is distributed in the hope that it will be useful, |
| davidprentice | 0:5952bbaff1c6 | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| davidprentice | 0:5952bbaff1c6 | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| davidprentice | 0:5952bbaff1c6 | 14 | Lesser General Public License for more details. |
| davidprentice | 0:5952bbaff1c6 | 15 | |
| davidprentice | 0:5952bbaff1c6 | 16 | You should have received a copy of the GNU Lesser General Public |
| davidprentice | 0:5952bbaff1c6 | 17 | License along with this library; if not, write to the Free Software |
| davidprentice | 0:5952bbaff1c6 | 18 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| davidprentice | 0:5952bbaff1c6 | 19 | */ |
| davidprentice | 0:5952bbaff1c6 | 20 | |
| davidprentice | 0:5952bbaff1c6 | 21 | #include <stdio.h> |
| davidprentice | 0:5952bbaff1c6 | 22 | #include <stdlib.h> |
| davidprentice | 0:5952bbaff1c6 | 23 | #include <string.h> |
| davidprentice | 0:5952bbaff1c6 | 24 | #include <stdint.h> //.kbv |
| davidprentice | 0:5952bbaff1c6 | 25 | |
| davidprentice | 0:5952bbaff1c6 | 26 | char *dtostrf(double val, signed char width, unsigned char prec, char *sout) |
| davidprentice | 0:5952bbaff1c6 | 27 | { |
| davidprentice | 0:5952bbaff1c6 | 28 | //Commented code is the original version |
| davidprentice | 0:5952bbaff1c6 | 29 | /*char fmt[20]; |
| davidprentice | 0:5952bbaff1c6 | 30 | sprintf(fmt, "%%%d.%df", width, prec); |
| davidprentice | 0:5952bbaff1c6 | 31 | sprintf(sout, fmt, val); |
| davidprentice | 0:5952bbaff1c6 | 32 | return sout;*/ |
| davidprentice | 0:5952bbaff1c6 | 33 | |
| davidprentice | 0:5952bbaff1c6 | 34 | // Handle negative numbers |
| davidprentice | 0:5952bbaff1c6 | 35 | uint8_t negative = 0; |
| davidprentice | 0:5952bbaff1c6 | 36 | if (val < 0.0) { |
| davidprentice | 0:5952bbaff1c6 | 37 | negative = 1; |
| davidprentice | 0:5952bbaff1c6 | 38 | val = -val; |
| davidprentice | 0:5952bbaff1c6 | 39 | } |
| davidprentice | 0:5952bbaff1c6 | 40 | |
| davidprentice | 0:5952bbaff1c6 | 41 | // Round correctly so that print(1.999, 2) prints as "2.00" |
| davidprentice | 0:5952bbaff1c6 | 42 | double rounding = 0.5; |
| davidprentice | 0:5952bbaff1c6 | 43 | for (int i = 0; i < prec; ++i) { |
| davidprentice | 0:5952bbaff1c6 | 44 | rounding /= 10.0; |
| davidprentice | 0:5952bbaff1c6 | 45 | } |
| davidprentice | 0:5952bbaff1c6 | 46 | |
| davidprentice | 0:5952bbaff1c6 | 47 | val += rounding; |
| davidprentice | 0:5952bbaff1c6 | 48 | |
| davidprentice | 0:5952bbaff1c6 | 49 | // Extract the integer part of the number |
| davidprentice | 0:5952bbaff1c6 | 50 | unsigned long int_part = (unsigned long)val; |
| davidprentice | 0:5952bbaff1c6 | 51 | double remainder = val - (double)int_part; |
| davidprentice | 0:5952bbaff1c6 | 52 | |
| davidprentice | 0:5952bbaff1c6 | 53 | // Extract digits from the remainder |
| davidprentice | 0:5952bbaff1c6 | 54 | unsigned long dec_part = 0; |
| davidprentice | 0:5952bbaff1c6 | 55 | double decade = 1.0; |
| davidprentice | 0:5952bbaff1c6 | 56 | for (int i = 0; i < prec; i++) { |
| davidprentice | 0:5952bbaff1c6 | 57 | decade *= 10.0; |
| davidprentice | 0:5952bbaff1c6 | 58 | } |
| davidprentice | 0:5952bbaff1c6 | 59 | remainder *= decade; |
| davidprentice | 0:5952bbaff1c6 | 60 | dec_part = (int)remainder; |
| davidprentice | 0:5952bbaff1c6 | 61 | |
| davidprentice | 0:5952bbaff1c6 | 62 | if (negative) { |
| davidprentice | 0:5952bbaff1c6 | 63 | sprintf(sout, "-%ld.%0*ld", int_part, prec, dec_part); |
| davidprentice | 0:5952bbaff1c6 | 64 | } else { |
| davidprentice | 0:5952bbaff1c6 | 65 | sprintf(sout, "%ld.%0*ld", int_part, prec, dec_part); |
| davidprentice | 0:5952bbaff1c6 | 66 | } |
| davidprentice | 0:5952bbaff1c6 | 67 | // Handle minimum field width of the output string |
| davidprentice | 0:5952bbaff1c6 | 68 | // width is signed value, negative for left adjustment. |
| davidprentice | 0:5952bbaff1c6 | 69 | // Range -128,127 |
| davidprentice | 0:5952bbaff1c6 | 70 | char fmt[129] = ""; |
| davidprentice | 0:5952bbaff1c6 | 71 | unsigned int w = width; |
| davidprentice | 0:5952bbaff1c6 | 72 | if (width < 0) { |
| davidprentice | 0:5952bbaff1c6 | 73 | negative = 1; |
| davidprentice | 0:5952bbaff1c6 | 74 | w = -width; |
| davidprentice | 0:5952bbaff1c6 | 75 | } else { |
| davidprentice | 0:5952bbaff1c6 | 76 | negative = 0; |
| davidprentice | 0:5952bbaff1c6 | 77 | } |
| davidprentice | 0:5952bbaff1c6 | 78 | |
| davidprentice | 0:5952bbaff1c6 | 79 | if (strlen(sout) < w) { |
| davidprentice | 0:5952bbaff1c6 | 80 | memset(fmt, ' ', 128); |
| davidprentice | 0:5952bbaff1c6 | 81 | fmt[w - strlen(sout)] = '\0'; |
| davidprentice | 0:5952bbaff1c6 | 82 | if (negative == 0) { |
| davidprentice | 0:5952bbaff1c6 | 83 | char *tmp = malloc(strlen(sout) + 1); |
| davidprentice | 0:5952bbaff1c6 | 84 | strcpy(tmp, sout); |
| davidprentice | 0:5952bbaff1c6 | 85 | strcpy(sout, fmt); |
| davidprentice | 0:5952bbaff1c6 | 86 | strcat(sout, tmp); |
| davidprentice | 0:5952bbaff1c6 | 87 | free(tmp); |
| davidprentice | 0:5952bbaff1c6 | 88 | } else { |
| davidprentice | 0:5952bbaff1c6 | 89 | // left adjustment |
| davidprentice | 0:5952bbaff1c6 | 90 | strcat(sout, fmt); |
| davidprentice | 0:5952bbaff1c6 | 91 | } |
| davidprentice | 0:5952bbaff1c6 | 92 | } |
| davidprentice | 0:5952bbaff1c6 | 93 | |
| davidprentice | 0:5952bbaff1c6 | 94 | return sout; |
| davidprentice | 0:5952bbaff1c6 | 95 | } |
| davidprentice | 0:5952bbaff1c6 | 96 |