+//by marshmellow
+//biphase decode
+//take 01 or 10 = 0 and 11 or 00 = 1
+//takes 1 argument "offset" default = 0 if 1 it will shift the decode by one bit
+// since it is not like manchester and doesn't have an incorrect bit pattern we
+// cannot determine if our decode is correct or if it should be shifted by one bit
+// the argument offset allows us to manually shift if the output is incorrect
+// (better would be to demod and decode at the same time so we can distinguish large
+// width waves vs small width waves to help the decode positioning) or askbiphdemod
+int CmdBiphaseDecodeRaw(const char *Cmd)
+{
+ int i = 0;
+ int errCnt=0;
+ int bitnum=0;
+ int offset=0;
+ int high=0, low=0;
+ sscanf(Cmd, "%i", &offset);
+ uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
+ //get graphbuffer & high and low
+ for (;i<GraphTraceLen;++i){
+ if(GraphBuffer[i]>high)high=GraphBuffer[i];
+ else if(GraphBuffer[i]<low)low=GraphBuffer[i];
+ BitStream[i]=GraphBuffer[i];
+ }
+ if (high>1 || low <0){
+ PrintAndLog("Error: please raw demod the wave first then decode");
+ return 0;
+ }
+ bitnum=i;
+ errCnt=BiphaseRawDecode(BitStream,&bitnum, offset);
+ if (errCnt>=20){
+ PrintAndLog("Too many errors attempting to decode: %d",errCnt);
+ return 0;
+ }
+ PrintAndLog("Biphase Decoded using offset: %d - # errors:%d - data:",offset,errCnt);
+ printBitStream(BitStream,bitnum);
+ PrintAndLog("\nif bitstream does not look right try offset=1");
+ return 1;
+}
+
+