/*
Arduino light control of Nexa and Everflourish RF 433.92 MHz based systems.
For details, see:
http://hblok.net
http://hblok.net/blog/posts/tag/433
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
#include "Arduino.h"
#include
int OUT_PIN;
int short_low_0;
int long_low_1;
int high_space;
void set_out_pin(int pin) {
OUT_PIN = pin;
pinMode(OUT_PIN, OUTPUT);
}
void send(Light light, State state) {
for(int i = 0; i < 4; i++) {
switch(light.type) {
case EVERFLOURISH:
send_everflourish(state == OFF ? light.off : light.on);
break;
case NEXA:
send_nexa(state == OFF ? light.off : light.on);
break;
}
}
}
void send_everflourish(const int *light_data) {
digitalWrite(OUT_PIN, LOW);
delayMicroseconds(EF_LONG_LOW_1);
write_wire(0); write_wire(0); write_wire(0); write_wire(0); // "RF wakeup"
short_low_0 = EF_SHORT_LOW_0;
long_low_1 = EF_LONG_LOW_1;
high_space = EF_HIGH_SPACE;
send_bytes(light_data, 3);
digitalWrite(OUT_PIN, HIGH);
delayMicroseconds(EF_HIGH_SPACE);
digitalWrite(OUT_PIN, LOW);
delayMicroseconds(EF_END_LOW);
digitalWrite(OUT_PIN, HIGH);
}
void send_nexa(const int *light_data) {
digitalWrite(OUT_PIN, HIGH);
delayMicroseconds(NEXA_HIGH_SPACE);
digitalWrite(OUT_PIN, LOW);
delayMicroseconds(NEXA_START_LOW);
short_low_0 = NEXA_SHORT_LOW_0;
long_low_1 = NEXA_LONG_LOW_1;
high_space = NEXA_HIGH_SPACE;
send_bytes(light_data, 4);
digitalWrite(OUT_PIN, HIGH);
delayMicroseconds(NEXA_SHORT_LOW_0);
digitalWrite(OUT_PIN, LOW);
delay(10);
}
void send_bytes(const int *bytes, int len_bytes) {
for(int b = 0; b < len_bytes; b++) {
int data_byte = bytes[b];
//Serial.print(data_byte);
//Serial.print(", ");
for(int i = 8 - 1; i >= 0; i--) {
int data_bit = ((data_byte >> i) & 1);
write_data(data_bit);
}
}
//Serial.println("");
}
// Writes one data bit, which means two bits on the wire.
void write_data(int data_bit) {
//Serial.print(data_bit);
if (data_bit) {
write_wire(0); write_wire(1);
} else {
write_wire(1); write_wire(0);
}
}
void write_wire(int wire_bit) {
//Serial.print(wire_bit);
digitalWrite(OUT_PIN, HIGH);
delayMicroseconds(high_space);
digitalWrite(OUT_PIN, LOW);
delayMicroseconds(wire_bit ? short_low_0 : long_low_1);
}