RobocupSSLのメイン基板白mbedのプログラム
Rootsロボット mainプログラム
~ Robocup SSL(小型車輪リーグ)ロボット ~
Robocup SSLとは
●試合構成
Robocup小型ロボットリーグ(Small Size League)は,直径180[mm],高さ150[mm]以内のサイズのロボット6台が1チームとなり,オレンジ色のゴルフボールを使ってサッカー競技を行う自立型ロボットコンテストである.
フィールドの上には2台のWebカメラが設置され,フィールド上のロボットとボールを撮影する.Visionサーバは,フィールドの画像データよりロボットとボールの座標データを算出し,LANを用い各チームのAI用PCに送信する.Webカメラの撮影速度は,60[fps]である.レフリーボックスは,ファウルやフリーキック,スローインなどの審判の判定を入力し,LANを通じて各チームのAI用PCに送信する.それぞれのチームのAI用PCは,ロボットとボールの座標,審判の判定を元にロボットの移動,キックなどの作戦を決定し,無線によってロボットに指令を送信する.
ロボット機能紹介
●オムニホイールによる方向転換不要の全方位移動
オムニホイールは,自由に回転可能なローラをホイールの外周上に配置した車輪である.ローラの回転により,車輪の回転と垂直の方向に駆動力を発することはできないが移動は可能となる.各車輪の角速度を調整することによって全方向への移動を可能にする.
●ドリブルバーのバックスピンによるボール保持
●電磁力を利用したキッカー
●キッカーの電磁力エネルギーを充電する充電回路
●ロボット情報が一目でわかるLCD
interface/i2c/xprintf/xprintf.cpp@35:b1b00cfaa163, 2019-09-22 (annotated)
- Committer:
- alt0710
- Date:
- Sun Sep 22 10:23:42 2019 +0000
- Revision:
- 35:b1b00cfaa163
- Parent:
- 1:c359b1fcfb5a
2019JapanOpenSoft
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alt0710 | 1:c359b1fcfb5a | 1 | /*------------------------------------------------------------------------/ |
alt0710 | 1:c359b1fcfb5a | 2 | / Universal string handler for user console interface |
alt0710 | 1:c359b1fcfb5a | 3 | /-------------------------------------------------------------------------/ |
alt0710 | 1:c359b1fcfb5a | 4 | / |
alt0710 | 1:c359b1fcfb5a | 5 | / Copyright (C) 2011, ChaN, all right reserved. |
alt0710 | 1:c359b1fcfb5a | 6 | / |
alt0710 | 1:c359b1fcfb5a | 7 | / * This software is a free software and there is NO WARRANTY. |
alt0710 | 1:c359b1fcfb5a | 8 | / * No restriction on use. You can use, modify and redistribute it for |
alt0710 | 1:c359b1fcfb5a | 9 | / personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY. |
alt0710 | 1:c359b1fcfb5a | 10 | / * Redistributions of source code must retain the above copyright notice. |
alt0710 | 1:c359b1fcfb5a | 11 | / |
alt0710 | 1:c359b1fcfb5a | 12 | /-------------------------------------------------------------------------*/ |
alt0710 | 1:c359b1fcfb5a | 13 | |
alt0710 | 1:c359b1fcfb5a | 14 | #include "xprintf.h" |
alt0710 | 1:c359b1fcfb5a | 15 | |
alt0710 | 1:c359b1fcfb5a | 16 | |
alt0710 | 1:c359b1fcfb5a | 17 | #if _USE_XFUNC_OUT |
alt0710 | 1:c359b1fcfb5a | 18 | #include <stdarg.h> |
alt0710 | 1:c359b1fcfb5a | 19 | void (*xfunc_out)(unsigned char); /* Pointer to the output stream */ |
alt0710 | 1:c359b1fcfb5a | 20 | static char *outptr; |
alt0710 | 1:c359b1fcfb5a | 21 | |
alt0710 | 1:c359b1fcfb5a | 22 | /*----------------------------------------------*/ |
alt0710 | 1:c359b1fcfb5a | 23 | /* Put a character */ |
alt0710 | 1:c359b1fcfb5a | 24 | /*----------------------------------------------*/ |
alt0710 | 1:c359b1fcfb5a | 25 | |
alt0710 | 1:c359b1fcfb5a | 26 | void xputc (char c) |
alt0710 | 1:c359b1fcfb5a | 27 | { |
alt0710 | 1:c359b1fcfb5a | 28 | if (_CR_CRLF && c == '\n') xputc('\r'); /* CR -> CRLF */ |
alt0710 | 1:c359b1fcfb5a | 29 | |
alt0710 | 1:c359b1fcfb5a | 30 | if (outptr) { |
alt0710 | 1:c359b1fcfb5a | 31 | *outptr++ = (unsigned char)c; |
alt0710 | 1:c359b1fcfb5a | 32 | return; |
alt0710 | 1:c359b1fcfb5a | 33 | } |
alt0710 | 1:c359b1fcfb5a | 34 | |
alt0710 | 1:c359b1fcfb5a | 35 | if (xfunc_out) xfunc_out((unsigned char)c); |
alt0710 | 1:c359b1fcfb5a | 36 | } |
alt0710 | 1:c359b1fcfb5a | 37 | |
alt0710 | 1:c359b1fcfb5a | 38 | |
alt0710 | 1:c359b1fcfb5a | 39 | |
alt0710 | 1:c359b1fcfb5a | 40 | /*----------------------------------------------*/ |
alt0710 | 1:c359b1fcfb5a | 41 | /* Put a null-terminated string */ |
alt0710 | 1:c359b1fcfb5a | 42 | /*----------------------------------------------*/ |
alt0710 | 1:c359b1fcfb5a | 43 | |
alt0710 | 1:c359b1fcfb5a | 44 | void xputs ( /* Put a string to the default device */ |
alt0710 | 1:c359b1fcfb5a | 45 | const char* str /* Pointer to the string */ |
alt0710 | 1:c359b1fcfb5a | 46 | ) |
alt0710 | 1:c359b1fcfb5a | 47 | { |
alt0710 | 1:c359b1fcfb5a | 48 | while (*str) |
alt0710 | 1:c359b1fcfb5a | 49 | xputc(*str++); |
alt0710 | 1:c359b1fcfb5a | 50 | } |
alt0710 | 1:c359b1fcfb5a | 51 | |
alt0710 | 1:c359b1fcfb5a | 52 | |
alt0710 | 1:c359b1fcfb5a | 53 | void xfputs ( /* Put a string to the specified device */ |
alt0710 | 1:c359b1fcfb5a | 54 | void(*func)(unsigned char), /* Pointer to the output function */ |
alt0710 | 1:c359b1fcfb5a | 55 | const char* str /* Pointer to the string */ |
alt0710 | 1:c359b1fcfb5a | 56 | ) |
alt0710 | 1:c359b1fcfb5a | 57 | { |
alt0710 | 1:c359b1fcfb5a | 58 | void (*pf)(unsigned char); |
alt0710 | 1:c359b1fcfb5a | 59 | |
alt0710 | 1:c359b1fcfb5a | 60 | |
alt0710 | 1:c359b1fcfb5a | 61 | pf = xfunc_out; /* Save current output device */ |
alt0710 | 1:c359b1fcfb5a | 62 | xfunc_out = func; /* Switch output to specified device */ |
alt0710 | 1:c359b1fcfb5a | 63 | while (*str) /* Put the string */ |
alt0710 | 1:c359b1fcfb5a | 64 | xputc(*str++); |
alt0710 | 1:c359b1fcfb5a | 65 | xfunc_out = pf; /* Restore output device */ |
alt0710 | 1:c359b1fcfb5a | 66 | } |
alt0710 | 1:c359b1fcfb5a | 67 | |
alt0710 | 1:c359b1fcfb5a | 68 | |
alt0710 | 1:c359b1fcfb5a | 69 | |
alt0710 | 1:c359b1fcfb5a | 70 | /*----------------------------------------------*/ |
alt0710 | 1:c359b1fcfb5a | 71 | /* Formatted string output */ |
alt0710 | 1:c359b1fcfb5a | 72 | /*----------------------------------------------*/ |
alt0710 | 1:c359b1fcfb5a | 73 | /* xprintf("%d", 1234); "1234" |
alt0710 | 1:c359b1fcfb5a | 74 | xprintf("%6d,%3d%%", -200, 5); " -200, 5%" |
alt0710 | 1:c359b1fcfb5a | 75 | xprintf("%-6u", 100); "100 " |
alt0710 | 1:c359b1fcfb5a | 76 | xprintf("%ld", 12345678L); "12345678" |
alt0710 | 1:c359b1fcfb5a | 77 | xprintf("%04x", 0xA3); "00a3" |
alt0710 | 1:c359b1fcfb5a | 78 | xprintf("%08LX", 0x123ABC); "00123ABC" |
alt0710 | 1:c359b1fcfb5a | 79 | xprintf("%016b", 0x550F); "0101010100001111" |
alt0710 | 1:c359b1fcfb5a | 80 | xprintf("%s", "String"); "String" |
alt0710 | 1:c359b1fcfb5a | 81 | xprintf("%-4s", "abc"); "abc " |
alt0710 | 1:c359b1fcfb5a | 82 | xprintf("%4s", "abc"); " abc" |
alt0710 | 1:c359b1fcfb5a | 83 | xprintf("%c", 'a'); "a" |
alt0710 | 1:c359b1fcfb5a | 84 | xprintf("%f", 10.0); <xprintf lacks floating point support> |
alt0710 | 1:c359b1fcfb5a | 85 | */ |
alt0710 | 1:c359b1fcfb5a | 86 | |
alt0710 | 1:c359b1fcfb5a | 87 | void xvprintf ( |
alt0710 | 1:c359b1fcfb5a | 88 | const char* fmt, /* Pointer to the format string */ |
alt0710 | 1:c359b1fcfb5a | 89 | va_list arp /* Pointer to arguments */ |
alt0710 | 1:c359b1fcfb5a | 90 | ) |
alt0710 | 1:c359b1fcfb5a | 91 | { |
alt0710 | 1:c359b1fcfb5a | 92 | unsigned int r, i, j, w, f; |
alt0710 | 1:c359b1fcfb5a | 93 | unsigned long v; |
alt0710 | 1:c359b1fcfb5a | 94 | char s[16], c, d, *p; |
alt0710 | 1:c359b1fcfb5a | 95 | |
alt0710 | 1:c359b1fcfb5a | 96 | |
alt0710 | 1:c359b1fcfb5a | 97 | for (;;) { |
alt0710 | 1:c359b1fcfb5a | 98 | c = *fmt++; /* Get a char */ |
alt0710 | 1:c359b1fcfb5a | 99 | if (!c) break; /* End of format? */ |
alt0710 | 1:c359b1fcfb5a | 100 | if (c != '%') { /* Pass through it if not a % sequense */ |
alt0710 | 1:c359b1fcfb5a | 101 | xputc(c); continue; |
alt0710 | 1:c359b1fcfb5a | 102 | } |
alt0710 | 1:c359b1fcfb5a | 103 | f = 0; |
alt0710 | 1:c359b1fcfb5a | 104 | c = *fmt++; /* Get first char of the sequense */ |
alt0710 | 1:c359b1fcfb5a | 105 | if (c == '0') { /* Flag: '0' padded */ |
alt0710 | 1:c359b1fcfb5a | 106 | f = 1; c = *fmt++; |
alt0710 | 1:c359b1fcfb5a | 107 | } else { |
alt0710 | 1:c359b1fcfb5a | 108 | if (c == '-') { /* Flag: left justified */ |
alt0710 | 1:c359b1fcfb5a | 109 | f = 2; c = *fmt++; |
alt0710 | 1:c359b1fcfb5a | 110 | } |
alt0710 | 1:c359b1fcfb5a | 111 | } |
alt0710 | 1:c359b1fcfb5a | 112 | for (w = 0; c >= '0' && c <= '9'; c = *fmt++) /* Minimum width */ |
alt0710 | 1:c359b1fcfb5a | 113 | w = w * 10 + c - '0'; |
alt0710 | 1:c359b1fcfb5a | 114 | if (c == 'l' || c == 'L') { /* Prefix: Size is long int */ |
alt0710 | 1:c359b1fcfb5a | 115 | f |= 4; c = *fmt++; |
alt0710 | 1:c359b1fcfb5a | 116 | } |
alt0710 | 1:c359b1fcfb5a | 117 | if (!c) break; /* End of format? */ |
alt0710 | 1:c359b1fcfb5a | 118 | d = c; |
alt0710 | 1:c359b1fcfb5a | 119 | if (d >= 'a') d -= 0x20; |
alt0710 | 1:c359b1fcfb5a | 120 | switch (d) { /* Type is... */ |
alt0710 | 1:c359b1fcfb5a | 121 | case 'S' : /* String */ |
alt0710 | 1:c359b1fcfb5a | 122 | p = va_arg(arp, char*); |
alt0710 | 1:c359b1fcfb5a | 123 | for (j = 0; p[j]; j++) ; |
alt0710 | 1:c359b1fcfb5a | 124 | while (!(f & 2) && j++ < w) xputc(' '); |
alt0710 | 1:c359b1fcfb5a | 125 | xputs(p); |
alt0710 | 1:c359b1fcfb5a | 126 | while (j++ < w) xputc(' '); |
alt0710 | 1:c359b1fcfb5a | 127 | continue; |
alt0710 | 1:c359b1fcfb5a | 128 | case 'C' : /* Character */ |
alt0710 | 1:c359b1fcfb5a | 129 | xputc((char)va_arg(arp, int)); continue; |
alt0710 | 1:c359b1fcfb5a | 130 | case 'B' : /* Binary */ |
alt0710 | 1:c359b1fcfb5a | 131 | r = 2; break; |
alt0710 | 1:c359b1fcfb5a | 132 | case 'O' : /* Octal */ |
alt0710 | 1:c359b1fcfb5a | 133 | r = 8; break; |
alt0710 | 1:c359b1fcfb5a | 134 | case 'D' : /* Signed decimal */ |
alt0710 | 1:c359b1fcfb5a | 135 | case 'U' : /* Unsigned decimal */ |
alt0710 | 1:c359b1fcfb5a | 136 | r = 10; break; |
alt0710 | 1:c359b1fcfb5a | 137 | case 'X' : /* Hexdecimal */ |
alt0710 | 1:c359b1fcfb5a | 138 | r = 16; break; |
alt0710 | 1:c359b1fcfb5a | 139 | default: /* Unknown type (passthrough) */ |
alt0710 | 1:c359b1fcfb5a | 140 | xputc(c); continue; |
alt0710 | 1:c359b1fcfb5a | 141 | } |
alt0710 | 1:c359b1fcfb5a | 142 | |
alt0710 | 1:c359b1fcfb5a | 143 | /* Get an argument and put it in numeral */ |
alt0710 | 1:c359b1fcfb5a | 144 | v = (f & 4) ? va_arg(arp, long) : ((d == 'D') ? (long)va_arg(arp, int) : (long)va_arg(arp, unsigned int)); |
alt0710 | 1:c359b1fcfb5a | 145 | if (d == 'D' && (v & 0x80000000)) { |
alt0710 | 1:c359b1fcfb5a | 146 | v = 0 - v; |
alt0710 | 1:c359b1fcfb5a | 147 | f |= 8; |
alt0710 | 1:c359b1fcfb5a | 148 | } |
alt0710 | 1:c359b1fcfb5a | 149 | i = 0; |
alt0710 | 1:c359b1fcfb5a | 150 | do { |
alt0710 | 1:c359b1fcfb5a | 151 | d = (char)(v % r); v /= r; |
alt0710 | 1:c359b1fcfb5a | 152 | if (d > 9) d += (c == 'x') ? 0x27 : 0x07; |
alt0710 | 1:c359b1fcfb5a | 153 | s[i++] = d + '0'; |
alt0710 | 1:c359b1fcfb5a | 154 | } while (v && i < sizeof(s)); |
alt0710 | 1:c359b1fcfb5a | 155 | if (f & 8) s[i++] = '-'; |
alt0710 | 1:c359b1fcfb5a | 156 | j = i; d = (f & 1) ? '0' : ' '; |
alt0710 | 1:c359b1fcfb5a | 157 | while (!(f & 2) && j++ < w) xputc(d); |
alt0710 | 1:c359b1fcfb5a | 158 | do xputc(s[--i]); while(i); |
alt0710 | 1:c359b1fcfb5a | 159 | while (j++ < w) xputc(' '); |
alt0710 | 1:c359b1fcfb5a | 160 | } |
alt0710 | 1:c359b1fcfb5a | 161 | } |
alt0710 | 1:c359b1fcfb5a | 162 | |
alt0710 | 1:c359b1fcfb5a | 163 | |
alt0710 | 1:c359b1fcfb5a | 164 | void xprintf ( /* Put a formatted string to the default device */ |
alt0710 | 1:c359b1fcfb5a | 165 | const char* fmt, /* Pointer to the format string */ |
alt0710 | 1:c359b1fcfb5a | 166 | ... /* Optional arguments */ |
alt0710 | 1:c359b1fcfb5a | 167 | ) |
alt0710 | 1:c359b1fcfb5a | 168 | { |
alt0710 | 1:c359b1fcfb5a | 169 | va_list arp; |
alt0710 | 1:c359b1fcfb5a | 170 | |
alt0710 | 1:c359b1fcfb5a | 171 | |
alt0710 | 1:c359b1fcfb5a | 172 | va_start(arp, fmt); |
alt0710 | 1:c359b1fcfb5a | 173 | xvprintf(fmt, arp); |
alt0710 | 1:c359b1fcfb5a | 174 | va_end(arp); |
alt0710 | 1:c359b1fcfb5a | 175 | } |
alt0710 | 1:c359b1fcfb5a | 176 | |
alt0710 | 1:c359b1fcfb5a | 177 | |
alt0710 | 1:c359b1fcfb5a | 178 | void xsprintf ( /* Put a formatted string to the memory */ |
alt0710 | 1:c359b1fcfb5a | 179 | char* buff, /* Pointer to the output buffer */ |
alt0710 | 1:c359b1fcfb5a | 180 | const char* fmt, /* Pointer to the format string */ |
alt0710 | 1:c359b1fcfb5a | 181 | ... /* Optional arguments */ |
alt0710 | 1:c359b1fcfb5a | 182 | ) |
alt0710 | 1:c359b1fcfb5a | 183 | { |
alt0710 | 1:c359b1fcfb5a | 184 | va_list arp; |
alt0710 | 1:c359b1fcfb5a | 185 | |
alt0710 | 1:c359b1fcfb5a | 186 | |
alt0710 | 1:c359b1fcfb5a | 187 | outptr = buff; /* Switch destination for memory */ |
alt0710 | 1:c359b1fcfb5a | 188 | |
alt0710 | 1:c359b1fcfb5a | 189 | va_start(arp, fmt); |
alt0710 | 1:c359b1fcfb5a | 190 | xvprintf(fmt, arp); |
alt0710 | 1:c359b1fcfb5a | 191 | va_end(arp); |
alt0710 | 1:c359b1fcfb5a | 192 | |
alt0710 | 1:c359b1fcfb5a | 193 | *outptr = 0; /* Terminate output string with a \0 */ |
alt0710 | 1:c359b1fcfb5a | 194 | outptr = 0; /* Switch destination for device */ |
alt0710 | 1:c359b1fcfb5a | 195 | } |
alt0710 | 1:c359b1fcfb5a | 196 | |
alt0710 | 1:c359b1fcfb5a | 197 | |
alt0710 | 1:c359b1fcfb5a | 198 | void xfprintf ( /* Put a formatted string to the specified device */ |
alt0710 | 1:c359b1fcfb5a | 199 | void(*func)(unsigned char), /* Pointer to the output function */ |
alt0710 | 1:c359b1fcfb5a | 200 | const char* fmt, /* Pointer to the format string */ |
alt0710 | 1:c359b1fcfb5a | 201 | ... /* Optional arguments */ |
alt0710 | 1:c359b1fcfb5a | 202 | ) |
alt0710 | 1:c359b1fcfb5a | 203 | { |
alt0710 | 1:c359b1fcfb5a | 204 | va_list arp; |
alt0710 | 1:c359b1fcfb5a | 205 | void (*pf)(unsigned char); |
alt0710 | 1:c359b1fcfb5a | 206 | |
alt0710 | 1:c359b1fcfb5a | 207 | |
alt0710 | 1:c359b1fcfb5a | 208 | pf = xfunc_out; /* Save current output device */ |
alt0710 | 1:c359b1fcfb5a | 209 | xfunc_out = func; /* Switch output to specified device */ |
alt0710 | 1:c359b1fcfb5a | 210 | |
alt0710 | 1:c359b1fcfb5a | 211 | va_start(arp, fmt); |
alt0710 | 1:c359b1fcfb5a | 212 | xvprintf(fmt, arp); |
alt0710 | 1:c359b1fcfb5a | 213 | va_end(arp); |
alt0710 | 1:c359b1fcfb5a | 214 | |
alt0710 | 1:c359b1fcfb5a | 215 | xfunc_out = pf; /* Restore output device */ |
alt0710 | 1:c359b1fcfb5a | 216 | } |
alt0710 | 1:c359b1fcfb5a | 217 | |
alt0710 | 1:c359b1fcfb5a | 218 | |
alt0710 | 1:c359b1fcfb5a | 219 | |
alt0710 | 1:c359b1fcfb5a | 220 | /*----------------------------------------------*/ |
alt0710 | 1:c359b1fcfb5a | 221 | /* Dump a line of binary dump */ |
alt0710 | 1:c359b1fcfb5a | 222 | /*----------------------------------------------*/ |
alt0710 | 1:c359b1fcfb5a | 223 | |
alt0710 | 1:c359b1fcfb5a | 224 | void put_dump ( |
alt0710 | 1:c359b1fcfb5a | 225 | const void* buff, /* Pointer to the array to be dumped */ |
alt0710 | 1:c359b1fcfb5a | 226 | unsigned long addr, /* Heading address value */ |
alt0710 | 1:c359b1fcfb5a | 227 | int len, /* Number of items to be dumped */ |
alt0710 | 1:c359b1fcfb5a | 228 | int width /* Size of the items (DF_CHAR, DF_SHORT, DF_LONG) */ |
alt0710 | 1:c359b1fcfb5a | 229 | ) |
alt0710 | 1:c359b1fcfb5a | 230 | { |
alt0710 | 1:c359b1fcfb5a | 231 | int i; |
alt0710 | 1:c359b1fcfb5a | 232 | const unsigned char *bp; |
alt0710 | 1:c359b1fcfb5a | 233 | const unsigned short *sp; |
alt0710 | 1:c359b1fcfb5a | 234 | const unsigned long *lp; |
alt0710 | 1:c359b1fcfb5a | 235 | |
alt0710 | 1:c359b1fcfb5a | 236 | |
alt0710 | 1:c359b1fcfb5a | 237 | xprintf("%08lX ", addr); /* address */ |
alt0710 | 1:c359b1fcfb5a | 238 | |
alt0710 | 1:c359b1fcfb5a | 239 | switch (width) { |
alt0710 | 1:c359b1fcfb5a | 240 | case DW_CHAR: |
alt0710 | 1:c359b1fcfb5a | 241 | bp = (unsigned char*) buff; |
alt0710 | 1:c359b1fcfb5a | 242 | for (i = 0; i < len; i++) /* Hexdecimal dump */ |
alt0710 | 1:c359b1fcfb5a | 243 | xprintf(" %02X", bp[i]); |
alt0710 | 1:c359b1fcfb5a | 244 | xputc(' '); |
alt0710 | 1:c359b1fcfb5a | 245 | for (i = 0; i < len; i++) /* ASCII dump */ |
alt0710 | 1:c359b1fcfb5a | 246 | xputc((bp[i] >= ' ' && bp[i] <= '~') ? bp[i] : '.'); |
alt0710 | 1:c359b1fcfb5a | 247 | break; |
alt0710 | 1:c359b1fcfb5a | 248 | case DW_SHORT: |
alt0710 | 1:c359b1fcfb5a | 249 | sp = (unsigned short*)buff; |
alt0710 | 1:c359b1fcfb5a | 250 | do /* Hexdecimal dump */ |
alt0710 | 1:c359b1fcfb5a | 251 | xprintf(" %04X", *sp++); |
alt0710 | 1:c359b1fcfb5a | 252 | while (--len); |
alt0710 | 1:c359b1fcfb5a | 253 | break; |
alt0710 | 1:c359b1fcfb5a | 254 | case DW_LONG: |
alt0710 | 1:c359b1fcfb5a | 255 | lp = (unsigned long*)buff; |
alt0710 | 1:c359b1fcfb5a | 256 | do /* Hexdecimal dump */ |
alt0710 | 1:c359b1fcfb5a | 257 | xprintf(" %08LX", *lp++); |
alt0710 | 1:c359b1fcfb5a | 258 | while (--len); |
alt0710 | 1:c359b1fcfb5a | 259 | break; |
alt0710 | 1:c359b1fcfb5a | 260 | } |
alt0710 | 1:c359b1fcfb5a | 261 | |
alt0710 | 1:c359b1fcfb5a | 262 | xputc('\n'); |
alt0710 | 1:c359b1fcfb5a | 263 | } |
alt0710 | 1:c359b1fcfb5a | 264 | |
alt0710 | 1:c359b1fcfb5a | 265 | #endif /* _USE_XFUNC_OUT */ |
alt0710 | 1:c359b1fcfb5a | 266 | |
alt0710 | 1:c359b1fcfb5a | 267 | |
alt0710 | 1:c359b1fcfb5a | 268 | |
alt0710 | 1:c359b1fcfb5a | 269 | #if _USE_XFUNC_IN |
alt0710 | 1:c359b1fcfb5a | 270 | unsigned char (*xfunc_in)(void); /* Pointer to the input stream */ |
alt0710 | 1:c359b1fcfb5a | 271 | |
alt0710 | 1:c359b1fcfb5a | 272 | /*----------------------------------------------*/ |
alt0710 | 1:c359b1fcfb5a | 273 | /* Get a line from the input */ |
alt0710 | 1:c359b1fcfb5a | 274 | /*----------------------------------------------*/ |
alt0710 | 1:c359b1fcfb5a | 275 | |
alt0710 | 1:c359b1fcfb5a | 276 | int xgets ( /* 0:End of stream, 1:A line arrived */ |
alt0710 | 1:c359b1fcfb5a | 277 | char* buff, /* Pointer to the buffer */ |
alt0710 | 1:c359b1fcfb5a | 278 | int len /* Buffer length */ |
alt0710 | 1:c359b1fcfb5a | 279 | ) |
alt0710 | 1:c359b1fcfb5a | 280 | { |
alt0710 | 1:c359b1fcfb5a | 281 | int c, i; |
alt0710 | 1:c359b1fcfb5a | 282 | |
alt0710 | 1:c359b1fcfb5a | 283 | |
alt0710 | 1:c359b1fcfb5a | 284 | if (!xfunc_in) return 0; /* No input function specified */ |
alt0710 | 1:c359b1fcfb5a | 285 | |
alt0710 | 1:c359b1fcfb5a | 286 | i = 0; |
alt0710 | 1:c359b1fcfb5a | 287 | for (;;) { |
alt0710 | 1:c359b1fcfb5a | 288 | c = xfunc_in(); /* Get a char from the incoming stream */ |
alt0710 | 1:c359b1fcfb5a | 289 | if (!c) return 0; /* End of stream? */ |
alt0710 | 1:c359b1fcfb5a | 290 | if (c == '\r') break; /* End of line? */ |
alt0710 | 1:c359b1fcfb5a | 291 | if (c == '\b' && i) { /* Back space? */ |
alt0710 | 1:c359b1fcfb5a | 292 | i--; |
alt0710 | 1:c359b1fcfb5a | 293 | if (_LINE_ECHO) xputc(c); |
alt0710 | 1:c359b1fcfb5a | 294 | continue; |
alt0710 | 1:c359b1fcfb5a | 295 | } |
alt0710 | 1:c359b1fcfb5a | 296 | if (c >= ' ' && i < len - 1) { /* Visible chars */ |
alt0710 | 1:c359b1fcfb5a | 297 | buff[i++] = c; |
alt0710 | 1:c359b1fcfb5a | 298 | if (_LINE_ECHO) xputc(c); |
alt0710 | 1:c359b1fcfb5a | 299 | } |
alt0710 | 1:c359b1fcfb5a | 300 | } |
alt0710 | 1:c359b1fcfb5a | 301 | buff[i] = 0; /* Terminate with a \0 */ |
alt0710 | 1:c359b1fcfb5a | 302 | if (_LINE_ECHO) xputc('\n'); |
alt0710 | 1:c359b1fcfb5a | 303 | return 1; |
alt0710 | 1:c359b1fcfb5a | 304 | } |
alt0710 | 1:c359b1fcfb5a | 305 | |
alt0710 | 1:c359b1fcfb5a | 306 | |
alt0710 | 1:c359b1fcfb5a | 307 | int xfgets ( /* 0:End of stream, 1:A line arrived */ |
alt0710 | 1:c359b1fcfb5a | 308 | unsigned char (*func)(void), /* Pointer to the input stream function */ |
alt0710 | 1:c359b1fcfb5a | 309 | char* buff, /* Pointer to the buffer */ |
alt0710 | 1:c359b1fcfb5a | 310 | int len /* Buffer length */ |
alt0710 | 1:c359b1fcfb5a | 311 | ) |
alt0710 | 1:c359b1fcfb5a | 312 | { |
alt0710 | 1:c359b1fcfb5a | 313 | unsigned char (*pf)(void); |
alt0710 | 1:c359b1fcfb5a | 314 | int n; |
alt0710 | 1:c359b1fcfb5a | 315 | |
alt0710 | 1:c359b1fcfb5a | 316 | |
alt0710 | 1:c359b1fcfb5a | 317 | pf = xfunc_in; /* Save current input device */ |
alt0710 | 1:c359b1fcfb5a | 318 | xfunc_in = func; /* Switch input to specified device */ |
alt0710 | 1:c359b1fcfb5a | 319 | n = xgets(buff, len); /* Get a line */ |
alt0710 | 1:c359b1fcfb5a | 320 | xfunc_in = pf; /* Restore input device */ |
alt0710 | 1:c359b1fcfb5a | 321 | |
alt0710 | 1:c359b1fcfb5a | 322 | return n; |
alt0710 | 1:c359b1fcfb5a | 323 | } |
alt0710 | 1:c359b1fcfb5a | 324 | |
alt0710 | 1:c359b1fcfb5a | 325 | |
alt0710 | 1:c359b1fcfb5a | 326 | /*----------------------------------------------*/ |
alt0710 | 1:c359b1fcfb5a | 327 | /* Get a value of the string */ |
alt0710 | 1:c359b1fcfb5a | 328 | /*----------------------------------------------*/ |
alt0710 | 1:c359b1fcfb5a | 329 | /* "123 -5 0x3ff 0b1111 0377 w " |
alt0710 | 1:c359b1fcfb5a | 330 | ^ 1st call returns 123 and next ptr |
alt0710 | 1:c359b1fcfb5a | 331 | ^ 2nd call returns -5 and next ptr |
alt0710 | 1:c359b1fcfb5a | 332 | ^ 3rd call returns 1023 and next ptr |
alt0710 | 1:c359b1fcfb5a | 333 | ^ 4th call returns 15 and next ptr |
alt0710 | 1:c359b1fcfb5a | 334 | ^ 5th call returns 255 and next ptr |
alt0710 | 1:c359b1fcfb5a | 335 | ^ 6th call fails and returns 0 |
alt0710 | 1:c359b1fcfb5a | 336 | */ |
alt0710 | 1:c359b1fcfb5a | 337 | |
alt0710 | 1:c359b1fcfb5a | 338 | int xatoi ( /* 0:Failed, 1:Successful */ |
alt0710 | 1:c359b1fcfb5a | 339 | char **str, /* Pointer to pointer to the string */ |
alt0710 | 1:c359b1fcfb5a | 340 | long *res /* Pointer to the valiable to store the value */ |
alt0710 | 1:c359b1fcfb5a | 341 | ) |
alt0710 | 1:c359b1fcfb5a | 342 | { |
alt0710 | 1:c359b1fcfb5a | 343 | unsigned long val; |
alt0710 | 1:c359b1fcfb5a | 344 | unsigned char c, r, s = 0; |
alt0710 | 1:c359b1fcfb5a | 345 | |
alt0710 | 1:c359b1fcfb5a | 346 | |
alt0710 | 1:c359b1fcfb5a | 347 | *res = 0; |
alt0710 | 1:c359b1fcfb5a | 348 | |
alt0710 | 1:c359b1fcfb5a | 349 | while ((c = **str) == ' ') (*str)++; /* Skip leading spaces */ |
alt0710 | 1:c359b1fcfb5a | 350 | |
alt0710 | 1:c359b1fcfb5a | 351 | if (c == '-') { /* negative? */ |
alt0710 | 1:c359b1fcfb5a | 352 | s = 1; |
alt0710 | 1:c359b1fcfb5a | 353 | c = *(++(*str)); |
alt0710 | 1:c359b1fcfb5a | 354 | } |
alt0710 | 1:c359b1fcfb5a | 355 | |
alt0710 | 1:c359b1fcfb5a | 356 | if (c == '0') { |
alt0710 | 1:c359b1fcfb5a | 357 | c = *(++(*str)); |
alt0710 | 1:c359b1fcfb5a | 358 | switch (c) { |
alt0710 | 1:c359b1fcfb5a | 359 | case 'x': /* hexdecimal */ |
alt0710 | 1:c359b1fcfb5a | 360 | r = 16; c = *(++(*str)); |
alt0710 | 1:c359b1fcfb5a | 361 | break; |
alt0710 | 1:c359b1fcfb5a | 362 | case 'b': /* binary */ |
alt0710 | 1:c359b1fcfb5a | 363 | r = 2; c = *(++(*str)); |
alt0710 | 1:c359b1fcfb5a | 364 | break; |
alt0710 | 1:c359b1fcfb5a | 365 | default: |
alt0710 | 1:c359b1fcfb5a | 366 | if (c <= ' ') return 1; /* single zero */ |
alt0710 | 1:c359b1fcfb5a | 367 | if (c < '0' || c > '9') return 0; /* invalid char */ |
alt0710 | 1:c359b1fcfb5a | 368 | r = 8; /* octal */ |
alt0710 | 1:c359b1fcfb5a | 369 | } |
alt0710 | 1:c359b1fcfb5a | 370 | } else { |
alt0710 | 1:c359b1fcfb5a | 371 | if (c < '0' || c > '9') return 0; /* EOL or invalid char */ |
alt0710 | 1:c359b1fcfb5a | 372 | r = 10; /* decimal */ |
alt0710 | 1:c359b1fcfb5a | 373 | } |
alt0710 | 1:c359b1fcfb5a | 374 | |
alt0710 | 1:c359b1fcfb5a | 375 | val = 0; |
alt0710 | 1:c359b1fcfb5a | 376 | while (c > ' ') { |
alt0710 | 1:c359b1fcfb5a | 377 | if (c >= 'a') c -= 0x20; |
alt0710 | 1:c359b1fcfb5a | 378 | c -= '0'; |
alt0710 | 1:c359b1fcfb5a | 379 | if (c >= 17) { |
alt0710 | 1:c359b1fcfb5a | 380 | c -= 7; |
alt0710 | 1:c359b1fcfb5a | 381 | if (c <= 9) return 0; /* invalid char */ |
alt0710 | 1:c359b1fcfb5a | 382 | } |
alt0710 | 1:c359b1fcfb5a | 383 | if (c >= r) return 0; /* invalid char for current radix */ |
alt0710 | 1:c359b1fcfb5a | 384 | val = val * r + c; |
alt0710 | 1:c359b1fcfb5a | 385 | c = *(++(*str)); |
alt0710 | 1:c359b1fcfb5a | 386 | } |
alt0710 | 1:c359b1fcfb5a | 387 | if (s) val = 0 - val; /* apply sign if needed */ |
alt0710 | 1:c359b1fcfb5a | 388 | |
alt0710 | 1:c359b1fcfb5a | 389 | *res = val; |
alt0710 | 1:c359b1fcfb5a | 390 | return 1; |
alt0710 | 1:c359b1fcfb5a | 391 | } |
alt0710 | 1:c359b1fcfb5a | 392 | |
alt0710 | 1:c359b1fcfb5a | 393 | #endif /* _USE_XFUNC_IN */ |