John Donnal / IrReceiver
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Ir.cpp Source File

Ir.cpp

00001 #include "Ir.h"
00002  
00003  
00004 IrIn::IrIn(PinName input): ir_(input){
00005     ir_.rise(this, &IrIn::rising_edge_);
00006     ir_.fall(this, &IrIn::falling_edge_);
00007     ir_.disable_irq();
00008     rx_bits_ = 0;
00009     msg_ = 0x0;
00010     rx_in_progress_ = 0;
00011     rx_complete_ = 0;
00012     t_.start();
00013 }
00014 
00015 int IrIn::read(){
00016     ir_.enable_irq();
00017     while(!rx_complete_)
00018         wait(0.1);
00019     int rx_msg = msg_;
00020     msg_ = 0x0;
00021     rx_complete_ = 0;
00022     rx_bits_ = 0;
00023     return rx_msg;
00024 }
00025 
00026 void IrIn::rising_edge_(){
00027     char rx_bit = 0x0;
00028     int dt = t_.read_us();
00029     
00030     //make sure we align to a start condition
00031     if(!rx_in_progress_){
00032         if(dt>2000 && dt < 2500){
00033             rx_in_progress_ = 1;
00034         }
00035         return;
00036     }
00037     if(dt>700)
00038         rx_bit = 0x1;
00039     else
00040         rx_bit = 0x0;
00041     rx_bits_++;
00042     msg_ = (msg_ << 1) | rx_bit;
00043     if(rx_bits_ == 12){
00044         rx_in_progress_ = 0;
00045         rx_complete_ = 1;
00046         ir_.disable_irq();
00047     }
00048     
00049 }
00050 
00051 void IrIn::falling_edge_(){
00052     t_.reset();
00053 }