-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIRMessage.cpp
153 lines (129 loc) · 3.08 KB
/
IRMessage.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "IRMessage.h"
// Simple constructor
IRMessage::IRMessage() {
}
// Constructor with IRcode
IRMessage::IRMessage(decode_results *results) {
type = results->decode_type;
if (type == UNKNOWN) {
bits = results->rawlen - 1;
for (int i = 0; i <= bits; i++) {
if (i % 2) {
// Mark
rawCodes[i] = results->rawbuf[i]*USECPERTICK - MARK_EXCESS;
}
else {
// Space
rawCodes[i] = results->rawbuf[i]*USECPERTICK + MARK_EXCESS;
}
}
}
else {
value = results->value;
bits = results->bits;
}
}
// Send JsonObject to serial
void IRMessage::send() {
// Creating Json Object
JsonObject& root = jsonBuffer.createObject();
root["type"] = type;
root["value"] = value;
root["bits"] = bits;
// Send RawCodes if type == UNKNOWN
if (type == UNKNOWN) {
JsonArray& data = root.createNestedArray("rawCodes");
for (int i=0; i<bits; i++) {
data.add(rawCodes[i]);
}
}
// Print json to serial
char buffer[256];
root.printTo(buffer, sizeof(buffer));
printToSerial(buffer);
}
// Decode json (from serial)
void IRMessage::decode(String message) {
// Convert message to char array
int str_len = message.length() + 1;
char char_array[str_len];
message.toCharArray(char_array, str_len);
// Parse json
JsonObject& root = jsonBuffer.parseObject(char_array);
// Set properties
type = (int) root["type"];
value = (unsigned long) root["value"];
bits = (int) root["bits"];
// Decode Raw if type == UNKNOWN
if (type == UNKNOWN) {
for (int i=0; i<bits; i++) {
rawCodes[i] = (unsigned int) root["rawCodes"][i];
}
}
// Send ir signal
irSend();
}
// Sending ir signal
void IRMessage::irSend() {
// -1 == UNKNOW
if (type == UNKNOWN) {
irsend.sendRaw(rawCodes, bits, 38);
}
// 1 == RC5, 2 == RC6
else if (type == RC5 || type == RC6) {
// Put the toggle bit into the code to send
value = value & ~(1 << (bits - 1));
value = value | (toggle << (bits - 1));
if (type == RC5) {
irsend.sendRC5(value, bits);
}
else {
irsend.sendRC6(value, bits);
}
}
// 3 == NEC
else if (type == NEC) {
irsend.sendNEC(value, bits);
}
// 4 == SONY
else if (type == SONY) {
irsend.sendSony(value, bits);
}
// 5 == PANASONIC
else if (type == PANASONIC) {
irsend.sendPanasonic(value, bits);
}
// 6 == JVC
else if (type == JVC) {
// TODO : last param = repeat
irsend.sendJVC(value, bits, false);
}
// 7 == SAMSUNG
else if (type == SAMSUNG) {
irsend.sendSAMSUNG(value, bits);
}
// 8 == WHYNTER
else if (type == WHYNTER) {
irsend.sendWhynter(value, bits);
}
// 10 = LG
else if (type == LG) {
irsend.sendLG(value, bits);
}
else {
sendError();
}
}
// Send an error
void IRMessage::sendError() {
// Creating Json Object
JsonObject& root = jsonBuffer.createObject();
root["error"] = "Cannot send ir message";
char buffer[256];
root.printTo(buffer, sizeof(buffer));
printToSerial(buffer);
}
// Send json to serial
void IRMessage::printToSerial(char* json) {
Serial.println(json);
}