library for implementing ir transmission

Revision:
4:55946953c6e3
Parent:
3:823708586b3e
Child:
5:ad52a062d494
--- a/XMIT_IR.cpp	Sat Oct 06 11:34:30 2012 +0000
+++ b/XMIT_IR.cpp	Sat Oct 06 12:52:28 2012 +0000
@@ -6,8 +6,8 @@
 {
     ir_serial = new Serial(TX,RX);
     ir_pwm = new PwmOut(PWM);
-    packet_buffer_out = new char(data_buff_len);
-    packet_buffer_in = new char(data_buff_len+2);
+    packet_buffer_out = new char[data_buff_len]();
+    packet_buffer_in = new char[data_buff_len+3]();
 
     //Initialize modules
     (*ir_pwm).period(1.0/38000.0);  //38kHz Modulation Freq
@@ -16,7 +16,8 @@
     (*ir_serial).attach(fptr,Serial::RxIrq);
 
     //Initialize Variables
-    for(int i=0; i<data_buff_len+2; i++) packet_buffer_in[i] = 0;
+    for(int i=0; i<data_buff_len+3; i++)
+        packet_buffer_in[i] = 0;
 }
 
 IRTransmitter::~IRTransmitter()
@@ -27,6 +28,11 @@
     delete packet_buffer_in;
 }
 
+void IRTransmitter::set_debug_port(Serial* port)
+{
+    debug_port = port;
+}
+
 
 void IRTransmitter::MakePacket(char* data,int len)
 {
@@ -63,20 +69,32 @@
     //          -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;*/
+        //check for valid packet - update data if so
+        update_packet_buffer(*data,len);
+
+        /*(*debug_port).printf("\n");
+        for(int i=0; i<len+3; i++) {
+            (*debug_port).printf("0x%02X.",packet_buffer_in[i]);
+        }
+        (*debug_port).printf("  PKT_VALID=0x%02X.",packet_is_valid(len));*/
+
+
+        if(packet_is_valid(len)==0) {
+            return_state = 1;
+            for(int i=0; i<len; i++) {
+                data[i] = packet_buffer_in[i+1];
+            }
+        }
 
         ir_data_mailbox.free(mail);
-        /*if(return_state)
-            return return_state;*/
+        if(return_state) {
+            return return_state;
+        }
 
         evt = ir_data_mailbox.get();
     }
@@ -84,7 +102,8 @@
 }
 
 char IRTransmitter::packet_is_valid(int data_len)
-{/*
+{
+
     char check = 0;
     //check for valid packet
     if(packet_buffer_in[0]!=ASCII_STX)
@@ -97,18 +116,19 @@
     }
     if(check!=packet_buffer_in[data_len+1]) {
         return 0x3; //bad checksum
-    }*/
+    }
 
     return 0;
 }
 
 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;*/
+    packet_buffer_in[data_len+2] = new_data;
 }