library for implementing ir transmission

Committer:
cmulady
Date:
Sat Oct 06 17:36:37 2012 +0000
Revision:
6:281167565d6d
Parent:
5:ad52a062d494
Child:
7:01243fe65940
going to remove tx interrupt handling

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 6:281167565d6d 19 // uint_32 tmp = LPC_UART1->IER;
cmulady 6:281167565d6d 20
cmulady 6:281167565d6d 21 // LPC_UART1->IER = tmp;
cmulady 6:281167565d6d 22
cmulady 3:823708586b3e 23 //Initialize Variables
cmulady 4:55946953c6e3 24 for(int i=0; i<data_buff_len+3; i++)
cmulady 4:55946953c6e3 25 packet_buffer_in[i] = 0;
cmulady 1:ae1d2167a10c 26 }
cmulady 1:ae1d2167a10c 27
cmulady 1:ae1d2167a10c 28 IRTransmitter::~IRTransmitter()
cmulady 1:ae1d2167a10c 29 {
cmulady 1:ae1d2167a10c 30 delete ir_serial;
cmulady 1:ae1d2167a10c 31 delete ir_pwm;
cmulady 3:823708586b3e 32 delete packet_buffer_out;
cmulady 3:823708586b3e 33 delete packet_buffer_in;
cmulady 0:556f9be6047d 34 }
cmulady 0:556f9be6047d 35
cmulady 4:55946953c6e3 36 void IRTransmitter::set_debug_port(Serial* port)
cmulady 4:55946953c6e3 37 {
cmulady 4:55946953c6e3 38 debug_port = port;
cmulady 4:55946953c6e3 39 }
cmulady 4:55946953c6e3 40
cmulady 1:ae1d2167a10c 41
cmulady 1:ae1d2167a10c 42 void IRTransmitter::MakePacket(char* data,int len)
cmulady 1:ae1d2167a10c 43 {
cmulady 1:ae1d2167a10c 44 char check =0x0;
cmulady 6:281167565d6d 45 Timer t;
cmulady 6:281167565d6d 46 t.reset();
cmulady 6:281167565d6d 47 t.start();
cmulady 6:281167565d6d 48
cmulady 6:281167565d6d 49 //(*ir_serial).putc(ASCII_STX);
cmulady 6:281167565d6d 50 mail_tx_packet(ASCII_STX);
cmulady 1:ae1d2167a10c 51 for(int i=0; i<len; i++) {
cmulady 1:ae1d2167a10c 52 check^=data[i];
cmulady 6:281167565d6d 53 //(*ir_serial).putc(data[i]);
cmulady 6:281167565d6d 54 mail_tx_packet(data[i]);
cmulady 1:ae1d2167a10c 55 }
cmulady 6:281167565d6d 56 //(*ir_serial).putc(check);
cmulady 6:281167565d6d 57 mail_tx_packet(check);
cmulady 6:281167565d6d 58 //(*ir_serial).putc(ASCII_ETX);
cmulady 6:281167565d6d 59 mail_tx_packet(ASCII_ETX);
cmulady 6:281167565d6d 60
cmulady 6:281167565d6d 61 t.stop();
cmulady 6:281167565d6d 62 int time = t.read_ms();
cmulady 6:281167565d6d 63 (*debug_port).printf("\nms=%d\n",time);
cmulady 6:281167565d6d 64
cmulady 6:281167565d6d 65 char tmp = isr_tx();
cmulady 6:281167565d6d 66 LPC_UART1->THR = (uint32_t)data;
cmulady 6:281167565d6d 67
cmulady 6:281167565d6d 68
cmulady 6:281167565d6d 69
cmulady 6:281167565d6d 70 }
cmulady 6:281167565d6d 71
cmulady 6:281167565d6d 72 void IRTransmitter::mail_tx_packet(char data)
cmulady 6:281167565d6d 73 {
cmulady 6:281167565d6d 74 char* mail = ir_txdata_mailbox.alloc();
cmulady 6:281167565d6d 75 mail[0] = data;
cmulady 6:281167565d6d 76 ir_txdata_mailbox.put(mail);
cmulady 1:ae1d2167a10c 77 }
cmulady 1:ae1d2167a10c 78
cmulady 3:823708586b3e 79
cmulady 3:823708586b3e 80
cmulady 3:823708586b3e 81 char IRTransmitter::ReadPacket(char* data, int len)
cmulady 3:823708586b3e 82 {
cmulady 3:823708586b3e 83 //Run this every few milliseconds to run through mailbox and check packet data.
cmulady 3:823708586b3e 84 //The data array will update with new values once a packet has been validated.
cmulady 3:823708586b3e 85 //Returns true if new data, false otherwise
cmulady 3:823708586b3e 86 //Inputs:
cmulady 3:823708586b3e 87 // -data: a character array to store data variables
cmulady 3:823708586b3e 88 // -len: length of character array
cmulady 3:823708586b3e 89
cmulady 3:823708586b3e 90 char return_state=0;
cmulady 5:ad52a062d494 91 osEvent evt = ir_rxdata_mailbox.get();
cmulady 3:823708586b3e 92 while(evt.status == osEventMail) {
cmulady 3:823708586b3e 93 char* mail = (char*)evt.value.p;
cmulady 3:823708586b3e 94 (*data) = (*mail);
cmulady 3:823708586b3e 95
cmulady 4:55946953c6e3 96 //check for valid packet - update data if so
cmulady 4:55946953c6e3 97 update_packet_buffer(*data,len);
cmulady 4:55946953c6e3 98
cmulady 4:55946953c6e3 99 /*(*debug_port).printf("\n");
cmulady 4:55946953c6e3 100 for(int i=0; i<len+3; i++) {
cmulady 4:55946953c6e3 101 (*debug_port).printf("0x%02X.",packet_buffer_in[i]);
cmulady 4:55946953c6e3 102 }
cmulady 4:55946953c6e3 103 (*debug_port).printf(" PKT_VALID=0x%02X.",packet_is_valid(len));*/
cmulady 4:55946953c6e3 104
cmulady 6:281167565d6d 105 if((*data)=='?') {
cmulady 6:281167565d6d 106 (*debug_port).putc('*');
cmulady 6:281167565d6d 107 }
cmulady 6:281167565d6d 108
cmulady 6:281167565d6d 109
cmulady 4:55946953c6e3 110
cmulady 4:55946953c6e3 111 if(packet_is_valid(len)==0) {
cmulady 4:55946953c6e3 112 return_state = 1;
cmulady 4:55946953c6e3 113 for(int i=0; i<len; i++) {
cmulady 4:55946953c6e3 114 data[i] = packet_buffer_in[i+1];
cmulady 4:55946953c6e3 115 }
cmulady 4:55946953c6e3 116 }
cmulady 3:823708586b3e 117
cmulady 5:ad52a062d494 118 ir_rxdata_mailbox.free(mail);
cmulady 4:55946953c6e3 119 if(return_state) {
cmulady 4:55946953c6e3 120 return return_state;
cmulady 4:55946953c6e3 121 }
cmulady 3:823708586b3e 122
cmulady 5:ad52a062d494 123 evt = ir_rxdata_mailbox.get();
cmulady 3:823708586b3e 124 }
cmulady 3:823708586b3e 125 return return_state;
cmulady 1:ae1d2167a10c 126 }
cmulady 1:ae1d2167a10c 127
cmulady 3:823708586b3e 128 char IRTransmitter::packet_is_valid(int data_len)
cmulady 4:55946953c6e3 129 {
cmulady 4:55946953c6e3 130
cmulady 3:823708586b3e 131 char check = 0;
cmulady 3:823708586b3e 132 //check for valid packet
cmulady 3:823708586b3e 133 if(packet_buffer_in[0]!=ASCII_STX)
cmulady 3:823708586b3e 134 return 0x1; //bad start byte
cmulady 3:823708586b3e 135 if(packet_buffer_in[data_len+2]!=ASCII_ETX)
cmulady 3:823708586b3e 136 return 0x2; //bad end byte
cmulady 1:ae1d2167a10c 137
cmulady 3:823708586b3e 138 for(int i=1; i<data_len+1; i++) {
cmulady 3:823708586b3e 139 check^=packet_buffer_in[i];
cmulady 3:823708586b3e 140 }
cmulady 3:823708586b3e 141 if(check!=packet_buffer_in[data_len+1]) {
cmulady 3:823708586b3e 142 return 0x3; //bad checksum
cmulady 4:55946953c6e3 143 }
cmulady 3:823708586b3e 144
cmulady 3:823708586b3e 145 return 0;
cmulady 3:823708586b3e 146 }
cmulady 1:ae1d2167a10c 147
cmulady 3:823708586b3e 148 void IRTransmitter::update_packet_buffer(char new_data, int data_len)
cmulady 4:55946953c6e3 149 {
cmulady 4:55946953c6e3 150
cmulady 3:823708586b3e 151 //Shift All data 1 cell over
cmulady 3:823708586b3e 152 for(int i=0; i<data_len+2; i++) {
cmulady 3:823708586b3e 153 packet_buffer_in[i] = packet_buffer_in[i+1];
cmulady 3:823708586b3e 154 }
cmulady 4:55946953c6e3 155 packet_buffer_in[data_len+2] = new_data;
cmulady 3:823708586b3e 156 }
cmulady 0:556f9be6047d 157
cmulady 0:556f9be6047d 158
cmulady 5:ad52a062d494 159 void IRTransmitter::isr_rx(char data)
cmulady 2:2eb0a3a29b5b 160 {
cmulady 5:ad52a062d494 161 char* mail = ir_rxdata_mailbox.alloc();
cmulady 2:2eb0a3a29b5b 162 mail[0] = (char)data;
cmulady 5:ad52a062d494 163 ir_rxdata_mailbox.put(mail);
cmulady 2:2eb0a3a29b5b 164 }
cmulady 0:556f9be6047d 165
cmulady 6:281167565d6d 166 char IRTransmitter::isr_tx(void)
cmulady 5:ad52a062d494 167 {
cmulady 5:ad52a062d494 168 //char* mail = ir_rxdata_mailbox.alloc();
cmulady 5:ad52a062d494 169 //mail[0] = (char)data;
cmulady 5:ad52a062d494 170 //ir_rxdata_mailbox.put(mail);
cmulady 6:281167565d6d 171
cmulady 6:281167565d6d 172 char data = 0xF1;
cmulady 6:281167565d6d 173 osEvent evt = ir_txdata_mailbox.get();
cmulady 6:281167565d6d 174 if(evt.status == osEventMail) {
cmulady 6:281167565d6d 175 char* mail = (char*)evt.value.p;
cmulady 6:281167565d6d 176 data = (*mail);
cmulady 6:281167565d6d 177
cmulady 6:281167565d6d 178 //(*ir_serial).putc(data);
cmulady 6:281167565d6d 179 ir_rxdata_mailbox.free(mail);
cmulady 6:281167565d6d 180 }
cmulady 6:281167565d6d 181 //(*debug_port).printf("TX=0x%02X.",data);
cmulady 6:281167565d6d 182 return data;
cmulady 5:ad52a062d494 183 }
cmulady 2:2eb0a3a29b5b 184