Richard Nash / Mbed OS XPL-App5

Dependencies:   TFTv2 nihh-utils xpl

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 // ##################################################################################
00003 // XPL with TFT Screen Application
00004 // ##################################################################################
00005 
00006 // if defined this will echo all debug txt going to the TFT onto the serial port
00007 int _TFT_ECHO = 0;
00008 
00009 // turn OFF dependency on being connected to ethernet
00010 //#define ETH_OFF
00011 
00012 
00013 // MBED Headers
00014 #include "mbed.h"
00015 #include "EthernetInterface.h"
00016 #include "Watchdog.h"
00017 #include "rtos.h"
00018 
00019 // -----------------------------------------------------------
00020 // Graphics Headers
00021 #include "ILI9341.h"
00022 #include "SPI_STMPE610.h"
00023 #include "Arial12x12.h"
00024 #include "Arial24x23.h"
00025 #include "Arial28x28.h"
00026 #include "Arial43x48_numb.h"
00027 
00028 // -----------------------------------------------------------
00029 // My Headers
00030 #include "Watchdog.h"
00031 #include "xPL.h"
00032 #include "nihh_utils.h"
00033 
00034 // -----------------------------------------------------------
00035 // XPL defines
00036 #define UDP_PORT            3865
00037 #define UDP_BUFFER_SIZE     500
00038 
00039 #define XPL_NAME            "NUCLEO1"
00040 
00041 // -----------------------------------------------------------
00042 // global variables
00043 Watchdog wd;
00044 xPL xpl;
00045 
00046 DigitalOut led1(LED1);  // green = normal startup
00047 DigitalOut led2(LED2);  // blue = ethernet data received
00048 DigitalOut led3(LED3);  // red = watchdog startup
00049 
00050 Serial logger(SERIAL_TX, SERIAL_RX);
00051 
00052 // =====================================
00053 
00054 EthernetInterface   gEth;
00055 UDPSocket           gEth_socket;
00056 char                gEth_socket_in_buffer[UDP_BUFFER_SIZE];
00057 
00058 //char                in_buffer[UDP_BUFFER_SIZE+1];
00059 int                 iRXBytes;
00060 int                 GLOBAL_NET_STATUS = 0;
00061 
00062 SocketAddress       _sAddr;
00063 
00064 
00065 // =====================================
00066 
00067 
00068 #define PIN_RESET_TFT   PC_13 /* place holder */
00069 
00070 #define PIN_MOSI        D11
00071 #define PIN_MISO        D12
00072 #define PIN_SCLK        D13
00073 #define PIN_CS_TFT      D5
00074 #define PIN_DC_TFT      D6
00075 #define PIN_BL_TFT      D7
00076 #define PIN_CS_SD       D4
00077 
00078 #define PIN_XP          A2
00079 #define PIN_XM          A0
00080 #define PIN_YP          A3
00081 #define PIN_YM          A1
00082 
00083 struct point {
00084     int x;
00085     int y;
00086 };
00087 
00088 void calibrate(void);
00089 bool getPixel(point& p);
00090 point toPixel(point p);
00091 
00092 typedef enum { YES, MAYBE, NO } TOUCH;
00093 
00094 TOUCH getTouch(point& p);
00095 
00096 int readTouch(PinName p, PinName m, PinName a, PinName i);
00097 
00098 int x_off = 22071;
00099 int y_off = 18707;
00100 int pp_tx = 373;
00101 int pp_ty = 297;
00102 
00103 DigitalOut backlight(PIN_BL_TFT) ;
00104 
00105 ILI9341 TFT(SPI_8, 10000000, PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, PIN_RESET_TFT, PIN_DC_TFT, "Seeed2.8") ;
00106 
00107 // which screen page are we on
00108 int page = 0 ;   // 0 is the special debug screen
00109 
00110 
00111 // =====================================
00112 
00113 Mutex mutex_TFT;
00114 
00115 void TFT_Debug_XY_F ( int, int, char *, ... );
00116 
00117 // =====================================
00118 
00119 
00120 // -----------------------------------------------------------
00121 // 
00122 // -----------------------------------------------------------
00123 void terminal_error_state( void )
00124 {
00125     led1=0;
00126     led2=0;
00127     led3=0;
00128 
00129     logger.printf("\r\n\r\nJUST TO BE CLEAR - THIS IS BAD BAD NEWS !!! \r\n");
00130     while( 1 )
00131         {
00132         led1 = !led1;
00133         led2 = !led2;
00134         led3 = !led3;
00135         wait(0.5);
00136         
00137         // Just waiting for the watchdog to kill us
00138         }
00139 }
00140 
00141 
00142 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00143 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00144 // ++++++++   TFT SCREEN FUNCTIONS
00145 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00146 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00147 
00148 int readTouch(PinName p, PinName m, PinName a, PinName i)
00149 {
00150     DigitalOut _p(p);
00151     _p = 1;
00152     DigitalOut _m(m);
00153     _m = 0;
00154     AnalogIn   _a(a);
00155     AnalogIn   _i(i); // this pin has to be high Z (DigitalIn may also work)
00156     wait_us(10);
00157     return _a.read_u16();
00158 }
00159 
00160 TOUCH getTouch(point& p)
00161 {
00162     int y2 = readTouch(PIN_XP,PIN_XM,PIN_YP,PIN_YM);
00163     int x2 = readTouch(PIN_YP,PIN_YM,PIN_XP,PIN_XM);
00164     int y1 = readTouch(PIN_XP,PIN_XM,PIN_YP,PIN_YM);
00165     int x1 = readTouch(PIN_YP,PIN_YM,PIN_XP,PIN_XM);
00166     int xd = x1 - x2;
00167     int yd = y1 - y2;
00168     xd = (xd > 0) ? xd : -xd;
00169     yd = (yd > 0) ? xd : -xd;
00170     p.x = x1 + x2;
00171     p.y = y1 + y2;
00172     
00173     const int th = 8000;
00174     const int df =  100;
00175     TOUCH touch;
00176     if (x1 < th || x2 < th ||
00177             y1 < th || y2 < th) {
00178         p.x = 0;
00179         p.y = 0;
00180         touch = NO;
00181     } else if (xd > df || yd > df) {
00182         touch = MAYBE;
00183     } else {
00184         touch = YES;
00185     }
00186     //TFT.locate(0,50);
00187     //TFT.printf("x: %6i y: %6i",p.x,p.y);
00188     return touch;
00189 }
00190 
00191 point toPixel(point p)
00192 {
00193     p.x -= x_off;
00194     p.x /= pp_tx;
00195     int w = TFT.width();
00196     if (p.x > w) p.x = w;
00197     if (p.x < 0) p.x = 0;
00198     p.y -= y_off;
00199     p.y /= pp_ty;
00200     int h = TFT.height();
00201     if (p.y > h) p.y = h;
00202     if (p.y < 0) p.y = 0;
00203     return (p);
00204 }
00205 
00206 bool getPixel(point& p)
00207 {
00208     TOUCH touch = getTouch(p);
00209     p = toPixel(p);
00210     return touch == YES;
00211 }
00212 
00213 // -----------------------------------------------------------
00214 // 
00215 // -----------------------------------------------------------
00216 void initTFT(void)
00217 {
00218     //Configure the display driver
00219     //TFT.BusEnable(true) ;
00220     TFT.FastWindow(true) ;
00221     TFT.background(Black);
00222     TFT.foreground(White);
00223     wait(0.01) ;
00224     TFT.cls();
00225     //TFT.BusEnable(false) ;
00226 }
00227 
00228 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00229 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00230 
00231 
00232 void screen1(void) // Welcome Screen
00233 {
00234     page = 1;
00235     
00236     //TFT.BusEnable(true) ;
00237     backlight = 0 ;
00238     TFT.background(White) ;
00239     wait(0.1) ;
00240     TFT.cls() ;
00241     wait(0.1) ;
00242     
00243     TFT.set_font((unsigned char*) Arial24x23);
00244     TFT.foreground(Red) ;
00245     TFT.locate(20, 40) ;
00246     TFT.printf("NASHLAND") ;
00247     TFT.foreground(Blue);
00248     TFT.locate(20, 80) ;
00249     TFT.printf("EMBEDDED") ; 
00250     TFT.locate(20, 120) ;
00251     TFT.printf("CONTROLLER") ;
00252     
00253     TFT.foreground(Black);
00254     TFT.set_font((unsigned char*) Arial12x12);
00255     TFT.foreground(Blue) ;
00256     TFT.locate(30, 180) ;
00257     TFT.printf("This program is running on a") ;
00258     TFT.locate(30, 200) ;
00259     TFT.printf("ST Nucleo 144 Controller") ;
00260     TFT.locate(30, 220) ;
00261     TFT.printf("running mbed.os") ;
00262     
00263     //TFT.BusEnable(false) ;
00264     backlight = 1 ;
00265 }
00266 
00267 void screen_Debug(void)
00268 {
00269     int y = Terminal6x8[2] + 2;
00270     
00271     mutex_TFT.lock();
00272     
00273     page = 0;
00274     
00275     TFT.set_font((unsigned char*) Terminal6x8);
00276     
00277     //Draw some text
00278     backlight = 0 ;
00279     
00280     TFT.background(Black);
00281     TFT.foreground(White);
00282     wait(0.1) ;
00283     TFT.cls() ;
00284     wait(0.1) ;
00285     TFT.cls() ;
00286     
00287     backlight = 1 ;
00288     
00289         {
00290         // Output the network information
00291         const char *ip = gEth.get_ip_address();
00292         const char *netmask = gEth.get_netmask();
00293         const char *gateway = gEth.get_gateway();
00294         TFT.locate(0, 1*y);
00295         TFT.printf("IP address: %s\r\n", ip ? ip : "None");
00296         TFT.locate(0, 2*y);
00297         TFT.printf("Netmask: %s\r\n", netmask ? netmask : "None");
00298         TFT.locate(0, 3*y);
00299         TFT.printf("Gateway: %s\r\n", gateway ? gateway : "None");
00300         }
00301     
00302     mutex_TFT.unlock();
00303 }
00304 
00305 
00306 void calibrate(void)
00307 {
00308     int i;
00309     int a = 0,b = 0,c = 0, d = 0;
00310     int pos_x = 0, pos_y = 0;
00311     point p;
00312     
00313     page = 99;
00314     
00315     backlight = 0 ;
00316     //TFT.BusEnable(true) ;
00317     
00318     TFT.background(Black);
00319     wait(0.1) ;
00320     TFT.foreground(White);
00321     wait(0.1) ;
00322     TFT.cls() ;
00323     wait(0.1) ;
00324     TFT.set_font((unsigned char*) Arial12x12);
00325     TFT.locate(90,0);
00326     TFT.printf("Graphics");
00327     
00328     TFT.line(0,3,6,3,White);
00329     TFT.line(3,0,3,6,White);
00330     
00331     backlight = 1 ;
00332     
00333     //if (font)
00334     //{
00335         // get the center of the screen
00336         pos_x = TFT.columns() / 2 - 3;
00337         pos_x = pos_x * Arial12x12[1];
00338         pos_y = (TFT.rows() / 2) - 1;
00339         pos_y = pos_y * Arial12x12[2];
00340         TFT.locate(pos_x,pos_y);
00341         TFT.printf("press cross    ");
00342         TFT.locate(pos_x,pos_y + Arial12x12[2]);
00343         TFT.printf("to calibrate   ");
00344     //}
00345     
00346     for (i=0; i<5; i++) {
00347         while (getTouch(p) != YES)
00348             /*nothing*/;
00349         a += p.x;
00350         b += p.y;
00351     }
00352     a = a / 5;
00353     b = b / 5;
00354     //if (font)
00355     //{
00356         TFT.locate(pos_x,pos_y);
00357         TFT.printf("ok             ");
00358         TFT.locate(pos_x,pos_y + Arial12x12[2]);
00359         TFT.printf("release touch  ");
00360     //}
00361     while (getTouch(p) != NO)
00362         /*nothing*/;
00363     
00364     TFT.cls();
00365     TFT.line(TFT.width() -5, TFT.height() - 8,TFT.width() - 5,TFT.height() -1,White);   // paint cross
00366     TFT.line(TFT.width() - 8,TFT.height() - 5,TFT.width() - 1,TFT.height() - 5,White);
00367     //if (font)
00368     //{
00369         TFT.locate(pos_x,pos_y);
00370         TFT.printf("press cross    ");
00371         TFT.locate(pos_x,pos_y + Arial12x12[2]);
00372         TFT.printf("to calibrate   ");
00373     //}
00374     for (i=0; i<5; i++) {
00375         while (getTouch(p) != YES)
00376             /*nothing*/;
00377         c+= p.x;
00378         d+= p.y;
00379     }
00380     c = c / 5;
00381     d = d / 5;
00382     x_off = a;
00383     y_off = b;
00384     i = c-a;  // delta x
00385     pp_tx = i / (TFT.width() - 6);
00386     i = d-b;  // delta y
00387     pp_ty = i / (TFT.height() - 6);
00388     //if (font)
00389     //{
00390         TFT.locate(pos_x,pos_y);
00391         TFT.printf("Calibrated     ");
00392         TFT.locate(pos_x,pos_y + Arial12x12[2]);
00393         TFT.printf("x %6i %4i", x_off, pp_tx);
00394         TFT.locate(pos_x,pos_y + 2*Arial12x12[2]);
00395         TFT.printf("y %6i %4i", y_off, pp_ty);
00396     //}
00397     while (getTouch(p) != NO)
00398         /*nothing*/;
00399     TFT.cls();
00400     
00401     //TFT.BusEnable(false) ;
00402     
00403     printf("x_off:%6i pp_tx:%4i \n\r", x_off, pp_tx);
00404     printf("y_off:%6i pp_ty:%4i \n\r", y_off, pp_ty);
00405 }
00406 
00407 
00408 // -----------------------------------------------------------
00409 // 
00410 // -----------------------------------------------------------
00411 void TFT_Debug_XY2(int x, int y, const char * pszTxt)
00412 {
00413     // is the TFT on the debug page?
00414     if( page == 0 ) 
00415         {
00416         int xFont = Terminal6x8[1];
00417         int yFont = Terminal6x8[2] + 2;
00418         
00419         mutex_TFT.lock();
00420             
00421             TFT.locate(x*xFont, y*yFont);
00422             TFT.printf(pszTxt);
00423             
00424         mutex_TFT.unlock();
00425         }
00426     
00427     // is serial echo on?
00428     if( _TFT_ECHO ) 
00429         {
00430         logger.printf("%s\r\n", pszTxt );
00431         }
00432 }
00433 
00434 void TFT_Debug_XY_F2( int x, int y, const char *fmt, ... )
00435 {
00436     mutex_TFT.lock();
00437         {
00438         char szLogFormat [100];
00439         
00440         // sort out the args !
00441         va_list args;
00442         va_start(args, fmt);
00443         
00444         // Format the string
00445         vsnprintf( szLogFormat, 100, fmt, args);
00446         va_end(args);
00447         
00448         // output the string
00449         TFT_Debug_XY2( x, y, szLogFormat);
00450         }
00451     
00452     mutex_TFT.unlock();
00453 }
00454 
00455 // =========================================================================================================
00456 
00457 // -----------------------------------------------------------
00458 // 
00459 // -----------------------------------------------------------
00460 void status_callback(nsapi_event_t status, intptr_t param)
00461 {
00462     //char szLogFormat [100];
00463     
00464     logger.printf("status_callback\r\n");
00465     
00466     //strcpy( szLogFormat, "ETH: Connection " );
00467     //TFT_Debug_XY2( 0, 0, szLogFormat);
00468     
00469     GLOBAL_NET_STATUS = param;
00470     
00471     switch(param) 
00472         {
00473         case NSAPI_STATUS_LOCAL_UP:
00474             {
00475             logger.printf("NSAPI_STATUS_LOCAL_UP\r\n");
00476 //          strcpy( szLogFormat, "ETH: Connection - LOCAL_UP              ");
00477 //          TFT_Debug_XY2( 0, 0, szLogFormat);
00478             break;
00479             }
00480         case NSAPI_STATUS_GLOBAL_UP:
00481             {
00482             logger.printf("NSAPI_STATUS_GLOBAL_UP\r\n");
00483             
00484 /*
00485             strcpy( szLogFormat, "ETH: Connection - GLOBAL_UP              ");
00486             TFT_Debug_XY2( 0, 0, szLogFormat);
00487             
00488             // Output the network address
00489             const char *ip = gEth.get_ip_address();
00490             const char *netmask = gEth.get_netmask();
00491             const char *gateway = gEth.get_gateway();
00492             
00493             TFT_Debug_XY_F2(0, 1, "IP address: %s\r\n", ip ? ip : "None");
00494             TFT_Debug_XY_F2(0, 2, "Netmask: %s\r\n", netmask ? netmask : "None");
00495             TFT_Debug_XY_F2(0, 3, "Gateway: %s\r\n", gateway ? gateway : "None");
00496 */
00497             
00498             break;
00499             }
00500         case NSAPI_STATUS_DISCONNECTED:
00501             {
00502             logger.printf("NSAPI_STATUS_DISCONNECTED\r\n");
00503 //          strcpy( szLogFormat, "ETH: Connection - DISCONNECTED              ");
00504 //          TFT_Debug_XY2( 0, 0, szLogFormat);
00505             break;
00506             }
00507         case NSAPI_STATUS_CONNECTING:
00508             {
00509             logger.printf("NSAPI_STATUS_CONNECTING\r\n");
00510 //          strcpy( szLogFormat, "ETH: Connection - CONNECTING              ");
00511 //          TFT_Debug_XY2( 0, 0, szLogFormat);
00512             break;
00513             }
00514         default:
00515             {
00516             logger.printf("NSAPI - default ?\r\n");
00517 //          strcpy( szLogFormat, "ETH: Connection - ????                  ");
00518 //          TFT_Debug_XY2( 0, 0, szLogFormat);
00519             break;
00520             }
00521         }
00522 }
00523 
00524 // -----------------------------------------------------------
00525 // 
00526 // -----------------------------------------------------------
00527 static bool DeviceConnect()
00528 {
00529     int retries = 10;
00530     int rc = 0;
00531     
00532     //logger.printf("DeviceConnect()\r\n");
00533     
00534     while (retries--) 
00535         {
00536         rc = gEth.connect();
00537         if (rc == 0) {
00538 //          logger.printf("DeviceConnect: Waiting\r\n");
00539 //          wait(2);
00540 //          logger.printf("DeviceConnect: Done\r\n");
00541             return true;
00542             }
00543         else
00544             {
00545             logger.printf("Connecting... ERR %d\r\n", rc);
00546             }
00547         
00548         wd.Service();
00549         }
00550     
00551     logger.printf("Connecting... FAILED\r\n");
00552     return false;
00553 }
00554 
00555 // -----------------------------------------------------------
00556 // 
00557 // -----------------------------------------------------------
00558 static bool DeviceEthStartup()
00559 {
00560     //logger.printf("DeviceEthStartup()\r\n");
00561     
00562     // get ethernet up !
00563     if( !DeviceConnect() ) terminal_error_state();
00564     
00565     // Output the network address
00566     //const char *ip = gEth.get_ip_address();
00567     //const char *netmask = gEth.get_netmask();
00568     //const char *gateway = gEth.get_gateway();
00569     
00570     //TFT_Debug_XY_F2(0, 1, "IP address: %s\r\n", ip ? ip : "None");
00571     //TFT_Debug_XY_F2(0, 2, "Netmask: %s\r\n", netmask ? netmask : "None");
00572     //TFT_Debug_XY_F2(0, 3, "Gateway: %s\r\n", gateway ? gateway : "None");
00573     
00574     return true;
00575 }
00576 
00577 // -----------------------------------------------------------
00578 // 
00579 // -----------------------------------------------------------
00580 void SendUdPMessage(char *buffer)
00581 {
00582     // send the UDP data as broadcast
00583     int rc = gEth_socket.sendto( "255.255.255.255", UDP_PORT, (const void *)buffer, strlen( buffer ) );
00584     //logger.printf("[SEND rc=%d] buffer[%d]\r\n%s\r\n", rc, strlen(buffer), buffer);
00585 }
00586 
00587 // -----------------------------------------------------------
00588 // 
00589 // -----------------------------------------------------------
00590 void AfterParseAction(xPL_Message * message)
00591 {
00592     char szLogFormat [100];
00593     
00594     // is this a message for me ?
00595     if (xpl.TargetIsMe(message))
00596         {
00597         logger.printf("XPL: MSG IS FOR ME !");
00598         logger.printf("%s\r\n", message->toString());
00599 
00600         // is this a control message?
00601         if (message->IsSchema("control", "basic"))
00602             {
00603             logger.printf("XPL: {control.basic}");
00604             }
00605         }
00606     
00607     // Put it on the screen
00608     sprintf( szLogFormat, "XPL: [%20s] %2d items    ", message->source.device_id, message->command_count );
00609     TFT_Debug_XY2( 1, 18, szLogFormat);
00610     
00611     //logger.printf("XPL: [%s]\r\n", message->source.device_id );
00612     //logger.printf("XPL: [%s] %d items\r\n", message->source.device_id, message->command_count );
00613     //TFT_Debug_XY_F( 0, 18, "XPL: [%s] %d items      ", message->source.device_id, message->command_count );
00614     //TFT_Debug_XY( 0, 19, "XPL:                          ");
00615     
00616     // do we have any data ???  maybe the parse failed....
00617     if( message->command_count == 0 )
00618         {
00619         // 
00620 //        logger.printf("XPL: no data? [bytes: %d] \r\n%s\r\n", iRXBytes, gEth_socket_in_buffer );
00621         //TFT_Debug_XY( 0, 19, "XPL: ERROR - PARSE FAILED ??  ");
00622         sprintf( szLogFormat, "XPL: ERROR - PARSE FAILED ??  ");
00623         TFT_Debug_XY2( 1, 19, szLogFormat);
00624         }
00625     else
00626         {
00627         // output the message data  
00628         for (short i=0; i<message->command_count; i++)
00629             {
00630             // grab each item and do something with it
00631             //logger.printf("%s=%s\r\n", message->command[i].name, message->command[i].value);
00632             }
00633         //TFT_Debug_XY_F( 0, 19, "XPL: %d items processed        ", message->command_count );
00634         sprintf( szLogFormat, "XPL: %d items processed        ", message->command_count );
00635         TFT_Debug_XY2( 1, 19, szLogFormat);
00636         }
00637     
00638     // output the full msg!
00639     //logger.printf("%s\r\n", message->toString());
00640 }
00641 
00642 // -----------------------------------------------------------
00643 // 
00644 // -----------------------------------------------------------
00645 void Receiver_Thread()
00646 {
00647     SocketAddress   _sAddr;
00648     int             iNoDataCount = 0;
00649     int             rc;
00650     int             iDataMsgCount = 0;
00651 
00652     // setup a listener
00653     gEth_socket.set_blocking(true);
00654     gEth_socket.set_timeout(3000);
00655     
00656     // open the socket
00657     rc = gEth_socket.open((NetworkInterface*)&gEth);
00658     if( rc != 0)
00659         {
00660         logger.printf("UDPSocket RECEIVER OPEN ERROR   (rc=%d)\r\n", rc);
00661         terminal_error_state();
00662         }
00663     
00664     // bind the port
00665     rc = gEth_socket.bind(UDP_PORT);
00666     logger.printf("Binding port %d\r\n", UDP_PORT);
00667     if( rc != 0)
00668         {
00669         logger.printf("UDPSocket BIND ERROR   (rc=%d)\r\n", rc);
00670         terminal_error_state();
00671         }
00672     
00673     // loop forever
00674     while(1)
00675         {
00676         // receive from the udp socket
00677         
00678         TFT_Debug_XY_F2(1, 15, "RX: Attempt (%2d)                ", iNoDataCount);
00679         
00680         iRXBytes = gEth_socket.recvfrom(&_sAddr, gEth_socket_in_buffer, UDP_BUFFER_SIZE);
00681         
00682         TFT_Debug_XY2(20, 15, "-DONE-");
00683         
00684         // see what weve got !
00685         if (iRXBytes == NSAPI_ERROR_WOULD_BLOCK)
00686             {
00687             led3 = 1; // error
00688             
00689             // timeout with no data...
00690             ++iNoDataCount;
00691             TFT_Debug_XY2(1, 16, "RX: NoData (TIMEOUT)             ");
00692             
00693             //screen_Debug();
00694             }
00695         else if (iRXBytes < 0)
00696             {
00697             led3 = 1; // error
00698             
00699             // some other error
00700             ++iNoDataCount;
00701             TFT_Debug_XY2(1, 16, "RX: NoData (ERR)                 ");
00702             }
00703         else if( iRXBytes > 0)
00704             {
00705             led3 = 0; // no error
00706             led2 = 1;
00707             
00708             // good data :)
00709             iNoDataCount = 0;
00710             gEth_socket_in_buffer[iRXBytes] = '\0';
00711             TFT_Debug_XY_F2(1, 16, "RX: [%d bytes] %s:%d    ", iRXBytes, _sAddr.get_ip_address(), _sAddr.get_port());
00712             
00713             // try and parse the msg
00714             xpl.ParseInputMessage(gEth_socket_in_buffer);
00715             
00716             TFT_Debug_XY_F2(1, 26, "RX: DATA(%d)", iDataMsgCount++);
00717             led2 = 0;
00718             }
00719         
00720         // have we stopped receiving data ?!?!?
00721         if( iNoDataCount > 60 ) // about 3 mins (60*3secs) of no data
00722             {
00723             // go for a reboot...
00724             terminal_error_state();
00725             }
00726         
00727         // stroke the WD
00728         wd.Service();  
00729         }
00730 }
00731 
00732 
00733 // -----------------------------------------------------------
00734 // 
00735 // -----------------------------------------------------------
00736 Thread thread1(osPriorityAboveNormal, 8 * 1024); // OS_STACK_SIZE in theory 8k
00737 int main() 
00738     {
00739     const char *ip;
00740     char xpl_ip[20];
00741     int prev_net_status=0;
00742     
00743     point p;
00744     
00745     bool waitTouch = false ;
00746     int iAppLoop=0;
00747     int iHBSentCount = 0;
00748     
00749     // FIRST THINGS FIRST!!!  set the WD timeout interval...
00750     // 100 seconds to get connected and receive an IP address
00751     wd.Configure(100000.0);
00752     
00753     // open pc serial
00754     logger.baud(115200);
00755     logger.printf("---- ETHERNET UDP Test ----\r\n");
00756     
00757     // turn all the LEDs off
00758     led1 = 0;   // green = normal startup
00759     led2 = 0;   // blue = ethernet data received
00760     led3 = 0;   // red = watchdog startup
00761     
00762     // WatchDog Startup Information
00763     logger.printf("\r\n------------------------------------------------------\r\n" );
00764     logger.printf("[SystemCoreClock] %d Hz\r\n", SystemCoreClock);
00765     logger.printf("[Reset reason] ");
00766     if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST)) logger.printf("Independant-Watchdog  ");
00767     if (__HAL_RCC_GET_FLAG(RCC_FLAG_WWDGRST)) logger.printf("Window-Watchdog  ");
00768     if (__HAL_RCC_GET_FLAG(RCC_FLAG_LPWRRST)) logger.printf("Low-Power-Reset  ");
00769     if (__HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST)) logger.printf("Software-Reset  ");
00770     if (__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST)) logger.printf("Power-On/Down-Reset  ");
00771     if (__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST)) logger.printf("Reset-pin  ");
00772     if (__HAL_RCC_GET_FLAG(RCC_FLAG_BORRST)) logger.printf("Brownout  ");
00773     if (__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY)) logger.printf("Low-speed-internal-clock-ready  ");
00774     __HAL_RCC_CLEAR_RESET_FLAGS();
00775     logger.printf("\r\n------------------------------------------------------\r\n");
00776     
00777     if (wd.WatchdogCausedReset())
00778         {
00779         logger.printf("<<Watchdog caused reset>>\r\n");
00780         led3 = 1;
00781         }
00782     else
00783         {
00784         logger.printf("<<Normal Startup>>\r\n");
00785         led1 = 1;
00786         led2 = 1;
00787         led3 = 0;
00788         }
00789     logger.printf("------------------------------------------------------\r\n\r\n");
00790     
00791     // Fireup the TFT Screen
00792     initTFT() ;
00793     //logger.printf("TFT Started {width = %d, height = %d}\n\r", TFT.width(), TFT.height()) ;
00794     //calibrate();
00795     //screen_Debug();
00796     screen1();
00797     
00798     // fire up the ethernet & the receive socket
00799     gEth.attach(&status_callback);
00800     DeviceEthStartup();
00801     
00802     // just wait for everything to settle
00803     logger.printf("Network.");
00804     while( GLOBAL_NET_STATUS != NSAPI_STATUS_GLOBAL_UP )
00805         {
00806         logger.printf(".");
00807         wait(1);
00808         }
00809     logger.printf("UP!\r\n");
00810     
00811     // stroke the wd - all ok so far !
00812     wd.Service();
00813     
00814     screen_Debug();
00815     
00816     // 10 second is enough to make sure everything is ok - UDP read is every 3 seconds
00817     wd.Configure(10000.0);
00818     
00819     // process the devices ip adress into the XPL instance ID
00820     ip = gEth.get_ip_address();
00821     strcpy( xpl_ip, ip );
00822     UTILS_strip_char_in_string( xpl_ip, '.' );
00823     
00824     // setup xpl
00825     xpl.SendExternal = &SendUdPMessage;         // pointer to the send callback
00826     xpl.AfterParseAction = &AfterParseAction;   // pointer to a post parsing action callback 
00827     xpl.SetSource( "NIHH", XPL_NAME, xpl_ip);   // parameters for hearbeat message
00828     
00829     // kick off the XPL receiver thread to run in the background
00830 //    thread1.set_priority(osPriorityAboveNormal);  // osPriorityNormal osPriorityHigh osPriorityBelowNormal osPriorityAboveNormal
00831     thread1.start( Receiver_Thread );
00832     
00833     
00834     // calibrate the screen
00835     //calibrate();
00836     
00837     
00838     // start running the main processing stuff here
00839     while (true) 
00840         {
00841         //logger.printf(".\n\r") ;
00842         
00843         //TFT_Debug_XY2( 5, 10, "Hello world..." );
00844         
00845         // pause for a mo !
00846         //wait(5);
00847         
00848         
00849         do  {
00850             led1 = !led1;
00851             
00852             if( GLOBAL_NET_STATUS != prev_net_status )
00853                 {
00854                 TFT_Debug_XY_F2(0, 0, "NET: %d", GLOBAL_NET_STATUS);
00855                 logger.printf("net..\n\r") ;
00856                 
00857                 prev_net_status = GLOBAL_NET_STATUS;
00858                 }
00859             
00860             
00861             // allow xpl to do its thing...
00862             #ifndef CONNECTED_OFF
00863                 xpl.Process();
00864             #endif
00865             
00866             if( getPixel(p) ) 
00867                 {
00868                 logger.printf("TFT Touch x = %d, y = %d\n\r", p.x, p.y) ;
00869                 waitTouch = false ;
00870                 wait(.2); // stop 
00871                 
00872                 backlight = !backlight;
00873                 }
00874             
00875             // every X loops send a test XPL msg
00876             ++iAppLoop;
00877             if( iAppLoop >= 50 )
00878                 {
00879                 xPL_Message _message;
00880                 
00881                 /*
00882                 _message.SetSchema( "clock", "update" );
00883                 _message.SetTarget( "*" );
00884                 _message.type = XPL_STAT;
00885                 _message.hop = 1;
00886                 _message.AddCommand("time", "20180730113315");
00887                 xpl.SendMessage( (xPL_Message *)&_message, true );
00888                 */
00889                 
00890                 _message.SetSchema( "sensor", "basic" );
00891                 _message.SetTarget( "*" );
00892                 _message.type = XPL_STAT;
00893                 _message.hop = 1;
00894                 _message.AddCommand("device", "test1");
00895                 _message.AddCommand("type", "test");
00896                 _message.AddCommand("current", "1");
00897                 xpl.SendMessage( (xPL_Message *)&_message, true );
00898                 
00899                 logger.printf("<<<<<< Sample Message Sent >>>>>>\r\n");
00900                 TFT_Debug_XY_F2(1, 27, "TX: HB(%d)", iHBSentCount++);
00901                 
00902                 // reset
00903                 iAppLoop = 0;
00904                 }
00905             
00906             // pause for a sec !
00907             ThisThread::sleep_for(200);
00908             
00909             } while(waitTouch != false) ;
00910         
00911         }
00912     }
00913 
00914 // test