SSI OpComms 3 CM TX

Dependencies:   mbed

Fork of Optical3cmTXnucleo by Thomas Teisberg

Committer:
tdiamandis
Date:
Tue Nov 10 06:39:32 2015 +0000
Revision:
6:8eae544417af
Parent:
5:4bf7b8f3fd58
Child:
7:4fafb1b60eab
Fixed place for possible integer overflow

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tdiamandis 4:024257aaea1e 1 #include "mbed.h"
tteisberg 0:cb5d20585be4 2
tteisberg 0:cb5d20585be4 3 DigitalOut tx(D15);
tteisberg 0:cb5d20585be4 4
tteisberg 0:cb5d20585be4 5 Serial pc(USBTX, USBRX); // tx, rx
tdiamandis 1:a5e80a54e72f 6 const int PPM = 4;
tdiamandis 1:a5e80a54e72f 7 const int PACKET_LENGTH = 98;
tdiamandis 6:8eae544417af 8 unsigned int PULSE_LENGTH = 0;
tteisberg 0:cb5d20585be4 9
tdiamandis 1:a5e80a54e72f 10 //Function Prototypes
tdiamandis 1:a5e80a54e72f 11 void blink_packet(char* buffer, int len);
tdiamandis 1:a5e80a54e72f 12 void blink_char(char c);
tdiamandis 1:a5e80a54e72f 13 void blink(int data);
tdiamandis 1:a5e80a54e72f 14
tdiamandis 1:a5e80a54e72f 15
tdiamandis 1:a5e80a54e72f 16 /*Function: blink_packet:
tdiamandis 1:a5e80a54e72f 17 Preconditions:
tdiamandis 1:a5e80a54e72f 18 len is index of last filled character in buffer
tdiamandis 1:a5e80a54e72f 19 Postconditions:
tdiamandis 1:a5e80a54e72f 20 transmits the packet buffer
tdiamandis 1:a5e80a54e72f 21 */
tdiamandis 1:a5e80a54e72f 22 void blink_packet(char* buffer, int len)
tdiamandis 1:a5e80a54e72f 23 {
tdiamandis 1:a5e80a54e72f 24 //Encodes and transmits each character
tdiamandis 1:a5e80a54e72f 25 for (int i=0; i < len; i++) {
tdiamandis 1:a5e80a54e72f 26 blink_char(buffer[i]);
tteisberg 0:cb5d20585be4 27 }
tdiamandis 1:a5e80a54e72f 28
tdiamandis 1:a5e80a54e72f 29 //Signifies end of packet
tdiamandis 1:a5e80a54e72f 30 tx = 1;
tdiamandis 5:4bf7b8f3fd58 31 wait_us(PULSE_LENGTH*(PPM+1));
tdiamandis 1:a5e80a54e72f 32 tx = 0;
tdiamandis 1:a5e80a54e72f 33 }
tdiamandis 1:a5e80a54e72f 34
tdiamandis 1:a5e80a54e72f 35
tdiamandis 1:a5e80a54e72f 36 /*Function: blink_char:
tdiamandis 1:a5e80a54e72f 37 Postconditions:
tdiamandis 1:a5e80a54e72f 38 transmits the char c via DPPM 4
tdiamandis 1:a5e80a54e72f 39 */
tdiamandis 1:a5e80a54e72f 40 void blink_char(char c) {
tdiamandis 1:a5e80a54e72f 41 printf("%d", c);
tdiamandis 1:a5e80a54e72f 42 printf("%c", '\r');
tdiamandis 1:a5e80a54e72f 43 printf("%c", '\n');
tdiamandis 1:a5e80a54e72f 44 for (int i=3; i>=0; i--) {
tdiamandis 1:a5e80a54e72f 45 blink((c & (3 << i*2)) >> i*2);
tdiamandis 1:a5e80a54e72f 46 }
tteisberg 0:cb5d20585be4 47 }
tteisberg 0:cb5d20585be4 48
tdiamandis 1:a5e80a54e72f 49
tdiamandis 1:a5e80a54e72f 50 /*Function: blink_packet:
tdiamandis 1:a5e80a54e72f 51 Preconditions:
tdiamandis 1:a5e80a54e72f 52 data < DPPM used
tdiamandis 1:a5e80a54e72f 53 Postconditions:
tdiamandis 1:a5e80a54e72f 54 pulses the light to transmit data
tdiamandis 1:a5e80a54e72f 55 */
tdiamandis 1:a5e80a54e72f 56 void blink(int data) {
tdiamandis 1:a5e80a54e72f 57 printf("%d", data);
tdiamandis 1:a5e80a54e72f 58 printf("%c", '\r');
tdiamandis 1:a5e80a54e72f 59 printf("%c", '\n');
tdiamandis 1:a5e80a54e72f 60 //Time on = PULSE_LENGTH
tdiamandis 1:a5e80a54e72f 61 tx = 1;
tdiamandis 1:a5e80a54e72f 62 wait_us(PULSE_LENGTH);
tdiamandis 1:a5e80a54e72f 63
tdiamandis 1:a5e80a54e72f 64 //Time off = PULSE_LENGTH*(data value)
tdiamandis 1:a5e80a54e72f 65 // For example, a 01 transmitted would have a difference between pulses of 2 PULSE_LENGTH
tdiamandis 1:a5e80a54e72f 66 tx = 0;
tdiamandis 2:eb6621f41f07 67 wait_us(PULSE_LENGTH*(data+1));
tdiamandis 1:a5e80a54e72f 68 tx = 1;
tdiamandis 1:a5e80a54e72f 69 }
tdiamandis 1:a5e80a54e72f 70
tteisberg 0:cb5d20585be4 71 int main()
tteisberg 0:cb5d20585be4 72 {
tdiamandis 1:a5e80a54e72f 73 pc.printf("3 CM Link Board - Transmit\r\n");
tdiamandis 4:024257aaea1e 74 pc.printf("Enter pulse length in microseconds (10e-6), enter for 1000\r\n");
tdiamandis 4:024257aaea1e 75 while(1) {
tdiamandis 4:024257aaea1e 76 char d = pc.getc();
tdiamandis 4:024257aaea1e 77 if(d != '\n') {
tdiamandis 4:024257aaea1e 78 PULSE_LENGTH = PULSE_LENGTH*10 + (d-'0');
tdiamandis 4:024257aaea1e 79 }
tdiamandis 4:024257aaea1e 80 else {
tdiamandis 4:024257aaea1e 81 if (PULSE_LENGTH == 0) PULSE_LENGTH = 1000;
tdiamandis 4:024257aaea1e 82 pc.printf("Pulse length is ");
tdiamandis 4:024257aaea1e 83 pc.printf("%d", PULSE_LENGTH);
tdiamandis 4:024257aaea1e 84 pc.printf("\r\n");
tdiamandis 4:024257aaea1e 85 break;
tdiamandis 4:024257aaea1e 86 }
tdiamandis 4:024257aaea1e 87 }
tteisberg 0:cb5d20585be4 88
tdiamandis 1:a5e80a54e72f 89 //Packet
tdiamandis 1:a5e80a54e72f 90 char buffer[PACKET_LENGTH + 2];
tteisberg 0:cb5d20585be4 91 int idx = 0;
tteisberg 0:cb5d20585be4 92 while(1) {
tteisberg 0:cb5d20585be4 93 char a = pc.getc();
tdiamandis 1:a5e80a54e72f 94
tdiamandis 1:a5e80a54e72f 95 //Fills buffer then transmits
tdiamandis 4:024257aaea1e 96 if (a == '~') {
tdiamandis 4:024257aaea1e 97 tx = tx ^ 1;
tdiamandis 4:024257aaea1e 98 while (pc.getc() != '~');
tdiamandis 4:024257aaea1e 99 }
tdiamandis 4:024257aaea1e 100 else if(a != '\n' && idx < PACKET_LENGTH){
tteisberg 0:cb5d20585be4 101 buffer[idx] = a;
tteisberg 0:cb5d20585be4 102 idx++;
tdiamandis 4:024257aaea1e 103 }
tdiamandis 4:024257aaea1e 104 else {
tdiamandis 1:a5e80a54e72f 105 //Adds ending characters
tdiamandis 4:024257aaea1e 106 buffer[idx] = '\r';
tdiamandis 4:024257aaea1e 107 idx++;
tdiamandis 4:024257aaea1e 108 buffer[idx] = '\n';
tdiamandis 4:024257aaea1e 109 idx++;
tdiamandis 1:a5e80a54e72f 110
tdiamandis 1:a5e80a54e72f 111 //Transmits packet
tdiamandis 1:a5e80a54e72f 112 blink_packet(buffer, idx);
tteisberg 0:cb5d20585be4 113 idx = 0;
tteisberg 0:cb5d20585be4 114 }
tteisberg 0:cb5d20585be4 115 }
tteisberg 0:cb5d20585be4 116 }