-
Notifications
You must be signed in to change notification settings - Fork 1
/
logicdata.cpp
88 lines (78 loc) · 1.64 KB
/
logicdata.cpp
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
#include "logicdata.hpp"
#include <Arduino.h>
const unsigned char MARKER_ARRAY_LENGTH = 23;
const unsigned char markerArray[MARKER_ARRAY_LENGTH] = {1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1};
LogicData::LogicData(unsigned char pin)
{
_pin = pin;
_currentPos = 0;
_lastTime = micros();
}
bool LogicData::checkTime()
{
unsigned long currentTime = micros();
if (currentTime - _lastTime < 1000)
{
return false;
}
_lastTime = currentTime;
return true;
}
bool LogicData::checkParity(unsigned char currentBit) {
bool parity = true;
for (char i = 0; i < 8; i++)
{
if (_heightArray[i]) {
parity = !parity;
}
}
return parity == currentBit;
}
unsigned char LogicData::calculateHeight() {
unsigned char value = 0;
for (unsigned char i = 0; i < 8; ++i) {
value |= (_heightArray[i] << (i));
}
//Invert bits
return ~value;
}
bool LogicData::checkArray() {
for (char i = 0; i < MARKER_ARRAY_LENGTH; i++)
{
if (!_receiveArray[i] == markerArray[i]) {
return false;
}
}
return true;
}
unsigned char LogicData::process()
{
if (!checkTime())
{
return 0;
}
unsigned char currentBit = digitalRead(_pin);
for (char i = 0; i < MARKER_ARRAY_LENGTH - 1; i++)
{
_receiveArray[i] = _receiveArray[i + 1];
}
_receiveArray[MARKER_ARRAY_LENGTH - 1] = currentBit;
//checkArray
if (checkArray())
{
if (_currentPos < 8)
{
_heightArray[_currentPos] = currentBit;
_currentPos++;
}
else
{
// Parity bit
if (checkParity(currentBit)) {
return calculateHeight();
}
_currentPos = 0;
}
}
return 0;
}