luni64 / EncoderTool

The EncoderTool is a library to manage and read out rotary encoders connected either directly or via multiplexers to ARM based boards. Encoder push buttons are supported. Callback functions can be attached to encoder changes and button presses to allow for event driven applications
MIT License
47 stars 11 forks source link

Push button support for Multiplexed encoders #31

Open Cinezaster opened 1 year ago

Cinezaster commented 1 year ago

Am I correct to assume their is no push button support for Multiplexed encoders? Is it in the pipeline?

luni64 commented 1 year ago

Actually there is support for push buttons. See e.g. (line 15): https://github.com/luni64/EncoderTool/blob/decf3e781214c33aa57cc1fd14a2df741ed97e7d/src/Multiplexed/EncPlex74165.h#L11-L23 Which mulitplexing scheme are you aiming for?

Cinezaster commented 1 year ago

I'm using the 4051, I did not look into the other multiplexer codes. https://github.com/luni64/EncoderTool/blob/master/src/Multiplexed/EncPlex4051.h I might implement it myself. My project has way more IO's to deal with so, I'm not sure if I'm going to keep your awesome library. In any case thx for your project and your quick response.

luni64 commented 1 year ago

In case you add the push button (which should be simple) to the 4051 example I'd be happy to update it.

I'm not sure if I'm going to keep your awesome library

Thanks! Let me know if you need any help or "decission support" :-)

hakonbraga commented 1 year ago

Hi, I can see that the schematics for the 74165 mux has a extra chip for the push buttons, but in the example code you don't use the buttons. Could you add that to the example or tell me what is the best way to implement it like in the schematics?

serlecu commented 1 year ago

Looking inside src, it looks like you only need to add an extra argument to the EncPlex74165 :

https://github.com/luni64/EncoderTool/blob/master/src/Multiplexed/EncPlex74165.h

inline EncPlex74165_tpl(unsigned nrOfEncoders, unsigned pinLD, unsigned pinCLK, unsigned pinA, unsigned pinB, unsigned pinBtn = -1);

So you give the QH pin of the third encoder (pushButtons) aspinBtn argument value.

Then you could use set the callbacks with attachButtonCallback(encBtnCallback_t) as it appears in EncoderBase.h line 35 what will return (uint_fast8_t channel, int_fast8_t state)to your callback function as it appears in EncPlexBase.h line 17

it is also supposed to be analogous to lines 41 and 41 of the 74165 Multiplexed example:

 encoders[0].attachCallback(onFirstEncoder); // standard callback
 encoders.attachCallback(onAnyEncoder);

I might guess it turns into:

encoders[0].attachButtonCallback(onFirstEncoderBtn); // standard callback
encoders.attachButtonCallback(onAnyEncoderBtn);

I'll test it soon

serlecu commented 1 year ago

@hakonbraga just tested and somehow Arduino IDE cant find attachButtonCallback() for using it with the whole array of encoders, but you can still assign individual callbacks like this:

constexpr unsigned QH_A = 2;   // output pin QH of shift register A
constexpr unsigned QH_B = 3;   // output pin QH of shift register B
constexpr unsigned QH_S = 4;   // output pin QH of shift register S
constexpr unsigned pinLD = 5;  // load pin for all shift registers)
constexpr unsigned pinCLK = 6; // clock pin for all shift registers

EncPlex74165 encoders(encoderCount, pinLD, pinCLK, QH_A, QH_B, QH_S);

void onFirstEncoderBtn(int state){}
void onSecondEncoderBtn(int state){}

void setup() {
...
encoders[0].attachButtonCallback(onFirstEncoderBtn);
encoders[1].attachButtonCallback(onSecondEncoderBtn);
...
}

void loop() {
    encoders.tick();
}