library for implementing ir transmission

Committer:
cmulady
Date:
Sat Oct 06 14:40:38 2012 +0000
Revision:
5:ad52a062d494
Parent:
4:55946953c6e3
Child:
6:281167565d6d
middle of trying to add new feature - may not work

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 5:ad52a062d494 5 IRTransmitter::IRTransmitter(PinName TX, PinName PWM, PinName RX, int data_buff_len, void (*fptr_isr_rx)(void), void (*fptr_isr_tx)(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 4:55946953c6e3 9 packet_buffer_out = new char[data_buff_len]();
cmulady 4:55946953c6e3 10 packet_buffer_in = new char[data_buff_len+3]();
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 5:ad52a062d494 16 (*ir_serial).attach(fptr_isr_rx,Serial::RxIrq);
cmulady 5:ad52a062d494 17 (*ir_serial).attach(fptr_isr_tx,Serial::TxIrq);
cmulady 3:823708586b3e 18
cmulady 3:823708586b3e 19 //Initialize Variables
cmulady 4:55946953c6e3 20 for(int i=0; i<data_buff_len+3; i++)
cmulady 4:55946953c6e3 21 packet_buffer_in[i] = 0;
cmulady 1:ae1d2167a10c 22 }
cmulady 1:ae1d2167a10c 23
cmulady 1:ae1d2167a10c 24 IRTransmitter::~IRTransmitter()
cmulady 1:ae1d2167a10c 25 {
cmulady 1:ae1d2167a10c 26 delete ir_serial;
cmulady 1:ae1d2167a10c 27 delete ir_pwm;
cmulady 3:823708586b3e 28 delete packet_buffer_out;
cmulady 3:823708586b3e 29 delete packet_buffer_in;
cmulady 0:556f9be6047d 30 }
cmulady 0:556f9be6047d 31
cmulady 4:55946953c6e3 32 void IRTransmitter::set_debug_port(Serial* port)
cmulady 4:55946953c6e3 33 {
cmulady 4:55946953c6e3 34 debug_port = port;
cmulady 4:55946953c6e3 35 }
cmulady 4:55946953c6e3 36
cmulady 1:ae1d2167a10c 37
cmulady 1:ae1d2167a10c 38 void IRTransmitter::MakePacket(char* data,int len)
cmulady 1:ae1d2167a10c 39 {
cmulady 1:ae1d2167a10c 40 char check =0x0;
cmulady 1:ae1d2167a10c 41 (*ir_serial).putc(ASCII_STX);
cmulady 1:ae1d2167a10c 42 for(int i=0; i<len; i++) {
cmulady 1:ae1d2167a10c 43 check^=data[i];
cmulady 1:ae1d2167a10c 44 (*ir_serial).putc(data[i]);
cmulady 1:ae1d2167a10c 45 }
cmulady 1:ae1d2167a10c 46 (*ir_serial).putc(check);
cmulady 1:ae1d2167a10c 47 (*ir_serial).putc(ASCII_ETX);
cmulady 5:ad52a062d494 48
cmulady 5:ad52a062d494 49 /*char* mail = ir_txdata_mailbox.alloc();
cmulady 5:ad52a062d494 50 mail[0] = (char)data;
cmulady 5:ad52a062d494 51 ir_txdata_mailbox.put(mail);*/
cmulady 1:ae1d2167a10c 52 }
cmulady 1:ae1d2167a10c 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 5:ad52a062d494 66 osEvent evt = ir_rxdata_mailbox.get();
cmulady 3:823708586b3e 67 while(evt.status == osEventMail) {
cmulady 3:823708586b3e 68 char* mail = (char*)evt.value.p;
cmulady 3:823708586b3e 69 (*data) = (*mail);
cmulady 3:823708586b3e 70
cmulady 4:55946953c6e3 71 //check for valid packet - update data if so
cmulady 4:55946953c6e3 72 update_packet_buffer(*data,len);
cmulady 4:55946953c6e3 73
cmulady 4:55946953c6e3 74 /*(*debug_port).printf("\n");
cmulady 4:55946953c6e3 75 for(int i=0; i<len+3; i++) {
cmulady 4:55946953c6e3 76 (*debug_port).printf("0x%02X.",packet_buffer_in[i]);
cmulady 4:55946953c6e3 77 }
cmulady 4:55946953c6e3 78 (*debug_port).printf(" PKT_VALID=0x%02X.",packet_is_valid(len));*/
cmulady 4:55946953c6e3 79
cmulady 4:55946953c6e3 80
cmulady 4:55946953c6e3 81 if(packet_is_valid(len)==0) {
cmulady 4:55946953c6e3 82 return_state = 1;
cmulady 4:55946953c6e3 83 for(int i=0; i<len; i++) {
cmulady 4:55946953c6e3 84 data[i] = packet_buffer_in[i+1];
cmulady 4:55946953c6e3 85 }
cmulady 4:55946953c6e3 86 }
cmulady 3:823708586b3e 87
cmulady 5:ad52a062d494 88 ir_rxdata_mailbox.free(mail);
cmulady 4:55946953c6e3 89 if(return_state) {
cmulady 4:55946953c6e3 90 return return_state;
cmulady 4:55946953c6e3 91 }
cmulady 3:823708586b3e 92
cmulady 5:ad52a062d494 93 evt = ir_rxdata_mailbox.get();
cmulady 3:823708586b3e 94 }
cmulady 3:823708586b3e 95 return return_state;
cmulady 1:ae1d2167a10c 96 }
cmulady 1:ae1d2167a10c 97
cmulady 3:823708586b3e 98 char IRTransmitter::packet_is_valid(int data_len)
cmulady 4:55946953c6e3 99 {
cmulady 4:55946953c6e3 100
cmulady 3:823708586b3e 101 char check = 0;
cmulady 3:823708586b3e 102 //check for valid packet
cmulady 3:823708586b3e 103 if(packet_buffer_in[0]!=ASCII_STX)
cmulady 3:823708586b3e 104 return 0x1; //bad start byte
cmulady 3:823708586b3e 105 if(packet_buffer_in[data_len+2]!=ASCII_ETX)
cmulady 3:823708586b3e 106 return 0x2; //bad end byte
cmulady 1:ae1d2167a10c 107
cmulady 3:823708586b3e 108 for(int i=1; i<data_len+1; i++) {
cmulady 3:823708586b3e 109 check^=packet_buffer_in[i];
cmulady 3:823708586b3e 110 }
cmulady 3:823708586b3e 111 if(check!=packet_buffer_in[data_len+1]) {
cmulady 3:823708586b3e 112 return 0x3; //bad checksum
cmulady 4:55946953c6e3 113 }
cmulady 3:823708586b3e 114
cmulady 3:823708586b3e 115 return 0;
cmulady 3:823708586b3e 116 }
cmulady 1:ae1d2167a10c 117
cmulady 3:823708586b3e 118 void IRTransmitter::update_packet_buffer(char new_data, int data_len)
cmulady 4:55946953c6e3 119 {
cmulady 4:55946953c6e3 120
cmulady 3:823708586b3e 121 //Shift All data 1 cell over
cmulady 3:823708586b3e 122 for(int i=0; i<data_len+2; i++) {
cmulady 3:823708586b3e 123 packet_buffer_in[i] = packet_buffer_in[i+1];
cmulady 3:823708586b3e 124 }
cmulady 4:55946953c6e3 125 packet_buffer_in[data_len+2] = new_data;
cmulady 3:823708586b3e 126 }
cmulady 0:556f9be6047d 127
cmulady 0:556f9be6047d 128
cmulady 5:ad52a062d494 129 void IRTransmitter::isr_rx(char data)
cmulady 2:2eb0a3a29b5b 130 {
cmulady 5:ad52a062d494 131 char* mail = ir_rxdata_mailbox.alloc();
cmulady 2:2eb0a3a29b5b 132 mail[0] = (char)data;
cmulady 5:ad52a062d494 133 ir_rxdata_mailbox.put(mail);
cmulady 2:2eb0a3a29b5b 134 }
cmulady 0:556f9be6047d 135
cmulady 5:ad52a062d494 136 void IRTransmitter::isr_tx()
cmulady 5:ad52a062d494 137 {
cmulady 5:ad52a062d494 138 //char* mail = ir_rxdata_mailbox.alloc();
cmulady 5:ad52a062d494 139 //mail[0] = (char)data;
cmulady 5:ad52a062d494 140 //ir_rxdata_mailbox.put(mail);
cmulady 5:ad52a062d494 141 }
cmulady 2:2eb0a3a29b5b 142