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

Long key press help / advice #55

Open
Electrik-rich546456 opened this issue Feb 24, 2021 · 1 comment
Open

Long key press help / advice #55

Electrik-rich546456 opened this issue Feb 24, 2021 · 1 comment

Comments

@Electrik-rich546456
Copy link

Hi I've been trying all sorts of different approaches to be able to differentiate between a long key press and a short one. I can't seem to be able to get it right as in working like i want.
This is the code i'm trying at the moment.

#!/usr/bin/env python3
import time
from rpi_rf import RFDevice
rfdevice = None
rfdevice = RFDevice(gpio=27)
rfdevice.enable_rx()
big_button = 3764961
rfdevice.rx_callback(gpio=27)
timestamp = None
# timestamp2 = time.perf_counter()
def rfff():
    print(str(rfdevice.rx_code) + " [pulselength " + str(rfdevice.rx_pulselength) +", protocol " + str(rfdevice.rx_proto) + "]")

while True:
    
#    rfff()
    if rfdevice.rx_code_timestamp != timestamp:
        timestamp = rfdevice.rx_code_timestamp
        command = (str(rfdevice.rx_code))
        t = time.time()
        if command == '3764961':
            print("yay")
            timestamp2 = time.perf_counter()
            elapsed =  timestamp - timestamp2
            time_taken = round(time.time() - t, 2) #rounding the long decimal float
            print(time_taken,'seconds')


#            print("elapsed" ,  elapsed)

    #    print("time" ,  timestamp)
    #    if ((str(rfdevice.rx_code) == '3764961')):
#            timestamp2 = rfdevice.rx_code_timestamp
    #        print("time 2" ,  timestamp2)
    #        elapsed =  timestamp - timestamp2
    #        print("elapsed" ,  elapsed)
    #        print(str(rfdevice.rx_code))

    time.sleep(0.50)

But the output is not helping

yay
0.0 seconds
yay
0.0 seconds

Why is it showing 0.0 ??
I want it to do something when Long hey press is detected
and something else when short key is pressed

@paf0186
Copy link

paf0186 commented Jan 7, 2022

The difference between a long and short keypress is in the number of times the code is repeated. It's just a repeated stream of the same code - there's no way to 'extend' how long a given code takes to send. How long a code takes to send is defined by the protocol and pulse length (the pulse length is the length of individual pulses).

If you think about it, what you're doing here is getting the last code from the receiver device, and you're measuring how long it's been since that was received. That of course doesn't help for a stream of repeats.

What you need to do is see how long the repeats last for - that means trying to pick up the first one, then looking for a longer gap indicating it's stopped and measuring that time. Something like a while loop with a very small sleep, checking the timestamp reported from the library.

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