In this code, the error is passed through to the rest of the app using an 'error' flag set, with zero values for the channel data. The error flag could be displayed as a red line on an oscilloscope display, for example. The code auto-detects the packet counter range, and behaves correctly even before it has completely established that range. It also handles the case where no counter is used and the field contains a constant value.
Pseudo-code for the input loop looks like this:
packet= get_next_packet(); pc= packet->counter; min= pc; max= pc; prev= pc; while (packet= get_next_packet()) { pc= packet->counter; if (pc < min) min= pc; if (pc > max) max= pc; next= (prev == max) ? min : prev+1; while (pc != next) { // Fill in a missing packet with an error packet packet2->chans[...]= 0; packet2->error= 1; process(packet2); prev= next; next= (prev == max) ? min : prev+1; } // Everything is fine packet->error= 0; process(packet); prev= pc; }
For output, if you are reading an EEG hardware device, you can just pass the raw packet counts through, and expect apps to deal with it.
However, if you have error flags on pre-processed packets, then the only way to pass that info through is to keep the packet counter going, but just omit the error packets. This allows the error to be detected correctly by aware apps, but has the disadvantage that unaware apps will squeeze out that error time-gap as if it didn't exist.
For example:
pc= 0; while (packet= get_next_packet_to_output()) { packet->count= pc; pc++; pc &= 255; // Increase counter in any case if (!packet->error) output(packet); }