Anders Rundgren / Mbed 2 deprecated SerialDriverTest

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 Serial pc(USBTX, USBRX); // tx, rx
00004 
00005 DigitalOut myled_4(LED4);
00006 DigitalOut myled_3(LED3);
00007 DigitalOut myled_2(LED2);
00008 DigitalOut myled_1(LED1);
00009 
00010 Timer serial_timeout;
00011 
00012 const int MAX_DELAY_BETWEEN_CHARCTERS_IN_MS = 2000;
00013 
00014 const int BAUD_RATE = 115200;
00015 
00016 const int CAPACITY_COMMAND       = 1;
00017 const int MISSING_DATA_COMMAND   = 2;
00018 const int DEVICE_TIMEOUT_COMMEND = 3;
00019 
00020 static int counter;
00021 
00022 static int length;
00023 static int curr_pos;
00024 
00025 static bool buffer_full;
00026 
00027 static int readCharTimed ()
00028   {
00029     serial_timeout.reset ();
00030     while (!pc.readable ())
00031       {
00032         if (serial_timeout.read_ms () > MAX_DELAY_BETWEEN_CHARCTERS_IN_MS)
00033           {
00034             myled_1 = 1;
00035             return -1;
00036           }
00037       }
00038     return pc.getc ();
00039   }
00040 
00041 int getCommand (int first_char)
00042   {
00043     buffer_full = false;
00044     serial_timeout.start ();
00045     int next_char = readCharTimed ();
00046     if (next_char < 0)
00047       {
00048         myled_2 = 1;
00049         return -1;
00050       }
00051     length = ((first_char << 8) + next_char) & 0xFFFF;
00052     curr_pos = 1;
00053     return readCharTimed ();  // This is the command (pos 0 in expected buffer)
00054   }
00055 
00056 #define OUTPUT_TIMED
00057 
00058 void putByte (int c)
00059   {
00060 #ifdef OUTPUT_TIMED
00061     if (!buffer_full)
00062       {
00063         serial_timeout.start ();
00064         while (true)
00065           {
00066             if (pc.writeable ())
00067               {
00068 #endif
00069                 pc.putc (c);
00070 #ifdef OUTPUT_TIMED
00071                 break;
00072               }
00073             if (serial_timeout.read_ms () > MAX_DELAY_BETWEEN_CHARCTERS_IN_MS)
00074               {
00075                 myled_1 = 1;
00076                 myled_4 = 1;
00077                 buffer_full = true;
00078                 break;
00079               }
00080           }
00081       }
00082 #endif
00083   }
00084 
00085 
00086 void putSuccessStatus ()
00087   {
00088     if (curr_pos != length)
00089       {
00090         myled_1 = 1;
00091         myled_2 = 1;
00092         myled_3 = 1;
00093       }
00094     putByte (0);
00095   }
00096 
00097 
00098 void putShort (int v)
00099   {
00100     putByte (v >> 8);
00101     putByte (v & 0xFF);
00102   }
00103 
00104 
00105 void putString (char *string)
00106   {
00107     int len = strlen (string);
00108     putShort (len);
00109     for (int i = 0; i < len; i++)
00110       {
00111         pc.putc (string[i]);
00112       }
00113   }
00114 
00115 
00116 int getChar ()
00117   {
00118     if (curr_pos++ >= length)
00119       {
00120         myled_3 = 1;
00121       }
00122     return readCharTimed() & 0xFF;
00123   }
00124 
00125 int getShort ()
00126   {
00127     int v = getChar () << 8;
00128     return v + getChar ();
00129   }
00130 
00131 int main()
00132   {
00133     pc.baud (BAUD_RATE);
00134     while (1)
00135       {
00136         counter++;
00137         switch (getCommand (pc.getc ()))
00138           {
00139             case CAPACITY_COMMAND:
00140               int out_buffer_size;
00141               out_buffer_size = getShort () & 0xFFFF;
00142               int in_buffer_size;
00143               in_buffer_size = getShort () & 0xFFFF;
00144               while (in_buffer_size-- > 0)
00145                 {
00146                   getChar ();
00147                 }
00148               putSuccessStatus ();
00149               putShort (out_buffer_size);
00150               while (out_buffer_size-- > 0)
00151                 {
00152                   putByte (0);
00153                 }    
00154               break;
00155 
00156             case MISSING_DATA_COMMAND:
00157               getShort ();
00158               putSuccessStatus ();
00159               break;
00160 
00161             case DEVICE_TIMEOUT_COMMEND:
00162               wait (30);
00163               putSuccessStatus ();
00164               break;
00165               
00166             default:
00167               myled_2 = 1;
00168               pc.putc (7);
00169               putString ("No such command...");
00170           }
00171       }
00172   }
00173