Arduino is slowly becoming my bitch. OK, maybe I’m just a little over-impressed with myself, but I feel a great deal of satisfaction after altering one of the Arduino’s base classes.
I was trying to get the WiFly initialization routine to show up nicely on the 4×20 LCD, but I found that the lcd.println() function kept adding a second line break after writing text to the screen. I expected it to just go to the next line rather than skipping it.
My brief workaround was to revert to the basic lcd.print() function which leaves the cursor at the end of the string (every successive call to print() results in the new text being printed immediately after the prior text which is ugly). Then, I had to include “\r” at the end of my text strings or insert the code between print()s of values. Not ideal.
I decided to look into the LCDi2cR.h library which I’m using for communication with the LCD. Upon inspecting the file, there was no definition of the print() or println() functions. Then I remembered something Martin taught me about extending a class. Sure enough, at the beginning of the class was the following:
class LCDi2cR : public Print {
Ah-ha! Whoever wrote this LCD library simply took the existing Print class and extended it to print to the LCD instead of its normal target. However, this means I still needed to find where the actual Print class was originally defined. Looking at the #includes at the start of the header file, I didn’t see anything special. Just #includes of the “Stdio.h” and “Stdlib.h” files. Before trying to find those files, though, I decided to look for “WProgram.h” which I learned is attached to every Arduino program. Upon finding that header file buried in the Arduino package contents, I inspected it…only to find no trace of Print there, either. However, I did see “Print.h” at the top, so I went looking for it and found it.
Inside this header file was a declaration of the various print() and println() methods. So I opened up the .cpp file and found the actual definitions. Strangely, after printing the appropriate value or string, there was another call to println(). Not knowing what method this was, I commented it out and replaced it with print(“\r”). I did this for every variation of println() I could find.
After saving these changes, I removed all the “\r” codes from my main code and reverted the print() commands to println(). After compiling and uploading, the display showed the 4 steps of WiFly initialization one after the other, nice and pretty.
So this indicates that I’m starting to get a deeper understanding and feeling of developing C++ code. Funny–one of the first things I bought with my very first paycheck in high school was a Borland C++ 3.0 book (more like a bible, the thing was so thick). Technically, I’ve had all the necessary knowledge to do what I’m doing right now right at my fingertips for the last 20 years. Strange how it took this long for it to actually become relevant…
Leave Your Response