Rick McConney / Mbed 2 deprecated StarterKit

Dependencies:   FXOS8700CQ mbed

Fork of AvnetATT_shape_hackathon by Rick McConney

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <cctype>
00003 #include "SerialBuffered.h"
00004 #include "config_me.h"
00005 #include "Wnc.h"
00006 
00007 extern SerialBuffered mdm;
00008 Wnc wnc;
00009 int seqNum =0;
00010 int msgLen = 10;
00011 
00012 
00013 // comment out the following line if color is not supported on the terminal
00014 #define USE_COLOR
00015 #ifdef USE_COLOR
00016  #define BLK "\033[30m"
00017  #define RED "\033[31m"
00018  #define GRN "\033[32m"
00019  #define YEL "\033[33m"
00020  #define BLU "\033[34m"
00021  #define MAG "\033[35m"
00022  #define CYN "\033[36m"
00023  #define WHT "\033[37m"
00024  #define DEF "\033[39m"
00025 #else
00026  #define BLK
00027  #define RED
00028  #define GRN
00029  #define YEL
00030  #define BLU
00031  #define MAG
00032  #define CYN
00033  #define WHT
00034  #define DEF
00035 #endif
00036 
00037 #define MDM_DBG_OFF                             0
00038 #define MDM_DBG_AT_CMDS                         (1 << 0)
00039 
00040 
00041 
00042 int mdm_dbgmask = MDM_DBG_OFF;
00043 
00044 Serial         pc(USBTX, USBRX);
00045 
00046 DigitalOut led_green(LED_GREEN);
00047 DigitalOut led_red(LED_RED);
00048 DigitalOut led_blue(LED_BLUE);
00049 
00050 
00051 
00052 
00053 
00054 //DigitalIn   slot1(PTB3,PullUp);
00055 //DigitalIn   slot2(PTB10,PullUp);
00056 //DigitalIn   slot3(PTB11,PullUp);
00057 
00058 DigitalIn   sw2(SW2);
00059 
00060 
00061 #define TOUPPER(a) (a) //toupper(a)
00062 
00063 
00064 #define MDM_OK                                  0
00065 #define MDM_ERR_TIMEOUT                         -1
00066 
00067 #define MAX_AT_RSP_LEN                          255
00068 
00069 bool toggleLed = false;
00070 
00071 
00072 
00073 //********************************************************************************************************************************************
00074 //* Set the RGB LED's Color
00075 //* LED Color 0=Off to 7=White.  3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue) 
00076 //********************************************************************************************************************************************
00077 void SetLedColor(unsigned char ucColor)
00078 {
00079 
00080     //Note that when an LED is on, you write a 0 to it:
00081     led_red = !(ucColor & 0x1); //bit 0
00082     led_green = !(ucColor & 0x2); //bit 1
00083     led_blue = !(ucColor & 0x4); //bit 2
00084 
00085 } //SetLedColor() 
00086 
00087 
00088 void system_reset()
00089 {
00090     printf(RED "\n\rSystem resetting..." DEF "\n");
00091     NVIC_SystemReset();
00092 }
00093  
00094 void GenerateTestString(char * modem_string,int len)
00095 {           
00096     seqNum++;
00097     char data[len+1];
00098     for(int i = 0;i<len;i++)
00099         data[i] = 'a';
00100     data[len] = 0;
00101     sprintf(modem_string, "%s", data);
00102 }
00103 
00104 int main() {
00105 
00106     pc.baud(115200);
00107    
00108     // Set LED to RED until init finishes
00109     SetLedColor(0x1);
00110 
00111     pc.printf(BLU "Hello World\r\n\n\r");
00112 
00113     // Initialize the modem
00114     printf(GRN "Modem initializing... will take up to 60 seconds" DEF "\r\n");
00115     if(!wnc.init()) 
00116     {
00117         pc.printf(RED "Modem initialization failed!" DEF "\n");
00118         system_reset();
00119     }
00120           
00121     char* iccid = wnc.getIccid();
00122     printf(GRN "ICCID = %s" DEF "\r\n",iccid);
00123     
00124 
00125            
00126     //Software init
00127     if(!wnc.startInternet())
00128         system_reset();
00129     
00130     char* ip = wnc.resolveDn(MY_SERVER_URL);
00131     if(strlen(ip) == 0)
00132     {
00133        printf(RED "Failed to resolve IP for %s" DEF "\r\n",MY_SERVER_URL); 
00134     }
00135     else
00136         printf(GRN "IP = %s" DEF "\r\n",ip);
00137 
00138     // Set LED BLUE for partial init
00139     SetLedColor(0x4);
00140  
00141     char modem_string[512];                
00142     
00143     while(true)
00144     {   
00145         toggleLed = !toggleLed;
00146         if(toggleLed)
00147             SetLedColor(0x2); //green 
00148         else    
00149             SetLedColor(0); //off 
00150             
00151         if(sw2 == 0)
00152         {      
00153             GenerateTestString(modem_string,msgLen);
00154             printf(BLU "Sending to modem : %s" DEF "\r\n", modem_string);
00155                       
00156             if(wnc.connect(ip,MY_PORT)) // shelf
00157             {
00158                 if(wnc.writeSocket(modem_string))
00159                 {       
00160                     char* mydata;
00161                     int tries = 8;
00162                     while(tries > 0) // wait for reply
00163                     {
00164                         tries--;
00165                         mydata = wnc.readSocket();
00166                         if (strlen(mydata) > 0)
00167                             break;
00168                  
00169                         wait(0.5);
00170                     }
00171             
00172                     if (strlen(mydata) > 0)
00173                     {                                 
00174                         SetLedColor(0x2); // green
00175                         printf(BLU "Read back : [%s]" DEF "\r\n", mydata);
00176                         sscanf(mydata,"LEN=%d",&msgLen);
00177                     }
00178                 }
00179                 wnc.disconnect();
00180             }
00181             else // failed to connect reset
00182             {
00183                 SetLedColor(0x1); //red
00184                 system_reset(); 
00185             }   
00186         }
00187         wait(0.2); 
00188     } //forever loop
00189 }