EasyCAT LAB - EtherCAT master legacy example

Dependencies:   SOEM SPI_STMPE610 SPI_TFT_ILI9341 TFT_fonts

The EasyCAT LAB is a complete educational and experimental EtherCAT® system, composed of one master and two slaves .

/media/uploads/EasyCAT/easycat_lab.jpg

/media/uploads/EasyCAT/components.jpg

Revision:
7:d71db73dcdd9
Parent:
6:4b39b4dee215
Child:
9:e43af5b19937
--- a/main.cpp	Fri May 28 09:40:12 2021 +0000
+++ b/main.cpp	Wed Oct 25 00:02:31 2023 +0200
@@ -28,14 +28,17 @@
 
 //******************************************************************************
 
-//#define ADA_TFT       // IMPORTANT!!! 
+                        // IMPORTANT!!! 
 
-                        // If your EasyCAT LAB uses the Adafruit TFT
+#define ADA_TFT       // If your EasyCAT LAB uses the Adafruit TFT
                         // you must uncomment this define
                         
-                        // If your EasyCAT LAB uses the Seeed Studio TFT
-                        // you must comment this define                                                  
-
+//#define SEEED_TFT     // If your EasyCAT LAB uses the Seeed Studio TFT
+                        // you must uncomment this define     
+                        
+//#define PARA_TFT        // If your EasyCAT LAB uses the parallel interface TFT
+                        // you must uncomment this define                         
+                                                                     
 //******************************************************************************
 
 #define ETH_TXBUFNB 16
@@ -72,39 +75,79 @@
 //
 // or the Adafruit 2.8" with resistive touchscreen
 // https://www.adafruit.com/product/1651
+//
+// or the parallel interface ARD SHD 2,8TD 
+
 
 
-#define PIN_YP          A3                          // resistive touchscreen
-#define PIN_YM          A1                          //
-#define PIN_XM          A2                          //
-#define PIN_XP          A0                          //
-
-#define PIN_MOSI        D11                         // TFT display SPI
-#define PIN_MISO        D12                         //
-#define PIN_SCLK        D13                         //
-
-#if defined ADA_TFT                                 // pins for the Adafruit TFT                      
-    #define PIN_CS_TFT  D10                         //                 
+#ifdef ADA_TFT                                      // pins for the Adafruit TFT                      
+    #define PIN_CS_TFT  D10                         //               
     #define PIN_DC_TFT  D9                          //                     
     #define PIN_CS_TSC  D8                          // 
+
+    #define PIN_MOSI    D11                         // SPI
+    #define PIN_MISO    D12                         //
+    #define PIN_SCLK    D13                         // 
+#endif
     
-#else                                               // pins for the SeeedStudio TFT
+#ifdef SEEED_TFT                                    // pins for the SeeedStudio TFT
     #define PIN_CS_TFT  D5                          //    
     #define PIN_DC_TFT  D6                          //
+
+    #define PIN_MOSI    D11                         // SPI
+    #define PIN_MISO    D12                         //
+    #define PIN_SCLK    D13                         //    
 #endif                                              //
 
+#ifdef PARA_TFT                                     // pins for the parallel interface TFT
+    #define PIN_D0_TFT  D8                          //
+    #define PIN_D1_TFT  D9                          //
+    #define PIN_D2_TFT  D2                          //
+    #define PIN_D3_TFT  D3                          //
+    #define PIN_D4_TFT  D4                          //
+    #define PIN_D5_TFT  D5                          //
+    #define PIN_D6_TFT  D6                          //
+    #define PIN_D7_TFT  D7                          //
+                                                       
+    #define PIN_RD_TFT  A0                          //                
+    #define PIN_WR_TFT  A1                          //
+    #define PIN_DC_TFT  A2                          //
+    #define PIN_CS_TFT  A3                          //
+    #define PIN_RES_TFT A4                          //
+#endif
+
+ #ifdef SEEED_TFT
+    #define PIN_YP      A3                          // resistive touchscreen
+    #define PIN_YM      A1                          //
+    #define PIN_XM      A2                          //
+    #define PIN_XP      A0                          //
+#else                                               //
+    #define PIN_XP      A3                          //
+    #define PIN_YP      A2                          //
+    #define PIN_XM      D9                          //
+    #define PIN_YM      D8                          //  
+#endif
+
 
 //---- touchscreen parameters --------------------------------------------------
 
 #define TOUCH_SAMPLES 8                                                     
 #define TOUCH_WINDOW 0.05
