Read IR data from TV Remote
Revision 1:0fadd6080445, committed 2020-02-03
- Comitter:
- jdonnal
- Date:
- Mon Feb 03 16:14:43 2020 +0000
- Parent:
- 0:422eb3991ee3
- Commit message:
- Initial Commit
Changed in this revision
diff -r 422eb3991ee3 -r 0fadd6080445 Ir.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Ir.cpp Mon Feb 03 16:14:43 2020 +0000 @@ -0,0 +1,53 @@ +#include "Ir.h" + + +IrIn::IrIn(PinName input): ir_(input){ + ir_.rise(this, &IrIn::rising_edge_); + ir_.fall(this, &IrIn::falling_edge_); + ir_.disable_irq(); + rx_bits_ = 0; + msg_ = 0x0; + rx_in_progress_ = 0; + rx_complete_ = 0; + t_.start(); +} + +int IrIn::read(){ + ir_.enable_irq(); + while(!rx_complete_) + wait(0.1); + int rx_msg = msg_; + msg_ = 0x0; + rx_complete_ = 0; + rx_bits_ = 0; + return rx_msg; +} + +void IrIn::rising_edge_(){ + char rx_bit = 0x0; + int dt = t_.read_us(); + + //make sure we align to a start condition + if(!rx_in_progress_){ + if(dt>2000 && dt < 2500){ + rx_in_progress_ = 1; + } + return; + } + if(dt>700) + rx_bit = 0x1; + else + rx_bit = 0x0; + rx_bits_++; + msg_ = (msg_ << 1) | rx_bit; + if(rx_bits_ == 12){ + rx_in_progress_ = 0; + rx_complete_ = 1; + ir_.disable_irq(); + } + +} + +void IrIn::falling_edge_(){ + t_.reset(); +} \ No newline at end of file
diff -r 422eb3991ee3 -r 0fadd6080445 Ir.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Ir.h Mon Feb 03 16:14:43 2020 +0000 @@ -0,0 +1,44 @@ + +/* Parallax High Speed Continuous Servo + John Donnal 2018 +*/ + +#ifndef _IR_H +#define _IR_H + +/** + * Includes + */ +#include "mbed.h" + +/** + * 38KHz IR Receiver (for Sony RM-EZ4 Remote) + */ +class IrIn { + +public: + + /** + * Constructor. + * + * @param output mbed pin for input channel. + */ + IrIn(PinName input); + + /** + * Decode IR data, blocks until data is received + */ + int read(); +private: + + void falling_edge_(); + void rising_edge_(); + InterruptIn ir_; + Timer t_; + int rx_in_progress_; + int rx_complete_; + int rx_bits_; + int msg_; +}; + +#endif /* CONTINUOUS_SERVO_H */
diff -r 422eb3991ee3 -r 0fadd6080445 ir.cpp
diff -r 422eb3991ee3 -r 0fadd6080445 ir.h