2014-05-03 18:27:38 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-03-12 21:06:26 -04:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2014-06-28 16:32:03 -04:00
|
|
|
#include "mpconfig.h"
|
2014-03-12 21:06:26 -04:00
|
|
|
#include "std.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "systick.h"
|
|
|
|
#include "qstr.h"
|
|
|
|
#include "obj.h"
|
2014-03-31 00:06:50 -04:00
|
|
|
#include "pfenv.h"
|
2014-03-12 21:06:26 -04:00
|
|
|
#if 0
|
|
|
|
#include "lcd.h"
|
|
|
|
#endif
|
2014-04-21 07:03:09 -04:00
|
|
|
#include "uart.h"
|
2014-03-12 21:06:26 -04:00
|
|
|
#include "usb.h"
|
2014-07-20 08:57:43 -04:00
|
|
|
#include "pybstdio.h"
|
2014-03-12 21:06:26 -04:00
|
|
|
|
2014-06-01 08:32:54 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-03-12 21:06:26 -04:00
|
|
|
#include "formatfloat.h"
|
|
|
|
#endif
|
|
|
|
|
2014-07-13 16:07:42 -04:00
|
|
|
int pfenv_vprintf(const pfenv_t *pfenv, const char *fmt, va_list args);
|
|
|
|
|
2014-10-05 16:51:54 -04:00
|
|
|
STATIC void stdout_print_strn(void *dummy_env, const char *str, mp_uint_t len) {
|
2014-07-20 08:57:43 -04:00
|
|
|
stdout_tx_strn_cooked(str, len);
|
2014-03-12 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
2014-07-20 08:57:43 -04:00
|
|
|
STATIC const pfenv_t pfenv_stdout = {0, stdout_print_strn};
|
2014-03-12 21:06:26 -04:00
|
|
|
|
|
|
|
int printf(const char *fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2014-07-13 16:07:42 -04:00
|
|
|
int ret = pfenv_vprintf(&pfenv_stdout, fmt, ap);
|
2014-03-12 21:06:26 -04:00
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int vprintf(const char *fmt, va_list ap) {
|
2014-07-13 16:07:42 -04:00
|
|
|
return pfenv_vprintf(&pfenv_stdout, fmt, ap);
|
2014-03-12 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#if MICROPY_DEBUG_PRINTERS
|
2014-10-07 03:50:20 -04:00
|
|
|
mp_uint_t mp_verbose_flag = 1;
|
|
|
|
|
2014-03-12 21:06:26 -04:00
|
|
|
int DEBUG_printf(const char *fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2014-07-20 08:10:18 -04:00
|
|
|
int ret = pfenv_vprintf(&pfenv_stdout, fmt, ap);
|
2014-03-12 21:06:26 -04:00
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// need this because gcc optimises printf("%c", c) -> putchar(c), and printf("a") -> putchar('a')
|
|
|
|
int putchar(int c) {
|
|
|
|
char chr = c;
|
|
|
|
stdout_print_strn(0, &chr, 1);
|
|
|
|
return chr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// need this because gcc optimises printf("string\n") -> puts("string")
|
|
|
|
int puts(const char *s) {
|
|
|
|
stdout_print_strn(0, s, strlen(s));
|
|
|
|
char chr = '\n';
|
|
|
|
stdout_print_strn(0, &chr, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct _strn_pfenv_t {
|
|
|
|
char *cur;
|
|
|
|
size_t remain;
|
|
|
|
} strn_pfenv_t;
|
|
|
|
|
2014-10-05 16:51:54 -04:00
|
|
|
STATIC void strn_print_strn(void *data, const char *str, mp_uint_t len) {
|
2014-03-12 21:06:26 -04:00
|
|
|
strn_pfenv_t *strn_pfenv = data;
|
|
|
|
if (len > strn_pfenv->remain) {
|
|
|
|
len = strn_pfenv->remain;
|
|
|
|
}
|
|
|
|
memcpy(strn_pfenv->cur, str, len);
|
|
|
|
strn_pfenv->cur += len;
|
|
|
|
strn_pfenv->remain -= len;
|
|
|
|
}
|
2014-04-17 13:58:09 -04:00
|
|
|
|
2014-03-12 21:06:26 -04:00
|
|
|
int vsnprintf(char *str, size_t size, const char *fmt, va_list ap) {
|
|
|
|
strn_pfenv_t strn_pfenv;
|
|
|
|
strn_pfenv.cur = str;
|
|
|
|
strn_pfenv.remain = size;
|
|
|
|
pfenv_t pfenv;
|
|
|
|
pfenv.data = &strn_pfenv;
|
|
|
|
pfenv.print_strn = strn_print_strn;
|
2014-07-20 08:10:18 -04:00
|
|
|
int len = pfenv_vprintf(&pfenv, fmt, ap);
|
2014-03-12 21:06:26 -04:00
|
|
|
// add terminating null byte
|
|
|
|
if (size > 0) {
|
|
|
|
if (strn_pfenv.remain == 0) {
|
|
|
|
strn_pfenv.cur[-1] = 0;
|
|
|
|
} else {
|
|
|
|
strn_pfenv.cur[0] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int snprintf(char *str, size_t size, const char *fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
int ret = vsnprintf(str, size, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|