Thomas Cauwelier / Mbed 2 deprecated visible_light_communication

Dependencies:   SoftSerial SDFileSystem mbed wave_player

Revision:
19:822d93e0bf34
Parent:
17:8683abdc1912
Child:
20:d2a8daeb6eb7
--- a/MySoftSerial.cpp	Wed May 10 15:54:29 2017 +0000
+++ b/MySoftSerial.cpp	Wed May 10 18:21:14 2017 +0200
@@ -7,6 +7,8 @@
 
 MySoftSerial::MySoftSerial(PinName TX, PinName RX, const char *name) : SoftSerial(TX, RX, name) {
     format(16, SoftSerial::None, 1);
+    timer.reset();
+    timer.start();
 }
 
 MySoftSerial::~MySoftSerial() {
@@ -15,11 +17,18 @@
 
 int MySoftSerial::putc(int c) {
     c = Manchester::encode(c);
+    timer.stop();
+    if(timer.read_us() > max_time_between_transmission_us){
+        correct_dc_offset();
+    }
     return _putc(c);
 }
 
 int MySoftSerial::getc() {
-    return Manchester::decode(_getc());
+    int character = _getc();
+    timer.reset();
+    timer.start();
+    return Manchester::decode(character);
 }
 
 void MySoftSerial::prepare_tx(int c) {
@@ -38,3 +47,12 @@
 int MySoftSerial::writeable() {
     return SoftSerial::writeable();
 }
+
+void MySoftSerial::correct_dc_offset() {
+    for (int i = 0; i < 1; ++i) {
+        tx->write(0);
+        wait_us(bit_period * 12); // send 12 zeros
+        tx->write(1);
+        wait_us(bit_period * 4); // send 4 ones
+    }
+}