slave.ino
11.2 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
MIT License
Copyright (c) 2020 adbenjumea
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#define RX_PIN 2
#define TX_PIN 3
#define TX_RAW_PIN 4
#define TX_CLK_PIN 5
#define RX_DATA_LEN 16
#define TX_DATA_LEN 8
#define BAUD_RATE 1200
#define B0 6
#define B1 7
#define B2 8
#define B3 9
#define B4 10
#define B5 11
#define B6 12
#define B7 13
#define ADDR 29 //direccion 5 en realidad
int GROUP[16] = {1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; //0 a 15, grupos 1,5 y 16 en realidad
uint8_t Scene;
const int ledPIN = 11;
uint16_t ledPOWER = 0;
uint8_t ledPOWER_fade = 0;
int fadeRate = 1600;//ms
unsigned int Te;
uint16_t rx_buffer = 0;
uint16_t rx_data = 0;
//uint16_t rx_data = 0b100001000111010; //Lampara 34, luminosidad 23%
//uint16_t rx_data = 0b1000100000000010; //Grupo 5, luminosidad 1%
//uint16_t rx_data = 0b1000101000000010; //Grupo 6, luminosidad 1%
//uint16_t rx_data = 0b100011111111; //Lampara 5, luminosidad 100%
//uint16_t rx_data = 0b101011111111; //Lampara 6, luminosidad 100%
//uint16_t rx_data = 0b1111111100100000; //Broadcast, comando 32
void manchester_encode(uint16_t data);
void manchester_decode(void);
void wait_nTe(int n = 1);
void rx_routine(void);
void setup() {
pinMode(TX_PIN, OUTPUT);
pinMode(RX_PIN, INPUT);
pinMode(TX_RAW_PIN, OUTPUT);
pinMode(TX_CLK_PIN, OUTPUT);
pinMode(B0, OUTPUT);
pinMode(B1, OUTPUT);
pinMode(B2, OUTPUT);
pinMode(B3, OUTPUT);
pinMode(B4, OUTPUT);
pinMode(B5, OUTPUT);
pinMode(B6, OUTPUT);
pinMode(B7, OUTPUT);
attachInterrupt(digitalPinToInterrupt(RX_PIN), manchester_decode, CHANGE);
Te = 500000 / BAUD_RATE;
// Initialize LED
pinMode(ledPIN , OUTPUT);
// Initialize Serial Port
Serial.begin(115200);
while (!Serial) {}
}
void loop() {
/*Serial.print("Buenas, soy la lámpara ");Serial.print(ADDR+1);Serial.print(" y pertenezco a los grupos ");Serial.print(GROUP[0]+1);Serial.print(", ");
Serial.print(GROUP[1]+1);Serial.print(" y ");Serial.print(GROUP[2]+1);Serial.println(".");
Serial.println("");Serial.println("Enviando a lámpara 34, luminosidad 23%");
rx_data = 0b100001000111010; //Lampara 34, luminosidad 23%
delay(1000);
rx_routine();
Serial.println("");Serial.println("Enviando a grupo 5, luminosidad 1%");
rx_data = 0b1000100000000010; //Grupo 5, luminosidad 1%
delay(1000);
rx_routine();
Serial.println("");Serial.println("Enviando a grupo 6, luminosidad 1%");
rx_data = 0b1000101000000010; //Grupo 6, luminosidad 1%
delay(1000);
rx_routine();
Serial.println("");Serial.println("Enviando broadcast, comando 32%");
rx_data = 0b1111111100100000; //Broadcast, comando 32
delay(1000);
rx_routine();
Serial.println("");Serial.println("Enviando a lámpara 5, luminosidad 100%");
rx_data = 0b100011111111; //Lampara 5, luminosidad 100%
delay(1000);
rx_routine();
Serial.println("");Serial.println("Enviando a lámpara 6, luminosidad 100%");
rx_data = 0b101011111111; //Lampara 6, luminosidad 100%
delay(1000);
rx_routine();*/
/*Serial.println("");Serial.println("Enviando a lámpara 5, luminosidad 100%");
rx_data = 0b100011111111; //Lampara 5, luminosidad 100%
delay(1000);
rx_routine();
Serial.println("");Serial.println("Enviando a lámpara 5, luminosidad 50%");
rx_data = 0b100010000000; //Lampara 5, luminosidad 50%
delay(1000);
rx_routine();
Serial.println("");Serial.println("Enviando broadcast, comando 0, apagar");
rx_data = 0b1111111100000000; //Broadcast, comando 0
delay(1000);
rx_routine();
Serial.println("");Serial.println("Enviando a grupo 5, go to scene 10");
rx_data = 0b1000100100011010; //Grupo 5, gte 10
delay(1000);
rx_routine();
Serial.println("");Serial.println("Enviando a lámpara 5, añadir al grupo 4");
rx_data = 0b100101100011; //Lampara 5, atg 4
delay(1000);
rx_routine();*/
}
void rx_routine(void) {
int i, j;
int myADDR = 0; //1 = Trama me incluye, 0 = Trama no me incluye
uint8_t byte_dir, byte_com;
//Serial.print((char)rx_data);
Serial.println(rx_data, BIN);
byte_dir = rx_data >> 8;
byte_com = (rx_data << 8) >> 8;
//COMPROBAR SI SOY DESTINATARIO
//¿Es un broadcast?
if ((byte_dir >> 1) == 0b1111111)
{
myADDR = 1;
Serial.println("Soy destinatario. Por Broadcast.");
//¿Es un grupo?
}
else if ((byte_dir >> 5) >= 0b100) {
if (GROUP[((byte_dir % 0b100000) >> 1)]) //¿Pertenezco al grupo?
{
myADDR = 1;
Serial.println("Soy destinatario. Por pertenecer al grupo.");
}
}
//¿Es mi dirección particular?
else {
if (ADDR == (byte_dir >> 1))
{
myADDR = 1;
Serial.println("Soy destinatario. Por ser mi dirección particular.");
}
}
//ACTUAR EN CASO DE SER DESTINATARIO
if (myADDR) {
//¿Es un valor directo de luminosidad?
if (byte_dir % 0b10 == 0)
{
Serial.println("Es un valor directo de la luminosidad");
analogWrite(ledPIN , byte_com);
ledPOWER = byte_com;
}
//¿Es un comando DALI
else if (byte_dir % 0b10 == 1)
{
Serial.println("Es un comando DALI");
if (byte_com == 0b00000000) //¿Comando OFF?
{
Serial.println("Apagar luz");
analogWrite(ledPIN , 0);
ledPOWER = 0;
}
else if ((byte_com >> 4) == 0b0001) //¿Comando Go To Scene?
{
Scene = byte_com % 0b10000;
Serial.print("Go to scene "); Serial.println(Scene);
switch (Scene) {
case 6:
if (ADDR == 33)
{
analogWrite(ledPIN , 20 * 255 / 100);
ledPOWER = 20 * 255 / 100;
}
else if (ADDR == 54)
{
analogWrite(ledPIN , 80 * 255 / 100);
ledPOWER = 20 * 255 / 100;
}
break;
case 8:
if (ADDR == 54)
{
analogWrite(ledPIN , 20 * 255 / 100);
ledPOWER = 20 * 255 / 100;
}
else if (ADDR == 33)
{
analogWrite(ledPIN , 80 * 255 / 100);
ledPOWER = 20 * 255 / 100;
}
break;
default:
// statements
break;
}
}
else if ((byte_com >> 4) == 0b0110) //¿Comando Add To Group?
{
GROUP[(byte_com % 0b10000)] = 1;
Serial.print("Add To Group "); Serial.println(byte_com % 0b10000 + 1);
}
else if ((byte_com >> 4) == 0b0111) //¿Comando Remove From Group?
{
GROUP[(byte_com % 0b10000)] = 0;
Serial.print("Remove From Group "); Serial.println(byte_com % 0b10000 + 1);
}
else if (byte_com == 0b10100000) //¿Comando Query Level?
{
Serial.print("Enviando nivel de intensidad "); Serial.println(ledPOWER);
wait_nTe(8);
manchester_encode(ledPOWER);
}
else if ((byte_com >> 4) == 0b1101) //¿Comando Go To Level?
{
Serial.print("Go to level "); Serial.println((byte_com % 0b10000));
ledPOWER_fade = (byte_com % 0b10000) * 255 / 15;
if (ledPOWER < ledPOWER_fade)
{
while (ledPOWER < ledPOWER_fade)
{
for (j = 0; j < 10; j++)
{
delayMicroseconds(fadeRate);
}
analogWrite(ledPIN , ledPOWER + 1);
ledPOWER = ledPOWER + 1;
}
}
else if (ledPOWER > ledPOWER_fade)
{
while (ledPOWER > ledPOWER_fade)
{
for (j = 0; j < 10; j++)
{
delayMicroseconds(fadeRate);
}
analogWrite(ledPIN , ledPOWER - 1);
ledPOWER = ledPOWER - 1;
}
}
}
}
myADDR = 0;
}
else
{
Serial.println("No soy destinatario");
}
/*
digitalWrite(B0, bitRead(rx_data, 0));
digitalWrite(B1, bitRead(rx_data, 1));
digitalWrite(B2, bitRead(rx_data, 2));
digitalWrite(B3, bitRead(rx_data, 3));
digitalWrite(B4, bitRead(rx_data, 4));
digitalWrite(B5, bitRead(rx_data, 5));
digitalWrite(B6, bitRead(rx_data, 6));
digitalWrite(B7, bitRead(rx_data, 7));
*/
}
// ====================== //
// COMMON FUNCTIONS //
// ====================== //
void manchester_encode(uint16_t data) {
detachInterrupt(digitalPinToInterrupt(RX_PIN));
// ***** START BIT ***** //
digitalWrite(TX_PIN, 1);
wait_nTe();
digitalWrite(TX_PIN, 0);
wait_nTe();
// ******************** //
for (int i = (TX_DATA_LEN - 1); i >= 0; i--) {
byte current_bit;
current_bit = bitRead(data, i);
// ---- For debuging only ---- //
//digitalWrite(TX_RAW_PIN, current_bit);
//digitalWrite(TX_CLK_PIN, 0);
// --------------------------- //
digitalWrite(TX_PIN, current_bit);
wait_nTe();
// ---- For debuging only ---- //
//digitalWrite(TX_CLK_PIN, 1);
// --------------------------- //
digitalWrite(TX_PIN, !current_bit);
wait_nTe();
}
// ***** STOP BIT ***** //
digitalWrite(TX_CLK_PIN, 0);
digitalWrite(TX_PIN, 0);
wait_nTe(4);
// ******************* //
attachInterrupt(digitalPinToInterrupt(RX_PIN), manchester_decode, CHANGE);
}
void manchester_decode(void) {
byte current_bit;
static int rx_count = 0;
static bool receiving = false;
delayMicroseconds(Te >> 1);
if (!receiving) {
if (digitalRead(RX_PIN)) receiving = true;
}
else {
detachInterrupt(digitalPinToInterrupt(RX_PIN));
delayMicroseconds(Te + (Te >> 1));
current_bit = digitalRead(RX_PIN);
rx_buffer <<= 1;
bitWrite(rx_buffer, 0, current_bit);
rx_count++;
if (rx_count == RX_DATA_LEN) {
wait_nTe(4); // Stop bits
rx_data = rx_buffer;
rx_buffer = 0;
rx_count = 0;
receiving = false;
rx_routine();
}
attachInterrupt(digitalPinToInterrupt(RX_PIN), manchester_decode, CHANGE);
}
}
void wait_nTe(int n) {
for (int i = 0; i < n; i++) {
delayMicroseconds(Te);
}
}