kytkija
Posts: 13
Joined: Fri Feb 15, 2019 1:47 pm
Location: Finland

Reading several inputs for same counter

Thu May 02, 2019 7:34 am

Hello all,

Could someone give me directions, advices or examples on following;

I have created a Python program that is counting pulses from inputs and showing it to user via Tkinter GUI. Pulses are 1< sec long. The pulses should be increasing the same global counter.

It is working for me now, however, the device is missing pulses even when I'm using only one input. At the moment I am (atleast I think so) running the counter functions in another thread in parallel to main thread.

When I try to read 5 inputs for same counter, I just use the GPIOZERO when_pressed function, which calls the function that increments the counter. Below you see what I have running in the parallel thread (readbutton() function is started in parallel thread)

Code: Select all

def readbutton():
    
    def increment():
        
        global counter
        if is_transfer == False:
            counter = counter+1
            print(counter)
        else:
            pass
        
    while True:
        nappi.when_pressed = increment
        nappi2.when_pressed = increment
        nappi3.when_pressed = increment
        nappi4.when_pressed = increment
        nappi5.when_pressed = increment
The "is_transfer" is a variable that is set to true while data is being transferred to MySQL database. I did that because I didn't understand how to use the thread locks, buffers, queues etc. It is allright if I loose a pulse while the transfer function is running, but it is not the case that bothers me. These signals can be on at the same time, but the program should have time to iterate the increment function for each button within the pulse length?

When I follow the counter value from GUI or terminal, the counter sometimes freezes - I mean, that it doesnt count for certain button push even if I hold the button for a while. But after I release button and press it again, it starts counting again.

Anyt tips what to look for, what would be the correct solution to read 5 simultaneous inputs to same counter? If you have a example I would be glad. This is just part of bigger code, but very important to get it straight.

kytkija
Posts: 13
Joined: Fri Feb 15, 2019 1:47 pm
Location: Finland

Re: Reading several inputs for same counter

Thu May 02, 2019 9:20 am

And another question;

When I run the readbutton() function in second thread where I call the nested function increment( ), will the increment -function run on the parallel thread that was started before or will it pop up in the main thread?

Best regards
Kytkija

pfletch101
Posts: 623
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

Re: Reading several inputs for same counter

Thu May 02, 2019 3:46 pm

In general, if you are dealing with multiple asynchronous events (your button presses in this case) and want to record each one without interference from any of the others, you would need to have a separate subprocess (not just a separate thread) for each event, to work around Python's limitation to having a single thread in a process active at any one time. Also, if you are going to have a single response function called from multiple asynchronous event detection functions, you need to write it in a way that allows this - perhaps by using a semaphore to protect it from problems caused by 're-entrancy'.

kytkija
Posts: 13
Joined: Fri Feb 15, 2019 1:47 pm
Location: Finland

Re: Reading several inputs for same counter

Fri May 03, 2019 6:03 am

pfletch101 wrote:
Thu May 02, 2019 3:46 pm
In general, if you are dealing with multiple asynchronous events (your button presses in this case) and want to record each one without interference from any of the others, you would need to have a separate subprocess (not just a separate thread) for each event, to work around Python's limitation to having a single thread in a process active at any one time. Also, if you are going to have a single response function called from multiple asynchronous event detection functions, you need to write it in a way that allows this - perhaps by using a semaphore to protect it from problems caused by 're-entrancy'.
Wow. Sounds like overkill for such a simple sounding task, though I understand very well what this is all about. Just that I can not do it or have the time to learn all needed fundamendals for this…

I will try to find someone to help me with the Program (for money), and maybe in the next life I will become a programmer :D

Best regards
Kytkija

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Reading several inputs for same counter

Fri May 03, 2019 8:35 am

A pigpio method.

Code: Select all

#!/usr/bin/env python

import pigpio
import time

GPIO=[7, 8, 9, 10, 11]
cb_list=[]
counter=0

def callback(g, l, t):
   global counter
   counter += 1
   print(g, counter)

pi = pigpio.pi()
if not pi.connected:
   exit()

for g in GPIO:
   cb_list.append(pi.callback(g, pigpio.RISING_EDGE, callback))

time.sleep(60)

for cb in cb_list:
   cb.cancel()

pi.stop()         

kytkija
Posts: 13
Joined: Fri Feb 15, 2019 1:47 pm
Location: Finland

Re: Reading several inputs for same counter

Fri May 03, 2019 8:32 pm

joan wrote:
Fri May 03, 2019 8:35 am
A pigpio method.

Code: Select all

#!/usr/bin/env python

import pigpio
import time

GPIO=[7, 8, 9, 10, 11]
cb_list=[]
counter=0

def callback(g, l, t):
   global counter
   counter += 1
   print(g, counter)

pi = pigpio.pi()
if not pi.connected:
   exit()

for g in GPIO:
   cb_list.append(pi.callback(g, pigpio.RISING_EDGE, callback))

time.sleep(60)

for cb in cb_list:
   cb.cancel()

pi.stop()         
Thank you thousand Times! This seems promising and simple. I will google and test it to understand it next week, but could you refer quickly

- why is the sleep? 60 seconds?
- should i run some parts of the code in while loop to Make the gpio surveillance endless? The for loop?
- can i use this in main thread?

I am running Tkinter mainloop and screen update etc with the tkinter's after function if it matters.. (at least makes everything more complicated).

kytkija
Posts: 13
Joined: Fri Feb 15, 2019 1:47 pm
Location: Finland

Re: Reading several inputs for same counter

Sat May 04, 2019 6:17 pm

After first testings, with 600 signals generated by switches I got 0% loss. This solution seems to work, Thank you very much!

PIGPIO seems so interesting, so I start learning more about it.

Best regards
kytkija

Return to “Beginners”