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

Dependencies:   DevInterfaces I2Cinterfaces MCP4725 mbed

Revision:
2:2330ad8b1baa
Parent:
0:b40341017545
--- a/Translator.h	Wed May 18 11:35:45 2016 +0000
+++ b/Translator.h	Wed Jun 15 10:53:32 2016 +0000
@@ -1,17 +1,51 @@
 #pragma once
 
+#define MAX(a,b) (((a) > (b)) ? (a) : (b))
+
 #include <string>
 #include "mbed.h"
-#include "Utility.h"
 
 // Implements the Data-Link layer, translating a raw message to MessageInfo and back
 class Translator {
+    
 public:
+    enum EErrorCode {
+        SUCCES,
+        INCOMPLETE,
+        INVALID
+    };
+    
+    // Rules over how the command is structured, indexes are per byte
+    struct Rules {
+        static const int StartCharIndex = 0;   // Location of starting char
+        static const int CharsToReadIndex = 1; // Location of chars to read value
+        static const int AddressIndex = 2;     // Location of pin address
+        static const int CommandIndex = 3;     // Location of command type char
+        static const int DataIndex = 4;        // Location of data start
+
+        static const char StartChar = '!'; // Starting character
+        static const char ReadChar = 'r';  // Character to indicate read command
+        static const char WriteChar = 'w'; // Character to indicate write command
+        static const char OtherChar = 'o'; // Character to indicate other command
+        static const char ErrorChar = 'e'; // Character to indicate an error
+
+        static const int PrefixSize = CharsToReadIndex + 1; // Number of chars in the prefix
+        static const int ReadDataSize = 5;   // Number of chars used in read data
+        static const int WriteDataSize = 3;   // Number of chars used in write data
+        static const int MaxDataSize = MAX(ReadDataSize, WriteDataSize); // Maximum size of data array
+        static const int EmptyDataCmdSize = DataIndex; // Number of chars in command if no data
+        static const int ReadCmdSize = EmptyDataCmdSize + ReadDataSize;  // Size of a read command
+        static const int WriteCmdSize = EmptyDataCmdSize + WriteDataSize; // Size of a write command
+        static const int ErrorCmdSize = EmptyDataCmdSize; // Size of an error command
+
+        static const int MaxCmdSize = MAX(ReadCmdSize, WriteCmdSize); // Maximum total command size
+    };
+    
     // Contains easy retreivable message info
     struct MessageInfo {
-        enum ECommandType { NONE, READ, WRITE, OPTION, ERROR } CommandType;
-        int Channel; //Not used
-        int DacValue;
+        enum ECommandType { NONE, READ, WRITE, ERROR } CommandType;
+        int Address;
+        char Data[Rules::MaxCmdSize];
         
         MessageInfo();
         
@@ -19,36 +53,10 @@
         bool IsNone();
         bool IsRead();
         bool IsWrite();
-        bool IsOption();
-    };
-    
-private:
-    // Contains the raw message in bytes
-    const int8_t* _rawMessage;
-    // Size of the raw message
-    int _size;
-    // Message info, is populated in SetRawMessage
-    MessageInfo _message;
-    
-public:
-    // Contains rules about how the raw message is formatted
-    struct Rules {
-        static int    StartCharIndex;
-        static int8_t StartChar;
-        static int    CharsToReadIndex;
-        static int    CharsToReadOffset;
-        static int    rwoIndex;
-        static int8_t ReadChar;
-        static int8_t WriteChar;
-        static int8_t OptionChar;
-        static int8_t ErrorChar;
-        //static int    ChannelIndex;
-        static int    DataIndex;
-        static int    DataLength;
+        bool IsError();
 
-        static int ReadCmdSize;
-        static int WriteCmdSize;
-        static int ErrorCmdSize;
+        // Resets all values to default
+        void Reset();
     };
 
 private:
@@ -62,19 +70,14 @@
     //  cmdSize geeft de grootte van de pointer ZONDER het null karakter zodat het zowel veilig
     //  kan gebruikt worden in printf en andere functies die geen null karakter vereisen (en waarbij
     //  dus het NULL karakter niet zal ingelezen worden>
-    // Translate MessageInfo to raw int8_t* (NULL terminated)
-    static bool Translate(MessageInfo info, int8_t** const cmd, int* const cmdSize);
+    // Translate MessageInfo to raw char* (NULL terminated)
+    static bool Translate(MessageInfo info, char cmd[Rules::MaxCmdSize], int* const cmdSize);
     // <Hier bijvoorbeeld is het NULL karakter niet vereist omdat de grootte wordt opgevraagt
-    //  Indien de int8_t* van vorige functie hier terug wordt in gezet volstaat het om ook zijn
+    //  Indien de char* van vorige functie hier terug wordt in gezet volstaat het om ook zijn
     //  cmdSize te gebruiken: het NULL karakter valt hier buiten en wordt daarom dus niet gelezen
     //  Dit zal ik toevoegen aan de ondervonden problemen.>
-    // Translate raw int8_t* message to MessageInfo
-    static bool Translate(const int8_t* const cmd, int cmdSize, MessageInfo* const info);
+    // Translate raw char* message to MessageInfo
+    // Returns 0 if succesful, -1 if message too short, 1 for faulty/unrecoverable message
+    static EErrorCode Translate(const char* const cmd, int cmdSize, MessageInfo* const info);
 
-    
-private:
-    void SetMessageInfo();
-    void ResetMessageInfo();
-    void InvalidateMessage();
-    
 };
\ No newline at end of file