Final Project files for mBed development.

Dependencies:   m3pi mbed

Files at this revision

API Documentation at this revision

Comitter:
alecguertin
Date:
Mon Dec 01 00:41:04 2014 +0000
Parent:
16:f1beef7beeb9
Child:
18:eab7b0e89398
Commit message:
added instruction parsing loop -- untested

Changed in this revision

main.c Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.c	Sun Nov 30 21:52:58 2014 +0000
+++ b/main.c	Mon Dec 01 00:41:04 2014 +0000
@@ -40,11 +40,18 @@
 /* Local File System */
 LocalFileSystem local("local");
 
+/* Boolean for drawing/moving. */
+int draw;
+
 /**
  * @brief Entry point. Main loop.
  */
 int main()
 {
+    FILE *ps_file;
+    int instbuflen = 250;
+    char instbuf[instbuflen];
+    
     //
     // Basic setup information
     start_button.mode(PullUp);
@@ -76,7 +83,7 @@
         pi.locate(0,0);
         pi.printf("P: %f", pos);
         */
-    } while (pos != -1 && pos < 0.2);
+    } while (pos != -1 && pos <= 0.3);
     pi.stop();
     wait(1);
     if (pos != -1) {
@@ -181,17 +188,60 @@
     //
     // Main program loop.
     // robot_loop();
-    
+    size_t bytes_read = 0;
+    int err, x, y, last_x, last_y;
+    int offset = 0;
+    char *cur, *next;
     ps_file = fopen("/local/test.ps", "r");
-    while () {
-        memset(instbuf, 0, instbuflen);
-        fread(instbuf, sizeof(char), instbuflen, ps_file);
+    /* PS parsing loop. */
+    while (1) {
+        memset(instbuf+offset, 0, instbuflen-offset);
+        bytes_read = fread(instbuf+offset, sizeof(char), instbuflen-1-offset, ps_file);
+        if (bytes_read == 0) {
+            break;
+        }
+        cur = instbuf;
+        while (cur[0] != '\0') {
+            err = retrieve_inst(instbuf, &x, &y, &draw);
+            move(x, y, draw);
+            last_x = x;
+            last_y = y;
+            next = strchr(cur, '\n');
+            if (next == NULL) {
+                break;
+            }
+            cur = next+1;
+        }
+        offset = instbuf+instbuflen-cur;
+        memcpy(instbuf, cur, offset);
     }
     //
     // We should never reach this point!
     return 0;
 }
 
+int retrieve_inst(char *buf, int *x, int *y, int *draw)
+{
+    int matches;
+    char *srch;
+    matches = sscanf(buf, "%d %d", x, y);
+    if (matches != 2) {
+        return 0;
+    }
+    srch = strchr(buf, ' ');
+    srch++;
+    srch = strchr(buf, ' ');
+    srch++;
+    if (srch[0] == 'm') {
+        *draw = 0;
+    } else if (srch[0] == 'd') {
+        *draw = 1;
+    } else {
+        return 0;
+    }
+    return 1;
+}
+
 int forward(int amt)
 {
     Timer t;
--- a/main.h	Sun Nov 30 21:52:58 2014 +0000
+++ b/main.h	Mon Dec 01 00:41:04 2014 +0000
@@ -20,6 +20,18 @@
 #define DRIVE_RATE  1/50
 
 /**
+ * @brief get values of next PostScript instruction.
+ *
+ * @param       buf     Buffer with PS instructions.
+ * @param       x       Pointer to storage for x coordinate.
+ * @param       y       Pointer to storage for y coordinate.
+ * @param       draw    Pointer to storage for draw/move boolean.
+ * 
+ * @return              Success or failure code.
+ */
+int retrieve_inst(char *buf, int *x, int *y, int *draw);
+
+/**
  * @brief Driver forward.
  *
  * @param[in]   amt     Amount to drive forward. In centimeters.