Thomas Cauwelier / Mbed 2 deprecated visible_light_communication

Dependencies:   SoftSerial SDFileSystem mbed wave_player

Committer:
Desertification
Date:
Wed May 10 07:10:32 2017 +0000
Revision:
14:e29cca7e64b2
Parent:
13:c111ea1c5990
Child:
15:75f9bd5d7659
16bit softserial test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Desertification 1:6b839581dc9f 1
Desertification 1:6b839581dc9f 2 #include "mbed.h"
Thomas Cauwelier 6:0e1973fdfe28 3 #include "rtos.h"
Thomas Cauwelier 9:d95984ce5de9 4 #include "Manchester.h"
Thomas Cauwelier 12:a070de571ea1 5 #include "SoftSerial.h"
Thomas Cauwelier 6:0e1973fdfe28 6
Thomas Cauwelier 12:a070de571ea1 7 //Serial out(p9, p10, 3200); //p9
Thomas Cauwelier 12:a070de571ea1 8 //Serial in(p13, p14, 3200); //p14
Thomas Cauwelier 12:a070de571ea1 9 //Serial usb(USBTX,USBRX);
Thomas Cauwelier 12:a070de571ea1 10 //
Thomas Cauwelier 12:a070de571ea1 11 //char *read_2_bytes(Serial *serial, char out[2]) {
Thomas Cauwelier 12:a070de571ea1 12 // int i = 0;
Thomas Cauwelier 12:a070de571ea1 13 // while (true) {
Thomas Cauwelier 12:a070de571ea1 14 // while (!serial->readable());
Thomas Cauwelier 12:a070de571ea1 15 // char c = (char) serial->getc();
Thomas Cauwelier 12:a070de571ea1 16 // if (c == 0b11100011) //no data flag
Thomas Cauwelier 12:a070de571ea1 17 // continue;
Thomas Cauwelier 12:a070de571ea1 18 // else {
Thomas Cauwelier 12:a070de571ea1 19 // out[i] = c;
Thomas Cauwelier 12:a070de571ea1 20 // i++;
Thomas Cauwelier 12:a070de571ea1 21 // }
Thomas Cauwelier 12:a070de571ea1 22 //
Thomas Cauwelier 12:a070de571ea1 23 // if (i == 2) {
Thomas Cauwelier 12:a070de571ea1 24 // break;
Thomas Cauwelier 12:a070de571ea1 25 // }
Thomas Cauwelier 12:a070de571ea1 26 // }
Thomas Cauwelier 12:a070de571ea1 27 // return out;
Thomas Cauwelier 12:a070de571ea1 28 //}
Thomas Cauwelier 12:a070de571ea1 29 //
Thomas Cauwelier 12:a070de571ea1 30 //
Thomas Cauwelier 12:a070de571ea1 31 //void receive_uart() {
Thomas Cauwelier 12:a070de571ea1 32 // while (true) {
Thomas Cauwelier 12:a070de571ea1 33 // if (in.readable()) {
Thomas Cauwelier 12:a070de571ea1 34 // char s[2];
Thomas Cauwelier 12:a070de571ea1 35 // read_2_bytes(&in, s);
Thomas Cauwelier 12:a070de571ea1 36 // char dec[1];
Thomas Cauwelier 12:a070de571ea1 37 // bool success = Manchester::decode_manchester(s, 2, dec);
Thomas Cauwelier 12:a070de571ea1 38 // printf(dec);
Thomas Cauwelier 12:a070de571ea1 39 // if (!success) {
Thomas Cauwelier 12:a070de571ea1 40 // printf("#");
Thomas Cauwelier 12:a070de571ea1 41 // }
Thomas Cauwelier 12:a070de571ea1 42 // } else {
Thomas Cauwelier 12:a070de571ea1 43 // Thread::yield();
Thomas Cauwelier 12:a070de571ea1 44 // }
Thomas Cauwelier 12:a070de571ea1 45 // }
Thomas Cauwelier 12:a070de571ea1 46 //}
Thomas Cauwelier 12:a070de571ea1 47 //
Thomas Cauwelier 12:a070de571ea1 48 //void send_uart(Serial *serial, const char *s, int size) {
Thomas Cauwelier 12:a070de571ea1 49 // int size_m = size * 2;
Thomas Cauwelier 12:a070de571ea1 50 // char encoded[size_m];
Thomas Cauwelier 12:a070de571ea1 51 // Manchester::encode_manchester(s, size, encoded);
Thomas Cauwelier 12:a070de571ea1 52 //
Thomas Cauwelier 12:a070de571ea1 53 // for (int i = 0; i < size_m; ++i) {
Thomas Cauwelier 12:a070de571ea1 54 // while (!serial->writeable());
Thomas Cauwelier 12:a070de571ea1 55 // serial->putc(encoded[i]);
Thomas Cauwelier 12:a070de571ea1 56 // }
Thomas Cauwelier 12:a070de571ea1 57 //}
Thomas Cauwelier 12:a070de571ea1 58 //
Thomas Cauwelier 12:a070de571ea1 59 //void hardware_uart_test(){
Thomas Cauwelier 12:a070de571ea1 60 //
Thomas Cauwelier 12:a070de571ea1 61 //
Thomas Cauwelier 12:a070de571ea1 62 // printf("%s\r\n", "hello, term!");
Thomas Cauwelier 12:a070de571ea1 63 //
Thomas Cauwelier 12:a070de571ea1 64 // Thread thread;
Thomas Cauwelier 12:a070de571ea1 65 // thread.start(receive_uart);
Thomas Cauwelier 12:a070de571ea1 66 //
Thomas Cauwelier 12:a070de571ea1 67 // while (true) {
Thomas Cauwelier 12:a070de571ea1 68 // char tosend[28];
Thomas Cauwelier 12:a070de571ea1 69 // Manchester::encode_manchester("hello, term\r\n", 14, tosend);
Thomas Cauwelier 12:a070de571ea1 70 // send_uart(&out, tosend, 28);
Thomas Cauwelier 12:a070de571ea1 71 // //wait(1);
Thomas Cauwelier 12:a070de571ea1 72 // }
Thomas Cauwelier 12:a070de571ea1 73 //}
Desertification 1:6b839581dc9f 74
Thomas Cauwelier 13:c111ea1c5990 75 void client(){
Desertification 14:e29cca7e64b2 76 SoftSerial s(p18,p17);
Desertification 14:e29cca7e64b2 77 s.baud(1);
Desertification 14:e29cca7e64b2 78 s.format(16, SoftSerial::None, 1);
Thomas Cauwelier 13:c111ea1c5990 79 while (true) {
Thomas Cauwelier 13:c111ea1c5990 80 while (!s.readable());
Desertification 14:e29cca7e64b2 81 printf("got %i\r\n",s.getc());
Thomas Cauwelier 13:c111ea1c5990 82 }
Thomas Cauwelier 13:c111ea1c5990 83 }
Thomas Cauwelier 13:c111ea1c5990 84
Thomas Cauwelier 13:c111ea1c5990 85 void relay(){
Desertification 14:e29cca7e64b2 86 SoftSerial s(LED1,p18);
Desertification 14:e29cca7e64b2 87 s.baud(1);
Desertification 14:e29cca7e64b2 88 s.format(16, SoftSerial::None, 1);
Desertification 14:e29cca7e64b2 89 int i = 0;
Thomas Cauwelier 13:c111ea1c5990 90 while (true){
Desertification 14:e29cca7e64b2 91 wait(0.1);
Desertification 14:e29cca7e64b2 92 while(!s.writeable());
Desertification 14:e29cca7e64b2 93 printf("put %i\r\n", 0b0101010100110011);
Desertification 14:e29cca7e64b2 94 s.putc(0b0101010100110011);
Desertification 14:e29cca7e64b2 95 i++;
Thomas Cauwelier 13:c111ea1c5990 96 }
Thomas Cauwelier 13:c111ea1c5990 97 }
Thomas Cauwelier 13:c111ea1c5990 98
Thomas Cauwelier 12:a070de571ea1 99 #define RELAY 1
Thomas Cauwelier 11:e462ce7a42b9 100
Thomas Cauwelier 13:c111ea1c5990 101 Serial usb(USBTX,USBRX);
Thomas Cauwelier 13:c111ea1c5990 102
Desertification 1:6b839581dc9f 103 int main() {
Thomas Cauwelier 13:c111ea1c5990 104 usb.baud(115200);
Thomas Cauwelier 12:a070de571ea1 105 if (RELAY){
Thomas Cauwelier 13:c111ea1c5990 106 printf("relay\r\n");
Thomas Cauwelier 13:c111ea1c5990 107 relay();
Thomas Cauwelier 12:a070de571ea1 108 } else {
Thomas Cauwelier 13:c111ea1c5990 109 printf("client\r\n");
Thomas Cauwelier 13:c111ea1c5990 110 client();
Thomas Cauwelier 6:0e1973fdfe28 111 }
thoma@THOMAS-CAUWELIER.khbo.be 4:a07612fbf279 112 }