Tweak the Encoder class

Hi Sir/Fellow.

Good day.

Just a suggestions.

Can we be able to simplify the Encoder library
by just omitting the initialization of these functions
in the main program .

void doA(){encoder.handleA();} // tweak the Encoder class
void doB(){encoder.handleB();}

and just simply use "Encoder.xxx(), Encoder.yyy()

Thanks

/Christopher
Philippines

Hey christopher!

I have not found a easy and intuitive way to do it so far. If you have a suggestion hos to do it exactly I’d be happy to hear it. :grinning:

The issue is that the attachInterrupt function needs to receive a function that is not associated with any class. And then it needs to be either static member of a class or a standalone function.

  • The static function would only work for one encoder at the time.
  • and with the standalone function it is only possible to do if you create it after you created the encoder class. That means in the arduino sketch.

I don’t like it etteher, but for now that is the best solution I could find.:sweat_smile:

Can you elaborate a bit more your ideas?
Also why do you think this method at the moment is too complicated, is there a practical reason when this creates problems?

Hi Sir Anrun,

Good day!

Thanks for this information.

/Christopher

Unfortunately, there is no other way. Interrupts are called by hardware, and until the hardware provides a way to associate parameters with interrupts (i.e. the object reference on which to call the method), you can’t make a (non-static) method an interrupt handler. So there is nothing that can be done about this. The best we can do it provide a easy-to-call function in the class (which we have) which you then link to the interrupt handler in the initialisation code.

A slight optimisation can be to use lamda functions for doA and doB, but it is quite debatable wether this makes the code easier or harder to understand :smiley: .

Hi Sir Runger ,

Good day!

Actually, I have done the code already but am about to test it. I just get the idea
on the standard encoder library available in Arduino.

But this time its not a burden of priority, I’ll try to post it here once I am certain
its going to work for others. Yup somehow a slight optimization to the code’s function
of the encoder.

Thanks for your shared ideas Sir.

Thanks

/Christopher

On some platforms (stm32 and I think esp32), the attachInterrupt can (alternatively) take a std::function callback arg which allows you to bind a class instance method. This would mean that for example the encoder class could implement its own doA non-static method meaning that the main.ino wouldn’t have to implement this boiler plate code.

Unfortunately Uno has no such nicety. Not sure if implementing this for some architectures would cause confusion. I guess we could say that it only needs doing for some architectures.

Hi Sir Owen,

Good day!

Yes Sir, usually UNO will have a less use once more interrupts are required and the speed of the clock.

Thanks for the tips Sir!

/Christopher
Philippines