1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#include "manchester.h"
// G. E. Thomas Manchester decoder
uint8_t manchester_decode(uint8_t partOne, uint8_t partTwo)
{
uint8_t data = 0x00;
uint8_t temp = 0x00;
uint8_t bitCount = 0, bitOneCount = 0, bitTwoCount = 0, bitByOneCount = 1, bitByTwoCount = 1, firstHalfCount = 0;
while (bitCount != 8)
{
if (firstHalfCount <= 6)
{
temp |= ((partOne >> (bitOneCount + bitByOneCount)) & 0x01);
if (temp == 0x01)
{
data |= (temp << bitCount);
}
if (temp == 0x00)
{
data |= (temp << bitCount);
}
bitCount++;
bitOneCount++;
bitByOneCount++;
}
else
{
temp |= ((partTwo >> (bitTwoCount + bitByTwoCount)) & 0x01);
if (temp == 0x01)
{
data |= (temp << bitCount);
}
if (temp == 0x00)
{
data |= (temp << bitCount);
}
bitCount++;
bitTwoCount++;
bitByTwoCount++;
}
firstHalfCount = firstHalfCount + 2;
temp = 0x00;
}
return data;
}
int manchesterDecoder(uint8_t *in, int length, uint8_t *out)
{
for (int i = 0; i < length; i += 2)
{
out[i / 2] = manchester_decode(in[i + 1], in[i]);
}
return length / 2;
}
|