My first revision of this m3pi logo program

Dependencies:   mbed MSCFileSystem_Lib m3pi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MSCFileSystem.h"
00003 #include "m3pi.h"
00004 
00005 #define MOTOR_ENABLED
00006 #define USBFS
00007 
00008 m3pi m3pi;
00009 
00010 BusOut leds(LED1,LED2,LED3,LED4);
00011 
00012 DigitalIn button(p21);
00013 
00014 int main() {
00015 
00016     // pull up the digital input for the button - The
00017     button.mode(PullUp);
00018 
00019     LocalFileSystem *lfs = NULL;
00020     MSCFileSystem *mfs = NULL;
00021     m3pi.locate(0,1);
00022 
00023     if (button) {
00024         mfs = new MSCFileSystem("fs");
00025         m3pi.printf(" USB    ");
00026     } else {
00027         lfs = new LocalFileSystem("fs");
00028         m3pi.printf(" Local    ");
00029     }
00030  
00031     wait (0.5);
00032 
00033     float speed = 0.5;
00034     leds = 0x0;
00035 
00036     // Open the command.txt file
00037     FILE *commandfile = fopen("/fs/command.txt", "r");
00038 
00039     // If there is no file, sit flashing the LEDs
00040     if (commandfile == NULL) {
00041         m3pi.cls();
00042         m3pi.printf("No File");
00043         while (1) {
00044             leds=0x9;
00045             wait(0.2);
00046             leds=0x6;
00047             wait(0.2);
00048         }
00049     }
00050 
00051     printf("File found\n");
00052 
00053     m3pi.cls();
00054     m3pi.printf(" File   ");
00055     m3pi.locate(0,1);
00056     m3pi.printf(" Found  ");
00057 
00058     wait (0.5);
00059 
00060     // process each of the commands in the file
00061     while (!feof(commandfile)) {
00062 
00063         char command = 0;
00064         int data = 0.0;
00065 
00066         // Read from the command file
00067         fscanf(commandfile, "%c %d\n", &command, &data);
00068         printf("%c %d\n",command,data);
00069 
00070         m3pi.cls();
00071         m3pi.printf(" %c %4d",command,data);
00072 
00073         // Turn forward
00074         if ((command=='f') || (command=='F')) {
00075 #ifdef MOTOR_ENABLED
00076             m3pi.forward(speed);
00077 #endif
00078         }
00079 
00080         // Turn back
00081         else if ((command=='b') || (command=='B')) {
00082 #ifdef MOTOR_ENABLED
00083             m3pi.backward(speed);
00084 #endif
00085         }
00086 
00087         // Turn left
00088         else if ((command=='l') || (command=='L')) {
00089 #ifdef MOTOR_ENABLED
00090             m3pi.left(speed);
00091 #endif
00092         }
00093 
00094         // Turn right
00095         else if ((command=='r') || (command=='R')) {
00096 #ifdef MOTOR_ENABLED
00097             m3pi.right(speed);
00098 #endif
00099         }
00100 
00101         // Set LED 8,4,2,1
00102         else if ((command=='d') || (command=='D')) {
00103             leds=data;
00104         }
00105 
00106         // Set Speed 0-100
00107         else if ((command=='s') || (command=='S')) {
00108             if ((data > 0) && (data <= 100)) {
00109                 float foo = data/100.0;
00110                 speed = foo;
00111                 printf("speed = %f\n", foo);
00112             }
00113         }
00114 
00115         // Wait commands
00116         else if ((command=='w') || (command=='W')) {
00117         }
00118 
00119         // wait for the length of the command
00120         wait(data/1000.0);
00121         m3pi.stop();
00122 
00123     } // this is the end of the file while loop
00124 
00125     fclose(commandfile);
00126 
00127     m3pi.stop();
00128     m3pi.cls();
00129     m3pi.printf("  Done  ");
00130 
00131     printf("Done\n");
00132 
00133     while (1) {
00134         leds=0;
00135         wait(0.5);
00136         leds=0xf;
00137         wait(0.5);
00138     }
00139 
00140 } // end of main