-
Notifications
You must be signed in to change notification settings - Fork 0
/
RFBlindComs.cpp
261 lines (215 loc) · 7.79 KB
/
RFBlindComs.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
RFBlindsComs - Arduino libary for remote control outlet switches
*/
//#define RECEIVE_ATTR ICACHE_RAM_ATTR
#include "RFBlindComs.h"
#include <ArduinoLog.h>
volatile char RFBlindComs::nReceivedValue [50];
volatile int RFBlindComs::nReceiverInterrupt = -1;
volatile bool RFBlindComs::nReceivedValueReady = false;
RFBlindComs::RFBlindComs() {
this->nTransmitterPin = -1;
this->setRepeatTransmit(10);
RFBlindComs::nReceiverInterrupt = -1;
RFBlindComs::nReceivedValue;
RFBlindComs::nReceivedValueReady = false;
}
/**
* Sets Repeat Transmits
*/
void RFBlindComs::setRepeatTransmit(int nRepeatTransmit) {
this->nRepeatTransmit = nRepeatTransmit;
}
/**
* Enable transmissions
*
* @param nTransmitterPin Arduino Pin to which the sender is connected to
*/
void RFBlindComs::enableTransmit(int nTransmitterPin) {
this->nTransmitterPin = nTransmitterPin;
pinMode(this->nTransmitterPin, OUTPUT);
}
/**
* Disable transmissions
*/
void RFBlindComs::disableTransmit() {
this->nTransmitterPin = -1;
}
/**
* Transmit the first 'length' bits of the integer 'code'. The
* bits are sent from MSB to LSB, i.e., first the bit at position length-1,
* then the bit at position length-2, and so on, till finally the bit at position 0.
*/
void RFBlindComs::send(const char* command) {
if (this->nTransmitterPin == -1)
return;
// make sure the receiver is disabled while we transmit
int nReceiverInterrupt_backup = RFBlindComs::nReceiverInterrupt;
if (nReceiverInterrupt_backup != -1) {
this->disableReceive();
}
for (int nRepeat = 0; nRepeat < nRepeatTransmit; nRepeat++) {
this->sendCommand(command);
}
// Disable transmit after sending (i.e., for inverted protocols)
digitalWrite(this->nTransmitterPin, LOW);
// enable receiver again if we just disabled it
if (nReceiverInterrupt_backup != -1) {
this->enableReceive(nReceiverInterrupt_backup);
}
}
/**
* Enable receiving data
*/
void RFBlindComs::enableReceive(int interrupt) {
RFBlindComs::nReceiverInterrupt = interrupt;
this->enableReceive();
}
void RFBlindComs::enableReceive() {
if (RFBlindComs::nReceiverInterrupt != -1) {
RFBlindComs::nReceivedValueReady = false;
attachInterrupt(RFBlindComs::nReceiverInterrupt, handleInterruptx, CHANGE);
}
}
/**
* Disable receiving data
*/
void RFBlindComs::disableReceive() {
detachInterrupt(RFBlindComs::nReceiverInterrupt);
RFBlindComs::nReceiverInterrupt = -1;
}
bool RFBlindComs::available() {
return RFBlindComs::nReceivedValueReady;
}
void RFBlindComs::resetAvailable() {
RFBlindComs::nReceivedValueReady = false;
}
volatile char* RFBlindComs::getReceivedValue() {
return RFBlindComs::nReceivedValue;
}
#define RECEIVE_ATTR ICACHE_RAM_ATTR
void RECEIVE_ATTR RFBlindComs::handleInterruptx() {
static unsigned int i = 0;
static unsigned long lastTime = 0;
static bool AGC1 = false;
static bool AGC2 = false;
static bool AGC3 = false;
static int pinState = 0;
const char log1 = '1';
const char log0 = '0';
const long time = micros();
const unsigned int duration = time - lastTime;
if (AGC1 && AGC2 && AGC3){
pinState = digitalRead(RFBlindComs::nReceiverInterrupt);
//Log.trace(F("data Duration %d" CR),duration);
if (pinState ==0){
if (duration > 500 && duration < 800) { // Found 1
RFBlindComs::nReceivedValue[i] = log1;
i++;
} else if (duration > 200 && duration < 400) { // Found 0
RFBlindComs::nReceivedValue[i] = log0;
i++;
} else { // Unrecognized bit, finish
//Log.trace(F("Reset %d " CR),i);
AGC1 = false;
AGC2 = false;
AGC3 = false;
i = 0;
}
if (i == MARKISOL_COMMAND_BIT_ARRAY_SIZE){
//set value
//RFBlindComs::nReceivedValue = command;
RFBlindComs::nReceivedValueReady = true;
Log.trace(F("Found RF %s" CR), RFBlindComs::nReceivedValue);
}
}
}
// *********************************************************************
// Wait for the first AGC:
// *********************************************************************
// HIGH between 4500-6000 us
// *********************************************************************
if(duration > 4500 && duration < 6000){
AGC1 = true;
AGC2 = false;
AGC3 = false;
i = 0;
//Log.trace(F("Init %d " CR),i);
//Log.trace(F("AC1 Duration %d" CR),duration);
}
// *********************************************************************
// Wait for the second AGC:
// *********************************************************************
// LOW between 2300-2600 us
// *********************************************************************
if(duration > 2300 && duration < 2600 && AGC1){
AGC2 = true;
AGC3 = false;
//Log.trace(F("AC2 Duration %d" CR),duration);
}
// *********************************************************************
// Wait for the third AGC:
// *********************************************************************
// HIGH between 1100-1900 us
// *********************************************************************
if(duration > 1100 && duration < 1900 && AGC1 && AGC2){
AGC3 = true;
//Log.trace(F("AC3 Duration %d" CR),duration);
}
lastTime = time;
}
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------
void RFBlindComs::transmitHigh(int delay_microseconds) {
digitalWrite(this->nTransmitterPin, HIGH);
//PORTB = PORTB D13high; // If you wish to use faster PORTB calls instead
delayMicroseconds(delay_microseconds);
}
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------
void RFBlindComs::transmitLow(int delay_microseconds) {
digitalWrite(this->nTransmitterPin, LOW);
//PORTB = PORTB D13low; // If you wish to use faster PORTB calls instead
delayMicroseconds(delay_microseconds);
}
void RFBlindComs::errorLog(String message) {
Serial.println(message);
}
void RFBlindComs::sendCommand(const char* command) {
if (command == NULL) {
this->errorLog("sendMarkisolCommand(): Command array pointer was NULL, cannot continue.");
return;
}
this->doMarkisolTribitSend(command);
// Radio silence at the end of last command.
// It's better to go a bit over than under minimum required length:
this->transmitLow(MARKISOL_RADIO_SILENCE);
// Disable output to transmitter to prevent interference with
// other devices. Otherwise the transmitter will keep on transmitting,
// disrupting most appliances operating on the 433.92MHz band:
digitalWrite(this->nTransmitterPin, LOW);
}
void RFBlindComs::doMarkisolTribitSend(const char* command) {
const char log1 = '1';
const char log0 = '0';
// Starting (AGC) bits:
this->transmitHigh(MARKISOL_AGC1_PULSE);
this->transmitLow(MARKISOL_AGC2_PULSE);
this->transmitHigh(MARKISOL_AGC3_PULSE);
char c[50];
strcpy(c, command);
// Transmit command:
for (int i = 0; i < MARKISOL_COMMAND_BIT_ARRAY_SIZE; i++) {
// If current bit is 0, transmit LOW-HIGH-LOW (010):
if (c[i] == log0) {
this->transmitLow(MARKISOL_PULSE_SHORT);
this->transmitHigh(MARKISOL_PULSE_SHORT);
this->transmitLow(MARKISOL_PULSE_SHORT);
}
// If current bit is 1, transmit LOW-HIGH-HIGH (011):
if (c[i] == log1) {
this->transmitLow(MARKISOL_PULSE_SHORT);
this->transmitHigh(MARKISOL_PULSE_LONG);
}
}
}