library for implementing ir transmission

Revision:
3:823708586b3e
Parent:
2:2eb0a3a29b5b
Child:
4:55946953c6e3
--- a/XMIT_IR.cpp	Sat Oct 06 02:45:52 2012 +0000
+++ b/XMIT_IR.cpp	Sat Oct 06 11:34:30 2012 +0000
@@ -6,22 +6,25 @@
 {
     ir_serial = new Serial(TX,RX);
     ir_pwm = new PwmOut(PWM);
-    data_buffer_out = new char(data_buff_len);
-    data_buffer_in = new char(data_buff_len+2);
+    packet_buffer_out = new char(data_buff_len);
+    packet_buffer_in = new char(data_buff_len+2);
 
     //Initialize modules
     (*ir_pwm).period(1.0/38000.0);  //38kHz Modulation Freq
     (*ir_pwm) = 0.5;                //pulse width = 50%
     (*ir_serial).baud(2400);
     (*ir_serial).attach(fptr,Serial::RxIrq);
+
+    //Initialize Variables
+    for(int i=0; i<data_buff_len+2; i++) packet_buffer_in[i] = 0;
 }
 
 IRTransmitter::~IRTransmitter()
 {
     delete ir_serial;
     delete ir_pwm;
-    delete data_buffer_out;
-    delete data_buffer_in;
+    delete packet_buffer_out;
+    delete packet_buffer_in;
 }
 
 
@@ -37,7 +40,7 @@
     (*ir_serial).putc(ASCII_ETX);
 }
 
-char IRTransmitter::ReadPacket(char* data)
+/*char IRTransmitter::ReadPacket(char* data)
 {
     osEvent evt = ir_data_mailbox.get();
     if(evt.status == osEventMail) {
@@ -47,23 +50,67 @@
         return 0;
     }
     return 1;
+}*/
+
+
+char IRTransmitter::ReadPacket(char* data, int len)
+{
+    //Run this every few milliseconds to run through mailbox and check packet data.
+    //The data array will update with new values once a packet has been validated.
+    //Returns true if new data, false otherwise
+    //Inputs:
+    //          -data: a character array to store data variables
+    //          -len:  length of character array
+
+    char return_state=0;
+
+    osEvent evt = ir_data_mailbox.get();
+    while(evt.status == osEventMail) {
+        char* mail = (char*)evt.value.p;
+        (*data) = (*mail);
+
+        //check for valid packet
+        /*update_packet_buffer(*data,len);
+        if(packet_is_valid(len))
+            return_state = 1;*/
+
+        ir_data_mailbox.free(mail);
+        /*if(return_state)
+            return return_state;*/
+
+        evt = ir_data_mailbox.get();
+    }
+    return return_state;
 }
 
-//Handle Reception of RX data (mail it to appropriate thread)
-void IRTransmitter::isr_uartrx(void)
-{
-    //get RX data (and prevent ISR from looping forever
-    uint32_t RBR = LPC_UART1->RBR; //This only works for p13/p14!
+char IRTransmitter::packet_is_valid(int data_len)
+{/*
+    char check = 0;
+    //check for valid packet
+    if(packet_buffer_in[0]!=ASCII_STX)
+        return 0x1; //bad start byte
+    if(packet_buffer_in[data_len+2]!=ASCII_ETX)
+        return 0x2; //bad end byte
 
-    //write letter and put in mailbox
-    char* mail = ir_data_mailbox.alloc();
-    mail[0] = (char)RBR;
-    ir_data_mailbox.put(mail);
+    for(int i=1; i<data_len+1; i++) {
+        check^=packet_buffer_in[i];
+    }
+    if(check!=packet_buffer_in[data_len+1]) {
+        return 0x3; //bad checksum
+    }*/
+
+    return 0;
+}
 
-    //Let message handler run
-    //(*threadptr_irstuff).signal_set(0x1);
+void IRTransmitter::update_packet_buffer(char new_data, int data_len)
+{/*
+    //Shift All data 1 cell over
+    for(int i=0; i<data_len+2; i++) {
+        packet_buffer_in[i] = packet_buffer_in[i+1];
+    }
+    packet_buffer_in[data_len+2] = new_data;*/
+}
 
-}
 
 void IRTransmitter::isr(char data)
 {