forked from pimatic/homeduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfcontrol.h
92 lines (84 loc) · 2.04 KB
/
rfcontrol.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
#include <RFControl.h>
void rfcontrol_command_send();
void rfcontrol_command_receive();
void rfcontrol_loop() {
if(RFControl::hasData()) {
unsigned int *timings;
unsigned int timings_size;
RFControl::getRaw(&timings, &timings_size);
unsigned int buckets[8];
RFControl::compressTimings(buckets, timings, timings_size);
Serial.print("RF receive ");
for(unsigned int i=0; i < 8; i++) {
Serial.print(buckets[i]);
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 {
argument_error();
}
}
void rfcontrol_command_receive() {
char* arg = sCmd.next();
if(arg == NULL) {
argument_error();
return;
}
int interrupt_pin = atoi(arg);
RFControl::startReceiving(interrupt_pin);
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 int buckets[8];
for(unsigned int i = 0; i < 8; i++) {
arg = sCmd.next();
if(arg == NULL) {
argument_error();
return;
}
buckets[i] = atoi(arg);
}
//read pulse sequence
arg = sCmd.next();
if(arg == NULL) {
argument_error();
return;
}
unsigned int timings_size = strlen(arg);
unsigned int timings[timings_size];
for(unsigned int i = 0; i < timings_size; i++) {
unsigned int index = arg[i] - '0';
timings[i] = buckets[index];
}
RFControl::sendByTimings(transmitter_pin, timings, timings_size, repeats);
Serial.print("ACK\r\n");
}