From e699a77ea8b93d8ecba14e25749d7fc964baf134 Mon Sep 17 00:00:00 2001
From: Federico Terraneo <fede.tft@miosix.org>
Date: Tue, 4 Jul 2017 22:31:45 +0200
Subject: [PATCH] Piksi driver cleanup, added copyright notices

---
 src/entrypoints/test-piksi.cpp              | 71 +++++++++++++++++++--
 src/shared/drivers/piksi/contiguous_queue.h | 26 ++++++++
 src/shared/drivers/piksi/piksi.cpp          | 38 ++++++++---
 src/shared/drivers/piksi/piksi.h            | 22 +++++++
 4 files changed, 143 insertions(+), 14 deletions(-)

diff --git a/src/entrypoints/test-piksi.cpp b/src/entrypoints/test-piksi.cpp
index 7e6f2e0bf..f4990508f 100644
--- a/src/entrypoints/test-piksi.cpp
+++ b/src/entrypoints/test-piksi.cpp
@@ -1,3 +1,25 @@
+/* Copyright (c) 2017 Skyward Experimental Rocketry
+ * Authors: Federico Terraneo
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
 #include <drivers/piksi/piksi.h>
 #include <time.h>
 #include <iostream>
@@ -7,17 +29,26 @@ using namespace std;
 #ifdef _MIOSIX
 
 #include <miosix.h>
-
 using namespace miosix;
 
 #endif //_MIOSIX
 
-int main(int argc, char *argv[])
+// Polling test, usng getGpsData()
+
+int main()
 {
+    #ifdef _MIOSIX
+    
     Piksi piksi("/dev/gps");
+    #else //_MIOSIX
+    Piksi piksi("/dev/ttyUSB0");
+    #endif //_MIOSIX
+    
     for(;;)
     {
-        auto gps=piksi.waitForGpsData();
+        Thread::sleep(200);
+        try {
+        auto gps=piksi.getGpsData();
         #ifdef _MIOSIX
         long long now=getTick();
         #else //_MIOSIX
@@ -31,5 +62,37 @@ int main(int argc, char *argv[])
             <<" ve: "<<gps.velocityEast
             <<" vd: "<<gps.velocityDown
             <<" ns: "<<gps.numSatellites<<endl;
+        } catch(...) {
+            cout<<"---"<<endl;
+        }
     }
-}
\ No newline at end of file
+}
+
+// Blocking wait test, using waitForGpsData()
+
+// int main()
+// {
+//     #ifdef _MIOSIX
+//     Piksi piksi("/dev/gps");
+//     #else //_MIOSIX
+//     Piksi piksi("/dev/ttyUSB0");
+//     #endif //_MIOSIX
+// 
+//     for(;;)
+//     {
+//         auto gps=piksi.waitForGpsData();
+//         #ifdef _MIOSIX
+//         long long now=getTick();
+//         #else //_MIOSIX
+//         long long now=clock()/(CLOCKS_PER_SEC/1000);
+//         #endif //_MIOSIX
+//         cout<<" t: "<<now-gps.timestamp
+//             <<" lat: "<<gps.latitude
+//             <<" lon: "<<gps.longitude
+//             <<" h: "<<gps.height
+//             <<" vn: "<<gps.velocityNorth
+//             <<" ve: "<<gps.velocityEast
+//             <<" vd: "<<gps.velocityDown
+//             <<" ns: "<<gps.numSatellites<<endl;
+//     }
+// }
diff --git a/src/shared/drivers/piksi/contiguous_queue.h b/src/shared/drivers/piksi/contiguous_queue.h
index 51c7ef1a9..f723a5ec9 100644
--- a/src/shared/drivers/piksi/contiguous_queue.h
+++ b/src/shared/drivers/piksi/contiguous_queue.h
@@ -1,3 +1,29 @@
+/***************************************************************************
+ *   Copyright (C) 2017 by Terraneo Federico                               *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   As a special exception, if other files instantiate templates or use   *
+ *   macros or inline functions from this file, or you compile this file   *
+ *   and link it with other works to produce a work based on this file,    *
+ *   this file does not by itself cause the resulting work to be covered   *
+ *   by the GNU General Public License. However the source code for this   *
+ *   file must still be made available in accordance with the GNU General  *
+ *   Public License. This exception does not invalidate any other reasons  *
+ *   why a work based on this file might be covered by the GNU General     *
+ *   Public License.                                                       *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, see <http://www.gnu.org/licenses/>   *
+ ***************************************************************************/
 
 #ifndef CONTIGUOUS_QUEUE
 #define CONTIGUOUS_QUEUE
diff --git a/src/shared/drivers/piksi/piksi.cpp b/src/shared/drivers/piksi/piksi.cpp
index 680a6e184..4c406ea65 100644
--- a/src/shared/drivers/piksi/piksi.cpp
+++ b/src/shared/drivers/piksi/piksi.cpp
@@ -1,3 +1,24 @@
+/* Copyright (c) 2017 Skyward Experimental Rocketry
+ * Authors: Federico Terraneo
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
 
 #include "piksi.h"
 #include <unistd.h>
@@ -88,11 +109,11 @@ GPSData Piksi::waitForGpsData()
 
 Piksi::~Piksi()
 {
-    close(fd);
-    pthread_mutex_destroy(&mutex);
-    pthread_cond_destroy(&cond);
     quit=true;
     pthread_join(thread,NULL);
+    pthread_mutex_destroy(&mutex);
+    pthread_cond_destroy(&cond);
+    close(fd);
 }
 
 void* Piksi::threadLauncher(void* arg)
@@ -111,13 +132,10 @@ void Piksi::run()
 
 unsigned int Piksi::readData(unsigned char *buffer, unsigned int size)
 {
-    for(;;)
-    {
-        int result=read(fd,buffer,size);
-        if(result>0) return result;
-        usleep(10000); //We want to retry but avoid 100% CPU utilization
-        return 0;      //To go to a loop of run() and notice the quit flag
-    }
+    int result=read(fd,buffer,size);
+    if(result>0) return result;
+    usleep(10000); //We want to retry but avoid 100% CPU utilization
+    return 0;      //To go to a loop of run() and notice the quit flag
 }
 
 unsigned int Piksi::lookForMessages(uint8_t *buffer, unsigned int size)
diff --git a/src/shared/drivers/piksi/piksi.h b/src/shared/drivers/piksi/piksi.h
index 51c7aae8f..4907c22c6 100644
--- a/src/shared/drivers/piksi/piksi.h
+++ b/src/shared/drivers/piksi/piksi.h
@@ -1,3 +1,24 @@
+/* Copyright (c) 2017 Skyward Experimental Rocketry
+ * Authors: Federico Terraneo
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
 
 #ifndef PIKSI_H
 #define PIKSI_H
@@ -38,6 +59,7 @@ public:
     /**
      * Constructor
      * \param serialPath path to the device file of the Piksi serial port
+     * \throws runtime_error if the serial port cannot be opened
      */
     Piksi(const char *serialPath);
     
-- 
GitLab