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

We get fake long pressed buttons when conect device to PI. #5

Closed
shuricksumy opened this issue Jun 8, 2015 · 3 comments
Closed

We get fake long pressed buttons when conect device to PI. #5

shuricksumy opened this issue Jun 8, 2015 · 3 comments

Comments

@shuricksumy
Copy link

Hi,
on my PI I very often get fake long pressed buttons. It happens when I connect devices, like usb flash drive. I analyzed issue. To my mind, the main reason is electromagnetic interference. The signal causes GPIO.add_event_detect and after that very quickly set GPIO.input(channel) == 1. The self.down_time_previous has previos value (from last call of function) - as result we get long press button. I changed code (ex: volume down button):

longpress_time = 1
longpress_time2 = 5

def vol_down(self, channel):
if GPIO.input(channel) == 1:
if self.down_time_vol_down + longpress_time > time.time():
self.frontend.input({'key': 'volume_down', 'long': False})
elif self.down_time_vol_down + longpress_time2 > time.time():
self.frontend.input({'key': 'volume_down', 'long': True})
else:
self.down_time_vol_down = time.time()

Now I don't get fake long pressed buttons. Please, check it and fix.

@ghost
Copy link

ghost commented Jun 9, 2015

Please next time use this for commenting code as in python indentation is essential:

Your code

This is how I am interpreting your code (Tell me if I am interpreting wrong):

longpress_time = 1
longpress_time2 = 5

def vol_down(self, channel):
    if GPIO.input(channel) == 1:
        if self.down_time_vol_down + longpress_time > time.time():
            self.frontend.input({'key': 'volume_down', 'long': False})
        elif self.down_time_vol_down + longpress_time2 > time.time():
            self.frontend.input({'key': 'volume_down', 'long': True})
    else:
        self.down_time_vol_down = time.time()

I am not able to reproduce the error as I am using a powered USB hub. Without the hub the pi reboots when I insert a USB.

So to be sure if I understood this correctly. The problem is that when you plug a USB, GPIO value changes so fast that GPIO.input(channel) is never 0 and down_time is not updated. So as the down time is a old value longpress is triggered.

To fix that your idea is that it should be a max time (5 seconds in your example) for a button press and that we should ignore that press as it will probably be a false positive.

Another fix I think of could be to check if the time has been updated:

def vol_down(self, channel):
    if GPIO.input(channel) == 1:
        if self.down_time_vol_down >= 0:
            if self.down_time_vol_down + longpress_time > time.time():
                self.frontend.input({'key': 'volume_down', 'long': False})
            else:
                self.frontend.input({'key': 'volume_down', 'long': True})
        self.down_time_vol_down = -1
    else:
        self.down_time_vol_down = time.time()

Please try this code and reply if it fixes or not.

Thanks for your help in making mopidy-ttspio better!

@shuricksumy
Copy link
Author

Hi,
you interpreted correctly my code. I checked your code. It worked good. But when we start mopidy the variables are declared with value = 0. And we get fake pressed buttons only one time. So we should use next:

    self.down_time_previous = -1
    self.down_time_next = -1
    self.down_time_main = -1
    self.down_time_vol_up = -1
    self.down_time_vol_down = -1

Thanks for your help too.

@ghost ghost closed this as completed in 1d2b01e Jun 12, 2015
@ghost
Copy link

ghost commented Jun 12, 2015

Thanks for the feedback!

The fix will be included in next release

This issue was closed.
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

1 participant