+ if ( buff == NULL ) return;
+
+ uint16_t i = 0;
+ if ( size > MAX_GRAPH_TRACE_LEN )
+ size = MAX_GRAPH_TRACE_LEN;
+ ClearGraph(0);
+ for (; i < size; ++i){
+ GraphBuffer[i]=buff[i]-128;
+ }
+ GraphTraceLen=size;
+ RepaintGraphWindow();
+ return;
+}
+size_t getFromGraphBuf(uint8_t *buff)
+{
+ if (buff == NULL ) return 0;
+ uint32_t i;
+ for (i=0;i<GraphTraceLen;++i){
+ if (GraphBuffer[i]>127) GraphBuffer[i]=127; //trim
+ if (GraphBuffer[i]<-127) GraphBuffer[i]=-127; //trim
+ buff[i]=(uint8_t)(GraphBuffer[i]+128);
+ }
+ return i;
+}
+
+// A simple test to see if there is any data inside Graphbuffer.
+bool HasGraphData(){
+
+ if ( GraphTraceLen <= 0) {
+ PrintAndLog("No data available, try reading something first");
+ return false;
+ }
+ return true;
+}
+
+// Detect high and lows in Grapbuffer.
+// Only loops the first 256 values.
+void DetectHighLowInGraph(int *high, int *low, bool addFuzz) {
+
+ uint8_t loopMax = 255;
+ if ( loopMax > GraphTraceLen)
+ loopMax = GraphTraceLen;
+
+ for (uint8_t i = 0; i < loopMax; ++i) {
+ if (GraphBuffer[i] > *high)
+ *high = GraphBuffer[i];
+ else if (GraphBuffer[i] < *low)
+ *low = GraphBuffer[i];
+ }
+
+ //12% fuzz in case highs and lows aren't clipped
+ if (addFuzz) {
+ *high = (int)(*high * .88);
+ *low = (int)(*low * .88);
+ }
+}
+
+// Get or auto-detect ask clock rate
+int GetAskClock(const char str[], bool printAns, bool verbose)
+{
+ int clock;
+ sscanf(str, "%i", &clock);
+ if (!strcmp(str, ""))
+ clock = 0;
+
+ if (clock != 0)
+ return clock;
+ // Auto-detect clock
+ uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
+ size_t size = getFromGraphBuf(grph);
+ if (size == 0) {
+ if (verbose)
+ PrintAndLog("Failed to copy from graphbuffer");
+ return -1;
+ }
+ //, size_t *ststart, size_t *stend
+ size_t ststart = 0, stend = 0;
+ bool st = DetectST(grph, &size, &clock, &ststart, &stend);
+ int start = stend;
+ if (st == false) {
+ start = DetectASKClock(grph, size, &clock, 20);
+ }
+ setClockGrid(clock, start);
+ // Only print this message if we're not looping something
+ if (printAns || g_debugMode) {
+ PrintAndLog("Auto-detected clock rate: %d, Best Starting Position: %d", clock, start);
+ }
+ return clock;
+}
+
+uint8_t GetPskCarrier(const char str[], bool printAns, bool verbose)
+{
+ uint8_t carrier=0;
+ uint8_t grph[MAX_GRAPH_TRACE_LEN]={0};
+ size_t size = getFromGraphBuf(grph);
+ if ( size == 0 ) {
+ if (verbose)
+ PrintAndLog("Failed to copy from graphbuffer");
+ return 0;
+ }
+ uint16_t fc = countFC(grph,size,0);
+ carrier = fc & 0xFF;
+ if (carrier != 2 && carrier != 4 && carrier != 8) return 0;
+ if ((fc>>8) == 10 && carrier == 8) return 0;
+ // Only print this message if we're not looping something
+ if (printAns) {
+ PrintAndLog("Auto-detected PSK carrier rate: %d", carrier);
+ }
+ return carrier;