This my big project for PYRN Board

Dependencies:   CAN HTTPClient MODSERIAL MyThings Pyrn3GModem Socket TinyGPS MyUSBHost lwip-sys lwip mbed-rtos mbed-src

Files at this revision

API Documentation at this revision

Comitter:
clemounet
Date:
Tue Apr 14 13:30:02 2015 +0000
Commit message:
All the projet PYRN 3G-CAN-ACC...

Changed in this revision

3G/WanModem.cpp Show annotated file Show diff for this revision Revisions of this file
3G/WanModem.h Show annotated file Show diff for this revision Revisions of this file
CAN.lib Show annotated file Show diff for this revision Revisions of this file
GPSSensor.cpp Show annotated file Show diff for this revision Revisions of this file
GPSSensor.h Show annotated file Show diff for this revision Revisions of this file
HTTPClient.lib Show annotated file Show diff for this revision Revisions of this file
HuaweiE372DongleInitializer.cpp Show annotated file Show diff for this revision Revisions of this file
HuaweiE372DongleInitializer.h Show annotated file Show diff for this revision Revisions of this file
IMUSensor.cpp Show annotated file Show diff for this revision Revisions of this file
IMUSensor.h Show annotated file Show diff for this revision Revisions of this file
LSM303DLH.cpp Show annotated file Show diff for this revision Revisions of this file
LSM303DLH.h Show annotated file Show diff for this revision Revisions of this file
MODSERIAL.lib Show annotated file Show diff for this revision Revisions of this file
MainThread.cpp Show annotated file Show diff for this revision Revisions of this file
MyThings.lib Show annotated file Show diff for this revision Revisions of this file
Pyrn3GModem.lib Show annotated file Show diff for this revision Revisions of this file
Socket.lib Show annotated file Show diff for this revision Revisions of this file
TinyGPS.lib Show annotated file Show diff for this revision Revisions of this file
USBHost.lib Show annotated file Show diff for this revision Revisions of this file
lwip-sys.lib Show annotated file Show diff for this revision Revisions of this file
lwip.lib Show annotated file Show diff for this revision Revisions of this file
lwipopts_conf.h Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed-src.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/3G/WanModem.cpp	Tue Apr 14 13:30:02 2015 +0000
@@ -0,0 +1,27 @@
+
+#include "WanModem.h"
+
+#define __DEBUG__ 5
+#ifndef __MODULE__
+#define __MODULE__ "WANModem.cpp"
+#endif
+#include "MyDebug.h"
+
+WanModem::WanModem(): initializer(USBHost::getHostInst()) {
+    DBG("Instanciate WanDongle");
+    dongle.addInitializer(&initializer);
+}
+
+void WanModem::Init(void) {
+    if(!dongle.tryConnect()) {
+        DBG("Wait 10s to modem restart properly");
+        Thread::wait(10000);
+        if(!dongle.tryConnect()) {
+            DBG("After retry the dongle Could not be connected");
+        } else {
+            DBG("After retry the dongle got connected"); 
+        }
+    }  else {
+        DBG("Dongle got connected"); 
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/3G/WanModem.h	Tue Apr 14 13:30:02 2015 +0000
@@ -0,0 +1,19 @@
+
+#ifndef WAN_MODEM_H
+#define WAN_MODEM_H
+
+#include "WANDongle.h"
+#include "MyThread.h"
+#include "HuaweiE372DongleInitializer.h"
+#include "USBSerialStream.h"
+#include "ATCommandsInterface.h"
+
+class WanModem {
+    HuaweiE372DongleInitializer initializer;
+    WANDongle dongle;
+public:
+    WanModem();
+    virtual void Init(void);
+};
+
+#endif // WAN_MODEM_H
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CAN.lib	Tue Apr 14 13:30:02 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/clemounet/code/CAN/#b9201bec01bf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GPSSensor.cpp	Tue Apr 14 13:30:02 2015 +0000
@@ -0,0 +1,237 @@
+
+
+#include "GPSSensor.h"
+
+#define __DEBUG__ 2
+#ifndef __MODULE__
+#define __MODULE__ "GPSSensor.cpp"
+#endif
+#include "MyDebug.h"
+
+#define GPS_THREAD_STACK_SIZE   768
+
+bool gpsNewlineDetected;
+
+void rxFullCallback(MODSERIAL_IRQ_INFO *q){
+    DBG("OVF");
+    MODSERIAL *serial = q->serial;
+    serial->rxBufferFlush();
+}
+
+void rxCallback(MODSERIAL_IRQ_INFO *q) {
+    MODSERIAL *serial = q->serial;
+    if ( serial->rxGetLastChar() == '\n') {
+        gpsNewlineDetected = true;
+    }
+}
+
+// Well there is an issue in the RX Serial data, sometime multiples frames collides between themselves... 
+// To debug when we gonna have GSP Signal 
+
+GPSSensor::GPSSensor(PinName tx, PinName rx, uint32_t trackingTime, uint32_t idle):
+    MySensor(SENSOR_NAME_GPS, SENSOR_TYPE_GPS, idle, GPS_THREAD_STACK_SIZE),
+    gps(tx,rx) {
+    fixed = false;
+    trackTime = trackingTime;
+    lastImpact = 0;
+    gpsNewlineDetected = false;
+    gps.baud(9600);
+    gps.rxBufferFlush();
+    gps.txBufferFlush();
+    WARN("Increase the baudrate to 57600");
+    gps.printf("$PUBX,41,1,0007,0003,57600,0*2B\r\n");
+    Thread::wait(1000);
+    gps.baud(57600);
+    WARN("Disabling all unused NMEA sentences");
+    gps.printf("$PUBX,40,GLL,0,0,0,0,0,0*5C\r\n");
+    gps.printf("$PUBX,40,ZDA,0,0,0,0,0,0*44\r\n");
+    gps.printf("$PUBX,40,VTG,0,0,0,0,0,0*5E\r\n");
+    gps.printf("$PUBX,40,GSV,0,0,0,0,0,0*59\r\n");
+    gps.printf("$PUBX,40,GSA,0,0,0,0,0,0*4E\r\n");
+    //gps.printf("$PUBX,40,RMC,0,0,0,0,0,0*47\r\n");
+    Thread::wait(500);
+    gps.rxBufferFlush();
+    gps.txBufferFlush();
+    WARN("Setup Callbacks");
+    gps.attach(&rxCallback, MODSERIAL::RxIrq);
+    gps.attach(&rxFullCallback, MODSERIAL::RxOvIrq);
+    InitResults(SENSOR_RESSZ_DEFAULT);
+    WARN("Finish Init");
+}
+
+// Could we have a way to put the InitResultStatic/StoreLastImpact into the base class?
+
+void GPSSensor::InitResultsStatic(void) {
+    DBG("InitResultStatic have been defined");
+    results.start = (uint8_t*)store;
+    results.current = (uint8_t*)store;
+    results.max = sizeof(store); //IMU_STORE_SIZE*sizeof(uint16_t);
+}
+
+void GPSSensor::Loop(void) {
+    // We read all the available data as fast as possible 
+    // but giving opportunity to be preampted (wait 100ms)
+    // Doing this reading should avoid serial internal buffer to ovf
+     Sample();
+     StoreLastImpact();
+     Thread::wait(idleTime);
+}
+
+void GPSSensor::StoreLastImpact(void) {
+    if(fixed && NeedImpact()) {
+        if((results.max - results.num)>=sizeof(gpsImpact)){
+            DBG("===========> Storing New impact <===========");
+            if(resultsMutex.trylock()) {
+                memcpy((char*)results.current,(const char*)&impact,sizeof(gpsImpact));
+                results.current += sizeof(gpsImpact);
+                results.num += sizeof(gpsImpact);
+                DBG("%p %d %d",results.current,results.num,sizeof(gpsImpact));
+                resultsMutex.unlock();
+            }
+        } else
+            ERR("No Space to Store results... Data will be overwrited at next Loop");
+    } else {
+        DBG("Nope, (%s)", (!fixed)?"not fixed":"no impact needed");
+    }
+}
+
+bool GPSSensor::NeedImpact(void) {
+    time_t now = time(NULL);
+    DBG("%ld-%ld = %d > %d ",now,lastImpact,now-lastImpact,trackTime);
+    if((now-lastImpact)>trackTime){
+        DBG("NEED IMPACT");
+        lastImpact = now;
+        return true;
+    }
+    return false;
+}
+
+int GPSSensor::GetLine(void) {
+    int len = 0;
+    // Consume the chars until we found a '$'
+    while (gps.getc() != '$');
+    recvBuff[0] = '$';
+    // Fill the GPS_RECV_BUFF 
+    for (int i=1; i<(GPS_RECV_BUFF-2); i++) {
+        recvBuff[i] = gps.getc();
+        len++;
+        if (recvBuff[i] == '\n') {
+            recvBuff[i+1] = '\0';
+            return len;
+        }        
+    }
+    error("Overflow in getline");
+    return GPS_RECV_BUFF-2;
+}
+
+void GPSSensor::Sample(void) {
+    int len = 0;
+    bool newData = false;
+    if(gpsNewlineDetected) {
+        while(1) {
+            int nChars = gps.rxBufferGetCount();
+            if(nChars == 0) 
+                break;
+            //else
+            //    DBG("To read --> %d",nChars);
+            // We could consider passing the data directly to parser whithout buffering it
+            // but: That's annoying cause if so we could not printf them smoothly on console
+            len = GetLine();
+            
+            //DBG("GotLine(%d) %s",len,recvBuff);
+            // DBG_MEMDUMP("GPSData",(const char*)recvBuff,len);
+            
+            for(unsigned int i = 0; i < len; i++) {
+                // Insert all data to gpsParser
+                if(gpsParser.encode(recvBuff[i])) {
+                    newData = true;
+                }
+            }
+            
+            // Only take a sample when all the data for each epoch have been received
+            if(newData && gpsParser.rmc_ready() && gpsParser.gga_ready()){
+                long lat = 0, lon = 0, hdop = 0;
+                unsigned long date = 0, time = 0;
+                gpsParser.get_position(&lat,&lon);
+                gpsParser.get_datetime(&date,&time);
+                hdop = gpsParser.hdop();
+                if((lat != TinyGPS::GPS_INVALID_ANGLE) && (lon != TinyGPS::GPS_INVALID_ANGLE)){
+                    fixed = true;
+                    impact.date = date;
+                    impact.time = time/100;
+                    impact.lat = lat;
+                    impact.lon = lon;
+                    if(hdop>0xffff) {
+                        impact.hdop = -1;
+                    } else {
+                        impact.hdop = hdop;
+                    }
+                    WARN("######[%ld-%ld][%09ld|%09ld|%04d]######",impact.date,impact.time,impact.lat,impact.lon,impact.hdop);
+                } else {
+                    fixed = false;
+                    DBG("All data have been received but lat/lon does not seems to be valid",date,time,lat,lon,hdop);
+                }
+                gpsParser.reset_ready();
+            } else {
+                fixed = false;
+                DBG("All frames did not came yet RMC(%c)GGA(%c)newData(%c)",gpsParser.rmc_ready()?'x':'o',gpsParser.gga_ready()?'x':'o',newData?'x':'o');
+            }
+        }
+    }
+}
+
+// OLD SAMPLING 
+/*
+void GPSSensor::Sample(void) {
+    int lineParsed = 0;
+    DBG("Read GPS");
+    if(gpsNewlineDetected) {
+        gpsNewlineDetected = false;
+        while(1) {
+            int nChars = gps.rxBufferGetCount();
+            if(nChars == 0) {
+                DBG("No data to read anymore on gps line");
+                break;
+            } else {
+                DBG("--> %d",nChars);
+            }
+                
+            DBG("Read Line GPS");
+            GetLine();
+            DBG("GotLine, %s",recvBuff);
+            DBG_MEMDUMP("GPSData",(const char*)recvBuff,strlen((const char*)recvBuff));
+            
+            // Stuffs used in GPGGA
+            
+            // The only NMEA sentence we gonna have (all other are disabled)
+            int nArgs = sscanf((const char*)recvBuff, "GPGGA,%f,%f,%c,%f,%c,%d,%d,%f,%f,%c",&hour,&lat,&ns,&lon,&ew,&q,&su,&hdop,&alt,&altU);
+            if(nArgs == 10) {
+                DBG("We got all the arguments");
+                DBG("%d [%f/%f/%c/%f/%c/%d/%d/%f/%f/%c]\n",nArgs,hour,lat,ns,lon,ew,q,su,hdop,alt,altU);
+                lineParsed = GGA;
+            }
+            
+            // if lineParsed is different from zero meens that's we got new data
+            if(lineParsed) {
+                DBG("Impact Result %d", su);
+                // When 0 satelites usualy this we can't lock
+                if(su == 0)
+                    q = 0;
+                
+                if(q!=0){
+                    DBG("Locked (q = %d)",q);
+                    fixed = true;
+                    impact.utc = (uint32_t) hour;
+                    impact.lat = DegToDec(lat,ns);
+                    impact.lon = DegToDec(lon,ew);
+                    impact.alt = alt;
+                    impact.su = su;
+                    impact.hdop = hdop;
+                    DBG("Impact Results %d, %f, %f, %f, %d, %d", impact.utc, impact.lat, impact.lon, impact.alt, impact.su, impact.hdop);
+                }
+            }
+        }
+    }
+}
+*/
+ 
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GPSSensor.h	Tue Apr 14 13:30:02 2015 +0000
@@ -0,0 +1,45 @@
+#ifndef GPS_SENSOR_H
+#define GPS_SENSOR_H
+
+#include "mbed.h"
+#include "MySensor.h"
+#include "MODSERIAL.h"
+#include "TinyGPS.h"
+
+#define GPS_STORE_SIZE      256
+#define GPS_RECV_BUFF       512
+
+class GPSSensor: public MySensor {
+protected:
+    TinyGPS gpsParser;
+    enum frameType{
+        GGA = 1,
+        RMC = 2
+    };
+    typedef struct _gpsImpact{
+        uint32_t date;
+        uint32_t time;
+        int32_t lon;
+        int32_t lat;
+        int32_t alt;
+        uint16_t hdop;
+    }  __attribute__((packed)) gpsImpact;
+    uint8_t recvBuff[GPS_RECV_BUFF];
+    uint8_t store[GPS_STORE_SIZE];
+    gpsImpact impact;
+    MODSERIAL gps;
+    //Serial gps;
+    uint32_t trackTime;
+    uint32_t lastImpact;
+    bool fixed;
+public:
+    GPSSensor(PinName tx, PinName rx, uint32_t trackingTime = 10, uint32_t idle = 250);
+    virtual void InitResultsStatic(void);
+    virtual void Loop(void);
+    virtual void StoreLastImpact(void);
+    virtual bool NeedImpact(void);
+    virtual int GetLine(void);
+    virtual void Sample(void);
+};
+
+#endif //GPS_SENSOR_H
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPClient.lib	Tue Apr 14 13:30:02 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/clemounet/code/HTTPClient/#50c849f8d2c1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HuaweiE372DongleInitializer.cpp	Tue Apr 14 13:30:02 2015 +0000
@@ -0,0 +1,88 @@
+
+#include "HuaweiE372DongleInitializer.h"
+
+#define __DEBUG__ 5
+#ifndef __MODULE__
+#define __MODULE__ "MyThread.cpp"
+#endif
+#include "MyDebug.h"
+
+#define VENDSPEC_CLASS  0xFF
+
+//Huawei E372 (Vodafone)
+
+// switch string => "55 53 42 43 00 00 00 00 00 00 00 00 00 00 00 11 06 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
+static uint8_t huawei_E372_switch_packet[] = {
+    0x55, 0x53, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
+    0x11, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    0x00
+};
+
+HuaweiE372DongleInitializer::HuaweiE372DongleInitializer(USBHost *h): WANDongleInitializer(h) {
+        
+}
+
+bool HuaweiE372DongleInitializer::switchMode(USBDeviceConnected* pDev) {
+    for (int i = 0; i < pDev->getNbIntf(); i++) {
+        if (pDev->getInterface(i)->intf_class == MSD_CLASS) {
+            USBEndpoint* pEp = pDev->getEndpoint(i, BULK_ENDPOINT, OUT);
+            if ( pEp != NULL )  {
+                USB_DBG("MSD descriptor found on device %p, intf %d, will now try to switch into serial mode", (void *)pDev, i);
+                m_pHost->bulkWrite(pDev, pEp, huawei_E372_switch_packet, 31);
+                return true;
+            }
+        }  
+    }
+    return false;
+}
+
+USBEndpoint* HuaweiE372DongleInitializer::getEp(USBDeviceConnected* pDev, int serialPortNumber, bool tx) {
+    return pDev->getEndpoint(serialPortNumber, BULK_ENDPOINT, tx ? OUT : IN, 0);
+}
+
+void HuaweiE372DongleInitializer::setVidPid(uint16_t vid, uint16_t pid) {
+    if( (vid == getSerialVid()) && (pid == getSerialPid()) ) {
+      m_hasSwitched = true;
+      m_currentSerialIntf = 0;
+      m_endpointsToFetch = 4*2;
+    } else {
+      m_hasSwitched = false;
+      m_endpointsToFetch = 1;
+    }
+}
+ 
+bool HuaweiE372DongleInitializer::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) {
+    if( m_hasSwitched ) {
+        USB_DBG("Interface #%d; Class:%02x; SubClass:%02x; Protocol:%02x", intf_nb, intf_class, intf_subclass, intf_protocol);
+        if( intf_class == VENDSPEC_CLASS ) {
+            // The first 4 Interfaces are parsable.
+            if( m_currentSerialIntf <4 ) {
+                m_currentSerialIntf++;
+                return true;
+            }
+            m_currentSerialIntf++;
+        }
+    } else {
+        // The first 2 Interface are parsable.
+        if( ((intf_nb == 0) || (intf_nb == 1)) && (intf_class == MSD_CLASS) ) {
+            return true;
+        }
+    }
+    return false;
+}
+
+bool HuaweiE372DongleInitializer::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) {//Must return true if the endpoint will be used
+    if( m_hasSwitched ) {
+        USB_DBG("USBEndpoint on Inteface #%d; Type:%d; Direction:%d", intf_nb, type, dir);
+        if( (type == BULK_ENDPOINT) && m_endpointsToFetch ) {
+            m_endpointsToFetch--;
+            return true;
+        }
+    } else {
+        if( (type == BULK_ENDPOINT) && (dir == OUT) && m_endpointsToFetch ) {
+            m_endpointsToFetch--;
+            return true;
+        }
+    }
+    return false;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HuaweiE372DongleInitializer.h	Tue Apr 14 13:30:02 2015 +0000
@@ -0,0 +1,31 @@
+
+#ifndef HUAWEI372_DONGLE_INITIALIZER_H
+#define HUAWEI372_DONGLE_INITIALIZER_H
+
+#include "WANDongleInitializer.h"
+
+#define WAN_DONGLE_TYPE_HUAWEI_E372  1
+
+// For the moment we have only one key
+class HuaweiE372DongleInitializer: public WANDongleInitializer {
+public:
+    HuaweiE372DongleInitializer(USBHost *h);
+    virtual uint16_t getMSDVid() { return 0x12D1; }
+    virtual uint16_t getMSDPid() { return 0x1505; }
+    virtual uint16_t getSerialVid() { return 0x12D1; }
+    virtual uint16_t getSerialPid() { return 0x14ac; }
+    virtual bool switchMode(USBDeviceConnected* pDev) ;
+    virtual USBEndpoint* getEp(USBDeviceConnected* pDev, int serialPortNumber, bool tx);
+    virtual int getSerialPortCount() { return 4; }
+    virtual void setVidPid(uint16_t vid, uint16_t pid) ;
+    virtual bool parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol); //Must return true if the interface should be parsed
+    virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) ; //Must return true if the endpoint will be used
+    virtual int getType() { return WAN_DONGLE_TYPE_HUAWEI_E372; }
+    virtual uint8_t getSerialIntf(int index) { return index; }
+private:
+    bool m_hasSwitched;
+    int m_currentSerialIntf;
+    int m_endpointsToFetch;
+};
+
+#endif  // HUAWEI372_DONGLE_INITIALIZER_H
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IMUSensor.cpp	Tue Apr 14 13:30:02 2015 +0000
@@ -0,0 +1,53 @@
+
+#define __DEBUG__ 0
+#ifndef __MODULE__
+#define __MODULE__ "IMUSensor.cpp"
+#endif
+#include "MyDebug.h"
+
+#include "IMUSensor.h"
+
+#define IMU_THREAD_STACK_SIZE   512
+
+IMUSensor::IMUSensor(PinName sda, PinName scl, uint32_t idle): 
+    MySensor(SENSOR_NAME_IMU, SENSOR_TYPE_IMU, idle, IMU_THREAD_STACK_SIZE), 
+    Accelerometer(sda,scl){
+    InitResults(SENSOR_RESSZ_DEFAULT);
+}
+
+void IMUSensor::InitResultsStatic(void) {
+    DBG("InitResultStatic have been defined");
+    results.start = (uint8_t*)store;
+    results.current = (uint8_t*)store;
+    results.max = sizeof(store); //IMU_STORE_SIZE*sizeof(uint16_t);
+}
+
+void IMUSensor::Loop(void) {
+    // Do the reading
+    float lax,lay,laz,lmx,lmy,lmz;              // Locals
+    Accelerometer.readAcc(&lax,&lay,&laz);
+    Accelerometer.readMag(&lmx,&lmy,&lmz);
+    //DBG("[%+.4f,%+.4f,%+.4f] - [%+.4f,%+.4f,%+.4f]", lax,lay,laz,lmx,lmy,lmz);
+    // Do the conversion to the impact struct
+    impact.ax = (int16_t) (lax * 1000);
+    impact.ay = (int16_t) (lay * 1000);
+    impact.az = (int16_t) (laz * 1000);
+    impact.mx = (int16_t) (lmx * 1000);
+    impact.my = (int16_t) (lmy * 1000);
+    impact.mz = (int16_t) (lmz * 1000);
+    // Print result if debug is activated
+    INFO("[%+05d,%+05d,%+05d] - [%+05d,%+05d,%+05d]", impact.ax, impact.ay, impact.az, impact.mx, impact.my, impact.mz);
+}
+
+void IMUSensor::StoreLastImpact(void) {
+    if((results.max - results.num)>=sizeof(imuImpact)){
+        if(resultsMutex.trylock()) {
+            memcpy((char*)results.current,(const char*)&impact,sizeof(imuImpact));
+            results.current += sizeof(imuImpact);
+            results.num += sizeof(imuImpact);
+            //DBG("%p %d %d",results.current,results.num,sizeof(imuImpact));
+            resultsMutex.unlock();
+        }
+    } else
+        WARN("No Space to Store results... Data will be overwrited at next Loop");
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IMUSensor.h	Tue Apr 14 13:30:02 2015 +0000
@@ -0,0 +1,30 @@
+#ifndef IMU_SENSOR_H
+#define IMU_SENSOR_H
+
+#include "mbed.h"
+#include "MySensor.h"
+#include "LSM303DLH.h"
+
+#define IMU_STORE_SIZE      128
+
+class IMUSensor: public MySensor {
+protected:
+    uint16_t store[IMU_STORE_SIZE];
+    typedef struct _imuImpact{
+        int16_t ax;
+        int16_t ay;
+        int16_t az;
+        int16_t mx;
+        int16_t my;
+        int16_t mz;
+    }  __attribute__((packed)) imuImpact;
+    imuImpact   impact;
+    LSM303DLH   Accelerometer;
+public:
+    IMUSensor(PinName sda, PinName scl, uint32_t idle = 1000);
+    virtual void InitResultsStatic(void);
+    virtual void Loop(void);
+    virtual void StoreLastImpact(void);
+};
+
+#endif // IMU_SENSOR_H
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LSM303DLH.cpp	Tue Apr 14 13:30:02 2015 +0000
@@ -0,0 +1,122 @@
+
+#define __DEBUG__ 0
+#ifndef __MODULE__
+#define __MODULE__ "LSM303DLH.cpp"
+#endif
+#include "MyDebug.h"
+
+#include "LSM303DLH.h"
+
+LSM303DLH::LSM303DLH(PinName sda, PinName scl): dev(sda,scl) {
+    uint8_t d[16];
+    INFO("LSM303DLH Init (400kHz)");
+    dev.frequency(400000);
+    // Check accelerometer presence
+    this->devRead(WHO_AM_I,d,1);
+    if(d[0] == LSM303DH_ID) {
+        INFO("LSM303DH(0x%02X) well detected!!",d[0]);
+        // Config accelerometer
+        this->basicConfig();
+    } else
+        ERR("Unknow ID 0x%02X ...",d[0]);
+}
+
+uint8_t LSM303DLH::devRead(const uint8_t reg, uint8_t *data, uint8_t size) {
+    uint8_t *pReg = (uint8_t*)&reg; 
+    *pReg |= 0x80;
+    __disable_irq();
+    dev.write(LSM303D_ADDR_W,(char*)pReg,1);
+    dev.read(LSM303D_ADDR_R,(char*)data,size);
+    __enable_irq();
+    return size;
+}
+
+uint8_t LSM303DLH::devReadSingle(const uint8_t reg, uint8_t byte) {
+    return this->devRead(reg,&byte,1);
+}
+
+uint8_t LSM303DLH::devWrite(const uint8_t reg, uint8_t *data, uint8_t size) {
+    // Check size
+    if((size+1)>WRITE_BUFF_MAX) {
+        ERR("BAD SIZE WRITE");
+        return 0;
+    } else {
+        // Put the reg addr in first byte place
+        dataBuff[0] = reg | 0x80;
+        for(int i = 0; i< size; i++)
+            dataBuff[i+1] = data[i];
+        // Do the Writing
+        dev.write(LSM303D_ADDR_W,(char*)dataBuff,size+1);
+        return size;
+    }
+}
+
+uint8_t LSM303DLH::devWriteSingle(const uint8_t reg, uint8_t byte) {
+    return this->devWrite(reg,&byte,1);
+}
+
+// Hi Level API
+
+void LSM303DLH::basicConfig() {
+    // Accelerometer
+    // 0x00 = 0b00000000
+    // AFS = 0 (+/- 2 g full scale) - > 0.061 g/LSB
+    this->devWriteSingle(CTRL2, 0x00);
+    accLSB = 61.0;
+    // 0x57 = 0b01010111
+    // AODR = 0101 (50 Hz ODR); AZEN = AYEN = AXEN = 1 (all axes enabled)
+    this->devWriteSingle(CTRL1, 0x57);
+
+    // Magnetometer
+    // 0x64 = 0b01100100
+    // M_RES = 11 (high resolution mode); M_ODR = 001 (6.25 Hz ODR)
+    this->devWriteSingle(CTRL5, 0x64);
+    // 0x20 = 0b00100000
+    // MFS = 01 (+/- 4 gauss full scale)
+    this->devWriteSingle(CTRL6, 0x20);
+    magLSB = 80.0;
+    // 0x00 = 0b00000000
+    // MLP = 0 (low power mode off); MD = 00 (continuous-conversion mode)
+    this->devWriteSingle(CTRL7, 0x00);
+    
+    DBG("LSM303DH configuration done\r\n");
+}
+
+void LSM303DLH::readRawAcc(int16_t *x, int16_t *y, int16_t *z) {
+    uint8_t rawData[6] = {0,0,0,0,0,0};
+    if(this->devRead(OUT_X_L_A,rawData,6)){
+        *x = (int16_t)(rawData[0] | (rawData[1] << 8));
+        *y = (int16_t)(rawData[2] | (rawData[3] << 8));
+        *z = (int16_t)(rawData[4] | (rawData[5] << 8));
+    }
+}
+
+void LSM303DLH::readAcc(float *x, float *y, float *z) {
+    int16_t rawX;
+    int16_t rawY;
+    int16_t rawZ;
+    this->readRawAcc(&rawX,&rawY,&rawZ);
+    *x = (((float) rawX) * accLSB) /1000000.0;
+    *y = (((float) rawY) * accLSB) /1000000.0;
+    *z = (((float) rawZ) * accLSB) /1000000.0;
+}
+
+
+void LSM303DLH::readRawMag(int16_t *x, int16_t *y, int16_t *z) {
+    uint8_t rawData[6] = {0,0,0,0,0,0};
+    if(this->devRead(OUT_X_L_M,rawData,6)){
+        *x = (int16_t)(rawData[0] | (rawData[1] << 8));
+        *y = (int16_t)(rawData[2] | (rawData[3] << 8));
+        *z = (int16_t)(rawData[4] | (rawData[5] << 8));
+    }
+}
+
+void LSM303DLH::readMag(float *x, float *y, float *z) {
+    int16_t rawX;
+    int16_t rawY;
+    int16_t rawZ;
+    this->readRawMag(&rawX,&rawY,&rawZ);
+    *x = (((float) rawX) * magLSB) / 1000000.0;
+    *y = (((float) rawY) * magLSB) / 1000000.0;
+    *z = (((float) rawZ) * magLSB) / 1000000.0;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LSM303DLH.h	Tue Apr 14 13:30:02 2015 +0000
@@ -0,0 +1,69 @@
+#ifndef __LSM303DLH_H
+#define __LSM303DLH_H
+
+#include "mbed.h"
+
+// CONSTS
+
+#define WRITE_BUFF_MAX  16
+#define LSM303DH_ID     0x49
+
+// I2C Addresses (SAO -> 1)
+
+#define LSM303D_ADDR_R   0x3B
+#define LSM303D_ADDR_W  0x3A       
+
+// REGISTERS
+
+#define CTRL0       0x1F
+#define CTRL1       0x20
+#define CTRL2       0x21
+#define CTRL3       0x22
+#define CTRL4       0x23
+#define CTRL5       0x24
+#define CTRL6       0x25
+#define CTRL7       0x26
+
+#define WHO_AM_I    0x0F
+
+#define TEMP_OUT_L  0x05
+#define TEMP_OUT_H  0x06
+
+#define STATUS_M    0x07
+#define OUT_X_L_M   0x08
+#define OUT_X_H_M   0x09
+#define OUT_Y_L_M   0x0A
+#define OUT_Y_H_M   0x0B
+#define OUT_Z_L_M   0x0C
+#define OUT_Z_H_M   0x0D
+
+#define STATUS_A    0x27
+#define OUT_X_L_A   0x28
+#define OUT_X_H_A   0x29
+#define OUT_Y_L_A   0x2A
+#define OUT_Y_H_A   0x2B
+#define OUT_Z_L_A   0x2C
+#define OUT_Z_H_A   0x2D
+
+class LSM303DLH {
+private:
+    float accLSB;
+    float magLSB;
+    
+    I2C dev;
+    uint8_t dataBuff[WRITE_BUFF_MAX];           // Who read more than WRITE_BUFF_MAX(16)?
+    
+    uint8_t devRead(const uint8_t reg, uint8_t *data, uint8_t size);
+    uint8_t devReadSingle(const uint8_t reg, uint8_t byte);
+    uint8_t devWrite(const uint8_t reg, uint8_t *data, uint8_t size);
+    uint8_t devWriteSingle(const uint8_t reg, uint8_t byte);
+public:
+    LSM303DLH(PinName sda = p28, PinName scl = p27); 
+    void basicConfig(void);
+    void readRawAcc(int16_t *x, int16_t *y, int16_t *z);
+    void readRawMag(int16_t *x, int16_t *y, int16_t *z);
+    void readAcc(float *x, float *y, float *z);
+    void readMag(float *x, float *y, float *z);
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MODSERIAL.lib	Tue Apr 14 13:30:02 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/AjK/code/MODSERIAL/#ae0408ebdd68
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MainThread.cpp	Tue Apr 14 13:30:02 2015 +0000
@@ -0,0 +1,277 @@
+
+#define __DEBUG__ 5
+#ifndef __MODULE__
+#define __MODULE__ "MainThread.cpp"
+#endif
+#include "MyDebug.h"
+
+#include "mbed.h"
+
+#include "MyOsHelpers.h"
+#include "MyThread.h"
+#include "MyBlinker.h"
+#include "IMUSensor.h"
+#include "GPSSensor.h"
+#include "CANInterface.h"
+#include "CANSniffer.h"
+#include "PyrnUSBModem.h"
+
+#include "HTTPClient.h"
+//#include "TCPSocketConnection.h"
+
+char dataResult[1024];
+
+#if 0
+void test(void const*) {
+
+    DBG("Start");
+
+    //HTTPClient http;
+    //TCPSocketConnection tcpSock;
+    
+    IMUSensor imu(p28,p27);
+    GPSSensor gps(p13,p14,4,250);
+    CANInterface canItf;
+    CANSniffer canSnif(&canItf);
+    //PyrnUSBModem modem;
+    
+    PrintActiveThreads();
+    
+    //modem.connect("a2bouygtel.com","","");
+
+    PwmOut led_red(LED1);
+    PwmOut led_green(LED2);
+
+    MyBlinker br = MyBlinker(&led_red,1000);
+    MyBlinker bg = MyBlinker(&led_green,2000);
+
+    imu.Start();
+    gps.Start();
+    canItf.Start();
+    br.Start();
+    bg.Start();
+
+    imu.Run();
+    gps.Run();
+    canItf.Run();
+    br.Run();
+    bg.Run();
+    
+    // ecrire dans 5 et 6
+    char cbuff[8] = {'C','A','N','1','[',' ',' ',']'};
+    uint16_t loop = 0;
+    uint16_t len = 0;
+    
+    while(1) {
+        // Fill up the cbuff
+        cbuff[5] = 0xff & (loop>>8);
+        cbuff[6] = 0xff & loop;
+        // Print all threads
+        PrintActiveThreads();
+        Thread::wait(1000);
+        time_t seconds = time(NULL);
+        if((loop % 20) == 0) {
+            gps.Capture(dataResult,&len);
+            DBG("GPS[%04d] Got %03d chars",seconds,len);
+        }
+        if((loop % 30) == 0) {
+            imu.Capture(dataResult,&len);
+            DBG("IMU[%04d] Got %03d chars",seconds,len);
+        }
+        /*
+        if((loop%10) == 0) {
+            DBG("Trying to fetch page...\n");
+            int ret = http.get("http://developer.mbed.org/media/uploads/donatien/hello.txt", dataResult, 128);
+            if (!ret)
+            {
+              DBG("Page fetched successfully - read %d characters\n", strlen(dataResult));
+              DBG("Result: %s\n", dataResult);
+            } else {
+              DBG("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
+            }
+        }
+        */
+        /*
+        if((loop%10) == 0) {
+            char rcvBuf[16];
+            if(modem.pppConnected()) {
+                DBG("====> PPP is connected go for TCP Socket");
+                if(!tcpSock.is_connected()) {
+                    if(tcpSock.connect("carbon14.clemounet.fr",8888) == 0){
+                    //if(tcpSock.connect("195.154.85.42",8888) == 0){
+                         DBG("TCP Socket got Connected");
+                    } else {
+                         DBG("TCP Socket could not Connected");
+                    }
+                }
+                if(tcpSock.is_connected()) {
+                //if(tcpSock.connect("carbon14.clemounet.fr",8888) == 0){
+                    DBG("TCP Socket is Connected");
+                    if(tcpSock.send(cbuff,8)){
+                        DBG("TCP Data Sent ... now receive");
+                        int n = tcpSock.receive(rcvBuf, 16);
+                        if(n) {
+                            DBG("TCP Data Received");
+                            DBG_MEMDUMP("TCP RCV",rcvBuf,n);
+                        } else {
+                            DBG("TCP Data Receive Failed");
+                        }
+                    } else {
+                        DBG("TCP Failed to send data");
+                        
+                    }
+                } else {
+                    DBG("TCP Socket not connected");
+                }
+            }
+        }
+        */
+        canItf.Send(1,0x666,cbuff,8);
+        loop++;
+    }
+    
+    imu.Stop();
+    gps.Stop();
+    canItf.Stop();
+    br.Stop();
+    bg.Stop();
+}
+#endif
+
+void ModemLuncher(void const*) {
+    DBG("Start");
+    HTTPClient http;
+    PyrnUSBModem modem;
+    
+    PrintActiveThreads();
+    
+    Thread::wait(500);
+    modem.connect("a2bouygtel.com","","");
+    int loop = 0;
+    
+    while(1) {
+        DBG("LoopThread");
+        Thread::wait(1000);
+        
+        if((loop%10) == 0) {
+            PrintActiveThreads();
+            DBG("Trying to fetch page...\n");
+            int ret = http.get("http://developer.mbed.org/media/uploads/donatien/hello.txt", dataResult, 128);
+            if (!ret)
+            {
+              DBG("Page fetched successfully - read %d characters\n", strlen(dataResult));
+              DBG("Result: %s\n", dataResult);
+            } else {
+              DBG("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
+            }
+        }
+        
+        /*
+        if((loop%10) == 0) {
+            char rcvBuf[16];
+            if(modem.pppConnected()) {
+                DBG("====> PPP is connected go for TCP Socket");
+                if(!tcpSock.is_connected()) {
+                    if(tcpSock.connect("carbon14.clemounet.fr",8888) == 0){
+                    //if(tcpSock.connect("195.154.85.42",8888) == 0){
+                         DBG("TCP Socket got Connected");
+                    } else {
+                         DBG("TCP Socket could not Connected");
+                    }
+                }
+                if(tcpSock.is_connected()) {
+                //if(tcpSock.connect("carbon14.clemounet.fr",8888) == 0){
+                    DBG("TCP Socket is Connected");
+                    if(tcpSock.send(cbuff,8)){
+                        DBG("TCP Data Sent ... now receive");
+                        int n = tcpSock.receive(rcvBuf, 16);
+                        if(n) {
+                            DBG("TCP Data Received");
+                            DBG_MEMDUMP("TCP RCV",rcvBuf,n);
+                        } else {
+                            DBG("TCP Data Receive Failed");
+                        }
+                    } else {
+                        DBG("TCP Failed to send data");
+                        
+                    }
+                } else {
+                    DBG("TCP Socket not connected");
+                }
+            }
+        }
+        */
+        loop++;
+    }
+}
+
+
+int main(void) {
+    
+    set_time(0);
+    debug_init();
+    debug_set_newline("\r\n");
+    debug_set_speed(115200);
+    
+    // Simply lunch the modem stuffs in separate thread, (not using the MyThread Stuffs)
+    // >>>> It allows to do other stuffs during this dead time <<<<
+    Thread testTask(ModemLuncher, NULL, osPriorityNormal, 1024 * 5);
+    
+    //IMUSensor imu(p28,p27);
+    //GPSSensor gps(p13,p14,4,250);
+    //CANInterface canItf;
+    //CANSniffer canSnif(&canItf);
+    
+    //PwmOut led_red(LED1);
+    //PwmOut led_green(LED2);
+
+    //MyBlinker br = MyBlinker(&led_red,1000);
+    //MyBlinker bg = MyBlinker(&led_green,2000);
+
+    //imu.Start();
+    //gps.Start();
+    //canItf.Start();
+    //br.Start();
+    //bg.Start();
+
+    //imu.Run();
+    //gps.Run();
+    //canItf.Run();
+    //br.Run();
+    //bg.Run();
+    
+    // ecrire dans 5 et 6
+    char cbuff[8] = {'C','A','N','1','[',' ',' ',']'};
+    uint16_t loop = 0;
+    uint16_t len = 0;
+    
+    while(1){
+        // Fill up the cbuff
+        cbuff[5] = 0xff & (loop>>8);
+        cbuff[6] = 0xff & loop;
+        // Print all threads
+        PrintActiveThreads();
+        Thread::wait(1000);
+        time_t seconds = time(NULL);
+        /*if((loop % 20) == 0) {
+            gps.Capture(dataResult,&len);
+            DBG("GPS[%04d] Got %03d chars",seconds,len);
+        }
+        if((loop % 30) == 0) {
+            imu.Capture(dataResult,&len);
+            DBG("IMU[%04d] Got %03d chars",seconds,len);
+        }*/
+        DBG("LoopMain");
+        //canItf.Send(1,0x666,cbuff,8);
+        Thread::wait(1000);  
+        loop++;   
+    }
+    
+    //imu.Stop();
+    //gps.Stop();
+    //canItf.Stop();
+    //br.Stop();
+    //bg.Stop();
+    
+    return 0;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MyThings.lib	Tue Apr 14 13:30:02 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/clemounet/code/MyThings/#eef83534b19e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Pyrn3GModem.lib	Tue Apr 14 13:30:02 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/clemounet/code/Pyrn3GModem/#61ac95f0af72
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket.lib	Tue Apr 14 13:30:02 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/clemounet/code/Socket/#c35d004aaf72
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TinyGPS.lib	Tue Apr 14 13:30:02 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/clemounet/code/TinyGPS/#34b4815eb82f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBHost.lib	Tue Apr 14 13:30:02 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/clemounet/code/MyUSBHost/#79b6743d7dc5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lwip-sys.lib	Tue Apr 14 13:30:02 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mbed_official/code/lwip-sys/#7d4b24b58e04
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lwip.lib	Tue Apr 14 13:30:02 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mbed_official/code/lwip/#1046f8be4d44
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lwipopts_conf.h	Tue Apr 14 13:30:02 2015 +0000
@@ -0,0 +1,26 @@
+/* lwipopts.h */
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * 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 LWIPOPTS_CONF_H_
+#define LWIPOPTS_CONF_H_
+ 
+#define LWIP_TRANSPORT_ETHERNET 0 
+#define LWIP_TRANSPORT_PPP 1
+
+#endif /* LWIPOPTS_CONF_H_ */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Tue Apr 14 13:30:02 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#83895f30f8f2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-src.lib	Tue Apr 14 13:30:02 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-src/#6cc86c42d7e3