SSI OpComms 3 CM TX

Dependencies:   mbed

Fork of Optical3cmTXnucleo by Thomas Teisberg

main.cpp

Committer:
tdiamandis
Date:
2015-11-08
Revision:
1:a5e80a54e72f
Parent:
0:cb5d20585be4
Child:
2:eb6621f41f07

File content as of revision 1:a5e80a54e72f:

#include "mbed.h"

DigitalOut tx(D15);

Serial pc(USBTX, USBRX); // tx, rx
const int PULSE_LENGTH = 1000;
const int PPM = 4;
const int PACKET_LENGTH = 98;

//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]);
    }
    
    //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()
{
    pc.printf("3 CM Link Board - Transmit\r\n");
    
    //Packet
    char buffer[PACKET_LENGTH + 2];
    int idx = 0;
    while(1) {
        char a = pc.getc();
        
        //Fills buffer then transmits  
        if(a != '\n' && idx < PACKET_LENGTH){
            buffer[idx] = a;
            idx++;
        }else{
            //Adds ending characters
//            buffer[idx] = '\r';
//            idx++;
//            buffer[idx] = '\n';
//            idx++;
            
            //Transmits packet
            blink_packet(buffer, idx);
            idx = 0;
        }
    }
}