Cppdemo.c

From Nutwiki
Revision as of 22:09, 1 May 2007 by Steinwedel (Talk)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Dieses Beispiel zeigt, wie Nut/OS in Verbindung mit C++ verwendet werden kann. Auch wenn die Verwendung von C++ möglich ist, sollten die Vor- und Nachteile sorgfältig abgewogen werden. Nicht nur die Resourcen können knapp sein. C++ kann die Fehlersuche komplizieren.

// Trivial C++ Demo for NutOS.

#include <cpp/nutcpp.h>

extern "C" {
#include <dev/board.h>
#include <sys/version.h>
#include <inttypes.h>
#include <io.h>
#include <stdio.h>
}



template<class tp_type> class TemplateCounter
{
protected:
    tp_type m_value;

public:
    tp_type value() const { return m_value; }
    void inc() { m_value++; }
    void dec() { m_value--; }
    void set(const tp_type &newValue) { m_value = newValue; }
};



class Counter: public TemplateCounter<uint8_t>
{
public:
    void print(FILE *stream);

    Counter(uint8_t initValue=10);
};


void Counter::print(FILE* stream)
{
    fprintf(stream, "\nCounter value = %i\n", value());
}


Counter::Counter(uint8_t initValue)
{
    m_value = initValue;
}



int main(void) {
    u_long baud = 115200;

    NutRegisterDevice(&DEV_UART0, 0, 0);
    FILE *stream = fopen(DEV_UART0_NAME, "r+");
    _ioctl(_fileno(stream), UART_SETSPEED, &baud);

    fprintf(stream, "\n\nC++ Demo on Nut/OS %s ready.\n", NutVersionString());

    Counter counter;
    counter.print(stream);

    for (;;) {
        char c;
        fread(&c, sizeof(c), 1, stream);

        switch (c) {
        case '+':
            counter.inc();
            counter.print(stream);
            break;
        case '-':
            counter.dec();
            counter.print(stream);
            break;
        case 'r':
            counter.set(0);
            counter.print(stream);
            break;
        default:
            fprintf(stream, "Unknown command.\n");
        }
    }
}

cppdemo.c Copyright by egnite Software GmbH