50ButtonsEach / fliclib-linux-hci

Flic SDK for Linux
306 stars 54 forks source link

Handling ButtonHold correctly #29

Closed s0riak closed 8 years ago

s0riak commented 8 years ago

I used the python clientlib to integrate flic buttons and got an issue with the events passed to channel.on_button_single_or_double_click_or_hold When a Button is hold down, the callback is called with a type fliclib.ClickType.ButtonHold but when the button is released an additional call with fliclib.ClickType.ButtonSingleClick is fired. This requires me to have some timing logic which is rather unstable to filter those single clicks. I would like to have callbacks on a highlevel:

  1. Single click
  2. Double click
  3. Button Hold (without any other event fired on release)

and low level callbacks

  1. Button Down
  2. Button Up (incl. the Time the button was pressed)

My question do I get the API wrong or is this the way the current implementation works? In the latter case I think it would be an improvement to have the callbacks as described.

By the way, very easy to use lib. Worked great out of the box.

Emill commented 8 years ago

I couldn't reproduce what you describe. I used this code:

import fliclib

client = fliclib.FlicClient("localhost")

def got_button(bd_addr):
    cc = fliclib.ButtonConnectionChannel(bd_addr)
    cc.on_button_single_or_double_click_or_hold = \
        lambda channel, click_type, was_queued, time_diff: \
            print(channel.bd_addr + " " + str(click_type))
    cc.on_connection_status_changed = \
        lambda channel, connection_status, disconnect_reason: \
            print(channel.bd_addr + " " + str(connection_status) + (" " + str(disconnect_reason) if connection_status == fliclib.ConnectionStatus.Disconnected else ""))
    client.add_connection_channel(cc)

def got_info(items):
    print(items)
    for bd_addr in items["bd_addr_of_verified_buttons"]:
        got_button(bd_addr)

client.get_info(got_info)

client.on_new_verified_button = got_button

client.handle_events()

and it prints only ClickType.ButtonHold when I hold the button.

The addition of time the button was pressed to the "low level callbacks" would be nice but won't work with the current protocol between Flic and the host since the time is simply not included. You could have your own timestamp you set when a button down event is received and later compared when the button up event is received, however latency over BLE might make that not that accurate when the reception is bad. Is there a particular reason you want this and think the click/hold, single/double click, single/double click/hold events are not enough?

s0riak commented 8 years ago

@Emill thanks for your rapid reply and the hit to the sample. I figured out what my issue was, I registered all callbacks not only on_button_single_or_double_click_or_hold and thus all were calling the same method. Sorry for wasting your time. Concerning the "low level callbacks", this would enable using different click patterns for diffent actions. I understand the latency issues and would implement it in the client side if needed.