Frank Duignan
/
Nucleo_microrealms
Microrealms: An adventure game on the mbed platform
serial.cpp@0:4da21a20e2c1, 2014-12-04 (annotated)
- Committer:
- f3d
- Date:
- Thu Dec 04 14:17:50 2014 +0000
- Revision:
- 0:4da21a20e2c1
An adventure game called MicroRealms on the mbed platform. Should work with most mbed enabled devices as it is pretty frugal with resources. ; Requires a dumb terminal program (e.g. hyperterminal,minicom etc)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
f3d | 0:4da21a20e2c1 | 1 | /* |
f3d | 0:4da21a20e2c1 | 2 | Copyright (C) 2014 Frank Duignan |
f3d | 0:4da21a20e2c1 | 3 | |
f3d | 0:4da21a20e2c1 | 4 | This program is free software; you can redistribute it and/or |
f3d | 0:4da21a20e2c1 | 5 | modify it under the terms of the GNU General Public License |
f3d | 0:4da21a20e2c1 | 6 | as published by the Free Software Foundation; either version 2 |
f3d | 0:4da21a20e2c1 | 7 | of the License, or (at your option) any later version. |
f3d | 0:4da21a20e2c1 | 8 | |
f3d | 0:4da21a20e2c1 | 9 | This program is distributed in the hope that it will be useful, |
f3d | 0:4da21a20e2c1 | 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
f3d | 0:4da21a20e2c1 | 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
f3d | 0:4da21a20e2c1 | 12 | GNU General Public License for more details. |
f3d | 0:4da21a20e2c1 | 13 | |
f3d | 0:4da21a20e2c1 | 14 | You should have received a copy of the GNU General Public License |
f3d | 0:4da21a20e2c1 | 15 | along with this program; if not, write to the Free Software |
f3d | 0:4da21a20e2c1 | 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
f3d | 0:4da21a20e2c1 | 17 | */ |
f3d | 0:4da21a20e2c1 | 18 | |
f3d | 0:4da21a20e2c1 | 19 | #include "mbed.h" |
f3d | 0:4da21a20e2c1 | 20 | Serial pc(USBTX, USBRX); |
f3d | 0:4da21a20e2c1 | 21 | void eputc(const char c) |
f3d | 0:4da21a20e2c1 | 22 | { |
f3d | 0:4da21a20e2c1 | 23 | pc.putc(c); |
f3d | 0:4da21a20e2c1 | 24 | } |
f3d | 0:4da21a20e2c1 | 25 | char egetc() |
f3d | 0:4da21a20e2c1 | 26 | { |
f3d | 0:4da21a20e2c1 | 27 | if (pc.readable()) |
f3d | 0:4da21a20e2c1 | 28 | return pc.getc(); |
f3d | 0:4da21a20e2c1 | 29 | else |
f3d | 0:4da21a20e2c1 | 30 | return 0; |
f3d | 0:4da21a20e2c1 | 31 | } |
f3d | 0:4da21a20e2c1 | 32 | void eputs(const char *String) |
f3d | 0:4da21a20e2c1 | 33 | { |
f3d | 0:4da21a20e2c1 | 34 | while(*String) |
f3d | 0:4da21a20e2c1 | 35 | { |
f3d | 0:4da21a20e2c1 | 36 | eputc(*String); |
f3d | 0:4da21a20e2c1 | 37 | String++; |
f3d | 0:4da21a20e2c1 | 38 | } |
f3d | 0:4da21a20e2c1 | 39 | } |
f3d | 0:4da21a20e2c1 | 40 | |
f3d | 0:4da21a20e2c1 | 41 | void printString(const char *String) |
f3d | 0:4da21a20e2c1 | 42 | { |
f3d | 0:4da21a20e2c1 | 43 | eputs(String); |
f3d | 0:4da21a20e2c1 | 44 | eputs("\r\n"); |
f3d | 0:4da21a20e2c1 | 45 | } |
f3d | 0:4da21a20e2c1 | 46 | |
f3d | 0:4da21a20e2c1 | 47 | char HexDigit(int Value) |
f3d | 0:4da21a20e2c1 | 48 | { |
f3d | 0:4da21a20e2c1 | 49 | if ((Value >=0) && (Value < 10)) |
f3d | 0:4da21a20e2c1 | 50 | return Value+'0'; |
f3d | 0:4da21a20e2c1 | 51 | else |
f3d | 0:4da21a20e2c1 | 52 | return Value-10 + 'A'; |
f3d | 0:4da21a20e2c1 | 53 | } |
f3d | 0:4da21a20e2c1 | 54 | void printHex(unsigned int Number) |
f3d | 0:4da21a20e2c1 | 55 | { |
f3d | 0:4da21a20e2c1 | 56 | // Output the number over the serial port as |
f3d | 0:4da21a20e2c1 | 57 | // as hexadecimal string. |
f3d | 0:4da21a20e2c1 | 58 | char TxString[9]; |
f3d | 0:4da21a20e2c1 | 59 | int Index=8; |
f3d | 0:4da21a20e2c1 | 60 | TxString[Index]=0; // terminate the string |
f3d | 0:4da21a20e2c1 | 61 | Index--; |
f3d | 0:4da21a20e2c1 | 62 | while(Index >=0) |
f3d | 0:4da21a20e2c1 | 63 | { |
f3d | 0:4da21a20e2c1 | 64 | TxString[Index]=HexDigit(Number & 0x0f); |
f3d | 0:4da21a20e2c1 | 65 | Number = Number >> 4; |
f3d | 0:4da21a20e2c1 | 66 | Index--; |
f3d | 0:4da21a20e2c1 | 67 | } |
f3d | 0:4da21a20e2c1 | 68 | eputs(TxString); |
f3d | 0:4da21a20e2c1 | 69 | } |