![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
app5
Dependencies: TFTv2 nihh-utils xpl
Diff: main.cpp
- Revision:
- 3:f96822b3edb5
- Parent:
- 2:1335510cfb92
- Child:
- 4:ed0665b5d253
--- a/main.cpp Sat Oct 13 14:32:45 2018 +0100 +++ b/main.cpp Sun Oct 28 11:38:24 2018 +0000 @@ -3,17 +3,19 @@ // XPL with TFT Screen Application // ################################################################################## -// generate XPL debug level output on the serial line -//#define DEBUG_OUTPUT_XPL +// if defined this will echo all debug txt going to the TFT onto the serial port +//#define TFT_ECHO +int _TFT_ECHO = 0; // turn OFF dependency on being connected to ethernet -//#define CONNECTED_OFF +//#define ETH_OFF -// ----------------------------------------------------------- + // MBED Headers #include "mbed.h" #include "EthernetInterface.h" -#include <math.h> +#include "Watchdog.h" +#include "rtos.h" // ----------------------------------------------------------- // Graphics Headers @@ -45,19 +47,25 @@ DigitalOut led1(LED1); // green = normal startup DigitalOut led2(LED2); // blue = ethernet data received DigitalOut led3(LED3); // red = watchdog startup -DigitalOut led4(LED4); // ?? Serial logger(SERIAL_TX, SERIAL_RX); // ===================================== EthernetInterface gEth; -UDPSocket _socket; -UDPSocket _socketSender; -char in_buffer[UDP_BUFFER_SIZE]; +UDPSocket gEth_socket; +char gEth_socket_in_buffer[UDP_BUFFER_SIZE]; + +//char in_buffer[UDP_BUFFER_SIZE+1]; +int iRXBytes; +int GLOBAL_NET_STATUS = 0; + +SocketAddress _sAddr; + // ===================================== + #define PIN_RESET_TFT PC_13 /* place holder */ #define PIN_MOSI D11 @@ -97,9 +105,39 @@ ILI9341 TFT(SPI_8, 10000000, PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, PIN_RESET_TFT, PIN_DC_TFT, "Seeed2.8") ; -// screen pages -int page = 2 ; -int numPage = 3 ; +// which screen page are we on +int page = 0 ; // 0 is the special debug screen + + +// ===================================== + +Mutex mutex_TFT; + +void TFT_Debug_XY_F ( int, int, char *, ... ); + +// ===================================== + + +// ----------------------------------------------------------- +// +// ----------------------------------------------------------- +void terminal_error_state( void ) +{ + led1=0; + led2=0; + led3=0; + + logger.printf("\r\n\r\nJUST TO BE CLEAR - THIS IS BAD BAD NEWS !!! \r\n"); + while( 1 ) + { + led1 = !led1; + led2 = !led2; + led3 = !led3; + wait(0.5); + + // Just waiting for the watchdog to kill us + } +} // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ @@ -151,6 +189,121 @@ return touch; } +point toPixel(point p) +{ + p.x -= x_off; + p.x /= pp_tx; + int w = TFT.width(); + if (p.x > w) p.x = w; + if (p.x < 0) p.x = 0; + p.y -= y_off; + p.y /= pp_ty; + int h = TFT.height(); + if (p.y > h) p.y = h; + if (p.y < 0) p.y = 0; + return (p); +} + +bool getPixel(point& p) +{ + TOUCH touch = getTouch(p); + p = toPixel(p); + return touch == YES; +} + +// ----------------------------------------------------------- +// +// ----------------------------------------------------------- +void initTFT(void) +{ + //Configure the display driver + //TFT.BusEnable(true) ; + TFT.FastWindow(true) ; + TFT.background(Black); + TFT.foreground(White); + wait(0.01) ; + TFT.cls(); + //TFT.BusEnable(false) ; +} + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + +void screen1(void) // Welcome Screen +{ + page = 1; + + //TFT.BusEnable(true) ; + backlight = 0 ; + TFT.background(White) ; + wait(0.1) ; + TFT.cls() ; + wait(0.1) ; + + TFT.set_font((unsigned char*) Arial24x23); + TFT.foreground(Red) ; + TFT.locate(20, 40) ; + TFT.printf("NASHLAND") ; + TFT.foreground(Blue); + TFT.locate(20, 80) ; + TFT.printf("EMBEDDED") ; + TFT.locate(20, 120) ; + TFT.printf("CONTROLLER") ; + + TFT.foreground(Black); + TFT.set_font((unsigned char*) Arial12x12); + TFT.foreground(Blue) ; + TFT.locate(30, 180) ; + TFT.printf("This program is running on a") ; + TFT.locate(30, 200) ; + TFT.printf("ST Nucleo 144 Controller") ; + TFT.locate(30, 220) ; + TFT.printf("running mbed.os") ; + + //TFT.BusEnable(false) ; + backlight = 1 ; +} + +void screen_Debug(void) +{ + int y = Terminal6x8[2] + 2; + + mutex_TFT.lock(); + + page = 0; + + TFT.set_font((unsigned char*) Terminal6x8); + + //Draw some text + backlight = 0 ; + + TFT.background(Black); + TFT.foreground(White); + wait(0.1) ; + TFT.cls() ; + wait(0.1) ; + TFT.cls() ; + + backlight = 1 ; + + { + // Output the network information + const char *ip = gEth.get_ip_address(); + const char *netmask = gEth.get_netmask(); + const char *gateway = gEth.get_gateway(); + TFT.locate(0, 1*y); + TFT.printf("IP address: %s\r\n", ip ? ip : "None"); + TFT.locate(0, 2*y); + TFT.printf("Netmask: %s\r\n", netmask ? netmask : "None"); + TFT.locate(0, 3*y); + TFT.printf("Gateway: %s\r\n", gateway ? gateway : "None"); + } + + mutex_TFT.unlock(); +} + + void calibrate(void) { int i; @@ -158,6 +311,8 @@ int pos_x = 0, pos_y = 0; point p; + page = 99; + backlight = 0 ; //TFT.BusEnable(true) ; @@ -250,243 +405,121 @@ printf("y_off:%6i pp_ty:%4i \n\r", y_off, pp_ty); } -point toPixel(point p) -{ - p.x -= x_off; - p.x /= pp_tx; - int w = TFT.width(); - if (p.x > w) p.x = w; - if (p.x < 0) p.x = 0; - p.y -= y_off; - p.y /= pp_ty; - int h = TFT.height(); - if (p.y > h) p.y = h; - if (p.y < 0) p.y = 0; - return (p); -} -bool getPixel(point& p) -{ - TOUCH touch = getTouch(p); - p = toPixel(p); - return touch == YES; -} - - -// =================================================== - -void initTFT(void) +// ----------------------------------------------------------- +// +// ----------------------------------------------------------- +void TFT_Debug_XY2(int x, int y, const char * pszTxt) { - //Configure the display driver - //TFT.BusEnable(true) ; - TFT.FastWindow(true) ; - TFT.background(Black); - TFT.foreground(White); - wait(0.01) ; - TFT.cls(); - //TFT.BusEnable(false) ; -} - -void screen1(void) // Welcome Screen -{ - //TFT.BusEnable(true) ; - backlight = 0 ; - TFT.background(White) ; - wait(0.1) ; - TFT.cls() ; - wait(0.1) ; - - TFT.set_font((unsigned char*) Arial24x23); - TFT.foreground(Red) ; - TFT.locate(80, 40) ; - TFT.printf("MBED") ; - TFT.foreground(Blue); - TFT.locate(60, 80) ; - TFT.printf("2.8\"TFT") ; - TFT.locate(40, 120) ; - TFT.printf("with touch") ; - TFT.foreground(Black); - TFT.set_font((unsigned char*) Arial12x12); - TFT.foreground(Blue) ; - TFT.locate(30, 180) ; - TFT.printf("This program is running on") ; - TFT.locate(30, 200) ; - TFT.printf("ST Nucleo F411RE with") ; - TFT.locate(30, 220) ; - TFT.printf("a program developed in mbed") ; - TFT.foreground(Green) ; - TFT.locate(30, 260) ; - TFT.printf("To advance demo page, touch") ; - TFT.locate(30, 280) ; - TFT.printf("and hold right side of screen") ; - TFT.locate(30, 300) ; - TFT.printf("until the next screen starts") ; - //TFT.BusEnable(false) ; - backlight = 1 ; + // is the TFT on the debug page? + if( page == 0 ) + { + int xFont = Terminal6x8[1]; + int yFont = Terminal6x8[2] + 2; + + mutex_TFT.lock(); + + TFT.locate(x*xFont, y*yFont); + TFT.printf(pszTxt); + + mutex_TFT.unlock(); + } + + // is serial echo on? + if( _TFT_ECHO ) + { + logger.printf("%s\r\n", pszTxt ); + } } -void screen2(void) // Graphics +void TFT_Debug_XY_F2( int x, int y, const char *fmt, ... ) { - //Draw some graphics - int i, x[2], y[2] ; - backlight = 0 ; - //TFT.BusEnable(true) ; - TFT.background(Black); - TFT.foreground(White); - wait(0.1) ; - TFT.cls() ; - wait(0.1) ; - TFT.set_font((unsigned char*) Arial12x12); - TFT.locate(90,0); - TFT.printf("Graphics"); - - x[0] = 25 ; x[1] = 224 ; - y[0] = 20 ; y[1] = 219 ; - for (i = 20 ; i < 220 ; i += 10) { - TFT.line(i+5, y[0], i+5, y[1], Blue) ; - TFT.line(x[0], i, x[1], i, Blue) ; - } - TFT.line(125, y[0], 125, y[1], Green) ; - TFT.line(x[0], 120, x[1], 120, Green) ; - TFT.rect(x[0],y[0], x[1], y[1], Green) ; - TFT.locate(10, 20) ; - TFT.printf("V") ; - TFT.locate(0, 115) ; - TFT.printf("0.0") ; - TFT.locate(115, 225) ; - TFT.printf("0.0") ; - TFT.locate(215, 225) ; - TFT.printf("T") ; - - double s; - for (int i = x[0]; i < 225; i++) { - s = 40 * sin((long double)i / 20); - TFT.pixel(i, 120 + (int)s, White); - } - - TFT.fillrect(10, 240, 229, 309, White) ; - TFT.rect(10, 240, 229, 309, Red) ; - TFT.rect(11, 241, 228, 308, Red) ; - - TFT.background(White) ; - TFT.foreground(Black) ; - TFT.locate(20, 250) ; - TFT.printf("With QVGA resolution") ; - TFT.locate(20, 270) ; - TFT.printf("simple graphics drawing") ; - TFT.locate(20, 290) ; - TFT.printf("capability is provided") ; - //TFT.BusEnable(false) ; - backlight = 1 ; -} - -void screen3(void) // Graphics -{ - //int rowX; - //int rowY; - //int x = Terminal6x8[1]; - int y = Terminal6x8[2] + 2; - - TFT.set_font((unsigned char*) Terminal6x8); - - //Draw some text - backlight = 0 ; - - TFT.background(Black); - TFT.foreground(White); - wait(0.1) ; - TFT.cls() ; - wait(0.1) ; - TFT.cls() ; + mutex_TFT.lock(); + { + char szLogFormat [100]; + + // sort out the args ! + va_list args; + va_start(args, fmt); + + // Format the string + vsnprintf( szLogFormat, 100, fmt, args); + va_end(args); + + // output the string + TFT_Debug_XY2( x, y, szLogFormat); + } - backlight = 1 ; - - #ifndef CONNECTED_OFF - { - // Output the network information - const char *ip = gEth.get_ip_address(); - const char *netmask = gEth.get_netmask(); - const char *gateway = gEth.get_gateway(); - TFT.locate(0, 1*y); - TFT.printf("IP address: %s\r\n", ip ? ip : "None"); - TFT.locate(0, 2*y); - TFT.printf("Netmask: %s\r\n", netmask ? netmask : "None"); - TFT.locate(0, 3*y); - TFT.printf("Gateway: %s\r\n", gateway ? gateway : "None"); - } - #endif - - //TFT.set_font((unsigned char*) Terminal6x8); - -/* for( rowY=0; rowY<100; ++rowY ) - { - //TFT.locate(0, (rowY%25)*y); - TFT.locate(0, 1*y); - TFT.printf("%3d TFT width = %d, height = %d", rowY, TFT.width(), TFT.height()) ; - TFT.locate(0, 2*y); - TFT.printf("%3d FIRST TEST DATA %3d", rowY, rowY ) ; - TFT.locate(0, 3*y); - TFT.printf("%3d MORE TEST DATA %3d", rowY, rowY ) ; - } -*/ + mutex_TFT.unlock(); } -void TFT_Debug(char * pszTxt) // Graphics -{ - if (page == 2) { - int x = Terminal6x8[1]; - int y = Terminal6x8[2] + 2; - -// TFT.background(Black); -// TFT.foreground(White); -// TFT.set_font((unsigned char*) Terminal6x8); - -// backlight = 0 ; - TFT.locate(0, 10*y); - TFT.printf("%s", pszTxt ); -// backlight = 1 ; - } -} - -void incPage(void) -{ - page++ ; - if (page >= numPage) { - page = 0 ; - } -} - -void decPage(void) -{ - page-- ; - if (page < 0) { - page = numPage - 1 ; - } -} - -// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -// ++++++++ ETHERNET -// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +// ========================================================================================================= // ----------------------------------------------------------- // // ----------------------------------------------------------- -void terminal_error_state( void ) +void status_callback(nsapi_event_t status, intptr_t param) { - led1=0; - led2=0; - led3=0; - logger.printf("\r\n\r\nJUST TO BE CLEAR - THIS IS BAD BAD NEWS !!! \r\n"); - logger.printf("\r\n\r\n---- WAITING FOR THE WATCHDOG TO RESET US ---- \r\n"); - while( 1 ) - { - led1 = !led1; - led2 = !led2; - led3 = !led3; - wait(0.3); - } + //char szLogFormat [100]; + + logger.printf("status_callback\r\n"); + + //strcpy( szLogFormat, "ETH: Connection " ); + //TFT_Debug_XY2( 0, 0, szLogFormat); + + GLOBAL_NET_STATUS = param; + + switch(param) + { + case NSAPI_STATUS_LOCAL_UP: + { + logger.printf("NSAPI_STATUS_LOCAL_UP\r\n"); +// strcpy( szLogFormat, "ETH: Connection - LOCAL_UP "); +// TFT_Debug_XY2( 0, 0, szLogFormat); + break; + } + case NSAPI_STATUS_GLOBAL_UP: + { + logger.printf("NSAPI_STATUS_GLOBAL_UP\r\n"); + +/* + strcpy( szLogFormat, "ETH: Connection - GLOBAL_UP "); + TFT_Debug_XY2( 0, 0, szLogFormat); + + // Output the network address + const char *ip = gEth.get_ip_address(); + const char *netmask = gEth.get_netmask(); + const char *gateway = gEth.get_gateway(); + + TFT_Debug_XY_F2(0, 1, "IP address: %s\r\n", ip ? ip : "None"); + TFT_Debug_XY_F2(0, 2, "Netmask: %s\r\n", netmask ? netmask : "None"); + TFT_Debug_XY_F2(0, 3, "Gateway: %s\r\n", gateway ? gateway : "None"); +*/ + + break; + } + case NSAPI_STATUS_DISCONNECTED: + { + logger.printf("NSAPI_STATUS_DISCONNECTED\r\n"); +// strcpy( szLogFormat, "ETH: Connection - DISCONNECTED "); +// TFT_Debug_XY2( 0, 0, szLogFormat); + break; + } + case NSAPI_STATUS_CONNECTING: + { + logger.printf("NSAPI_STATUS_CONNECTING\r\n"); +// strcpy( szLogFormat, "ETH: Connection - CONNECTING "); +// TFT_Debug_XY2( 0, 0, szLogFormat); + break; + } + default: + { + logger.printf("NSAPI - default ?\r\n"); +// strcpy( szLogFormat, "ETH: Connection - ???? "); +// TFT_Debug_XY2( 0, 0, szLogFormat); + break; + } + } } // ----------------------------------------------------------- @@ -497,17 +530,23 @@ int retries = 10; int rc = 0; + //logger.printf("DeviceConnect()\r\n"); + while (retries--) { rc = gEth.connect(); if (rc == 0) { - // done +// logger.printf("DeviceConnect: Waiting\r\n"); +// wait(2); +// logger.printf("DeviceConnect: Done\r\n"); return true; } else { logger.printf("Connecting... ERR %d\r\n", rc); } + + wd.Service(); } logger.printf("Connecting... FAILED\r\n"); @@ -517,146 +556,72 @@ // ----------------------------------------------------------- // // ----------------------------------------------------------- -#ifndef CONNECTED_OFF static bool DeviceEthStartup() { - int rc = 0; - - // get ethernet up ! - if( !DeviceConnect() ) terminal_error_state(); - - // Output the network address - const char *ip = gEth.get_ip_address(); - const char *netmask = gEth.get_netmask(); - const char *gateway = gEth.get_gateway(); - logger.printf("IP address: %s\r\n", ip ? ip : "None"); - logger.printf("Netmask: %s\r\n", netmask ? netmask : "None"); - logger.printf("Gateway: %s\r\n", gateway ? gateway : "None"); - - // Output the network information to the TFT - if (page == 2) - { - int y = Terminal6x8[2] + 2; - TFT.set_font((unsigned char*) Terminal6x8); - TFT.locate(0, 1*y); - TFT.printf("IP address: %s\r\n", ip ? ip : "None"); - TFT.locate(0, 2*y); - TFT.printf("Netmask: %s\r\n", netmask ? netmask : "None"); - TFT.locate(0, 3*y); - TFT.printf("Gateway: %s\r\n", gateway ? gateway : "None"); - } - - // setup a UDP listener - _socket.set_blocking(true); - _socket.set_timeout(3000); - //_socket.set_broadcasting(true); - - // open the socket - rc = _socket.open((NetworkInterface*)&gEth); - if( rc != 0) - { - logger.printf("UDPSocket RECEIVER OPEN ERROR (rc=%d)\r\n", rc); - terminal_error_state(); - } - - // bind the XPL port - rc = _socket.bind(UDP_PORT); - logger.printf("Binding port %d\r\n", UDP_PORT); - if( rc != 0) - { - logger.printf("UDPSocket BIND ERROR (rc=%d)\r\n", rc); - terminal_error_state(); - } - - // open the sender socket - rc = _socketSender.open((NetworkInterface*)&gEth); - if( rc != 0) - { - logger.printf("UDPSocket SENDER OPEN ERROR (rc=%d)\r\n", rc); - terminal_error_state(); - } - //_socketSender.set_broadcasting(true); - + //logger.printf("DeviceEthStartup()\r\n"); + + // get ethernet up ! + if( !DeviceConnect() ) terminal_error_state(); + + // Output the network address + //const char *ip = gEth.get_ip_address(); + //const char *netmask = gEth.get_netmask(); + //const char *gateway = gEth.get_gateway(); + + //TFT_Debug_XY_F2(0, 1, "IP address: %s\r\n", ip ? ip : "None"); + //TFT_Debug_XY_F2(0, 2, "Netmask: %s\r\n", netmask ? netmask : "None"); + //TFT_Debug_XY_F2(0, 3, "Gateway: %s\r\n", gateway ? gateway : "None"); + return true; } -#endif // ----------------------------------------------------------- // // ----------------------------------------------------------- void SendUdPMessage(char *buffer) { - #ifndef CONNECTED_OFF - // send the UDP data as broadcast - int rc = _socketSender.sendto( "255.255.255.255", UDP_PORT, (const void *)buffer, strlen( buffer ) ); - - #ifdef DEBUG_OUTPUT_XPL - logger.printf("[SEND rc=%d] buffer[%d]\r\n%s\r\n", rc, strlen(buffer), buffer); - #endif - - #else - logger.printf("[OFFLINE] XPLsend [%d]\r\n%s\r\n", strlen(buffer), buffer); - #endif + // send the UDP data as broadcast + int rc = gEth_socket.sendto( "255.255.255.255", UDP_PORT, (const void *)buffer, strlen( buffer ) ); + //logger.printf("[SEND rc=%d] buffer[%d]\r\n%s\r\n", rc, strlen(buffer), buffer); } - -// ----------------------------------------------------------- -// -// ----------------------------------------------------------- -void SendXPLint(char *Device, int iCurrent ) -{ -// xPL_Message _message; -// _message.SetTarget( "*" ); -// _message.AddCommand("device", "xxxxx"); -// _message.AddCommand("current", "0"); -// xpl.SendMessage( &_message, true ); -} - - -// ----------------------------------------------------------- -// -// ----------------------------------------------------------- -void SendXPLString(char *Device, char *szCurrent ) -{ -// xPL_Message _message; -// _message.SetTarget( "*" ); -// _message.AddCommand("device", "xxxxx"); -// _message.AddCommand("current", "0"); -// xpl.SendMessage( &_message, true ); -} - - // ----------------------------------------------------------- // // ----------------------------------------------------------- void AfterParseAction(xPL_Message * message) { - char debugTxt[100]; - + char szLogFormat [100]; + // is this a message for me ? if (xpl.TargetIsMe(message)) - { - logger.printf("XPL: MSG IS FOR ME !"); - - logger.printf("%s\r\n", message->toString()); - - // is this a control message? - if (message->IsSchema("control", "basic")) - { - logger.printf("XPL: {control.basic}"); - } - } - - //logger.printf("XPL: [%s]\r\n", message->source.device_id ); - logger.printf("XPL: [%s] %d items\r\n", message->source.device_id, message->command_count ); - sprintf( debugTxt, "XPL: [%s] %d items\r\n", message->source.device_id, message->command_count ); - TFT_Debug( debugTxt ); + { + logger.printf("XPL: MSG IS FOR ME !"); + logger.printf("%s\r\n", message->toString()); - // do we have any data ??? maybe the parse failed.... - if( message->command_count == 0 ) - { + // is this a control message? + if (message->IsSchema("control", "basic")) + { + logger.printf("XPL: {control.basic}"); + } + } + + // Put it on the screen + sprintf( szLogFormat, "XPL: [%20s] %2d items ", message->source.device_id, message->command_count ); + TFT_Debug_XY2( 1, 18, szLogFormat); + + //logger.printf("XPL: [%s]\r\n", message->source.device_id ); + //logger.printf("XPL: [%s] %d items\r\n", message->source.device_id, message->command_count ); + //TFT_Debug_XY_F( 0, 18, "XPL: [%s] %d items ", message->source.device_id, message->command_count ); + //TFT_Debug_XY( 0, 19, "XPL: "); + + // do we have any data ??? maybe the parse failed.... + if( message->command_count == 0 ) + { // - logger.printf("XPL: no data?\r\n%s\r\n", in_buffer ); +// logger.printf("XPL: no data? [bytes: %d] \r\n%s\r\n", iRXBytes, gEth_socket_in_buffer ); + //TFT_Debug_XY( 0, 19, "XPL: ERROR - PARSE FAILED ?? "); + sprintf( szLogFormat, "XPL: ERROR - PARSE FAILED ?? "); + TFT_Debug_XY2( 1, 19, szLogFormat); } else { @@ -666,14 +631,15 @@ // grab each item and do something with it //logger.printf("%s=%s\r\n", message->command[i].name, message->command[i].value); } - logger.printf("XPL: %d items processed\r\n", message->command_count ); + //TFT_Debug_XY_F( 0, 19, "XPL: %d items processed ", message->command_count ); + sprintf( szLogFormat, "XPL: %d items processed ", message->command_count ); + TFT_Debug_XY2( 1, 19, szLogFormat); } // output the full msg! //logger.printf("%s\r\n", message->toString()); } - // ----------------------------------------------------------- // // ----------------------------------------------------------- @@ -681,59 +647,86 @@ { SocketAddress _sAddr; int iNoDataCount = 0; + int rc; + int iDataMsgCount = 0; + + // setup a listener + gEth_socket.set_blocking(true); + gEth_socket.set_timeout(3000); + + // open the socket + rc = gEth_socket.open((NetworkInterface*)&gEth); + if( rc != 0) + { + logger.printf("UDPSocket RECEIVER OPEN ERROR (rc=%d)\r\n", rc); + terminal_error_state(); + } + + // bind the port + rc = gEth_socket.bind(UDP_PORT); + logger.printf("Binding port %d\r\n", UDP_PORT); + if( rc != 0) + { + logger.printf("UDPSocket BIND ERROR (rc=%d)\r\n", rc); + terminal_error_state(); + } // loop forever while(1) { // receive from the udp socket - led2 = 0; - #ifndef CONNECTED_OFF - int n = _socket.recvfrom(&_sAddr, in_buffer, UDP_BUFFER_SIZE); - //led2 = 1; - - // see what weve got ! - if (n == NSAPI_ERROR_WOULD_BLOCK) - { - // timeout with no data... - ++iNoDataCount; - //logger.printf("-- NoData (TIMEOUT)\r\n"); - led2 = 0; - } - else if (n < 0) - { - // some other error - ++iNoDataCount; - //logger.printf("-- NoData (ERR) %i\r\n", n); - led2 = 0; - } - else if( n > 0) - { - // good data :) - iNoDataCount = 0; - in_buffer[n] = '\0'; - // logger.printf("[%d bytes] %s:%d\r\n", n, _sAddr.get_ip_address(), _sAddr.get_port()); - // logger.printf("%s\r\n", in_buffer); - led2 = 1; - - // try and parse the msg - xpl.ParseInputMessage(in_buffer); - } - - // have we stopped receiving data ?!?!? - // roughly 100*100ms = 10s... - if( iNoDataCount > 100 ) - { - logger.printf("ERROR: No UDP data repeatidly...\r\n"); - terminal_error_state(); - } - #endif + TFT_Debug_XY_F2(1, 15, "RX: Attempt (%2d) ", iNoDataCount); + + iRXBytes = gEth_socket.recvfrom(&_sAddr, gEth_socket_in_buffer, UDP_BUFFER_SIZE); + + TFT_Debug_XY2(20, 15, "-DONE-"); + + // see what weve got ! + if (iRXBytes == NSAPI_ERROR_WOULD_BLOCK) + { + led3 = 1; // error + + // timeout with no data... + ++iNoDataCount; + TFT_Debug_XY2(1, 16, "RX: NoData (TIMEOUT) "); + + //screen_Debug(); + } + else if (iRXBytes < 0) + { + led3 = 1; // error + + // some other error + ++iNoDataCount; + TFT_Debug_XY2(1, 16, "RX: NoData (ERR) "); + } + else if( iRXBytes > 0) + { + led3 = 0; // no error + led2 = 1; + + // good data :) + iNoDataCount = 0; + gEth_socket_in_buffer[iRXBytes] = '\0'; + TFT_Debug_XY_F2(1, 16, "RX: [%d bytes] %s:%d ", iRXBytes, _sAddr.get_ip_address(), _sAddr.get_port()); + + // try and parse the msg + xpl.ParseInputMessage(gEth_socket_in_buffer); + + TFT_Debug_XY_F2(1, 26, "RX: DATA(%d)", iDataMsgCount++); + led2 = 0; + } + + // have we stopped receiving data ?!?!? + if( iNoDataCount > 60 ) // about 3 mins (60*3secs) of no data + { + // go for a reboot... + terminal_error_state(); + } // stroke the WD - wd.Service(); - - // sleep for 100ms - ThisThread::sleep_for(100); + wd.Service(); } } @@ -741,38 +734,32 @@ // ----------------------------------------------------------- // // ----------------------------------------------------------- -Thread thread1; -int main() -{ - const char *ip; - char xpl_ip[16]; - int iAppLoop=0; - point p; - int prevPage = 99 ; - bool waitTouch = false ; - - // FIRST THINGS FIRST!!! set the WD timeout interval... - // enough to get up and running! - // 1000seconds to get connected and receive an IP address - wd.Configure(1000.0); - - // setup the pc serial logger - logger.baud(115200); - logger.printf("\r\n\r\n<<<<<<<<< Basic UDP XPL Listener >>>>>>>>>>\r\n"); - - // Fireup the TFT Screen - initTFT() ; - printf("TFT Started {width = %d, height = %d}\n\r", TFT.width(), TFT.height()) ; - //calibrate(); - screen3(); - prevPage = 2; - - // turn all the LEDs off - led1 = 0; // green = normal startup - led2 = 0; // blue = ethernet data received - led3 = 0; // red = watchdog startup - led4 = 0; // ?? - +Thread thread1(osPriorityAboveNormal, 8 * 1024); // OS_STACK_SIZE in theory 8k +int main() + { + const char *ip; + char xpl_ip[20]; + int prev_net_status=0; + + point p; + + bool waitTouch = false ; + int iAppLoop=0; + int iHBSentCount = 0; + + // FIRST THINGS FIRST!!! set the WD timeout interval... + // 100 seconds to get connected and receive an IP address + wd.Configure(100000.0); + + // open pc serial + logger.baud(115200); + logger.printf("---- ETHERNET UDP Test ----\r\n"); + + // turn all the LEDs off + led1 = 0; // green = normal startup + led2 = 0; // blue = ethernet data received + led3 = 0; // red = watchdog startup + // WatchDog Startup Information logger.printf("\r\n------------------------------------------------------\r\n" ); logger.printf("[SystemCoreClock] %d Hz\r\n", SystemCoreClock); @@ -801,69 +788,77 @@ led3 = 0; } logger.printf("------------------------------------------------------\r\n\r\n"); - - #ifndef CONNECTED_OFF - // Fireup the Ethernet - DeviceEthStartup(); - - // process the devices ip adress into the XPL instance ID - ip = gEth.get_ip_address(); - strcpy( xpl_ip, ip ); - UTILS_strip_char_in_string( xpl_ip, '.' ); - #endif + + // Fireup the TFT Screen + initTFT() ; + //logger.printf("TFT Started {width = %d, height = %d}\n\r", TFT.width(), TFT.height()) ; + //calibrate(); + //screen_Debug(); + screen1(); + + // fire up the ethernet & the receive socket + gEth.attach(&status_callback); + DeviceEthStartup(); - // now set the WD to something to run with - between receving data + // just wait for everything to settle + logger.printf("Network."); + while( GLOBAL_NET_STATUS != NSAPI_STATUS_GLOBAL_UP ) + { + logger.printf("."); + wait(1); + } + logger.printf("UP!\r\n"); + + // stroke the wd - all ok so far ! wd.Service(); - wd.Configure(300.0); // 300s + + screen_Debug(); + + // 10 second is enough to make sure everything is ok - UDP read is every 3 seconds + wd.Configure(10000.0); + // process the devices ip adress into the XPL instance ID + ip = gEth.get_ip_address(); + strcpy( xpl_ip, ip ); + UTILS_strip_char_in_string( xpl_ip, '.' ); + // setup xpl xpl.SendExternal = &SendUdPMessage; // pointer to the send callback xpl.AfterParseAction = &AfterParseAction; // pointer to a post parsing action callback xpl.SetSource( "NIHH", XPL_NAME, xpl_ip); // parameters for hearbeat message - + // kick off the XPL receiver thread to run in the background - //Thread thread1(Receiver_Thread, NULL, osPriorityNormal); //, DEFAULT_STACK_SIZE); //osPriorityHigh - thread1.set_priority(osPriorityNormal); +// thread1.set_priority(osPriorityAboveNormal); // osPriorityNormal osPriorityHigh osPriorityBelowNormal osPriorityAboveNormal thread1.start( Receiver_Thread ); - - #ifndef CONNECTED_OFF - // i like to do this on startup to announce ourself - logger.printf("XPL: STARTED {VendorID:%s, DeviceID:%s, InstanceID:%s}\n\r", "NIHH", XPL_NAME, xpl_ip ); - xpl.SendHBeat(); - #endif - - // start running the main processing stuff here - while (true) - { - // page details... - printf("page %d of %d\r\n", page, numPage ); - switch(page) { - case 0: - if (prevPage != page) { - screen1() ; - } - waitTouch = true ; - break ; - case 1: - if (prevPage != page) { - screen2() ; - } - waitTouch = true ; - break ; - case 2: - if (prevPage != page) { - screen3() ; - } - waitTouch = true ; - break ; - default: - page = 0 ; - break ; - } - prevPage = page ; - - + + + // calibrate the screen + //calibrate(); + + + // start running the main processing stuff here + while (true) + { + //logger.printf(".\n\r") ; + + //TFT_Debug_XY2( 5, 10, "Hello world..." ); + + // pause for a mo ! + //wait(5); + + do { + led1 = !led1; + + if( GLOBAL_NET_STATUS != prev_net_status ) + { + TFT_Debug_XY_F2(0, 0, "NET: %d", GLOBAL_NET_STATUS); + logger.printf("net..\n\r") ; + + prev_net_status = GLOBAL_NET_STATUS; + } + + // allow xpl to do its thing... #ifndef CONNECTED_OFF xpl.Process(); @@ -871,22 +866,17 @@ if( getPixel(p) ) { - printf("TFT Touch x = %d, y = %d\n\r", p.x, p.y) ; + logger.printf("TFT Touch x = %d, y = %d\n\r", p.x, p.y) ; + waitTouch = false ; + wait(.2); // stop - if (p.x < 100) - { // left - decPage() ; - } - else if (p.x > 150) - { // right - incPage() ; - } - waitTouch = false ; + backlight = !backlight; + } - // SIMPLE TEST - every 50 loops send a test XPL msg + // every X loops send a test XPL msg ++iAppLoop; - if( iAppLoop >= 10000 ) + if( iAppLoop >= 50 ) { xPL_Message _message; @@ -909,15 +899,18 @@ xpl.SendMessage( (xPL_Message *)&_message, true ); logger.printf("<<<<<< Sample Message Sent >>>>>>\r\n"); + TFT_Debug_XY_F2(1, 27, "TX: HB(%d)", iHBSentCount++); // reset iAppLoop = 0; } - // pause for a mo ! + // pause for a sec ! ThisThread::sleep_for(100); - + } while(waitTouch != false) ; - - } -} + + } + + } +