-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathmanchester_helpers.cpp
More file actions
104 lines (96 loc) · 2.72 KB
/
Copy pathmanchester_helpers.cpp
File metadata and controls
104 lines (96 loc) · 2.72 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
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// Manchester decode helpers ported from Flipper Zero firmware
// (GPL-3.0-or-later), lib/subghz/blocks/decoder.c / manchester_advance.
#include "manchester_helpers.h"
static inline unsigned int mh_diff(unsigned int a, unsigned int b) {
return (a > b) ? (a - b) : (b - a);
}
void manchester_reset(ManchesterState &s) {
s.state = 0;
}
bool manchester_advance(ManchesterState &s, ManchesterEvent event, bool *data) {
bool result = false;
*data = false;
switch (s.state) {
case 0: // Start
switch (event) {
case ManchesterEventShortHigh:
s.state = 1; // MidBit
break;
case ManchesterEventShortLow:
s.state = 1; // MidBit
break;
case ManchesterEventLongHigh:
*data = true;
result = true;
break;
case ManchesterEventLongLow:
*data = true;
result = true;
break;
default:
break;
}
break;
case 1: // MidBit
switch (event) {
case ManchesterEventShortLow:
s.state = 2; // Done
break;
case ManchesterEventShortHigh:
s.state = 2; // Done
break;
case ManchesterEventLongLow:
*data = false;
result = true;
break;
case ManchesterEventLongHigh:
*data = false;
result = true;
break;
default:
break;
}
break;
case 2: // Done
switch (event) {
case ManchesterEventShortLow:
*data = true;
result = true;
s.state = 0;
break;
case ManchesterEventShortHigh:
s.state = 0;
break;
case ManchesterEventLongLow:
*data = false;
result = true;
s.state = 0;
break;
case ManchesterEventLongHigh:
s.state = 0;
break;
default:
break;
}
break;
}
return result;
}
ManchesterEvent manchester_event_for(bool level, unsigned int dur,
unsigned int te_short, unsigned int te_long,
unsigned int te_delta) {
if (!level) {
if (mh_diff(dur, te_short) < te_delta)
return ManchesterEventShortLow;
if (mh_diff(dur, te_long) < te_delta)
return ManchesterEventLongLow;
} else {
if (mh_diff(dur, te_short) < te_delta)
return ManchesterEventShortHigh;
if (mh_diff(dur, te_long) < te_delta)
return ManchesterEventLongHigh;
}
return ManchesterEventReset;
}