RPi GPIO sampling faster than 60Hz

Hi! Not sure how trivial my problem/question is (maybe this should be moved to “beginners”?). I would appreciate any help.

What I have is RPi4 (running Lite system and oF 0.11.2), one of the GPIO ports is connected to a currency validator (coin/token/bill acceptor). Once a valid coin is inserted, the device will send a 100ms signal.

For security reasons I need to make sure that the signal duration is with 10% tolerance (so it needs to be between 90ms and 110ms). Signal length 89ms and shorter or 111ms and longer should be detected as invalid. So I need a precise way to measure signal duration.

I assume I need to monitor GPIO ports outside the main ofApp::update() loop (which is 60fps/Hz). In a separate thread? Any help with measuring signal length at for example 250Hz?

Hi! Just some ideas of how I think I would do it.

First of all, do the sampling in a thread. Create a class like this

class MySampler : public ofThread
{
public:
    void threadedFunction();
}

and in the threadedFunction() keep a small state machine where you time the signals using

ofGetElapsedTimeMicros()

The threadedFunction() should look like this

void MySampler::threadedFunction()
{
    while(isThreadRunning())
        // do the work;
}

In your ofApp::Setup() you start the thread

mysampler.startThread();

Hope this helps, I can elaborate further if you wish and if it is of any help.