printf.c 4.04 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * 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.
 */

27
28
29
30
#include <stdint.h>
#include <string.h>
#include <stdarg.h>

31
#include "mpconfig.h"
32
33
34
35
36
#include "std.h"
#include "misc.h"
#include "systick.h"
#include "qstr.h"
#include "obj.h"
Dave Hylands's avatar
Dave Hylands committed
37
#include "pfenv.h"
38
39
40
#if 0
#include "lcd.h"
#endif
Damien George's avatar
Damien George committed
41
#include "uart.h"
42
43
#include "usb.h"

44
#if MICROPY_PY_BUILTINS_FLOAT
45
46
47
#include "formatfloat.h"
#endif

48
49
int pfenv_vprintf(const pfenv_t *pfenv, const char *fmt, va_list args);

50
51
52
53
void pfenv_prints(const pfenv_t *pfenv, const char *str) {
    pfenv->print_strn(pfenv->data, str, strlen(str));
}

54
STATIC void stdout_print_strn(void *data, const char *str, unsigned int len) {
55
56
    // TODO this needs to be replaced with a proper stdio interface ala CPython
    // send stdout to UART and USB CDC VCP
Damien George's avatar
Damien George committed
57
58
    if (pyb_uart_global_debug != PYB_UART_NONE) {
        uart_tx_strn_cooked(pyb_uart_global_debug, str, len);
59
60
61
62
63
64
65
66
67
68
69
    }
    if (usb_vcp_is_enabled()) {
        usb_vcp_send_strn_cooked(str, len);
    }
}

static const pfenv_t pfenv_stdout = {0, stdout_print_strn};

int printf(const char *fmt, ...) {
    va_list ap;
    va_start(ap, fmt);
70
    int ret = pfenv_vprintf(&pfenv_stdout, fmt, ap);
71
72
73
74
75
    va_end(ap);
    return ret;
}

int vprintf(const char *fmt, va_list ap) {
76
    return pfenv_vprintf(&pfenv_stdout, fmt, ap);
77
78
79
80
81
82
83
}

#if MICROPY_DEBUG_PRINTERS
int DEBUG_printf(const char *fmt, ...) {
    (void)stream;
    va_list ap;
    va_start(ap, fmt);
84
    int ret = pfenv_vprintf(&pfenv_stdout, fmt, ap);
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
    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;

void strn_print_strn(void *data, const char *str, unsigned int len) {
    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;
}
119

120
121
122
123
124
125
126
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;
127
    int len = pfenv_vprintf(&pfenv, fmt, ap);
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    // 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;
}