+void printBitStream(uint8_t BitStream[], uint32_t bitLen){
+ uint32_t i = 0;
+ if (bitLen<16) return;
+ if (bitLen>512) bitLen=512;
+ for (i = 0; i < (bitLen-16); i+=16) {
+ PrintAndLog("%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i",
+ BitStream[i],
+ BitStream[i+1],
+ BitStream[i+2],
+ BitStream[i+3],
+ BitStream[i+4],
+ BitStream[i+5],
+ BitStream[i+6],
+ BitStream[i+7],
+ BitStream[i+8],
+ BitStream[i+9],
+ BitStream[i+10],
+ BitStream[i+11],
+ BitStream[i+12],
+ BitStream[i+13],
+ BitStream[i+14],
+ BitStream[i+15]);
+ }
+ return;
+}
+
+//by marshmellow
+//takes 2 arguments - clock and invert both as integers
+//prints binary found and saves in graphbuffer for further commands
+int Cmdaskrawdemod(const char *Cmd)
+{
+ uint32_t i;
+ int invert=0; //invert default
+ int high = 0, low = 0;
+ int clk=64; //clock default
+ uint8_t BitStream[MAX_GRAPH_TRACE_LEN] = {0};
+ sscanf(Cmd, "%i %i", &clk, &invert);
+ if (!(clk>8)){
+ PrintAndLog("Invalid argument: %s",Cmd);
+ return 0;
+ }
+ if (invert != 0 && invert != 1) {
+ PrintAndLog("Invalid argument: %s", Cmd);
+ return 0;
+ }
+ uint32_t initLoopMax = 1000;
+ if (initLoopMax>GraphTraceLen) initLoopMax=GraphTraceLen;
+ // Detect high and lows
+ PrintAndLog("Using Clock: %d and invert=%d",clk,invert);
+ for (i = 0; i < initLoopMax; ++i) //1000 samples should be plenty to find high and low values
+ {
+ if (GraphBuffer[i] > high)
+ high = GraphBuffer[i];
+ else if (GraphBuffer[i] < low)
+ low = GraphBuffer[i];
+ }
+ if ((high < 30) && ((high !=1)||(low !=-1))){ //throw away static - allow 1 and -1 (in case of threshold command first)
+ PrintAndLog("no data found");
+ return 0;
+ }
+ //13% fuzz in case highs and lows aren't clipped [marshmellow]
+ high=(int)(0.75*high);
+ low=(int)(0.75*low);
+
+ //PrintAndLog("valid high: %d - valid low: %d",high,low);
+ int lastBit = 0; //set first clock check
+ uint32_t bitnum = 0; //output counter
+ uint8_t tol = 32;//clock tolerance adjust
+ uint32_t iii = 0;
+ uint32_t gLen = GraphTraceLen;
+ if (gLen > 500) gLen=500;
+ uint8_t errCnt =0;
+
+ //PrintAndLog("lastbit - %d",lastBit);
+
+ //loop to find first wave that works
+ for (iii=0; iii < gLen; ++iii){
+ if ((GraphBuffer[iii]>=high)||(GraphBuffer[iii]<=low)){
+ lastBit=iii-clk;
+ //loop through to see if this start location works
+ for (i = iii; i < GraphTraceLen; ++i) {
+ if ((GraphBuffer[i] >= high) && ((i-lastBit)>(clk-((int)clk/tol)))) { // && GraphBuffer[i-1] < high
+ lastBit+=clk;
+ BitStream[bitnum] = invert;
+ bitnum++;
+ } else if ((GraphBuffer[i] <= low) && ((i-lastBit)>(clk-((int)clk/tol)))){
+ //low found and we are expecting a bar
+ lastBit+=clk;
+ BitStream[bitnum] = 1-invert;
+ bitnum++;
+ } else {
+ //mid value found or no bar supposed to be here
+ if ((i-lastBit)>(clk+((int)(clk/tol)))){
+ //should have hit a high or low based on clock!!
+
+ /*
+ //debug
+ PrintAndLog("no wave in expected area - location: %d, expected: %d-%d, lastBit: %d - resetting search",i,(lastBit+(clk-((int)(clk/tol)))),(lastBit+(clk+((int)(clk/tol)))),lastBit);
+ if (bitnum > 0){
+ BitStream[bitnum]=77;
+ bitnum++;
+ }
+ */
+
+ errCnt++;
+ lastBit+=clk;//skip over until hit too many errors
+ if (errCnt>((GraphTraceLen/1000)*2)){ //allow 2 errors for every 1000 samples else start over
+ errCnt=0;
+ bitnum=0;//start over
+ break;
+ }
+ }
+ }
+ }
+ /*
+ //debug
+ if ((bitnum>64) && (BitStream[bitnum-1]!=77)) break;
+ */
+ }
+ }
+ ClearGraph(0);
+ //move BitStream back to GraphBuffer
+ for (i=0; i < bitnum; ++i){
+ GraphBuffer[i]=BitStream[i];
+ }
+ GraphTraceLen=bitnum;
+ RepaintGraphWindow();
+
+ //output
+ if (errCnt>0){
+ PrintAndLog("# Error during Demoding: %d\n",errCnt);
+ }
+ PrintAndLog("ASK decoded bitstream:");
+ // Now output the bitstream to the scrollback by line of 16 bits
+ printBitStream(BitStream,bitnum);
+
+ return 0;
+}
+