Hello, I'm trying to implement the pulseIn() function we can find in
arduino libraries into an external.
I couldn't make it work because some part doesn't seem to behave like I
expect so I'm wondering if it's caused by puredata process,
so maybe someone in the list could enlight me about that.
The purpose of the external is about using an ultrasound sensor
connected on gpio that require a microsecond timer for getting distance
like this:
//here is a custom timer using a struct provided by pcduino headers:
#include core.h
unsigned long pat_micros() {
struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_sec*(uint64_t)1000000+tv.tv_usec;
}
//here is the custom pulseIn() function:
unsigned long pat_pulseIn(t_pdpcduino *x, int pin)
{
unsigned long timeout = 100000;
unsigned long start = pat_micros();
while( pdpcduino_gpio_read2 (x, pin) == 1 )
if( pat_micros() - start > timeout )
return 0;
// this is where I'm getting a segmentation fault:
while ( pdpcduino_gpio_read2 (x, pin) == 0 )
if( pat_micros() - start > timeout )
return 0;
//...
unsigned long value = pat_micros();
while( pdpcduino_gpio_read2 (x, pin) == 1 )
if( pat_micros() - start > timeout )
return 0;
return pat_micros() - value;
}
//...
If I reduce the timeout number, segfault comes randomly after this
function has been triggered, so I'm wondering if this method is really
appropriate,
is there an exemple of external (containing such while loops) I could
get inspired from to write this pulseIn() function?