+
 #define TOUCH_THRESHOLD 0.2
+
 #define TOUCH_MAX_ROUNDS 16
 #define TIME_TOUCH_RELEASED 300
 
-#define TOUCH_X_OFFSET 0.118
+#ifdef PARA_TFT
+    #define TOUCH_X_OFFSET 0.075
+#else
+    #define TOUCH_X_OFFSET 0.118
+#endif
+
 #define TOUCH_X_GAIN 402
 
+
 #define TOUCH_Y_OFFSET 0.090
 #define TOUCH_Y_GAIN 302
 
@@ -174,7 +217,6 @@
 
 float ReadAnalog(AnalogIn Ana);
 
-
 void Application();   
 
 void TouchScreenManagement();
@@ -184,19 +226,30 @@
 bool TouchRead_Z();
 
 
-
 //---- global variables --------------------------------------------------------
 
 
 bool TouchWasReleased;
-
 bool FirstRound;
 
 //------------------------------------------------------------------------------
 
-SPI_TFT_ILI9341 TFT(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, NC, PIN_DC_TFT);
+
+#ifdef PARA_TFT
+    SPI_TFT_ILI9341 TFT(PIN_D0_TFT, PIN_D1_TFT, PIN_D2_TFT, PIN_D3_TFT, PIN_D4_TFT, PIN_D5_TFT,
+    PIN_D6_TFT, PIN_D7_TFT, PIN_RD_TFT, PIN_WR_TFT, PIN_CS_TFT, PIN_DC_TFT, PIN_RES_TFT, "PARA");
+#endif
 
-#if defined ADA_TFT             // touchscreen controller for the Adafruit TFT       
+#ifdef ADA_TFT
+    SPI_TFT_ILI9341 TFT(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, NC, PIN_DC_TFT, 27000000, "ADA");
+#endif
+
+#ifdef SEEED_TFT
+    SPI_TFT_ILI9341 TFT(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, NC, PIN_DC_TFT, 27000000, "SEEED");
+#endif
+
+
+#ifdef ADA_TFT             // touchscreen controller for the Adafruit TFT       
     SPI_STMPE610 TSC(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TSC);             
 #endif
 
@@ -312,23 +365,28 @@
     int i;
     
     printf("Start \n");
-    
+   
     Test_1 = 0; 
     Test_2 = 0;
     Test_3 = 0;
     Test_4 = 0;      
   
-    TFT.background(Black);                                          // init TFT  
-    TFT.cls();                                                      //
-    
-    #if defined ADA_TFT
+     
+    #ifdef ADA_TFT
         TFT.set_orientation(1);     
-    #else
+    #endif
+    #ifdef SEEED_TFT
         TFT.set_orientation(3);                                        
-    #endif    
+    #endif  
+    #ifdef PARA_TFT
+        TFT.set_orientation(1);                                        
+    #endif 
+
+    TFT.background(Black);                                       
+    TFT.cls();                                                  
     
-    DrawBanner();     
-        
+    DrawBanner();    
+    
     NetworkError = false;  
     VisuSlave = LAB_1; 
     
@@ -337,7 +395,7 @@
     fAlarmThreshold = 28.8;
     fTemperature = 0;
     TouchWasReleased = true;
-    
+
 
     if (ec_init(NULL))                                              // init SOEM
     {
@@ -629,7 +687,7 @@
     uint16_t Y;  
     int i;
     
-    #if defined ADA_TFT                                             // if the touchscreen has been tapped
+    #ifdef ADA_TFT                                                  // if the touchscreen has been tapped
         if (TSC.GetPoint(&X, &Y))                                   //
     #else                                                           //
         if (TouchRead(&X, &Y))                                      //   
@@ -952,11 +1010,15 @@
     
     TFT.foreground(Red);    
     TFT.locate(30, 220);    
-    #if defined ADA_TFT                                 
+    #ifdef ADA_TFT                                 
         TFT.printf("Adafruit TFT");   
-    #else
+    #endif
+    #ifdef SEEED_TFT  
         TFT.printf("Seeed Studio TFT");
-    #endif          
+    #endif  
+    #ifdef PARA_TFT  
+        TFT.printf("Parallel TFT");
+    #endif               
 }  
 
 
