library for implementing ir transmission
XMIT_IR.cpp@7:01243fe65940, 2012-10-06 (annotated)
- Committer:
- cmulady
- Date:
- Sat Oct 06 17:45:20 2012 +0000
- Revision:
- 7:01243fe65940
- Parent:
- 6:281167565d6d
good stopping point. ir tx works well enough
Who changed what in which revision?
User | Revision | Line number | New 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 | 7:01243fe65940 | 5 | IRTransmitter::IRTransmitter(PinName TX, PinName PWM, PinName RX, int data_buff_len, void (*fptr_isr_rx)(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 | 6:281167565d6d | 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 | 7:01243fe65940 | 40 | |
cmulady | 7:01243fe65940 | 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 | 7:01243fe65940 | 44 | (*ir_serial).putc(data[i]); |
cmulady | 1:ae1d2167a10c | 45 | } |
cmulady | 7:01243fe65940 | 46 | (*ir_serial).putc(check); |
cmulady | 7:01243fe65940 | 47 | (*ir_serial).putc(ASCII_ETX); |
cmulady | 6:281167565d6d | 48 | |
cmulady | 1:ae1d2167a10c | 49 | } |
cmulady | 1:ae1d2167a10c | 50 | |
cmulady | 3:823708586b3e | 51 | |
cmulady | 3:823708586b3e | 52 | |
cmulady | 3:823708586b3e | 53 | char IRTransmitter::ReadPacket(char* data, int len) |
cmulady | 3:823708586b3e | 54 | { |
cmulady | 3:823708586b3e | 55 | //Run this every few milliseconds to run through mailbox and check packet data. |
cmulady | 3:823708586b3e | 56 | //The data array will update with new values once a packet has been validated. |
cmulady | 3:823708586b3e | 57 | //Returns true if new data, false otherwise |
cmulady | 3:823708586b3e | 58 | //Inputs: |
cmulady | 3:823708586b3e | 59 | // -data: a character array to store data variables |
cmulady | 3:823708586b3e | 60 | // -len: length of character array |
cmulady | 3:823708586b3e | 61 | |
cmulady | 3:823708586b3e | 62 | char return_state=0; |
cmulady | 5:ad52a062d494 | 63 | osEvent evt = ir_rxdata_mailbox.get(); |
cmulady | 3:823708586b3e | 64 | while(evt.status == osEventMail) { |
cmulady | 3:823708586b3e | 65 | char* mail = (char*)evt.value.p; |
cmulady | 3:823708586b3e | 66 | (*data) = (*mail); |
cmulady | 3:823708586b3e | 67 | |
cmulady | 4:55946953c6e3 | 68 | //check for valid packet - update data if so |
cmulady | 4:55946953c6e3 | 69 | update_packet_buffer(*data,len); |
cmulady | 4:55946953c6e3 | 70 | |
cmulady | 4:55946953c6e3 | 71 | /*(*debug_port).printf("\n"); |
cmulady | 4:55946953c6e3 | 72 | for(int i=0; i<len+3; i++) { |
cmulady | 4:55946953c6e3 | 73 | (*debug_port).printf("0x%02X.",packet_buffer_in[i]); |
cmulady | 4:55946953c6e3 | 74 | } |
cmulady | 4:55946953c6e3 | 75 | (*debug_port).printf(" PKT_VALID=0x%02X.",packet_is_valid(len));*/ |
cmulady | 4:55946953c6e3 | 76 | |
cmulady | 6:281167565d6d | 77 | |
cmulady | 4:55946953c6e3 | 78 | |
cmulady | 4:55946953c6e3 | 79 | if(packet_is_valid(len)==0) { |
cmulady | 4:55946953c6e3 | 80 | return_state = 1; |
cmulady | 4:55946953c6e3 | 81 | for(int i=0; i<len; i++) { |
cmulady | 4:55946953c6e3 | 82 | data[i] = packet_buffer_in[i+1]; |
cmulady | 4:55946953c6e3 | 83 | } |
cmulady | 4:55946953c6e3 | 84 | } |
cmulady | 3:823708586b3e | 85 | |
cmulady | 5:ad52a062d494 | 86 | ir_rxdata_mailbox.free(mail); |
cmulady | 4:55946953c6e3 | 87 | if(return_state) { |
cmulady | 4:55946953c6e3 | 88 | return return_state; |
cmulady | 4:55946953c6e3 | 89 | } |
cmulady | 3:823708586b3e | 90 | |
cmulady | 5:ad52a062d494 | 91 | evt = ir_rxdata_mailbox.get(); |
cmulady | 3:823708586b3e | 92 | } |
cmulady | 3:823708586b3e | 93 | return return_state; |
cmulady | 1:ae1d2167a10c | 94 | } |
cmulady | 1:ae1d2167a10c | 95 | |
cmulady | 3:823708586b3e | 96 | char IRTransmitter::packet_is_valid(int data_len) |
cmulady | 4:55946953c6e3 | 97 | { |
cmulady | 4:55946953c6e3 | 98 | |
cmulady | 3:823708586b3e | 99 | char check = 0; |
cmulady | 3:823708586b3e | 100 | //check for valid packet |
cmulady | 3:823708586b3e | 101 | if(packet_buffer_in[0]!=ASCII_STX) |
cmulady | 3:823708586b3e | 102 | return 0x1; //bad start byte |
cmulady | 3:823708586b3e | 103 | if(packet_buffer_in[data_len+2]!=ASCII_ETX) |
cmulady | 3:823708586b3e | 104 | return 0x2; //bad end byte |
cmulady | 1:ae1d2167a10c | 105 | |
cmulady | 3:823708586b3e | 106 | for(int i=1; i<data_len+1; i++) { |
cmulady | 3:823708586b3e | 107 | check^=packet_buffer_in[i]; |
cmulady | 3:823708586b3e | 108 | } |
cmulady | 3:823708586b3e | 109 | if(check!=packet_buffer_in[data_len+1]) { |
cmulady | 3:823708586b3e | 110 | return 0x3; //bad checksum |
cmulady | 4:55946953c6e3 | 111 | } |
cmulady | 3:823708586b3e | 112 | |
cmulady | 3:823708586b3e | 113 | return 0; |
cmulady | 3:823708586b3e | 114 | } |
cmulady | 1:ae1d2167a10c | 115 | |
cmulady | 3:823708586b3e | 116 | void IRTransmitter::update_packet_buffer(char new_data, int data_len) |
cmulady | 4:55946953c6e3 | 117 | { |
cmulady | 4:55946953c6e3 | 118 | |
cmulady | 3:823708586b3e | 119 | //Shift All data 1 cell over |
cmulady | 3:823708586b3e | 120 | for(int i=0; i<data_len+2; i++) { |
cmulady | 3:823708586b3e | 121 | packet_buffer_in[i] = packet_buffer_in[i+1]; |
cmulady | 3:823708586b3e | 122 | } |
cmulady | 4:55946953c6e3 | 123 | packet_buffer_in[data_len+2] = new_data; |
cmulady | 3:823708586b3e | 124 | } |
cmulady | 0:556f9be6047d | 125 | |
cmulady | 0:556f9be6047d | 126 | |
cmulady | 5:ad52a062d494 | 127 | void IRTransmitter::isr_rx(char data) |
cmulady | 2:2eb0a3a29b5b | 128 | { |
cmulady | 5:ad52a062d494 | 129 | char* mail = ir_rxdata_mailbox.alloc(); |
cmulady | 2:2eb0a3a29b5b | 130 | mail[0] = (char)data; |
cmulady | 5:ad52a062d494 | 131 | ir_rxdata_mailbox.put(mail); |
cmulady | 2:2eb0a3a29b5b | 132 | } |
cmulady | 0:556f9be6047d | 133 | |
cmulady | 6:281167565d6d | 134 |