Nut/OS  4.10.3
API Reference
cppdemo.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 by Oliver Schulz (MPI)
00003  *
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  * 2. Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in the
00014  *    documentation and/or other materials provided with the distribution.
00015  * 3. Neither the name of the copyright holders nor the names of
00016  *    contributors may be used to endorse or promote products derived
00017  *    from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00022  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00023  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00024  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00025  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00026  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00027  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00028  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00029  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00030  * SUCH DAMAGE.
00031  *
00032  * For additional information see http://www.ethernut.de/
00033  */
00034 
00048 #include <cpp/nutcpp.h>
00049 
00050 extern "C" {
00051 #include <dev/board.h>
00052 #include <sys/version.h>
00053 #include <inttypes.h>
00054 #include <io.h>
00055 #include <stdio.h>
00056 }
00057 
00058 
00059 
00060 template<class tp_type> class TemplateCounter
00061 {
00062 protected:
00063     tp_type m_value;
00064 
00065 public:
00066     tp_type value() const { return m_value; }
00067     void inc() { m_value++; }
00068     void dec() { m_value--; }
00069     void set(const tp_type &newValue) { m_value = newValue; }
00070 };
00071 
00072 
00073 
00074 class Counter: public TemplateCounter<uint8_t>
00075 {
00076 public:
00077     void print(FILE *stream);
00078 
00079     Counter(uint8_t initValue=10);
00080 };
00081 
00082 
00083 void Counter::print(FILE* stream)
00084 {
00085     fprintf(stream, "\nCounter value = %i\n", value());
00086 }
00087 
00088 
00089 Counter::Counter(uint8_t initValue)
00090 {
00091     m_value = initValue;
00092 }
00093 
00094 
00095 
00096 int main(void) {
00097     u_long baud = 115200;
00098 
00099     NutRegisterDevice(&DEV_UART0, 0, 0);
00100     FILE *stream = fopen(DEV_UART0_NAME, "r+");
00101     _ioctl(_fileno(stream), UART_SETSPEED, &baud);
00102 
00103     fprintf(stream, "\n\nC++ Demo on Nut/OS %s ready.\n", NutVersionString());
00104 
00105     Counter counter;
00106     counter.print(stream);
00107 
00108     for (;;) {
00109         char c;
00110         fread(&c, sizeof(c), 1, stream);
00111 
00112         switch (c) {
00113         case '+':
00114             counter.inc();
00115             counter.print(stream);
00116             break;
00117         case '-':
00118             counter.dec();
00119             counter.print(stream);
00120             break;
00121         case 'r':
00122             counter.set(0);
00123             counter.print(stream);
00124             break;
00125         default:
00126             fprintf(stream, "Unknown command.\n");
00127         }
00128     }
00129     return 0;
00130 }
00131 
00132