library for implementing ir transmission

Committer:
cmulady
Date:
Sat Oct 06 02:45:52 2012 +0000
Revision:
2:2eb0a3a29b5b
Parent:
1:ae1d2167a10c
Child:
3:823708586b3e
bad luck, ol mum.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cmulady 0:556f9be6047d 1 #include "mbed.h"
cmulady 0:556f9be6047d 2 #include "XMIT_IR.h"
cmulady 0:556f9be6047d 3
cmulady 0:556f9be6047d 4
cmulady 1:ae1d2167a10c 5 IRTransmitter::IRTransmitter(PinName TX, PinName PWM, PinName RX, int data_buff_len, void (*fptr)(void))
cmulady 0:556f9be6047d 6 {
cmulady 1:ae1d2167a10c 7 ir_serial = new Serial(TX,RX);
cmulady 1:ae1d2167a10c 8 ir_pwm = new PwmOut(PWM);
cmulady 1:ae1d2167a10c 9 data_buffer_out = new char(data_buff_len);
cmulady 1:ae1d2167a10c 10 data_buffer_in = new char(data_buff_len+2);
cmulady 0:556f9be6047d 11
cmulady 1:ae1d2167a10c 12 //Initialize modules
cmulady 1:ae1d2167a10c 13 (*ir_pwm).period(1.0/38000.0); //38kHz Modulation Freq
cmulady 1:ae1d2167a10c 14 (*ir_pwm) = 0.5; //pulse width = 50%
cmulady 1:ae1d2167a10c 15 (*ir_serial).baud(2400);
cmulady 1:ae1d2167a10c 16 (*ir_serial).attach(fptr,Serial::RxIrq);
cmulady 1:ae1d2167a10c 17 }
cmulady 1:ae1d2167a10c 18
cmulady 1:ae1d2167a10c 19 IRTransmitter::~IRTransmitter()
cmulady 1:ae1d2167a10c 20 {
cmulady 1:ae1d2167a10c 21 delete ir_serial;
cmulady 1:ae1d2167a10c 22 delete ir_pwm;
cmulady 1:ae1d2167a10c 23 delete data_buffer_out;
cmulady 1:ae1d2167a10c 24 delete data_buffer_in;
cmulady 0:556f9be6047d 25 }
cmulady 0:556f9be6047d 26
cmulady 1:ae1d2167a10c 27
cmulady 1:ae1d2167a10c 28 void IRTransmitter::MakePacket(char* data,int len)
cmulady 1:ae1d2167a10c 29 {
cmulady 1:ae1d2167a10c 30 char check =0x0;
cmulady 1:ae1d2167a10c 31 (*ir_serial).putc(ASCII_STX);
cmulady 1:ae1d2167a10c 32 for(int i=0; i<len; i++) {
cmulady 1:ae1d2167a10c 33 check^=data[i];
cmulady 1:ae1d2167a10c 34 (*ir_serial).putc(data[i]);
cmulady 1:ae1d2167a10c 35 }
cmulady 1:ae1d2167a10c 36 (*ir_serial).putc(check);
cmulady 1:ae1d2167a10c 37 (*ir_serial).putc(ASCII_ETX);
cmulady 1:ae1d2167a10c 38 }
cmulady 1:ae1d2167a10c 39
cmulady 1:ae1d2167a10c 40 char IRTransmitter::ReadPacket(char* data)
cmulady 0:556f9be6047d 41 {
cmulady 1:ae1d2167a10c 42 osEvent evt = ir_data_mailbox.get();
cmulady 1:ae1d2167a10c 43 if(evt.status == osEventMail) {
cmulady 1:ae1d2167a10c 44 char* mail = (char*)evt.value.p;
cmulady 1:ae1d2167a10c 45 (*data) = (*mail);
cmulady 1:ae1d2167a10c 46 ir_data_mailbox.free(mail);
cmulady 1:ae1d2167a10c 47 return 0;
cmulady 1:ae1d2167a10c 48 }
cmulady 1:ae1d2167a10c 49 return 1;
cmulady 1:ae1d2167a10c 50 }
cmulady 1:ae1d2167a10c 51
cmulady 1:ae1d2167a10c 52 //Handle Reception of RX data (mail it to appropriate thread)
cmulady 1:ae1d2167a10c 53 void IRTransmitter::isr_uartrx(void)
cmulady 1:ae1d2167a10c 54 {
cmulady 1:ae1d2167a10c 55 //get RX data (and prevent ISR from looping forever
cmulady 1:ae1d2167a10c 56 uint32_t RBR = LPC_UART1->RBR; //This only works for p13/p14!
cmulady 1:ae1d2167a10c 57
cmulady 1:ae1d2167a10c 58 //write letter and put in mailbox
cmulady 1:ae1d2167a10c 59 char* mail = ir_data_mailbox.alloc();
cmulady 1:ae1d2167a10c 60 mail[0] = (char)RBR;
cmulady 1:ae1d2167a10c 61 ir_data_mailbox.put(mail);
cmulady 1:ae1d2167a10c 62
cmulady 1:ae1d2167a10c 63 //Let message handler run
cmulady 1:ae1d2167a10c 64 //(*threadptr_irstuff).signal_set(0x1);
cmulady 0:556f9be6047d 65
cmulady 0:556f9be6047d 66 }
cmulady 0:556f9be6047d 67
cmulady 2:2eb0a3a29b5b 68 void IRTransmitter::isr(char data)
cmulady 2:2eb0a3a29b5b 69 {
cmulady 2:2eb0a3a29b5b 70 char* mail = ir_data_mailbox.alloc();
cmulady 2:2eb0a3a29b5b 71 mail[0] = (char)data;
cmulady 2:2eb0a3a29b5b 72 ir_data_mailbox.put(mail);
cmulady 2:2eb0a3a29b5b 73 }
cmulady 0:556f9be6047d 74
cmulady 2:2eb0a3a29b5b 75