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

Committer:
EasyCAT
Date:
Thu Oct 26 12:29:46 2023 +0000
Revision:
9:e43af5b19937
Parent:
7:d71db73dcdd9
Removed debug test

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