Multiple Callbacks (Or dedicated callbacks)



  • Hey guys,

    Is it possible to have multiple call backs for different events on an object?

    To clear up my question, I'll give two examples:

    1. Pin interrupts:
      As you know you can OR the events you are interested in for a Pin interrupt like this:
    p_in.callback(Pin.IRQ_FALLING | Pin.IRQ_RISING,  button_handler)
    

    You can not (to my knowledge) have different event handlers for different triggers for example. This might be due to the limitations on interrupts available (is that right?!)
    It doesn't matter how many times you assign the callback, the last one will have any effects.
    In this case, you can actually read the Pin's value and you might get away with the triggers' difference.

    But,

    1. BLE Characteristic callback:

    Currently there are 2 event types: CHAR_READ_EVENT and CHAR_WRITE_EVENT. You can't practically discern these 2 events (even if OR together). If you try to assign two different callbacks, the last one will be assigned only. Now imagine we have other (required) events like Subscribe!

    Am I missing something? Is there a trick to it that I don't know? Granted, I know very little of Python, but I'm an experienced software developer and find this callback assignment pattern a bit strange.

    Now the solution I could imagine is to have dedicated callbacks for BLE which has a limited set of event types and ORing events doesn't make sense at all with a simple syntax which could be disabled by assigning None to it:

    #to enable:
    ch1.read_callback(ch_read_handler)
    ch1.write_callback(ch_write_handler)
    
    #to disable:
    ch1.read_callback(None)
    ch1.write_callback(None)
    
    

    Cheers,
    M.


Log in to reply
 

Pycom on Twitter