DSGateway sketch program for Nucleo. This program is using DSGatewayLibMBED
Dependencies: DSgatewayMBED mbed TrackReporterS88_DS
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 else if (function.compare("getS88") == 0) 00165 { 00166 int aMaxS88Num = MAX_S88DECODER; 00167 00168 if( arguments[0] > 0) 00169 { 00170 aMaxS88Num = arguments[0]; 00171 } 00172 00173 reporter.refresh(aMaxS88Num); 00174 00175 //Send a S88 sensor reply 00176 serial_pc.printf("@S88,"); 00177 00178 word aFlags = 0; 00179 00180 for( int j = 0; j < aMaxS88Num; j++) 00181 { 00182 aFlags = (reporter.getByte((j << 1) + 1) << 8) + reporter.getByte(j << 1); 00183 //aFlags = 0; 00184 00185 serial_pc.printf("%x", aFlags); 00186 serial_pc.printf(","); 00187 } 00188 00189 serial_pc.printf("\n"); 00190 00191 return true; 00192 00193 } /* getS88 */ 00194 else if (function.compare("reset") == 0) 00195 { 00196 00197 serial_pc.printf("100 Ready\n"); 00198 00199 return true; 00200 } /* reset */ 00201 else if (function.compare("setPing") == 0) { 00202 serial_pc.printf("@DSG,001,\n"); 00203 return true; 00204 00205 } else if (function == "getLocoConfig") { 00206 00207 /*aResult = ctrl.readConfig(arguments[0], arguments[1], &aValue);*/ 00208 serial_pc.printf("@CV,"); 00209 serial_pc.printf("%d", arguments[0]); 00210 serial_pc.printf(","); 00211 serial_pc.printf("%d", arguments[1]); 00212 serial_pc.printf(","); 00213 serial_pc.printf("%d", 0x00); 00214 serial_pc.printf(",\n"); 00215 00216 return true; 00217 } else if (function == "setLocoConfig") { 00218 return ds_gw.WriteConfig(arguments[0], arguments[1], arguments[2]); 00219 00220 } 00221 00222 else 00223 { 00224 return false; 00225 } 00226 00227 } 00228 00229 int main() { 00230 00231 //initialization 00232 serial_pc.baud(115200); 00233 //serial_pc.format(8, 0, 1); 00234 serial_pc.attach(isrRx, Serial::RxIrq); 00235 00236 serial_pc.printf("--------------------------------------\n"); 00237 serial_pc.printf("Desktop Station Gateway \n"); 00238 serial_pc.printf("--------------------------------------\n"); 00239 serial_pc.printf("100 Ready\n"); 00240 00241 ds_gw.begin(); 00242 00243 while(1) { 00244 wait_ms(50); 00245 00246 } 00247 } 00248
Generated on Wed Jul 13 2022 14:46:03 by 1.7.2