Dependents:   communication

Revision:
0:964bbeb0a1d3
Child:
1:36cd04f61e68
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/serial_communication.cpp	Thu Mar 31 04:42:48 2016 +0000
@@ -0,0 +1,62 @@
+#include "serial_communication.hpp"
+#include "mbed.h"
+#include "SimpleMapDeSerializer.hpp"
+#include <map>
+
+serial_communication* serial_communication::instance_ = NULL;
+
+serial_communication::serial_communication() : serial_(USBTX, USBRX),
+                                               semaphore_(true),
+                                               is_new_data_existing_(false) {
+    serial_.baud(115200);
+    serial_.format(7, RawSerial::None, 1);
+    serial_.attach(this, &serial_communication::receive);
+}
+
+serial_communication::~serial_communication() {
+    delete instance_;
+}
+
+int serial_communication::get_data(string key) {
+    //while (!semaphore_) {}
+    return map_[key];
+}
+
+bool serial_communication::update() {
+    if (is_new_data_existing_) {
+        is_new_data_existing_ = false;
+        SimpleMapSerialization::simpleMapDeSerializer(data_, ',', map_);
+        return true;
+    }
+    
+    return false;
+}
+
+I2C i2c(I2C_SDA, I2C_SCL);
+
+void serial_communication::receive() {
+    char c = serial_.getc();
+    
+    switch (c) {
+        case '\n':
+            /*
+            semaphore_ = false;
+            SimpleMapSerialization::simpleMapDeSerializer(buffer_, ',', map_);
+            semaphore_ = true;
+            */
+            data_ = buffer_;
+            buffer_.erase();
+            buffer_ += "a,0,";
+            is_new_data_existing_ = true;
+            
+            break;
+            
+        case '\r':
+            break;
+            
+        default:
+            //i2c.write(0x11 << 1, &c, 1);
+            buffer_ += c;
+            break;
+    }
+}