1

I need to split up code which I've already developed for one micro into two different Arduinos connected by serial. The "remote" micro is directly connected to a 20x4 character LCD and the main micro will be sending it a command set to print, move cursor, etc.

My plan was to make functions that used existing code such as lcd.print("This or that"); to replace the lcd library on the main micro and send command bytes and data bytes up to the remote micro. Seems pretty straightforward.

Then I discovered that apparently lcd.print cannot be a function name. Fine, so I changed it to lcd_print(). So far, so good. But in the existing library, lcd.print accepts strings, bytes, just about anything any length. For example: lcd_print("any string of any length"); lcd_print(a_2d_array [x] [y]); lcd_print(this_byte); lcd_print(this_integer);

Now it's getting over my head. I know I can painstakingly turn a single function into many different functions, but wouldn't it be nice to replicate the functionality of the Arduino LCD library with a single command:

lcd.print();

Then all I have to do is create the function and keep the existing code and send the bytes up to the slave micro. Wouldn't that be nice! I appreciate any thoughts. Thanks.

1 Answer 1

1

Just inherit Print class and implement virtual size_t write(uint8_t); and you can use all kinds of print/println that Print implements. See Print Class @arduino.cc.

It implements these methods for you:

size_t print(const __FlashStringHelper *);
size_t print(const String &);
size_t print(const char[]);
size_t print(char);
size_t print(unsigned char, int = DEC);
size_t print(int, int = DEC);
size_t print(unsigned int, int = DEC);
size_t print(long, int = DEC);
size_t print(unsigned long, int = DEC);
size_t print(double, int = 2);
size_t print(const Printable&);

size_t println(const __FlashStringHelper *);
size_t println(const String &s);
size_t println(const char[]);
size_t println(char);
size_t println(unsigned char, int = DEC);
size_t println(int, int = DEC);
size_t println(unsigned int, int = DEC);
size_t println(long, int = DEC);
size_t println(unsigned long, int = DEC);
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);

However you have to implement 2D array print if you really need this, I don't see why do you need it but whatever (you can print each line separately). It just saves you time with implementing everything else.

And for std::ostream like approach I like this (works with anything inherited from Print class):

template <class T> inline Print & operator<<(Print & p, const T & val) {
  p.print(val);
  return p;
}

// ...

void loop() {
  float variableA = 221.23;
  String variableB = "Some string";
  Serial << F("flash string to save some ram: ") << variableA << F(" / ") << variableB << "\n";
}

Then you can implement some commands interface.

5
  • Dear KIV: Thank you for your response! I'm afraid I don't know how to "inherit Print class". It's beyond my current programming experience. I often see the "uint8_t" in code I've looked at and puzzled over what it does. I'll try to do some research but would appreciate a jump start!
    – Bob Katz
    Aug 22, 2016 at 23:41
  • Dear KIV: Thank you. Assuming I add this code, would it be in setup? class lcdclass: public Print { public: virtual size_t write(uint8_t); }; Given the old lcd command sans library: Will the old command work? lcd.print(some_crazy_set_of_data); if I write a function: void lcd.print()? Don't know how to pass the many different formats of possible parameters to this function. Maybe cause it's inherited, but please advisee.
    – Bob Katz
    Aug 22, 2016 at 23:59
  • That answers it, KIV! Thanks. You're right. Now I have to decide if it's gonna be easier to retain all the lcd.xxx commands in the current code and write new functions, or go through the code line by line and revise it to send serials up to the remote micro. Despite the attraction of the print class it sounds more attractive to me to just revise the code because some of the commands are 2d, some are 1d, etc. and you're absolutely right, eventually it's going to be sent down serial as one byte followed by another!!!
    – Bob Katz
    Aug 23, 2016 at 12:31
  • At the remote micro end, it's going to reassemble those bytes into lcd.xxx commands anyway! As the remote is going to use the existing lcd library.
    – Bob Katz
    Aug 23, 2016 at 12:33
  • You can find an implementation of this in the Cosa VLCD library. It implements the Cosa LCD interface but uses I2C/TWI as an adapter. The library contains both the application interface and the TWI slave implementation. github.com/mikaelpatel/Cosa/blob/master/libraries/VLCD/VLCD.hh Aug 23, 2016 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.