@@ -1166,7 +1228,6 @@
         TFT.printf("%4d", (int)PotValue);                  //  
     }             
     
-    
     if (++Graph_x > ANALOG_WIDTH-3)
     {
         Graph_x = 0;     
@@ -1196,25 +1257,34 @@
 bool TouchRead(uint16_t* X, uint16_t* Y)                        
 {
     bool Result = false;
-      
-    {                                       
-        if (TouchRead_Z())                      // if the touchscreen is tapped
-        {                                       //
-            *X = TouchRead_X();                 // read also the X and Y axis
-            *Y = TouchRead_Y();                 //
-            
-            wait_us (1000);
-                                            
-            if (TouchRead_Z())                  // if the touchscreen is still tapped
-            {                                   // we assume that the result is good        
-                Result = true;                  //
-                
-              //  TFT.pixel (*X, *Y, White);      //debug - draw the touched point on the TFT
-            }
-        }                          
-    }              
+
+    if (TouchRead_Z())                      // if the touchscreen is tapped
+    {                                       //
+        *X = TouchRead_X();                 // read also the X and Y axis
+        *Y = TouchRead_Y();                 //
+        
+        wait_us (1000);
+                                        
+        if (TouchRead_Z())                  // if the touchscreen is still tapped
+        {                                   // we assume that the result is good        
+            Result = true;                  //
+        }
+    }                          
 
-   return Result;                         
+    DigitalOut  Ym_(PIN_YM);
+    DigitalOut  Xm_(PIN_XM);
+    DigitalOut  Xp_(PIN_XP); 
+    DigitalOut  Yp_(PIN_YP);  
+
+    Xp_ = 1;       
+    Yp_ = 1; 
+    Xm_ = 1;       
+    Ym_ = 1;             
+
+    if (Result)                                 //debug - draw the touched point on the TFT 
+        TFT.pixel (*X, *Y, White);              //  
+
+    return Result;                         
 }   
 
 
@@ -1232,6 +1302,11 @@
                                                 //        
     DigitalOut Xm(PIN_XM);                      //
     Xm = 0;                                     //
+
+    #ifdef PARA_TFT                             // reverse the X axis for the parallel TFT
+        Xp = 0;                                 //
+        Xm = 1;                                 //
+    #endif
                                                 //
     AnalogIn Yp(PIN_YP);                        //
             
@@ -1242,7 +1317,9 @@
         fValue = 0;                             //
                                                 //
     fValue *= TOUCH_X_GAIN;                     //
-                                         
+
+    //printf("X %f\n\n", fValue);                 // debug  
+
     return (uint16_t)fValue;
 }
 
@@ -1272,6 +1349,8 @@
                                                 //
     fValue *= TOUCH_Y_GAIN;                     //    
                                  
+    //printf("Y %f\n", fValue);                   // debug
+
     return (uint16_t)fValue;
 }
 
@@ -1282,11 +1361,11 @@
 {                                               // touchscreen has been tapped            
     float fValue = 0;    
     bool Result;
-                                                  
-    DigitalIn  Yp(PIN_YP);                      // set the I/O
-    Yp.mode(PullUp);                            //
+
+    DigitalIn  Ym(PIN_YM);                      // set the I/O
+    Ym.mode(PullUp);                            //
                                                 //
-    AnalogIn Ym(PIN_YM);                        //
+    AnalogIn Yp(PIN_YP);                        //
                                                 //
     DigitalOut Xm(PIN_XM);                      //    
     Xm = 0;                                     //
@@ -1298,8 +1377,9 @@
     {                                           // and average the result
         wait_us(10);                            //
                                                 //
-        fValue += Ym.read();                    //
+        fValue += Yp.read();                    //
     }                                           //
+                 
     fValue /= TOUCH_SAMPLES;                    //
     
     if (fValue < TOUCH_THRESHOLD)               // compare the result with
@@ -1307,6 +1387,8 @@
     else                                        //
         Result = false;                         //
 
+    //printf("Z %f\n", fValue);             // debug          
+
     return Result;                              //
 }