Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include <sstream> 00003 00004 #define MBED_PROJECT "USB3Bug" 00005 00006 UnbufferedSerial pc(USBTX, USBRX, 230400); 00007 00008 struct typeTRANSMISSION { string buffer; string serial; volatile bool SERIAL; } message = { "", "", false }; 00009 00010 void serial_event(void); 00011 void fn_processing(string); 00012 00013 int main() 00014 { 00015 message.buffer.reserve(1024); 00016 message.serial.reserve(1024); 00017 pc.attach(callback(&serial_event)); 00018 while(1) 00019 { 00020 if(message.SERIAL) 00021 { 00022 fn_processing(message.serial); 00023 message.SERIAL = false; 00024 } 00025 } 00026 } 00027 00028 void serial_event(void) 00029 { 00030 char c; 00031 pc.read(&c, 1); 00032 if(c == '\n') 00033 { 00034 message.buffer += ';'; 00035 message.serial = message.buffer; 00036 message.buffer.clear(); 00037 message.SERIAL = true; 00038 } 00039 else if((c < 128) && (message.buffer.size() < message.buffer.capacity()-2)) message.buffer += c; 00040 } 00041 00042 void fn_processing(string cmd) 00043 { 00044 stringstream srecv(cmd), ssend; 00045 ssend.precision(3); 00046 while(getline(srecv, cmd, ';')) 00047 { 00048 for (unsigned int i = 0; i < cmd.size(); i++) if ((cmd[i] >= 'a') && (cmd[i] <= 'z')) cmd[i] += 'A' - 'a'; 00049 if(ssend.str().size() > 0) ssend << ' '; 00050 if(cmd.empty()); 00051 else if(cmd == "*IDN?") 00052 { 00053 ssend << MBED_PROJECT << ", Mbed OS " << MBED_VERSION << ", Version dated, " << __DATE__ << ", " << __TIME__; 00054 } 00055 else if(cmd == "M?") 00056 { 00057 ssend << "Motor[" << 0 << "][" << "STOP" << "]"; 00058 } 00059 else if(cmd == "TRIG?") 00060 { 00061 ssend << "Trigger(" << 0 << ")(" << NAN << "V)"; 00062 } 00063 else if(cmd == "TARG?") 00064 { 00065 ssend << "Target[" << 0 << "](" << 4294967295 << ")[" << 0 << "]{" << 0 << "}"; 00066 } 00067 else if(cmd == "POS?") 00068 { 00069 ssend << "Position[" << 0 << "][" << "UNKNOWN" << "](" << 4294967295 << ")"; 00070 } 00071 else if(cmd.find("?")!=string::npos) ssend << "incorrect requeste [" << cmd << "]"; 00072 } 00073 if(!ssend.str().empty()) ssend << "\n"; 00074 pc.write(ssend.str().c_str(), ssend.str().length()); 00075 }
Generated on Mon Jul 18 2022 19:50:49 by
1.7.2