SmartWheels self-driving race car. Designed for NXP Cup. Uses FRDM-KL25Z, area-scan camera, and simple image processing to detect and navigate any NXP spec track.

Dependencies:   TSI USBDevice mbed-dev

Fork of SmartWheels by haofan Zheng

Revision:
18:bf6c5f8281eb
Parent:
17:84acc292b4c2
Child:
19:c93f7fab165d
--- a/PCConnector/SWUSBServer.cpp	Fri Feb 10 16:17:57 2017 +0000
+++ b/PCConnector/SWUSBServer.cpp	Sat Feb 11 05:05:44 2017 +0000
@@ -5,17 +5,25 @@
 
 #include "USBSerial.h"
 
+namespace
+{
+    char recvBuf[100];
+    size_t pos = 0;
+    bool hasMsg = false;
+    bool isReading = false;
+}
+
 namespace SW{
 
 USBServer::USBServer(uint16_t vendor_id, uint16_t product_id) : 
+    Serial(USBTX, USBRX),
     //m_hid(HID_REPORT_LENGTH, HID_REPORT_LENGTH, vendor_id, product_id),
     m_shouldTerminate(false),
     m_stat(SER_STAT_STOPPED),
     m_usbThread(NULL),
-    m_usb(NULL),
     m_tickMsgTimer(0.0f)
 {
-    
+    this->attach(callback(this, &USBServer::RxCallback), RxIrq);
 }
 
 USBServer::~USBServer()
@@ -28,12 +36,12 @@
     m_shouldTerminate = true;
     m_usbThread->signal_set(THREAD_SIGNAL_QUEUE);
     m_usbThread->signal_set(0xffff);
-    //m_usbThread->join();
     m_usbThread->terminate();
     delete m_usbThread;
-    delete m_usb;
 }
 
+
+
 void USBServer::Update(float deltaTime)
 {
     m_tickMsgTimer += deltaTime;
@@ -41,6 +49,7 @@
     {
         if(m_usbThread)
         {
+            m_usbThread->terminate();
             delete m_usbThread;
         }
         m_usbThread = new Thread(callback(this, &USBServer::ConnectingThread));
@@ -52,19 +61,12 @@
         PushReliableMsg('D', "Terminal Connected!");
         if(m_usbThread)
         {
+            m_usbThread->terminate();
             delete m_usbThread;
         }
         m_usbThread = new Thread(callback(this, &USBServer::RunningThread));
         //m_hidThread.start(callback(this, &USBServer::HIDThread));
     }
-    
-    //char buf[15];
-    if(m_tickMsgTimer >= 2.0f)
-    {
-        //sprintf(buf, "%s,%f", TICKING_MSG, m_tickMsgTimer);
-        PushReliableMsg('T', TICKING_MSG);
-        m_tickMsgTimer = 0.0f;
-    }
 }
 
 bool USBServer::PushReliableMsg(const char type, const std::string & msg)
@@ -126,44 +128,71 @@
             m_qlocker.unlock();
         }
         
-        m_usb->printf("%s\n", msg.c_str());
+        this->printf("\x02%s\x03", msg.c_str());
     }
     
 }
+
+
+void USBServer::RxCallback()
+{
+    
+    if(!isReading && !hasMsg)
+    {
+        char ch = this->_getc();
+        if(ch == '\x02')
+        {
+            isReading = true;
+        }
+    }
+    
+    while(this->readable())
+    {
+        char ch = this->_getc();
+        if(ch == '\x03')
+        {
+            isReading = false;
+            hasMsg = true;
+        }
+        else
+        {
+            recvBuf[pos++] = ch;
+        }
+    }
+}
     
 void USBServer::ConnectingThread()
 {
     m_stat = SER_STAT_CONNECTING;
     
-    m_usb = new Serial(USBTX, USBRX, "SmartWheels");
-    
     while(m_stat != SER_STAT_CONNECTED)
     {
         
-        while(!m_usb->readable())
+        while(!hasMsg)
         {
-            m_usb->printf("\x02%s\x03", HANDSHAKE_MSG_TER);
+            this->printf("\x02%s\x03", HANDSHAKE_MSG_TER);
             wait(1.0);
         }
         
-        char chBuf = m_usb->getc();
-        if(chBuf == '\x02')
+        std::string tempStr;
+        if(hasMsg)
         {
-            std::string strBuf;
-            while(chBuf != '\x03')
-            {
-                chBuf = m_usb->getc();
-                if(chBuf != '\x03')
-                    strBuf.push_back(chBuf);
-            }
-            
-            if(strBuf.compare(HANDSHAKE_MSG_PC) == 0)
-            {
-                m_stat = SER_STAT_CONNECTED;
-            }
+            this->attach(NULL, RxIrq);
+            recvBuf[pos] = '\0';
+            tempStr = recvBuf;
+            pos = 0;
+            hasMsg = false;
+            this->attach(callback(this, &USBServer::RxCallback), RxIrq);
         }
+        
+        if(tempStr.compare(HANDSHAKE_MSG_PC) == 0)
+        {
+            m_stat = SER_STAT_CONNECTED;
+        }
+        
     }
     
+    
 }