From 93cbc7b50c4489b9907504c0a5dc3c47d2c444a7 Mon Sep 17 00:00:00 2001 From: randy Date: Sun, 24 Mar 2013 22:18:07 +0100 Subject: [PATCH] * Uses NewRemodeCode::on, ::off, ::dim, ::on_with_dim instead of 0, 1, 2 and 3, for better readability. This change is backwards compatible. * Updated examples to use the new NewRemodeCode::on, ::off, ::dim and ::on_with_dim notation. --- .../ReceiveRemoteAndSensor.ino | 14 +++++--- NewRemoteSwitch/NewRemoteReceiver.cpp | 33 +++++++++++++------ NewRemoteSwitch/NewRemoteReceiver.h | 9 ++++- NewRemoteSwitch/README.TXT | 14 +++++--- .../examples/LearnCode/LearnCode.ino | 13 ++++---- .../examples/Retransmitter/Retransmitter.ino | 20 +++++------ .../ShowReceivedCode/ShowReceivedCode.ino | 12 ++++--- README.TXT | 2 +- 8 files changed, 75 insertions(+), 42 deletions(-) diff --git a/InterruptChain/examples/ReceiveRemoteAndSensor/ReceiveRemoteAndSensor.ino b/InterruptChain/examples/ReceiveRemoteAndSensor/ReceiveRemoteAndSensor.ino index d18a32e..e849e9d 100644 --- a/InterruptChain/examples/ReceiveRemoteAndSensor/ReceiveRemoteAndSensor.ino +++ b/InterruptChain/examples/ReceiveRemoteAndSensor/ReceiveRemoteAndSensor.ino @@ -67,14 +67,18 @@ void showNewCode(NewRemoteCode receivedCode) { } switch (receivedCode.switchType) { - case 0: + case NewRemoteCode::off: Serial.print(" off"); break; - case 1: + case NewRemoteCode::on: Serial.print(" on"); break; - case 2: - Serial.print(" dim level"); + case NewRemoteCode::dim: + Serial.print(" dim level "); + Serial.print(receivedCode.dimLevel); + break; + case NewRemoteCode::on_with_dim: + Serial.print(" on with dim level "); Serial.print(receivedCode.dimLevel); break; } @@ -102,4 +106,4 @@ void showTempHumi(byte *data) { Serial.print('.'); Serial.println(temp % 10); // decimal } -} +} diff --git a/NewRemoteSwitch/NewRemoteReceiver.cpp b/NewRemoteSwitch/NewRemoteReceiver.cpp index c73751b..fbed398 100644 --- a/NewRemoteSwitch/NewRemoteReceiver.cpp +++ b/NewRemoteSwitch/NewRemoteReceiver.cpp @@ -20,7 +20,7 @@ but with slightly different timings, as measured on my device.) _ _ dim: | |_| |_ (T,T,T,T) -T = short period of ~260µs. However, this code tries +T = short period of ~260µs. However, this code tries to figure out the correct period A full frame looks like this: @@ -111,8 +111,8 @@ void NewRemoteReceiver::interruptHandler() { if (_state == -1) { // wait for the long low part of a stop bit. // Stopbit: 1T high, 40T low - // By default 1T is 260us, but for maximum compatiblity go as low as 120us - if (duration > 4800) { // =40*120us, minimal time between two edges before decoding starts. + // By default 1T is 260µs, but for maximum compatibility go as low as 120µs + if (duration > 4800) { // =40*120µs, minimal time between two edges before decoding starts. // Sync signal received.. Preparing for decoding repeats = 0; @@ -156,7 +156,20 @@ void NewRemoteReceiver::interruptHandler() { // if first part op stopbit was a short signal (short signal yielded a 0 as second bit in receivedBit), and ... ((receivedBit & B10) == B00) && // we are in a state in which a stopbit is actually valid, only then ... - (_state == 147 || _state == 131) ) { + (_state == 147 || _state == 131) ) { + // If a dim-level was present... + if (_state == 147) { + // ... test if it was an "on" signal ... + if (receivedCode.switchType == NewRemoteCode::on) { + // ... set the appropriate switch type + receivedCode.switchType = NewRemoteCode::on_with_dim; + } else { + // ... otherwise it was wrong (e.g. off-signal with dim) + _state = -1; + return; + } + } + // a valid signal was found! if ( receivedCode.address != previousCode.address || @@ -233,13 +246,13 @@ void NewRemoteReceiver::interruptHandler() { // States 110 - 113 are switch bit states. switch (receivedBit & B1111) { case B0001: // Bit "0" received. - receivedCode.switchType = 0; + receivedCode.switchType = NewRemoteCode::off; break; - case B0100: // Bit "1" received. - receivedCode.switchType = 1; + case B0100: // Bit "1" received. Note: this might turn out to be a on_with_dim signal. + receivedCode.switchType = NewRemoteCode::on; break; - case B0000: // Bit "dim" receivd. - receivedCode.switchType = 2; + case B0000: // Bit "dim" received. + receivedCode.switchType = NewRemoteCode::dim; break; default: // Bit was invalid. Abort. _state = -1; @@ -275,7 +288,7 @@ void NewRemoteReceiver::interruptHandler() { case B0001: // Bit "0" received. // receivedCode.dimLevel |= 0; But let's not do that, as it is wasteful. break; - case B0100: // Bit "1" receivd. + case B0100: // Bit "1" received. receivedCode.dimLevel |= 1; break; default: // Bit was invalid. Abort. diff --git a/NewRemoteSwitch/NewRemoteReceiver.h b/NewRemoteSwitch/NewRemoteReceiver.h index c174153..ad5fba9 100644 --- a/NewRemoteSwitch/NewRemoteReceiver.h +++ b/NewRemoteSwitch/NewRemoteReceiver.h @@ -10,10 +10,17 @@ #include struct NewRemoteCode { + enum SwitchType : unsigned short { + off = 0, + on = 1, + dim = 2, + on_with_dim = 3 + }; + unsigned int period; // Detected duration in microseconds of 1T in the received signal unsigned long address; // Address of received code. [0..2^26-1] boolean groupBit; // Group bit set or not - unsigned short switchType; // 0: swich off; 1: switch on; 2: set dim level + SwitchType switchType; // off, on, dim, on_with_dim. unsigned short unit; // Unit code of received code [0..15] unsigned short dimLevel; // Dim level [0..15] iff switchType == 2 }; diff --git a/NewRemoteSwitch/README.TXT b/NewRemoteSwitch/README.TXT index 8690369..ea4d001 100644 --- a/NewRemoteSwitch/README.TXT +++ b/NewRemoteSwitch/README.TXT @@ -7,8 +7,8 @@ used by some common "new 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 the RemoteSwitch library instead. - - "new style", which use a button on the receivers to "learn" a signal. Use this - library. + - "new style", which use a button on the receivers to "learn" a signal. Use + this library. License: GPLv3. See ./NewRemoteSwitch/license.txt @@ -33,10 +33,14 @@ Default installation demo: Changelog: NewRemoteSwitch library v1.1.0 (BETA) for Arduino 1.0 - - BUGFIX: in many occassions, when receiving a dim-level, the code was rejected + - BUGFIX: in many occasions, when receiving a dim-level, the code was rejected even if the signal was correct. - - Support decoding A-series transmitters which transmit a dim-level in combination - with an on-signal, instead of a dim-signal. + - Support decoding A-series transmitters which transmit a dim-level in + combination with an on-signal, instead of a dim-signal. + - Uses NewRemodeCode::on, ::off, ::dim, ::on_with_dim instead of 0, 1, 2 and 3, + for better readability. This change is backwards compatible. + - Updated examples to use the new NewRemodeCode::on, ::off, ::dim and + ::on_with_dim notation. NewRemoteSwitch library v1.0.0 (20121229) for Arduino 1.0 - Support for receiving A-series Klik-aan-klik-uit remote. (NewRemoteReceiver) diff --git a/NewRemoteSwitch/examples/LearnCode/LearnCode.ino b/NewRemoteSwitch/examples/LearnCode/LearnCode.ino index 436403b..3d19ae2 100644 --- a/NewRemoteSwitch/examples/LearnCode/LearnCode.ino +++ b/NewRemoteSwitch/examples/LearnCode/LearnCode.ino @@ -51,13 +51,14 @@ void processCode(NewRemoteCode receivedCode) { // Yes! // Is the received code identical to the learned code? if (receivedCode.address == learnedAddress && receivedCode.unit == learnedUnit) { - // Yes! - // switchType == 1 means on, switchType == 0 means off. - if (receivedCode.switchType == 1) { - digitalWrite(13, HIGH); - } else if (receivedCode.switchType == 0) { + // Yes! + // Switch the LED off if the received code was "off". + // Anything else (on, dim, on_with_dim) will switch the LED on. + if (receivedCode.switchType == NewRemoteCode::off) { digitalWrite(13, LOW); + } else { + digitalWrite(13, HIGH); } } } -} +} diff --git a/NewRemoteSwitch/examples/Retransmitter/Retransmitter.ino b/NewRemoteSwitch/examples/Retransmitter/Retransmitter.ino index 6bab021..87f3a52 100644 --- a/NewRemoteSwitch/examples/Retransmitter/Retransmitter.ino +++ b/NewRemoteSwitch/examples/Retransmitter/Retransmitter.ino @@ -4,7 +4,7 @@ * * Connect the transmitter to digital pin 11, and the receiver to digital pin 2. * - * When run, this sketch waits for a valid code from a new-style the receiver, + * When run, this sketch waits for a valid code from a new-style the receiver, * decodes it, and retransmits it after 5 seconds. */ @@ -19,7 +19,7 @@ void setup() { void loop() { } -void retransmitter(NewRemoteCode receivedCode) { +void retransmitter(NewRemoteCode receivedCode) { // Disable the receiver; otherwise it might pick up the retransmit as well. NewRemoteReceiver::disable(); @@ -33,24 +33,24 @@ void retransmitter(NewRemoteCode receivedCode) { NewRemoteTransmitter transmitter(receivedCode.address, 11, receivedCode.period); - // Switch type 0 = switch off, type 1 = switch on, type 2 = set dim level. - if (receivedCode.switchType == 2) { + if (receivedCode.switchType == NewReceiverCode::dim) { // Dimmer signal received - transmitter.sendDim(receivedCode.unit, receivedCode.dimLevel); - } + transmitter.sendDim(receivedCode.unit, receivedCode.dimLevel); + } else { // On/Off signal received + bool isOn == receivedCode.switchType == NewReceiverCode::on || receivedCode.switchType == NewReceiverCode::on_with_dim if (receivedCode.groupBit) { // Send to the group - transmitter.sendGroup(receivedCode.switchType); - } + transmitter.sendGroup(isOn); + } else { // Send to a single unit - transmitter.sendUnit(receivedCode.unit, receivedCode.switchType); + transmitter.sendUnit(receivedCode.unit, isOn); } } NewRemoteReceiver::enable(); } - + diff --git a/NewRemoteSwitch/examples/ShowReceivedCode/ShowReceivedCode.ino b/NewRemoteSwitch/examples/ShowReceivedCode/ShowReceivedCode.ino index 09c7628..14d8228 100644 --- a/NewRemoteSwitch/examples/ShowReceivedCode/ShowReceivedCode.ino +++ b/NewRemoteSwitch/examples/ShowReceivedCode/ShowReceivedCode.ino @@ -41,14 +41,18 @@ void showCode(NewRemoteCode receivedCode) { } switch (receivedCode.switchType) { - case 0: + case NewRemoteCode::off: Serial.print(" off"); break; - case 1: + case NewRemoteCode::on: Serial.print(" on"); break; - case 2: - Serial.print(" dim level"); + case NewRemoteCode::dim: + Serial.print(" dim level "); + Serial.print(receivedCode.dimLevel); + break; + case NewRemoteCode::on_with_dim: + Serial.print(" on with dim level "); Serial.print(receivedCode.dimLevel); break; } diff --git a/README.TXT b/README.TXT index 467cb23..72f9e33 100644 --- a/README.TXT +++ b/README.TXT @@ -25,7 +25,7 @@ QUICKSTART: ReceiveRemoteAndSensor. * Connect a 433MHz receiver to digital pin 2 on the Arduino. * Run the sketch. - * Enabled the serial monitor at 1152 baud. + * Enabled the serial monitor at 115200 baud. * Press buttons on your remote(s), and/or wait for your thermo/hygro sensor to transmit. * You should see the received data in the serial monitor. \ No newline at end of file