0

all.

I'm attempting to write a class that inherits from the Print class. I only need serial output.

However, I'm having difficulty locating any reference on how to specify which pin on the ATMega my class will use as output of the write(...) functions.

How does the Print class know where to send output to ??

Thanks in advance.

asked Jan 30, 2017 at 0:18

2 Answers 2

1

You need to do far more than just "specify a pin". You need to write the software that transmits data through that pin in your chosen format.

You cannot send data through a pin. You can only set a pin HIGH or set a pin LOW. It is the sequence (and most importantly the timing) of setting a pin HIGH and LOW that constitutes sending data.

So you have to write the software that sets the pin HIGH and LOW in the right way and at the right time.

Fortunately sending is far simpler than receiving, as you can see if you look at the source code to SoftwareSerial.

Specifying the pin itself though is simple:

class MyClass : public Print {
 private:
 uint8_t _pin;
 public:
 MyClass(uint8_t pin);
 size_t write(uint8_t data);
};
MyClass::MyClass(uint8_t pin) {
 _pin = pin;
}
size_t MyClass::write(uint8_t data) {
 // In here you do whatever you have to in order
 // to manipulate _pin in the right way to send your data.
 // Don't forget to return the number of bytes written (there
 // is only one, so either 1 for "it got sent" or 0 for "it
 // didn't get sent".
 return 1;
}

It can be quite useful to have a begin() function for setting things up, such as configuring the right pinMode() for _pin, and maybe setting a baud rate like Serial does (although for simplicity you may choose to just have a single fixed baud rate, which means you can pre-calculate delay loops etc).

answered Jan 30, 2017 at 10:36
1
  • It seems I got past the "brain block"... Just had to walk away from it for a while and the procedure just came to me. Thanks to all. Commented Jan 30, 2017 at 12:02
0

The parent class (your class, in this case) provides read() and write() functions which in turn, know what ports to use. Presumably those were provided to the parent class' constructor. Now the Print class just has to know the standard function names.

answered Jan 30, 2017 at 0:50
3
  • Thank you, JRobert. Apparently, it's specifying the "specifying which pin to use in my class' constructor" that's causing my "obvious brain block". I don't want to use the hardware serial so I can leave normal TX and RX available for re-programming. I tried inheriting from SoftwareSerial, but it apparently requires both TX and RX to be specified, and I only need Commented Jan 30, 2017 at 1:59
  • I only need my class to send data (not receive). I hope I stated that clear enough. Commented Jan 30, 2017 at 2:07
  • It won't hurt to implement the read function anyway; make it an empty one if you wish. But I'd expect Print would refer to it (statically) and you'd get an undefined function error at compile time if no such function is present. Commented Jan 30, 2017 at 15:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.