SSI OpComms 3 CM TX

Dependencies:   mbed

Fork of Optical3cmTXnucleo by Thomas Teisberg

Revision:
1:a5e80a54e72f
Parent:
0:cb5d20585be4
Child:
2:eb6621f41f07
--- a/main.cpp	Fri Oct 30 05:32:28 2015 +0000
+++ b/main.cpp	Sun Nov 08 00:26:32 2015 +0000
@@ -3,47 +3,95 @@
 DigitalOut tx(D15);
 
 Serial pc(USBTX, USBRX); // tx, rx
+const int PULSE_LENGTH = 1000;
+const int PPM = 4;
+const int PACKET_LENGTH = 98;
 
-void blink_binary(char c)
-{
-    for (int i = 7; i >= 0; --i) {
-        tx = 0;
-        wait_ms(10);
-        tx = 1;
-        wait_ms((c & (1 << i)) ? 20 : 10);
-        tx = 0;
+//Function Prototypes
+void blink_packet(char* buffer, int len);
+void blink_char(char c);
+void blink(int data);
+
+
+/*Function: blink_packet:
+    Preconditions: 
+        len is index of last filled character in buffer
+    Postconditions:
+        transmits the packet buffer
+*/
+void blink_packet(char* buffer, int len)
+{   
+    //Encodes and transmits each character
+    for (int i=0; i < len; i++) {
+        blink_char(buffer[i]);
     }
-    wait_ms(30);
+    
+    //Signifies end of packet
+    tx = 1;
+    wait_us(PULSE_LENGTH*2*(PPM+1));
+    tx = 0;
+}
+
+
+/*Function: blink_char:
+    Postconditions:
+        transmits the char c via DPPM 4
+*/
+void blink_char(char c) {
+    printf("%d", c);
+    printf("%c", '\r');
+    printf("%c", '\n');
+    for (int i=3; i>=0; i--) {
+        blink((c & (3 << i*2)) >> i*2);
+    } 
 }
 
+
+/*Function: blink_packet:
+    Preconditions: 
+        data < DPPM used
+    Postconditions:
+        pulses the light to transmit data
+*/
+void blink(int data) {
+    printf("%d", data);
+    printf("%c", '\r');
+    printf("%c", '\n');
+    //Time on = PULSE_LENGTH
+    tx = 1;
+    wait_us(PULSE_LENGTH);
+    
+    //Time off = PULSE_LENGTH*(data value)
+    //  For example, a 01 transmitted would have a difference between pulses of 2 PULSE_LENGTH
+    tx = 0;
+    wait_us(PULSE_LENGTH*data);
+    tx = 1;
+} 
+
 int main()
 {
-    char* teststr = "Hello World\r\n";
-    int testlen = strlen(teststr);
+    pc.printf("3 CM Link Board - Transmit\r\n");
     
-    pc.printf("3 CM Link Board - Transmit\r\n");
-    char buffer[100];
+    //Packet
+    char buffer[PACKET_LENGTH + 2];
     int idx = 0;
     while(1) {
         char a = pc.getc();
-        if(a != '\n' && idx < 100){
+        
+        //Fills buffer then transmits  
+        if(a != '\n' && idx < PACKET_LENGTH){
             buffer[idx] = a;
             idx++;
         }else{
-            for(int i=0;i<idx;i++){
-                blink_binary(buffer[i]);
-            }
-            blink_binary('\r');
-            blink_binary('\n');
+            //Adds ending characters
+//            buffer[idx] = '\r';
+//            idx++;
+//            buffer[idx] = '\n';
+//            idx++;
+            
+            //Transmits packet
+            blink_packet(buffer, idx);
             idx = 0;
         }
-        
-        
-        
-        /*
-        for(int i=0;i<testlen;i++){
-            blink_binary(teststr[i]);
-        }
-        */
     }
 }
\ No newline at end of file