+void getNextLow(uint8_t samples[], size_t size, int low, int *i) {
+ while ((samples[*i] > low) && (*i < size))
+ *i+=1;
+}
+
+void getNextHigh(uint8_t samples[], size_t size, int high, int *i) {
+ while ((samples[*i] < high) && (*i < size))
+ *i+=1;
+}
+
+// load wave counters
+bool loadWaveCounters(uint8_t samples[], size_t size, int lowToLowWaveLen[], int highToLowWaveLen[], int *waveCnt, int *skip, int *minClk, int *high, int *low) {
+ int i=0, start, waveStart;
+ size_t testsize = (size < 512) ? size : 512;
+
+ if ( getHiLo(samples, testsize, high, low, 80, 80) == -1 ) {
+ if (g_debugMode==2) prnt("DEBUG STT: just noise detected - quitting");
+ return false; //just noise
+ }
+
+ // get to first full low to prime loop and skip incomplete first pulse
+ getNextHigh(samples, size, *high, &i);
+ getNextLow(samples, size, *low, &i);
+ *skip = i;
+
+ // populate tmpbuff buffer with pulse lengths
+ while (i < size) {
+ // measure from low to low
+ getNextLow(samples, size, *low, &i);
+ start = i;
+
+ //find first high point for this wave
+ getNextHigh(samples, size, *high, &i);
+ waveStart = i;
+
+ getNextLow(samples, size, *low, &i);
+
+ if (*waveCnt >= (size/32))
+ break;
+
+ highToLowWaveLen[*waveCnt] = i - waveStart; //first high to first low
+ lowToLowWaveLen[*waveCnt] = i - start;
+ *waveCnt += 1;
+ if (i-start < *minClk && i < size) {
+ *minClk = i - start;
+ }
+ }
+ return true;
+}
+