Takashi Kawai
/
Nucleo_blink_led_for_mrubyc
This is a port of the mruby/c tutorial Chapter 03 to the mbed environment.
Embed:
(wiki syntax)
Show/hide line numbers
console.c
Go to the documentation of this file.
00001 /*! @file 00002 @brief 00003 console I/O module. 00004 00005 <pre> 00006 Copyright (C) 2015 Kyushu Institute of Technology. 00007 Copyright (C) 2015 Shimane IT Open-Innovation Center. 00008 00009 This file is distributed under BSD 3-Clause License. 00010 00011 </pre> 00012 */ 00013 00014 #include <stdarg.h> 00015 #include <string.h> 00016 #include <stdint.h> 00017 #include "hal/hal.h" 00018 #include "console.h " 00019 00020 00021 //================================================================ 00022 /*! output string with format 00023 00024 @param value output value 00025 @param align left(-1) or right(1) 00026 @param w width 00027 @param base n base 00028 @param pad padding character 00029 */ 00030 static void format_output_str (const char *value, int align, int w, char pad) 00031 { 00032 if( !value ) return; 00033 00034 int len = strlen(value); 00035 int n_pad = w - len; 00036 00037 if( align == 1 ) { 00038 while( n_pad-- > 0 ) { 00039 console_putchar (pad); 00040 } 00041 } 00042 hal_write(1, value, len); 00043 while( n_pad-- > 0 ) { 00044 console_putchar (pad); 00045 } 00046 } 00047 00048 00049 //================================================================ 00050 /*! output int value with format 00051 00052 @param value output value 00053 @param align left(-1) or right(1) 00054 @param w width 00055 @param base n base 00056 @param pad padding character 00057 */ 00058 static void format_output_int (int32_t value, int align, int w, int base, char pad) 00059 { 00060 char buf[21]; 00061 int sign = 0; 00062 int idx = sizeof(buf); 00063 buf[--idx] = 0; 00064 00065 if( value < 0 ) { 00066 sign = -1; 00067 value = -value; 00068 } 00069 00070 do { 00071 buf[--idx] = "0123456789ABCDEF"[value % base]; 00072 value /= base; 00073 } while( value != 0 && idx != 0 ); 00074 00075 if( sign < 0 && align > 0 && pad == '0' ) { 00076 console_putchar ('-'); // when "%08d",-12345 then "-0012345" 00077 w--; 00078 } else if( sign < 0 && idx != 0 ) { 00079 buf[--idx] = '-'; 00080 } 00081 00082 format_output_str (buf + idx, align, w, pad); 00083 } 00084 00085 00086 //================================================================ 00087 /*! output unsigned int value with format 00088 00089 @param value output value 00090 @param align left(-1) or right(1) 00091 @param w width 00092 @param base n base 00093 @param pad padding character 00094 */ 00095 static void format_output_uint (uint32_t value, int align, int w, int base, char pad) 00096 { 00097 char buf[21]; 00098 int idx = sizeof(buf); 00099 buf[--idx] = 0; 00100 00101 do { 00102 buf[--idx] = "0123456789ABCDEF"[value % base]; 00103 value /= base; 00104 } while( value != 0 && idx != 0 ); 00105 00106 format_output_str (buf + idx, align, w, pad); 00107 } 00108 00109 00110 //================================================================ 00111 /*! output a character 00112 00113 @param c character 00114 */ 00115 void console_putchar (char c) 00116 { 00117 hal_write(1, &c, 1); 00118 } 00119 00120 00121 //================================================================ 00122 /*! output string 00123 00124 @param str str 00125 */ 00126 void console_print (const char *str) 00127 { 00128 hal_write(1, str, strlen(str)); 00129 } 00130 00131 00132 //================================================================ 00133 /*! output formatted string 00134 00135 @param fmt format string. 00136 @note 00137 */ 00138 void console_printf (const char *fmt, ...) 00139 { 00140 va_list params; 00141 va_start(params, fmt); 00142 00143 int c; 00144 while((c = *fmt++)) { 00145 if( c != '%' ) { 00146 console_putchar (c); 00147 continue; 00148 } 00149 00150 int align = 1; // left(-1) or right(1) 00151 char pad = ' '; // padding 00152 int w = 0; // width 00153 while( 1 ) { 00154 switch( (c = *fmt++) ) { 00155 case '-': 00156 align = -1; 00157 break; 00158 00159 case '0': 00160 if( pad == ' ' ) { 00161 pad = '0'; 00162 break; 00163 } 00164 // fall through. 00165 00166 case '1': case '2': case '3': case '4': case '5': 00167 case '6': case '7': case '8': case '9': 00168 w = w * 10 + (c - '0'); 00169 break; 00170 00171 case '\0': 00172 goto L_return; 00173 00174 default: 00175 goto L_exit; 00176 } 00177 } 00178 00179 L_exit: 00180 switch(c) { 00181 case 's': 00182 format_output_str (va_arg(params, char *), align, w, pad); 00183 break; 00184 00185 case 'd': 00186 case 'i': 00187 format_output_int (va_arg(params, int), align, w, 10, pad); 00188 break; 00189 00190 case 'u': 00191 format_output_uint (va_arg(params, unsigned int), align, w, 10, pad); 00192 break; 00193 00194 case 'X': 00195 case 'x': 00196 format_output_uint (va_arg(params, unsigned int), align, w, 16, pad); 00197 break; 00198 00199 case 'c': 00200 console_putchar (va_arg(params, int)); // ignore "%03c" and others. 00201 break; 00202 00203 default: 00204 console_putchar (c); 00205 } 00206 } 00207 00208 L_return: 00209 va_end(params); 00210 } 00211
Generated on Tue Jul 12 2022 23:36:30 by 1.7.2