Gets messages form the pc and translates it to I2C and back.

Dependencies:   DevInterfaces I2Cinterfaces MCP4725 mbed

Revision:
2:2330ad8b1baa
Parent:
1:8ba039abd9b8
--- a/Reader.cpp	Wed May 18 11:35:45 2016 +0000
+++ b/Reader.cpp	Wed Jun 15 10:53:32 2016 +0000
@@ -9,8 +9,6 @@
     _buffer(),
     _bufferSize(0),
     _lastChar(0),
-    _isMessageStarted(false),
-    _charsToRead(-1),
     _index(0)
 {
     // Attach read function to pc
@@ -32,7 +30,7 @@
     return _lastMessageInfo;
 }
 
-const int8_t* Reader::GetLastMessage() const {
+const char* Reader::GetLastMessage() const {
     return _lastMessage;
 }
 
@@ -43,65 +41,41 @@
 void Reader::pcCallback() {
     // Get the sent character
     _lastChar = pc.getc();
-    if (_lastChar == Translator::Rules::StartChar) {
-        _index = 0;
-    }
-    // Add character to buffer
-    _buffer[_index] = _lastChar;
-    // Check if valid message by translating it
-    Translator::MessageInfo info;
-    if (!Translator::Translate(_buffer, _index + 1, &info)) {
-        _index++;
-        return;
-    }
     
-    // Translation succesful, copy to _info
-    _lastMessageInfo = info;
-    // Notify user of new message
-    _isNewMessageReceived = true;
-    // Reset counter
-    _index = 0;
-    return;
-
-    
-    
+    // If message not started and StartChar was read
+    //if (!_isMessageStarted && _lastChar == Translator::Rules::StartChar) {
+    //    // Reset and signal start
+    //    _index = 0;
+    //    _isMessageStarted = true;
+    //}
     
-    //printf("'%c' (%i) ; ", _lastChar, (int)_lastChar);
-    // If no ongoing message and starting character was received
-    if (_lastChar == Translator::Rules::StartChar) {
-        //printf("First char: %c", _lastChar);
-        // Get ready to receive the message
-        _isMessageStarted = true;
-        _index = 0;
-        _charsToRead = -1; // value < 0 indicates no length was read yet
-    } // If message was started but no length was read yet
-    else if (_charsToRead < 0) {
-        // Get ready to receive the command
-        _charsToRead = (int)_lastChar;
-        //printf("CharsToRead: %i", _charsToRead);
-        //_buffer = new int8_t[_charsToRead];
-        _index = 0;
-    } // If message was started and size is known
-    else if (_charsToRead > 0) {
-        //printf("Storing '%c' (%i)", _lastChar, (int)_lastChar);
-        // Read _charsToRead characters and store in _buffer
-        if (_index < _charsToRead) {
-            _buffer[_index] = _lastChar;
+    // If ongoing message
+    //if (_isMessageStarted) {
+        
+        // Add character to buffer
+        _buffer[_index] = _lastChar;
+        // Check if valid message by translating it
+        Translator::MessageInfo info;
+        
+        switch (Translator::Translate(_buffer, _index + 1, &info)) {
+        case Translator::INCOMPLETE:
+            // Keep adding chars
             _index++;
-            
-            // If message was fully received
-            if (_index == _charsToRead) {
-                // Copy _buffer to _lastMessage (see Utility.h)
-                DeepCopy(_buffer, _charsToRead, _lastMessage);
-                _lastMessageSize = _charsToRead;
-                // Reset values
-                _isMessageStarted = false;
-                _charsToRead = -1;
-                _index = 0;
-                // Let user know that a new message was received
-                _isNewMessageReceived = true;
-            }
+            break;;
+        case Translator::SUCCES:
+            // Copy message info
+            _lastMessageInfo = info;
+            // Notify user
+            _isNewMessageReceived = true;
+            // Reset counter
+            _index = 0;
+            break;
+        case Translator::INVALID:
+        default:
+            // Reset buffer
+            _index = 0;
+            break;
         }
-    }
-    //printf(" / ");
+    //}
+    return;
 }
\ No newline at end of file