library for implementing ir transmission

Committer:
cmulady
Date:
Sat Oct 06 11:34:30 2012 +0000
Revision:
3:823708586b3e
Parent:
2:2eb0a3a29b5b
Child:
4:55946953c6e3
stack memory error somewhere

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 3:823708586b3e 9 packet_buffer_out = new char(data_buff_len);
cmulady 3:823708586b3e 10 packet_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 3:823708586b3e 17
cmulady 3:823708586b3e 18 //Initialize Variables
cmulady 3:823708586b3e 19 for(int i=0; i<data_buff_len+2; i++) packet_buffer_in[i] = 0;
cmulady 1:ae1d2167a10c 20 }
cmulady 1:ae1d2167a10c 21
cmulady 1:ae1d2167a10c 22 IRTransmitter::~IRTransmitter()
cmulady 1:ae1d2167a10c 23 {
cmulady 1:ae1d2167a10c 24 delete ir_serial;
cmulady 1:ae1d2167a10c 25 delete ir_pwm;
cmulady 3:823708586b3e 26 delete packet_buffer_out;
cmulady 3:823708586b3e 27 delete packet_buffer_in;
cmulady 0:556f9be6047d 28 }
cmulady 0:556f9be6047d 29
cmulady 1:ae1d2167a10c 30
cmulady 1:ae1d2167a10c 31 void IRTransmitter::MakePacket(char* data,int len)
cmulady 1:ae1d2167a10c 32 {
cmulady 1:ae1d2167a10c 33 char check =0x0;
cmulady 1:ae1d2167a10c 34 (*ir_serial).putc(ASCII_STX);
cmulady 1:ae1d2167a10c 35 for(int i=0; i<len; i++) {
cmulady 1:ae1d2167a10c 36 check^=data[i];
cmulady 1:ae1d2167a10c 37 (*ir_serial).putc(data[i]);
cmulady 1:ae1d2167a10c 38 }
cmulady 1:ae1d2167a10c 39 (*ir_serial).putc(check);
cmulady 1:ae1d2167a10c 40 (*ir_serial).putc(ASCII_ETX);
cmulady 1:ae1d2167a10c 41 }
cmulady 1:ae1d2167a10c 42
cmulady 3:823708586b3e 43 /*char IRTransmitter::ReadPacket(char* data)
cmulady 0:556f9be6047d 44 {
cmulady 1:ae1d2167a10c 45 osEvent evt = ir_data_mailbox.get();
cmulady 1:ae1d2167a10c 46 if(evt.status == osEventMail) {
cmulady 1:ae1d2167a10c 47 char* mail = (char*)evt.value.p;
cmulady 1:ae1d2167a10c 48 (*data) = (*mail);
cmulady 1:ae1d2167a10c 49 ir_data_mailbox.free(mail);
cmulady 1:ae1d2167a10c 50 return 0;
cmulady 1:ae1d2167a10c 51 }
cmulady 1:ae1d2167a10c 52 return 1;
cmulady 3:823708586b3e 53 }*/
cmulady 3:823708586b3e 54
cmulady 3:823708586b3e 55
cmulady 3:823708586b3e 56 char IRTransmitter::ReadPacket(char* data, int len)
cmulady 3:823708586b3e 57 {
cmulady 3:823708586b3e 58 //Run this every few milliseconds to run through mailbox and check packet data.
cmulady 3:823708586b3e 59 //The data array will update with new values once a packet has been validated.
cmulady 3:823708586b3e 60 //Returns true if new data, false otherwise
cmulady 3:823708586b3e 61 //Inputs:
cmulady 3:823708586b3e 62 // -data: a character array to store data variables
cmulady 3:823708586b3e 63 // -len: length of character array
cmulady 3:823708586b3e 64
cmulady 3:823708586b3e 65 char return_state=0;
cmulady 3:823708586b3e 66
cmulady 3:823708586b3e 67 osEvent evt = ir_data_mailbox.get();
cmulady 3:823708586b3e 68 while(evt.status == osEventMail) {
cmulady 3:823708586b3e 69 char* mail = (char*)evt.value.p;
cmulady 3:823708586b3e 70 (*data) = (*mail);
cmulady 3:823708586b3e 71
cmulady 3:823708586b3e 72 //check for valid packet
cmulady 3:823708586b3e 73 /*update_packet_buffer(*data,len);
cmulady 3:823708586b3e 74 if(packet_is_valid(len))
cmulady 3:823708586b3e 75 return_state = 1;*/
cmulady 3:823708586b3e 76
cmulady 3:823708586b3e 77 ir_data_mailbox.free(mail);
cmulady 3:823708586b3e 78 /*if(return_state)
cmulady 3:823708586b3e 79 return return_state;*/
cmulady 3:823708586b3e 80
cmulady 3:823708586b3e 81 evt = ir_data_mailbox.get();
cmulady 3:823708586b3e 82 }
cmulady 3:823708586b3e 83 return return_state;
cmulady 1:ae1d2167a10c 84 }
cmulady 1:ae1d2167a10c 85
cmulady 3:823708586b3e 86 char IRTransmitter::packet_is_valid(int data_len)
cmulady 3:823708586b3e 87 {/*
cmulady 3:823708586b3e 88 char check = 0;
cmulady 3:823708586b3e 89 //check for valid packet
cmulady 3:823708586b3e 90 if(packet_buffer_in[0]!=ASCII_STX)
cmulady 3:823708586b3e 91 return 0x1; //bad start byte
cmulady 3:823708586b3e 92 if(packet_buffer_in[data_len+2]!=ASCII_ETX)
cmulady 3:823708586b3e 93 return 0x2; //bad end byte
cmulady 1:ae1d2167a10c 94
cmulady 3:823708586b3e 95 for(int i=1; i<data_len+1; i++) {
cmulady 3:823708586b3e 96 check^=packet_buffer_in[i];
cmulady 3:823708586b3e 97 }
cmulady 3:823708586b3e 98 if(check!=packet_buffer_in[data_len+1]) {
cmulady 3:823708586b3e 99 return 0x3; //bad checksum
cmulady 3:823708586b3e 100 }*/
cmulady 3:823708586b3e 101
cmulady 3:823708586b3e 102 return 0;
cmulady 3:823708586b3e 103 }
cmulady 1:ae1d2167a10c 104
cmulady 3:823708586b3e 105 void IRTransmitter::update_packet_buffer(char new_data, int data_len)
cmulady 3:823708586b3e 106 {/*
cmulady 3:823708586b3e 107 //Shift All data 1 cell over
cmulady 3:823708586b3e 108 for(int i=0; i<data_len+2; i++) {
cmulady 3:823708586b3e 109 packet_buffer_in[i] = packet_buffer_in[i+1];
cmulady 3:823708586b3e 110 }
cmulady 3:823708586b3e 111 packet_buffer_in[data_len+2] = new_data;*/
cmulady 3:823708586b3e 112 }
cmulady 0:556f9be6047d 113
cmulady 0:556f9be6047d 114
cmulady 2:2eb0a3a29b5b 115 void IRTransmitter::isr(char data)
cmulady 2:2eb0a3a29b5b 116 {
cmulady 2:2eb0a3a29b5b 117 char* mail = ir_data_mailbox.alloc();
cmulady 2:2eb0a3a29b5b 118 mail[0] = (char)data;
cmulady 2:2eb0a3a29b5b 119 ir_data_mailbox.put(mail);
cmulady 2:2eb0a3a29b5b 120 }
cmulady 0:556f9be6047d 121
cmulady 2:2eb0a3a29b5b 122