* Typical values can be 64, 32, 128...
*/
static void Cmdmanchesterdemod(char *str) {
- int i;
+ int i, j;\r
+ int bit;\r
int clock;
int lastval;
+ int low = 0;\r
+ int high = 0;\r
+ int hithigh, hitlow, first;\r
int lc = 0;
int bitidx = 0;
int bit2idx = 0;
-
+ int warnings = 0;\r
sscanf(str, "%i", &clock);
+ if (!clock)\r
+ {\r
+ PrintToScrollback("You must provide a clock rate.");\r
+ return;\r
+ }\r
int tolerance = clock/4;
/* Holds the decoded bitstream: each clock period contains 2 bits */
/* large array */
int BitStream[MAX_GRAPH_TRACE_LEN];
+ /* Detect high and lows */\r
+ for (i = 0; i < GraphTraceLen; i++)\r
+ {\r
+ if (GraphBuffer[i] > high)\r
+ high = GraphBuffer[i];\r
+ else if (GraphBuffer[i] < low)\r
+ low = GraphBuffer[i];\r
+ }\r
+\r
/* Detect first transition */
/* Lo-Hi (arbitrary) */
- for(i=1;i<GraphTraceLen;i++) {
- if (GraphBuffer[i-1]<GraphBuffer[i]) {
+ for (i = 0; i < GraphTraceLen; i++)\r
+ {\r
+ if (GraphBuffer[i] == low)\r
+ {\r
+// BitStream[0]=0; // Previous state = 0;\r
lastval = i;
- BitStream[0]=0; // Previous state = 0;
break;
}
}
+//PrintToScrollback("cool %d %d %d %d", low, high, lastval, GraphBuffer[i]);\r
+\r
+ /* If we're not working with 1/0s, demod based off clock */\r
+ if (high != 1)\r
+ {\r
+ bit = 0;\r
+ for (i = 0; i < (GraphTraceLen / clock); i++)\r
+ {\r
+ hithigh = 0;\r
+ hitlow = 0;\r
+ first = 1;\r
+\r
+ /* Find out if we hit both high and low peaks */\r
+ for (j = 0; j < clock; j++)\r
+ {\r
+ if (GraphBuffer[(i * clock) + j] == high)\r
+ hithigh = 1;\r
+ else if (GraphBuffer[(i * clock) + j] == low)\r
+ hitlow = 1;\r
+\r
+ /* it doesn't count if it's the first part of our read\r
+ because it's really just trailing from the last sequence */\r
+ if (first && (hithigh || hitlow))\r
+ hithigh = hitlow = 0;\r
+ else\r
+ first = 0;\r
+\r
+ if (hithigh && hitlow)\r
+ break;\r
+ }\r
+\r
+ /* If we didn't hit both high and low peaks, we had a bit transition */\r
+ if (!hithigh || !hitlow)\r
+ bit ^= 1;\r
+\r
+ BitStream[bit2idx++] = bit;\r
+ }\r
+ }\r
+\r
+ /* standard 1/0 bitstream */\r
+ else\r
+ {\r
/* Then detect duration between 2 successive transitions */
- for(bitidx = 1 ;i<GraphTraceLen;i++) {
- if (GraphBuffer[i-1] != GraphBuffer[i]) {
+ for (bitidx = 1; i < GraphTraceLen; i++)\r
+ {\r
+ if (GraphBuffer[i-1] != GraphBuffer[i])\r
+ {\r
lc = i-lastval;
lastval = i;
+\r
// Error check: if bitidx becomes too large, we do not
// have a Manchester encoded bitstream or the clock is really
// wrong!
BitStream[bitidx++]=GraphBuffer[i-1];
} else {
// Error
+ warnings++;\r
PrintToScrollback("Warning: Manchester decode error for pulse width detection.");
PrintToScrollback("(too many of those messages mean either the stream is not Manchester encoded, or clock is wrong)");
+\r
+ if (warnings > 100)\r
+ {\r
+ PrintToScrollback("Error: too many detection errors, aborting.");\r
+ return;\r
+ }\r
}
}
}
// We cannot end up in this state, this means we are unsynchronized,
// move up 1 bit:
i++;
+ warnings++;\r
PrintToScrollback("Unsynchronized, resync...");
PrintToScrollback("(too many of those messages mean the stream is not Manchester encoded)");
+\r
+ if (warnings > 100)\r
+ {\r
+ PrintToScrollback("Error: too many decode errors, aborting.");\r
+ return;\r
+ }\r
}
}
- PrintToScrollback("Manchester decoded bitstream \n---------");
+ }\r
+\r
+ PrintToScrollback("Manchester decoded bitstream");\r
// Now output the bitstream to the scrollback by line of 16 bits
for (i = 0; i < (bit2idx-16); i+=16) {
PrintToScrollback("%i %i %i %i %i %i %i %i %i %i %i %i %i %i %i %i",