library for implementing ir transmission

Committer:
cmulady
Date:
Sat Oct 06 12:52:28 2012 +0000
Revision:
4:55946953c6e3
Parent:
3:823708586b3e
Child:
5:ad52a062d494
kinda sorta working

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