c = rgData.length var pA = 0 // previos encoder A value var iA = 0 // last encoder A rising edge var cnt = 0 // starting step count for(var i = 0; i < c; i++){ fA = 1&(rgData[i]<<0) // live encoder-A signal on channel DIO = 0 fB = 1&(rgData[i]<<1) // live encoder-B signal on channel DIO = 1 if(fA==1 && pA==0){ // encoder A rising edge (A_now = 1 && A_previous = 0) if(fB==0){ // encoder B level at time of A-rising edge indicates direction cnt++ // increase counter by 1 //cnt = 1 // should see "1"s in one direction & "0"s in other direction } else if(fB==1){ // encoder B level at time of A-rising edge indicates direction cnt-- // decrease counter by 1 //cnt = 0 // should see "1"s in one direction & "0"s in other direction } for(var j = iA; j < i; j++){ // assign value (step count) to previous A cycle rgValue[j] = cnt rgFlag[j] = 1 } iA = i // update last encoder A rising edge index value (i) } pA = fA // update previous encoder A value with current encoder A value } for(var j = iA; j < c; j++){ // fill the remaining samples rgValue[j] = cnt rgFlag[j] = 1 }