Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the library event driven with function callbacks #7

Open
codebeat-nl opened this issue Jan 23, 2019 · 5 comments
Open

Make the library event driven with function callbacks #7

codebeat-nl opened this issue Jan 23, 2019 · 5 comments

Comments

@codebeat-nl
Copy link

codebeat-nl commented Jan 23, 2019

Because it is now a more advanced product and you can use more than one, make it event driven. Hopefully you understand the idea and why this would be nice. It makes it easier to handle more objects at once and avoids a if then else mess (structorize the code).

for example:

#define MAX_ENCODERS 5
#define ENC_ID_CHANNEL1 0x30
#define ENC_ID_CHANNEL2 0x31
#define ENC_ID_CHANNEL3 0x32
#define ENC_ID_CHANNEL4 0x33
#define ENC_ID_CHANNEL5 0x34


i2cEncoderLibV2* encoders[MAX_ENCODERS];

// Just an example event handler: 
void myRotaryEncoderChangeEvent(  i2cEncoderLibV2* opSender, int8_t iValue )
{
  Serial.print( opSender->addres );
  Serial.print( "has been changed to " );
  Serial.println( iValue );

 if( opSender->addres == ENC_ID_CHANNEL3 )
 {
   // do something nice 
 }
}

void setup()
{ 
  uint8_t i = MAX_ENCODERS;
  while( i-- )
  {
    encoders[i]= new i2cEncoderLibV2(ENC_ID_CHANNEL1+i);
    encoders[i]->onEncoderChange = myRotaryEncoderChangeEvent;
  }  
}

void loop()
{
  uint8_t i = MAX_ENCODERS;
  while( i-- )
  {
     // if something has been changed, it triggers an event if assigned
     encoders[i]->update();
  }  
}


--------------------





@Fattoresaimon
Copy link
Owner

Hi,
Can be a nice idea,
When i will have more free time, i would definitely try.
Thank you
Simone

@Fattoresaimon
Copy link
Owner

Hi,
I have followed your suggestion.
Here the result: https://github.com/Fattoresaimon/ArduinoDuPPaLib

@codebeat-nl
Copy link
Author

codebeat-nl commented Mar 5, 2019

Hi,
Nice, great, however, why you call the event and handlers at some points interrupts? In your code of attachInterrupt() you assign a function to an event array (it is called Events). On update, you poll the encoder and fire events if assigned. Why is it not called attachEvent() ( and detachEvent() )? An interrupt is really something else, this is confusing.

Another thing is the amount of events and the type of events. For example ENCODER_INCREMENT and ENCODER_DECREMENT could be one event type, for example onChange. Now you have to specify two eventhandlers for practically the same - the encoder 'wiper' changed. This could be something like onChange( obj, direction ), for example negative (steps) when turned left and positive (steps) when turned right.

Also the assignment of events could be more user friendly and less prone to errors (for example using the wrong constants, there is also no range check in the attachInterrupt function!). For example each encoder provides these simple event properties:

- onChange
- onButtonClick
- onButtonDown / onButtonPress 
- onButtonUp / onButtonRelease 
- onButtonDoubleClick
- onButtonHold
- onMinMaxRange   // Do not know why you want to know this.

So you can do, to assign an event handler:

encoder.onChange = evMyEncoderChange;
encoder.onButtonClick = evMyEncoderButtonClick;

This naming convention is more clear, nicer, easier and also very similar to other languages and you don't need to call a function to assign a handler. To remove the handler, simply assign NULL.

@Fattoresaimon
Copy link
Owner

Thank you a lot for the suggestion!

there is also no range check in the attachInterrupt function!

Yeah i have noticed that huge mistake, i will fix.
I think i will follow (again 😅) your suggestions!

@Fattoresaimon
Copy link
Owner

Hello,
I have made an update following your suggestions.
Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants