- int clk[]={0,8,16,32,40,50,64,100,128};
- size_t idx = 40;
- uint8_t high=0;
- size_t cnt = 0;
- size_t highCnt = 0;
- size_t highCnt2 = 0;
- for (;idx < size; idx++){
- if (dest[idx]>128) {
- if (!high){
- high=1;
- if (cnt > highCnt){
- if (highCnt != 0) highCnt2 = highCnt;
- highCnt = cnt;
- } else if (cnt > highCnt2) {
- highCnt2 = cnt;
- }
- cnt=1;
- } else {
- cnt++;
- }
- } else if (dest[idx] <= 128){
- if (high) {
- high=0;
- if (cnt > highCnt) {
- if (highCnt != 0) highCnt2 = highCnt;
- highCnt = cnt;
- } else if (cnt > highCnt2) {
- highCnt2 = cnt;
- }
- cnt=1;
- } else {
- cnt++;
- }
- }
+ uint8_t fndClk[] = {8,16,32,40,50,64,128};
+ size_t startwave;
+ size_t i = 0;
+ size_t minClk = 255;
+ // get to first full low to prime loop and skip incomplete first pulse
+ while ((dest[i] < high) && (i < size))
+ ++i;
+ while ((dest[i] > low) && (i < size))
+ ++i;
+
+ // loop through all samples
+ while (i < size) {
+ // measure from low to low
+ while ((dest[i] > low) && (i < size))
+ ++i;
+ startwave= i;
+ while ((dest[i] < high) && (i < size))
+ ++i;
+ while ((dest[i] > low) && (i < size))
+ ++i;
+ //get minimum measured distance
+ if (i-startwave < minClk && i < size)
+ minClk = i - startwave;