forked from sui77/rc-switch
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathProtocolAnalyzeDemo.ino
100 lines (86 loc) · 3.73 KB
/
ProtocolAnalyzeDemo.ino
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
/*
Example for analyzing and proposing unknown new protocols
https://github.com/Martin-Laclaustra/rc-switch/
Requires modified rc-switch branch "protocollessreceiver"
with ReceivedInverted() function exposed.
*/
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
void setup() {
Serial.begin(9600);
mySwitch.enableReceive(0); // Receiver on interrupt 0 => that is pin #2
// Use mySwitch.enableReceive(D2); for ESP8266
}
void loop() {
if (mySwitch.available()) {
int value = mySwitch.getReceivedValue();
if (value == 0) {
Serial.print("Unknown encoding");
} else {
int databuffer[64]; // get a copy of the received timings before they are overwritten
int numberoftimings = 2 * mySwitch.getReceivedBitlength() + 2;
if(numberoftimings > 64) numberoftimings = 64;
for (int i = 0; i < numberoftimings; i++) {
databuffer[i] = mySwitch.getReceivedRawdata()[i];
}
Serial.print("Received ");
Serial.print( mySwitch.getReceivedValue() );
Serial.print(" / ");
Serial.print( mySwitch.getReceivedBitlength() );
Serial.print("bit ");
Serial.print("Protocol: ");
Serial.println( mySwitch.getReceivedProtocol() );
unsigned int databitsoffset = abs( (int)mySwitch.getReceivedLevelInFirstTiming() - (int)mySwitch.getReceivedInverted());
//Serial.println( mySwitch.getReceivedLevelInFirstTiming() );
//Serial.println( mySwitch.getReceivedInverted() );
//Serial.println( databitsoffset );
unsigned long dataduration = 0;
for (unsigned int i = 1 + databitsoffset; i < numberoftimings - 1 + databitsoffset; i++) {
dataduration += databuffer[i];
}
Serial.print("data bits of pulse train duration: ");
Serial.println( dataduration );
unsigned int averagebitduration = (int)(0.5 + ((double)dataduration)/mySwitch.getReceivedBitlength());
unsigned int protocolratio = (unsigned int)(0.5 + ((double)(averagebitduration - mySwitch.getReceivedDelay())) / (double)mySwitch.getReceivedDelay());
Serial.print("proposed protocol: { ");
Serial.print(mySwitch.getReceivedDelay());
Serial.print(", { ");
Serial.print( (databitsoffset==0) ?
(int) (0.5 + (double)databuffer[2*mySwitch.getReceivedBitlength()+1]/(double)mySwitch.getReceivedDelay())
:
(int) (0.5 + (double)databuffer[0]/(double)mySwitch.getReceivedDelay())
);
Serial.print(", ");
Serial.print( (databitsoffset==0) ?
(int) (0.5 + (double)databuffer[0]/(double)mySwitch.getReceivedDelay())
:
(int) (0.5 + (double)databuffer[1]/(double)mySwitch.getReceivedDelay())
);
Serial.print(" }, { ");
Serial.print("1");
Serial.print(", ");
Serial.print(protocolratio);
Serial.print(" }, { ");
Serial.print(protocolratio);
Serial.print(", ");
Serial.print("1");
Serial.print(" }, ");
Serial.print((mySwitch.getReceivedInverted()) ? "true" : "false" );
Serial.println(" }");
// raw signal
Serial.println("====");
Serial.print("first level ");
Serial.println((mySwitch.getReceivedLevelInFirstTiming() == 0) ? "down" : "up" );
for (int i = 0; i < 2*mySwitch.getReceivedBitlength()+2 - 1 + databitsoffset; i++) {
Serial.print(databuffer[i]);
Serial.print(" ");
if((i - databitsoffset) % 16 == 0) Serial.println("");
}
if ((2*mySwitch.getReceivedBitlength()+2 - 1 + databitsoffset - 1) % 16 != 0) Serial.println("");
if (databitsoffset != 1) Serial.println(databuffer[2*mySwitch.getReceivedBitlength()+1]);
// plot signal in spreadsheet
Serial.println("====");
}
mySwitch.resetAvailable();
}
}