library for implementing ir transmission

XMIT_IR.cpp

Committer:
cmulady
Date:
2012-10-06
Revision:
2:2eb0a3a29b5b
Parent:
1:ae1d2167a10c
Child:
3:823708586b3e

File content as of revision 2:2eb0a3a29b5b:

#include "mbed.h"
#include "XMIT_IR.h"


IRTransmitter::IRTransmitter(PinName TX, PinName PWM, PinName RX, int data_buff_len, void (*fptr)(void))
{
    ir_serial = new Serial(TX,RX);
    ir_pwm = new PwmOut(PWM);
    data_buffer_out = new char(data_buff_len);
    data_buffer_in = new char(data_buff_len+2);

    //Initialize modules
    (*ir_pwm).period(1.0/38000.0);  //38kHz Modulation Freq
    (*ir_pwm) = 0.5;                //pulse width = 50%
    (*ir_serial).baud(2400);
    (*ir_serial).attach(fptr,Serial::RxIrq);
}

IRTransmitter::~IRTransmitter()
{
    delete ir_serial;
    delete ir_pwm;
    delete data_buffer_out;
    delete data_buffer_in;
}


void IRTransmitter::MakePacket(char* data,int len)
{
    char check =0x0;
    (*ir_serial).putc(ASCII_STX);
    for(int i=0; i<len; i++) {
        check^=data[i];
        (*ir_serial).putc(data[i]);
    }
    (*ir_serial).putc(check);
    (*ir_serial).putc(ASCII_ETX);
}

char IRTransmitter::ReadPacket(char* data)
{
    osEvent evt = ir_data_mailbox.get();
    if(evt.status == osEventMail) {
        char* mail = (char*)evt.value.p;
        (*data) = (*mail);
        ir_data_mailbox.free(mail);
        return 0;
    }
    return 1;
}

//Handle Reception of RX data (mail it to appropriate thread)
void IRTransmitter::isr_uartrx(void)
{
    //get RX data (and prevent ISR from looping forever
    uint32_t RBR = LPC_UART1->RBR; //This only works for p13/p14!

    //write letter and put in mailbox
    char* mail = ir_data_mailbox.alloc();
    mail[0] = (char)RBR;
    ir_data_mailbox.put(mail);

    //Let message handler run
    //(*threadptr_irstuff).signal_set(0x1);

}

void IRTransmitter::isr(char data)
{
    char* mail = ir_data_mailbox.alloc();
    mail[0] = (char)data;
    ir_data_mailbox.put(mail);
}