Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi we are developing one application which involves in repeatedly check the pin status high / low. if high we should execute one function. is there any way to avoid long polling ? https://github.com/raspberry-sharp/raspberry-sharp-io/blob/master/Tests/Test.Gpio.WatchPin/Program.cs i got solution for single pin but no success with multiple pins can any one solve this issue ?
Posted

1 solution

Unfortunately, I did not work with this unit, but I know that you have to look for hardware interrupts and their handling. Here is what I found so far:
https://projects.drogon.net/raspberry-pi/wiringpi/functions[^] (see waitForInterrupt, wiringPiISR).

See also threading API.

Note: "The GPIO can be set to interrupt on a rising, falling or both edges of the incoming signal". Basically, you need to setup/enable interrupts on some external events on incoming signal. Then you should call waitForInterrupt in some thread which you can allow to block. This will be a blocking call which should put the thread in the wait state, which spends zero CPU time: switched out and not scheduled to execution until it is waken up by some event; one of such events is the interrupt you are waiting for. This way, you will avoid any polling and wast of any processor time.

You should also learn how to terminate the thread in this system even if no hardware events are invoked. But if you supply the external event, the thread will always pass the wait. Then you can cycle it and exit the thread execution simply by checking some "exit" flag you could set by your main (or other) thread.

Alternatively, you can supply a handler in the last parameter of wiringPiISR. This way, you can handle multiple pins calling this function several time, per each pin you need to handle. As the function passed accepts no parameters, you can have separate handler for each of the pins.

I know nothing about the CLR of Raspberry PI, but I hope you can use it and use available managed analogs of the functions I mentioned or can make them using P/Invoke.

—SA
 
Share this answer
 
v4
Comments
uma5b3 13-May-15 2:42am    
Could you please send me any code sample for C#.NET
Sergey Alexandrovich Kryukov 13-May-15 2:56am    
"C# .NET" should mean for Microsoft .NET. How could it help you?

As to the CLR implementation for Raspberry PI, I never used it, sorry. (What is it, MONO?) I never developed for Raspberry PI in any other way. I could write the code, but I could not test it, I don't have anything to try it out. I don't want to spend so much time and confuse you with something which might not work. But the implementation seems very obvious to me. I can try to answer your follow-up questions though.

Can you write the code yourself and come up with all problems you face? It might work.

—SA
uma5b3 13-May-15 3:05am    
Yes i'm writing an application for MONO on raspberry pi. I want to check the pin status whether it is high or low on multiple pins. Is there any way to avoid long polling ?
Sergey Alexandrovich Kryukov 13-May-15 3:11am    
I answered your question in full. I pointed out two ways where you not only avoid long polling, but eliminate any polling completely. Did you realize that?
—SA
uma5b3 13-May-15 3:10am    
if you are a c# developer you can understand this https://github.com/raspberry-sharp/raspberry-sharp-io/blob/master/Tests/Test.Gpio.WatchPin/Program.cs
that program is for monitoring single pin, and it is working fine. but i need to monitor multiple pins.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900