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

Dependencies:   DevInterfaces I2Cinterfaces MCP4725 mbed

Revision:
2:2330ad8b1baa
Parent:
1:8ba039abd9b8
--- a/main.cpp	Wed May 18 11:35:45 2016 +0000
+++ b/main.cpp	Wed Jun 15 10:53:32 2016 +0000
@@ -25,14 +25,28 @@
 #include "mcp4725.h"
 #include "Translator.h"
 #include "Reader.h"
-#include "Writer.h"
 
 // Initialize LEDs
 PwmOut rled(LED1);
 PwmOut gled(LED2);
 PwmOut bled(LED3);
 
-Serial pc(USBTX, USBRX);
+//Serial pc(USBTX, USBRX);
+
+// Sends the message to the pc
+bool Send(Translator::MessageInfo info);
+void SendError();
+void SendError(int address);
+void SendError(char* data, int dataSize);
+void SendError(int address, char* data, int dataSize);
+void SendError(Translator::MessageInfo info);
+
+// Turns LED blue
+void SignalWaiting();
+// Turns LED green
+void SignalReceived();
+// Turns LED red
+void SignalError();
 
 int main(void) {
     // <SDA en SCL zijn de pins hierboven gedefinieerd, wss hardware eigenschappen dat je mag negeren>
@@ -51,54 +65,124 @@
     
     // Start infinite loop
     while (true) {
-        rled = 1.0f;
-        gled = 1.0f;
-        bled = 0.0f;
+        SignalWaiting();
         if (!reader.IsNewMessageReceived())
             continue; // Do nothing as long as no new complete message was received
-        rled = 1.0f;
-        gled = 0.0f;
-        bled = 1.0f;
+        SignalReceived();
+        
         
-        // Translate the raw message
+        
+        // Get the formatted message
         Translator::MessageInfo info = reader.GetLastMessageInfo();
-        //if (!Translator::Translate(reader.GetLastMessage(), reader.GetLastMessageSize(), &info)) {
-        //    printf("I have failed you...");
-        //    continue;
-        //}
         
-        int dacValue;
-        if (info.IsRead()) { // If read command
-            //printf("Reading ");
-            // Read the current DAC value
-            if (dac.getDACvalue(dacValue) == 0) {
-                //printf("succes!");
-                // Succesful: send back the current DAC value
-                Writer::Send(info, dacValue);
-            } else {
-                //printf("failed");
-                // Failed: send error message
-                Writer::SendError(info);
+        // If write was specified
+        if (info.IsWrite()) {
+            // Issue write
+            if (i2cdev->write(info.Address, info.Data, Translator::Rules::WriteDataSize, false) != 0) {
+            //if (dac.setDACvalue((int)(unsigned char)info.Data[1]<<4+(int)(unsigned char)info.Data[2]>>4, 0) != 0) {
+                SendError(info);
+                continue;
+            }
+            // Issue also a read
+            //if (i2cdev->read(info.Address, info.Data, Translator::Rules::ReadDataSize, false) != 0) {
+            //    //SendError(info);
+            //    continue;
+            //}
+            if (!Send(info)) {
+                SendError(info);
             }
-        } else if (info.IsWrite()) { // If write command
-            //printf("Writing ");
-            // Set the given DAC value
-            if (dac.setDACvalue(info.DacValue) == 0) {
-                //printf("succes!");
-                // Succesful: Get the DAC value
-                dac.getDACvalue(dacValue);
-                // Send new value
-                Writer::Send(info, dacValue);
-            } else {
-                //printf("failed");
-                // Failed: send error message
-                Writer::SendError(info);
+        }
+        // If read was specified
+        else if (info.IsRead()) {
+            // read from DAC
+            if (i2cdev->read(info.Address, info.Data, sizeof(info.Data)/sizeof(info.Data[0]), false) != 0) {
+                SendError(info);
+                continue;
             }
-        } else if (info.IsOption()) {
-            //printf("Option failed");
-            // Not implemented yet
-            Writer::SendError(info);
+            // send read values to pc
+            if (!Send(info))
+                SendError();
         }
+        // If error was specified
+        else if (info.IsError()) {
+            // Send the error back
+            if (!Send(info))
+                SendError();
+        }
+        
+        // Don't overdo it
+        wait_ms(500);
     }
 }
 
+
+bool Send(Translator::MessageInfo info) {
+    // Create buffer
+    int bufferSize;
+    char buffer[Translator::Rules::MaxCmdSize];
+    // Translate to raw message
+    if (!Translator::Translate(info, buffer, &bufferSize))
+        return false;
+    // Send every character
+    for (int i = 0; i < bufferSize; i++)
+        printf("%c", buffer[i]);
+    return true;
+}
+
+void SendError() {
+    SendError(0,0,0);
+}
+
+void SendError(int address) {
+    SendError(address, 0, 0);
+}
+
+void SendError(char* data, int dataSize) {
+    SendError(0, data, dataSize);
+}
+
+void SendError(int address, char* data , int dataSize) {
+    // Create MessageInfo with error
+    Translator::MessageInfo info;
+    info.Address = address;
+    for (int i = 0; i < dataSize; i++)
+        info.Data[i] = data[i];
+    SendError(info);
+}
+
+void SendError(Translator::MessageInfo info) {
+    SignalError();
+    info.CommandType = Translator::MessageInfo::ERROR;
+    Send(info);
+}
+
+void SignalWaiting() {
+    // Blue light
+    rled = 1.0f;
+    gled = 1.0f;
+    bled = 0.0f;
+}
+
+void SignalReceived() {
+    // Green light
+    rled = 1.0f;
+    gled = 0.0f;
+    bled = 1.0f;
+}
+
+void SignalError() {
+    // Red light
+    rled = 0.0f;
+    gled = 1.0f;
+    bled = 1.0f;
+}
+
+
+
+
+
+
+
+
+
+