manchester.cpp
2.18 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Copyright (c) 2015-2016, Arkadiusz Materek (arekmat@poczta.fm)
*
* Licensed under GNU General Public License 3.0 or later.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include "manchester.hpp"
uint32_t manchesterEncode16(uint16_t data) {
uint32_t result = 0xffffffff;
for (uint8_t i = 0; i < 8; ++i) {
result <<= 2;
if (data & 0x80) {
result |= 1;
} else {
result |= 2;
}
data <<= 1;
}
return result;
}
uint32_t manchesterEncode32(uint16_t data) {
uint32_t result = 0xffffffff;
for (uint8_t i = 0; i < 16; ++i) {
result <<= 2;
if (data & 0x8000) {
result |= 1;
} else {
result |= 2;
}
data <<= 1;
}
return result;
}
uint32_t manchesterEncode16Inv(uint16_t data) {
uint32_t result = 0xffffffff;
for (uint8_t i = 0; i < 8; ++i) {
result <<= 2;
if (data & 0x01) {
result |= 2;
} else {
result |= 1;
}
data >>= 1;
}
return result;
}
uint32_t manchesterEncode32Inv(uint16_t data) {
uint32_t result = 0xffffffff;
for (uint8_t i = 0; i < 16; ++i) {
result <<= 2;
if (data & 0x01) {
result |= 2;
} else {
result |= 1;
}
data >>= 1;
}
return result;
}
uint16_t manchesterDecode32(uint32_t data) {
uint16_t result = 0x00000000;
for (uint8_t i = 0; i < 16; i++) {
uint16_t x = data >> 30;
switch (x) {
case 1:
result <<= 1;
result |= 1;
data <<= 2;
break;
case 2:
result <<= 1;
data <<= 2;
break;
default:
return 0xffff;
}
}
return result;
}
uint16_t manchesterDecode16(uint32_t data) {
uint16_t result = 0x0000;
for (uint8_t i = 0; i < 8; i++) {
uint16_t x = (data & 0xffff) >> 14;
switch (x) {
case 1:
result <<= 1;
result |= 1;
data <<= 2;
break;
case 2:
result <<= 1;
data <<= 2;
break;
default:
return 0xffff;
}
}
return result;
}