app5

Dependencies:   TFTv2 nihh-utils xpl

Committer:
richnash
Date:
Sun Oct 28 11:38:24 2018 +0000
Revision:
3:f96822b3edb5
Parent:
2:1335510cfb92
Child:
4:ed0665b5d253
good base

Who changed what in which revision?

UserRevisionLine numberNew contents of line
richnash 0:9094a94896f6 1
richnash 0:9094a94896f6 2 // ##################################################################################
richnash 0:9094a94896f6 3 // XPL with TFT Screen Application
richnash 0:9094a94896f6 4 // ##################################################################################
richnash 0:9094a94896f6 5
richnash 3:f96822b3edb5 6 // if defined this will echo all debug txt going to the TFT onto the serial port
richnash 3:f96822b3edb5 7 //#define TFT_ECHO
richnash 3:f96822b3edb5 8 int _TFT_ECHO = 0;
richnash 0:9094a94896f6 9
richnash 0:9094a94896f6 10 // turn OFF dependency on being connected to ethernet
richnash 3:f96822b3edb5 11 //#define ETH_OFF
richnash 0:9094a94896f6 12
richnash 3:f96822b3edb5 13
richnash 0:9094a94896f6 14 // MBED Headers
richnash 0:9094a94896f6 15 #include "mbed.h"
richnash 0:9094a94896f6 16 #include "EthernetInterface.h"
richnash 3:f96822b3edb5 17 #include "Watchdog.h"
richnash 3:f96822b3edb5 18 #include "rtos.h"
richnash 0:9094a94896f6 19
richnash 0:9094a94896f6 20 // -----------------------------------------------------------
richnash 0:9094a94896f6 21 // Graphics Headers
richnash 0:9094a94896f6 22 #include "ILI9341.h"
richnash 0:9094a94896f6 23 #include "SPI_STMPE610.h"
richnash 0:9094a94896f6 24 #include "Arial12x12.h"
richnash 0:9094a94896f6 25 #include "Arial24x23.h"
richnash 0:9094a94896f6 26 #include "Arial28x28.h"
richnash 0:9094a94896f6 27 #include "Arial43x48_numb.h"
richnash 0:9094a94896f6 28
richnash 0:9094a94896f6 29 // -----------------------------------------------------------
richnash 0:9094a94896f6 30 // My Headers
richnash 0:9094a94896f6 31 #include "Watchdog.h"
richnash 0:9094a94896f6 32 #include "xPL.h"
richnash 0:9094a94896f6 33 #include "nihh_utils.h"
richnash 0:9094a94896f6 34
richnash 0:9094a94896f6 35 // -----------------------------------------------------------
richnash 0:9094a94896f6 36 // XPL defines
richnash 0:9094a94896f6 37 #define UDP_PORT 3865
richnash 0:9094a94896f6 38 #define UDP_BUFFER_SIZE 500
richnash 0:9094a94896f6 39
richnash 0:9094a94896f6 40 #define XPL_NAME "NUCLEO1"
richnash 0:9094a94896f6 41
richnash 0:9094a94896f6 42 // -----------------------------------------------------------
richnash 0:9094a94896f6 43 // global variables
richnash 0:9094a94896f6 44 Watchdog wd;
richnash 0:9094a94896f6 45 xPL xpl;
richnash 0:9094a94896f6 46
richnash 2:1335510cfb92 47 DigitalOut led1(LED1); // green = normal startup
richnash 2:1335510cfb92 48 DigitalOut led2(LED2); // blue = ethernet data received
richnash 2:1335510cfb92 49 DigitalOut led3(LED3); // red = watchdog startup
richnash 0:9094a94896f6 50
richnash 0:9094a94896f6 51 Serial logger(SERIAL_TX, SERIAL_RX);
richnash 0:9094a94896f6 52
richnash 2:1335510cfb92 53 // =====================================
richnash 2:1335510cfb92 54
richnash 0:9094a94896f6 55 EthernetInterface gEth;
richnash 3:f96822b3edb5 56 UDPSocket gEth_socket;
richnash 3:f96822b3edb5 57 char gEth_socket_in_buffer[UDP_BUFFER_SIZE];
richnash 3:f96822b3edb5 58
richnash 3:f96822b3edb5 59 //char in_buffer[UDP_BUFFER_SIZE+1];
richnash 3:f96822b3edb5 60 int iRXBytes;
richnash 3:f96822b3edb5 61 int GLOBAL_NET_STATUS = 0;
richnash 3:f96822b3edb5 62
richnash 3:f96822b3edb5 63 SocketAddress _sAddr;
richnash 3:f96822b3edb5 64
richnash 0:9094a94896f6 65
richnash 2:1335510cfb92 66 // =====================================
richnash 0:9094a94896f6 67
richnash 3:f96822b3edb5 68
richnash 0:9094a94896f6 69 #define PIN_RESET_TFT PC_13 /* place holder */
richnash 0:9094a94896f6 70
richnash 0:9094a94896f6 71 #define PIN_MOSI D11
richnash 0:9094a94896f6 72 #define PIN_MISO D12
richnash 0:9094a94896f6 73 #define PIN_SCLK D13
richnash 0:9094a94896f6 74 #define PIN_CS_TFT D5
richnash 0:9094a94896f6 75 #define PIN_DC_TFT D6
richnash 0:9094a94896f6 76 #define PIN_BL_TFT D7
richnash 0:9094a94896f6 77 #define PIN_CS_SD D4
richnash 0:9094a94896f6 78
richnash 0:9094a94896f6 79 #define PIN_XP A2
richnash 0:9094a94896f6 80 #define PIN_XM A0
richnash 0:9094a94896f6 81 #define PIN_YP A3
richnash 0:9094a94896f6 82 #define PIN_YM A1
richnash 0:9094a94896f6 83
richnash 0:9094a94896f6 84 struct point {
richnash 0:9094a94896f6 85 int x;
richnash 0:9094a94896f6 86 int y;
richnash 0:9094a94896f6 87 };
richnash 0:9094a94896f6 88
richnash 0:9094a94896f6 89 void calibrate(void);
richnash 0:9094a94896f6 90 bool getPixel(point& p);
richnash 0:9094a94896f6 91 point toPixel(point p);
richnash 0:9094a94896f6 92
richnash 0:9094a94896f6 93 typedef enum { YES, MAYBE, NO } TOUCH;
richnash 0:9094a94896f6 94
richnash 0:9094a94896f6 95 TOUCH getTouch(point& p);
richnash 0:9094a94896f6 96
richnash 0:9094a94896f6 97 int readTouch(PinName p, PinName m, PinName a, PinName i);
richnash 0:9094a94896f6 98
richnash 0:9094a94896f6 99 int x_off = 22071;
richnash 0:9094a94896f6 100 int y_off = 18707;
richnash 0:9094a94896f6 101 int pp_tx = 373;
richnash 0:9094a94896f6 102 int pp_ty = 297;
richnash 0:9094a94896f6 103
richnash 0:9094a94896f6 104 DigitalOut backlight(PIN_BL_TFT) ;
richnash 0:9094a94896f6 105
richnash 0:9094a94896f6 106 ILI9341 TFT(SPI_8, 10000000, PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, PIN_RESET_TFT, PIN_DC_TFT, "Seeed2.8") ;
richnash 0:9094a94896f6 107
richnash 3:f96822b3edb5 108 // which screen page are we on
richnash 3:f96822b3edb5 109 int page = 0 ; // 0 is the special debug screen
richnash 3:f96822b3edb5 110
richnash 3:f96822b3edb5 111
richnash 3:f96822b3edb5 112 // =====================================
richnash 3:f96822b3edb5 113
richnash 3:f96822b3edb5 114 Mutex mutex_TFT;
richnash 3:f96822b3edb5 115
richnash 3:f96822b3edb5 116 void TFT_Debug_XY_F ( int, int, char *, ... );
richnash 3:f96822b3edb5 117
richnash 3:f96822b3edb5 118 // =====================================
richnash 3:f96822b3edb5 119
richnash 3:f96822b3edb5 120
richnash 3:f96822b3edb5 121 // -----------------------------------------------------------
richnash 3:f96822b3edb5 122 //
richnash 3:f96822b3edb5 123 // -----------------------------------------------------------
richnash 3:f96822b3edb5 124 void terminal_error_state( void )
richnash 3:f96822b3edb5 125 {
richnash 3:f96822b3edb5 126 led1=0;
richnash 3:f96822b3edb5 127 led2=0;
richnash 3:f96822b3edb5 128 led3=0;
richnash 3:f96822b3edb5 129
richnash 3:f96822b3edb5 130 logger.printf("\r\n\r\nJUST TO BE CLEAR - THIS IS BAD BAD NEWS !!! \r\n");
richnash 3:f96822b3edb5 131 while( 1 )
richnash 3:f96822b3edb5 132 {
richnash 3:f96822b3edb5 133 led1 = !led1;
richnash 3:f96822b3edb5 134 led2 = !led2;
richnash 3:f96822b3edb5 135 led3 = !led3;
richnash 3:f96822b3edb5 136 wait(0.5);
richnash 3:f96822b3edb5 137
richnash 3:f96822b3edb5 138 // Just waiting for the watchdog to kill us
richnash 3:f96822b3edb5 139 }
richnash 3:f96822b3edb5 140 }
richnash 0:9094a94896f6 141
richnash 0:9094a94896f6 142
richnash 0:9094a94896f6 143 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
richnash 0:9094a94896f6 144 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
richnash 0:9094a94896f6 145 // ++++++++ TFT SCREEN FUNCTIONS
richnash 0:9094a94896f6 146 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
richnash 0:9094a94896f6 147 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
richnash 0:9094a94896f6 148
richnash 0:9094a94896f6 149 int readTouch(PinName p, PinName m, PinName a, PinName i)
richnash 0:9094a94896f6 150 {
richnash 0:9094a94896f6 151 DigitalOut _p(p);
richnash 0:9094a94896f6 152 _p = 1;
richnash 0:9094a94896f6 153 DigitalOut _m(m);
richnash 0:9094a94896f6 154 _m = 0;
richnash 0:9094a94896f6 155 AnalogIn _a(a);
richnash 0:9094a94896f6 156 AnalogIn _i(i); // this pin has to be high Z (DigitalIn may also work)
richnash 0:9094a94896f6 157 wait_us(10);
richnash 0:9094a94896f6 158 return _a.read_u16();
richnash 0:9094a94896f6 159 }
richnash 0:9094a94896f6 160
richnash 0:9094a94896f6 161 TOUCH getTouch(point& p)
richnash 0:9094a94896f6 162 {
richnash 0:9094a94896f6 163 int y2 = readTouch(PIN_XP,PIN_XM,PIN_YP,PIN_YM);
richnash 0:9094a94896f6 164 int x2 = readTouch(PIN_YP,PIN_YM,PIN_XP,PIN_XM);
richnash 0:9094a94896f6 165 int y1 = readTouch(PIN_XP,PIN_XM,PIN_YP,PIN_YM);
richnash 0:9094a94896f6 166 int x1 = readTouch(PIN_YP,PIN_YM,PIN_XP,PIN_XM);
richnash 0:9094a94896f6 167 int xd = x1 - x2;
richnash 0:9094a94896f6 168 int yd = y1 - y2;
richnash 0:9094a94896f6 169 xd = (xd > 0) ? xd : -xd;
richnash 0:9094a94896f6 170 yd = (yd > 0) ? xd : -xd;
richnash 0:9094a94896f6 171 p.x = x1 + x2;
richnash 0:9094a94896f6 172 p.y = y1 + y2;
richnash 0:9094a94896f6 173
richnash 0:9094a94896f6 174 const int th = 8000;
richnash 0:9094a94896f6 175 const int df = 100;
richnash 0:9094a94896f6 176 TOUCH touch;
richnash 0:9094a94896f6 177 if (x1 < th || x2 < th ||
richnash 0:9094a94896f6 178 y1 < th || y2 < th) {
richnash 0:9094a94896f6 179 p.x = 0;
richnash 0:9094a94896f6 180 p.y = 0;
richnash 0:9094a94896f6 181 touch = NO;
richnash 0:9094a94896f6 182 } else if (xd > df || yd > df) {
richnash 0:9094a94896f6 183 touch = MAYBE;
richnash 0:9094a94896f6 184 } else {
richnash 0:9094a94896f6 185 touch = YES;
richnash 0:9094a94896f6 186 }
richnash 0:9094a94896f6 187 //TFT.locate(0,50);
richnash 0:9094a94896f6 188 //TFT.printf("x: %6i y: %6i",p.x,p.y);
richnash 0:9094a94896f6 189 return touch;
richnash 0:9094a94896f6 190 }
richnash 0:9094a94896f6 191
richnash 3:f96822b3edb5 192 point toPixel(point p)
richnash 3:f96822b3edb5 193 {
richnash 3:f96822b3edb5 194 p.x -= x_off;
richnash 3:f96822b3edb5 195 p.x /= pp_tx;
richnash 3:f96822b3edb5 196 int w = TFT.width();
richnash 3:f96822b3edb5 197 if (p.x > w) p.x = w;
richnash 3:f96822b3edb5 198 if (p.x < 0) p.x = 0;
richnash 3:f96822b3edb5 199 p.y -= y_off;
richnash 3:f96822b3edb5 200 p.y /= pp_ty;
richnash 3:f96822b3edb5 201 int h = TFT.height();
richnash 3:f96822b3edb5 202 if (p.y > h) p.y = h;
richnash 3:f96822b3edb5 203 if (p.y < 0) p.y = 0;
richnash 3:f96822b3edb5 204 return (p);
richnash 3:f96822b3edb5 205 }
richnash 3:f96822b3edb5 206
richnash 3:f96822b3edb5 207 bool getPixel(point& p)
richnash 3:f96822b3edb5 208 {
richnash 3:f96822b3edb5 209 TOUCH touch = getTouch(p);
richnash 3:f96822b3edb5 210 p = toPixel(p);
richnash 3:f96822b3edb5 211 return touch == YES;
richnash 3:f96822b3edb5 212 }
richnash 3:f96822b3edb5 213
richnash 3:f96822b3edb5 214 // -----------------------------------------------------------
richnash 3:f96822b3edb5 215 //
richnash 3:f96822b3edb5 216 // -----------------------------------------------------------
richnash 3:f96822b3edb5 217 void initTFT(void)
richnash 3:f96822b3edb5 218 {
richnash 3:f96822b3edb5 219 //Configure the display driver
richnash 3:f96822b3edb5 220 //TFT.BusEnable(true) ;
richnash 3:f96822b3edb5 221 TFT.FastWindow(true) ;
richnash 3:f96822b3edb5 222 TFT.background(Black);
richnash 3:f96822b3edb5 223 TFT.foreground(White);
richnash 3:f96822b3edb5 224 wait(0.01) ;
richnash 3:f96822b3edb5 225 TFT.cls();
richnash 3:f96822b3edb5 226 //TFT.BusEnable(false) ;
richnash 3:f96822b3edb5 227 }
richnash 3:f96822b3edb5 228
richnash 3:f96822b3edb5 229 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
richnash 3:f96822b3edb5 230 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
richnash 3:f96822b3edb5 231
richnash 3:f96822b3edb5 232
richnash 3:f96822b3edb5 233 void screen1(void) // Welcome Screen
richnash 3:f96822b3edb5 234 {
richnash 3:f96822b3edb5 235 page = 1;
richnash 3:f96822b3edb5 236
richnash 3:f96822b3edb5 237 //TFT.BusEnable(true) ;
richnash 3:f96822b3edb5 238 backlight = 0 ;
richnash 3:f96822b3edb5 239 TFT.background(White) ;
richnash 3:f96822b3edb5 240 wait(0.1) ;
richnash 3:f96822b3edb5 241 TFT.cls() ;
richnash 3:f96822b3edb5 242 wait(0.1) ;
richnash 3:f96822b3edb5 243
richnash 3:f96822b3edb5 244 TFT.set_font((unsigned char*) Arial24x23);
richnash 3:f96822b3edb5 245 TFT.foreground(Red) ;
richnash 3:f96822b3edb5 246 TFT.locate(20, 40) ;
richnash 3:f96822b3edb5 247 TFT.printf("NASHLAND") ;
richnash 3:f96822b3edb5 248 TFT.foreground(Blue);
richnash 3:f96822b3edb5 249 TFT.locate(20, 80) ;
richnash 3:f96822b3edb5 250 TFT.printf("EMBEDDED") ;
richnash 3:f96822b3edb5 251 TFT.locate(20, 120) ;
richnash 3:f96822b3edb5 252 TFT.printf("CONTROLLER") ;
richnash 3:f96822b3edb5 253
richnash 3:f96822b3edb5 254 TFT.foreground(Black);
richnash 3:f96822b3edb5 255 TFT.set_font((unsigned char*) Arial12x12);
richnash 3:f96822b3edb5 256 TFT.foreground(Blue) ;
richnash 3:f96822b3edb5 257 TFT.locate(30, 180) ;
richnash 3:f96822b3edb5 258 TFT.printf("This program is running on a") ;
richnash 3:f96822b3edb5 259 TFT.locate(30, 200) ;
richnash 3:f96822b3edb5 260 TFT.printf("ST Nucleo 144 Controller") ;
richnash 3:f96822b3edb5 261 TFT.locate(30, 220) ;
richnash 3:f96822b3edb5 262 TFT.printf("running mbed.os") ;
richnash 3:f96822b3edb5 263
richnash 3:f96822b3edb5 264 //TFT.BusEnable(false) ;
richnash 3:f96822b3edb5 265 backlight = 1 ;
richnash 3:f96822b3edb5 266 }
richnash 3:f96822b3edb5 267
richnash 3:f96822b3edb5 268 void screen_Debug(void)
richnash 3:f96822b3edb5 269 {
richnash 3:f96822b3edb5 270 int y = Terminal6x8[2] + 2;
richnash 3:f96822b3edb5 271
richnash 3:f96822b3edb5 272 mutex_TFT.lock();
richnash 3:f96822b3edb5 273
richnash 3:f96822b3edb5 274 page = 0;
richnash 3:f96822b3edb5 275
richnash 3:f96822b3edb5 276 TFT.set_font((unsigned char*) Terminal6x8);
richnash 3:f96822b3edb5 277
richnash 3:f96822b3edb5 278 //Draw some text
richnash 3:f96822b3edb5 279 backlight = 0 ;
richnash 3:f96822b3edb5 280
richnash 3:f96822b3edb5 281 TFT.background(Black);
richnash 3:f96822b3edb5 282 TFT.foreground(White);
richnash 3:f96822b3edb5 283 wait(0.1) ;
richnash 3:f96822b3edb5 284 TFT.cls() ;
richnash 3:f96822b3edb5 285 wait(0.1) ;
richnash 3:f96822b3edb5 286 TFT.cls() ;
richnash 3:f96822b3edb5 287
richnash 3:f96822b3edb5 288 backlight = 1 ;
richnash 3:f96822b3edb5 289
richnash 3:f96822b3edb5 290 {
richnash 3:f96822b3edb5 291 // Output the network information
richnash 3:f96822b3edb5 292 const char *ip = gEth.get_ip_address();
richnash 3:f96822b3edb5 293 const char *netmask = gEth.get_netmask();
richnash 3:f96822b3edb5 294 const char *gateway = gEth.get_gateway();
richnash 3:f96822b3edb5 295 TFT.locate(0, 1*y);
richnash 3:f96822b3edb5 296 TFT.printf("IP address: %s\r\n", ip ? ip : "None");
richnash 3:f96822b3edb5 297 TFT.locate(0, 2*y);
richnash 3:f96822b3edb5 298 TFT.printf("Netmask: %s\r\n", netmask ? netmask : "None");
richnash 3:f96822b3edb5 299 TFT.locate(0, 3*y);
richnash 3:f96822b3edb5 300 TFT.printf("Gateway: %s\r\n", gateway ? gateway : "None");
richnash 3:f96822b3edb5 301 }
richnash 3:f96822b3edb5 302
richnash 3:f96822b3edb5 303 mutex_TFT.unlock();
richnash 3:f96822b3edb5 304 }
richnash 3:f96822b3edb5 305
richnash 3:f96822b3edb5 306
richnash 0:9094a94896f6 307 void calibrate(void)
richnash 0:9094a94896f6 308 {
richnash 0:9094a94896f6 309 int i;
richnash 0:9094a94896f6 310 int a = 0,b = 0,c = 0, d = 0;
richnash 0:9094a94896f6 311 int pos_x = 0, pos_y = 0;
richnash 0:9094a94896f6 312 point p;
richnash 0:9094a94896f6 313
richnash 3:f96822b3edb5 314 page = 99;
richnash 3:f96822b3edb5 315
richnash 0:9094a94896f6 316 backlight = 0 ;
richnash 0:9094a94896f6 317 //TFT.BusEnable(true) ;
richnash 0:9094a94896f6 318
richnash 0:9094a94896f6 319 TFT.background(Black);
richnash 0:9094a94896f6 320 wait(0.1) ;
richnash 0:9094a94896f6 321 TFT.foreground(White);
richnash 0:9094a94896f6 322 wait(0.1) ;
richnash 0:9094a94896f6 323 TFT.cls() ;
richnash 0:9094a94896f6 324 wait(0.1) ;
richnash 0:9094a94896f6 325 TFT.set_font((unsigned char*) Arial12x12);
richnash 0:9094a94896f6 326 TFT.locate(90,0);
richnash 0:9094a94896f6 327 TFT.printf("Graphics");
richnash 0:9094a94896f6 328
richnash 0:9094a94896f6 329 TFT.line(0,3,6,3,White);
richnash 0:9094a94896f6 330 TFT.line(3,0,3,6,White);
richnash 0:9094a94896f6 331
richnash 0:9094a94896f6 332 backlight = 1 ;
richnash 0:9094a94896f6 333
richnash 0:9094a94896f6 334 //if (font)
richnash 0:9094a94896f6 335 //{
richnash 0:9094a94896f6 336 // get the center of the screen
richnash 0:9094a94896f6 337 pos_x = TFT.columns() / 2 - 3;
richnash 0:9094a94896f6 338 pos_x = pos_x * Arial12x12[1];
richnash 0:9094a94896f6 339 pos_y = (TFT.rows() / 2) - 1;
richnash 0:9094a94896f6 340 pos_y = pos_y * Arial12x12[2];
richnash 0:9094a94896f6 341 TFT.locate(pos_x,pos_y);
richnash 0:9094a94896f6 342 TFT.printf("press cross ");
richnash 0:9094a94896f6 343 TFT.locate(pos_x,pos_y + Arial12x12[2]);
richnash 0:9094a94896f6 344 TFT.printf("to calibrate ");
richnash 0:9094a94896f6 345 //}
richnash 0:9094a94896f6 346
richnash 0:9094a94896f6 347 for (i=0; i<5; i++) {
richnash 0:9094a94896f6 348 while (getTouch(p) != YES)
richnash 0:9094a94896f6 349 /*nothing*/;
richnash 0:9094a94896f6 350 a += p.x;
richnash 0:9094a94896f6 351 b += p.y;
richnash 0:9094a94896f6 352 }
richnash 0:9094a94896f6 353 a = a / 5;
richnash 0:9094a94896f6 354 b = b / 5;
richnash 0:9094a94896f6 355 //if (font)
richnash 0:9094a94896f6 356 //{
richnash 0:9094a94896f6 357 TFT.locate(pos_x,pos_y);
richnash 0:9094a94896f6 358 TFT.printf("ok ");
richnash 0:9094a94896f6 359 TFT.locate(pos_x,pos_y + Arial12x12[2]);
richnash 0:9094a94896f6 360 TFT.printf("release touch ");
richnash 0:9094a94896f6 361 //}
richnash 0:9094a94896f6 362 while (getTouch(p) != NO)
richnash 0:9094a94896f6 363 /*nothing*/;
richnash 0:9094a94896f6 364
richnash 0:9094a94896f6 365 TFT.cls();
richnash 0:9094a94896f6 366 TFT.line(TFT.width() -5, TFT.height() - 8,TFT.width() - 5,TFT.height() -1,White); // paint cross
richnash 0:9094a94896f6 367 TFT.line(TFT.width() - 8,TFT.height() - 5,TFT.width() - 1,TFT.height() - 5,White);
richnash 0:9094a94896f6 368 //if (font)
richnash 0:9094a94896f6 369 //{
richnash 0:9094a94896f6 370 TFT.locate(pos_x,pos_y);
richnash 0:9094a94896f6 371 TFT.printf("press cross ");
richnash 0:9094a94896f6 372 TFT.locate(pos_x,pos_y + Arial12x12[2]);
richnash 0:9094a94896f6 373 TFT.printf("to calibrate ");
richnash 0:9094a94896f6 374 //}
richnash 0:9094a94896f6 375 for (i=0; i<5; i++) {
richnash 0:9094a94896f6 376 while (getTouch(p) != YES)
richnash 0:9094a94896f6 377 /*nothing*/;
richnash 0:9094a94896f6 378 c+= p.x;
richnash 0:9094a94896f6 379 d+= p.y;
richnash 0:9094a94896f6 380 }
richnash 0:9094a94896f6 381 c = c / 5;
richnash 0:9094a94896f6 382 d = d / 5;
richnash 0:9094a94896f6 383 x_off = a;
richnash 0:9094a94896f6 384 y_off = b;
richnash 0:9094a94896f6 385 i = c-a; // delta x
richnash 0:9094a94896f6 386 pp_tx = i / (TFT.width() - 6);
richnash 0:9094a94896f6 387 i = d-b; // delta y
richnash 0:9094a94896f6 388 pp_ty = i / (TFT.height() - 6);
richnash 0:9094a94896f6 389 //if (font)
richnash 0:9094a94896f6 390 //{
richnash 0:9094a94896f6 391 TFT.locate(pos_x,pos_y);
richnash 0:9094a94896f6 392 TFT.printf("Calibrated ");
richnash 0:9094a94896f6 393 TFT.locate(pos_x,pos_y + Arial12x12[2]);
richnash 0:9094a94896f6 394 TFT.printf("x %6i %4i", x_off, pp_tx);
richnash 0:9094a94896f6 395 TFT.locate(pos_x,pos_y + 2*Arial12x12[2]);
richnash 0:9094a94896f6 396 TFT.printf("y %6i %4i", y_off, pp_ty);
richnash 0:9094a94896f6 397 //}
richnash 0:9094a94896f6 398 while (getTouch(p) != NO)
richnash 0:9094a94896f6 399 /*nothing*/;
richnash 0:9094a94896f6 400 TFT.cls();
richnash 0:9094a94896f6 401
richnash 0:9094a94896f6 402 //TFT.BusEnable(false) ;
richnash 0:9094a94896f6 403
richnash 0:9094a94896f6 404 printf("x_off:%6i pp_tx:%4i \n\r", x_off, pp_tx);
richnash 0:9094a94896f6 405 printf("y_off:%6i pp_ty:%4i \n\r", y_off, pp_ty);
richnash 0:9094a94896f6 406 }
richnash 0:9094a94896f6 407
richnash 0:9094a94896f6 408
richnash 3:f96822b3edb5 409 // -----------------------------------------------------------
richnash 3:f96822b3edb5 410 //
richnash 3:f96822b3edb5 411 // -----------------------------------------------------------
richnash 3:f96822b3edb5 412 void TFT_Debug_XY2(int x, int y, const char * pszTxt)
richnash 0:9094a94896f6 413 {
richnash 3:f96822b3edb5 414 // is the TFT on the debug page?
richnash 3:f96822b3edb5 415 if( page == 0 )
richnash 3:f96822b3edb5 416 {
richnash 3:f96822b3edb5 417 int xFont = Terminal6x8[1];
richnash 3:f96822b3edb5 418 int yFont = Terminal6x8[2] + 2;
richnash 3:f96822b3edb5 419
richnash 3:f96822b3edb5 420 mutex_TFT.lock();
richnash 3:f96822b3edb5 421
richnash 3:f96822b3edb5 422 TFT.locate(x*xFont, y*yFont);
richnash 3:f96822b3edb5 423 TFT.printf(pszTxt);
richnash 3:f96822b3edb5 424
richnash 3:f96822b3edb5 425 mutex_TFT.unlock();
richnash 3:f96822b3edb5 426 }
richnash 3:f96822b3edb5 427
richnash 3:f96822b3edb5 428 // is serial echo on?
richnash 3:f96822b3edb5 429 if( _TFT_ECHO )
richnash 3:f96822b3edb5 430 {
richnash 3:f96822b3edb5 431 logger.printf("%s\r\n", pszTxt );
richnash 3:f96822b3edb5 432 }
richnash 0:9094a94896f6 433 }
richnash 0:9094a94896f6 434
richnash 3:f96822b3edb5 435 void TFT_Debug_XY_F2( int x, int y, const char *fmt, ... )
richnash 0:9094a94896f6 436 {
richnash 3:f96822b3edb5 437 mutex_TFT.lock();
richnash 3:f96822b3edb5 438 {
richnash 3:f96822b3edb5 439 char szLogFormat [100];
richnash 3:f96822b3edb5 440
richnash 3:f96822b3edb5 441 // sort out the args !
richnash 3:f96822b3edb5 442 va_list args;
richnash 3:f96822b3edb5 443 va_start(args, fmt);
richnash 3:f96822b3edb5 444
richnash 3:f96822b3edb5 445 // Format the string
richnash 3:f96822b3edb5 446 vsnprintf( szLogFormat, 100, fmt, args);
richnash 3:f96822b3edb5 447 va_end(args);
richnash 3:f96822b3edb5 448
richnash 3:f96822b3edb5 449 // output the string
richnash 3:f96822b3edb5 450 TFT_Debug_XY2( x, y, szLogFormat);
richnash 3:f96822b3edb5 451 }
richnash 2:1335510cfb92 452
richnash 3:f96822b3edb5 453 mutex_TFT.unlock();
richnash 2:1335510cfb92 454 }
richnash 2:1335510cfb92 455
richnash 3:f96822b3edb5 456 // =========================================================================================================
richnash 0:9094a94896f6 457
richnash 0:9094a94896f6 458 // -----------------------------------------------------------
richnash 0:9094a94896f6 459 //
richnash 0:9094a94896f6 460 // -----------------------------------------------------------
richnash 3:f96822b3edb5 461 void status_callback(nsapi_event_t status, intptr_t param)
richnash 0:9094a94896f6 462 {
richnash 3:f96822b3edb5 463 //char szLogFormat [100];
richnash 3:f96822b3edb5 464
richnash 3:f96822b3edb5 465 logger.printf("status_callback\r\n");
richnash 3:f96822b3edb5 466
richnash 3:f96822b3edb5 467 //strcpy( szLogFormat, "ETH: Connection " );
richnash 3:f96822b3edb5 468 //TFT_Debug_XY2( 0, 0, szLogFormat);
richnash 3:f96822b3edb5 469
richnash 3:f96822b3edb5 470 GLOBAL_NET_STATUS = param;
richnash 3:f96822b3edb5 471
richnash 3:f96822b3edb5 472 switch(param)
richnash 3:f96822b3edb5 473 {
richnash 3:f96822b3edb5 474 case NSAPI_STATUS_LOCAL_UP:
richnash 3:f96822b3edb5 475 {
richnash 3:f96822b3edb5 476 logger.printf("NSAPI_STATUS_LOCAL_UP\r\n");
richnash 3:f96822b3edb5 477 // strcpy( szLogFormat, "ETH: Connection - LOCAL_UP ");
richnash 3:f96822b3edb5 478 // TFT_Debug_XY2( 0, 0, szLogFormat);
richnash 3:f96822b3edb5 479 break;
richnash 3:f96822b3edb5 480 }
richnash 3:f96822b3edb5 481 case NSAPI_STATUS_GLOBAL_UP:
richnash 3:f96822b3edb5 482 {
richnash 3:f96822b3edb5 483 logger.printf("NSAPI_STATUS_GLOBAL_UP\r\n");
richnash 3:f96822b3edb5 484
richnash 3:f96822b3edb5 485 /*
richnash 3:f96822b3edb5 486 strcpy( szLogFormat, "ETH: Connection - GLOBAL_UP ");
richnash 3:f96822b3edb5 487 TFT_Debug_XY2( 0, 0, szLogFormat);
richnash 3:f96822b3edb5 488
richnash 3:f96822b3edb5 489 // Output the network address
richnash 3:f96822b3edb5 490 const char *ip = gEth.get_ip_address();
richnash 3:f96822b3edb5 491 const char *netmask = gEth.get_netmask();
richnash 3:f96822b3edb5 492 const char *gateway = gEth.get_gateway();
richnash 3:f96822b3edb5 493
richnash 3:f96822b3edb5 494 TFT_Debug_XY_F2(0, 1, "IP address: %s\r\n", ip ? ip : "None");
richnash 3:f96822b3edb5 495 TFT_Debug_XY_F2(0, 2, "Netmask: %s\r\n", netmask ? netmask : "None");
richnash 3:f96822b3edb5 496 TFT_Debug_XY_F2(0, 3, "Gateway: %s\r\n", gateway ? gateway : "None");
richnash 3:f96822b3edb5 497 */
richnash 3:f96822b3edb5 498
richnash 3:f96822b3edb5 499 break;
richnash 3:f96822b3edb5 500 }
richnash 3:f96822b3edb5 501 case NSAPI_STATUS_DISCONNECTED:
richnash 3:f96822b3edb5 502 {
richnash 3:f96822b3edb5 503 logger.printf("NSAPI_STATUS_DISCONNECTED\r\n");
richnash 3:f96822b3edb5 504 // strcpy( szLogFormat, "ETH: Connection - DISCONNECTED ");
richnash 3:f96822b3edb5 505 // TFT_Debug_XY2( 0, 0, szLogFormat);
richnash 3:f96822b3edb5 506 break;
richnash 3:f96822b3edb5 507 }
richnash 3:f96822b3edb5 508 case NSAPI_STATUS_CONNECTING:
richnash 3:f96822b3edb5 509 {
richnash 3:f96822b3edb5 510 logger.printf("NSAPI_STATUS_CONNECTING\r\n");
richnash 3:f96822b3edb5 511 // strcpy( szLogFormat, "ETH: Connection - CONNECTING ");
richnash 3:f96822b3edb5 512 // TFT_Debug_XY2( 0, 0, szLogFormat);
richnash 3:f96822b3edb5 513 break;
richnash 3:f96822b3edb5 514 }
richnash 3:f96822b3edb5 515 default:
richnash 3:f96822b3edb5 516 {
richnash 3:f96822b3edb5 517 logger.printf("NSAPI - default ?\r\n");
richnash 3:f96822b3edb5 518 // strcpy( szLogFormat, "ETH: Connection - ???? ");
richnash 3:f96822b3edb5 519 // TFT_Debug_XY2( 0, 0, szLogFormat);
richnash 3:f96822b3edb5 520 break;
richnash 3:f96822b3edb5 521 }
richnash 3:f96822b3edb5 522 }
richnash 0:9094a94896f6 523 }
richnash 0:9094a94896f6 524
richnash 0:9094a94896f6 525 // -----------------------------------------------------------
richnash 0:9094a94896f6 526 //
richnash 0:9094a94896f6 527 // -----------------------------------------------------------
richnash 0:9094a94896f6 528 static bool DeviceConnect()
richnash 0:9094a94896f6 529 {
richnash 0:9094a94896f6 530 int retries = 10;
richnash 0:9094a94896f6 531 int rc = 0;
richnash 0:9094a94896f6 532
richnash 3:f96822b3edb5 533 //logger.printf("DeviceConnect()\r\n");
richnash 3:f96822b3edb5 534
richnash 0:9094a94896f6 535 while (retries--)
richnash 0:9094a94896f6 536 {
richnash 0:9094a94896f6 537 rc = gEth.connect();
richnash 0:9094a94896f6 538 if (rc == 0) {
richnash 3:f96822b3edb5 539 // logger.printf("DeviceConnect: Waiting\r\n");
richnash 3:f96822b3edb5 540 // wait(2);
richnash 3:f96822b3edb5 541 // logger.printf("DeviceConnect: Done\r\n");
richnash 0:9094a94896f6 542 return true;
richnash 0:9094a94896f6 543 }
richnash 0:9094a94896f6 544 else
richnash 0:9094a94896f6 545 {
richnash 0:9094a94896f6 546 logger.printf("Connecting... ERR %d\r\n", rc);
richnash 0:9094a94896f6 547 }
richnash 3:f96822b3edb5 548
richnash 3:f96822b3edb5 549 wd.Service();
richnash 0:9094a94896f6 550 }
richnash 0:9094a94896f6 551
richnash 0:9094a94896f6 552 logger.printf("Connecting... FAILED\r\n");
richnash 0:9094a94896f6 553 return false;
richnash 0:9094a94896f6 554 }
richnash 0:9094a94896f6 555
richnash 0:9094a94896f6 556 // -----------------------------------------------------------
richnash 0:9094a94896f6 557 //
richnash 0:9094a94896f6 558 // -----------------------------------------------------------
richnash 0:9094a94896f6 559 static bool DeviceEthStartup()
richnash 0:9094a94896f6 560 {
richnash 3:f96822b3edb5 561 //logger.printf("DeviceEthStartup()\r\n");
richnash 3:f96822b3edb5 562
richnash 3:f96822b3edb5 563 // get ethernet up !
richnash 3:f96822b3edb5 564 if( !DeviceConnect() ) terminal_error_state();
richnash 3:f96822b3edb5 565
richnash 3:f96822b3edb5 566 // Output the network address
richnash 3:f96822b3edb5 567 //const char *ip = gEth.get_ip_address();
richnash 3:f96822b3edb5 568 //const char *netmask = gEth.get_netmask();
richnash 3:f96822b3edb5 569 //const char *gateway = gEth.get_gateway();
richnash 3:f96822b3edb5 570
richnash 3:f96822b3edb5 571 //TFT_Debug_XY_F2(0, 1, "IP address: %s\r\n", ip ? ip : "None");
richnash 3:f96822b3edb5 572 //TFT_Debug_XY_F2(0, 2, "Netmask: %s\r\n", netmask ? netmask : "None");
richnash 3:f96822b3edb5 573 //TFT_Debug_XY_F2(0, 3, "Gateway: %s\r\n", gateway ? gateway : "None");
richnash 3:f96822b3edb5 574
richnash 0:9094a94896f6 575 return true;
richnash 0:9094a94896f6 576 }
richnash 0:9094a94896f6 577
richnash 0:9094a94896f6 578 // -----------------------------------------------------------
richnash 0:9094a94896f6 579 //
richnash 0:9094a94896f6 580 // -----------------------------------------------------------
richnash 0:9094a94896f6 581 void SendUdPMessage(char *buffer)
richnash 0:9094a94896f6 582 {
richnash 3:f96822b3edb5 583 // send the UDP data as broadcast
richnash 3:f96822b3edb5 584 int rc = gEth_socket.sendto( "255.255.255.255", UDP_PORT, (const void *)buffer, strlen( buffer ) );
richnash 3:f96822b3edb5 585 //logger.printf("[SEND rc=%d] buffer[%d]\r\n%s\r\n", rc, strlen(buffer), buffer);
richnash 0:9094a94896f6 586 }
richnash 0:9094a94896f6 587
richnash 0:9094a94896f6 588 // -----------------------------------------------------------
richnash 0:9094a94896f6 589 //
richnash 0:9094a94896f6 590 // -----------------------------------------------------------
richnash 0:9094a94896f6 591 void AfterParseAction(xPL_Message * message)
richnash 0:9094a94896f6 592 {
richnash 3:f96822b3edb5 593 char szLogFormat [100];
richnash 3:f96822b3edb5 594
richnash 0:9094a94896f6 595 // is this a message for me ?
richnash 0:9094a94896f6 596 if (xpl.TargetIsMe(message))
richnash 3:f96822b3edb5 597 {
richnash 3:f96822b3edb5 598 logger.printf("XPL: MSG IS FOR ME !");
richnash 3:f96822b3edb5 599 logger.printf("%s\r\n", message->toString());
richnash 2:1335510cfb92 600
richnash 3:f96822b3edb5 601 // is this a control message?
richnash 3:f96822b3edb5 602 if (message->IsSchema("control", "basic"))
richnash 3:f96822b3edb5 603 {
richnash 3:f96822b3edb5 604 logger.printf("XPL: {control.basic}");
richnash 3:f96822b3edb5 605 }
richnash 3:f96822b3edb5 606 }
richnash 3:f96822b3edb5 607
richnash 3:f96822b3edb5 608 // Put it on the screen
richnash 3:f96822b3edb5 609 sprintf( szLogFormat, "XPL: [%20s] %2d items ", message->source.device_id, message->command_count );
richnash 3:f96822b3edb5 610 TFT_Debug_XY2( 1, 18, szLogFormat);
richnash 3:f96822b3edb5 611
richnash 3:f96822b3edb5 612 //logger.printf("XPL: [%s]\r\n", message->source.device_id );
richnash 3:f96822b3edb5 613 //logger.printf("XPL: [%s] %d items\r\n", message->source.device_id, message->command_count );
richnash 3:f96822b3edb5 614 //TFT_Debug_XY_F( 0, 18, "XPL: [%s] %d items ", message->source.device_id, message->command_count );
richnash 3:f96822b3edb5 615 //TFT_Debug_XY( 0, 19, "XPL: ");
richnash 3:f96822b3edb5 616
richnash 3:f96822b3edb5 617 // do we have any data ??? maybe the parse failed....
richnash 3:f96822b3edb5 618 if( message->command_count == 0 )
richnash 3:f96822b3edb5 619 {
richnash 0:9094a94896f6 620 //
richnash 3:f96822b3edb5 621 // logger.printf("XPL: no data? [bytes: %d] \r\n%s\r\n", iRXBytes, gEth_socket_in_buffer );
richnash 3:f96822b3edb5 622 //TFT_Debug_XY( 0, 19, "XPL: ERROR - PARSE FAILED ?? ");
richnash 3:f96822b3edb5 623 sprintf( szLogFormat, "XPL: ERROR - PARSE FAILED ?? ");
richnash 3:f96822b3edb5 624 TFT_Debug_XY2( 1, 19, szLogFormat);
richnash 0:9094a94896f6 625 }
richnash 0:9094a94896f6 626 else
richnash 0:9094a94896f6 627 {
richnash 0:9094a94896f6 628 // output the message data
richnash 0:9094a94896f6 629 for (short i=0; i<message->command_count; i++)
richnash 0:9094a94896f6 630 {
richnash 0:9094a94896f6 631 // grab each item and do something with it
richnash 0:9094a94896f6 632 //logger.printf("%s=%s\r\n", message->command[i].name, message->command[i].value);
richnash 0:9094a94896f6 633 }
richnash 3:f96822b3edb5 634 //TFT_Debug_XY_F( 0, 19, "XPL: %d items processed ", message->command_count );
richnash 3:f96822b3edb5 635 sprintf( szLogFormat, "XPL: %d items processed ", message->command_count );
richnash 3:f96822b3edb5 636 TFT_Debug_XY2( 1, 19, szLogFormat);
richnash 0:9094a94896f6 637 }
richnash 0:9094a94896f6 638
richnash 0:9094a94896f6 639 // output the full msg!
richnash 0:9094a94896f6 640 //logger.printf("%s\r\n", message->toString());
richnash 0:9094a94896f6 641 }
richnash 0:9094a94896f6 642
richnash 0:9094a94896f6 643 // -----------------------------------------------------------
richnash 0:9094a94896f6 644 //
richnash 0:9094a94896f6 645 // -----------------------------------------------------------
richnash 0:9094a94896f6 646 void Receiver_Thread()
richnash 0:9094a94896f6 647 {
richnash 0:9094a94896f6 648 SocketAddress _sAddr;
richnash 0:9094a94896f6 649 int iNoDataCount = 0;
richnash 3:f96822b3edb5 650 int rc;
richnash 3:f96822b3edb5 651 int iDataMsgCount = 0;
richnash 3:f96822b3edb5 652
richnash 3:f96822b3edb5 653 // setup a listener
richnash 3:f96822b3edb5 654 gEth_socket.set_blocking(true);
richnash 3:f96822b3edb5 655 gEth_socket.set_timeout(3000);
richnash 3:f96822b3edb5 656
richnash 3:f96822b3edb5 657 // open the socket
richnash 3:f96822b3edb5 658 rc = gEth_socket.open((NetworkInterface*)&gEth);
richnash 3:f96822b3edb5 659 if( rc != 0)
richnash 3:f96822b3edb5 660 {
richnash 3:f96822b3edb5 661 logger.printf("UDPSocket RECEIVER OPEN ERROR (rc=%d)\r\n", rc);
richnash 3:f96822b3edb5 662 terminal_error_state();
richnash 3:f96822b3edb5 663 }
richnash 3:f96822b3edb5 664
richnash 3:f96822b3edb5 665 // bind the port
richnash 3:f96822b3edb5 666 rc = gEth_socket.bind(UDP_PORT);
richnash 3:f96822b3edb5 667 logger.printf("Binding port %d\r\n", UDP_PORT);
richnash 3:f96822b3edb5 668 if( rc != 0)
richnash 3:f96822b3edb5 669 {
richnash 3:f96822b3edb5 670 logger.printf("UDPSocket BIND ERROR (rc=%d)\r\n", rc);
richnash 3:f96822b3edb5 671 terminal_error_state();
richnash 3:f96822b3edb5 672 }
richnash 0:9094a94896f6 673
richnash 0:9094a94896f6 674 // loop forever
richnash 0:9094a94896f6 675 while(1)
richnash 0:9094a94896f6 676 {
richnash 0:9094a94896f6 677 // receive from the udp socket
richnash 0:9094a94896f6 678
richnash 3:f96822b3edb5 679 TFT_Debug_XY_F2(1, 15, "RX: Attempt (%2d) ", iNoDataCount);
richnash 3:f96822b3edb5 680
richnash 3:f96822b3edb5 681 iRXBytes = gEth_socket.recvfrom(&_sAddr, gEth_socket_in_buffer, UDP_BUFFER_SIZE);
richnash 3:f96822b3edb5 682
richnash 3:f96822b3edb5 683 TFT_Debug_XY2(20, 15, "-DONE-");
richnash 3:f96822b3edb5 684
richnash 3:f96822b3edb5 685 // see what weve got !
richnash 3:f96822b3edb5 686 if (iRXBytes == NSAPI_ERROR_WOULD_BLOCK)
richnash 3:f96822b3edb5 687 {
richnash 3:f96822b3edb5 688 led3 = 1; // error
richnash 3:f96822b3edb5 689
richnash 3:f96822b3edb5 690 // timeout with no data...
richnash 3:f96822b3edb5 691 ++iNoDataCount;
richnash 3:f96822b3edb5 692 TFT_Debug_XY2(1, 16, "RX: NoData (TIMEOUT) ");
richnash 3:f96822b3edb5 693
richnash 3:f96822b3edb5 694 //screen_Debug();
richnash 3:f96822b3edb5 695 }
richnash 3:f96822b3edb5 696 else if (iRXBytes < 0)
richnash 3:f96822b3edb5 697 {
richnash 3:f96822b3edb5 698 led3 = 1; // error
richnash 3:f96822b3edb5 699
richnash 3:f96822b3edb5 700 // some other error
richnash 3:f96822b3edb5 701 ++iNoDataCount;
richnash 3:f96822b3edb5 702 TFT_Debug_XY2(1, 16, "RX: NoData (ERR) ");
richnash 3:f96822b3edb5 703 }
richnash 3:f96822b3edb5 704 else if( iRXBytes > 0)
richnash 3:f96822b3edb5 705 {
richnash 3:f96822b3edb5 706 led3 = 0; // no error
richnash 3:f96822b3edb5 707 led2 = 1;
richnash 3:f96822b3edb5 708
richnash 3:f96822b3edb5 709 // good data :)
richnash 3:f96822b3edb5 710 iNoDataCount = 0;
richnash 3:f96822b3edb5 711 gEth_socket_in_buffer[iRXBytes] = '\0';
richnash 3:f96822b3edb5 712 TFT_Debug_XY_F2(1, 16, "RX: [%d bytes] %s:%d ", iRXBytes, _sAddr.get_ip_address(), _sAddr.get_port());
richnash 3:f96822b3edb5 713
richnash 3:f96822b3edb5 714 // try and parse the msg
richnash 3:f96822b3edb5 715 xpl.ParseInputMessage(gEth_socket_in_buffer);
richnash 3:f96822b3edb5 716
richnash 3:f96822b3edb5 717 TFT_Debug_XY_F2(1, 26, "RX: DATA(%d)", iDataMsgCount++);
richnash 3:f96822b3edb5 718 led2 = 0;
richnash 3:f96822b3edb5 719 }
richnash 3:f96822b3edb5 720
richnash 3:f96822b3edb5 721 // have we stopped receiving data ?!?!?
richnash 3:f96822b3edb5 722 if( iNoDataCount > 60 ) // about 3 mins (60*3secs) of no data
richnash 3:f96822b3edb5 723 {
richnash 3:f96822b3edb5 724 // go for a reboot...
richnash 3:f96822b3edb5 725 terminal_error_state();
richnash 3:f96822b3edb5 726 }
richnash 0:9094a94896f6 727
richnash 0:9094a94896f6 728 // stroke the WD
richnash 3:f96822b3edb5 729 wd.Service();
richnash 0:9094a94896f6 730 }
richnash 0:9094a94896f6 731 }
richnash 0:9094a94896f6 732
richnash 0:9094a94896f6 733
richnash 0:9094a94896f6 734 // -----------------------------------------------------------
richnash 0:9094a94896f6 735 //
richnash 0:9094a94896f6 736 // -----------------------------------------------------------
richnash 3:f96822b3edb5 737 Thread thread1(osPriorityAboveNormal, 8 * 1024); // OS_STACK_SIZE in theory 8k
richnash 3:f96822b3edb5 738 int main()
richnash 3:f96822b3edb5 739 {
richnash 3:f96822b3edb5 740 const char *ip;
richnash 3:f96822b3edb5 741 char xpl_ip[20];
richnash 3:f96822b3edb5 742 int prev_net_status=0;
richnash 3:f96822b3edb5 743
richnash 3:f96822b3edb5 744 point p;
richnash 3:f96822b3edb5 745
richnash 3:f96822b3edb5 746 bool waitTouch = false ;
richnash 3:f96822b3edb5 747 int iAppLoop=0;
richnash 3:f96822b3edb5 748 int iHBSentCount = 0;
richnash 3:f96822b3edb5 749
richnash 3:f96822b3edb5 750 // FIRST THINGS FIRST!!! set the WD timeout interval...
richnash 3:f96822b3edb5 751 // 100 seconds to get connected and receive an IP address
richnash 3:f96822b3edb5 752 wd.Configure(100000.0);
richnash 3:f96822b3edb5 753
richnash 3:f96822b3edb5 754 // open pc serial
richnash 3:f96822b3edb5 755 logger.baud(115200);
richnash 3:f96822b3edb5 756 logger.printf("---- ETHERNET UDP Test ----\r\n");
richnash 3:f96822b3edb5 757
richnash 3:f96822b3edb5 758 // turn all the LEDs off
richnash 3:f96822b3edb5 759 led1 = 0; // green = normal startup
richnash 3:f96822b3edb5 760 led2 = 0; // blue = ethernet data received
richnash 3:f96822b3edb5 761 led3 = 0; // red = watchdog startup
richnash 3:f96822b3edb5 762
richnash 0:9094a94896f6 763 // WatchDog Startup Information
richnash 0:9094a94896f6 764 logger.printf("\r\n------------------------------------------------------\r\n" );
richnash 0:9094a94896f6 765 logger.printf("[SystemCoreClock] %d Hz\r\n", SystemCoreClock);
richnash 0:9094a94896f6 766 logger.printf("[Reset reason] ");
richnash 0:9094a94896f6 767 if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST)) logger.printf("Independant-Watchdog ");
richnash 0:9094a94896f6 768 if (__HAL_RCC_GET_FLAG(RCC_FLAG_WWDGRST)) logger.printf("Window-Watchdog ");
richnash 0:9094a94896f6 769 if (__HAL_RCC_GET_FLAG(RCC_FLAG_LPWRRST)) logger.printf("Low-Power-Reset ");
richnash 0:9094a94896f6 770 if (__HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST)) logger.printf("Software-Reset ");
richnash 0:9094a94896f6 771 if (__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST)) logger.printf("Power-On/Down-Reset ");
richnash 0:9094a94896f6 772 if (__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST)) logger.printf("Reset-pin ");
richnash 0:9094a94896f6 773 if (__HAL_RCC_GET_FLAG(RCC_FLAG_BORRST)) logger.printf("Brownout ");
richnash 0:9094a94896f6 774 if (__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY)) logger.printf("Low-speed-internal-clock-ready ");
richnash 0:9094a94896f6 775 __HAL_RCC_CLEAR_RESET_FLAGS();
richnash 0:9094a94896f6 776 logger.printf("\r\n------------------------------------------------------\r\n");
richnash 0:9094a94896f6 777
richnash 0:9094a94896f6 778 if (wd.WatchdogCausedReset())
richnash 0:9094a94896f6 779 {
richnash 0:9094a94896f6 780 logger.printf("<<Watchdog caused reset>>\r\n");
richnash 0:9094a94896f6 781 led3 = 1;
richnash 0:9094a94896f6 782 }
richnash 0:9094a94896f6 783 else
richnash 0:9094a94896f6 784 {
richnash 0:9094a94896f6 785 logger.printf("<<Normal Startup>>\r\n");
richnash 0:9094a94896f6 786 led1 = 1;
richnash 0:9094a94896f6 787 led2 = 1;
richnash 0:9094a94896f6 788 led3 = 0;
richnash 0:9094a94896f6 789 }
richnash 0:9094a94896f6 790 logger.printf("------------------------------------------------------\r\n\r\n");
richnash 3:f96822b3edb5 791
richnash 3:f96822b3edb5 792 // Fireup the TFT Screen
richnash 3:f96822b3edb5 793 initTFT() ;
richnash 3:f96822b3edb5 794 //logger.printf("TFT Started {width = %d, height = %d}\n\r", TFT.width(), TFT.height()) ;
richnash 3:f96822b3edb5 795 //calibrate();
richnash 3:f96822b3edb5 796 //screen_Debug();
richnash 3:f96822b3edb5 797 screen1();
richnash 3:f96822b3edb5 798
richnash 3:f96822b3edb5 799 // fire up the ethernet & the receive socket
richnash 3:f96822b3edb5 800 gEth.attach(&status_callback);
richnash 3:f96822b3edb5 801 DeviceEthStartup();
richnash 0:9094a94896f6 802
richnash 3:f96822b3edb5 803 // just wait for everything to settle
richnash 3:f96822b3edb5 804 logger.printf("Network.");
richnash 3:f96822b3edb5 805 while( GLOBAL_NET_STATUS != NSAPI_STATUS_GLOBAL_UP )
richnash 3:f96822b3edb5 806 {
richnash 3:f96822b3edb5 807 logger.printf(".");
richnash 3:f96822b3edb5 808 wait(1);
richnash 3:f96822b3edb5 809 }
richnash 3:f96822b3edb5 810 logger.printf("UP!\r\n");
richnash 3:f96822b3edb5 811
richnash 3:f96822b3edb5 812 // stroke the wd - all ok so far !
richnash 0:9094a94896f6 813 wd.Service();
richnash 3:f96822b3edb5 814
richnash 3:f96822b3edb5 815 screen_Debug();
richnash 3:f96822b3edb5 816
richnash 3:f96822b3edb5 817 // 10 second is enough to make sure everything is ok - UDP read is every 3 seconds
richnash 3:f96822b3edb5 818 wd.Configure(10000.0);
richnash 0:9094a94896f6 819
richnash 3:f96822b3edb5 820 // process the devices ip adress into the XPL instance ID
richnash 3:f96822b3edb5 821 ip = gEth.get_ip_address();
richnash 3:f96822b3edb5 822 strcpy( xpl_ip, ip );
richnash 3:f96822b3edb5 823 UTILS_strip_char_in_string( xpl_ip, '.' );
richnash 3:f96822b3edb5 824
richnash 0:9094a94896f6 825 // setup xpl
richnash 0:9094a94896f6 826 xpl.SendExternal = &SendUdPMessage; // pointer to the send callback
richnash 0:9094a94896f6 827 xpl.AfterParseAction = &AfterParseAction; // pointer to a post parsing action callback
richnash 0:9094a94896f6 828 xpl.SetSource( "NIHH", XPL_NAME, xpl_ip); // parameters for hearbeat message
richnash 3:f96822b3edb5 829
richnash 0:9094a94896f6 830 // kick off the XPL receiver thread to run in the background
richnash 3:f96822b3edb5 831 // thread1.set_priority(osPriorityAboveNormal); // osPriorityNormal osPriorityHigh osPriorityBelowNormal osPriorityAboveNormal
richnash 0:9094a94896f6 832 thread1.start( Receiver_Thread );
richnash 3:f96822b3edb5 833
richnash 3:f96822b3edb5 834
richnash 3:f96822b3edb5 835 // calibrate the screen
richnash 3:f96822b3edb5 836 //calibrate();
richnash 3:f96822b3edb5 837
richnash 3:f96822b3edb5 838
richnash 3:f96822b3edb5 839 // start running the main processing stuff here
richnash 3:f96822b3edb5 840 while (true)
richnash 3:f96822b3edb5 841 {
richnash 3:f96822b3edb5 842 //logger.printf(".\n\r") ;
richnash 3:f96822b3edb5 843
richnash 3:f96822b3edb5 844 //TFT_Debug_XY2( 5, 10, "Hello world..." );
richnash 3:f96822b3edb5 845
richnash 3:f96822b3edb5 846 // pause for a mo !
richnash 3:f96822b3edb5 847 //wait(5);
richnash 3:f96822b3edb5 848
richnash 3:f96822b3edb5 849
richnash 0:9094a94896f6 850 do {
richnash 3:f96822b3edb5 851 led1 = !led1;
richnash 3:f96822b3edb5 852
richnash 3:f96822b3edb5 853 if( GLOBAL_NET_STATUS != prev_net_status )
richnash 3:f96822b3edb5 854 {
richnash 3:f96822b3edb5 855 TFT_Debug_XY_F2(0, 0, "NET: %d", GLOBAL_NET_STATUS);
richnash 3:f96822b3edb5 856 logger.printf("net..\n\r") ;
richnash 3:f96822b3edb5 857
richnash 3:f96822b3edb5 858 prev_net_status = GLOBAL_NET_STATUS;
richnash 3:f96822b3edb5 859 }
richnash 3:f96822b3edb5 860
richnash 3:f96822b3edb5 861
richnash 0:9094a94896f6 862 // allow xpl to do its thing...
richnash 0:9094a94896f6 863 #ifndef CONNECTED_OFF
richnash 0:9094a94896f6 864 xpl.Process();
richnash 0:9094a94896f6 865 #endif
richnash 0:9094a94896f6 866
richnash 0:9094a94896f6 867 if( getPixel(p) )
richnash 0:9094a94896f6 868 {
richnash 3:f96822b3edb5 869 logger.printf("TFT Touch x = %d, y = %d\n\r", p.x, p.y) ;
richnash 3:f96822b3edb5 870 waitTouch = false ;
richnash 3:f96822b3edb5 871 wait(.2); // stop
richnash 0:9094a94896f6 872
richnash 3:f96822b3edb5 873 backlight = !backlight;
richnash 3:f96822b3edb5 874
richnash 0:9094a94896f6 875 }
richnash 0:9094a94896f6 876
richnash 3:f96822b3edb5 877 // every X loops send a test XPL msg
richnash 0:9094a94896f6 878 ++iAppLoop;
richnash 3:f96822b3edb5 879 if( iAppLoop >= 50 )
richnash 0:9094a94896f6 880 {
richnash 0:9094a94896f6 881 xPL_Message _message;
richnash 0:9094a94896f6 882
richnash 0:9094a94896f6 883 /*
richnash 0:9094a94896f6 884 _message.SetSchema( "clock", "update" );
richnash 0:9094a94896f6 885 _message.SetTarget( "*" );
richnash 0:9094a94896f6 886 _message.type = XPL_STAT;
richnash 0:9094a94896f6 887 _message.hop = 1;
richnash 0:9094a94896f6 888 _message.AddCommand("time", "20180730113315");
richnash 0:9094a94896f6 889 xpl.SendMessage( (xPL_Message *)&_message, true );
richnash 0:9094a94896f6 890 */
richnash 0:9094a94896f6 891
richnash 0:9094a94896f6 892 _message.SetSchema( "sensor", "basic" );
richnash 0:9094a94896f6 893 _message.SetTarget( "*" );
richnash 0:9094a94896f6 894 _message.type = XPL_STAT;
richnash 0:9094a94896f6 895 _message.hop = 1;
richnash 0:9094a94896f6 896 _message.AddCommand("device", "test1");
richnash 0:9094a94896f6 897 _message.AddCommand("type", "test");
richnash 0:9094a94896f6 898 _message.AddCommand("current", "1");
richnash 0:9094a94896f6 899 xpl.SendMessage( (xPL_Message *)&_message, true );
richnash 0:9094a94896f6 900
richnash 0:9094a94896f6 901 logger.printf("<<<<<< Sample Message Sent >>>>>>\r\n");
richnash 3:f96822b3edb5 902 TFT_Debug_XY_F2(1, 27, "TX: HB(%d)", iHBSentCount++);
richnash 0:9094a94896f6 903
richnash 0:9094a94896f6 904 // reset
richnash 0:9094a94896f6 905 iAppLoop = 0;
richnash 0:9094a94896f6 906 }
richnash 0:9094a94896f6 907
richnash 3:f96822b3edb5 908 // pause for a sec !
richnash 2:1335510cfb92 909 ThisThread::sleep_for(100);
richnash 3:f96822b3edb5 910
richnash 0:9094a94896f6 911 } while(waitTouch != false) ;
richnash 3:f96822b3edb5 912
richnash 3:f96822b3edb5 913 }
richnash 3:f96822b3edb5 914
richnash 3:f96822b3edb5 915 }
richnash 3:f96822b3edb5 916