Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 Damien P. George |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
| 26 | |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 27 | #include <stdint.h> |
| 28 | #include <string.h> |
| 29 | #include <stdarg.h> |
| 30 | |
Paul Sokolovsky | 8139494 | 2014-06-28 23:32:03 +0300 | [diff] [blame] | 31 | #include "mpconfig.h" |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 32 | #include "std.h" |
| 33 | #include "misc.h" |
| 34 | #include "systick.h" |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 35 | #include "qstr.h" |
| 36 | #include "obj.h" |
Dave Hylands | baf6f14 | 2014-03-30 21:06:50 -0700 | [diff] [blame] | 37 | #include "pfenv.h" |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 38 | #if 0 |
| 39 | #include "lcd.h" |
| 40 | #endif |
Damien George | 7533700 | 2014-04-21 12:03:09 +0100 | [diff] [blame] | 41 | #include "uart.h" |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 42 | #include "usb.h" |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 43 | |
Damien George | fb510b3 | 2014-06-01 13:32:54 +0100 | [diff] [blame] | 44 | #if MICROPY_PY_BUILTINS_FLOAT |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 45 | #include "formatfloat.h" |
| 46 | #endif |
| 47 | |
Paul Sokolovsky | cb66f41 | 2014-07-13 23:07:42 +0300 | [diff] [blame^] | 48 | int pfenv_vprintf(const pfenv_t *pfenv, const char *fmt, va_list args); |
| 49 | |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 50 | void pfenv_prints(const pfenv_t *pfenv, const char *str) { |
| 51 | pfenv->print_strn(pfenv->data, str, strlen(str)); |
| 52 | } |
| 53 | |
Damien George | e79c669 | 2014-06-15 09:10:07 +0100 | [diff] [blame] | 54 | STATIC void stdout_print_strn(void *data, const char *str, unsigned int len) { |
Damien George | 34ab8dd | 2014-06-15 00:41:47 +0100 | [diff] [blame] | 55 | // TODO this needs to be replaced with a proper stdio interface ala CPython |
| 56 | // send stdout to UART and USB CDC VCP |
Damien George | 7533700 | 2014-04-21 12:03:09 +0100 | [diff] [blame] | 57 | if (pyb_uart_global_debug != PYB_UART_NONE) { |
| 58 | uart_tx_strn_cooked(pyb_uart_global_debug, str, len); |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 59 | } |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 60 | if (usb_vcp_is_enabled()) { |
| 61 | usb_vcp_send_strn_cooked(str, len); |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
| 65 | static const pfenv_t pfenv_stdout = {0, stdout_print_strn}; |
| 66 | |
| 67 | int printf(const char *fmt, ...) { |
| 68 | va_list ap; |
| 69 | va_start(ap, fmt); |
Paul Sokolovsky | cb66f41 | 2014-07-13 23:07:42 +0300 | [diff] [blame^] | 70 | int ret = pfenv_vprintf(&pfenv_stdout, fmt, ap); |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 71 | va_end(ap); |
| 72 | return ret; |
| 73 | } |
| 74 | |
| 75 | int vprintf(const char *fmt, va_list ap) { |
Paul Sokolovsky | cb66f41 | 2014-07-13 23:07:42 +0300 | [diff] [blame^] | 76 | return pfenv_vprintf(&pfenv_stdout, fmt, ap); |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | #if MICROPY_DEBUG_PRINTERS |
| 80 | int DEBUG_printf(const char *fmt, ...) { |
| 81 | (void)stream; |
| 82 | va_list ap; |
| 83 | va_start(ap, fmt); |
| 84 | int ret = pfenv_printf(&pfenv_stdout, fmt, ap); |
| 85 | va_end(ap); |
| 86 | return ret; |
| 87 | } |
| 88 | #endif |
| 89 | |
| 90 | // need this because gcc optimises printf("%c", c) -> putchar(c), and printf("a") -> putchar('a') |
| 91 | int putchar(int c) { |
| 92 | char chr = c; |
| 93 | stdout_print_strn(0, &chr, 1); |
| 94 | return chr; |
| 95 | } |
| 96 | |
| 97 | // need this because gcc optimises printf("string\n") -> puts("string") |
| 98 | int puts(const char *s) { |
| 99 | stdout_print_strn(0, s, strlen(s)); |
| 100 | char chr = '\n'; |
| 101 | stdout_print_strn(0, &chr, 1); |
| 102 | return 1; |
| 103 | } |
| 104 | |
| 105 | typedef struct _strn_pfenv_t { |
| 106 | char *cur; |
| 107 | size_t remain; |
| 108 | } strn_pfenv_t; |
| 109 | |
| 110 | void strn_print_strn(void *data, const char *str, unsigned int len) { |
| 111 | strn_pfenv_t *strn_pfenv = data; |
| 112 | if (len > strn_pfenv->remain) { |
| 113 | len = strn_pfenv->remain; |
| 114 | } |
| 115 | memcpy(strn_pfenv->cur, str, len); |
| 116 | strn_pfenv->cur += len; |
| 117 | strn_pfenv->remain -= len; |
| 118 | } |
Damien George | d0f9f6c | 2014-04-17 18:58:09 +0100 | [diff] [blame] | 119 | |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 120 | int vsnprintf(char *str, size_t size, const char *fmt, va_list ap) { |
| 121 | strn_pfenv_t strn_pfenv; |
| 122 | strn_pfenv.cur = str; |
| 123 | strn_pfenv.remain = size; |
| 124 | pfenv_t pfenv; |
| 125 | pfenv.data = &strn_pfenv; |
| 126 | pfenv.print_strn = strn_print_strn; |
| 127 | int len = pfenv_printf(&pfenv, fmt, ap); |
| 128 | // add terminating null byte |
| 129 | if (size > 0) { |
| 130 | if (strn_pfenv.remain == 0) { |
| 131 | strn_pfenv.cur[-1] = 0; |
| 132 | } else { |
| 133 | strn_pfenv.cur[0] = 0; |
| 134 | } |
| 135 | } |
| 136 | return len; |
| 137 | } |
| 138 | |
| 139 | int snprintf(char *str, size_t size, const char *fmt, ...) { |
| 140 | va_list ap; |
| 141 | va_start(ap, fmt); |
| 142 | int ret = vsnprintf(str, size, fmt, ap); |
| 143 | va_end(ap); |
| 144 | return ret; |
| 145 | } |