Possible to use a variable to point to Serial or LCD or other char devices?

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all,

 

What I'm trying to figure out is how to use a variable to point to different character devices. Here's an example of what I want to do:
 

static LiquidCrystal LCD (_RS, _RW, _EN, _D4, _D5, _D6, _D7); // setup the LCD
Serial.begin (115200); // setup the serial port

static PTR; // will be set to various char devices

PTR = Serial;
PTR.println ("Hello there serial port"); // goes to the serial port

PTR = LCD;
PTR.println ("Hello there LCD display"); // goes to the LCD

 

I've tried everything I can think of and nothing works (i.e. I get various compiler errors).

 

Trying "static Print &ptr = 0" compiles, but gives me an error that certain functions do not exist (such as Serial doesn't have "createChar" like LCD does).

 

The only thing that DOES work is to use #define... but I would rather not do it that way.

 

Any ideas will be appreciated!

 

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What you have posted makes no sense.  Can you post your program?

 

Why use PTR for two different functions?  Makes no sense, and make for confusing code to read, and keep track of.

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

https://www.nongnu.org/avr-libc/...

 

commonly known as a 'file pointer'

 

but your code shows some Arduino stuff, in which case you need to implement a stream interface.

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Krupski wrote:
Trying "static Print &ptr = 0" compiles, but gives me an error that certain functions do not exist (such as Serial doesn't have "createChar" like LCD does).

Well the Arduino Print class has a header that starts like this:

/*
  Print.h - Base class that provides print() and println()
  Copyright (c) 2008 David A. Mellis.  All right reserved.
  This library is free software; ...
  ...
*/

So those are the only two Public functions that are available to your PTR object.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Krupski wrote:
Trying "static Print &ptr = 0" compiles

 

Um... It appears that you are making something up. This cannot possibly compile. Also, I don't understand why you are talking about pointers, but attempting to use a reference instead of a pointer.

 

From what you posted, you just need a pointer

static Print *ptr = 0;

after which you can do

ptr = &LCD;

or 

ptr = &Serial;

You can do it since both `LiquidCrystal` class and `Stream` class (base of various `Serial` classes) are polymorphic subclasses of `Print`.

 

And later you can just use

ptr->println("Hello World!");

That's it.

 

Krupski wrote:
but gives me an error that certain functions do not exist (such as Serial doesn't have "createChar" like LCD does)

 

If you are planning to use functions that are not part of common interface of these two classes (i.e. not part of `Print`), then you will not be able to do it through that "common pointer".

 

To use class-specific functions, access the objects directly. To use the common interface in abstract contexts, use the pointer.

Dessine-moi un mouton

Last Edited: Tue. Dec 24, 2019 - 11:36 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

#5

Last Edited: Tue. Dec 24, 2019 - 11:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is an example of the ability of C to use pointers to access functions. Its not commonly done, but there are plenty of tutorials out there, and it is discussed in the K&R book.

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OP is on the right track

 

void LcdTest (void)
{
	LiquidCrystal lcd = LiquidCrystal();
	Print &ptr = lcd;

	ptr.print("Hello World");
}

 

I synthesised Print and LiquidCrystal classes that minimally mimic the Arduino classes because I'm coding this on the PC.

 

The above code compiles and run and does indeed print "Hello World" using the function defined in the Print class.

 

<edit>

Corrected Copy & Paste mistake:

CHANGED

    LiquidCrystal &ptr = lcd;

TO

   Print &ptr = lcd;

</edit>

 

Last Edited: Tue. Dec 24, 2019 - 11:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, I guess we have to search out the LiquidCrystal files from Arduino-land, and then there is enough info. That thing already inherits the Print class, so-

 

static Print* PTR; // will be set to various char devices

PTR = &Serial;
PTR->println ("Hello there serial port"); // goes to the serial port

PTR = &LCD;
PTR->println ("Hello there LCD display"); // goes to the LCD

 

also note you cannot use a reference-

Print& PTR = Serial; //ok

PTR = LCD; //not ok, cannot reassign a reference

 

 

and the answer is already provided in #5, so sorry to #5 for repeating

Last Edited: Tue. Dec 24, 2019 - 10:57 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:

This is an example of the ability of C to use pointers to access functions. Its not commonly done, but there are plenty of tutorials out there, and it is discussed in the K&R book.

 

Um... Firstly, this is C++, not C. Secondly, this is an example of using pointers to access polymorphic objects, which is more than commonly done: its is extremely widely used in C++. This is basically the very foundation of C++ OOP.

Dessine-moi un mouton

Last Edited: Tue. Dec 24, 2019 - 11:21 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

N.Winterbottom wrote:

OP is on the right track

 

void LcdTest (void)
{
	LiquidCrystal lcd = LiquidCrystal();
	Print &ptr = lcd;

	ptr.print("Hello World");
}

 

The OP clearly wants a reassignable way to refer to a specific object. Which immediately puts C++ references out of the picture.

Dessine-moi un mouton