forked from bittmanv/RIBSwitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RIBControl.cpp
311 lines (279 loc) · 8.04 KB
/
RIBControl.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "RIBControl.h"
#if ARDUINO >= 100
#include "Arduino.h"
#else
extern "C" {
#include "WConstants.h"
}
#endif
// #define RIB_DEBUG
const PROGMEM byte syncCode[]={45,22,22,22,22,70};
const PROGMEM byte buttAcode[]={68,46,22,22,22,22,22,110,0};
const PROGMEM byte buttBcode[]={45,23,45,45,22,22,22,110,0};
const PROGMEM byte buttCcode[]={45,23,22,22,45,46,22,110,0};
const PROGMEM byte buttDcode[]={45,23,22,22,22,22,45,134,0};
const byte* const button_table[] PROGMEM = {buttAcode, buttBcode, buttCcode, buttDcode};
RibControl::RibControl(int transmitterPin)
{
transPin=transmitterPin;
}
void RibControl::sendMessage(int dipSetup, uint8_t buttonNum)
{
// msg position counter
int msgPos=0;
// total lenght of msh in micros
int msgLength=0;
// current value
int curval=0;
int workDip=dipSetup;
// array for pulse message
uint8_t builtMsg[maxMsgSize];
// first clear generated message
for(int i=0;i<maxMsgSize;i++)
{
builtMsg[i]=0;
}
// first member is pulse
builtMsg[msgPos++]=pulse4;
msgLength+=pulse4;
// pin 1 is there everytime, so first pulse will be set directly
// first high pulse is depends on pin0 directly
#ifdef RIB_DEBUG
Serial.print(".");
#endif
if((workDip&1)==1)
{
curval=1;
builtMsg[msgPos++]=pulse4;
msgLength+=pulse4;
}
else
{
curval=0;
builtMsg[msgPos++]=pulse2;
msgLength+=pulse2;
}
#ifdef RIB_DEBUG
Serial.print("/");
#endif
for(int pin=1;pin<9;pin++)
{
#ifdef RIB_DEBUG
Serial.println(pin+1);
Serial.print(F("A"));
#endif
// low pulse config
//if(curval==dipSetup[pin])
if((workDip&0b11)==0b11||((workDip&0b0000000011)|0b00)==0b00)
{
#ifdef RIB_DEBUG
Serial.print(F("aa"));
#endif
// if pin value is same as previous, short low pulse
builtMsg[msgPos++]=pulse2;
msgLength+=pulse2;
#ifdef RIB_DEBUG
Serial.print(F(".aa"));
#endif
}
else
{
// if old value is high and new value should be low
// if(curval==1&&dipSetup[pin]==0)
if((workDip&0b11)==0b01)
{
#ifdef RIB_DEBUG
Serial.print(F("bb"));
#endif
// long low pulse will be added
builtMsg[msgPos++]=pulse4;
msgLength+=pulse4;
// first 0 after 1 should be skipped
// pin++;
workDip=workDip>>1;
curval=0;
#ifdef RIB_DEBUG
Serial.print(F(".bb"));
#endif
}
else
{
#ifdef RIB_DEBUG
Serial.print(F("c"));
#endif
// switching from 0 to 1
builtMsg[msgPos++]=pulse2;
msgLength+=pulse2;
// curval=1;
#ifdef RIB_DEBUG
Serial.print(F("c"));
#endif
}
}
#ifdef RIB_DEBUG
Serial.print(F("B"));
#endif
// high pulse config is simple, and configured based on value of
// current pin,
// if(dipSetup[pin]==1&&curval==0)
if((workDip&0b11)==0b10)
{
#ifdef RIB_DEBUG
Serial.print(F("d"));
#endif
builtMsg[msgPos++]=pulse4;
msgLength+=pulse4;
curval=1;
#ifdef RIB_DEBUG
Serial.print(F(".d"));
#endif
}
else
{
#ifdef RIB_DEBUG
Serial.print(F("e"));
#endif
builtMsg[msgPos++]=pulse2;
msgLength+=pulse2;
// curval=dipSetup[pin];
#ifdef RIB_DEBUG
Serial.print(F(".e"));
#endif
}
#ifdef RIB_DEBUG
Serial.println(msgPos);
Serial.print(pin);
Serial.println(F("C"));
#endif
workDip=workDip>>1;
}
// if 10 is on
//if(dipSetup[9]==1)
if((workDip&0b11)>0)
{
// is dip 9 also on
// if(dipSetup[8]==1)
// {
// low pulse for 9
builtMsg[msgPos++]=pulse2;
msgLength+=pulse2;
// high pulse for 10
builtMsg[msgPos++]=pulse2;
msgLength+=pulse2;
builtMsg[msgPos++]=pulse2;
msgLength+=pulse2;
}
else if(workDip&0b11)
{
builtMsg[msgPos++]=pulse2;
msgLength+=pulse2;
// dip 9 is off
builtMsg[msgPos++]=pulse4;
msgLength+=pulse4;
// high pulse for 10
builtMsg[msgPos++]=pulse2;
msgLength+=pulse2;
}
else if(workDip&&0b00)
{
// pulse for 10 will be added just for case when 9 is also off
//if(dipSetup[8]==0)
//{
builtMsg[msgPos++]=pulse2;
// high pulse for 10
builtMsg[msgPos++]=pulse2;
// msgLength+=pulse2;
builtMsg[msgPos++]=pulse2;
// msgLength+=pulse2;
msgLength+=(3*pulse2);
}
else
{
builtMsg[msgPos++]=pulse4;
msgLength+=pulse4;
}
// parity bit high in button setup
#ifdef RIB_DEBUG
Serial.print(F("F"));
#endif
if(msgLength<500)
{
builtMsg[msgPos++]=pulse4;
}
else
{
builtMsg[msgPos++]=pulse2;
}
#ifdef RIB_DEBUG
Serial.print(F("D"));
#endif
// now copy medium sync part
for(int i=0;i<syncLength;i++)
{
builtMsg[msgPos++]=pgm_read_byte_near(syncCode + i);
}
#ifdef RIB_DEBUG
Serial.print(F("E"));
#endif
// now copy buton code
// for(int i=0;i<buttonLength;i++)
//{
strcpy_P(builtMsg+msgPos,(char*)pgm_read_word(&(button_table[buttonNum])));
// builtMsg[msgPos++]=buttAcode[i];
// }
// physical msg send
#ifdef RIB_DEBUG
Serial.println("------");
for(int i=0;i<maxMsgSize;i+=2)
{
Serial.print(builtMsg[i]);
Serial.print(F(","));
Serial.println(builtMsg[i+1]);
}
#endif
sendCode(builtMsg);
} // SendMessage method
void RibControl::sendCode(uint8_t message[])
{
// The LED will be turned on to create a visual signal transmission indicator.
// digitalWrite(ledPin, HIGH);
//initialise the variables
int highLength = 0;
int lowLength = 0;
//The signal is transmitted 6 times in succession - this may vary with your remote.
unsigned long start=micros();
int total=0;
// int total=0;
for(int j = 0; j<transmitCount; j++){
int i=0;
for(i = 0; i<maxMsgSize; i+=2){
if(message[i]!=0)
{
highLength=message[i];
lowLength=message[i+1];
// total+=highLength;
total+=highLength;
total+=lowLength;
/* Transmit a HIGH signal - the duration of transmission will be determined
by the highLength and timeDelay variables */
digitalWrite(transPin, HIGH);
delayMicroseconds(highLength*timedelay);
/* T
* ransmit a LOW signal - the duration of transmission will be determined
by the lowLength and timeDelay variables */
digitalWrite(transPin,LOW);
delayMicroseconds(lowLength*timedelay);
/*Serial.print(lowLength);
Serial.print(",");
Serial.println(highLength);*/
}
}
// Serial.println(total);
}
unsigned long misEnd=micros();
// Serial.print("Sent time is");
//Serial.println(misEnd-start);
//Turn the LED off after the code has been transmitted.
// digitalWrite(ledPin, LOW);
digitalWrite(transPin, LOW);
} // end send code method