Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mprintf: reduce include overhead\nproper declare symbols with C linka… #791

Merged
merged 2 commits into from
Nov 5, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions Sming/system/include/m_printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@ License: MIT
Date: 21.12.2015
Descr: embedded very simple version of printf with float support
*/

#ifndef _M_PRINTF_
#define _M_PRINTF_

#include <stdarg.h>

#ifdef __cplusplus
extern "C" {
#endif

extern int m_vsnprintf(char *buf, size_t maxLen, const char *fmt, va_list args);
extern int m_snprintf(char* buf, int length, const char *fmt, ...);
extern int m_printf(const char *fmt, ...);
extern int m_printf(char const*, ...);
extern int m_vprintf ( const char * format, va_list arg );
extern void m_putc(char c);

#ifdef __cplusplus
}
#endif

#endif /*_M_PRINTF_*/
#endif /*_M_PRINTF_*/
2 changes: 2 additions & 0 deletions Sming/system/include/stringconversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extern "C" {

int atoi(const char *nptr); // Already implemented

extern char* ltoa_wp(long val, char* buffer, int base, int width, char pad);
extern char* ltoa_w (long, char*, int, int width);
extern char* ltoa (long, char*, int);

Expand All @@ -22,6 +23,7 @@ extern char* ultoa_w(unsigned long val, char* buffer, unsigned int base, int wid
extern char* ultoa(unsigned long val, char* buffer, unsigned int base);

#define itoa ltoa
extern char *dtostrf_p(double floatVar, int minStringWidthIncDecimalPoint, int numDigitsAfterDecimal, char *outputBuffer, char pad);
extern char *dtostrf(double floatVar, int minStringWidthIncDecimalPoint, int numDigitsAfterDecimal, char *outputBuffer);
long atol(const char *nptr);
extern long os_strtol(const char* str, char** endptr, int base);
Expand Down
63 changes: 42 additions & 21 deletions Sming/system/m_printf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Descr: embedded very simple version of printf with float support
*/

#include <stdarg.h>
#include "../../SmingCore/SmingCore.h"
//#include "../../SmingCore/SmingCore.h"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ADiea If you want to use only osapi.h then please remove the commented include above.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, good to see activity on sming again!

#include "osapi.h"

#define MPRINTF_BUF_SIZE 256

Expand All @@ -32,6 +33,12 @@ void setMPrintfPrinterCbc(void (*callback)(char))
cbc_printchar = callback;
}

void m_putc(char c)
{
if (cbc_printchar)
cbc_printchar(c);
}

/**
* @fn int m_snprintf(char* buf, int length, const char *fmt, ...);
*
Expand All @@ -54,30 +61,20 @@ int m_snprintf(char* buf, int length, const char *fmt, ...)
return n;
}

/**
* @fn int m_printf(const char *fmt, ...);
*
* @param fmt - printf compatible format string
*
* @retval int - number of characters written to console
*/
int m_printf(const char *fmt, ...)
int m_vprintf ( const char * format, va_list arg )
{
if(!cbc_printchar)
{
return 0;
}

char buf[MPRINTF_BUF_SIZE], *p;
va_list args;
int n = 0;

va_start(args, fmt);
m_vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
int n = 0;
m_vsnprintf(buf, sizeof(buf), format, arg);

p = buf;
while (*p)
while (p && n < sizeof(buf) && *p)
{
cbc_printchar(*p);
n++;
Expand All @@ -87,6 +84,30 @@ int m_printf(const char *fmt, ...)
return n;
}

/**
* @fn int m_printf(const char *fmt, ...);
*
* @param fmt - printf compatible format string
*
* @retval int - number of characters written to console
*/
int m_printf(const char* fmt, ...)
{
int n=0;

if(!fmt)
return 0;

va_list args;
va_start(args, fmt);

n = m_vprintf(fmt, args);

va_end(args);

return n;
}

int m_vsnprintf(char *buf, size_t maxLen, const char *fmt, va_list args)
{
int i, base, flags;
Expand All @@ -95,7 +116,7 @@ int m_vsnprintf(char *buf, size_t maxLen, const char *fmt, va_list args)
int8_t precision, width;
char pad;

char tempNum[24];
char tempNum[40];

for (str = buf; *fmt; fmt++)
{
Expand Down Expand Up @@ -176,7 +197,7 @@ int m_vsnprintf(char *buf, size_t maxLen, const char *fmt, va_list args)

case 'p':
s = ultoa((unsigned long) va_arg(args, void *), tempNum, 16);
while (*s)
while (*s && (maxLen - (uint32_t)(str - buf) > OVERFLOW_GUARD))
*str++ = *s++;
continue;

Expand All @@ -197,8 +218,8 @@ int m_vsnprintf(char *buf, size_t maxLen, const char *fmt, va_list args)

case 'f':

s = dtostrf(va_arg(args, double), width, precision, tempNum);
while (*s)
s = dtostrf_p(va_arg(args, double), width, precision, tempNum, pad);
while (*s && (maxLen - (uint32_t)(str - buf) > OVERFLOW_GUARD))
*str++ = *s++;
continue;

Expand All @@ -213,11 +234,11 @@ int m_vsnprintf(char *buf, size_t maxLen, const char *fmt, va_list args)
}

if (flags & SIGN)
s = ltoa_w(va_arg(args, int), tempNum, base, width);
s = ltoa_wp(va_arg(args, int), tempNum, base, width, pad);
else
s = ultoa_wp(va_arg(args, unsigned int), tempNum, base, width, pad);

while (*s)
while (*s && (maxLen - (uint32_t)(str - buf) > OVERFLOW_GUARD))
*str++ = *s++;
}

Expand Down
27 changes: 19 additions & 8 deletions Sming/system/stringconversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ char* ltoa(long val, char* buffer, int base)

char* ltoa_w(long val, char* buffer, int base, int width)
{
int i = 34, p = 0;
char buf[36] = {0};
return ltoa_wp(val, buffer, base, width, ' ');
}

char* ltoa_wp(long val, char* buffer, int base, int width, char pad)
{
int i = 38, p = 0;
char buf[40] = {0};
bool ngt = val < 0;
if (ngt) val = -val;

Expand All @@ -29,8 +34,9 @@ char* ltoa_w(long val, char* buffer, int base, int width)
width -= strlen(&buf[i+1]);
if(width > 0)
{
memset(buffer, ' ', width);
memset(buffer, pad, width);
}
else width = 0;
}

strcpy(buffer + width, &buf[i+1]);
Expand All @@ -50,8 +56,8 @@ char* ultoa_w(unsigned long val, char* buffer, unsigned int base, int width)
}
char* ultoa_wp(unsigned long val, char* buffer, unsigned int base, int width, char pad)
{
int i = 34, p = 0;
char buf[36] = {0};
int i = 38, p = 0;
char buf[40] = {0};

for(; val && i ; --i, p++, val /= base)
buf[i] = "0123456789abcdef"[val % base];
Expand All @@ -64,17 +70,22 @@ char* ultoa_wp(unsigned long val, char* buffer, unsigned int base, int width, ch
{
memset(buffer, pad, width);
}
else width = 0;
}
strcpy(buffer + width, &buf[i+1]);

return buffer;
}

char *dtostrf(double floatVar, int minStringWidthIncDecimalPoint, int numDigitsAfterDecimal, char *outputBuffer)
{
return dtostrf_p(floatVar, minStringWidthIncDecimalPoint, numDigitsAfterDecimal, outputBuffer, ' ');
}
// Author zitron: http://forum.arduino.cc/index.php?topic=37391#msg276209
// modified by ADiea: remove dependencies strcat, floor, round; reorganize+speedup code
char *dtostrf(double floatVar, int minStringWidthIncDecimalPoint, int numDigitsAfterDecimal, char *outputBuffer)
extern char *dtostrf_p(double floatVar, int minStringWidthIncDecimalPoint, int numDigitsAfterDecimal, char *outputBuffer, char pad)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ADiea why you have extern here?!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm agree should be removed, no idea what was on my mind

{
char temp[24], num[24];
char temp[40], num[40];
unsigned long mult = 1, int_part;
int16_t i, processedFracLen = numDigitsAfterDecimal;

Expand Down Expand Up @@ -174,7 +185,7 @@ char *dtostrf(double floatVar, int minStringWidthIncDecimalPoint, int numDigitsA
i = minStringWidthIncDecimalPoint - strlen(num) + 1;
while (--i > 0)
{
*buf++ = ' ';
*buf++ = pad;
}

//Write output buffer
Expand Down