Passing Encoder to library

Status
Not open for further replies.

APutz

Member
I am attempting to write a library that expands on the functions of the Encoder library that is included with teensyduino. Normally when I pass an object to a library I am writing I dont run into issues, but I seem to have a gap in my understanding. It seems as tho the compilier is expecting an argument for the encoder object, but ive already declared the object in my main code. Do I need to pass the pins as an argument to the library? I dont have to do it that way, thats just how ive always passed objects to a library. Thanks!

Code:
#ifndef _MYENCODER_h
#define _MYENCODER_h

#if defined(ARDUINO) && ARDUINO >= 100
	#include "arduino.h"
#else
	#include "WProgram.h"
#endif


#endif

#include "Encoder.h"

class myEncoder{
	
	public:
	
		myEncoder();
		void attach(Encoder encX);


	private:
	
		Encoder encX;
	
	};



Code:
#include "myEncoder.h"


myEncoder::myEncoder()


{}

void myEncoder::attach(Encoder encX){
	
	this->encX = encX;
	
}

Errors:

myEncoder.cpp: In constructor myEncoder::myEncoder()

myEncoder.cpp: 8:22: error: no matching function for call to 'Encoder::Encoder()
myEncoder*: myEncoder()
Error compiling project sources

myEncoder.h:15: In file included from
myEncoder.cpp:5: from
Encoder.h:72: note candidate Encoder Encoder(uint8_t, uint8_t)
Encoder(uint8_t pin1, uint8_t pin2) {
Encoder.h:72: note candidate expects 2 arguments, 0 provided
Encoder.h:69: note candidate constexpr Encoder Encoder(const Encoder&)
class Encoder
Encoder.h:69: note candidate expects 1 argument, 0 provided
Encoder.h:69: note candidate constexpr Encoder Encoder(Encoder&&)
Encoder.h:69: note candidate expects 1 argument, 0 provided
Build failed
 
If you want to embed an encoder object in your class you would do it like this:

Code:
class myEncoder
{
 public:
    myEncoder(unsigned A, unsigned B)
        : encX(A, B)                      // call the constructor of the embedded encoder
    { }

  int getValue()                          // do something with the encoder
  {
      return encX.read();
  }

 private:
    Encoder encX;
};

//------------------------------

myEncoder encoder(0, 1);  // define a myEncoder object

void setup()
{
}

void loop()
{
    Serial.println(encoder.getValue());    // use the myEncoder object
    delay(100);
}


If you want to pass an 'external' encoder object to your class you need to store either a reference or a pointer to the encoder object in your class. Since you don't pass the encoder object in your class constructor, I assume that you want to attach different encoders during the lifetime of your object. This is only possible by passing a pointer. (or the Encoder class provides a copy constructor / copy operator, which IIRC it doesn't, and wouldn't make much sense anyway)

Code:
#include "Encoder.h"

class myEncoder
{
 public:

    void attachEncoder(Encoder* encX)   //pass the address of the external encoder object to your class and store it in encX
    {
        this->encX = encX;
    }

  int getValue()
  {
      return encX->read();              // use -> instead of . since you now access the object through a pointer to its address
  }

 private:
    Encoder* encX;
};

//------------------------------

Encoder enc(0, 1);

myEncoder encoder;

void setup()
{
    encoder.attachEncoder(&enc);
}

void loop()
{
    Serial.println(encoder.getValue());
    delay(100);
}
 
Last edited:
Both solution are helpful in correcting my understanding. I will try them tomorrow when I get back to work. Thanks for your help!
 
Status
Not open for further replies.
Back
Top