From 131c17dcf1e982ba24528de5f0958e05df0d0829 Mon Sep 17 00:00:00 2001 From: Mikiya Kobayashi Date: Mon, 26 Jun 2023 13:14:33 +0900 Subject: [PATCH] [added] RL78/G16_FPB related files for Arduino library. --- cores/rl78g16/dtostrf.c | 142 ++++++++++++++++++++++++++++++++++++++++ cores/rl78g16/itoa.c | 125 +++++++++++++++++++++++++++++++++++ 2 files changed, 267 insertions(+) create mode 100644 cores/rl78g16/dtostrf.c create mode 100644 cores/rl78g16/itoa.c diff --git a/cores/rl78g16/dtostrf.c b/cores/rl78g16/dtostrf.c new file mode 100644 index 0000000..efcd96d --- /dev/null +++ b/cores/rl78g16/dtostrf.c @@ -0,0 +1,142 @@ +//#include "api/deprecated-avr-comp/avr/dtostrf.c.impl" + +#if 1 +#include +#include +#include + +static size_t printNumber(unsigned long n, uint8_t base, char *inputbuff) +{ + char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte. + char *str = &buf[sizeof(buf) - 1]; + + *str = '\0'; + + // prevent crash if called with base == 1 + if (base < 2) + { + base = 10; + } + + do + { + char c = (char)(n % base); + n /= base; + + *--str = c < 10 ? c + '0' : c + 'A' - 10; + } while(n); + + strcpy(inputbuff,str); + + return strlen(str); +} + + +char *dtostrf (double val, signed char width, unsigned char prec, char *sout) +{ + char fmt[20]; + double number = val; + unsigned char digits = prec; + signed short n; + signed short width_local = width; + memset(fmt, 0, sizeof(fmt)); + + n = 0; + + if (isnan(number)) + { + strcpy(fmt, "nan"); + strcpy(sout, fmt); + return sout; + } + if (isinf(number)) + { + strcpy(fmt, "inf"); + strcpy(sout, fmt); + return sout; + } + if (number > 4294967040.0) + { + strcpy(fmt, "ovf"); + strcpy(sout, fmt); + return sout; + } + if (number <-4294967040.0) + { + strcpy(fmt, "ovf"); + strcpy(sout, fmt); + return sout; + } + + // Handle negative numbers + if (number < 0.0) + { + // n += print('-'); + fmt[n] = '-'; + n++; + number = -number; + } + + + // Round correctly so that print(1.999, 2) prints as "2.00" + double rounding = 0.5; + for (uint8_t i=0; i 0) { + fmt[n] = '.'; + n++; +// n += print("."); + } + + // Extract digits from the remainder one at a time + while (digits-- > 0) + { + remainder *= 10.0; + unsigned int toPrint = (unsigned int)remainder; +// n += print(toPrint); + n+= printNumber((unsigned long)toPrint, 10, &fmt[n]); + remainder -= toPrint; + } + + if(width_local < 0) + { + /* left */ + width_local = -width_local; + if(n <= width_local) + { + n = width_local; + } + strncpy((char *)sout, (const char *)fmt, (size_t)n); + *(sout+n) = '\0'; + } + else + { + /* right */ + signed short offset = 0; + memset((void *)sout, ' ', (size_t)width_local); + if(n <= width_local) + { + offset = width_local - n; + strncpy((char *)sout+offset, (const char *)fmt, (size_t)n); + *(sout+n+offset) = '\0'; + } + else + { + strncpy((char *)sout, (const char *)fmt, (size_t)n); + *(sout+n) = '\0'; + } + } + return sout; +} +#endif diff --git a/cores/rl78g16/itoa.c b/cores/rl78g16/itoa.c new file mode 100644 index 0000000..fb468dc --- /dev/null +++ b/cores/rl78g16/itoa.c @@ -0,0 +1,125 @@ +/* + Copyright (c) 2014 Arduino LLC. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +char* ltoa( long value, char *string, int radix ) +{ + char tmp[33]; + char *tp = tmp; + long i; + unsigned long v; + int sign; + char *sp; + + if ( string == NULL ) + { + return 0 ; + } + + if (radix > 36 || radix <= 1) + { + return 0 ; + } + + sign = (radix == 10 && value < 0); + if (sign) + { + v = -value; + } + else + { + v = (unsigned long)value; + } + + while (v || tp == tmp) + { + i = v % radix; + v = v / radix; + if (i < 10) + *tp++ = i+'0'; + else + *tp++ = i + 'a' - 10; + } + + sp = string; + + if (sign) + *sp++ = '-'; + while (tp > tmp) + *sp++ = *--tp; + *sp = 0; + + return string; +} + +char* ultoa( unsigned long value, char *string, int radix ) +{ + char tmp[33]; + char *tp = tmp; + long i; + unsigned long v = value; + char *sp; + + if ( string == NULL ) + { + return 0; + } + + if (radix > 36 || radix <= 1) + { + return 0; + } + + while (v || tp == tmp) + { + i = v % radix; + v = v / radix; + if (i < 10) + *tp++ = i+'0'; + else + *tp++ = i + 'a' - 10; + } + + sp = string; + + + while (tp > tmp) + *sp++ = *--tp; + *sp = 0; + + return string; +} + +char* itoa( int value, char *string, int radix ) +{ + return ltoa( value, string, radix ) ; +} + +char* utoa( unsigned int value, char *string, int radix ) +{ + return ultoa( value, string, radix ) ; +} + +#ifdef __cplusplus +} // extern "C" +#endif