Clément BENOIT / PYRN

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MainThread.cpp Source File

MainThread.cpp

00001 
00002 #define __DEBUG__ 5
00003 #ifndef __MODULE__
00004 #define __MODULE__ "MainThread.cpp"
00005 #endif
00006 #include "MyDebug.h"
00007 
00008 #include "mbed.h"
00009 
00010 #include "MyOsHelpers.h"
00011 #include "MyThread.h"
00012 #include "MyBlinker.h"
00013 #include "IMUSensor.h"
00014 #include "GPSSensor.h"
00015 #include "CANInterface.h"
00016 #include "CANSniffer.h"
00017 #include "PyrnUSBModem.h"
00018 
00019 #include "HTTPClient.h"
00020 //#include "TCPSocketConnection.h"
00021 
00022 char dataResult[1024];
00023 
00024 #if 0
00025 void test(void const*) {
00026 
00027     DBG("Start");
00028 
00029     //HTTPClient http;
00030     //TCPSocketConnection tcpSock;
00031     
00032     IMUSensor imu(p28,p27);
00033     GPSSensor gps(p13,p14,4,250);
00034     CANInterface canItf;
00035     CANSniffer canSnif(&canItf);
00036     //PyrnUSBModem modem;
00037     
00038     PrintActiveThreads();
00039     
00040     //modem.connect("a2bouygtel.com","","");
00041 
00042     PwmOut led_red(LED1);
00043     PwmOut led_green(LED2);
00044 
00045     MyBlinker br = MyBlinker(&led_red,1000);
00046     MyBlinker bg = MyBlinker(&led_green,2000);
00047 
00048     imu.Start();
00049     gps.Start();
00050     canItf.Start();
00051     br.Start();
00052     bg.Start();
00053 
00054     imu.Run();
00055     gps.Run();
00056     canItf.Run();
00057     br.Run();
00058     bg.Run();
00059     
00060     // ecrire dans 5 et 6
00061     char cbuff[8] = {'C','A','N','1','[',' ',' ',']'};
00062     uint16_t loop = 0;
00063     uint16_t len = 0;
00064     
00065     while(1) {
00066         // Fill up the cbuff
00067         cbuff[5] = 0xff & (loop>>8);
00068         cbuff[6] = 0xff & loop;
00069         // Print all threads
00070         PrintActiveThreads();
00071         Thread::wait(1000);
00072         time_t seconds = time(NULL);
00073         if((loop % 20) == 0) {
00074             gps.Capture(dataResult,&len);
00075             DBG("GPS[%04d] Got %03d chars",seconds,len);
00076         }
00077         if((loop % 30) == 0) {
00078             imu.Capture(dataResult,&len);
00079             DBG("IMU[%04d] Got %03d chars",seconds,len);
00080         }
00081         /*
00082         if((loop%10) == 0) {
00083             DBG("Trying to fetch page...\n");
00084             int ret = http.get("http://developer.mbed.org/media/uploads/donatien/hello.txt", dataResult, 128);
00085             if (!ret)
00086             {
00087               DBG("Page fetched successfully - read %d characters\n", strlen(dataResult));
00088               DBG("Result: %s\n", dataResult);
00089             } else {
00090               DBG("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
00091             }
00092         }
00093         */
00094         /*
00095         if((loop%10) == 0) {
00096             char rcvBuf[16];
00097             if(modem.pppConnected()) {
00098                 DBG("====> PPP is connected go for TCP Socket");
00099                 if(!tcpSock.is_connected()) {
00100                     if(tcpSock.connect("carbon14.clemounet.fr",8888) == 0){
00101                     //if(tcpSock.connect("195.154.85.42",8888) == 0){
00102                          DBG("TCP Socket got Connected");
00103                     } else {
00104                          DBG("TCP Socket could not Connected");
00105                     }
00106                 }
00107                 if(tcpSock.is_connected()) {
00108                 //if(tcpSock.connect("carbon14.clemounet.fr",8888) == 0){
00109                     DBG("TCP Socket is Connected");
00110                     if(tcpSock.send(cbuff,8)){
00111                         DBG("TCP Data Sent ... now receive");
00112                         int n = tcpSock.receive(rcvBuf, 16);
00113                         if(n) {
00114                             DBG("TCP Data Received");
00115                             DBG_MEMDUMP("TCP RCV",rcvBuf,n);
00116                         } else {
00117                             DBG("TCP Data Receive Failed");
00118                         }
00119                     } else {
00120                         DBG("TCP Failed to send data");
00121                         
00122                     }
00123                 } else {
00124                     DBG("TCP Socket not connected");
00125                 }
00126             }
00127         }
00128         */
00129         canItf.Send(1,0x666,cbuff,8);
00130         loop++;
00131     }
00132     
00133     imu.Stop();
00134     gps.Stop();
00135     canItf.Stop();
00136     br.Stop();
00137     bg.Stop();
00138 }
00139 #endif
00140 
00141 void ModemLuncher(void const*) {
00142     DBG("Start");
00143     HTTPClient http;
00144     PyrnUSBModem modem;
00145     
00146     PrintActiveThreads();
00147     
00148     Thread::wait(500);
00149     modem.connect("a2bouygtel.com","","");
00150     int loop = 0;
00151     
00152     while(1) {
00153         DBG("LoopThread");
00154         Thread::wait(1000);
00155         
00156         if((loop%10) == 0) {
00157             PrintActiveThreads();
00158             DBG("Trying to fetch page...\n");
00159             int ret = http.get("http://developer.mbed.org/media/uploads/donatien/hello.txt", dataResult, 128);
00160             if (!ret)
00161             {
00162               DBG("Page fetched successfully - read %d characters\n", strlen(dataResult));
00163               DBG("Result: %s\n", dataResult);
00164             } else {
00165               DBG("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
00166             }
00167         }
00168         
00169         /*
00170         if((loop%10) == 0) {
00171             char rcvBuf[16];
00172             if(modem.pppConnected()) {
00173                 DBG("====> PPP is connected go for TCP Socket");
00174                 if(!tcpSock.is_connected()) {
00175                     if(tcpSock.connect("carbon14.clemounet.fr",8888) == 0){
00176                     //if(tcpSock.connect("195.154.85.42",8888) == 0){
00177                          DBG("TCP Socket got Connected");
00178                     } else {
00179                          DBG("TCP Socket could not Connected");
00180                     }
00181                 }
00182                 if(tcpSock.is_connected()) {
00183                 //if(tcpSock.connect("carbon14.clemounet.fr",8888) == 0){
00184                     DBG("TCP Socket is Connected");
00185                     if(tcpSock.send(cbuff,8)){
00186                         DBG("TCP Data Sent ... now receive");
00187                         int n = tcpSock.receive(rcvBuf, 16);
00188                         if(n) {
00189                             DBG("TCP Data Received");
00190                             DBG_MEMDUMP("TCP RCV",rcvBuf,n);
00191                         } else {
00192                             DBG("TCP Data Receive Failed");
00193                         }
00194                     } else {
00195                         DBG("TCP Failed to send data");
00196                         
00197                     }
00198                 } else {
00199                     DBG("TCP Socket not connected");
00200                 }
00201             }
00202         }
00203         */
00204         loop++;
00205     }
00206 }
00207 
00208 
00209 int main(void) {
00210     
00211     set_time(0);
00212     debug_init();
00213     debug_set_newline("\r\n");
00214     debug_set_speed(115200);
00215     
00216     // Simply lunch the modem stuffs in separate thread, (not using the MyThread Stuffs)
00217     // >>>> It allows to do other stuffs during this dead time <<<<
00218     Thread testTask(ModemLuncher, NULL, osPriorityNormal, 1024 * 5);
00219     
00220     //IMUSensor imu(p28,p27);
00221     //GPSSensor gps(p13,p14,4,250);
00222     //CANInterface canItf;
00223     //CANSniffer canSnif(&canItf);
00224     
00225     //PwmOut led_red(LED1);
00226     //PwmOut led_green(LED2);
00227 
00228     //MyBlinker br = MyBlinker(&led_red,1000);
00229     //MyBlinker bg = MyBlinker(&led_green,2000);
00230 
00231     //imu.Start();
00232     //gps.Start();
00233     //canItf.Start();
00234     //br.Start();
00235     //bg.Start();
00236 
00237     //imu.Run();
00238     //gps.Run();
00239     //canItf.Run();
00240     //br.Run();
00241     //bg.Run();
00242     
00243     // ecrire dans 5 et 6
00244     char cbuff[8] = {'C','A','N','1','[',' ',' ',']'};
00245     uint16_t loop = 0;
00246     uint16_t len = 0;
00247     
00248     while(1){
00249         // Fill up the cbuff
00250         cbuff[5] = 0xff & (loop>>8);
00251         cbuff[6] = 0xff & loop;
00252         // Print all threads
00253         PrintActiveThreads();
00254         Thread::wait(1000);
00255         time_t seconds = time(NULL);
00256         /*if((loop % 20) == 0) {
00257             gps.Capture(dataResult,&len);
00258             DBG("GPS[%04d] Got %03d chars",seconds,len);
00259         }
00260         if((loop % 30) == 0) {
00261             imu.Capture(dataResult,&len);
00262             DBG("IMU[%04d] Got %03d chars",seconds,len);
00263         }*/
00264         DBG("LoopMain");
00265         //canItf.Send(1,0x666,cbuff,8);
00266         Thread::wait(1000);  
00267         loop++;   
00268     }
00269     
00270     //imu.Stop();
00271     //gps.Stop();
00272     //canItf.Stop();
00273     //br.Stop();
00274     //bg.Stop();
00275     
00276     return 0;
00277 }