C++ Code and Syntax help please

22 Jan 2013

Hello Everyone, I'm getting my head round classes again after a LONG break. I'm using the TextLCD.h and Text.cpp file from Simon Ford as examples. I've got my head round naming classes, what member declaration is and member functions, what enumeration is, what public: and private: means but I'm at a loss with the following:-

#if DOXYGEN_ONLY
    /** Write a character to the LCD
     *
     * @param c The character to write to the display
     */
    int putc(int c);

    /** Write a formated string to the LCD
     *
     * @param format A printf-style format string, followed by the
     *               variables to use in formating the string.
     */
    int printf(const char* format, ...);
#endif

What is the syntax DOXYGEN_ONLY ????

What do these three Private Member functions do? I've kind of understood a virtual function declaration as part of the polymorphism portion of OOP

int TextLCD::_getc() {
    return -1;
}

void TextLCD::writeCommand(int command) {
    _rs = 0;
    writeByte(command);
}

void TextLCD::writeData(int data) {
    _rs = 1;
    writeByte(data);
}

Am I right in thinking that writeByte is a function? If it is I simply can't find it or I'm missing something fundamental.

Greatly appreciate any help here.

22 Jan 2013

writeByte() is the low-level operation to write a byte to the LCD in two parts of 4 bits. That is because the class uses the 4bit databus mode to control the LCD rather than the normal 8bit mode. This saves datapins on the mbed.

You can find the declaration of writeByte just above TextLCD::writeCommand.

void TextLCD::writeByte(int value) {
    _d = value >> 4;
    wait(0.000040f); // most instructions take 40us
    _e = 0;
    wait(0.000040f);
    _e = 1;
    _d = value >> 0;
    wait(0.000040f);
    _e = 0;
    wait(0.000040f);  // most instructions take 40us
    _e = 1;
}

Depending on the value of the _rs bit a written byte will be interpreted by the LCD as either a command or a databyte (ie character in most cases).

TextLCD::_getc() needs to be implemented since TextLCD was defined as implementing the Stream protocol. See TextLCD.h

class TextLCD : public Stream {

We want to have 'Stream' so that we can make free use of all the neat printf() formatting stuff :) Stream needs implementations for _putc() and _getc().

The _putc() has been implemented to just send the character to the current LCD cursor location. However, _getc() makes no sense for the LCD and it is implemented as a dummy just returning -1.

23 Jan 2013

Regarding to DOXYGEN_ONLY, it doesnt do anything, literally. That if statement is always false, and the compiler will never include that part. Doxygen is the markup used for adding documentation, and the part that generates the documentation from the included comments does not recognize that if statement, so it will include those functions in the documentation.

So what it does is making sure those functions are not being defined in the class, but documentation is made for them. However as Wim said, the class does inherit from the Stream class, and the Stream class does have those functions implemented, so you can use them. And this is the nicest way to have documentation for those functions.

23 Jan 2013

Hi Chaps, Thanks for that, I Googled (DOXYGEN C++) and couldn't really get my head around what it meant, fair to say I'm not that advanced with coding, but I do want to understand what the code means and does. My first understanding was that it meant literally DO_XY_GEN i.e. DO XY GEN short for generate the XY locations.

Plus I need to read up on Stream as Wim pointed out.

I'm going to try writing a simple class on my own just to switch on and off LED's so I get used to putting it all together. Please be patient I will get there...........eventually Ha ha ha ha ha

I think I'll be posting regular as I get my head round things.

Many thanks as always.