fixed crlf for project

This commit is contained in:
Henrik Ekblad
2014-03-07 21:17:56 +01:00
parent 586a4d6239
commit c1441dca4a
62 changed files with 6964 additions and 6964 deletions
+80 -80
View File
@@ -1,81 +1,81 @@
RemoteSwitch library v2.3.0 (20121229) for Arduino 1.0
Made by Randy Simons http://randysimons.nl/
This library provides an easy class for Arduino, to send and receive signals
used by some common "old style" 433MHz remote control switches.
There are two styles of remote:
- "old style", which uses switches or a dial to set the house code. Use this
library.
- "new style", which use a button on the receivers to "learn" a signal. Use
the NewRemoteSwitch library instead.
See RemoteTransmitter.h and RemoteReceiver.h for details!
License: GPLv3. See ./RemoteSwitch/license.txt
Latest source and wiki: https://bitbucket.org/fuzzillogic/433mhzforarduino
Installation of library:
- Make sure Arduino is closed
- Copy the directory RemoteSwitch to the Arduino library directory (usually
<Sketchbook directory>/libraries/)
See http://arduino.cc/en/Guide/Libraries for detailed instructions.
Default installation sender & demo:
- Connect tha data-in-pin of a 433MHz transmitter to digital pin 11. See
docs/hardware setup.jpg.
(Note: your hardware may have a different pin configuration!)
- Start Arduino, and open the example: File -> Examples -> RemoteSwitch ->
LightShow
- Alter addresses/devices to reflect your own setup. Otherwise nothing will
happen.
- Compile, upload and run!
Default installation receiver & demo:
- Connect the data-out-pin of a 433MHz receiver to digital pin 2. See photo.
(Note: your hardware may have a different pin configuration!)
- Start Arduino, and open the example: File -> Examples -> RemoteSwitch ->
ShowReceivedCode or ShowReceivedCodeNewKaku, depending on your remote.
- Compile, upload and run
- Open serial monitor in Arduino (115200 baud)
- Press buttons on a 433MHz-remote, and watch the serial monitor
Notes:
- These transmitters often use the PT2262 IC, or equivalent. For details about
the waveforms used by these remotes, the datasheet of the PT2262 is provided.
Page 4 and 5 will be of most interest.
Changelog:
RemoteSwitch library v2.3.0 (20121229) for Arduino 1.0
- Improved reception quality by filtering too short pulses.
- Dropped pre-v1.0 Arduino support.
- Added RemoteReceiver::deinit()
RemoteSwitch library v2.2.1 (20120314) for Arduino 0022/1.0
- Fixed Elro code, it caused memory corruption.
- Renamed old-style .pde files to new-style .ino
RemoteSwitch library v2.2.0 (20120213) for Arduino 0022/1.0
- Added support for Elro switches (http://www.elro.eu/en/m/products/category/home_automation/home_control)
(untested; I don't have these kind of devices)
- Allowed the number of repeated signals transmitted by RemoteTransmitter to be
changed.
- By default, 16 instead of 8 repeats are sent.
- Support for Arduino 1.0.
- Added Datasheet PT2262, which (or equivalent) is often used in the transmitters.
- RemoteReceiver::isReceiving timeout changed to 150ms; 100ms was too short.
RemoteSwitch library v2.1.1 (20110920) for Arduino 0022
- Improved RemoteReceiver::isReceiving
RemoteSwitch library v2.1.0 (20110919) for Arduino 0022
- Changed classnames from *Switch to *Transmitter
- Added RemoteTransmitter::sendCode. See example "retransmitter".
- RemoteReceiver::interruptHandler() is now public, for use with InterruptChain
RemoteSwitch library v2.0.0 (20100130) for Arduino 0017
- Complete rewrite; can now receive and decode signals directly from
RemoteSwitch library v2.3.0 (20121229) for Arduino 1.0
Made by Randy Simons http://randysimons.nl/
This library provides an easy class for Arduino, to send and receive signals
used by some common "old style" 433MHz remote control switches.
There are two styles of remote:
- "old style", which uses switches or a dial to set the house code. Use this
library.
- "new style", which use a button on the receivers to "learn" a signal. Use
the NewRemoteSwitch library instead.
See RemoteTransmitter.h and RemoteReceiver.h for details!
License: GPLv3. See ./RemoteSwitch/license.txt
Latest source and wiki: https://bitbucket.org/fuzzillogic/433mhzforarduino
Installation of library:
- Make sure Arduino is closed
- Copy the directory RemoteSwitch to the Arduino library directory (usually
<Sketchbook directory>/libraries/)
See http://arduino.cc/en/Guide/Libraries for detailed instructions.
Default installation sender & demo:
- Connect tha data-in-pin of a 433MHz transmitter to digital pin 11. See
docs/hardware setup.jpg.
(Note: your hardware may have a different pin configuration!)
- Start Arduino, and open the example: File -> Examples -> RemoteSwitch ->
LightShow
- Alter addresses/devices to reflect your own setup. Otherwise nothing will
happen.
- Compile, upload and run!
Default installation receiver & demo:
- Connect the data-out-pin of a 433MHz receiver to digital pin 2. See photo.
(Note: your hardware may have a different pin configuration!)
- Start Arduino, and open the example: File -> Examples -> RemoteSwitch ->
ShowReceivedCode or ShowReceivedCodeNewKaku, depending on your remote.
- Compile, upload and run
- Open serial monitor in Arduino (115200 baud)
- Press buttons on a 433MHz-remote, and watch the serial monitor
Notes:
- These transmitters often use the PT2262 IC, or equivalent. For details about
the waveforms used by these remotes, the datasheet of the PT2262 is provided.
Page 4 and 5 will be of most interest.
Changelog:
RemoteSwitch library v2.3.0 (20121229) for Arduino 1.0
- Improved reception quality by filtering too short pulses.
- Dropped pre-v1.0 Arduino support.
- Added RemoteReceiver::deinit()
RemoteSwitch library v2.2.1 (20120314) for Arduino 0022/1.0
- Fixed Elro code, it caused memory corruption.
- Renamed old-style .pde files to new-style .ino
RemoteSwitch library v2.2.0 (20120213) for Arduino 0022/1.0
- Added support for Elro switches (http://www.elro.eu/en/m/products/category/home_automation/home_control)
(untested; I don't have these kind of devices)
- Allowed the number of repeated signals transmitted by RemoteTransmitter to be
changed.
- By default, 16 instead of 8 repeats are sent.
- Support for Arduino 1.0.
- Added Datasheet PT2262, which (or equivalent) is often used in the transmitters.
- RemoteReceiver::isReceiving timeout changed to 150ms; 100ms was too short.
RemoteSwitch library v2.1.1 (20110920) for Arduino 0022
- Improved RemoteReceiver::isReceiving
RemoteSwitch library v2.1.0 (20110919) for Arduino 0022
- Changed classnames from *Switch to *Transmitter
- Added RemoteTransmitter::sendCode. See example "retransmitter".
- RemoteReceiver::interruptHandler() is now public, for use with InterruptChain
RemoteSwitch library v2.0.0 (20100130) for Arduino 0017
- Complete rewrite; can now receive and decode signals directly from
the receiver's digital out.
+193 -193
View File
@@ -1,193 +1,193 @@
/*
* RemoteSwitch library v2.3.0 (20121229) made by Randy Simons http://randysimons.nl/
* See RemoteReceiver.h for details.
*
* License: GPLv3. See license.txt
*/
#include "RemoteReceiver.h"
/************
* RemoteReceiver
************/
int8_t RemoteReceiver::_interrupt;
volatile int8_t RemoteReceiver::_state;
byte RemoteReceiver::_minRepeats;
RemoteReceiverCallBack RemoteReceiver::_callback;
boolean RemoteReceiver::_inCallback = false;
boolean RemoteReceiver::_enabled = false;
void RemoteReceiver::init(int8_t interrupt, byte minRepeats, RemoteReceiverCallBack callback) {
_interrupt = interrupt;
_minRepeats = minRepeats;
_callback = callback;
enable();
if (_interrupt >= 0) {
attachInterrupt(_interrupt, interruptHandler, CHANGE);
}
}
void RemoteReceiver::enable() {
_state = -1;
_enabled = true;
}
void RemoteReceiver::disable() {
_enabled = false;
}
void RemoteReceiver::deinit() {
_enabled = false;
if (_interrupt >= 0) {
detachInterrupt(_interrupt);
}
}
void RemoteReceiver::interruptHandler() {
if (!_enabled) {
return;
}
static unsigned int period; // Calculated duration of 1 period
static byte receivedBit; // Contains "bit" currently receiving
static unsigned long receivedCode; // Contains received code
static unsigned long previousCode; // Contains previous received code
static byte repeats = 0; // The number of times the an identical code is received in a row.
static unsigned long edgeTimeStamp[3] = {0, }; // Timestamp of edges
static unsigned int min1Period, max1Period, min3Period, max3Period;
static bool skip;
// Filter out too short pulses. This method works as a low pass filter.
edgeTimeStamp[1] = edgeTimeStamp[2];
edgeTimeStamp[2] = micros();
if (skip) {
skip = false;
return;
}
if (_state >= 0 && edgeTimeStamp[2]-edgeTimeStamp[1] < min1Period) {
// Last edge was too short.
// Skip this edge, and the next too.
skip = true;
return;
}
unsigned int duration = edgeTimeStamp[1] - edgeTimeStamp[0];
edgeTimeStamp[0] = edgeTimeStamp[1];
// Note that if state>=0, duration is always >= 1 period.
if (_state==-1) { // Waiting for sync-signal
if (duration>3720) { // =31*120 minimal time between two edges before decoding starts.
// Sync signal received.. Preparing for decoding
period=duration/31;
receivedCode=previousCode=repeats=0;
// Allow for large error-margin. ElCheapo-hardware :(
min1Period=period*4/10; // Avoid floating point math; saves memory.
max1Period=period*16/10;
min3Period=period*23/10;
max3Period=period*37/10;
}
else {
return;
}
} else if (_state<48) { // Decoding message
receivedBit <<= 1;
// bit part durations can ONLY be 1 or 3 periods.
if (duration<=max1Period) {
receivedBit &= B1110; // Clear LSB of receivedBit
}
else if (duration>=min3Period && duration<=max3Period) {
receivedBit |= B1; // Set LSB of receivedBit
}
else { // Otherwise the entire sequence is invalid
_state=-1;
return;
}
if ((_state%4)==3) { // Last bit part?
// Shift
receivedCode*=3;
// Only 4 LSB's are used; trim the rest.
switch (receivedBit & B1111) {
case B0101: // short long short long == B0101
// bit "0" received
receivedCode+=0; // I hope the optimizer handles this ;)
break;
case B1010: // long short long short == B1010
// bit "1" received
receivedCode+=1;
break;
case B0110: // short long long short
// bit "f" received
receivedCode+=2;
break;
default:
// Bit was rubbish. Abort.
_state=-1;
return;
}
}
} else if (_state==48) { // Waiting for sync bit part 1
// Must be 1 period.
if (duration>max1Period) {
_state=-1;
return;
}
} else { // Waiting for sync bit part 2
// Must be 31 periods.
if (duration<period*25 || duration>period*36) {
_state=-1;
return;
}
// receivedCode is a valid code!
if (receivedCode!=previousCode) {
repeats=0;
previousCode=receivedCode;
}
repeats++;
if (repeats>=_minRepeats) {
if (!_inCallback) {
_inCallback = true;
(_callback)(receivedCode, period);
_inCallback = false;
}
// Reset after callback.
_state=-1;
return;
}
// Reset for next round
receivedCode = 0;
_state=0; // no need to wait for another sync-bit!
return;
}
_state++;
return;
}
boolean RemoteReceiver::isReceiving(int waitMillis) {
unsigned long startTime=millis();
int waited; // Signed int!
do {
if (_state == 48) { // Abort if a valid code has been received in the mean time
return true;
}
waited = (millis()-startTime);
} while(waited>=0 && waited <= waitMillis); // Yes, clock wraps every 50 days. And then you'd have to wait for a looooong time.
return false;
}
/*
* RemoteSwitch library v2.3.0 (20121229) made by Randy Simons http://randysimons.nl/
* See RemoteReceiver.h for details.
*
* License: GPLv3. See license.txt
*/
#include "RemoteReceiver.h"
/************
* RemoteReceiver
************/
int8_t RemoteReceiver::_interrupt;
volatile int8_t RemoteReceiver::_state;
byte RemoteReceiver::_minRepeats;
RemoteReceiverCallBack RemoteReceiver::_callback;
boolean RemoteReceiver::_inCallback = false;
boolean RemoteReceiver::_enabled = false;
void RemoteReceiver::init(int8_t interrupt, byte minRepeats, RemoteReceiverCallBack callback) {
_interrupt = interrupt;
_minRepeats = minRepeats;
_callback = callback;
enable();
if (_interrupt >= 0) {
attachInterrupt(_interrupt, interruptHandler, CHANGE);
}
}
void RemoteReceiver::enable() {
_state = -1;
_enabled = true;
}
void RemoteReceiver::disable() {
_enabled = false;
}
void RemoteReceiver::deinit() {
_enabled = false;
if (_interrupt >= 0) {
detachInterrupt(_interrupt);
}
}
void RemoteReceiver::interruptHandler() {
if (!_enabled) {
return;
}
static unsigned int period; // Calculated duration of 1 period
static byte receivedBit; // Contains "bit" currently receiving
static unsigned long receivedCode; // Contains received code
static unsigned long previousCode; // Contains previous received code
static byte repeats = 0; // The number of times the an identical code is received in a row.
static unsigned long edgeTimeStamp[3] = {0, }; // Timestamp of edges
static unsigned int min1Period, max1Period, min3Period, max3Period;
static bool skip;
// Filter out too short pulses. This method works as a low pass filter.
edgeTimeStamp[1] = edgeTimeStamp[2];
edgeTimeStamp[2] = micros();
if (skip) {
skip = false;
return;
}
if (_state >= 0 && edgeTimeStamp[2]-edgeTimeStamp[1] < min1Period) {
// Last edge was too short.
// Skip this edge, and the next too.
skip = true;
return;
}
unsigned int duration = edgeTimeStamp[1] - edgeTimeStamp[0];
edgeTimeStamp[0] = edgeTimeStamp[1];
// Note that if state>=0, duration is always >= 1 period.
if (_state==-1) { // Waiting for sync-signal
if (duration>3720) { // =31*120 minimal time between two edges before decoding starts.
// Sync signal received.. Preparing for decoding
period=duration/31;
receivedCode=previousCode=repeats=0;
// Allow for large error-margin. ElCheapo-hardware :(
min1Period=period*4/10; // Avoid floating point math; saves memory.
max1Period=period*16/10;
min3Period=period*23/10;
max3Period=period*37/10;
}
else {
return;
}
} else if (_state<48) { // Decoding message
receivedBit <<= 1;
// bit part durations can ONLY be 1 or 3 periods.
if (duration<=max1Period) {
receivedBit &= B1110; // Clear LSB of receivedBit
}
else if (duration>=min3Period && duration<=max3Period) {
receivedBit |= B1; // Set LSB of receivedBit
}
else { // Otherwise the entire sequence is invalid
_state=-1;
return;
}
if ((_state%4)==3) { // Last bit part?
// Shift
receivedCode*=3;
// Only 4 LSB's are used; trim the rest.
switch (receivedBit & B1111) {
case B0101: // short long short long == B0101
// bit "0" received
receivedCode+=0; // I hope the optimizer handles this ;)
break;
case B1010: // long short long short == B1010
// bit "1" received
receivedCode+=1;
break;
case B0110: // short long long short
// bit "f" received
receivedCode+=2;
break;
default:
// Bit was rubbish. Abort.
_state=-1;
return;
}
}
} else if (_state==48) { // Waiting for sync bit part 1
// Must be 1 period.
if (duration>max1Period) {
_state=-1;
return;
}
} else { // Waiting for sync bit part 2
// Must be 31 periods.
if (duration<period*25 || duration>period*36) {
_state=-1;
return;
}
// receivedCode is a valid code!
if (receivedCode!=previousCode) {
repeats=0;
previousCode=receivedCode;
}
repeats++;
if (repeats>=_minRepeats) {
if (!_inCallback) {
_inCallback = true;
(_callback)(receivedCode, period);
_inCallback = false;
}
// Reset after callback.
_state=-1;
return;
}
// Reset for next round
receivedCode = 0;
_state=0; // no need to wait for another sync-bit!
return;
}
_state++;
return;
}
boolean RemoteReceiver::isReceiving(int waitMillis) {
unsigned long startTime=millis();
int waited; // Signed int!
do {
if (_state == 48) { // Abort if a valid code has been received in the mean time
return true;
}
waited = (millis()-startTime);
} while(waited>=0 && waited <= waitMillis); // Yes, clock wraps every 50 days. And then you'd have to wait for a looooong time.
return false;
}
+88 -88
View File
@@ -1,88 +1,88 @@
/*
* RemoteSwitch library v2.3.0 (20121229) made by Randy Simons http://randysimons.nl/
*
* License: GPLv3. See license.txt
*/
#ifndef RemoteReceiver_h
#define RemoteReceiver_h
#include <Arduino.h>
typedef void (*RemoteReceiverCallBack)(unsigned long, unsigned int);
/**
* See RemoteSwitch for introduction.
*
* RemoteReceiver decodes the signal received from a 433MHz-receiver, like the "KlikAanKlikUit"-system
* as well as the signal sent by the RemoteSwtich class. When a correct signal is received,
* a user-defined callback function is called.
*
* Note that in the callback function, the interrupts are still disabled. You can enabled them, if needed.
* A call to the callback must b finished before RemoteReceiver will call the callback function again, thus
* there is no re-entrant problem.
*
* When sending your own code using RemoteSwich, disable() the receiver first.
*
* This is a pure static class, for simplicity and to limit memory-use.
*/
class RemoteReceiver {
public:
/**
* Initializes the decoder.
*
* If interrupt >= 0, init will register pin <interrupt> to this library.
* If interrupt < 0, no interrupt is registered. In that case, you have to call interruptHandler()
* yourself whenever the output of the receiver changes, or you can use InterruptChain.
*
* @param interrupt The interrupt as is used by Arduino's attachInterrupt function. See attachInterrupt for details.
If < 0, you must call interruptHandler() yourself.
* @param minRepeats The number of times the same code must be received in a row before the callback is calles
* @param callback Pointer to a callback function, with signature void (*func)(unsigned long, unsigned int). First parameter is the decoded data, the second the period of the timing.
*/
static void init(int8_t interrupt, byte minRepeats, RemoteReceiverCallBack callback);
/**
* Enable decoding. No need to call enable() after init().
*/
static void enable();
/**
* Disable decoding. You can re-enable decoding by calling enable();
*/
static void disable();
/**
* Deinitializes the decoder. Disables decoding and detaches the interrupt handler. If you want to
* re-enable decoding, call init() again.
*/
static void deinit();
/**
* Tells wether a signal is being received. If a compatible signal is detected within the time out, isReceiving returns true.
* Since it makes no sense to transmit while another transmitter is active, it's best to wait for isReceiving() to false.
* By default it waits for 150ms, in which a (relative slow) KaKu signal can be broadcasted three times.
*
* Note: isReceiving() depends on interrupts enabled. Thus, when disabled()'ed, or when interrupts are disabled (as is
* the case in the callback), isReceiving() will not work properly.
*
* @param waitMillis number of milliseconds to monitor for signal.
* @return boolean If after waitMillis no signal was being processed, returns false. If before expiration a signal was being processed, returns true.
*/
static boolean isReceiving(int waitMillis = 150);
static void interruptHandler();
private:
static int8_t _interrupt; // Radio input interrupt
volatile static int8_t _state; // State of decoding process. There are 49 states, 1 for "waiting for signal" and 48 for decoding the 48 edges in a valid code.
static byte _minRepeats;
static RemoteReceiverCallBack _callback;
static boolean _inCallback; // When true, the callback function is being executed; prevents re-entrance.
static boolean _enabled; // If true, monitoring and decoding is enabled. If false, interruptHandler will return immediately.
};
#endif
/*
* RemoteSwitch library v2.3.0 (20121229) made by Randy Simons http://randysimons.nl/
*
* License: GPLv3. See license.txt
*/
#ifndef RemoteReceiver_h
#define RemoteReceiver_h
#include <Arduino.h>
typedef void (*RemoteReceiverCallBack)(unsigned long, unsigned int);
/**
* See RemoteSwitch for introduction.
*
* RemoteReceiver decodes the signal received from a 433MHz-receiver, like the "KlikAanKlikUit"-system
* as well as the signal sent by the RemoteSwtich class. When a correct signal is received,
* a user-defined callback function is called.
*
* Note that in the callback function, the interrupts are still disabled. You can enabled them, if needed.
* A call to the callback must b finished before RemoteReceiver will call the callback function again, thus
* there is no re-entrant problem.
*
* When sending your own code using RemoteSwich, disable() the receiver first.
*
* This is a pure static class, for simplicity and to limit memory-use.
*/
class RemoteReceiver {
public:
/**
* Initializes the decoder.
*
* If interrupt >= 0, init will register pin <interrupt> to this library.
* If interrupt < 0, no interrupt is registered. In that case, you have to call interruptHandler()
* yourself whenever the output of the receiver changes, or you can use InterruptChain.
*
* @param interrupt The interrupt as is used by Arduino's attachInterrupt function. See attachInterrupt for details.
If < 0, you must call interruptHandler() yourself.
* @param minRepeats The number of times the same code must be received in a row before the callback is calles
* @param callback Pointer to a callback function, with signature void (*func)(unsigned long, unsigned int). First parameter is the decoded data, the second the period of the timing.
*/
static void init(int8_t interrupt, byte minRepeats, RemoteReceiverCallBack callback);
/**
* Enable decoding. No need to call enable() after init().
*/
static void enable();
/**
* Disable decoding. You can re-enable decoding by calling enable();
*/
static void disable();
/**
* Deinitializes the decoder. Disables decoding and detaches the interrupt handler. If you want to
* re-enable decoding, call init() again.
*/
static void deinit();
/**
* Tells wether a signal is being received. If a compatible signal is detected within the time out, isReceiving returns true.
* Since it makes no sense to transmit while another transmitter is active, it's best to wait for isReceiving() to false.
* By default it waits for 150ms, in which a (relative slow) KaKu signal can be broadcasted three times.
*
* Note: isReceiving() depends on interrupts enabled. Thus, when disabled()'ed, or when interrupts are disabled (as is
* the case in the callback), isReceiving() will not work properly.
*
* @param waitMillis number of milliseconds to monitor for signal.
* @return boolean If after waitMillis no signal was being processed, returns false. If before expiration a signal was being processed, returns true.
*/
static boolean isReceiving(int waitMillis = 150);
static void interruptHandler();
private:
static int8_t _interrupt; // Radio input interrupt
volatile static int8_t _state; // State of decoding process. There are 49 states, 1 for "waiting for signal" and 48 for decoding the 48 edges in a valid code.
static byte _minRepeats;
static RemoteReceiverCallBack _callback;
static boolean _inCallback; // When true, the callback function is being executed; prevents re-entrance.
static boolean _enabled; // If true, monitoring and decoding is enabled. If false, interruptHandler will return immediately.
};
#endif
+305 -305
View File
@@ -1,305 +1,305 @@
/*
* RemoteSwitch library v2.3.0 (20121229) made by Randy Simons http://randysimons.nl/
* See RemoteTransmitter.h for details.
*
* License: GPLv3. See license.txt
*/
#include "RemoteTransmitter.h"
/************
* RemoteTransmitter
************/
RemoteTransmitter::RemoteTransmitter(byte pin, unsigned int periodusec, byte repeats) {
_pin=pin;
_periodusec=periodusec;
_repeats=repeats;
pinMode(_pin, OUTPUT);
}
unsigned long RemoteTransmitter::encodeTelegram(byte trits[]) {
unsigned long data = 0;
// Encode data
for (byte i=0;i<12;i++) {
data*=3;
data+=trits[i];
}
// Encode period duration
data |= (unsigned long)_periodusec << 23;
// Encode repeats
data |= (unsigned long)_repeats << 20;
return data;
}
void RemoteTransmitter::sendTelegram(byte trits[]) {
sendTelegram(encodeTelegram(trits),_pin);
}
/**
* Format data:
* pppppppp|prrrdddd|dddddddd|dddddddd (32 bit)
* p = perioud (9 bit unsigned int
* r = repeats as 2log. Thus, if r = 3, then signal is sent 2^3=8 times
* d = data
*/
void RemoteTransmitter::sendTelegram(unsigned long data, byte pin) {
unsigned int periodusec = (unsigned long)data >> 23;
byte repeats = ((unsigned long)data >> 20) & B111;
sendCode(pin, data, periodusec, repeats);
}
void RemoteTransmitter::sendCode(byte pin, unsigned long code, unsigned int periodusec, byte repeats) {
code &= 0xfffff; // Truncate to 20 bit ;
// Convert the base3-code to base4, to avoid lengthy calculations when transmitting.. Messes op timings.
// Also note this swaps endianess in the process. The MSB must be transmitted first, but is converted to
// LSB here. This is easier when actually transmitting later on.
unsigned long dataBase4 = 0;
for (byte i=0; i<12; i++) {
dataBase4<<=2;
dataBase4|=(code%3);
code/=3;
}
repeats = 1 << (repeats & B111); // repeats := 2^repeats;
for (byte j=0;j<repeats;j++) {
// Sent one telegram
// Recycle code as working var to save memory
code=dataBase4;
for (byte i=0; i<12; i++) {
switch (code & B11) {
case 0:
digitalWrite(pin, HIGH);
delayMicroseconds(periodusec);
digitalWrite(pin, LOW);
delayMicroseconds(periodusec*3);
digitalWrite(pin, HIGH);
delayMicroseconds(periodusec);
digitalWrite(pin, LOW);
delayMicroseconds(periodusec*3);
break;
case 1:
digitalWrite(pin, HIGH);
delayMicroseconds(periodusec*3);
digitalWrite(pin, LOW);
delayMicroseconds(periodusec);
digitalWrite(pin, HIGH);
delayMicroseconds(periodusec*3);
digitalWrite(pin, LOW);
delayMicroseconds(periodusec);
break;
case 2: // KA: X or float
digitalWrite(pin, HIGH);
delayMicroseconds(periodusec);
digitalWrite(pin, LOW);
delayMicroseconds(periodusec*3);
digitalWrite(pin, HIGH);
delayMicroseconds(periodusec*3);
digitalWrite(pin, LOW);
delayMicroseconds(periodusec);
break;
}
// Next trit
code>>=2;
}
// Send termination/synchronization-signal. Total length: 32 periods
digitalWrite(pin, HIGH);
delayMicroseconds(periodusec);
digitalWrite(pin, LOW);
delayMicroseconds(periodusec*31);
}
}
boolean RemoteTransmitter::isSameCode(unsigned long encodedTelegram, unsigned long receivedData) {
return (receivedData==(encodedTelegram & 0xFFFFF)); // compare the 20 LSB's
}
/************
* ActionTransmitter
************/
ActionTransmitter::ActionTransmitter(byte pin, unsigned int periodusec, byte repeats) : RemoteTransmitter(pin,periodusec,repeats) {
// Call constructor
}
void ActionTransmitter::sendSignal(byte systemCode, char device, boolean on) {
sendTelegram(getTelegram(systemCode,device,on), _pin);
}
unsigned long ActionTransmitter::getTelegram(byte systemCode, char device, boolean on) {
byte trits[12];
device-=65;
for (byte i=0; i<5; i++) {
// Trits 0-4 contain address (2^5=32 addresses)
trits[i]=(systemCode & 1)?1:2;
systemCode>>=1;
// Trits 5-9 contain device. Only one trit has value 0, others have 2 (float)!
trits[i+5]=(i==device?0:2);
}
// Switch on or off
trits[10]=(!on?0:2);
trits[11]=(on?0:2);
return encodeTelegram(trits);
}
/************
* BlokkerTransmitter
************/
BlokkerTransmitter::BlokkerTransmitter(byte pin, unsigned int periodusec, byte repeats) : RemoteTransmitter(pin,periodusec,repeats) {
// Call constructor
}
void BlokkerTransmitter::sendSignal(byte device, boolean on) {
sendTelegram(getTelegram(device,on), _pin);
}
unsigned long BlokkerTransmitter::getTelegram(byte device, boolean on) {
byte trits[12]={0};
device--;
for (byte i=1; i<4; i++) {
// Trits 1-3 contain device
trits[i]=(device & 1)?0:1;
device>>=1;
}
// Switch on or off
trits[8]=(on?1:0);
return encodeTelegram(trits);
}
/************
* KaKuTransmitter
************/
KaKuTransmitter::KaKuTransmitter(byte pin, unsigned int periodusec, byte repeats) : RemoteTransmitter(pin,periodusec,repeats) {
// Call constructor
}
void KaKuTransmitter::sendSignal(char address, byte device, boolean on) {
sendTelegram(getTelegram(address, device, on), _pin);
}
unsigned long KaKuTransmitter::getTelegram(char address, byte device, boolean on) {
byte trits[12];
address-=65;
device-=1;
for (byte i=0; i<4; i++) {
// Trits 0-3 contain address (2^4 = 16 addresses)
trits[i]=(address & 1)?2:0;
address>>=1;
// Trits 4-8 contain device (2^4 = 16 addresses)
trits[i+4]=(device & 1)?2:0;
device>>=1;
}
// Trits 8-10 seem to be fixed
trits[8]=0;
trits[9]=2;
trits[10]=2;
// Switch on or off
trits[11]=(on?2:0);
return encodeTelegram(trits);
}
void KaKuTransmitter::sendSignal(char address, byte group, byte device, boolean on) {
sendTelegram(getTelegram(address, group, on), _pin);
}
unsigned long KaKuTransmitter::getTelegram(char address, byte group, byte device, boolean on) {
byte trits[12], i;
address-=65;
group-=1;
device-=1;
// Address. M3E Pin A0-A3
for (i=0; i<4; i++) {
// Trits 0-3 contain address (2^4 = 16 addresses)
trits[i]=(address & 1)?2:0;
address>>=1;
}
// Device. M3E Pin A4-A5
for (; i<6; i++) {
trits[i]=(device & 1)?2:0;
device>>=1;
}
// Group. M3E Pin A6-A7
for (; i<8; i++) {
trits[i]=(group & 1)?2:0;
group>>=1;
}
// Trits 8-10 are be fixed. M3E Pin A8/D0-A10/D2
trits[8]=0;
trits[9]=2;
trits[10]=2;
// Switch on or off, M3E Pin A11/D3
trits[11]=(on?2:0);
return encodeTelegram(trits);
}
/************
* ElroTransmitter
************/
ElroTransmitter::ElroTransmitter(byte pin, unsigned int periodusec, byte repeats) : RemoteTransmitter(pin, periodusec, repeats) {
//Call constructor
}
void ElroTransmitter::sendSignal(byte systemCode, char device, boolean on) {
sendTelegram(getTelegram(systemCode, device, on), _pin);
}
unsigned long ElroTransmitter::getTelegram(byte systemCode, char device, boolean on) {
byte trits[12];
device-=65;
for (byte i=0; i<5; i++) {
//trits 0-4 contain address (2^5=32 addresses)
trits[i]=(systemCode & 1)?0:2;
systemCode>>=1;
//trits 5-9 contain device. Only one trit has value 0, others have 2 (float)!
trits[i+5]=(i==device?0:2);
}
//switch on or off
trits[10]=(on?0:2);
trits[11]=(!on?0:2);
return encodeTelegram(trits);
}
/*
* RemoteSwitch library v2.3.0 (20121229) made by Randy Simons http://randysimons.nl/
* See RemoteTransmitter.h for details.
*
* License: GPLv3. See license.txt
*/
#include "RemoteTransmitter.h"
/************
* RemoteTransmitter
************/
RemoteTransmitter::RemoteTransmitter(byte pin, unsigned int periodusec, byte repeats) {
_pin=pin;
_periodusec=periodusec;
_repeats=repeats;
pinMode(_pin, OUTPUT);
}
unsigned long RemoteTransmitter::encodeTelegram(byte trits[]) {
unsigned long data = 0;
// Encode data
for (byte i=0;i<12;i++) {
data*=3;
data+=trits[i];
}
// Encode period duration
data |= (unsigned long)_periodusec << 23;
// Encode repeats
data |= (unsigned long)_repeats << 20;
return data;
}
void RemoteTransmitter::sendTelegram(byte trits[]) {
sendTelegram(encodeTelegram(trits),_pin);
}
/**
* Format data:
* pppppppp|prrrdddd|dddddddd|dddddddd (32 bit)
* p = perioud (9 bit unsigned int
* r = repeats as 2log. Thus, if r = 3, then signal is sent 2^3=8 times
* d = data
*/
void RemoteTransmitter::sendTelegram(unsigned long data, byte pin) {
unsigned int periodusec = (unsigned long)data >> 23;
byte repeats = ((unsigned long)data >> 20) & B111;
sendCode(pin, data, periodusec, repeats);
}
void RemoteTransmitter::sendCode(byte pin, unsigned long code, unsigned int periodusec, byte repeats) {
code &= 0xfffff; // Truncate to 20 bit ;
// Convert the base3-code to base4, to avoid lengthy calculations when transmitting.. Messes op timings.
// Also note this swaps endianess in the process. The MSB must be transmitted first, but is converted to
// LSB here. This is easier when actually transmitting later on.
unsigned long dataBase4 = 0;
for (byte i=0; i<12; i++) {
dataBase4<<=2;
dataBase4|=(code%3);
code/=3;
}
repeats = 1 << (repeats & B111); // repeats := 2^repeats;
for (byte j=0;j<repeats;j++) {
// Sent one telegram
// Recycle code as working var to save memory
code=dataBase4;
for (byte i=0; i<12; i++) {
switch (code & B11) {
case 0:
digitalWrite(pin, HIGH);
delayMicroseconds(periodusec);
digitalWrite(pin, LOW);
delayMicroseconds(periodusec*3);
digitalWrite(pin, HIGH);
delayMicroseconds(periodusec);
digitalWrite(pin, LOW);
delayMicroseconds(periodusec*3);
break;
case 1:
digitalWrite(pin, HIGH);
delayMicroseconds(periodusec*3);
digitalWrite(pin, LOW);
delayMicroseconds(periodusec);
digitalWrite(pin, HIGH);
delayMicroseconds(periodusec*3);
digitalWrite(pin, LOW);
delayMicroseconds(periodusec);
break;
case 2: // KA: X or float
digitalWrite(pin, HIGH);
delayMicroseconds(periodusec);
digitalWrite(pin, LOW);
delayMicroseconds(periodusec*3);
digitalWrite(pin, HIGH);
delayMicroseconds(periodusec*3);
digitalWrite(pin, LOW);
delayMicroseconds(periodusec);
break;
}
// Next trit
code>>=2;
}
// Send termination/synchronization-signal. Total length: 32 periods
digitalWrite(pin, HIGH);
delayMicroseconds(periodusec);
digitalWrite(pin, LOW);
delayMicroseconds(periodusec*31);
}
}
boolean RemoteTransmitter::isSameCode(unsigned long encodedTelegram, unsigned long receivedData) {
return (receivedData==(encodedTelegram & 0xFFFFF)); // compare the 20 LSB's
}
/************
* ActionTransmitter
************/
ActionTransmitter::ActionTransmitter(byte pin, unsigned int periodusec, byte repeats) : RemoteTransmitter(pin,periodusec,repeats) {
// Call constructor
}
void ActionTransmitter::sendSignal(byte systemCode, char device, boolean on) {
sendTelegram(getTelegram(systemCode,device,on), _pin);
}
unsigned long ActionTransmitter::getTelegram(byte systemCode, char device, boolean on) {
byte trits[12];
device-=65;
for (byte i=0; i<5; i++) {
// Trits 0-4 contain address (2^5=32 addresses)
trits[i]=(systemCode & 1)?1:2;
systemCode>>=1;
// Trits 5-9 contain device. Only one trit has value 0, others have 2 (float)!
trits[i+5]=(i==device?0:2);
}
// Switch on or off
trits[10]=(!on?0:2);
trits[11]=(on?0:2);
return encodeTelegram(trits);
}
/************
* BlokkerTransmitter
************/
BlokkerTransmitter::BlokkerTransmitter(byte pin, unsigned int periodusec, byte repeats) : RemoteTransmitter(pin,periodusec,repeats) {
// Call constructor
}
void BlokkerTransmitter::sendSignal(byte device, boolean on) {
sendTelegram(getTelegram(device,on), _pin);
}
unsigned long BlokkerTransmitter::getTelegram(byte device, boolean on) {
byte trits[12]={0};
device--;
for (byte i=1; i<4; i++) {
// Trits 1-3 contain device
trits[i]=(device & 1)?0:1;
device>>=1;
}
// Switch on or off
trits[8]=(on?1:0);
return encodeTelegram(trits);
}
/************
* KaKuTransmitter
************/
KaKuTransmitter::KaKuTransmitter(byte pin, unsigned int periodusec, byte repeats) : RemoteTransmitter(pin,periodusec,repeats) {
// Call constructor
}
void KaKuTransmitter::sendSignal(char address, byte device, boolean on) {
sendTelegram(getTelegram(address, device, on), _pin);
}
unsigned long KaKuTransmitter::getTelegram(char address, byte device, boolean on) {
byte trits[12];
address-=65;
device-=1;
for (byte i=0; i<4; i++) {
// Trits 0-3 contain address (2^4 = 16 addresses)
trits[i]=(address & 1)?2:0;
address>>=1;
// Trits 4-8 contain device (2^4 = 16 addresses)
trits[i+4]=(device & 1)?2:0;
device>>=1;
}
// Trits 8-10 seem to be fixed
trits[8]=0;
trits[9]=2;
trits[10]=2;
// Switch on or off
trits[11]=(on?2:0);
return encodeTelegram(trits);
}
void KaKuTransmitter::sendSignal(char address, byte group, byte device, boolean on) {
sendTelegram(getTelegram(address, group, on), _pin);
}
unsigned long KaKuTransmitter::getTelegram(char address, byte group, byte device, boolean on) {
byte trits[12], i;
address-=65;
group-=1;
device-=1;
// Address. M3E Pin A0-A3
for (i=0; i<4; i++) {
// Trits 0-3 contain address (2^4 = 16 addresses)
trits[i]=(address & 1)?2:0;
address>>=1;
}
// Device. M3E Pin A4-A5
for (; i<6; i++) {
trits[i]=(device & 1)?2:0;
device>>=1;
}
// Group. M3E Pin A6-A7
for (; i<8; i++) {
trits[i]=(group & 1)?2:0;
group>>=1;
}
// Trits 8-10 are be fixed. M3E Pin A8/D0-A10/D2
trits[8]=0;
trits[9]=2;
trits[10]=2;
// Switch on or off, M3E Pin A11/D3
trits[11]=(on?2:0);
return encodeTelegram(trits);
}
/************
* ElroTransmitter
************/
ElroTransmitter::ElroTransmitter(byte pin, unsigned int periodusec, byte repeats) : RemoteTransmitter(pin, periodusec, repeats) {
//Call constructor
}
void ElroTransmitter::sendSignal(byte systemCode, char device, boolean on) {
sendTelegram(getTelegram(systemCode, device, on), _pin);
}
unsigned long ElroTransmitter::getTelegram(byte systemCode, char device, boolean on) {
byte trits[12];
device-=65;
for (byte i=0; i<5; i++) {
//trits 0-4 contain address (2^5=32 addresses)
trits[i]=(systemCode & 1)?0:2;
systemCode>>=1;
//trits 5-9 contain device. Only one trit has value 0, others have 2 (float)!
trits[i+5]=(i==device?0:2);
}
//switch on or off
trits[10]=(on?0:2);
trits[11]=(!on?0:2);
return encodeTelegram(trits);
}
+249 -249
View File
@@ -1,249 +1,249 @@
/*
* RemoteSwitch library v2.3.0 (20121229) made by Randy Simons http://randysimons.nl/
*
* License: GPLv3. See license.txt
*/
#ifndef RemoteTransmitter_h
#define RemoteTransmitter_h
#include <Arduino.h>
/**
* RemoteTransmitter provides a generic class for simulation of common RF remote controls, like the 'Klik aan Klik uit'-system
* (http://www.klikaanklikuit.nl/), used to remotely switch lights etc.
*
* Many of these remotes seem to use a 433MHz SAW resonator and one of these chips: LP801B, HX2262, PT2262, M3E.
* Datasheet for the HX2262/PT2262 ICs:
* http://www.princeton.com.tw/downloadprocess/downloadfile.asp?mydownload=PT2262_1.pdf
*
* Hardware required for this library: a 433MHz/434MHz SAW oscillator transmitter, e.g.
* http://www.sparkfun.com/products/10534
* http://www.conrad.nl/goto/?product=130428
*
* Notes:
* - Since these chips use (and send!) tri-state inputs (low, high and floating) I use 'trits' instead of 'bits',
* when appropriate.
* - I measured the period lengths with a scope. Thus: they work for my remotes, but may fail for yours...
* A better way would be to calculate the 'target'-timings using the datasheets and the resistor-values on the remotes.
*/
class RemoteTransmitter {
public:
/**
* Constructor.
*
* To obtain the correct period length, an oscilloscope is convenient, but you can also read the
* datasheet of the transmitter, measure the resistor for the oscillator and calculate the frequency.
*
* @param pin output pin on Arduino to which the transmitter is connected
* @param periodusec [0..511] Duration of one period, in microseconds. A trit is 6 periods.
* @param repeats [0..7] The 2log-Number of times the signal is repeated. The actual number of repeats will be 2^repeats. 2 would be bare minimum, 4 seems robust.
*/
RemoteTransmitter(byte pin, unsigned int periodusec, byte repeats);
/**
* Encodes the data base on the current object and the given trits. The data can be reused, e.g.
* for use with the static version of sendTelegram, so you won't need to instantiate costly objects!
*
* @return The data suited for use with RemoteTransmitter::sendTelegram.
*/
unsigned long encodeTelegram(byte trits[]);
/**
* Send a telegram, including synchronization-part.
*
* @param trits Array of size 12. "trits" should be either 0, 1 or 2, where 2 indicates "float"
*/
void sendTelegram(byte trits[]);
/**
* Send a telegram, including synchronization-part. The data-param encodes the period duration, number of repeats and the actual data.
* Note: static method, which allows for use in low-mem situations.
*
* Format data:
* pppppppp|prrrdddd|dddddddd|dddddddd (32 bit)
* p = period (9 bit unsigned integer)
* r = repeats as 2log. Thus, if r = 3, then signal is sent 2^3=8 times
* d = data
*
* @param data data, period and repeats.
* @param pin Pin number of the transmitter.
*/
static void sendTelegram(unsigned long data, byte pin);
/**
* The complement of RemoteReceiver. Send a telegram with specified code as data.
*
* Note: static method, which allows for use in low-mem situations.
*
* @param pin output pin on Arduino to which the transmitter is connected
* @param periodusec [0..511] Duration of one period, in microseconds. A trit is 6 periods.
* @param repeats [0..7] The 2log-Number of times the signal is repeated. The actual number of repeats will be 2^repeats. 2 would be bare minimum, 4 seems robust.
* @param code The code to transmit. Note: only the first 20 bits are used, the rest is ignored. Also see sendTelegram.
*/
static void sendCode(byte pin, unsigned long code, unsigned int periodusec, byte repeats);
/**
* Compares the data received with RemoteReceive with the data obtained by one of the getTelegram-functions.
* Period duration and repetitions are ignored by this function; only the data-payload is compared.
*
* @return true, if the codes are identical (the 20 least significant bits match)
*/
static boolean isSameCode(unsigned long encodedTelegram, unsigned long receivedData);
protected:
byte _pin; // Transmitter output pin
unsigned int _periodusec; // Oscillator period in microseconds
byte _repeats; // Number over repetitions of one telegram
};
/**
* ActionTransmitter simulates a remote, as sold in the Dutch 'Action' stores. But there are many similar systems on the market.
* If your remote has setting for 5 address bits, and can control 5 devices on or off, then you can try to use the ActionTransmitter.
* Otherwise you may have luck with the ElroTransmitter, which is similar.
*/
class ActionTransmitter: RemoteTransmitter {
public:
/**
* Constructor
*
* @param pin output pin on Arduino to which the transmitter is connected
* @param periodsec Duration of one period, in microseconds. Default is 190usec
* @param repeats [0..7] The 2log-Number of times the signal is repeated.
* @see RemoteTransmitter
*/
ActionTransmitter(byte pin, unsigned int periodusec=190, byte repeats=4);
/**
* Send a on or off signal to a device.
*
* @param systemCode 5-bit address (dip switches in remote). Range [0..31]
* @param device Device to switch. Range: [A..E] (case sensitive!)
* @param on True, to switch on. False to switch off,
*/
void sendSignal(byte systemCode, char device, boolean on);
/**
* Generates the telegram (data) which can be used for RemoteTransmitter::sendTelegram.
* See sendSignal for details on the parameters
*
* @return Encoded data, including repeats and period duration.
*/
unsigned long getTelegram(byte systemCode, char device, boolean on);
};
/**
* BlokkerTransmitter simulates a remote, as sold in the Dutch 'Blokker' stores. But there are many similar systems on the market.
* These remotes have 4 on, 4 off buttons and a switch to switch between device 1-4 and 5-8. No further configuration
* possible.
*/
class BlokkerTransmitter: RemoteTransmitter {
public:
/**
* Constructor
*
* @param pin output pin on Arduino to which the transmitter is connected
* @param periodsec Duration of one period, in microseconds. Default is 307usec
* @param repeats [0..7] The 2log-Number of times the signal is repeated.
* @see RemoteTransmitter
*/
BlokkerTransmitter(byte pin, unsigned int periodusec=230, byte repeats=4);
/**
* Send a on or off signal to a device.
*
* @param device Device to switch. Range: [1..8]
* @param on True, to switch on. False to switch off,
*/
void sendSignal(byte device, boolean on);
/**
* @see RemoteTransmitter::getTelegram
*/
unsigned long getTelegram(byte device, boolean on);
};
/**
* KaKuTransmitter simulates a KlikAanKlikUit-remote, but there are many clones.
* If your transmitter has a address dial with the characters A till P, you can try this class.
*/
class KaKuTransmitter: RemoteTransmitter {
public:
/**
* Constructor
*
* @param pin output pin on Arduino to which the transmitter is connected
* @param periodsec Duration of one period, in microseconds. Default is 375usec
* @param repeats [0..7] The 2log-Number of times the signal is repeated.
* @see RemoteTransmitter
*/
KaKuTransmitter(byte pin, unsigned int periodusec=375, byte repeats=4);
/**
* Send a on or off signal to a device.
*
* @param address address (dial switches in remote). Range [A..P] (case sensitive!)
* @param group Group to switch. Range: [1..4]
* @param device Device to switch. Range: [1..4]
* @param on True, to switch on. False to switch off,
*/
void sendSignal(char address, byte group, byte device, boolean on);
/**
* Send a on or off signal to a device.
*
* @param address address (dip switches in remote). Range [A..P] (case sensitive!)
* @param device device (dial switches in remote). Range [1..16]
* @param on True, to switch on. False to switch off,
*/
void sendSignal(char address, byte device, boolean on);
/**
* @see RemoteTransmitter::getTelegram
*/
unsigned long getTelegram(char address, byte group, byte device, boolean on);
/**
* @see RemoteTransmitter::getTelegram
*/
unsigned long getTelegram(char address, byte device, boolean on);
};
/**
* ElroTransmitter simulates remotes of the Elro "Home Control" series
* see http://www.elro.eu/en/m/products/category/home_automation/home_control
* There are are many similar systems on the market. If your remote has setting for 5 address bits, and can control
* 4 devices on or off, then you can try to use the ElroTransmitter. Otherwise you may have luck with the
* ActionTransmitter, which is similar.
*/
class ElroTransmitter: RemoteTransmitter {
public:
/**
* Constructor
*
* @param pin output pin on Arduino to which the transmitter is connected
* @param periodsec Duration of one period, in microseconds. Default is 320usec
* @param repeats [0..7] The 2log-Number of times the signal is repeated.
* @see RemoteSwitch
*/
ElroTransmitter(byte pin, unsigned int periodusec=320, byte repeats=4);
/**
* Send a on or off signal to a device.
*
* @param systemCode 5-bit address (dip switches in remote). Range [0..31]
* @param device Device to switch. Range: [A..D] (case sensitive!)
* @param on True, to switch on. False to switch off,
*/
void sendSignal(byte systemCode, char device, boolean on);
/**
* Generates the telegram (data) which can be used for RemoteSwitch::sendTelegram.
* See sendSignal for details on the parameters
*
* @return Encoded data, including repeats and period duration.
*/
unsigned long getTelegram(byte systemCode, char device, boolean on);
};
#endif
/*
* RemoteSwitch library v2.3.0 (20121229) made by Randy Simons http://randysimons.nl/
*
* License: GPLv3. See license.txt
*/
#ifndef RemoteTransmitter_h
#define RemoteTransmitter_h
#include <Arduino.h>
/**
* RemoteTransmitter provides a generic class for simulation of common RF remote controls, like the 'Klik aan Klik uit'-system
* (http://www.klikaanklikuit.nl/), used to remotely switch lights etc.
*
* Many of these remotes seem to use a 433MHz SAW resonator and one of these chips: LP801B, HX2262, PT2262, M3E.
* Datasheet for the HX2262/PT2262 ICs:
* http://www.princeton.com.tw/downloadprocess/downloadfile.asp?mydownload=PT2262_1.pdf
*
* Hardware required for this library: a 433MHz/434MHz SAW oscillator transmitter, e.g.
* http://www.sparkfun.com/products/10534
* http://www.conrad.nl/goto/?product=130428
*
* Notes:
* - Since these chips use (and send!) tri-state inputs (low, high and floating) I use 'trits' instead of 'bits',
* when appropriate.
* - I measured the period lengths with a scope. Thus: they work for my remotes, but may fail for yours...
* A better way would be to calculate the 'target'-timings using the datasheets and the resistor-values on the remotes.
*/
class RemoteTransmitter {
public:
/**
* Constructor.
*
* To obtain the correct period length, an oscilloscope is convenient, but you can also read the
* datasheet of the transmitter, measure the resistor for the oscillator and calculate the frequency.
*
* @param pin output pin on Arduino to which the transmitter is connected
* @param periodusec [0..511] Duration of one period, in microseconds. A trit is 6 periods.
* @param repeats [0..7] The 2log-Number of times the signal is repeated. The actual number of repeats will be 2^repeats. 2 would be bare minimum, 4 seems robust.
*/
RemoteTransmitter(byte pin, unsigned int periodusec, byte repeats);
/**
* Encodes the data base on the current object and the given trits. The data can be reused, e.g.
* for use with the static version of sendTelegram, so you won't need to instantiate costly objects!
*
* @return The data suited for use with RemoteTransmitter::sendTelegram.
*/
unsigned long encodeTelegram(byte trits[]);
/**
* Send a telegram, including synchronization-part.
*
* @param trits Array of size 12. "trits" should be either 0, 1 or 2, where 2 indicates "float"
*/
void sendTelegram(byte trits[]);
/**
* Send a telegram, including synchronization-part. The data-param encodes the period duration, number of repeats and the actual data.
* Note: static method, which allows for use in low-mem situations.
*
* Format data:
* pppppppp|prrrdddd|dddddddd|dddddddd (32 bit)
* p = period (9 bit unsigned integer)
* r = repeats as 2log. Thus, if r = 3, then signal is sent 2^3=8 times
* d = data
*
* @param data data, period and repeats.
* @param pin Pin number of the transmitter.
*/
static void sendTelegram(unsigned long data, byte pin);
/**
* The complement of RemoteReceiver. Send a telegram with specified code as data.
*
* Note: static method, which allows for use in low-mem situations.
*
* @param pin output pin on Arduino to which the transmitter is connected
* @param periodusec [0..511] Duration of one period, in microseconds. A trit is 6 periods.
* @param repeats [0..7] The 2log-Number of times the signal is repeated. The actual number of repeats will be 2^repeats. 2 would be bare minimum, 4 seems robust.
* @param code The code to transmit. Note: only the first 20 bits are used, the rest is ignored. Also see sendTelegram.
*/
static void sendCode(byte pin, unsigned long code, unsigned int periodusec, byte repeats);
/**
* Compares the data received with RemoteReceive with the data obtained by one of the getTelegram-functions.
* Period duration and repetitions are ignored by this function; only the data-payload is compared.
*
* @return true, if the codes are identical (the 20 least significant bits match)
*/
static boolean isSameCode(unsigned long encodedTelegram, unsigned long receivedData);
protected:
byte _pin; // Transmitter output pin
unsigned int _periodusec; // Oscillator period in microseconds
byte _repeats; // Number over repetitions of one telegram
};
/**
* ActionTransmitter simulates a remote, as sold in the Dutch 'Action' stores. But there are many similar systems on the market.
* If your remote has setting for 5 address bits, and can control 5 devices on or off, then you can try to use the ActionTransmitter.
* Otherwise you may have luck with the ElroTransmitter, which is similar.
*/
class ActionTransmitter: RemoteTransmitter {
public:
/**
* Constructor
*
* @param pin output pin on Arduino to which the transmitter is connected
* @param periodsec Duration of one period, in microseconds. Default is 190usec
* @param repeats [0..7] The 2log-Number of times the signal is repeated.
* @see RemoteTransmitter
*/
ActionTransmitter(byte pin, unsigned int periodusec=190, byte repeats=4);
/**
* Send a on or off signal to a device.
*
* @param systemCode 5-bit address (dip switches in remote). Range [0..31]
* @param device Device to switch. Range: [A..E] (case sensitive!)
* @param on True, to switch on. False to switch off,
*/
void sendSignal(byte systemCode, char device, boolean on);
/**
* Generates the telegram (data) which can be used for RemoteTransmitter::sendTelegram.
* See sendSignal for details on the parameters
*
* @return Encoded data, including repeats and period duration.
*/
unsigned long getTelegram(byte systemCode, char device, boolean on);
};
/**
* BlokkerTransmitter simulates a remote, as sold in the Dutch 'Blokker' stores. But there are many similar systems on the market.
* These remotes have 4 on, 4 off buttons and a switch to switch between device 1-4 and 5-8. No further configuration
* possible.
*/
class BlokkerTransmitter: RemoteTransmitter {
public:
/**
* Constructor
*
* @param pin output pin on Arduino to which the transmitter is connected
* @param periodsec Duration of one period, in microseconds. Default is 307usec
* @param repeats [0..7] The 2log-Number of times the signal is repeated.
* @see RemoteTransmitter
*/
BlokkerTransmitter(byte pin, unsigned int periodusec=230, byte repeats=4);
/**
* Send a on or off signal to a device.
*
* @param device Device to switch. Range: [1..8]
* @param on True, to switch on. False to switch off,
*/
void sendSignal(byte device, boolean on);
/**
* @see RemoteTransmitter::getTelegram
*/
unsigned long getTelegram(byte device, boolean on);
};
/**
* KaKuTransmitter simulates a KlikAanKlikUit-remote, but there are many clones.
* If your transmitter has a address dial with the characters A till P, you can try this class.
*/
class KaKuTransmitter: RemoteTransmitter {
public:
/**
* Constructor
*
* @param pin output pin on Arduino to which the transmitter is connected
* @param periodsec Duration of one period, in microseconds. Default is 375usec
* @param repeats [0..7] The 2log-Number of times the signal is repeated.
* @see RemoteTransmitter
*/
KaKuTransmitter(byte pin, unsigned int periodusec=375, byte repeats=4);
/**
* Send a on or off signal to a device.
*
* @param address address (dial switches in remote). Range [A..P] (case sensitive!)
* @param group Group to switch. Range: [1..4]
* @param device Device to switch. Range: [1..4]
* @param on True, to switch on. False to switch off,
*/
void sendSignal(char address, byte group, byte device, boolean on);
/**
* Send a on or off signal to a device.
*
* @param address address (dip switches in remote). Range [A..P] (case sensitive!)
* @param device device (dial switches in remote). Range [1..16]
* @param on True, to switch on. False to switch off,
*/
void sendSignal(char address, byte device, boolean on);
/**
* @see RemoteTransmitter::getTelegram
*/
unsigned long getTelegram(char address, byte group, byte device, boolean on);
/**
* @see RemoteTransmitter::getTelegram
*/
unsigned long getTelegram(char address, byte device, boolean on);
};
/**
* ElroTransmitter simulates remotes of the Elro "Home Control" series
* see http://www.elro.eu/en/m/products/category/home_automation/home_control
* There are are many similar systems on the market. If your remote has setting for 5 address bits, and can control
* 4 devices on or off, then you can try to use the ElroTransmitter. Otherwise you may have luck with the
* ActionTransmitter, which is similar.
*/
class ElroTransmitter: RemoteTransmitter {
public:
/**
* Constructor
*
* @param pin output pin on Arduino to which the transmitter is connected
* @param periodsec Duration of one period, in microseconds. Default is 320usec
* @param repeats [0..7] The 2log-Number of times the signal is repeated.
* @see RemoteSwitch
*/
ElroTransmitter(byte pin, unsigned int periodusec=320, byte repeats=4);
/**
* Send a on or off signal to a device.
*
* @param systemCode 5-bit address (dip switches in remote). Range [0..31]
* @param device Device to switch. Range: [A..D] (case sensitive!)
* @param on True, to switch on. False to switch off,
*/
void sendSignal(byte systemCode, char device, boolean on);
/**
* Generates the telegram (data) which can be used for RemoteSwitch::sendTelegram.
* See sendSignal for details on the parameters
*
* @return Encoded data, including repeats and period duration.
*/
unsigned long getTelegram(byte systemCode, char device, boolean on);
};
#endif
@@ -1,63 +1,63 @@
/*
* Demo for RF remote switch transmitter.
* For details, see RemoteTransmitter.h!
*
* This sketch switches some devices on and off in a loop.
*
* Setup:
* - Connect a 433MHz transmitter to digital pin 11.
*/
#include <RemoteTransmitter.h>
// Intantiate a new ActionTransmitter remote, use pin 11
ActionTransmitter actionTransmitter(11);
// Intantiate a new KaKuTransmitter remote, also use pin 11 (same transmitter!)
KaKuTransmitter kaKuTransmitter(11);
// Intantiate a new Blokker remote, also use pin 11 (same transmitter!)
BlokkerTransmitter blokkerTransmitter(11);
// Intantiate a new Elro remote, also use pin 11 (same transmitter!)
ElroTransmitter elroTransmitter(11);
void setup() {
}
void loop() {
// Switch off KaKu-device 10 on address M
kaKuTransmitter.sendSignal('M',10,false);
// Switch on Action-device B on system code 1.
actionTransmitter.sendSignal(1,'B',true);
// Switch on Blokker-device 7.
blokkerTransmitter.sendSignal(7,true);
// Switch on Elro-device C on system code 1.
elroTransmitter.sendSignal(1,'C',true);
// Wait 2 seconds
delay(2000);
// Switch on KaKu-device 2 of group 3 on address M (which is the same as device 10 on address M!)
kaKuTransmitter.sendSignal('M',3,2,true);
// Switch off Action-device B on system code 1.
actionTransmitter.sendSignal(1,'B',false);
// Switch off Blokker-device 7.
blokkerTransmitter.sendSignal(7,false);
// Switch off Elro-device C on system code 1.
elroTransmitter.sendSignal(1,'C',false);
// Wait 4 seconds
delay(4000);
}
/*
* Demo for RF remote switch transmitter.
* For details, see RemoteTransmitter.h!
*
* This sketch switches some devices on and off in a loop.
*
* Setup:
* - Connect a 433MHz transmitter to digital pin 11.
*/
#include <RemoteTransmitter.h>
// Intantiate a new ActionTransmitter remote, use pin 11
ActionTransmitter actionTransmitter(11);
// Intantiate a new KaKuTransmitter remote, also use pin 11 (same transmitter!)
KaKuTransmitter kaKuTransmitter(11);
// Intantiate a new Blokker remote, also use pin 11 (same transmitter!)
BlokkerTransmitter blokkerTransmitter(11);
// Intantiate a new Elro remote, also use pin 11 (same transmitter!)
ElroTransmitter elroTransmitter(11);
void setup() {
}
void loop() {
// Switch off KaKu-device 10 on address M
kaKuTransmitter.sendSignal('M',10,false);
// Switch on Action-device B on system code 1.
actionTransmitter.sendSignal(1,'B',true);
// Switch on Blokker-device 7.
blokkerTransmitter.sendSignal(7,true);
// Switch on Elro-device C on system code 1.
elroTransmitter.sendSignal(1,'C',true);
// Wait 2 seconds
delay(2000);
// Switch on KaKu-device 2 of group 3 on address M (which is the same as device 10 on address M!)
kaKuTransmitter.sendSignal('M',3,2,true);
// Switch off Action-device B on system code 1.
actionTransmitter.sendSignal(1,'B',false);
// Switch off Blokker-device 7.
blokkerTransmitter.sendSignal(7,false);
// Switch off Elro-device C on system code 1.
elroTransmitter.sendSignal(1,'C',false);
// Wait 4 seconds
delay(4000);
}
+14 -14
View File
@@ -1,15 +1,15 @@
RemoteTransmitter KEYWORD1
ActionTransmitter KEYWORD1
BlokkerTransmitter KEYWORD1
KaKuTransmitter KEYWORD1
ElroTransmitter KEYWORD1
sendTelegram KEYWORD2
sendCode KEYWORD2
sendSignal KEYWORD2
isSameCode KEYWORD2
RemoteReceiver KEYWORD1
init KEYWORD2
enable KEYWORD2
disable KEYWORD2
deinit KEYWORD2
RemoteTransmitter KEYWORD1
ActionTransmitter KEYWORD1
BlokkerTransmitter KEYWORD1
KaKuTransmitter KEYWORD1
ElroTransmitter KEYWORD1
sendTelegram KEYWORD2
sendCode KEYWORD2
sendSignal KEYWORD2
isSameCode KEYWORD2
RemoteReceiver KEYWORD1
init KEYWORD2
enable KEYWORD2
disable KEYWORD2
deinit KEYWORD2
isReceiving KEYWORD2