Thomas Cauwelier / Mbed 2 deprecated visible_light_communication

Dependencies:   SoftSerial SDFileSystem mbed wave_player

Committer:
thoma@THOMAS-CAUWELIER.khbo.be
Date:
Tue May 02 19:20:21 2017 +0200
Revision:
3:f128424d2e80
Child:
10:071b7cc8b0ff
add manchester

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 1 #include <iostream>
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 2 #include "Manchester.h"
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 3
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 4
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 5 void ::Manchester::encode_manchester(char *in, int size, char *out) {
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 6 int m_char_index = 0; // index for expanded manchester char
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 7
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 8 // iterate every char
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 9 for (int char_index = 0; char_index < size; char_index++) {
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 10 bool bits[16]; // manchester char
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 11 int m_bit_index = 0;
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 12
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 13 //split in bits and insert inverse
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 14 for (int i = 0; i < 8; i++) { // iterate over all the bits in the char
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 15 bool bit = (bool) ((in[char_index] >> 7 - i) & 1);
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 16 bits[m_bit_index] = bit;
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 17 m_bit_index++;
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 18 bits[m_bit_index] = !bit;
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 19 m_bit_index++;
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 20 }
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 21
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 22 //combine back to 2 chars
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 23 int bit_index = 0;
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 24 for (int i = 0; i < 2; ++i) {
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 25 out[m_char_index] = 0;
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 26 for (int j = 0; j < 8; j++) { //assemble first char
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 27 out[m_char_index] |= (bits[bit_index] << (7 - j));
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 28 bit_index++;
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 29 }
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 30 m_char_index++;
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 31 }
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 32 }
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 33 }
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 34
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 35 bool ::Manchester::decode_manchester(char *in, int size, char *out) {
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 36 for (int m_char_index = 0; m_char_index < size; ++m_char_index) {
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 37 bool bits[8];
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 38 bool bits_inverted[8];
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 39 int bit_index = 0;
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 40
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 41 // split bits and put in bit arrays
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 42 for (int i = 0; i < 2; ++i) {
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 43 for (int m_bit_index = 0; m_bit_index < 8; ++m_bit_index) {
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 44 bool bit = (bool) ((in[m_char_index + i] >> 7 - m_bit_index) & 1);
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 45 bits[bit_index] = bit;
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 46 m_bit_index++;
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 47 bool bit_inv = (bool) ((in[m_char_index + i] >> 7 - m_bit_index) & 1);
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 48 bits_inverted[bit_index] = bit_inv;
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 49 bit_index++;
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 50
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 51 // test if correct manchester encoded
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 52 if (bit == bit_inv) {
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 53 return false; //bit flip in transport
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 54 }
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 55 }
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 56 }
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 57 // assemble char
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 58 int char_index = m_char_index >> 1;
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 59 out[char_index] = 0;
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 60 for (int i = 0; i < 8; ++i) {
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 61 out[char_index] |= (bits[i] << (7 - i));
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 62 }
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 63 m_char_index++;
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 64 }
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 65 return true;
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 66 }
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 67
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 68 void ::Manchester::print_char_bits(char *buffer, int size) {
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 69 for (int i = 0; i < size; i++) {
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 70 for (int j = 0; j < 8; j++) {
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 71 int bit = ((buffer[i] >> 7 - j) & 1);
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 72 printf("%i", bit);
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 73 }
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 74 printf(" ");
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 75 }
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 76 }
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 77
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 78 void ::Manchester::debug() {
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 79 int size = 3;
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 80 char s[size];
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 81 s[0] = '\0';
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 82 s[1] = 15;
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 83 s[2] = 'c';
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 84 print_char_bits(s, size);
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 85 printf("\n");
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 86 char m[size * 2];
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 87 encode_manchester(s, size, m);
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 88 print_char_bits(m, size * 2);
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 89 printf("\n");
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 90 decode_manchester(m, size * 2, s);
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 91 printf("\n");
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 92 print_char_bits(s, size);
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 93 }
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 94
thoma@THOMAS-CAUWELIER.khbo.be 3:f128424d2e80 95