I have a solution that seems to work for our purposes.
Thanks for all the suggestions and help. I am going to keep learning the PRUs because I think we’ll need them in a more sophisticated version of the product.
This is a great community and I appreciate the patience with this neophyte to BB.
Here’s the C code with some parts eliminated for simplicity.
// ********************* ReadDetectors **********************************
void ReadDetectors(int TimeToRead)
{
// initialize local variables
struct timeval tv_start; //start time
struct timeval tv_now; // current time
long total_elapsed_time_us;
long start_secs;
long last_secs;
long last_usecs;
long elapsed_us;
int KeepReading = 1;
gettimeofday(&tv_now,NULL);
start_secs = tv_now.tv_sec;
last_usecs = tv_now.tv_usec;
elapsed_us = 0;
total_elapsed_time_us = 0;
while (KeepReading)
{
// READ SENSORS CODE IS HERE. REMOVED FOR SIMPLICITY IN THE POST
// update elapsed time
gettimeofday(&tv_now,NULL);
if (tv_now.tv_sec >= last_usecs) // this if/else code handles the situation where the microsecond value crosses over zero between reads.
{
elapsed_us = tv_now.tv_usec - last_usecs;
}
else
{
elapsed_us = (1000000 - last_usecs) + tv_now.tv_usec;
}
total_elapsed_time_us = (tv_now.tv_sec - start_secs)*1000000 + elapsed_us; // this code adds microseconds for every second that has elapsed since the routine started. In our case, we won’t be in the routine more than 5 seconds ever.
if (total_elapsed_time_us > TimeToRead) KeepReading = 0;
}
}
}