forked from pimatic/homeduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfcontrol_command.h
124 lines (112 loc) · 2.71 KB
/
rfcontrol_command.h
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
#include <RFControl.h>
int interrupt_pin = -1;
void rfcontrol_command_send();
void rfcontrol_command_receive();
void rfcontrol_command_raw();
bool in_raw_mode = false;
void rfcontrol_loop() {
if(in_raw_mode){
if(RFControl::existNewDuration()){
Serial.print(RFControl::getLastDuration() * RFControl::getPulseLengthDivider());
Serial.print(", ");
static byte line=0;
line++;
if(line>=16){
line=0;
Serial.write('\n');
}
}
}
else{
if(RFControl::hasData()) {
unsigned int *timings;
unsigned int timings_size;
RFControl::getRaw(&timings, &timings_size);
unsigned int buckets[8];
unsigned int pulse_length_divider = RFControl::getPulseLengthDivider();
RFControl::compressTimings(buckets, timings, timings_size);
Serial.print("RF receive ");
for(unsigned int i=0; i < 8; i++) {
unsigned long bucket = buckets[i] * pulse_length_divider;
Serial.print(bucket);
Serial.write(' ');
}
for(unsigned int i=0; i < timings_size; i++) {
Serial.write('0' + timings[i]);
}
Serial.print("\r\n");
RFControl::continueReceiving();
}
}
}
void rfcontrol_command() {
char* arg = sCmd.next();
if(arg == NULL) {
argument_error();
return;
}
if (strcmp(arg, "send") == 0) {
rfcontrol_command_send();
} else if (strcmp(arg, "receive") == 0) {
rfcontrol_command_receive();
} else if(strcmp(arg, "raw") == 0) {
rfcontrol_command_raw();
} else {
argument_error();
}
}
void rfcontrol_command_raw(){
char* arg = sCmd.next();
if(arg == NULL) {
argument_error();
return;
}
int interrupt_pin = atoi(arg);
RFControl::startReceiving(interrupt_pin);
in_raw_mode = true;
Serial.print("ACK\r\n");
}
void rfcontrol_command_receive() {
char* arg = sCmd.next();
if(arg == NULL) {
argument_error();
return;
}
interrupt_pin = atoi(arg);
RFControl::startReceiving(interrupt_pin);
in_raw_mode = false;
Serial.print("ACK\r\n");
}
void rfcontrol_command_send() {
char* arg = sCmd.next();
if(arg == NULL) {
argument_error();
return;
}
int transmitter_pin = atoi(arg);
arg = sCmd.next();
if(arg == NULL) {
argument_error();
return;
}
int repeats = atoi(arg);
// read pulse lengths
unsigned long buckets[8];
for(unsigned int i = 0; i < 8; i++) {
arg = sCmd.next();
if(arg == NULL) {
argument_error();
return;
}
buckets[i] = strtoul(arg, NULL, 10);
}
//read pulse sequence
arg = sCmd.next();
if(arg == NULL) {
argument_error();
return;
}
RFControl::sendByCompressedTimings(transmitter_pin, buckets, arg, repeats);
in_raw_mode = false;
Serial.print("ACK\r\n");
}