Command class for communication. Commands have a command member and up to four arguments with variable types.

Files at this revision

API Documentation at this revision

Comitter:
williampeers
Date:
Wed Aug 09 01:08:06 2017 +0000
Commit message:
Working. Doesn't detect bad argument type though, just treats it like expected type without giving error.

Changed in this revision

commands.cpp Show annotated file Show diff for this revision Revisions of this file
commands.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 90ca7dd67eb8 commands.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/commands.cpp	Wed Aug 09 01:08:06 2017 +0000
@@ -0,0 +1,80 @@
+#include "commands.h"
+
+Message::Message(){
+    cmd = VOID;
+    for (int i = 0; i < 4; i++) {
+        strcpy(arguments[i].characters, "");
+        arguments[i].type = BLANK;
+    }
+}
+
+Message::Message(char str[44]){
+    cmd = VOID;
+    for (int i = 0; i < 4; i++) {
+        strcpy(arguments[i].characters, "");
+        arguments[i].type = BLANK;
+    }
+    parse(str);
+}
+
+bool Message::parse(const char str[44]){
+    char args[5][8] = {};
+    int argIndex = 0, ind = 0;
+    for (int i = 0; i < 44; i++) {  
+        if (str[i] == '$') {
+            args[argIndex][ind] = '\0';
+            argIndex++;
+            ind = 0;
+        }
+        else if (str[i] == '\0') {
+            args[argIndex][ind] = '\0';
+            break;   
+        }
+        else {
+            if (ind > 7) {
+                return(false);   
+            }
+            else {
+                args[argIndex][ind] = str[i];
+                ind++;
+            }
+        }
+    }
+    cmd = VOID;
+    for (int i = 0; i < TOTAL_COMMANDS; i++) {
+        if (strcmp(args[0], commandStrings[i]) == 0) {
+            cmd = (command)i;
+        }
+    }
+    
+    for (int i = 0; i < 4; i++) {
+        switch(arguments[i].type = commandArgTypes[cmd][i]) {
+            case DECIMAL:
+                arguments[i].decimal = atof(args[i+1]);
+                break;
+            case INTEGER:
+                arguments[i].integer = atoi(args[i+1]);
+                break;
+            case CHARACTERS:
+                strcpy(arguments[i].characters, args[i+1]);
+                break;
+        } 
+    }
+    return(true);
+}
+
+command Message::getCommand(){
+    return(cmd);
+}
+
+argument Message::getArg(int ind, argumentType expectedType){
+    if (expectedType == arguments[ind].type) {
+        return(arguments[ind]);
+    }
+    else {
+        argument blankArg;
+        blankArg.type = BLANK;
+        strcpy(blankArg.characters, "");
+        return(blankArg);
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r 90ca7dd67eb8 commands.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/commands.h	Wed Aug 09 01:08:06 2017 +0000
@@ -0,0 +1,53 @@
+#ifndef __COMMANDS__
+#define __COMMANDS__
+
+#include "mbed.h"
+
+enum argumentType {
+    BLANK = -1,
+    DECIMAL,
+    INTEGER,
+    CHARACTERS
+};
+
+
+/*
+DRIVE: INTEGER lSpeed, INTEGER rSPeed
+LED: INTEGER on
+*/
+enum command {
+    VOID = -1,
+    DRIVE = 0,
+    LED,
+    TOTAL_COMMANDS
+};
+
+struct argument {
+    union {
+        float decimal;
+        int integer;
+        char characters[8];
+    };
+    argumentType type;
+};
+
+const char commandStrings[2][8] = {"DRIVE", "LED"};
+
+const argumentType commandArgTypes[2][4] = {
+    {INTEGER, INTEGER, BLANK, BLANK},
+    {INTEGER, BLANK, BLANK, BLANK}
+};
+
+class Message {
+private:
+    command cmd;
+    argument arguments[4];
+public:
+    Message();
+    Message(char[44]);
+    bool parse(const char[44]);
+    command getCommand();
+    argument getArg(int ind, argumentType expectedType);
+};
+
+#endif
\ No newline at end of file