Skip to content

Commit

Permalink
Added string conversion functions for ff16, ff32 and updated console …
Browse files Browse the repository at this point in the history
…module description (Stephane-D#396)

* Updated the description of memory consumption by the Console module

* Added functions fastFix16ToStr, fastFix32ToStr, FF16_toStr, FF32_toStr
  • Loading branch information
werton authored Mar 5, 2025
1 parent 882ebe9 commit a9837ff
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
2 changes: 1 addition & 1 deletion inc/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
/**
* \brief
* Set it to 1 if you want to enable the TTY text console module (written by Andreas Dietrich).<br>
* It consume about 34 bytes of memory when enabled.
* It consume about 28 bytes of memory when enabled.
*/
#define MODULE_CONSOLE 1

Expand Down
40 changes: 40 additions & 0 deletions inc/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,34 @@ void fix16ToStr(fix16 value, char *str, u16 numdec);
* Converts the specified fix32 value to string.<br>
*/
void fix32ToStr(fix32 value, char *str, u16 numdec);
/**
* \brief
* Convert a fastfix16 value to string.
*
* \param value
* The fastfix16 value to convert to string.
* \param str
* Destination string (it must be large enough to receive result).
* \param numdec
* Number of wanted decimal.
*
* Converts the specified fastfix16 value to string.<br>
*/
void fastFix16ToStr(fastfix16 value, char *str, u16 numdec);
/**
* \brief
* Convert a fastfix32 value to string.
*
* \param value
* The fastfix32 value to convert to string.
* \param str
* Destination string (it must be large enough to receive result).
* \param numdec
* Number of wanted decimal.
*
* Converts the specified fastfix32 value to string.<br>
*/
void fastFix32ToStr(fastfix32 value, char *str, u16 numdec);
/**
* \brief
* Same as fix16ToStr(..)
Expand All @@ -290,6 +318,18 @@ void F16_toStr(fix16 value, char *str, u16 numdec);
* \see fix32ToStr
*/
void F32_toStr(fix32 value, char *str, u16 numdec);
/**
* \brief
* Same as fastFix16ToStr(..)
* \see fastFix16ToStr
*/
void FF16_toStr(fastfix16 value, char *str, u16 numdec);
/**
* \brief
* Same as fastFix32ToStr(..)
* \see fastFix32ToStr
*/
void FF32_toStr(fastfix32 value, char *str, u16 numdec);

#endif // _STRING_H_

67 changes: 67 additions & 0 deletions src/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,64 @@ void fix32ToStr(fix32 value, char *str, u16 numdec)
else dst[numdec] = 0;
}

void fastFix16ToStr(fastfix16 value, char *str, u16 numdec)
{
char *dst = str;
fastfix16 v = value;

if (v < 0)
{
v = -v;
*dst++ = '-';
}

dst += uintToStr(FF16_toInt(v), dst, 1);
*dst++ = '.';

// get fractional part
const u16 frac = (((u16) FF16_frac(v)) * (u16) 1000) / ((u16) 1 << FASTFIX16_FRAC_BITS);
u16 len = uint16ToStr(frac, dst, 3);

if (len < numdec)
{
// need to add ending '0'
dst += len;
while(len++ < numdec) *dst++ = '0';
// mark end here
*dst = 0;
}
else dst[numdec] = 0;
}

void fastFix32ToStr(fastfix32 value, char *str, u16 numdec)
{
char *dst = str;
fastfix32 v = value;

if (v < 0)
{
v = -v;
*dst++ = '-';
}

dst += uintToStr(FF32_toInt(v), dst, 1);
*dst++ = '.';

// get fractional part
const u16 frac = (((u16) FF32_frac(v)) * (u16) 1000) / ((u16) 1 << FASTFIX32_FRAC_BITS);
u16 len = uint16ToStr(frac, dst, 3);

if (len < numdec)
{
// need to add ending '0'
dst += len;
while(len++ < numdec) *dst++ = '0';
// mark end here
*dst = 0;
}
else dst[numdec] = 0;
}

void F16_toStr(fix16 value, char *str, u16 numdec)
{
fix16ToStr(value, str, numdec);
Expand All @@ -344,6 +402,15 @@ void F32_toStr(fix32 value, char *str, u16 numdec)
fix32ToStr(value, str, numdec);
}

void FF16_toStr(fastfix16 value, char *str, u16 numdec)
{
fastFix16ToStr(value, str, numdec);
}

void FF32_toStr(fastfix32 value, char *str, u16 numdec)
{
fastFix32ToStr(value, str, numdec);
}

static u16 digits10(const u16 v)
{
Expand Down

0 comments on commit a9837ff

Please sign in to comment.