library for implementing ir transmission
XMIT_IR.cpp@1:ae1d2167a10c, 2012-10-06 (annotated)
- Committer:
- cmulady
- Date:
- Sat Oct 06 01:44:32 2012 +0000
- Revision:
- 1:ae1d2167a10c
- Parent:
- 0:556f9be6047d
- Child:
- 2:2eb0a3a29b5b
doesnt work!
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 | 0:556f9be6047d | 5 | |
cmulady | 1:ae1d2167a10c | 6 | IRTransmitter::IRTransmitter(PinName TX, PinName PWM, PinName RX, int data_buff_len, void (*fptr)(void)) |
cmulady | 0:556f9be6047d | 7 | { |
cmulady | 1:ae1d2167a10c | 8 | ir_serial = new Serial(TX,RX); |
cmulady | 1:ae1d2167a10c | 9 | ir_pwm = new PwmOut(PWM); |
cmulady | 1:ae1d2167a10c | 10 | data_buffer_out = new char(data_buff_len); |
cmulady | 1:ae1d2167a10c | 11 | data_buffer_in = new char(data_buff_len+2); |
cmulady | 0:556f9be6047d | 12 | |
cmulady | 1:ae1d2167a10c | 13 | //Initialize modules |
cmulady | 1:ae1d2167a10c | 14 | (*ir_pwm).period(1.0/38000.0); //38kHz Modulation Freq |
cmulady | 1:ae1d2167a10c | 15 | (*ir_pwm) = 0.5; //pulse width = 50% |
cmulady | 1:ae1d2167a10c | 16 | (*ir_serial).baud(2400); |
cmulady | 1:ae1d2167a10c | 17 | (*ir_serial).attach(fptr,Serial::RxIrq); |
cmulady | 1:ae1d2167a10c | 18 | } |
cmulady | 1:ae1d2167a10c | 19 | |
cmulady | 1:ae1d2167a10c | 20 | IRTransmitter::~IRTransmitter() |
cmulady | 1:ae1d2167a10c | 21 | { |
cmulady | 1:ae1d2167a10c | 22 | delete ir_serial; |
cmulady | 1:ae1d2167a10c | 23 | delete ir_pwm; |
cmulady | 1:ae1d2167a10c | 24 | delete data_buffer_out; |
cmulady | 1:ae1d2167a10c | 25 | delete data_buffer_in; |
cmulady | 0:556f9be6047d | 26 | } |
cmulady | 0:556f9be6047d | 27 | |
cmulady | 1:ae1d2167a10c | 28 | |
cmulady | 1:ae1d2167a10c | 29 | void IRTransmitter::MakePacket(char* data,int len) |
cmulady | 1:ae1d2167a10c | 30 | { |
cmulady | 1:ae1d2167a10c | 31 | char check =0x0; |
cmulady | 1:ae1d2167a10c | 32 | (*ir_serial).putc(ASCII_STX); |
cmulady | 1:ae1d2167a10c | 33 | for(int i=0; i<len; i++) { |
cmulady | 1:ae1d2167a10c | 34 | check^=data[i]; |
cmulady | 1:ae1d2167a10c | 35 | (*ir_serial).putc(data[i]); |
cmulady | 1:ae1d2167a10c | 36 | } |
cmulady | 1:ae1d2167a10c | 37 | (*ir_serial).putc(check); |
cmulady | 1:ae1d2167a10c | 38 | (*ir_serial).putc(ASCII_ETX); |
cmulady | 1:ae1d2167a10c | 39 | } |
cmulady | 1:ae1d2167a10c | 40 | |
cmulady | 1:ae1d2167a10c | 41 | char IRTransmitter::ReadPacket(char* data) |
cmulady | 0:556f9be6047d | 42 | { |
cmulady | 1:ae1d2167a10c | 43 | osEvent evt = ir_data_mailbox.get(); |
cmulady | 1:ae1d2167a10c | 44 | if(evt.status == osEventMail) { |
cmulady | 1:ae1d2167a10c | 45 | char* mail = (char*)evt.value.p; |
cmulady | 1:ae1d2167a10c | 46 | (*data) = (*mail); |
cmulady | 1:ae1d2167a10c | 47 | ir_data_mailbox.free(mail); |
cmulady | 1:ae1d2167a10c | 48 | return 0; |
cmulady | 1:ae1d2167a10c | 49 | } |
cmulady | 1:ae1d2167a10c | 50 | return 1; |
cmulady | 1:ae1d2167a10c | 51 | } |
cmulady | 1:ae1d2167a10c | 52 | |
cmulady | 1:ae1d2167a10c | 53 | //Handle Reception of RX data (mail it to appropriate thread) |
cmulady | 1:ae1d2167a10c | 54 | void IRTransmitter::isr_uartrx(void) |
cmulady | 1:ae1d2167a10c | 55 | { |
cmulady | 1:ae1d2167a10c | 56 | //get RX data (and prevent ISR from looping forever |
cmulady | 1:ae1d2167a10c | 57 | uint32_t RBR = LPC_UART1->RBR; //This only works for p13/p14! |
cmulady | 1:ae1d2167a10c | 58 | |
cmulady | 1:ae1d2167a10c | 59 | //write letter and put in mailbox |
cmulady | 1:ae1d2167a10c | 60 | char* mail = ir_data_mailbox.alloc(); |
cmulady | 1:ae1d2167a10c | 61 | mail[0] = (char)RBR; |
cmulady | 1:ae1d2167a10c | 62 | ir_data_mailbox.put(mail); |
cmulady | 1:ae1d2167a10c | 63 | |
cmulady | 1:ae1d2167a10c | 64 | //Let message handler run |
cmulady | 1:ae1d2167a10c | 65 | //(*threadptr_irstuff).signal_set(0x1); |
cmulady | 0:556f9be6047d | 66 | |
cmulady | 0:556f9be6047d | 67 | } |
cmulady | 0:556f9be6047d | 68 | |
cmulady | 0:556f9be6047d | 69 |