DCC Speed Step 128 Version

Dependencies:   DSgatewayMBED TrackReporterS88_DS mbed

Fork of DSGatewayMBED_Nucleo by Yaasan K

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "DSGatewayMBED.h"
00003 #include "TrackReporterS88_DS.h"
00004 #include <string>
00005 
00006 #define MAX_S88DECODER 1
00007 #define RELPYERROR_300 "300 Command error"
00008 #define RELPYERROR_301 "301 Syntax error"
00009 #define RELPYERROR_302 "302 receive timeout"
00010 #define RELPYERROR_303 "303 Unknown error"
00011 #define RELPYERROR_NONE ""
00012 
00013 /* class definition */
00014 Serial serial_pc(SERIAL_TX, SERIAL_RX);
00015 DSGatewayLib ds_gw;
00016 TrackReporterS88_DS reporter(MAX_S88DECODER);
00017 
00018 int gCounter = 0;
00019 string gReceivedMessage;
00020 word numOfArguments;
00021 string function;
00022 word arguments[8];
00023 
00024 /* function definition */
00025 word stringToWord(string s);
00026 void decodeSerialMessage(string inMessage);
00027 std::string trim(const std::string& string, const char* trimCharacterList);
00028 boolean ParseMessage(string inRequestText);
00029 boolean dispatch();
00030 
00031 /* Implementation  */
00032 
00033 void decodeSerialMessage(string inMessage)
00034 {
00035     string aReplyText = RELPYERROR_NONE;
00036    
00037     if( ParseMessage(inMessage) == true)
00038     {
00039        
00040         if( dispatch() == true)
00041         {
00042             /* Parse successed */
00043             serial_pc.printf("200 Ok");
00044         }
00045         else
00046         {
00047             serial_pc.printf(RELPYERROR_300);
00048         }
00049     }
00050     else
00051     {
00052         /* Parse failed */
00053         serial_pc.printf(RELPYERROR_301);
00054     }
00055     
00056     /* Reply to Desktop Station */
00057     serial_pc.printf("\n");
00058     
00059 }
00060 
00061 std::string trim(const std::string& string, const char* trimCharacterList = " \t\v\r\n")
00062 {
00063 std::string result;
00064  
00065 std::string::size_type left = string.find_first_not_of(trimCharacterList);
00066  
00067 if (left != std::string::npos)
00068 {
00069 // 左側からトリムする文字以外が見つかった場合は、同じように右側からも検索します。
00070 std::string::size_type right = string.find_last_not_of(trimCharacterList);
00071  
00072 // 戻り値を決定します。ここでは右側から検索しても、トリムする文字以外が必ず存在するので判定不要です。
00073 result = string.substr(left, right - left + 1);
00074 }
00075  
00076 return result;
00077 }
00078 
00079 boolean ParseMessage(string inRequestText)
00080 {
00081   int lpar = inRequestText.find('(');
00082   if (lpar == -1) {
00083     return false;
00084   }
00085   
00086   function = string(inRequestText.substr(0, lpar));
00087   trim(function);
00088               
00089   int offset = lpar + 1;
00090   int comma = inRequestText.find(',', offset);
00091   numOfArguments = 0;
00092   while (comma != -1) {
00093     string tmp = inRequestText.substr(offset, comma - offset);
00094     trim(tmp);
00095     arguments[numOfArguments++] = stringToWord(tmp);
00096     offset = comma + 1;
00097     comma = inRequestText.find(',', offset);
00098   }
00099 
00100   int rpar = inRequestText.find(')', offset);
00101   while (rpar == -1) {
00102     return false;
00103   }
00104   
00105   if (rpar > offset) {
00106     string tmp = inRequestText.substr(offset, rpar - offset);
00107     trim(tmp);
00108     arguments[numOfArguments++] = stringToWord(tmp);
00109   }
00110   
00111   return true;
00112 }
00113 
00114 word stringToWord(string s)
00115 {
00116   word result = 0;
00117   
00118   for (int i = 0; i < s.length(); i++) {
00119     result = 10 * result + (s.at(i) - '0');
00120   }
00121   
00122   return result;
00123 }
00124 
00125 // Serial receiver (IRQ)
00126 void isrRx() {
00127     char ch;
00128     ch = serial_pc.getc();         // 1文字受信バッファより取り出し
00129     
00130     if(ch != '\n')
00131     {
00132         gReceivedMessage = gReceivedMessage + ch;
00133         gCounter++;
00134     }
00135     else
00136     {
00137         /* Decode the commands from PC */
00138         decodeSerialMessage(gReceivedMessage);
00139         
00140         gCounter = 0;
00141         gReceivedMessage = "";
00142     }
00143     
00144 }
00145 
00146 boolean dispatch() {
00147   //boolean aResult;
00148 
00149   if (function.compare("setLocoDirection") == 0) {
00150     return ds_gw.SetLocoDirection(arguments[0], (unsigned char)arguments[1]);
00151     
00152   } else if (function.compare("setLocoFunction") == 0) {
00153     return ds_gw.SetLocoFunction(arguments[0], arguments[1], (byte)arguments[2]);
00154     
00155   } else if (function.compare("setTurnout") == 0) {
00156     return ds_gw.SetTurnout(arguments[0], (byte)arguments[1]);
00157     
00158   } else if (function.compare("setPower") == 0) {
00159     return ds_gw.SetPower((byte)arguments[0]);
00160   /*  
00161   } else if (function.compare("setLocoSpeed") == 0) {
00162     return ds_gw.SetLocoSpeed(arguments[0], arguments[1]);
00163   }
00164   */
00165   // Change Code <<Speed Step 128>>  2015/02/23
00166   } else if (function.compare("setLocoSpeed") == 0) {
00167     if( numOfArguments > 2)
00168     {
00169         return ds_gw.SetLocoSpeedEx(arguments[0],arguments[1],arguments[2]);
00170     }
00171     else 
00172     {
00173         return ds_gw.SetLocoSpeed(arguments[0], arguments[1]);
00174     }
00175   }
00176   
00177   else if (function.compare("getS88") == 0)
00178   {
00179     int aMaxS88Num = MAX_S88DECODER;
00180     
00181     if( arguments[0] > 0)
00182     {
00183         aMaxS88Num = arguments[0];
00184     }
00185 
00186     reporter.refresh(aMaxS88Num);
00187 
00188     //Send a S88 sensor reply 
00189     serial_pc.printf("@S88,");
00190 
00191     word aFlags = 0;
00192 
00193     for( int j = 0; j < aMaxS88Num; j++)
00194     {
00195       aFlags = (reporter.getByte((j << 1) + 1) << 8) + reporter.getByte(j << 1);
00196       //aFlags = 0;
00197       
00198       serial_pc.printf("%x", aFlags);
00199       serial_pc.printf(",");
00200     }
00201         
00202     serial_pc.printf("\n");
00203     
00204     return true;
00205     
00206   } /* getS88 */
00207   else if (function.compare("reset") == 0)
00208   {
00209   
00210     serial_pc.printf("100 Ready\n");
00211     
00212     return true;
00213   } /* reset */
00214   else if (function.compare("setPing") == 0) {
00215     serial_pc.printf("@DSG,001,\n");
00216     return true;
00217     
00218   } else if (function == "getLocoConfig") {
00219   
00220     /*aResult = ctrl.readConfig(arguments[0], arguments[1], &aValue);*/
00221     serial_pc.printf("@CV,");
00222     serial_pc.printf("%d", arguments[0]);
00223     serial_pc.printf(",");
00224     serial_pc.printf("%d", arguments[1]);
00225     serial_pc.printf(",");
00226     serial_pc.printf("%d", 0x00);
00227     serial_pc.printf(",\n");
00228     
00229     return true;
00230   } else if (function == "setLocoConfig") {
00231     return ds_gw.WriteConfig(arguments[0], arguments[1], arguments[2]);
00232     
00233   }
00234   
00235   else
00236   {
00237     return false;
00238   }
00239   
00240 }
00241  
00242 int main() {
00243     
00244     //initialization
00245     serial_pc.baud(115200);
00246     //serial_pc.format(8, 0, 1);
00247     serial_pc.attach(isrRx, Serial::RxIrq);
00248     
00249       serial_pc.printf("--------------------------------------\n");
00250       serial_pc.printf("Desktop Station Gateway               \n");
00251       serial_pc.printf("--------------------------------------\n");
00252       serial_pc.printf("100 Ready\n");
00253       
00254       ds_gw.begin();
00255     
00256     while(1) {
00257         wait_ms(50);
00258         
00259             }
00260 }
00261