Skip to content

Add Lego Power Functions Receiver. #267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion IRremote.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
#define SEND_AIWA_RC_T501 1

#define DECODE_LG 1
#define SEND_LG 1
#define SEND_LG 1

#define DECODE_SANYO 1
#define SEND_SANYO 0 // NOT WRITTEN
Expand All @@ -76,6 +76,9 @@
#define DECODE_PRONTO 0 // This function doe not logically make sense
#define SEND_PRONTO 1

#define DECODE_LEGO_POWERFUNCTIONS 1
#define SEND_LEGO_POWERFUNCTIONS 0 // not yet implemented!

//------------------------------------------------------------------------------
// When sending a Pronto code we request to send either the "once" code
// or the "repeat" code
Expand Down Expand Up @@ -115,6 +118,7 @@ typedef
SHARP,
DENON,
PRONTO,
LEGO_POWERFUNCTIONS
}
decode_type_t;

Expand Down Expand Up @@ -243,6 +247,10 @@ class IRrecv
# if DECODE_DENON
bool decodeDenon (decode_results *results) ;
# endif
# if DECODE_LEGO_POWERFUNCTIONS
bool decodeLegoPowerFunctions (decode_results *results) ;
# endif

} ;

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -327,6 +335,11 @@ class IRsend
# if SEND_PRONTO
void sendPronto (char* code, bool repeat, bool fallback) ;
# endif

# if SEND_LEGO_POWERFUNCTIONS
void sendLegoPowerFunctions(char* code, bool repeat, bool fallback) ;
# endif

} ;

#endif
11 changes: 8 additions & 3 deletions irRecv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ int IRrecv::decode (decode_results *results)
if (decodeDenon(results)) return true ;
#endif

#if DECODE_LEGO_POWERFUNCTIONS
DBG_PRINTLN("Attempting Lego Power Functions decode");
if (decodeLegoPowerFunctions(results)) return true ;
#endif

// decodeHash returns a hash on any input.
// Thus, it needs to be last in the list.
// If you add any decodes, add them before this.
Expand All @@ -102,7 +107,7 @@ IRrecv::IRrecv (int recvpin, int blinkpin)
irparams.recvpin = recvpin;
irparams.blinkpin = blinkpin;
pinMode(blinkpin, OUTPUT);
irparams.blinkflag = 0;
irparams.blinkflag = 1;
}


Expand Down Expand Up @@ -145,8 +150,8 @@ void IRrecv::blink13 (int blinkflag)

//+=============================================================================
// Return if receiving new IR signals
//
bool IRrecv::isIdle ( )
//
bool IRrecv::isIdle ( )
{
return (irparams.rcvstate == STATE_IDLE || irparams.rcvstate == STATE_STOP) ? true : false;
}
Expand Down
112 changes: 112 additions & 0 deletions ir_PowerFunctions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Lego Power Functions receive.
* As per document
* http://cache.lego.com/Media/Download/PowerfunctionsElementSpecsDownloads/otherfiles/download9FC026117C091015E81EC28101DACD4E/8884RemoteControlIRReceiver_Download.pdf

* Receives the 16 bit protocol. It can be decoded with the Open Powerfunctions code
* https://bitbucket.org/tinkerer_/openpowerfunctionsrx

*/

#include "IRremote.h"
#include "IRremoteInt.h"

#define PF_STARTSTOP 1579
#define PF_LOWBIT 526
#define PF_HIBIT 947
#define PF_LOWER 315
#define BITS 16 // The number of bits in the command

//+=============================================================================
//
#if SEND_LEGO_POWERFUNCTIONS
void IRsend::sendLegoPowerFunctions (unsigned long data, int nbits)
{
// // Set IR carrier frequency
// enableIROut(38);
//
// // Header
// mark (HDR_MARK);
// space(HDR_SPACE);
//
// // Data
// for (unsigned long mask = 1UL << (nbits - 1); mask; mask >>= 1)
// {
// if (data & mask)
// {
// mark (BIT_MARK);
// space(ONE_SPACE);
// }
// else
// {
// mark (BIT_MARK);
// space(ZERO_SPACE);
// }
// }
//
// // Footer
// mark(BIT_MARK);
// space(0); // Always end with the LED off
}
#endif

//+=============================================================================
//



#if DECODE_LEGO_POWERFUNCTIONS
bool IRrecv::decodeLegoPowerFunctions(decode_results *results)
{
unsigned long data = 0; // Somewhere to build our code
DBG_PRINTLN(results->rawlen,DEC);
// Check we have the right amount of data
if (irparams.rawlen != (2 * BITS) + 4)
return false ;

DBG_PRINTLN("Attempting Lego Power Functions Decode");

uint16_t desired_us = (results->rawbuf[1] + results->rawbuf[2]) * USECPERTICK;
DBG_PRINT("PF desired_us = ");
DBG_PRINTLN(desired_us,DEC);

if(desired_us > PF_HIBIT && desired_us <= PF_STARTSTOP){
DBG_PRINTLN("Found PF Start Bit");
int offset = 3;
for(int i = 0; i < BITS; i++){
desired_us = (results->rawbuf[offset] + results->rawbuf[offset+1]) * USECPERTICK;

DBG_PRINT("PF desired_us = ");
DBG_PRINTLN(desired_us,DEC);
if(desired_us >= PF_LOWER && desired_us <= PF_LOWBIT){
DBG_PRINTLN("PF 0");
data = (data << 1) | 0 ;
}else if(desired_us > PF_LOWBIT && desired_us <= PF_HIBIT){
DBG_PRINTLN("PF 1");
data = (data << 1) | 1 ;
}else{
DBG_PRINTLN("PF Failed");
return false;
}
offset += 2;
}

desired_us = (results->rawbuf[offset]) * USECPERTICK;

DBG_PRINT("PF END desired_us = ");
DBG_PRINTLN(desired_us,DEC);
if(desired_us < PF_LOWER){
DBG_PRINTLN("Found PF End Bit");
DBG_PRINTLN(data, BIN);

// Success
results->bits = BITS;
results->value = data;
results->decode_type = LEGO_POWERFUNCTIONS;
return true;
}

}
return false;
}
#endif