Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Multiple Callbacks (Or dedicated callbacks)

    Scheduled Pinned Locked Moved WiPy 2&3
    1 Posts 1 Posters 828 Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M Offline
      mohpor
      last edited by mohpor

      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.

      1 Reply Last reply Reply Quote 1

      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

      With your input, this post could be even better 💗

      Register Login
      • 1 / 1
      • First post
        Last post

      Pycom on Twitter

      Pycom