I want to generate square-signals with a first generation RPI's gpio output.
For this purpose I first wanted to use wiringPi.
Code language is fixed, shall be C or C++.
As per wiringPi's documentation for the blink example, solution should be easy:
#include <wiringPi.h>
int main (void)
{
wiringPiSetup () ;
pinMode (0, OUTPUT) ;
for (;;)
{
digitalWrite (0, LOW) ; delay (500) ;
digitalWrite (0, HIGH) ; delay (500) ;
}
return 0 ;
}
But I want to have ~600 microsecond pauses between them.
Therefore I've created an other delay method:
void myDelay(long int usec) {
struct timespec ts, rem;
ts.tv_sec = 0;
ts.tv_nsec = usec * 1000;
while (clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &rem)) {
ts = rem;
}
}
Then I switched the 2 delay(500) to myDelay(600).
This mostly works, however sometimes myDelay waits more than 600 microseconds.
How can I have exactly the same squares with C/C++?
I also tried a Python script with pigpio:
pi = pigpio.pi()
pi.wave_add_new()
pi.set_mode(1, pigpio.OUTPUT)
wf=[]
for i in range (0, 100):
wf.append(pigpio.pulse(0, 1<<1, 600))
wf.append(pigpio.pulse(1<<1, 0, 600))
wf.append(pigpio.pulse(0, 1<<1, 1000))
pi.wave_add_generic(wf)
wid = pi.wave_create()
pi.wave_send_once(wid)
while pi.wave_tx_busy():
pass
pi.wave_delete(wid)
pi.stop()
And this python gives the intended result (i.e.: all squares are equal on scope).
Now the question is, how can I achieve the same result with pure C/C++ implementation (without having to mess with gpioWave* functions)?