Revision 1.3 2006/07/10 14:24:11 haraldkipp Header files replaced by platform independent variants. Contributed by Matthias Wilde.
Revision 1.2 2005/08/02 17:46:44 haraldkipp Major API documentation update.
This sample demonstrates the usage of Nut/OS with C++.
You should carefully think about using C++ with tiny embedded systems. This sample just proofs, that it basically works.
00001 // Trivial C++ Demo for NutOS. 00002 00026 #include <cpp/nutcpp.h> 00027 00028 extern "C" { 00029 #include <dev/board.h> 00030 #include <sys/version.h> 00031 #include <inttypes.h> 00032 #include <io.h> 00033 #include <stdio.h> 00034 } 00035 00036 00037 00038 template<class tp_type> class TemplateCounter 00039 { 00040 protected: 00041 tp_type m_value; 00042 00043 public: 00044 tp_type value() const { return m_value; } 00045 void inc() { m_value++; } 00046 void dec() { m_value--; } 00047 void set(const tp_type &newValue) { m_value = newValue; } 00048 }; 00049 00050 00051 00052 class Counter: public TemplateCounter<uint8_t> 00053 { 00054 public: 00055 void print(FILE *stream); 00056 00057 Counter(uint8_t initValue=10); 00058 }; 00059 00060 00061 void Counter::print(FILE* stream) 00062 { 00063 fprintf(stream, "\nCounter value = %i\n", value()); 00064 } 00065 00066 00067 Counter::Counter(uint8_t initValue) 00068 { 00069 m_value = initValue; 00070 } 00071 00072 00073 00074 int main(void) { 00075 u_long baud = 115200; 00076 00077 NutRegisterDevice(&DEV_UART0, 0, 0); 00078 FILE *stream = fopen(DEV_UART0_NAME, "r+"); 00079 _ioctl(_fileno(stream), UART_SETSPEED, &baud); 00080 00081 fprintf(stream, "\n\nC++ Demo on Nut/OS %s ready.\n", NutVersionString()); 00082 00083 Counter counter; 00084 counter.print(stream); 00085 00086 for (;;) { 00087 char c; 00088 fread(&c, sizeof(c), 1, stream); 00089 00090 switch (c) { 00091 case '+': 00092 counter.inc(); 00093 counter.print(stream); 00094 break; 00095 case '-': 00096 counter.dec(); 00097 counter.print(stream); 00098 break; 00099 case 'r': 00100 counter.set(0); 00101 counter.print(stream); 00102 break; 00103 default: 00104 fprintf(stream, "Unknown command.\n"); 00105 } 00106 } 00107 return 0; 00108 } 00109 00110