diff options
| author | nucho <javatea@it.willcomlive.jp> | 2012-03-17 17:09:53 +0900 |
|---|---|---|
| committer | nucho <javatea@it.willcomlive.jp> | 2012-03-17 17:09:53 +0900 |
| commit | 8de17f2ec3e91b70061b801cc57d97d3adca2ef6 (patch) | |
| tree | e950cae77a400af4f86dd2ff832aa5a4c5b58823 | |
| parent | 3441ce8cae40b246be79b4ba7b347937b5114f9c (diff) | |
Added Print class
| -rw-r--r-- | src/core/Print.cpp | 109 | ||||
| -rw-r--r-- | src/core/Print.h | 40 |
2 files changed, 149 insertions, 0 deletions
diff --git a/src/core/Print.cpp b/src/core/Print.cpp new file mode 100644 index 0000000..ab58b9f --- /dev/null +++ b/src/core/Print.cpp @@ -0,0 +1,109 @@ +#include "Print.h" + +void Print::print(const char *s) //uart_puts +{ + int n; + for (n = 0; *s; s++, n++) { + write(*s); + } + //return n; + return; +} + +void Print::print(int val, RADIX_FORMAT format) +{ + if(format == BYTE) + { + write((char)(val & 0xff)); + return; + } + char s[32]; + int i,j; + i=j=0; // header of strings + if (val<0) + { //minus value + s[i]='-'; + val=~val+1; + i++; + j++; + } + switch(format) + { + case BIN: + while (val>0) + { // do until zero + s[i++]=radix_bin[val%format]; + val/=format; + } + break; + + case OCT: + while (val>0) + { // do until zero + s[i++]=radix_oct[val%format]; + val/=format; + } + break; + + case DEC: //RADIX = Decimal + while (val>0) + { // do until zero + s[i++]=radix_dec[val%format]; + val/=format; + } + break; + + case HEX: //RADIX = Hexadecimal + while (val>0) + { // do until zero + s[i++]=radix_hex[val%format]; + val/=format; + } + break; + default: + break; + } + s[i--]='\0'; // EOF + while (j<i) + { // reverse strings array + char t; + t=s[j]; s[j]=s[i]; s[i]=t; + j++; i--; + } + print(s); + + return ; +} + +void Print::write(const char *s, int length) +{ + int n; + for(n=0;n<length;s++,n++) + { + write(*s); + } + return; +} + +void Print::println(const char *s) +{ + print(s); + println(); + return; +} + + +void Print::println(int val, RADIX_FORMAT format) +{ + print(val,format); + println(); + return; +} + +void Print::println(void) +{ + write ('\r'); + write ('\n'); + return; +} + diff --git a/src/core/Print.h b/src/core/Print.h new file mode 100644 index 0000000..abfd04e --- /dev/null +++ b/src/core/Print.h @@ -0,0 +1,40 @@ +/* + Print.cpp + Base class that provides print() and println() + Created on: 2012/03/17 + */ +#ifndef Print_h +#define Print_h + +#include "LPC11xx.h" + +const char radix_bin[] = "01"; +const char radix_oct[] = "01234567"; +const char radix_dec[] = "0123456789"; +const char radix_hex[] = "0123456789ABCDEF"; + +typedef enum { + BYTE = 0x01, /*!< 1バイトのアスキーコードが表示される */ + BIN = 0x02, /*!< dataで与えられた数値を2進数で表示する */ + OCT = 0x08, /*!< dataで与えられた数値を8進数で表示する */ + DEC = 0x0A, /*!< dataで与えられた数値を10進数で表示する */ + HEX = 0x10 +/*!< dataで与えられた数値を16進数で表示する */ +} RADIX_FORMAT; + + +class Print { +private: + // RADIX_FORMAT format; +public: + virtual void write(const char c) = 0; + void write(const char *s,int length); + + virtual void println(void); + void print(const char *s); + void print(int val, RADIX_FORMAT format); + void println(const char *s); + void println(int val, RADIX_FORMAT format); +}; + +#endif |
