EasyCAT LAB - EtherCAT master legacy example

Dependencies:   SPI_STMPE610 TFT_fonts SPI_TFT_ILI9341 SOEM

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

Committer:
sulymarco
Date:
Fri May 28 09:40:12 2021 +0000
Revision:
6:4b39b4dee215
Parent:
5:550a1222dcba
Added clarification about the TFT

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sulymarco 0:7077d8f28b3e 1 //********************************************************************************************
sulymarco 0:7077d8f28b3e 2 // *
sulymarco 0:7077d8f28b3e 3 // This software is distributed as an example, "AS IS", in the hope that it could *
sulymarco 0:7077d8f28b3e 4 // be useful, WITHOUT ANY WARRANTY of any kind, express or implied, included, but *
sulymarco 0:7077d8f28b3e 5 // not limited, to the warranties of merchantability, fitness for a particular *
sulymarco 0:7077d8f28b3e 6 // purpose, and non infringiment. In no event shall the authors be liable for any *
sulymarco 0:7077d8f28b3e 7 // claim, damages or other liability, arising from, or in connection with this software. *
sulymarco 0:7077d8f28b3e 8 // *
sulymarco 0:7077d8f28b3e 9 //********************************************************************************************
sulymarco 0:7077d8f28b3e 10
sulymarco 0:7077d8f28b3e 11
sulymarco 0:7077d8f28b3e 12 // The AB&T EasyCAT LAB is a complete experimental EtherCAT® system, composed by
sulymarco 0:7077d8f28b3e 13 // one master and two slaves.
sulymarco 0:7077d8f28b3e 14 // The EasyCAT LAB software is provided free of charge and its pourpose is to allow
sulymarco 0:7077d8f28b3e 15 // makers and educational institutes to experiment with the EtherCAT® protocol.
sulymarco 0:7077d8f28b3e 16 //
sulymarco 0:7077d8f28b3e 17 // The EasyCAT LAB is developed by "AB&T Tecnologie Informatiche" Via dell'About 2A Ivrea Italy.
sulymarco 0:7077d8f28b3e 18 // www.bausano.net
sulymarco 0:7077d8f28b3e 19 // www.easycatshield.com
sulymarco 0:7077d8f28b3e 20 //
sulymarco 0:7077d8f28b3e 21 // The EasyCAT LAB uses the SOEM library by rt:labs
sulymarco 0:7077d8f28b3e 22 // https://rt-labs.com/products/soem-ethercat-master-stack/
sulymarco 0:7077d8f28b3e 23 //
sulymarco 0:7077d8f28b3e 24 // EtherCAT® is a registered trademark and patented technology, licensed by Beckhoff Automation GmbH.
sulymarco 0:7077d8f28b3e 25 // www.beckhoff.com
sulymarco 0:7077d8f28b3e 26 // www.ethercat.org
sulymarco 0:7077d8f28b3e 27
sulymarco 0:7077d8f28b3e 28
sulymarco 5:550a1222dcba 29 //******************************************************************************
sulymarco 5:550a1222dcba 30
sulymarco 5:550a1222dcba 31 //#define ADA_TFT // IMPORTANT!!!
sulymarco 5:550a1222dcba 32
sulymarco 5:550a1222dcba 33 // If your EasyCAT LAB uses the Adafruit TFT
sulymarco 5:550a1222dcba 34 // you must uncomment this define
sulymarco 5:550a1222dcba 35
sulymarco 5:550a1222dcba 36 // If your EasyCAT LAB uses the Seeed Studio TFT
sulymarco 5:550a1222dcba 37 // you must comment this define
sulymarco 5:550a1222dcba 38
sulymarco 5:550a1222dcba 39 //******************************************************************************
sulymarco 5:550a1222dcba 40
sulymarco 0:7077d8f28b3e 41 #define ETH_TXBUFNB 16
sulymarco 0:7077d8f28b3e 42 #define ETH_RXBUFNB 16
sulymarco 0:7077d8f28b3e 43
sulymarco 0:7077d8f28b3e 44 #include "mbed.h"
sulymarco 0:7077d8f28b3e 45
sulymarco 0:7077d8f28b3e 46 #ifndef __align
sulymarco 0:7077d8f28b3e 47 #define __align MBED_ALIGN
sulymarco 0:7077d8f28b3e 48 #endif
sulymarco 0:7077d8f28b3e 49
sulymarco 0:7077d8f28b3e 50 #include "config.h"
sulymarco 0:7077d8f28b3e 51 #include "SPI_TFT_ILI9341.h"
sulymarco 0:7077d8f28b3e 52 #include "Arial12x12.h"
sulymarco 0:7077d8f28b3e 53 #include "Arial24x23.h"
sulymarco 0:7077d8f28b3e 54 #include "Arial28x28.h"
sulymarco 0:7077d8f28b3e 55 #include "font_big.h"
sulymarco 0:7077d8f28b3e 56 #include "soem_start.h"
sulymarco 5:550a1222dcba 57 #include "SPI_STMPE610.h"
sulymarco 0:7077d8f28b3e 58
sulymarco 0:7077d8f28b3e 59 #define CYCLE_TIME 1000 // master cycle time in uS
sulymarco 0:7077d8f28b3e 60 // 1000 = 1mS
sulymarco 0:7077d8f28b3e 61
sulymarco 0:7077d8f28b3e 62
sulymarco 0:7077d8f28b3e 63 #define SysMilliS() (uint32_t)Kernel::get_ms_count()
sulymarco 0:7077d8f28b3e 64
EasyCAT 3:6ee00ee90d4c 65 UnbufferedSerial pc(USBTX,USBRX,115200); // set the debug serial line speed to 115200
sulymarco 0:7077d8f28b3e 66
sulymarco 0:7077d8f28b3e 67
sulymarco 0:7077d8f28b3e 68 //---- TFT with resistive touchscreen pins -------------------------------------
sulymarco 0:7077d8f28b3e 69
sulymarco 0:7077d8f28b3e 70 // the display used is the SeeedStudio 2.8 inch TFT v2.0
sulymarco 0:7077d8f28b3e 71 // http://wiki.seeedstudio.com/2.8inch_TFT_Touch_Shield_v2.0/
sulymarco 6:4b39b4dee215 72 //
sulymarco 6:4b39b4dee215 73 // or the Adafruit 2.8" with resistive touchscreen
sulymarco 6:4b39b4dee215 74 // https://www.adafruit.com/product/1651
sulymarco 6:4b39b4dee215 75
sulymarco 0:7077d8f28b3e 76
sulymarco 0:7077d8f28b3e 77 #define PIN_YP A3 // resistive touchscreen
sulymarco 0:7077d8f28b3e 78 #define PIN_YM A1 //
sulymarco 0:7077d8f28b3e 79 #define PIN_XM A2 //
sulymarco 0:7077d8f28b3e 80 #define PIN_XP A0 //
sulymarco 0:7077d8f28b3e 81
sulymarco 0:7077d8f28b3e 82 #define PIN_MOSI D11 // TFT display SPI
sulymarco 0:7077d8f28b3e 83 #define PIN_MISO D12 //
sulymarco 0:7077d8f28b3e 84 #define PIN_SCLK D13 //
sulymarco 5:550a1222dcba 85
sulymarco 5:550a1222dcba 86 #if defined ADA_TFT // pins for the Adafruit TFT
sulymarco 5:550a1222dcba 87 #define PIN_CS_TFT D10 //
sulymarco 5:550a1222dcba 88 #define PIN_DC_TFT D9 //
sulymarco 5:550a1222dcba 89 #define PIN_CS_TSC D8 //
sulymarco 5:550a1222dcba 90
sulymarco 5:550a1222dcba 91 #else // pins for the SeeedStudio TFT
sulymarco 5:550a1222dcba 92 #define PIN_CS_TFT D5 //
sulymarco 5:550a1222dcba 93 #define PIN_DC_TFT D6 //
sulymarco 5:550a1222dcba 94 #endif //
sulymarco 0:7077d8f28b3e 95
sulymarco 0:7077d8f28b3e 96
sulymarco 0:7077d8f28b3e 97 //---- touchscreen parameters --------------------------------------------------
sulymarco 0:7077d8f28b3e 98
sulymarco 0:7077d8f28b3e 99 #define TOUCH_SAMPLES 8
sulymarco 0:7077d8f28b3e 100 #define TOUCH_WINDOW 0.05
sulymarco 0:7077d8f28b3e 101 #define TOUCH_THRESHOLD 0.2
sulymarco 0:7077d8f28b3e 102 #define TOUCH_MAX_ROUNDS 16
sulymarco 0:7077d8f28b3e 103 #define TIME_TOUCH_RELEASED 300
sulymarco 0:7077d8f28b3e 104
sulymarco 0:7077d8f28b3e 105 #define TOUCH_X_OFFSET 0.118
sulymarco 0:7077d8f28b3e 106 #define TOUCH_X_GAIN 402
sulymarco 0:7077d8f28b3e 107
sulymarco 0:7077d8f28b3e 108 #define TOUCH_Y_OFFSET 0.090
sulymarco 0:7077d8f28b3e 109 #define TOUCH_Y_GAIN 302
sulymarco 0:7077d8f28b3e 110
sulymarco 0:7077d8f28b3e 111
sulymarco 0:7077d8f28b3e 112 //---- side menu parameters ----------------------------------------------------
sulymarco 0:7077d8f28b3e 113
sulymarco 0:7077d8f28b3e 114 #define MENU_Y 0
sulymarco 0:7077d8f28b3e 115 #define MENU_WIDTH 83
sulymarco 0:7077d8f28b3e 116 #define MENU_HEIGHT 42
sulymarco 0:7077d8f28b3e 117 #define MENU_X (319-MENU_WIDTH)
sulymarco 0:7077d8f28b3e 118
sulymarco 0:7077d8f28b3e 119
sulymarco 0:7077d8f28b3e 120 //---- slave parameters - LAB_1 EasyCAT with multifunction shield --------------
sulymarco 0:7077d8f28b3e 121
sulymarco 0:7077d8f28b3e 122 #define TERMO_X 55
sulymarco 0:7077d8f28b3e 123 #define TERMO_Y 185
sulymarco 0:7077d8f28b3e 124
sulymarco 0:7077d8f28b3e 125 #define ALARM_X 140
sulymarco 0:7077d8f28b3e 126 #define ALARM_Y 0
sulymarco 0:7077d8f28b3e 127 #define ALARM_WIDTH 83
sulymarco 0:7077d8f28b3e 128 #define ALARM_HEIGHT 42
sulymarco 0:7077d8f28b3e 129
sulymarco 0:7077d8f28b3e 130 #define TIME_BLINK 1000
sulymarco 0:7077d8f28b3e 131 #define TIME_AUTO_REP_START 1000
sulymarco 0:7077d8f28b3e 132 #define TIME_AUTO_REP_REPEAT 200
sulymarco 0:7077d8f28b3e 133
sulymarco 0:7077d8f28b3e 134
sulymarco 0:7077d8f28b3e 135 //---- slave parameters - LAB_2 EasyCAT with multifunction shield --------------
sulymarco 0:7077d8f28b3e 136
sulymarco 0:7077d8f28b3e 137 #define SEG_X 0
sulymarco 0:7077d8f28b3e 138 #define SEG_Y 0
sulymarco 0:7077d8f28b3e 139 #define SEG_WIDTH 42
sulymarco 0:7077d8f28b3e 140 #define SEG_HEIGHT 16
sulymarco 0:7077d8f28b3e 141 #define SEG_STEP 60
sulymarco 0:7077d8f28b3e 142
sulymarco 0:7077d8f28b3e 143 #define BUTTONS_X 38
sulymarco 0:7077d8f28b3e 144 #define BUTTONS_Y 80
sulymarco 0:7077d8f28b3e 145 #define BUTTONS_WIDTH 26
sulymarco 0:7077d8f28b3e 146 #define BUTTONS_R 3
sulymarco 0:7077d8f28b3e 147 #define BUTTONS_STEP 60
sulymarco 0:7077d8f28b3e 148
sulymarco 0:7077d8f28b3e 149 #define ANALOG_X 0
sulymarco 0:7077d8f28b3e 150 #define ANALOG_Y 120
sulymarco 0:7077d8f28b3e 151 #define ANALOG_WIDTH 222
sulymarco 0:7077d8f28b3e 152 #define ANALOG_HEIGHT 80
sulymarco 0:7077d8f28b3e 153
sulymarco 0:7077d8f28b3e 154 #define TIME_REP_SEG 300
sulymarco 0:7077d8f28b3e 155 #define TIME_POTENTIOMETER 100
sulymarco 0:7077d8f28b3e 156
sulymarco 0:7077d8f28b3e 157
sulymarco 0:7077d8f28b3e 158 //---- local functions ---------------------------------------------------------
sulymarco 0:7077d8f28b3e 159
sulymarco 0:7077d8f28b3e 160 void DrawBanner();
sulymarco 0:7077d8f28b3e 161 void DrawSlaveFixedParts();
sulymarco 0:7077d8f28b3e 162 void DrawSideMenu (uint8_t Slave);
sulymarco 0:7077d8f28b3e 163
sulymarco 0:7077d8f28b3e 164 void DrawTemperatureValue(float fValue);
sulymarco 0:7077d8f28b3e 165 void DrawAlarmSettings(float fThreshold, bool OnOff, bool MinMax);
sulymarco 0:7077d8f28b3e 166 void DrawOnlyThreshold(float fThreshold, bool OnOff, bool MinMax);
sulymarco 0:7077d8f28b3e 167 void DrawAlarmStatus(bool Alarm);
sulymarco 0:7077d8f28b3e 168 void DisplayInRect(int X, int Y, int X_off, int Y_off, char* Value, int BackColor, unsigned char* Font);
sulymarco 0:7077d8f28b3e 169
sulymarco 0:7077d8f28b3e 170 void DrawButtonsValue(uint8_t Value);
sulymarco 0:7077d8f28b3e 171 void DrawSegmentsValue(uint8_t Value);
sulymarco 0:7077d8f28b3e 172 void DrawPotentiometerValue(uint16_t PotValue);
sulymarco 0:7077d8f28b3e 173
sulymarco 0:7077d8f28b3e 174
sulymarco 0:7077d8f28b3e 175 float ReadAnalog(AnalogIn Ana);
sulymarco 0:7077d8f28b3e 176
sulymarco 0:7077d8f28b3e 177
sulymarco 0:7077d8f28b3e 178 void Application();
sulymarco 0:7077d8f28b3e 179
sulymarco 0:7077d8f28b3e 180 void TouchScreenManagement();
sulymarco 0:7077d8f28b3e 181 bool TouchRead(uint16_t* X, uint16_t* Y);
sulymarco 0:7077d8f28b3e 182 uint16_t TouchRead_X();
sulymarco 0:7077d8f28b3e 183 uint16_t TouchRead_Y();
sulymarco 0:7077d8f28b3e 184 bool TouchRead_Z();
sulymarco 0:7077d8f28b3e 185
sulymarco 0:7077d8f28b3e 186
sulymarco 0:7077d8f28b3e 187
sulymarco 0:7077d8f28b3e 188 //---- global variables --------------------------------------------------------
sulymarco 0:7077d8f28b3e 189
sulymarco 0:7077d8f28b3e 190
sulymarco 0:7077d8f28b3e 191 bool TouchWasReleased;
sulymarco 0:7077d8f28b3e 192
sulymarco 0:7077d8f28b3e 193 bool FirstRound;
sulymarco 0:7077d8f28b3e 194
sulymarco 0:7077d8f28b3e 195 //------------------------------------------------------------------------------
sulymarco 0:7077d8f28b3e 196
sulymarco 0:7077d8f28b3e 197 SPI_TFT_ILI9341 TFT(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, NC, PIN_DC_TFT);
sulymarco 0:7077d8f28b3e 198
sulymarco 5:550a1222dcba 199 #if defined ADA_TFT // touchscreen controller for the Adafruit TFT
sulymarco 5:550a1222dcba 200 SPI_STMPE610 TSC(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TSC);
sulymarco 5:550a1222dcba 201 #endif
sulymarco 5:550a1222dcba 202
sulymarco 0:7077d8f28b3e 203 Ticker SampleTicker;
sulymarco 0:7077d8f28b3e 204 Thread thread;
sulymarco 0:7077d8f28b3e 205
sulymarco 0:7077d8f28b3e 206 DigitalOut Test_1(D1); // debug test points
sulymarco 0:7077d8f28b3e 207 DigitalOut Test_2(D2); //
sulymarco 0:7077d8f28b3e 208 DigitalOut Test_3(D3); //
sulymarco 0:7077d8f28b3e 209 DigitalOut Test_4(D4); //
sulymarco 0:7077d8f28b3e 210
sulymarco 0:7077d8f28b3e 211
sulymarco 0:7077d8f28b3e 212 //------------------------------------------------------------------------------
sulymarco 0:7077d8f28b3e 213
sulymarco 0:7077d8f28b3e 214 float fAlarmThreshold;
sulymarco 0:7077d8f28b3e 215 bool AlarmOnOff;
sulymarco 0:7077d8f28b3e 216 bool AlarmMinMax;
sulymarco 0:7077d8f28b3e 217
sulymarco 0:7077d8f28b3e 218
sulymarco 0:7077d8f28b3e 219 uint16_t PotValue;
sulymarco 0:7077d8f28b3e 220 uint16_t PrevPotValue;
sulymarco 0:7077d8f28b3e 221 uint8_t Buttons;
sulymarco 0:7077d8f28b3e 222 uint8_t PrevButtons;
sulymarco 0:7077d8f28b3e 223 uint8_t Segments;
sulymarco 0:7077d8f28b3e 224 uint8_t PrevSegments;
sulymarco 0:7077d8f28b3e 225 int16_t Graph_x;
sulymarco 0:7077d8f28b3e 226
sulymarco 0:7077d8f28b3e 227 uint8_t Outputs;
sulymarco 0:7077d8f28b3e 228 uint8_t PrevOutputs;
sulymarco 0:7077d8f28b3e 229
sulymarco 0:7077d8f28b3e 230 uint8_t Inputs;
sulymarco 0:7077d8f28b3e 231 uint8_t PrevInputs;
sulymarco 0:7077d8f28b3e 232
sulymarco 0:7077d8f28b3e 233
sulymarco 0:7077d8f28b3e 234 int8_t VisuSlave;
sulymarco 0:7077d8f28b3e 235
sulymarco 0:7077d8f28b3e 236
sulymarco 0:7077d8f28b3e 237 //------------------------------------------------------------------------------
sulymarco 0:7077d8f28b3e 238
sulymarco 0:7077d8f28b3e 239 uint32_t Time;
sulymarco 0:7077d8f28b3e 240 uint32_t TimeBlink;
sulymarco 0:7077d8f28b3e 241 uint32_t TimeAutoRepeat;
sulymarco 0:7077d8f28b3e 242 uint32_t TimeAutoRepRepeat;
sulymarco 0:7077d8f28b3e 243 uint32_t TimePotentiometer;
sulymarco 0:7077d8f28b3e 244 uint32_t TimeTouchReleased;
sulymarco 0:7077d8f28b3e 245
sulymarco 0:7077d8f28b3e 246 bool Blink;
sulymarco 0:7077d8f28b3e 247
sulymarco 0:7077d8f28b3e 248 uint8_t TouchAxes = 0;
sulymarco 0:7077d8f28b3e 249 uint8_t Action = 0;
sulymarco 0:7077d8f28b3e 250
sulymarco 0:7077d8f28b3e 251 uint16_t X;
sulymarco 0:7077d8f28b3e 252 uint16_t Y;
sulymarco 0:7077d8f28b3e 253
sulymarco 0:7077d8f28b3e 254 float fTemperature;
sulymarco 0:7077d8f28b3e 255 float static fPrevTemperature;
sulymarco 0:7077d8f28b3e 256
sulymarco 0:7077d8f28b3e 257 int ExpectWorkCounter;
sulymarco 0:7077d8f28b3e 258 int WorkCounter;
sulymarco 0:7077d8f28b3e 259 int WorkCounterSafe;
sulymarco 0:7077d8f28b3e 260 bool NetworkError;
sulymarco 0:7077d8f28b3e 261 bool NetworkErrorSafe;
sulymarco 0:7077d8f28b3e 262
sulymarco 0:7077d8f28b3e 263 #define DATA_EXCHANGE_FLAG (1UL << 0)
sulymarco 0:7077d8f28b3e 264 #define APPLICATION_FLAG (1UL << 1)
sulymarco 0:7077d8f28b3e 265
sulymarco 0:7077d8f28b3e 266 EventFlags event_flags;
sulymarco 0:7077d8f28b3e 267
sulymarco 0:7077d8f28b3e 268 Mutex IO_data;
sulymarco 0:7077d8f28b3e 269
sulymarco 0:7077d8f28b3e 270
sulymarco 0:7077d8f28b3e 271 //---- data exchange thread ----------------------------------------------------
sulymarco 0:7077d8f28b3e 272
sulymarco 0:7077d8f28b3e 273 void ExchangeMaster()
sulymarco 0:7077d8f28b3e 274 {
sulymarco 0:7077d8f28b3e 275 while (true)
sulymarco 0:7077d8f28b3e 276 {
sulymarco 0:7077d8f28b3e 277
sulymarco 0:7077d8f28b3e 278
sulymarco 0:7077d8f28b3e 279 event_flags.wait_any(DATA_EXCHANGE_FLAG); // the thread waits for the synchronization flag
sulymarco 0:7077d8f28b3e 280
sulymarco 0:7077d8f28b3e 281 //Test_1 = 1;
sulymarco 0:7077d8f28b3e 282
sulymarco 0:7077d8f28b3e 283 IO_data.lock(); // Ethercat data exchange
sulymarco 0:7077d8f28b3e 284 ec_send_processdata(); //
sulymarco 0:7077d8f28b3e 285 WorkCounter = ec_receive_processdata(EC_TIMEOUTRET);
sulymarco 0:7077d8f28b3e 286
sulymarco 0:7077d8f28b3e 287 if (WorkCounter != ExpectWorkCounter)
sulymarco 0:7077d8f28b3e 288 NetworkError = true;
sulymarco 0:7077d8f28b3e 289 else
sulymarco 0:7077d8f28b3e 290 NetworkError = false;
sulymarco 0:7077d8f28b3e 291
sulymarco 0:7077d8f28b3e 292 IO_data.unlock(); //
sulymarco 0:7077d8f28b3e 293 event_flags.set(APPLICATION_FLAG); // synchronize the application
sulymarco 0:7077d8f28b3e 294
sulymarco 0:7077d8f28b3e 295 //Test_1 = 0;
sulymarco 0:7077d8f28b3e 296 }
sulymarco 0:7077d8f28b3e 297 }
sulymarco 0:7077d8f28b3e 298
sulymarco 0:7077d8f28b3e 299
sulymarco 0:7077d8f28b3e 300 //----- thicker generated sample time ------------------------------------------
sulymarco 0:7077d8f28b3e 301
sulymarco 0:7077d8f28b3e 302 void SampleIsr() // set the event that starts
sulymarco 0:7077d8f28b3e 303 { // the data exchange
sulymarco 0:7077d8f28b3e 304 event_flags.set(DATA_EXCHANGE_FLAG); //
sulymarco 0:7077d8f28b3e 305 } //
sulymarco 0:7077d8f28b3e 306
sulymarco 0:7077d8f28b3e 307
sulymarco 0:7077d8f28b3e 308 //****** initialization ********************************************************
sulymarco 0:7077d8f28b3e 309
sulymarco 0:7077d8f28b3e 310 int main()
sulymarco 0:7077d8f28b3e 311 {
sulymarco 0:7077d8f28b3e 312 int i;
sulymarco 0:7077d8f28b3e 313
sulymarco 0:7077d8f28b3e 314 printf("Start \n");
sulymarco 0:7077d8f28b3e 315
sulymarco 0:7077d8f28b3e 316 Test_1 = 0;
sulymarco 0:7077d8f28b3e 317 Test_2 = 0;
sulymarco 0:7077d8f28b3e 318 Test_3 = 0;
sulymarco 0:7077d8f28b3e 319 Test_4 = 0;
sulymarco 0:7077d8f28b3e 320
sulymarco 0:7077d8f28b3e 321 TFT.background(Black); // init TFT
sulymarco 0:7077d8f28b3e 322 TFT.cls(); //
sulymarco 5:550a1222dcba 323
sulymarco 5:550a1222dcba 324 #if defined ADA_TFT
sulymarco 5:550a1222dcba 325 TFT.set_orientation(1);
sulymarco 5:550a1222dcba 326 #else
sulymarco 5:550a1222dcba 327 TFT.set_orientation(3);
sulymarco 5:550a1222dcba 328 #endif
sulymarco 5:550a1222dcba 329
sulymarco 0:7077d8f28b3e 330 DrawBanner();
sulymarco 0:7077d8f28b3e 331
sulymarco 0:7077d8f28b3e 332 NetworkError = false;
sulymarco 0:7077d8f28b3e 333 VisuSlave = LAB_1;
sulymarco 0:7077d8f28b3e 334
sulymarco 0:7077d8f28b3e 335 AlarmOnOff = true;
sulymarco 0:7077d8f28b3e 336 AlarmMinMax = false;
sulymarco 0:7077d8f28b3e 337 fAlarmThreshold = 28.8;
sulymarco 0:7077d8f28b3e 338 fTemperature = 0;
sulymarco 0:7077d8f28b3e 339 TouchWasReleased = true;
sulymarco 0:7077d8f28b3e 340
sulymarco 0:7077d8f28b3e 341
sulymarco 0:7077d8f28b3e 342 if (ec_init(NULL)) // init SOEM
sulymarco 0:7077d8f28b3e 343 {
sulymarco 0:7077d8f28b3e 344 printf("ec_init succeeded.\n");
sulymarco 0:7077d8f28b3e 345 printf("Scanning the network\n");
sulymarco 0:7077d8f28b3e 346
sulymarco 0:7077d8f28b3e 347 TFT.cls();
sulymarco 0:7077d8f28b3e 348
sulymarco 0:7077d8f28b3e 349 TFT.set_font((unsigned char*) Arial12x12);
sulymarco 0:7077d8f28b3e 350 TFT.foreground(Green);
sulymarco 0:7077d8f28b3e 351 TFT.locate(0, 0);
sulymarco 0:7077d8f28b3e 352
sulymarco 0:7077d8f28b3e 353 TFT.printf("Scanning the network\n");
sulymarco 0:7077d8f28b3e 354
sulymarco 0:7077d8f28b3e 355 if (network_scanning())
sulymarco 0:7077d8f28b3e 356 {
sulymarco 0:7077d8f28b3e 357 if (network_configuration()) // check network configuration
sulymarco 0:7077d8f28b3e 358 {
sulymarco 0:7077d8f28b3e 359 ec_config_map(&IOmap); // map the I/O
sulymarco 0:7077d8f28b3e 360 MapLocalStructures();
sulymarco 0:7077d8f28b3e 361
sulymarco 0:7077d8f28b3e 362 printf("\nSlaves mapped, state to SAFE_OP.\n");
sulymarco 0:7077d8f28b3e 363 // wait for all slaves to reach SAFE_OP state
sulymarco 0:7077d8f28b3e 364 ec_statecheck(0, EC_STATE_SAFE_OP, EC_TIMEOUTSTATE);
sulymarco 0:7077d8f28b3e 365
sulymarco 0:7077d8f28b3e 366 printf("Request operational state for all slaves\n");
sulymarco 0:7077d8f28b3e 367 ec_slave[0].state = EC_STATE_OPERATIONAL;
sulymarco 0:7077d8f28b3e 368
sulymarco 0:7077d8f28b3e 369 ec_send_processdata(); // send one valid process data to make outputs in slaves happy
sulymarco 0:7077d8f28b3e 370 ExpectWorkCounter = ec_receive_processdata(EC_TIMEOUTRET);
sulymarco 0:7077d8f28b3e 371
sulymarco 0:7077d8f28b3e 372 ec_writestate(0); // request OP state for all slaves
sulymarco 0:7077d8f28b3e 373
sulymarco 0:7077d8f28b3e 374 // wait for all slaves to reach OP state
sulymarco 0:7077d8f28b3e 375 ec_statecheck(0, EC_STATE_OPERATIONAL, EC_TIMEOUTSTATE);
sulymarco 0:7077d8f28b3e 376 if (ec_slave[0].state == EC_STATE_OPERATIONAL )
sulymarco 0:7077d8f28b3e 377 {
sulymarco 0:7077d8f28b3e 378 printf("Operational state reached for all slaves.\n");
sulymarco 0:7077d8f28b3e 379 }
sulymarco 0:7077d8f28b3e 380 else
sulymarco 0:7077d8f28b3e 381 {
sulymarco 0:7077d8f28b3e 382 printf("Not all slaves reached operational state.\n");
sulymarco 0:7077d8f28b3e 383 ec_readstate();
sulymarco 0:7077d8f28b3e 384 for(i = 1; i<=ec_slavecount ; i++)
sulymarco 0:7077d8f28b3e 385 {
sulymarco 0:7077d8f28b3e 386 if(ec_slave[i].state != EC_STATE_OPERATIONAL)
sulymarco 0:7077d8f28b3e 387 {
sulymarco 0:7077d8f28b3e 388 printf("Slave %d State=0x%04x StatusCode=0x%04x\n",
sulymarco 0:7077d8f28b3e 389 i, ec_slave[i].state, ec_slave[i].ALstatuscode);
sulymarco 0:7077d8f28b3e 390 }
sulymarco 0:7077d8f28b3e 391 }
sulymarco 0:7077d8f28b3e 392
sulymarco 0:7077d8f28b3e 393 TFT.foreground(Red);
sulymarco 0:7077d8f28b3e 394 TFT.locate(0, 0);
sulymarco 0:7077d8f28b3e 395 TFT.printf("Not all slaves reached operational state!");
sulymarco 0:7077d8f28b3e 396 while(1){}
sulymarco 0:7077d8f28b3e 397 }
sulymarco 0:7077d8f28b3e 398
sulymarco 0:7077d8f28b3e 399 DrawSlaveFixedParts();
sulymarco 0:7077d8f28b3e 400
sulymarco 0:7077d8f28b3e 401 thread.start(ExchangeMaster);
sulymarco 0:7077d8f28b3e 402 thread.set_priority(osPriorityRealtime);
sulymarco 0:7077d8f28b3e 403
sulymarco 0:7077d8f28b3e 404 SampleTicker.attach_us(&SampleIsr, CYCLE_TIME);
sulymarco 0:7077d8f28b3e 405
sulymarco 0:7077d8f28b3e 406 Application();
sulymarco 0:7077d8f28b3e 407 }
sulymarco 0:7077d8f28b3e 408
sulymarco 0:7077d8f28b3e 409 else
sulymarco 0:7077d8f28b3e 410 {
sulymarco 0:7077d8f28b3e 411 printf("Mismatch of network units!\n");
sulymarco 0:7077d8f28b3e 412 TFT.foreground(Red);
sulymarco 0:7077d8f28b3e 413 TFT.locate(0, 0);
sulymarco 0:7077d8f28b3e 414 TFT.printf("Mismatch of network units!");
sulymarco 0:7077d8f28b3e 415
sulymarco 0:7077d8f28b3e 416 while(1){}
sulymarco 0:7077d8f28b3e 417 }
sulymarco 0:7077d8f28b3e 418 }
sulymarco 0:7077d8f28b3e 419
sulymarco 0:7077d8f28b3e 420 else
sulymarco 0:7077d8f28b3e 421 {
sulymarco 0:7077d8f28b3e 422 printf("No slaves found!\n");
sulymarco 0:7077d8f28b3e 423 TFT.foreground(Red);
sulymarco 0:7077d8f28b3e 424 TFT.printf("No slaves found!");
sulymarco 0:7077d8f28b3e 425
sulymarco 0:7077d8f28b3e 426 while(1){}
sulymarco 0:7077d8f28b3e 427 }
sulymarco 0:7077d8f28b3e 428 }
sulymarco 0:7077d8f28b3e 429 else
sulymarco 0:7077d8f28b3e 430 {
sulymarco 0:7077d8f28b3e 431 printf("Ethernet interface init failed!");
sulymarco 0:7077d8f28b3e 432 TFT.foreground(Red);
sulymarco 0:7077d8f28b3e 433 TFT.locate(0, 0);
sulymarco 0:7077d8f28b3e 434 TFT.printf("Ethernet interface init failed!");
sulymarco 0:7077d8f28b3e 435 while(1){}
sulymarco 0:7077d8f28b3e 436 }
sulymarco 0:7077d8f28b3e 437 }
sulymarco 0:7077d8f28b3e 438
sulymarco 0:7077d8f28b3e 439
sulymarco 0:7077d8f28b3e 440 //****** user master application **********************************************
sulymarco 0:7077d8f28b3e 441
sulymarco 0:7077d8f28b3e 442 void Application()
sulymarco 0:7077d8f28b3e 443 {
sulymarco 0:7077d8f28b3e 444
sulymarco 0:7077d8f28b3e 445 while(1)
sulymarco 0:7077d8f28b3e 446 {
sulymarco 0:7077d8f28b3e 447 event_flags.wait_any(APPLICATION_FLAG); // the application waits for the synchronization flag
sulymarco 0:7077d8f28b3e 448
sulymarco 0:7077d8f28b3e 449 //Test_2 = 1;
sulymarco 0:7077d8f28b3e 450
sulymarco 0:7077d8f28b3e 451 IO_data.lock(); // copy the Ethercat data to a safe buffer
sulymarco 0:7077d8f28b3e 452 memcpy(&IOmapSafe[0], &IOmap[0], IO_MAP_SIZE); //
sulymarco 0:7077d8f28b3e 453 //
sulymarco 0:7077d8f28b3e 454 if (NetworkError) //
sulymarco 0:7077d8f28b3e 455 { //
sulymarco 0:7077d8f28b3e 456 NetworkErrorSafe = NetworkError; //
sulymarco 0:7077d8f28b3e 457 WorkCounterSafe = WorkCounter; //
sulymarco 0:7077d8f28b3e 458 } //
sulymarco 0:7077d8f28b3e 459 IO_data.unlock(); //
sulymarco 0:7077d8f28b3e 460
sulymarco 0:7077d8f28b3e 461 if (NetworkErrorSafe)
sulymarco 0:7077d8f28b3e 462 {
sulymarco 0:7077d8f28b3e 463 TFT.rect(35,50, 285, 182, Red);
sulymarco 0:7077d8f28b3e 464 TFT.fillrect(36,51, 284, 181, Black);
sulymarco 0:7077d8f28b3e 465 TFT.foreground(Red);
sulymarco 0:7077d8f28b3e 466 TFT.set_font((unsigned char*) Arial28x28);
sulymarco 0:7077d8f28b3e 467 TFT.locate(58, 65);
sulymarco 0:7077d8f28b3e 468 TFT.printf("Network error!");
sulymarco 0:7077d8f28b3e 469 printf("Network error!\n");
sulymarco 0:7077d8f28b3e 470 TFT.foreground(Magenta);
sulymarco 0:7077d8f28b3e 471 TFT.set_font((unsigned char*) Arial12x12);
sulymarco 0:7077d8f28b3e 472 TFT.locate(58, 106);
sulymarco 0:7077d8f28b3e 473
sulymarco 0:7077d8f28b3e 474 if(WorkCounterSafe >= 0)
sulymarco 0:7077d8f28b3e 475 {
sulymarco 0:7077d8f28b3e 476 TFT.printf("Expected working counter %d", ExpectWorkCounter);
sulymarco 0:7077d8f28b3e 477 TFT.locate(58, 118);
sulymarco 0:7077d8f28b3e 478 TFT.printf("Actual working counter %d", WorkCounterSafe);
sulymarco 0:7077d8f28b3e 479 printf("Expected working counter %d\n", ExpectWorkCounter);
sulymarco 0:7077d8f28b3e 480 printf("Actual working counter %d\n", WorkCounterSafe);
sulymarco 0:7077d8f28b3e 481 }
sulymarco 0:7077d8f28b3e 482 else
sulymarco 0:7077d8f28b3e 483 {
sulymarco 0:7077d8f28b3e 484 TFT.printf("Timeout");
sulymarco 0:7077d8f28b3e 485 printf("Timeout\n");
sulymarco 0:7077d8f28b3e 486 }
sulymarco 0:7077d8f28b3e 487
sulymarco 0:7077d8f28b3e 488 TFT.foreground(Green);
sulymarco 0:7077d8f28b3e 489 TFT.locate(58, 142);
sulymarco 0:7077d8f28b3e 490 TFT.printf("Please fix the error and");
sulymarco 0:7077d8f28b3e 491 TFT.locate(58, 154);
sulymarco 0:7077d8f28b3e 492 TFT.printf("press the reset button");
sulymarco 0:7077d8f28b3e 493 printf("Please fix the error and press the reset button \n");
sulymarco 0:7077d8f28b3e 494
sulymarco 0:7077d8f28b3e 495 SampleTicker.detach(); // stop the sample interrupt
sulymarco 0:7077d8f28b3e 496 while(1){} // and loop for ever
sulymarco 0:7077d8f28b3e 497 }
sulymarco 0:7077d8f28b3e 498
sulymarco 0:7077d8f28b3e 499 //----- slave 1 data management ------------
sulymarco 0:7077d8f28b3e 500
sulymarco 0:7077d8f28b3e 501 fTemperature = in_LAB_1->Temperature; // read the temperature
sulymarco 0:7077d8f28b3e 502
sulymarco 0:7077d8f28b3e 503 if (fTemperature != fPrevTemperature) // check if the temperature has changed
sulymarco 0:7077d8f28b3e 504 {
sulymarco 0:7077d8f28b3e 505 fPrevTemperature = fTemperature; // remember the current temperature value
sulymarco 0:7077d8f28b3e 506
sulymarco 0:7077d8f28b3e 507 if (VisuSlave == LAB_1) // if the HMI is setted to slave 1
sulymarco 0:7077d8f28b3e 508 { // visualize it
sulymarco 0:7077d8f28b3e 509 DrawTemperatureValue (fTemperature); //
sulymarco 0:7077d8f28b3e 510 }
sulymarco 0:7077d8f28b3e 511 }
sulymarco 0:7077d8f28b3e 512
sulymarco 0:7077d8f28b3e 513 bool AlarmStatus;
sulymarco 0:7077d8f28b3e 514 if (AlarmOnOff) // check if we are in alarm
sulymarco 0:7077d8f28b3e 515 { //
sulymarco 0:7077d8f28b3e 516 if ((AlarmMinMax && (fTemperature < fAlarmThreshold)) || (!AlarmMinMax && (fTemperature > fAlarmThreshold)))
sulymarco 0:7077d8f28b3e 517 {
sulymarco 0:7077d8f28b3e 518 out_LAB_1->Alarm = 0x01; // signal the alarm condition to the slave
sulymarco 0:7077d8f28b3e 519 AlarmStatus = true; // and to remember it
sulymarco 0:7077d8f28b3e 520 }
sulymarco 0:7077d8f28b3e 521 else
sulymarco 0:7077d8f28b3e 522 {
sulymarco 0:7077d8f28b3e 523 out_LAB_1->Alarm = 0x00; // signal the no alarm condition to the slave
sulymarco 0:7077d8f28b3e 524 AlarmStatus = false; // and remember it
sulymarco 0:7077d8f28b3e 525 }
sulymarco 0:7077d8f28b3e 526 }
sulymarco 0:7077d8f28b3e 527 else
sulymarco 0:7077d8f28b3e 528 {
sulymarco 0:7077d8f28b3e 529 out_LAB_1->Alarm = 0x00; // signal the no alarm condition to the slave
sulymarco 0:7077d8f28b3e 530 AlarmStatus = false; // and remember it
sulymarco 0:7077d8f28b3e 531 }
sulymarco 0:7077d8f28b3e 532
sulymarco 0:7077d8f28b3e 533 if (VisuSlave == LAB_1) // if the HMI is set to slave 1
sulymarco 0:7077d8f28b3e 534 { //
sulymarco 0:7077d8f28b3e 535 DrawAlarmStatus(AlarmStatus); // update the alarm status on the TFT
sulymarco 0:7077d8f28b3e 536 }
sulymarco 0:7077d8f28b3e 537
sulymarco 0:7077d8f28b3e 538 if (VisuSlave == LAB_1 && FirstRound) // if the HMI is set to slave 1
sulymarco 0:7077d8f28b3e 539 { // and it is the first time
sulymarco 0:7077d8f28b3e 540 FirstRound = false; //
sulymarco 0:7077d8f28b3e 541 DrawTemperatureValue (fTemperature); // draw the current temperature value
sulymarco 0:7077d8f28b3e 542 DrawAlarmSettings(fAlarmThreshold, AlarmOnOff, AlarmMinMax); // draw the alarm settings
sulymarco 0:7077d8f28b3e 543 }
sulymarco 0:7077d8f28b3e 544
sulymarco 0:7077d8f28b3e 545 //----- end LAB_1 ------------------------
sulymarco 0:7077d8f28b3e 546
sulymarco 0:7077d8f28b3e 547
sulymarco 0:7077d8f28b3e 548 //----- slave 2 data management ------------
sulymarco 0:7077d8f28b3e 549
sulymarco 0:7077d8f28b3e 550 PotValue = in_LAB_2->Potentiometer; // read the potentiometer value
sulymarco 0:7077d8f28b3e 551
sulymarco 0:7077d8f28b3e 552 if (VisuSlave == LAB_2) // if the HMI is setted to slave 2
sulymarco 0:7077d8f28b3e 553 { //
sulymarco 0:7077d8f28b3e 554 Time = SysMilliS(); // and the visualization timer is elapsed
sulymarco 0:7077d8f28b3e 555 if (Time-TimePotentiometer > TIME_POTENTIOMETER) //
sulymarco 0:7077d8f28b3e 556 { // draw the potentiometer value
sulymarco 0:7077d8f28b3e 557 TimePotentiometer = Time; //
sulymarco 0:7077d8f28b3e 558 DrawPotentiometerValue(PotValue); //
sulymarco 0:7077d8f28b3e 559 } //
sulymarco 0:7077d8f28b3e 560 }
sulymarco 0:7077d8f28b3e 561
sulymarco 0:7077d8f28b3e 562 Buttons = in_LAB_2->Buttons; // read the buttons status from the slave
sulymarco 0:7077d8f28b3e 563
sulymarco 0:7077d8f28b3e 564 if (Buttons != PrevButtons) // check if the buttons value has changed
sulymarco 0:7077d8f28b3e 565 {
sulymarco 0:7077d8f28b3e 566 PrevButtons = Buttons; // remember the current buttons value
sulymarco 0:7077d8f28b3e 567
sulymarco 0:7077d8f28b3e 568 if (VisuSlave == LAB_2) // if the HMI is setted to slave 2
sulymarco 0:7077d8f28b3e 569 { //
sulymarco 0:7077d8f28b3e 570 DrawButtonsValue(Buttons); // draw the current buttons value
sulymarco 0:7077d8f28b3e 571 } //
sulymarco 0:7077d8f28b3e 572 }
sulymarco 0:7077d8f28b3e 573
sulymarco 0:7077d8f28b3e 574 if (Segments != PrevSegments) // check if the segments value has changed
sulymarco 0:7077d8f28b3e 575 {
sulymarco 0:7077d8f28b3e 576 PrevSegments = Segments; // remember the current segments value
sulymarco 0:7077d8f28b3e 577
sulymarco 0:7077d8f28b3e 578 if (VisuSlave == LAB_2) // if the HMI is setted to slave 2
sulymarco 0:7077d8f28b3e 579 { //
sulymarco 0:7077d8f28b3e 580 DrawSegmentsValue(Segments); // draw the current segments value
sulymarco 0:7077d8f28b3e 581 } //
sulymarco 0:7077d8f28b3e 582 }
sulymarco 0:7077d8f28b3e 583
sulymarco 0:7077d8f28b3e 584 if (VisuSlave == LAB_2 && FirstRound) // if the HMI is set to slave 2
sulymarco 0:7077d8f28b3e 585 { // and it is the first time
sulymarco 0:7077d8f28b3e 586 FirstRound = false; //
sulymarco 0:7077d8f28b3e 587 //
sulymarco 0:7077d8f28b3e 588 DrawButtonsValue(Buttons); // draw the current buttons value
sulymarco 0:7077d8f28b3e 589 DrawSegmentsValue(Segments); // draw the current segments value
sulymarco 0:7077d8f28b3e 590 }
sulymarco 0:7077d8f28b3e 591
sulymarco 0:7077d8f28b3e 592 out_LAB_2->Segments = Segments; // send the segments status to the slave
sulymarco 0:7077d8f28b3e 593
sulymarco 0:7077d8f28b3e 594
sulymarco 0:7077d8f28b3e 595 //----- end LAB_2 ------------------------
sulymarco 0:7077d8f28b3e 596
EasyCAT 2:7d4fd6354015 597
sulymarco 0:7077d8f28b3e 598 TouchScreenManagement(); // check if the touchscreen is tapped
sulymarco 0:7077d8f28b3e 599 // and handle it
sulymarco 0:7077d8f28b3e 600
sulymarco 0:7077d8f28b3e 601 Time = SysMilliS(); // toggle the variable Blink every
sulymarco 0:7077d8f28b3e 602 if ((Time-TimeBlink) > TIME_BLINK) // TIME_BLINK mS
sulymarco 0:7077d8f28b3e 603 { //
sulymarco 0:7077d8f28b3e 604 TimeBlink = Time; // we use it to blink a field on the TFT
sulymarco 0:7077d8f28b3e 605 Blink = !Blink; //
sulymarco 0:7077d8f28b3e 606 }
sulymarco 0:7077d8f28b3e 607
sulymarco 0:7077d8f28b3e 608 IO_data.lock(); // copy the IO data from the safe area
sulymarco 0:7077d8f28b3e 609 memcpy(&IOmap[0], &IOmapSafe[0], IO_MAP_SIZE); // to the EtherCAT buffer
sulymarco 0:7077d8f28b3e 610 IO_data.unlock(); //
sulymarco 0:7077d8f28b3e 611
sulymarco 0:7077d8f28b3e 612 //Test_2 = 0;
sulymarco 0:7077d8f28b3e 613 }
sulymarco 0:7077d8f28b3e 614 }
sulymarco 0:7077d8f28b3e 615
sulymarco 0:7077d8f28b3e 616
sulymarco 0:7077d8f28b3e 617 //******************************************************************************
sulymarco 0:7077d8f28b3e 618
sulymarco 0:7077d8f28b3e 619
sulymarco 0:7077d8f28b3e 620
sulymarco 0:7077d8f28b3e 621 //******* general functions ****************************************************
sulymarco 0:7077d8f28b3e 622
sulymarco 0:7077d8f28b3e 623
sulymarco 0:7077d8f28b3e 624 //------ touchscreen management ------------------------------------------------
sulymarco 0:7077d8f28b3e 625
sulymarco 0:7077d8f28b3e 626 void TouchScreenManagement()
sulymarco 0:7077d8f28b3e 627 {
sulymarco 0:7077d8f28b3e 628 uint16_t X;
sulymarco 5:550a1222dcba 629 uint16_t Y;
sulymarco 0:7077d8f28b3e 630 int i;
sulymarco 5:550a1222dcba 631
sulymarco 5:550a1222dcba 632 #if defined ADA_TFT // if the touchscreen has been tapped
sulymarco 5:550a1222dcba 633 if (TSC.GetPoint(&X, &Y)) //
sulymarco 5:550a1222dcba 634 #else //
sulymarco 5:550a1222dcba 635 if (TouchRead(&X, &Y)) //
sulymarco 5:550a1222dcba 636 #endif
sulymarco 0:7077d8f28b3e 637 {
sulymarco 0:7077d8f28b3e 638 TimeTouchReleased = SysMilliS();
sulymarco 5:550a1222dcba 639
sulymarco 5:550a1222dcba 640 if(TouchWasReleased)
sulymarco 5:550a1222dcba 641 { // check if it is the side menu
sulymarco 0:7077d8f28b3e 642 // decrement slave button
sulymarco 5:550a1222dcba 643 if ((X>MENU_X) && (X>MENU_X+MENU_WIDTH/2) && (Y>MENU_HEIGHT) && (Y<MENU_HEIGHT*2))
sulymarco 5:550a1222dcba 644 { //
sulymarco 5:550a1222dcba 645 VisuSlave--;
sulymarco 5:550a1222dcba 646
sulymarco 5:550a1222dcba 647 if (VisuSlave == 0) //
sulymarco 5:550a1222dcba 648 VisuSlave = SLAVE_NUM; //
sulymarco 0:7077d8f28b3e 649 // the visualized slave has changed
sulymarco 5:550a1222dcba 650 DrawSlaveFixedParts(); // draw the new slave fixed parts
sulymarco 5:550a1222dcba 651 } //
sulymarco 5:550a1222dcba 652
sulymarco 0:7077d8f28b3e 653 // check if it is the side menu
sulymarco 0:7077d8f28b3e 654 // increment slave button
sulymarco 5:550a1222dcba 655 if ((X>MENU_X) && (X<MENU_X+MENU_WIDTH/2) && (Y>MENU_HEIGHT) && (Y<MENU_HEIGHT*2))
sulymarco 5:550a1222dcba 656 { //
sulymarco 5:550a1222dcba 657 VisuSlave++;
sulymarco 5:550a1222dcba 658
sulymarco 5:550a1222dcba 659 if (VisuSlave > SLAVE_NUM) //
sulymarco 5:550a1222dcba 660 VisuSlave = LAB_1; //
sulymarco 0:7077d8f28b3e 661 // the visualized slave has changed
sulymarco 5:550a1222dcba 662 DrawSlaveFixedParts(); // draw the new slave fixed parts
sulymarco 5:550a1222dcba 663 } //
sulymarco 5:550a1222dcba 664 }
sulymarco 0:7077d8f28b3e 665
sulymarco 0:7077d8f28b3e 666 switch (VisuSlave) // check which slave is visualized on the TFT
sulymarco 0:7077d8f28b3e 667 {
sulymarco 0:7077d8f28b3e 668 case (LAB_1): //-------------- slave 1 -----------------------
sulymarco 0:7077d8f28b3e 669
sulymarco 0:7077d8f28b3e 670
sulymarco 0:7077d8f28b3e 671 if(TouchWasReleased) // first check if the touch was
sulymarco 0:7077d8f28b3e 672 { // not tapped in the previous rounds
sulymarco 0:7077d8f28b3e 673 TouchWasReleased = false; // because for the following fields
sulymarco 0:7077d8f28b3e 674 TimeTouchReleased = SysMilliS(); // we don't want autorepeat
sulymarco 0:7077d8f28b3e 675
sulymarco 0:7077d8f28b3e 676 TimeAutoRepeat = SysMilliS(); // reload the autorepeat time
sulymarco 0:7077d8f28b3e 677
sulymarco 0:7077d8f28b3e 678 // handle taps on the ">" and "<" buttons
sulymarco 0:7077d8f28b3e 679 if ((X>ALARM_X) && (X<ALARM_X+(ALARM_WIDTH/2)) && (Y>ALARM_Y+ALARM_HEIGHT) && (Y<ALARM_Y+ALARM_HEIGHT*2))
sulymarco 0:7077d8f28b3e 680 { //
sulymarco 0:7077d8f28b3e 681 AlarmMinMax = true; //
sulymarco 0:7077d8f28b3e 682 DrawAlarmSettings(fAlarmThreshold, AlarmOnOff, AlarmMinMax);
sulymarco 0:7077d8f28b3e 683 } //
sulymarco 0:7077d8f28b3e 684 if ((X>ALARM_X+(ALARM_WIDTH/2)) && (X<ALARM_X+(ALARM_WIDTH)) && (Y>ALARM_Y+ALARM_HEIGHT) && (Y<ALARM_Y+ALARM_HEIGHT*2)) //
sulymarco 0:7077d8f28b3e 685 { //
sulymarco 0:7077d8f28b3e 686 AlarmMinMax = false; //
sulymarco 0:7077d8f28b3e 687 DrawAlarmSettings(fAlarmThreshold, AlarmOnOff, AlarmMinMax);
sulymarco 0:7077d8f28b3e 688 }
sulymarco 0:7077d8f28b3e 689
sulymarco 0:7077d8f28b3e 690 // handle taps on the "ON" and "OFF" buttons
sulymarco 0:7077d8f28b3e 691 if ((X>ALARM_X) && (X<ALARM_X+(ALARM_WIDTH/2)) && (Y>ALARM_Y) && (Y<ALARM_Y+ALARM_HEIGHT))
sulymarco 0:7077d8f28b3e 692 { //
sulymarco 0:7077d8f28b3e 693 AlarmOnOff = true; //
sulymarco 0:7077d8f28b3e 694 DrawAlarmSettings(fAlarmThreshold, AlarmOnOff, AlarmMinMax);
sulymarco 0:7077d8f28b3e 695 } //
sulymarco 0:7077d8f28b3e 696 if ((X>ALARM_X+(ALARM_WIDTH/2)) && (X<ALARM_X+(ALARM_WIDTH)) && (Y>ALARM_Y) && (Y<ALARM_Y+ALARM_HEIGHT))
sulymarco 0:7077d8f28b3e 697 { //
sulymarco 0:7077d8f28b3e 698 AlarmOnOff = false; //
sulymarco 0:7077d8f28b3e 699 DrawAlarmSettings(fAlarmThreshold, AlarmOnOff, AlarmMinMax);
sulymarco 0:7077d8f28b3e 700 }
sulymarco 0:7077d8f28b3e 701
sulymarco 0:7077d8f28b3e 702 // handle taps on the "+" and "-" buttons
sulymarco 0:7077d8f28b3e 703 // here we don't use the autorepeat to increment
sulymarco 0:7077d8f28b3e 704 // or decrement the threshold by 0.1
sulymarco 0:7077d8f28b3e 705 if ((X>ALARM_X) && (X<ALARM_X+(ALARM_WIDTH/2)) && (Y>ALARM_Y+(ALARM_HEIGHT*3)) && (Y<ALARM_Y+(ALARM_HEIGHT*4)))
sulymarco 0:7077d8f28b3e 706 { //
sulymarco 0:7077d8f28b3e 707 fAlarmThreshold += 0.1; //
sulymarco 0:7077d8f28b3e 708 DrawOnlyThreshold(fAlarmThreshold, AlarmOnOff, AlarmMinMax);
sulymarco 0:7077d8f28b3e 709 } //
sulymarco 0:7077d8f28b3e 710 if ((X>ALARM_X+(ALARM_WIDTH/2)) && (X<ALARM_X+(ALARM_WIDTH)) && (Y>ALARM_Y+(ALARM_HEIGHT*3)) && (Y<ALARM_Y+(ALARM_HEIGHT*4)))
sulymarco 0:7077d8f28b3e 711 { //
sulymarco 0:7077d8f28b3e 712 fAlarmThreshold -= 0.1; //
sulymarco 0:7077d8f28b3e 713 DrawOnlyThreshold(fAlarmThreshold, AlarmOnOff, AlarmMinMax);
sulymarco 0:7077d8f28b3e 714 } //
sulymarco 0:7077d8f28b3e 715 }
sulymarco 0:7077d8f28b3e 716
sulymarco 0:7077d8f28b3e 717 else // autorepeat management
sulymarco 0:7077d8f28b3e 718 { //
sulymarco 0:7077d8f28b3e 719 Time = SysMilliS(); //
sulymarco 0:7077d8f28b3e 720 if (Time-TimeAutoRepeat > TIME_AUTO_REP_START) //
sulymarco 0:7077d8f28b3e 721 { //
sulymarco 0:7077d8f28b3e 722 if (Time-TimeAutoRepRepeat > TIME_AUTO_REP_REPEAT)
sulymarco 0:7077d8f28b3e 723 { //
sulymarco 0:7077d8f28b3e 724 TimeAutoRepRepeat = Time; //
sulymarco 0:7077d8f28b3e 725
sulymarco 0:7077d8f28b3e 726 // handle taps on the "+" and "-" buttons
sulymarco 0:7077d8f28b3e 727 // here we use the autorepeat to increment
sulymarco 0:7077d8f28b3e 728 // or decrement the threshold by 1
sulymarco 0:7077d8f28b3e 729 if ((X>ALARM_X) && (X<ALARM_X+(ALARM_WIDTH/2)) && (Y>ALARM_Y+(ALARM_HEIGHT*3)) && (Y<ALARM_Y+(ALARM_HEIGHT*4)))
sulymarco 0:7077d8f28b3e 730 { //
sulymarco 0:7077d8f28b3e 731 fAlarmThreshold += 1; //
sulymarco 0:7077d8f28b3e 732 DrawOnlyThreshold(fAlarmThreshold, AlarmOnOff, AlarmMinMax);
sulymarco 0:7077d8f28b3e 733 } //
sulymarco 0:7077d8f28b3e 734 if ((X>ALARM_X+(ALARM_WIDTH/2)) && (X<ALARM_X+(ALARM_WIDTH)) && (Y>ALARM_Y+(ALARM_HEIGHT*3)) && (Y<ALARM_Y+(ALARM_HEIGHT*4)))
sulymarco 0:7077d8f28b3e 735 { //
sulymarco 0:7077d8f28b3e 736 fAlarmThreshold -= 1; //
sulymarco 0:7077d8f28b3e 737 DrawOnlyThreshold(fAlarmThreshold, AlarmOnOff, AlarmMinMax);
sulymarco 0:7077d8f28b3e 738 }
sulymarco 0:7077d8f28b3e 739 }
sulymarco 0:7077d8f28b3e 740 }
sulymarco 0:7077d8f28b3e 741 }
sulymarco 0:7077d8f28b3e 742
sulymarco 0:7077d8f28b3e 743 break; //-------------- end slave 1 -------------------
sulymarco 0:7077d8f28b3e 744
sulymarco 0:7077d8f28b3e 745
sulymarco 0:7077d8f28b3e 746
sulymarco 0:7077d8f28b3e 747 case (LAB_2): //-------------- slave 2 -----------------------
sulymarco 0:7077d8f28b3e 748
sulymarco 0:7077d8f28b3e 749 if(TouchWasReleased) // first check if the touch was
sulymarco 0:7077d8f28b3e 750 { // not tapped in the previous rounds
sulymarco 0:7077d8f28b3e 751 TouchWasReleased = false; // because for the following fields
sulymarco 0:7077d8f28b3e 752 TimeTouchReleased = SysMilliS(); // we don't want autorepeat
sulymarco 0:7077d8f28b3e 753
sulymarco 0:7077d8f28b3e 754 uint8_t Mask = 0x08; // check if one of the segment is tapped
sulymarco 0:7077d8f28b3e 755 //
sulymarco 0:7077d8f28b3e 756 for (i=0; i<4; i++) //
sulymarco 0:7077d8f28b3e 757 { //
sulymarco 0:7077d8f28b3e 758 if ((X>SEG_X+(i*SEG_STEP)) && (X<SEG_X+(i*SEG_STEP)+SEG_WIDTH) && (Y>SEG_Y) && (Y<SEG_Y+SEG_HEIGHT*3))
sulymarco 0:7077d8f28b3e 759 { //
sulymarco 0:7077d8f28b3e 760 Segments ^= Mask >> i; //
sulymarco 0:7077d8f28b3e 761 } //
sulymarco 0:7077d8f28b3e 762 } //
sulymarco 0:7077d8f28b3e 763 }
sulymarco 0:7077d8f28b3e 764 break; //----------------- end slave 2 ----------------
sulymarco 0:7077d8f28b3e 765 }
sulymarco 0:7077d8f28b3e 766 }
sulymarco 0:7077d8f28b3e 767
sulymarco 0:7077d8f28b3e 768 else
sulymarco 0:7077d8f28b3e 769 {
sulymarco 0:7077d8f28b3e 770 if ( SysMilliS()-TimeTouchReleased > TIME_TOUCH_RELEASED) // if the touchscreen was not
sulymarco 0:7077d8f28b3e 771 { // tapped for enought time
sulymarco 0:7077d8f28b3e 772 TouchWasReleased = true; // remember it
sulymarco 0:7077d8f28b3e 773
sulymarco 0:7077d8f28b3e 774 }
sulymarco 0:7077d8f28b3e 775 }
sulymarco 0:7077d8f28b3e 776 }
sulymarco 0:7077d8f28b3e 777
sulymarco 0:7077d8f28b3e 778
sulymarco 0:7077d8f28b3e 779 //----- draw the fixed part of the visualized slave ----------------------------
sulymarco 0:7077d8f28b3e 780
sulymarco 0:7077d8f28b3e 781 void DrawSlaveFixedParts()
sulymarco 0:7077d8f28b3e 782 {
sulymarco 0:7077d8f28b3e 783 int i;
sulymarco 0:7077d8f28b3e 784 int Offset;
sulymarco 0:7077d8f28b3e 785
sulymarco 0:7077d8f28b3e 786 TFT.cls(); // clear screen
sulymarco 0:7077d8f28b3e 787 DrawSideMenu(VisuSlave); // draw the side menu
sulymarco 0:7077d8f28b3e 788
sulymarco 0:7077d8f28b3e 789 switch (VisuSlave) // check which slave is visualized on the TFT
sulymarco 0:7077d8f28b3e 790 {
sulymarco 0:7077d8f28b3e 791 case (LAB_1): //-------------- slave 1 ---------------------------
sulymarco 0:7077d8f28b3e 792
sulymarco 0:7077d8f28b3e 793 TFT.foreground(Yellow); // draw the thermometer
sulymarco 0:7077d8f28b3e 794
sulymarco 0:7077d8f28b3e 795 TFT.circle(TERMO_X, TERMO_Y, 18, Yellow); // bowl
sulymarco 0:7077d8f28b3e 796 TFT.fillcircle(TERMO_X, TERMO_Y, 17, Red); //
sulymarco 0:7077d8f28b3e 797
sulymarco 0:7077d8f28b3e 798 // tube
sulymarco 0:7077d8f28b3e 799 TFT.rect(TERMO_X-8, TERMO_Y-185, TERMO_X+8, TERMO_Y-18, Yellow);
sulymarco 0:7077d8f28b3e 800 TFT.fillrect(TERMO_X-7, TERMO_Y-18, TERMO_X+7, TERMO_Y-16, Red);
sulymarco 0:7077d8f28b3e 801
sulymarco 0:7077d8f28b3e 802 for (i=0; i<8; i++) // scale
sulymarco 0:7077d8f28b3e 803 { //
sulymarco 0:7077d8f28b3e 804 if (i <1) //
sulymarco 0:7077d8f28b3e 805 Offset = 5; //
sulymarco 0:7077d8f28b3e 806 else //
sulymarco 0:7077d8f28b3e 807 Offset = 0; //
sulymarco 0:7077d8f28b3e 808 //
sulymarco 0:7077d8f28b3e 809 TFT.line(TERMO_X-8, (TERMO_Y-28)-(i *20), TERMO_X-18,(TERMO_Y-28)-(i*20), Yellow);
sulymarco 0:7077d8f28b3e 810 TFT.locate(TERMO_X-48+Offset, TERMO_Y-(i*20)-32); //
sulymarco 0:7077d8f28b3e 811 TFT.set_font((unsigned char*) Arial12x12); //
sulymarco 0:7077d8f28b3e 812 TFT.printf("%3d",(i*10)-10); //
sulymarco 0:7077d8f28b3e 813 }
sulymarco 0:7077d8f28b3e 814
sulymarco 0:7077d8f28b3e 815 TFT.foreground(Green); //
sulymarco 0:7077d8f28b3e 816 TFT.set_font((unsigned char*) Arial28x28); //
sulymarco 0:7077d8f28b3e 817 TFT.locate(TERMO_X+25, TERMO_Y+28); //
sulymarco 0:7077d8f28b3e 818 TFT.printf("C"); //
sulymarco 0:7077d8f28b3e 819
sulymarco 0:7077d8f28b3e 820 // draw the alarm control panel
sulymarco 0:7077d8f28b3e 821
sulymarco 0:7077d8f28b3e 822 // frame
sulymarco 0:7077d8f28b3e 823 TFT.rect(ALARM_X, ALARM_Y, ALARM_X+ALARM_WIDTH, ALARM_Y+(ALARM_HEIGHT*4), Magenta);
sulymarco 0:7077d8f28b3e 824 //
sulymarco 0:7077d8f28b3e 825 for (i=0; i<3; i++) //
sulymarco 0:7077d8f28b3e 826 { //
sulymarco 0:7077d8f28b3e 827 TFT.line(ALARM_X, ALARM_Y+((i+1)*ALARM_HEIGHT) , ALARM_X+ALARM_WIDTH, ALARM_Y+((i+1)*ALARM_HEIGHT), Magenta);
sulymarco 0:7077d8f28b3e 828 } //
sulymarco 0:7077d8f28b3e 829 //
sulymarco 0:7077d8f28b3e 830 TFT.line(ALARM_X+(ALARM_WIDTH/2), ALARM_Y, ALARM_X+(ALARM_WIDTH/2), ALARM_Y+(ALARM_HEIGHT*2), Magenta);
sulymarco 0:7077d8f28b3e 831 TFT.line(ALARM_X+(ALARM_WIDTH/2), ALARM_Y+(ALARM_HEIGHT*3), ALARM_X+(ALARM_WIDTH/2), ALARM_Y+(ALARM_HEIGHT*4), Magenta);
sulymarco 0:7077d8f28b3e 832
sulymarco 0:7077d8f28b3e 833 TFT.set_font((unsigned char*) Arial28x28); // "+" and "-"
sulymarco 0:7077d8f28b3e 834 TFT.foreground(Green); //
sulymarco 0:7077d8f28b3e 835 TFT.locate(ALARM_X+11, ALARM_Y+(ALARM_HEIGHT*3)+9); //
sulymarco 0:7077d8f28b3e 836 TFT.printf("+"); //
sulymarco 0:7077d8f28b3e 837 TFT.locate(ALARM_X+(ALARM_WIDTH/2)+14, ALARM_Y+(ALARM_HEIGHT*3)+9);
sulymarco 0:7077d8f28b3e 838 TFT.printf("-"); //
sulymarco 0:7077d8f28b3e 839
sulymarco 0:7077d8f28b3e 840 // alarm bar
sulymarco 0:7077d8f28b3e 841 TFT.rect(ALARM_X-30, ALARM_Y, ALARM_X-25, ALARM_Y+168, Yellow);
sulymarco 0:7077d8f28b3e 842
sulymarco 0:7077d8f28b3e 843 TFT.set_font((unsigned char*) Arial12x12); // draw "ALARM SETTINGS"
sulymarco 0:7077d8f28b3e 844 TFT.foreground(Yellow); //
sulymarco 0:7077d8f28b3e 845 TFT.locate(ALARM_X-35, ALARM_Y+14+(ALARM_HEIGHT*4)); //
sulymarco 0:7077d8f28b3e 846 TFT.printf("ALARM SETTINGS"); //
sulymarco 0:7077d8f28b3e 847
sulymarco 0:7077d8f28b3e 848 break;
sulymarco 0:7077d8f28b3e 849
sulymarco 0:7077d8f28b3e 850
sulymarco 0:7077d8f28b3e 851
sulymarco 0:7077d8f28b3e 852
sulymarco 0:7077d8f28b3e 853 case (LAB_2): //-------------- slave 2 ---------------------------
sulymarco 0:7077d8f28b3e 854
sulymarco 0:7077d8f28b3e 855 TFT.foreground(Yellow);
sulymarco 0:7077d8f28b3e 856
sulymarco 0:7077d8f28b3e 857 for (i=0; i<4; i++) // draw the segments fixed parts
sulymarco 0:7077d8f28b3e 858 { //
sulymarco 0:7077d8f28b3e 859 TFT.rect(SEG_X+(i*SEG_STEP), SEG_Y, SEG_X+SEG_WIDTH+(i*SEG_STEP), SEG_Y+SEG_HEIGHT, Yellow);
sulymarco 0:7077d8f28b3e 860 //
sulymarco 0:7077d8f28b3e 861 TFT.set_font((unsigned char*) Arial12x12); //
sulymarco 0:7077d8f28b3e 862 TFT.locate(SEG_X+44, SEG_Y+SEG_HEIGHT+12); //
sulymarco 0:7077d8f28b3e 863 TFT.printf("MIDDLE SEGMENTS"); //
sulymarco 0:7077d8f28b3e 864 } //
sulymarco 0:7077d8f28b3e 865
sulymarco 0:7077d8f28b3e 866 for (i=0; i<3; i++) // draw the buttons fixed parts
sulymarco 0:7077d8f28b3e 867 { //
sulymarco 0:7077d8f28b3e 868 TFT.circle(BUTTONS_X+(i*BUTTONS_STEP), BUTTONS_Y, BUTTONS_R, Red);
sulymarco 0:7077d8f28b3e 869 TFT.circle(BUTTONS_X+BUTTONS_WIDTH+(i*BUTTONS_STEP), BUTTONS_Y, BUTTONS_R, Red);
sulymarco 0:7077d8f28b3e 870 //
sulymarco 0:7077d8f28b3e 871 TFT.line(BUTTONS_X+(i*BUTTONS_STEP)-BUTTONS_R, BUTTONS_Y, BUTTONS_X+(i*BUTTONS_STEP)-BUTTONS_R-4, BUTTONS_Y, Red);
sulymarco 0:7077d8f28b3e 872 TFT.line(BUTTONS_X+BUTTONS_WIDTH+(i*BUTTONS_STEP)+BUTTONS_R, BUTTONS_Y, BUTTONS_X+BUTTONS_WIDTH+(i*BUTTONS_STEP)+BUTTONS_R+4 , BUTTONS_Y, Red);
sulymarco 0:7077d8f28b3e 873 } //
sulymarco 0:7077d8f28b3e 874 //
sulymarco 0:7077d8f28b3e 875 TFT.set_font((unsigned char*) Arial12x12); //
sulymarco 0:7077d8f28b3e 876 TFT.locate(BUTTONS_X+38, BUTTONS_Y+12); //
sulymarco 0:7077d8f28b3e 877 TFT.printf("BUTTONS"); //
sulymarco 0:7077d8f28b3e 878
sulymarco 0:7077d8f28b3e 879 // draw the potentiometer window fixed parts
sulymarco 0:7077d8f28b3e 880 TFT.rect(ANALOG_X, ANALOG_Y, ANALOG_X+ANALOG_WIDTH, ANALOG_Y+ANALOG_HEIGHT, Magenta);
sulymarco 0:7077d8f28b3e 881 //
sulymarco 0:7077d8f28b3e 882 TFT.set_font((unsigned char*) Arial12x12); //
sulymarco 0:7077d8f28b3e 883 TFT.locate(ANALOG_X+35, ANALOG_Y+ANALOG_HEIGHT+12); //
sulymarco 0:7077d8f28b3e 884 TFT.printf("POTENTIOMETER"); //
sulymarco 0:7077d8f28b3e 885
sulymarco 0:7077d8f28b3e 886 Graph_x = 0;
sulymarco 0:7077d8f28b3e 887
sulymarco 0:7077d8f28b3e 888 DrawSegmentsValue(Segments); // draw the segments status
sulymarco 0:7077d8f28b3e 889 DrawButtonsValue (Buttons); // draw the buttons status
sulymarco 0:7077d8f28b3e 890 DrawPotentiometerValue(PotValue); // draw the potentiometer value
EasyCAT 2:7d4fd6354015 891 break;
sulymarco 0:7077d8f28b3e 892 }
sulymarco 0:7077d8f28b3e 893
sulymarco 0:7077d8f28b3e 894 FirstRound = true;
sulymarco 0:7077d8f28b3e 895 }
sulymarco 0:7077d8f28b3e 896
sulymarco 0:7077d8f28b3e 897
sulymarco 0:7077d8f28b3e 898 //---- draw the menu on the upper right part of the TFT ------------------------
sulymarco 0:7077d8f28b3e 899
sulymarco 0:7077d8f28b3e 900 void DrawSideMenu(uint8_t Slave)
sulymarco 0:7077d8f28b3e 901 { // draw the side menu frame
sulymarco 0:7077d8f28b3e 902 TFT.rect(MENU_X, MENU_Y, MENU_X+MENU_WIDTH, MENU_Y+MENU_HEIGHT*2, Green);
sulymarco 0:7077d8f28b3e 903 TFT.line(MENU_X, MENU_HEIGHT, MENU_X+MENU_WIDTH, MENU_HEIGHT, Green);
sulymarco 0:7077d8f28b3e 904 TFT.line(MENU_X+(MENU_WIDTH/2), MENU_Y+MENU_HEIGHT, MENU_X+(MENU_WIDTH/2), MENU_Y+(MENU_HEIGHT*2), Green);
sulymarco 0:7077d8f28b3e 905
sulymarco 0:7077d8f28b3e 906 // draw the slave number
sulymarco 0:7077d8f28b3e 907 TFT.fillrect(MENU_X+1, MENU_Y+1, MENU_X+MENU_WIDTH-1, MENU_Y+MENU_HEIGHT-1, Red);
sulymarco 0:7077d8f28b3e 908 TFT.set_font((unsigned char*) Arial12x12); //
sulymarco 0:7077d8f28b3e 909 TFT.foreground(Yellow); //
sulymarco 0:7077d8f28b3e 910 TFT.locate(MENU_X+8 , MENU_Y+18); //
sulymarco 0:7077d8f28b3e 911 TFT.background(Red); //
sulymarco 0:7077d8f28b3e 912 TFT.printf("SLAVE %d", Slave); //
sulymarco 0:7077d8f28b3e 913 TFT.background(Black); //
sulymarco 0:7077d8f28b3e 914
sulymarco 0:7077d8f28b3e 915 TFT.set_font((unsigned char*) Arial28x28); // draw "+" and "-"
sulymarco 0:7077d8f28b3e 916 TFT.foreground(Green); //
sulymarco 0:7077d8f28b3e 917 TFT.locate (MENU_X+11, MENU_Y+MENU_HEIGHT+9); //
sulymarco 0:7077d8f28b3e 918 TFT.printf("+"); //
sulymarco 0:7077d8f28b3e 919 TFT.locate (MENU_X+(MENU_WIDTH/2)+14, MENU_Y+MENU_HEIGHT+9); //
sulymarco 0:7077d8f28b3e 920 TFT.printf("-"); //
sulymarco 0:7077d8f28b3e 921
sulymarco 0:7077d8f28b3e 922 TFT.set_font((unsigned char*) Arial12x12); // draw the slave name
sulymarco 0:7077d8f28b3e 923 TFT.foreground(Red); //
sulymarco 0:7077d8f28b3e 924 //
sulymarco 0:7077d8f28b3e 925 TFT.locate(MENU_X, MENU_Y+(MENU_HEIGHT*2)+12); //
sulymarco 0:7077d8f28b3e 926 TFT.printf("%.9s", ec_slave[Slave].name); //
sulymarco 0:7077d8f28b3e 927 }
sulymarco 0:7077d8f28b3e 928
sulymarco 0:7077d8f28b3e 929
sulymarco 0:7077d8f28b3e 930 //---- draw the starting banner ------------------------------------------------
sulymarco 0:7077d8f28b3e 931
sulymarco 0:7077d8f28b3e 932 void DrawBanner()
sulymarco 0:7077d8f28b3e 933 {
sulymarco 0:7077d8f28b3e 934 TFT.set_font((unsigned char*) Arial24x23);
sulymarco 0:7077d8f28b3e 935 TFT.foreground(Red);
sulymarco 0:7077d8f28b3e 936 TFT.locate(30, 50);
sulymarco 0:7077d8f28b3e 937 TFT.printf("EasyCAT");
sulymarco 0:7077d8f28b3e 938 TFT.locate(30, 80);
sulymarco 0:7077d8f28b3e 939 TFT.printf("SOEM MASTER");
sulymarco 0:7077d8f28b3e 940
sulymarco 0:7077d8f28b3e 941 TFT.set_font((unsigned char*) Arial12x12);
sulymarco 0:7077d8f28b3e 942 TFT.foreground(Green);
sulymarco 0:7077d8f28b3e 943 TFT.locate(30, 140);
sulymarco 0:7077d8f28b3e 944 TFT.printf("www.bausano.net");
sulymarco 0:7077d8f28b3e 945
sulymarco 0:7077d8f28b3e 946 TFT.foreground(Green);
sulymarco 0:7077d8f28b3e 947 TFT.locate(30, 160);
sulymarco 0:7077d8f28b3e 948 TFT.printf("www.easycatshield.com");
sulymarco 0:7077d8f28b3e 949
sulymarco 0:7077d8f28b3e 950 TFT.locate(30, 180);
sulymarco 5:550a1222dcba 951 TFT.printf("https://openethercatsociety.github.io/");
sulymarco 5:550a1222dcba 952
sulymarco 5:550a1222dcba 953 TFT.foreground(Red);
sulymarco 5:550a1222dcba 954 TFT.locate(30, 220);
sulymarco 5:550a1222dcba 955 #if defined ADA_TFT
sulymarco 5:550a1222dcba 956 TFT.printf("Adafruit TFT");
sulymarco 5:550a1222dcba 957 #else
sulymarco 5:550a1222dcba 958 TFT.printf("Seeed Studio TFT");
sulymarco 5:550a1222dcba 959 #endif
sulymarco 0:7077d8f28b3e 960 }
sulymarco 0:7077d8f28b3e 961
sulymarco 0:7077d8f28b3e 962
sulymarco 0:7077d8f28b3e 963
sulymarco 0:7077d8f28b3e 964 //****** slave 1 functions *****************************************************
sulymarco 0:7077d8f28b3e 965
sulymarco 0:7077d8f28b3e 966 //---- draw the temperature value both in analog and in digital form ------------
sulymarco 0:7077d8f28b3e 967
sulymarco 0:7077d8f28b3e 968 void DrawTemperatureValue(float fValue)
sulymarco 0:7077d8f28b3e 969 {
sulymarco 0:7077d8f28b3e 970 TFT.set_font((unsigned char*) Arial28x28); // digital visualization
sulymarco 0:7077d8f28b3e 971 TFT.foreground(Green); //
sulymarco 0:7077d8f28b3e 972 TFT.locate(TERMO_X-45, TERMO_Y+28); //
sulymarco 0:7077d8f28b3e 973 TFT.printf("%4.1f", fValue); //
sulymarco 0:7077d8f28b3e 974
sulymarco 0:7077d8f28b3e 975 if (fValue > 68) // limit the value for the
sulymarco 0:7077d8f28b3e 976 fValue = 68; // analog visualization
sulymarco 0:7077d8f28b3e 977 //
sulymarco 0:7077d8f28b3e 978 if (fValue < -15) //
sulymarco 0:7077d8f28b3e 979 fValue = -15; //
sulymarco 0:7077d8f28b3e 980
sulymarco 0:7077d8f28b3e 981 int LenColonnina = fValue * 2; // analog visualization
sulymarco 0:7077d8f28b3e 982 TFT.fillrect(TERMO_X-7, TERMO_Y-184, TERMO_X+7, TERMO_Y-18-LenColonnina-30-1, Black);
sulymarco 0:7077d8f28b3e 983 TFT.fillrect(TERMO_X-7, TERMO_Y-18-LenColonnina-30, TERMO_X+7, TERMO_Y-18, Red);
sulymarco 0:7077d8f28b3e 984 }
sulymarco 0:7077d8f28b3e 985
sulymarco 0:7077d8f28b3e 986
sulymarco 0:7077d8f28b3e 987 //---- draw the current parameter of the temperature alarm ---------------------
sulymarco 0:7077d8f28b3e 988
sulymarco 0:7077d8f28b3e 989 void DrawAlarmSettings(float fThreshold, bool OnOff, bool MinMax)
sulymarco 0:7077d8f28b3e 990 {
sulymarco 0:7077d8f28b3e 991
sulymarco 0:7077d8f28b3e 992 if (OnOff) // alarm on
sulymarco 0:7077d8f28b3e 993 { // draw "ON" on red background
sulymarco 0:7077d8f28b3e 994 // and "OFF"" on black background
sulymarco 0:7077d8f28b3e 995 DisplayInRect (ALARM_X, ALARM_Y, 10, 14, "ON", Red, (unsigned char*)Arial12x12);
sulymarco 0:7077d8f28b3e 996 DisplayInRect (ALARM_X+(ALARM_WIDTH/2), ALARM_Y, 10, 14, "OFF", Black, (unsigned char*)Arial12x12);
sulymarco 0:7077d8f28b3e 997 }
sulymarco 0:7077d8f28b3e 998 else // alarm off
sulymarco 0:7077d8f28b3e 999 { // and "ON"" on black background
sulymarco 0:7077d8f28b3e 1000 // draw "OFF" on red background
sulymarco 0:7077d8f28b3e 1001 DisplayInRect (ALARM_X, ALARM_Y, 10, 14, "ON", Black, (unsigned char*)Arial12x12);
sulymarco 0:7077d8f28b3e 1002 DisplayInRect (ALARM_X+(ALARM_WIDTH/2), ALARM_Y, 10, 14, "OFF", Red, (unsigned char*)Arial12x12);
sulymarco 0:7077d8f28b3e 1003 }
sulymarco 0:7077d8f28b3e 1004
sulymarco 0:7077d8f28b3e 1005
sulymarco 0:7077d8f28b3e 1006 if (MinMax) // alarm when temperature < threshold
sulymarco 0:7077d8f28b3e 1007 { // draw ">" on red background
sulymarco 0:7077d8f28b3e 1008 // and ">"" on black background
sulymarco 0:7077d8f28b3e 1009 DisplayInRect (ALARM_X, ALARM_Y+ALARM_HEIGHT, 10, 9, ">", Red, (unsigned char*)Arial28x28);
sulymarco 0:7077d8f28b3e 1010 DisplayInRect (ALARM_X+(ALARM_WIDTH/2), ALARM_Y+ALARM_HEIGHT, 10, 9, "<", Black, (unsigned char*)Arial28x28);
sulymarco 0:7077d8f28b3e 1011 }
sulymarco 0:7077d8f28b3e 1012 else // alarm when temperature > threshold
sulymarco 0:7077d8f28b3e 1013 { // draw ">" on black background
sulymarco 0:7077d8f28b3e 1014 // and ">"" on red background
sulymarco 0:7077d8f28b3e 1015 DisplayInRect (ALARM_X, ALARM_Y+ALARM_HEIGHT, 10, 9, ">", Black, (unsigned char*)Arial28x28);
sulymarco 0:7077d8f28b3e 1016 DisplayInRect (ALARM_X+(ALARM_WIDTH/2), ALARM_Y+ALARM_HEIGHT, 10, 9, "<", Red, (unsigned char*)Arial28x28);
sulymarco 0:7077d8f28b3e 1017 }
sulymarco 0:7077d8f28b3e 1018
sulymarco 0:7077d8f28b3e 1019 DrawOnlyThreshold(fThreshold, OnOff, MinMax);
sulymarco 0:7077d8f28b3e 1020 }
sulymarco 0:7077d8f28b3e 1021
sulymarco 0:7077d8f28b3e 1022
sulymarco 0:7077d8f28b3e 1023 //---- draw only the threshold setting -----------------------------------------
sulymarco 0:7077d8f28b3e 1024
sulymarco 0:7077d8f28b3e 1025 void DrawOnlyThreshold(float fThreshold, bool OnOff, bool MinMax) // this function is used to update
sulymarco 0:7077d8f28b3e 1026 { // only the threshold value not to
sulymarco 0:7077d8f28b3e 1027 // flicker the TFT, when "+" or
sulymarco 0:7077d8f28b3e 1028 // "-" are tapped
sulymarco 0:7077d8f28b3e 1029
sulymarco 0:7077d8f28b3e 1030 TFT.foreground(Yellow); // draw the alarm threshold
sulymarco 0:7077d8f28b3e 1031 TFT.set_font((unsigned char*) Arial12x12); // in digital form
sulymarco 0:7077d8f28b3e 1032 TFT.locate(ALARM_X+26, ALARM_Y+14+(ALARM_HEIGHT*2)); //
sulymarco 0:7077d8f28b3e 1033 TFT.printf(" "); //
sulymarco 0:7077d8f28b3e 1034 TFT.locate(ALARM_X+26, ALARM_Y+14+(ALARM_HEIGHT*2)); //
sulymarco 0:7077d8f28b3e 1035 TFT.printf("%+3.1f", fThreshold); //
sulymarco 0:7077d8f28b3e 1036
sulymarco 0:7077d8f28b3e 1037 if (fThreshold > 68) // limit the value for the
sulymarco 0:7077d8f28b3e 1038 fThreshold = 68; // analog visualization
sulymarco 0:7077d8f28b3e 1039 //
sulymarco 0:7077d8f28b3e 1040 if (fThreshold < -15) //
sulymarco 0:7077d8f28b3e 1041 fThreshold = -15; //
sulymarco 0:7077d8f28b3e 1042
sulymarco 0:7077d8f28b3e 1043 int LenAlarmBar = fThreshold * 2;
sulymarco 0:7077d8f28b3e 1044
sulymarco 0:7077d8f28b3e 1045 if (OnOff) // alarm on
sulymarco 0:7077d8f28b3e 1046 { //
sulymarco 0:7077d8f28b3e 1047 if (MinMax) // fill the threshold bar
sulymarco 0:7077d8f28b3e 1048 { // in accordance with the MinMax setting
sulymarco 0:7077d8f28b3e 1049 TFT.fillrect(ALARM_X-29, ALARM_Y+1, ALARM_X-26, ALARM_Y+167-LenAlarmBar-30, Green);
sulymarco 0:7077d8f28b3e 1050 TFT.fillrect(ALARM_X-29, ALARM_Y+167-LenAlarmBar-30, ALARM_X-26, ALARM_Y+167, Red);
sulymarco 0:7077d8f28b3e 1051 } //
sulymarco 0:7077d8f28b3e 1052 else //
sulymarco 0:7077d8f28b3e 1053 { //
sulymarco 0:7077d8f28b3e 1054 TFT.fillrect(ALARM_X-29, ALARM_Y+1, ALARM_X-26, ALARM_Y+167-LenAlarmBar-30, Red);
sulymarco 0:7077d8f28b3e 1055 TFT.fillrect(ALARM_X-29, ALARM_Y+167-LenAlarmBar-30, ALARM_X-26, ALARM_Y+167, Green);
sulymarco 0:7077d8f28b3e 1056 } //
sulymarco 0:7077d8f28b3e 1057 }
sulymarco 0:7077d8f28b3e 1058
sulymarco 0:7077d8f28b3e 1059 else // alarm off
sulymarco 0:7077d8f28b3e 1060 { //
sulymarco 0:7077d8f28b3e 1061 // clear the threshold bar
sulymarco 0:7077d8f28b3e 1062 TFT.fillrect(ALARM_X-29, ALARM_Y+1, ALARM_X-26, ALARM_Y+167, Black);
sulymarco 0:7077d8f28b3e 1063 } //
sulymarco 0:7077d8f28b3e 1064 }
sulymarco 0:7077d8f28b3e 1065
sulymarco 0:7077d8f28b3e 1066
sulymarco 0:7077d8f28b3e 1067 //---- draw the blinking alarm signal ------------------------------------------
sulymarco 0:7077d8f28b3e 1068
sulymarco 0:7077d8f28b3e 1069 void DrawAlarmStatus(bool Alarm)
sulymarco 0:7077d8f28b3e 1070 {
sulymarco 0:7077d8f28b3e 1071 bool static PrevAlarmVisu;
sulymarco 0:7077d8f28b3e 1072
sulymarco 0:7077d8f28b3e 1073 TFT.set_font((unsigned char*) Arial28x28);
sulymarco 0:7077d8f28b3e 1074 TFT.foreground(Red);
sulymarco 0:7077d8f28b3e 1075 TFT.locate(TERMO_X+90, TERMO_Y+28);
sulymarco 0:7077d8f28b3e 1076
sulymarco 0:7077d8f28b3e 1077 if (Alarm && Blink && !PrevAlarmVisu)
sulymarco 0:7077d8f28b3e 1078 {
sulymarco 0:7077d8f28b3e 1079 TFT.printf("ALARM !");
sulymarco 0:7077d8f28b3e 1080 PrevAlarmVisu = true;
sulymarco 0:7077d8f28b3e 1081 }
sulymarco 0:7077d8f28b3e 1082 else if ((!Alarm && PrevAlarmVisu) || (Alarm && !Blink && PrevAlarmVisu))
sulymarco 0:7077d8f28b3e 1083 {
sulymarco 0:7077d8f28b3e 1084 TFT.printf(" ");
sulymarco 0:7077d8f28b3e 1085 PrevAlarmVisu = false;
sulymarco 0:7077d8f28b3e 1086 }
sulymarco 0:7077d8f28b3e 1087 }
sulymarco 0:7077d8f28b3e 1088
sulymarco 0:7077d8f28b3e 1089
sulymarco 0:7077d8f28b3e 1090 //----- draw a rectangle with text and colored background ----------------------
sulymarco 0:7077d8f28b3e 1091
sulymarco 0:7077d8f28b3e 1092 void DisplayInRect (int X, int Y, int X_off, int Y_off, char* Value, int BackColor, unsigned char* Font)
sulymarco 0:7077d8f28b3e 1093 {
sulymarco 0:7077d8f28b3e 1094 TFT.set_font(Font);
sulymarco 0:7077d8f28b3e 1095 TFT.foreground(Green);
sulymarco 0:7077d8f28b3e 1096
sulymarco 0:7077d8f28b3e 1097 TFT.fillrect(X+1, Y+1, X+(MENU_WIDTH/2)-1, Y+MENU_HEIGHT-1, BackColor);
sulymarco 0:7077d8f28b3e 1098
sulymarco 0:7077d8f28b3e 1099 TFT.locate(X+X_off , Y+Y_off);
sulymarco 0:7077d8f28b3e 1100 TFT.background(BackColor);
sulymarco 0:7077d8f28b3e 1101 TFT.printf("%s", Value );
sulymarco 0:7077d8f28b3e 1102 TFT.background(Black);
sulymarco 0:7077d8f28b3e 1103 }
sulymarco 0:7077d8f28b3e 1104
sulymarco 0:7077d8f28b3e 1105
sulymarco 0:7077d8f28b3e 1106
sulymarco 0:7077d8f28b3e 1107
sulymarco 0:7077d8f28b3e 1108 //****** slave 2 functions *****************************************************
sulymarco 0:7077d8f28b3e 1109
sulymarco 0:7077d8f28b3e 1110 //------------------------------------------------------------------------------
sulymarco 0:7077d8f28b3e 1111
sulymarco 0:7077d8f28b3e 1112 void DrawButtonsValue (uint8_t Value)
sulymarco 0:7077d8f28b3e 1113 {
sulymarco 0:7077d8f28b3e 1114 uint8_t Slope;
sulymarco 0:7077d8f28b3e 1115 int i;
sulymarco 0:7077d8f28b3e 1116
sulymarco 0:7077d8f28b3e 1117 for (i=0; i<3; i++)
sulymarco 0:7077d8f28b3e 1118 {
sulymarco 0:7077d8f28b3e 1119 if ((Value & 0x04) == 0x04)
sulymarco 0:7077d8f28b3e 1120 Slope = BUTTONS_R;
sulymarco 0:7077d8f28b3e 1121 else
sulymarco 0:7077d8f28b3e 1122 Slope = 16;
sulymarco 0:7077d8f28b3e 1123
sulymarco 0:7077d8f28b3e 1124 TFT.fillrect(BUTTONS_X+(i*BUTTONS_STEP), BUTTONS_Y-16-1, BUTTONS_X+BUTTONS_WIDTH+(i*BUTTONS_STEP), BUTTONS_Y-BUTTONS_R-1, Black);
sulymarco 0:7077d8f28b3e 1125
sulymarco 0:7077d8f28b3e 1126 TFT.line(BUTTONS_X+(i*BUTTONS_STEP), BUTTONS_Y-BUTTONS_R-1, BUTTONS_X+BUTTONS_WIDTH+(i*BUTTONS_STEP), BUTTONS_Y-Slope-1, Red);
sulymarco 0:7077d8f28b3e 1127
sulymarco 0:7077d8f28b3e 1128 Value = Value << 1;
sulymarco 0:7077d8f28b3e 1129 }
sulymarco 0:7077d8f28b3e 1130 }
sulymarco 0:7077d8f28b3e 1131
sulymarco 0:7077d8f28b3e 1132
sulymarco 0:7077d8f28b3e 1133 //------------------------------------------------------------------------------
sulymarco 0:7077d8f28b3e 1134
sulymarco 0:7077d8f28b3e 1135 void DrawSegmentsValue(uint8_t Value)
sulymarco 0:7077d8f28b3e 1136 {
sulymarco 0:7077d8f28b3e 1137 int i;
sulymarco 0:7077d8f28b3e 1138 int Color;
sulymarco 0:7077d8f28b3e 1139
sulymarco 0:7077d8f28b3e 1140 for (i=0; i<4; i++)
sulymarco 0:7077d8f28b3e 1141 {
sulymarco 0:7077d8f28b3e 1142 if ((Value & 0x08) == 0x08)
sulymarco 0:7077d8f28b3e 1143 Color = Red;
sulymarco 0:7077d8f28b3e 1144 else
sulymarco 0:7077d8f28b3e 1145 Color = Black;
sulymarco 0:7077d8f28b3e 1146
sulymarco 0:7077d8f28b3e 1147 TFT.fillrect(SEG_X+(i*SEG_STEP)+1, SEG_Y+1, SEG_X+SEG_WIDTH+(i*SEG_STEP)-1, SEG_Y+SEG_HEIGHT-1, Color);
sulymarco 0:7077d8f28b3e 1148
sulymarco 0:7077d8f28b3e 1149 Value = Value << 1;
sulymarco 0:7077d8f28b3e 1150 }
sulymarco 0:7077d8f28b3e 1151 }
sulymarco 0:7077d8f28b3e 1152
sulymarco 0:7077d8f28b3e 1153
sulymarco 0:7077d8f28b3e 1154 //---- draw the potentiometer value --------------------------------------------
sulymarco 0:7077d8f28b3e 1155
sulymarco 0:7077d8f28b3e 1156 void DrawPotentiometerValue (uint16_t PotValue)
sulymarco 0:7077d8f28b3e 1157 {
sulymarco 0:7077d8f28b3e 1158
sulymarco 0:7077d8f28b3e 1159 if (PotValue != PrevPotValue)
sulymarco 0:7077d8f28b3e 1160 {
sulymarco 0:7077d8f28b3e 1161 PrevPotValue = PotValue;
sulymarco 0:7077d8f28b3e 1162
sulymarco 0:7077d8f28b3e 1163 TFT.set_font((unsigned char*) Arial12x12); //
sulymarco 0:7077d8f28b3e 1164 TFT.foreground(Green); //
sulymarco 0:7077d8f28b3e 1165 TFT.locate(ANALOG_X+170, ANALOG_Y+ANALOG_HEIGHT+12); //
sulymarco 0:7077d8f28b3e 1166 TFT.printf("%4d", (int)PotValue); //
sulymarco 0:7077d8f28b3e 1167 }
sulymarco 0:7077d8f28b3e 1168
sulymarco 0:7077d8f28b3e 1169
sulymarco 0:7077d8f28b3e 1170 if (++Graph_x > ANALOG_WIDTH-3)
sulymarco 0:7077d8f28b3e 1171 {
sulymarco 0:7077d8f28b3e 1172 Graph_x = 0;
sulymarco 0:7077d8f28b3e 1173 }
sulymarco 0:7077d8f28b3e 1174
sulymarco 0:7077d8f28b3e 1175 PotValue = PotValue / (1023/(ANALOG_HEIGHT-2));
sulymarco 0:7077d8f28b3e 1176
sulymarco 0:7077d8f28b3e 1177 TFT.pixel(ANALOG_X+Graph_x+1, ANALOG_Y+ANALOG_HEIGHT-PotValue-1, Green);
sulymarco 0:7077d8f28b3e 1178
sulymarco 0:7077d8f28b3e 1179 int Overflow = (ANALOG_WIDTH-2) - (Graph_x+16);
sulymarco 0:7077d8f28b3e 1180 if (Overflow < 0)
sulymarco 0:7077d8f28b3e 1181 {
sulymarco 0:7077d8f28b3e 1182 TFT.fillrect(ANALOG_X+Graph_x+2, ANALOG_Y+1, ANALOG_X+Graph_x+16+Overflow, ANALOG_Y+ANALOG_HEIGHT-1, Black);
sulymarco 0:7077d8f28b3e 1183 TFT.fillrect(ANALOG_X+1, ANALOG_Y+1, ANALOG_X+1-Overflow, ANALOG_Y+ANALOG_HEIGHT-1, Black);
sulymarco 0:7077d8f28b3e 1184 }
sulymarco 0:7077d8f28b3e 1185 else
sulymarco 0:7077d8f28b3e 1186 {
sulymarco 0:7077d8f28b3e 1187 TFT.fillrect(ANALOG_X+Graph_x+2, ANALOG_Y+1, ANALOG_X+Graph_x+16, ANALOG_Y+ANALOG_HEIGHT-1, Black);
sulymarco 0:7077d8f28b3e 1188 }
sulymarco 0:7077d8f28b3e 1189 }
sulymarco 0:7077d8f28b3e 1190
sulymarco 0:7077d8f28b3e 1191
sulymarco 0:7077d8f28b3e 1192 //****** touchscreen functions *************************************************
sulymarco 0:7077d8f28b3e 1193
sulymarco 0:7077d8f28b3e 1194 //----- read touchscreen status ------------------------------------------------
sulymarco 0:7077d8f28b3e 1195
sulymarco 0:7077d8f28b3e 1196 bool TouchRead(uint16_t* X, uint16_t* Y)
sulymarco 0:7077d8f28b3e 1197 {
sulymarco 0:7077d8f28b3e 1198 bool Result = false;
sulymarco 0:7077d8f28b3e 1199
sulymarco 0:7077d8f28b3e 1200 {
sulymarco 0:7077d8f28b3e 1201 if (TouchRead_Z()) // if the touchscreen is tapped
sulymarco 0:7077d8f28b3e 1202 { //
sulymarco 0:7077d8f28b3e 1203 *X = TouchRead_X(); // read also the X and Y axis
sulymarco 0:7077d8f28b3e 1204 *Y = TouchRead_Y(); //
sulymarco 0:7077d8f28b3e 1205
sulymarco 0:7077d8f28b3e 1206 wait_us (1000);
sulymarco 0:7077d8f28b3e 1207
sulymarco 0:7077d8f28b3e 1208 if (TouchRead_Z()) // if the touchscreen is still tapped
sulymarco 0:7077d8f28b3e 1209 { // we assume that the result is good
sulymarco 0:7077d8f28b3e 1210 Result = true; //
sulymarco 0:7077d8f28b3e 1211
sulymarco 0:7077d8f28b3e 1212 // TFT.pixel (*X, *Y, White); //debug - draw the touched point on the TFT
sulymarco 0:7077d8f28b3e 1213 }
sulymarco 0:7077d8f28b3e 1214 }
sulymarco 0:7077d8f28b3e 1215 }
sulymarco 0:7077d8f28b3e 1216
sulymarco 0:7077d8f28b3e 1217 return Result;
sulymarco 0:7077d8f28b3e 1218 }
sulymarco 0:7077d8f28b3e 1219
sulymarco 0:7077d8f28b3e 1220
sulymarco 0:7077d8f28b3e 1221 //----- read touchscreen X axis ------------------------------------------------
sulymarco 0:7077d8f28b3e 1222
sulymarco 0:7077d8f28b3e 1223 uint16_t TouchRead_X()
sulymarco 0:7077d8f28b3e 1224 {
sulymarco 0:7077d8f28b3e 1225 float fValue;
sulymarco 0:7077d8f28b3e 1226
sulymarco 0:7077d8f28b3e 1227 DigitalIn Ym(PIN_YM); // set the I/O
sulymarco 0:7077d8f28b3e 1228 Ym.mode(PullDown); //
sulymarco 0:7077d8f28b3e 1229 //
sulymarco 0:7077d8f28b3e 1230 DigitalOut Xp(PIN_XP); //
sulymarco 0:7077d8f28b3e 1231 Xp = 1; //
sulymarco 0:7077d8f28b3e 1232 //
sulymarco 0:7077d8f28b3e 1233 DigitalOut Xm(PIN_XM); //
sulymarco 0:7077d8f28b3e 1234 Xm = 0; //
sulymarco 0:7077d8f28b3e 1235 //
sulymarco 0:7077d8f28b3e 1236 AnalogIn Yp(PIN_YP); //
sulymarco 0:7077d8f28b3e 1237
sulymarco 0:7077d8f28b3e 1238 fValue = ReadAnalog(Yp); // read the axis
sulymarco 0:7077d8f28b3e 1239
sulymarco 0:7077d8f28b3e 1240 fValue -= TOUCH_X_OFFSET; // rectify offsett and gain
sulymarco 0:7077d8f28b3e 1241 if (fValue <0 ) //
sulymarco 0:7077d8f28b3e 1242 fValue = 0; //
sulymarco 0:7077d8f28b3e 1243 //
sulymarco 0:7077d8f28b3e 1244 fValue *= TOUCH_X_GAIN; //
sulymarco 0:7077d8f28b3e 1245
sulymarco 0:7077d8f28b3e 1246 return (uint16_t)fValue;
sulymarco 0:7077d8f28b3e 1247 }
sulymarco 0:7077d8f28b3e 1248
sulymarco 0:7077d8f28b3e 1249
sulymarco 0:7077d8f28b3e 1250 //----- read touchscreen Y axis ------------------------------------------------
sulymarco 0:7077d8f28b3e 1251
sulymarco 0:7077d8f28b3e 1252 uint16_t TouchRead_Y()
sulymarco 0:7077d8f28b3e 1253 {
sulymarco 0:7077d8f28b3e 1254 float fValue;
sulymarco 0:7077d8f28b3e 1255
sulymarco 0:7077d8f28b3e 1256 DigitalIn Xm(PIN_XM); // set the I/O
sulymarco 0:7077d8f28b3e 1257 Xm.mode(PullDown); //
sulymarco 0:7077d8f28b3e 1258 //
sulymarco 0:7077d8f28b3e 1259 DigitalOut Yp(PIN_YP); //
sulymarco 0:7077d8f28b3e 1260 Yp = 1; //
sulymarco 0:7077d8f28b3e 1261 //
sulymarco 0:7077d8f28b3e 1262 DigitalOut Ym(PIN_YM); //
sulymarco 0:7077d8f28b3e 1263 Ym = 0; //
sulymarco 0:7077d8f28b3e 1264 //
sulymarco 0:7077d8f28b3e 1265 AnalogIn Xp(PIN_XP); //
sulymarco 0:7077d8f28b3e 1266
sulymarco 0:7077d8f28b3e 1267 fValue = ReadAnalog(Xp); // read the axis
sulymarco 0:7077d8f28b3e 1268
sulymarco 0:7077d8f28b3e 1269 fValue -= TOUCH_Y_OFFSET; // rectify offset and gain
sulymarco 0:7077d8f28b3e 1270 if (fValue <0 ) //
sulymarco 0:7077d8f28b3e 1271 fValue = 0; //
sulymarco 0:7077d8f28b3e 1272 //
sulymarco 0:7077d8f28b3e 1273 fValue *= TOUCH_Y_GAIN; //
sulymarco 0:7077d8f28b3e 1274
sulymarco 0:7077d8f28b3e 1275 return (uint16_t)fValue;
sulymarco 0:7077d8f28b3e 1276 }
sulymarco 0:7077d8f28b3e 1277
sulymarco 0:7077d8f28b3e 1278
sulymarco 0:7077d8f28b3e 1279 //----- read touchscreen Z axis ------------------------------------------------
sulymarco 0:7077d8f28b3e 1280
sulymarco 0:7077d8f28b3e 1281 bool TouchRead_Z() // read the Z axis to see if the
sulymarco 0:7077d8f28b3e 1282 { // touchscreen has been tapped
sulymarco 0:7077d8f28b3e 1283 float fValue = 0;
sulymarco 0:7077d8f28b3e 1284 bool Result;
sulymarco 0:7077d8f28b3e 1285
sulymarco 0:7077d8f28b3e 1286 DigitalIn Yp(PIN_YP); // set the I/O
sulymarco 0:7077d8f28b3e 1287 Yp.mode(PullUp); //
sulymarco 0:7077d8f28b3e 1288 //
sulymarco 0:7077d8f28b3e 1289 AnalogIn Ym(PIN_YM); //
sulymarco 0:7077d8f28b3e 1290 //
sulymarco 0:7077d8f28b3e 1291 DigitalOut Xm(PIN_XM); //
sulymarco 0:7077d8f28b3e 1292 Xm = 0; //
sulymarco 0:7077d8f28b3e 1293 //
sulymarco 0:7077d8f28b3e 1294 DigitalOut Xp(PIN_XP); //
sulymarco 0:7077d8f28b3e 1295 Xp = 0; //
sulymarco 0:7077d8f28b3e 1296
sulymarco 0:7077d8f28b3e 1297 for (int i = 0; i<TOUCH_SAMPLES; i++) // read the axis several times
sulymarco 0:7077d8f28b3e 1298 { // and average the result
sulymarco 0:7077d8f28b3e 1299 wait_us(10); //
sulymarco 0:7077d8f28b3e 1300 //
sulymarco 0:7077d8f28b3e 1301 fValue += Ym.read(); //
sulymarco 0:7077d8f28b3e 1302 } //
sulymarco 0:7077d8f28b3e 1303 fValue /= TOUCH_SAMPLES; //
sulymarco 0:7077d8f28b3e 1304
sulymarco 0:7077d8f28b3e 1305 if (fValue < TOUCH_THRESHOLD) // compare the result with
sulymarco 0:7077d8f28b3e 1306 Result = true; // the threshold
sulymarco 0:7077d8f28b3e 1307 else //
sulymarco 0:7077d8f28b3e 1308 Result = false; //
sulymarco 0:7077d8f28b3e 1309
sulymarco 0:7077d8f28b3e 1310 return Result; //
sulymarco 0:7077d8f28b3e 1311 }
sulymarco 0:7077d8f28b3e 1312
sulymarco 0:7077d8f28b3e 1313
sulymarco 0:7077d8f28b3e 1314 //----- read touchscreen X or Y axis with a window filter ----------------------
sulymarco 0:7077d8f28b3e 1315
sulymarco 0:7077d8f28b3e 1316 float ReadAnalog (AnalogIn AnaCh) // check that consecutive readings
sulymarco 0:7077d8f28b3e 1317 { // fall in the acceptance window
sulymarco 0:7077d8f28b3e 1318 float fArray[TOUCH_SAMPLES]; //
sulymarco 0:7077d8f28b3e 1319 float fResult;
sulymarco 0:7077d8f28b3e 1320 float fDiff;
sulymarco 0:7077d8f28b3e 1321
sulymarco 0:7077d8f28b3e 1322 int Rounds = TOUCH_MAX_ROUNDS; // maximum number of attempts
sulymarco 0:7077d8f28b3e 1323
sulymarco 0:7077d8f28b3e 1324 for (int i=0; i<TOUCH_SAMPLES; i++)
sulymarco 0:7077d8f28b3e 1325 {
sulymarco 0:7077d8f28b3e 1326 wait_us(10);
sulymarco 0:7077d8f28b3e 1327
sulymarco 0:7077d8f28b3e 1328 fResult = AnaCh.read();
sulymarco 0:7077d8f28b3e 1329
sulymarco 0:7077d8f28b3e 1330 if (i>0)
sulymarco 0:7077d8f28b3e 1331 {
sulymarco 0:7077d8f28b3e 1332 fDiff = abs(fResult - fArray[i-1]);
sulymarco 0:7077d8f28b3e 1333
sulymarco 0:7077d8f28b3e 1334 if (fDiff > TOUCH_WINDOW)
sulymarco 0:7077d8f28b3e 1335 i= -1;
sulymarco 0:7077d8f28b3e 1336 else
sulymarco 0:7077d8f28b3e 1337 fArray[i] = fResult;
sulymarco 0:7077d8f28b3e 1338
sulymarco 0:7077d8f28b3e 1339 if (Rounds-- < 0)
sulymarco 0:7077d8f28b3e 1340 {
sulymarco 0:7077d8f28b3e 1341 fResult = 0;
sulymarco 0:7077d8f28b3e 1342 return fResult;
sulymarco 0:7077d8f28b3e 1343 }
sulymarco 0:7077d8f28b3e 1344 }
sulymarco 0:7077d8f28b3e 1345
sulymarco 0:7077d8f28b3e 1346 else
sulymarco 0:7077d8f28b3e 1347 {
sulymarco 0:7077d8f28b3e 1348 fArray[i] = fResult;
sulymarco 0:7077d8f28b3e 1349 }
sulymarco 0:7077d8f28b3e 1350 }
sulymarco 0:7077d8f28b3e 1351
sulymarco 0:7077d8f28b3e 1352 fResult =0;
sulymarco 0:7077d8f28b3e 1353
sulymarco 0:7077d8f28b3e 1354 for (int i=0; i<TOUCH_SAMPLES; i++)
sulymarco 0:7077d8f28b3e 1355 {
sulymarco 0:7077d8f28b3e 1356 fResult += fArray[i];
sulymarco 0:7077d8f28b3e 1357 }
sulymarco 0:7077d8f28b3e 1358
sulymarco 0:7077d8f28b3e 1359 return fResult /= TOUCH_SAMPLES;
sulymarco 0:7077d8f28b3e 1360 }
sulymarco 0:7077d8f28b3e 1361
sulymarco 0:7077d8f28b3e 1362
sulymarco 0:7077d8f28b3e 1363