-
Notifications
You must be signed in to change notification settings - Fork 0
/
IRLibPanavox.cpp
130 lines (98 loc) · 2.45 KB
/
IRLibPanavox.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
Name: IRLibPanavox.cpp
Created: 11/2/2017 11:00:23 PM
Author: Th3DarKZi0N
Editor: http://www.visualmicro.com
*/
#include "IRLibPanavox.h"
#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
void IRsendPanavox::sendPacket(uint32_t value) {
uint8_t* data = (uint8_t*)&value;
mark(PANAVOX_AC_HDR_MARK);
space(PANAVOX_AC_HDR_SPACE);
for (int i = 0; i < 3; i++) {
sendByte(data[i]);
sendByte(~data[i]);
}
mark(PANAVOX_AC_BIT_MARK);
}
void IRsendPanavox::sendByte(uint8_t data) {
for (int i = 7; i >= 0; i--) {
mark(PANAVOX_AC_BIT_MARK);
if (CHECK_BIT(data, i)) {
space(PANAVOX_AC_ONE_SPACE);
}
else {
space(PANAVOX_AC_ZERO_SPACE);
}
}
}
void IRsendPanavox::send(uint32_t cmd)
{
enableIROut(PANAVOX_AC_FREQ);
sendPacket(cmd);
space(PANAVOX_AC_RPT_SPACE);
sendPacket(cmd);
space(PANAVOX_AC_END_SPACE);
}
int IRdecodePanavox::readNextByte(uint8_t offset) {
uint8_t data = 0;
uint8_t offset_limit = offset + 16;
while (offset < offset_limit) {
if (!MATCH(recvGlobal.decodeBuffer[offset], PANAVOX_AC_BIT_MARK)) {
return READ_ERROR;
}
offset++;
if (MATCH(recvGlobal.decodeBuffer[offset], PANAVOX_AC_ONE_SPACE)) {
data = (data << 1) | 1;
}
else if (MATCH(recvGlobal.decodeBuffer[offset], PANAVOX_AC_ZERO_SPACE)) {
data <<= 1;
}
else {
return READ_ERROR;
}
offset++;
}
return data;
}
bool IRdecodePanavox::decode(void)
{
this->resetDecoder();
uint32_t data[6];
uint8_t offset;
if (MATCH(recvGlobal.decodeBuffer[0], PANAVOX_AC_RPT_SPACE))
this->repeat = true;
else
this->repeat = false;
if (recvGlobal.decodeLength != PANAVOX_RAW_COUNT)
return RAW_COUNT_ERROR;
// Check header
if (!MATCH(recvGlobal.decodeBuffer[1], PANAVOX_AC_HDR_MARK))
return HEADER_MARK_ERROR(PANAVOX_AC_HDR_MARK);
if (!MATCH(recvGlobal.decodeBuffer[2], PANAVOX_AC_HDR_SPACE))
return HEADER_SPACE_ERROR(PANAVOX_AC_HDR_SPACE);
offset = 3; // Skip header and initial gap
// Read data
for (int i = 0; i < 6; i++) {
int result = this->readNextByte(offset + (i * 16));
if (result == READ_ERROR)
return false;
data[i] = (unsigned char)result;
}
// Check data integrity
for (int i = 0; i < 6; i += 2) {
if ((~data[i] & 0xFF) != data[i + 1])
return false;
}
this->value = (data[4] & 0xFF) << 16 | (data[2] & 0xFF) << 8
| (data[0] & 0xFF);
this->address = 0;
this->protocolNum = PANAVOX_AC;
this->bits = 24;
return true;
}
void IRdecodePanavox::dumpResults(bool verbose)
{
Serial.println(this->value, HEX);
}