-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1445 lines (1091 loc) · 67.1 KB
/
main.cpp
File metadata and controls
1445 lines (1091 loc) · 67.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//********************************************************************************************
// *
// This software is distributed as an example, "AS IS", in the hope that it could *
// be useful, WITHOUT ANY WARRANTY of any kind, express or implied, included, but *
// not limited, to the warranties of merchantability, fitness for a particular *
// purpose, and non infringiment. In no event shall the authors be liable for any *
// claim, damages or other liability, arising from, or in connection with this software. *
// *
//********************************************************************************************
// The AB&T EasyCAT LAB is a complete experimental EtherCAT® system, composed by
// one master and two slaves.
// The EasyCAT LAB software is provided free of charge and its pourpose is to allow
// makers and educational institutes to experiment with the EtherCAT® protocol.
//
// The EasyCAT LAB is developed by "AB&T Tecnologie Informatiche" Via dell'About 2A Ivrea Italy.
// www.bausano.net
// www.easycatshield.com
//
// The EasyCAT LAB uses the SOEM library by rt:labs
// https://rt-labs.com/products/soem-ethercat-master-stack/
//
// EtherCAT® is a registered trademark and patented technology, licensed by Beckhoff Automation GmbH.
// www.beckhoff.com
// www.ethercat.org
//******************************************************************************
// IMPORTANT!!!
#define ADA_TFT // If your EasyCAT LAB uses the Adafruit TFT
// you must uncomment this define
//#define SEEED_TFT // If your EasyCAT LAB uses the Seeed Studio TFT
// you must uncomment this define
//#define PARA_TFT // If your EasyCAT LAB uses the parallel interface TFT
// you must uncomment this define
//******************************************************************************
#define ETH_TXBUFNB 16
#define ETH_RXBUFNB 16
#include "mbed.h"
#ifndef __align
#define __align MBED_ALIGN
#endif
#include "config.h"
#include "SPI_TFT_ILI9341.h"
#include "Arial12x12.h"
#include "Arial24x23.h"
#include "Arial28x28.h"
#include "font_big.h"
#include "soem_start.h"
#include "SPI_STMPE610.h"
#define CYCLE_TIME 1000 // master cycle time in uS
// 1000 = 1mS
#define SysMilliS() (uint32_t)Kernel::get_ms_count()
UnbufferedSerial pc(USBTX,USBRX,115200); // set the debug serial line speed to 115200
//---- TFT with resistive touchscreen pins -------------------------------------
// the display used is the SeeedStudio 2.8 inch TFT v2.0
// http://wiki.seeedstudio.com/2.8inch_TFT_Touch_Shield_v2.0/
//
// or the Adafruit 2.8" with resistive touchscreen
// https://www.adafruit.com/product/1651
//
// or the parallel interface ARD SHD 2,8TD
#ifdef ADA_TFT // pins for the Adafruit TFT
#define PIN_CS_TFT D10 //
#define PIN_DC_TFT D9 //
#define PIN_CS_TSC D8 //
#define PIN_MOSI D11 // SPI
#define PIN_MISO D12 //
#define PIN_SCLK D13 //
#endif
#ifdef SEEED_TFT // pins for the SeeedStudio TFT
#define PIN_CS_TFT D5 //
#define PIN_DC_TFT D6 //
#define PIN_MOSI D11 // SPI
#define PIN_MISO D12 //
#define PIN_SCLK D13 //
#endif //
#ifdef PARA_TFT // pins for the parallel interface TFT
#define PIN_D0_TFT D8 //
#define PIN_D1_TFT D9 //
#define PIN_D2_TFT D2 //
#define PIN_D3_TFT D3 //
#define PIN_D4_TFT D4 //
#define PIN_D5_TFT D5 //
#define PIN_D6_TFT D6 //
#define PIN_D7_TFT D7 //
#define PIN_RD_TFT A0 //
#define PIN_WR_TFT A1 //
#define PIN_DC_TFT A2 //
#define PIN_CS_TFT A3 //
#define PIN_RES_TFT A4 //
#endif
#ifdef SEEED_TFT
#define PIN_YP A3 // resistive touchscreen
#define PIN_YM A1 //
#define PIN_XM A2 //
#define PIN_XP A0 //
#else //
#define PIN_XP A3 //
#define PIN_YP A2 //
#define PIN_XM D9 //
#define PIN_YM D8 //
#endif
//---- touchscreen parameters --------------------------------------------------
#define TOUCH_SAMPLES 8
#define TOUCH_WINDOW 0.05
#define TOUCH_THRESHOLD 0.2
#define TOUCH_MAX_ROUNDS 16
#define TIME_TOUCH_RELEASED 300
#ifdef PARA_TFT
#define TOUCH_X_OFFSET 0.075
#else
#define TOUCH_X_OFFSET 0.118
#endif
#define TOUCH_X_GAIN 402
#define TOUCH_Y_OFFSET 0.090
#define TOUCH_Y_GAIN 302
//---- side menu parameters ----------------------------------------------------
#define MENU_Y 0
#define MENU_WIDTH 83
#define MENU_HEIGHT 42
#define MENU_X (319-MENU_WIDTH)
//---- slave parameters - LAB_1 EasyCAT with multifunction shield --------------
#define TERMO_X 55
#define TERMO_Y 185
#define ALARM_X 140
#define ALARM_Y 0
#define ALARM_WIDTH 83
#define ALARM_HEIGHT 42
#define TIME_BLINK 1000
#define TIME_AUTO_REP_START 1000
#define TIME_AUTO_REP_REPEAT 200
//---- slave parameters - LAB_2 EasyCAT with multifunction shield --------------
#define SEG_X 0
#define SEG_Y 0
#define SEG_WIDTH 42
#define SEG_HEIGHT 16
#define SEG_STEP 60
#define BUTTONS_X 38
#define BUTTONS_Y 80
#define BUTTONS_WIDTH 26
#define BUTTONS_R 3
#define BUTTONS_STEP 60
#define ANALOG_X 0
#define ANALOG_Y 120
#define ANALOG_WIDTH 222
#define ANALOG_HEIGHT 80
#define TIME_REP_SEG 300
#define TIME_POTENTIOMETER 100
//---- local functions ---------------------------------------------------------
void DrawBanner();
void DrawSlaveFixedParts();
void DrawSideMenu (uint8_t Slave);
void DrawTemperatureValue(float fValue);
void DrawAlarmSettings(float fThreshold, bool OnOff, bool MinMax);
void DrawOnlyThreshold(float fThreshold, bool OnOff, bool MinMax);
void DrawAlarmStatus(bool Alarm);
void DisplayInRect(int X, int Y, int X_off, int Y_off, char* Value, int BackColor, unsigned char* Font);
void DrawButtonsValue(uint8_t Value);
void DrawSegmentsValue(uint8_t Value);
void DrawPotentiometerValue(uint16_t PotValue);
float ReadAnalog(AnalogIn Ana);
void Application();
void TouchScreenManagement();
bool TouchRead(uint16_t* X, uint16_t* Y);
uint16_t TouchRead_X();
uint16_t TouchRead_Y();
bool TouchRead_Z();
//---- global variables --------------------------------------------------------
bool TouchWasReleased;
bool FirstRound;
//------------------------------------------------------------------------------
#ifdef PARA_TFT
SPI_TFT_ILI9341 TFT(PIN_D0_TFT, PIN_D1_TFT, PIN_D2_TFT, PIN_D3_TFT, PIN_D4_TFT, PIN_D5_TFT,
PIN_D6_TFT, PIN_D7_TFT, PIN_RD_TFT, PIN_WR_TFT, PIN_CS_TFT, PIN_DC_TFT, PIN_RES_TFT, "PARA");
#endif
#ifdef ADA_TFT
SPI_TFT_ILI9341 TFT(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, NC, PIN_DC_TFT, 27000000, "ADA");
#endif
#ifdef SEEED_TFT
SPI_TFT_ILI9341 TFT(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, NC, PIN_DC_TFT, 27000000, "SEEED");
#endif
#ifdef ADA_TFT // touchscreen controller for the Adafruit TFT
SPI_STMPE610 TSC(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TSC);
#endif
Ticker SampleTicker;
Thread thread;
DigitalOut Test_1(D1); // debug test points
DigitalOut Test_2(D2); //
DigitalOut Test_3(D3); //
DigitalOut Test_4(D4); //
//------------------------------------------------------------------------------
float fAlarmThreshold;
bool AlarmOnOff;
bool AlarmMinMax;
uint16_t PotValue;
uint16_t PrevPotValue;
uint8_t Buttons;
uint8_t PrevButtons;
uint8_t Segments;
uint8_t PrevSegments;
int16_t Graph_x;
uint8_t Outputs;
uint8_t PrevOutputs;
uint8_t Inputs;
uint8_t PrevInputs;
int8_t VisuSlave;
//------------------------------------------------------------------------------
uint32_t Time;
uint32_t TimeBlink;
uint32_t TimeAutoRepeat;
uint32_t TimeAutoRepRepeat;
uint32_t TimePotentiometer;
uint32_t TimeTouchReleased;
bool Blink;
uint8_t TouchAxes = 0;
uint8_t Action = 0;
uint16_t X;
uint16_t Y;
float fTemperature;
float static fPrevTemperature;
int ExpectWorkCounter;
int WorkCounter;
int WorkCounterSafe;
bool NetworkError;
bool NetworkErrorSafe;
#define DATA_EXCHANGE_FLAG (1UL << 0)
#define APPLICATION_FLAG (1UL << 1)
EventFlags event_flags;
Mutex IO_data;
//---- data exchange thread ----------------------------------------------------
void ExchangeMaster()
{
while (true)
{
event_flags.wait_any(DATA_EXCHANGE_FLAG); // the thread waits for the synchronization flag
//Test_1 = 1;
IO_data.lock(); // Ethercat data exchange
ec_send_processdata(); //
WorkCounter = ec_receive_processdata(EC_TIMEOUTRET);
if (WorkCounter != ExpectWorkCounter)
NetworkError = true;
else
NetworkError = false;
IO_data.unlock(); //
event_flags.set(APPLICATION_FLAG); // synchronize the application
//Test_1 = 0;
}
}
//----- thicker generated sample time ------------------------------------------
void SampleIsr() // set the event that starts
{ // the data exchange
event_flags.set(DATA_EXCHANGE_FLAG); //
} //
//****** initialization ********************************************************
int main()
{
int i;
printf("Start \n");
Test_1 = 0;
Test_2 = 0;
Test_3 = 0;
Test_4 = 0;
#ifdef ADA_TFT
TFT.set_orientation(1);
#endif
#ifdef SEEED_TFT
TFT.set_orientation(3);
#endif
#ifdef PARA_TFT
TFT.set_orientation(1);
#endif
TFT.background(Black);
TFT.cls();
DrawBanner();
NetworkError = false;
VisuSlave = LAB_1;
AlarmOnOff = true;
AlarmMinMax = false;
fAlarmThreshold = 28.8;
fTemperature = 0;
TouchWasReleased = true;
if (ec_init(NULL)) // init SOEM
{
printf("ec_init succeeded.\n");
printf("Scanning the network\n");
TFT.cls();
TFT.set_font((unsigned char*) Arial12x12);
TFT.foreground(Green);
TFT.locate(0, 0);
TFT.printf("Scanning the network\n");
if (network_scanning())
{
if (network_configuration()) // check network configuration
{
ec_config_map(&IOmap); // map the I/O
MapLocalStructures();
printf("\nSlaves mapped, state to SAFE_OP.\n");
// wait for all slaves to reach SAFE_OP state
ec_statecheck(0, EC_STATE_SAFE_OP, EC_TIMEOUTSTATE);
printf("Request operational state for all slaves\n");
ec_slave[0].state = EC_STATE_OPERATIONAL;
ec_send_processdata(); // send one valid process data to make outputs in slaves happy
ExpectWorkCounter = ec_receive_processdata(EC_TIMEOUTRET);
ec_writestate(0); // request OP state for all slaves
// wait for all slaves to reach OP state
ec_statecheck(0, EC_STATE_OPERATIONAL, EC_TIMEOUTSTATE);
if (ec_slave[0].state == EC_STATE_OPERATIONAL )
{
printf("Operational state reached for all slaves.\n");
}
else
{
printf("Not all slaves reached operational state.\n");
ec_readstate();
for(i = 1; i<=ec_slavecount ; i++)
{
if(ec_slave[i].state != EC_STATE_OPERATIONAL)
{
printf("Slave %d State=0x%04x StatusCode=0x%04x\n",
i, ec_slave[i].state, ec_slave[i].ALstatuscode);
}
}
TFT.foreground(Red);
TFT.locate(0, 0);
TFT.printf("Not all slaves reached operational state!");
while(1){}
}
DrawSlaveFixedParts();
thread.start(ExchangeMaster);
thread.set_priority(osPriorityRealtime);
SampleTicker.attach_us(&SampleIsr, CYCLE_TIME);
Application();
}
else
{
printf("Mismatch of network units!\n");
TFT.foreground(Red);
TFT.locate(0, 0);
TFT.printf("Mismatch of network units!");
while(1){}
}
}
else
{
printf("No slaves found!\n");
TFT.foreground(Red);
TFT.printf("No slaves found!");
while(1){}
}
}
else
{
printf("Ethernet interface init failed!");
TFT.foreground(Red);
TFT.locate(0, 0);
TFT.printf("Ethernet interface init failed!");
while(1){}
}
}
//****** user master application **********************************************
void Application()
{
while(1)
{
event_flags.wait_any(APPLICATION_FLAG); // the application waits for the synchronization flag
//Test_2 = 1;
IO_data.lock(); // copy the Ethercat data to a safe buffer
memcpy(&IOmapSafe[0], &IOmap[0], IO_MAP_SIZE); //
//
if (NetworkError) //
{ //
NetworkErrorSafe = NetworkError; //
WorkCounterSafe = WorkCounter; //
} //
IO_data.unlock(); //
if (NetworkErrorSafe)
{
TFT.rect(35,50, 285, 182, Red);
TFT.fillrect(36,51, 284, 181, Black);
TFT.foreground(Red);
TFT.set_font((unsigned char*) Arial28x28);
TFT.locate(58, 65);
TFT.printf("Network error!");
printf("Network error!\n");
TFT.foreground(Magenta);
TFT.set_font((unsigned char*) Arial12x12);
TFT.locate(58, 106);
if(WorkCounterSafe >= 0)
{
TFT.printf("Expected working counter %d", ExpectWorkCounter);
TFT.locate(58, 118);
TFT.printf("Actual working counter %d", WorkCounterSafe);
printf("Expected working counter %d\n", ExpectWorkCounter);
printf("Actual working counter %d\n", WorkCounterSafe);
}
else
{
TFT.printf("Timeout");
printf("Timeout\n");
}
TFT.foreground(Green);
TFT.locate(58, 142);
TFT.printf("Please fix the error and");
TFT.locate(58, 154);
TFT.printf("press the reset button");
printf("Please fix the error and press the reset button \n");
SampleTicker.detach(); // stop the sample interrupt
while(1){} // and loop for ever
}
//----- slave 1 data management ------------
fTemperature = in_LAB_1->Temperature; // read the temperature
if (fTemperature != fPrevTemperature) // check if the temperature has changed
{
fPrevTemperature = fTemperature; // remember the current temperature value
if (VisuSlave == LAB_1) // if the HMI is setted to slave 1
{ // visualize it
DrawTemperatureValue (fTemperature); //
}
}
bool AlarmStatus;
if (AlarmOnOff) // check if we are in alarm
{ //
if ((AlarmMinMax && (fTemperature < fAlarmThreshold)) || (!AlarmMinMax && (fTemperature > fAlarmThreshold)))
{
out_LAB_1->Alarm = 0x01; // signal the alarm condition to the slave
AlarmStatus = true; // and to remember it
}
else
{
out_LAB_1->Alarm = 0x00; // signal the no alarm condition to the slave
AlarmStatus = false; // and remember it
}
}
else
{
out_LAB_1->Alarm = 0x00; // signal the no alarm condition to the slave
AlarmStatus = false; // and remember it
}
if (VisuSlave == LAB_1) // if the HMI is set to slave 1
{ //
DrawAlarmStatus(AlarmStatus); // update the alarm status on the TFT
}
if (VisuSlave == LAB_1 && FirstRound) // if the HMI is set to slave 1
{ // and it is the first time
FirstRound = false; //
DrawTemperatureValue (fTemperature); // draw the current temperature value
DrawAlarmSettings(fAlarmThreshold, AlarmOnOff, AlarmMinMax); // draw the alarm settings
}
//----- end LAB_1 ------------------------
//----- slave 2 data management ------------
PotValue = in_LAB_2->Potentiometer; // read the potentiometer value
if (VisuSlave == LAB_2) // if the HMI is setted to slave 2
{ //
Time = SysMilliS(); // and the visualization timer is elapsed
if (Time-TimePotentiometer > TIME_POTENTIOMETER) //
{ // draw the potentiometer value
TimePotentiometer = Time; //
DrawPotentiometerValue(PotValue); //
} //
}
Buttons = in_LAB_2->Buttons; // read the buttons status from the slave
if (Buttons != PrevButtons) // check if the buttons value has changed
{
PrevButtons = Buttons; // remember the current buttons value
if (VisuSlave == LAB_2) // if the HMI is setted to slave 2
{ //
DrawButtonsValue(Buttons); // draw the current buttons value
} //
}
if (Segments != PrevSegments) // check if the segments value has changed
{
PrevSegments = Segments; // remember the current segments value
if (VisuSlave == LAB_2) // if the HMI is setted to slave 2
{ //
DrawSegmentsValue(Segments); // draw the current segments value
} //
}
if (VisuSlave == LAB_2 && FirstRound) // if the HMI is set to slave 2
{ // and it is the first time
FirstRound = false; //
//
DrawButtonsValue(Buttons); // draw the current buttons value
DrawSegmentsValue(Segments); // draw the current segments value
}
out_LAB_2->Segments = Segments; // send the segments status to the slave
//----- end LAB_2 ------------------------
TouchScreenManagement(); // check if the touchscreen is tapped
// and handle it
Time = SysMilliS(); // toggle the variable Blink every
if ((Time-TimeBlink) > TIME_BLINK) // TIME_BLINK mS
{ //
TimeBlink = Time; // we use it to blink a field on the TFT
Blink = !Blink; //
}
IO_data.lock(); // copy the IO data from the safe area
memcpy(&IOmap[0], &IOmapSafe[0], IO_MAP_SIZE); // to the EtherCAT buffer
IO_data.unlock(); //
//Test_2 = 0;
}
}
//******************************************************************************
//******* general functions ****************************************************
//------ touchscreen management ------------------------------------------------
void TouchScreenManagement()
{
uint16_t X;
uint16_t Y;
int i;
#ifdef ADA_TFT // if the touchscreen has been tapped
if (TSC.GetPoint(&X, &Y)) //
#else //
if (TouchRead(&X, &Y)) //
#endif
{
TimeTouchReleased = SysMilliS();
if(TouchWasReleased)
{ // check if it is the side menu
// decrement slave button
if ((X>MENU_X) && (X>MENU_X+MENU_WIDTH/2) && (Y>MENU_HEIGHT) && (Y<MENU_HEIGHT*2))
{ //
VisuSlave--;
if (VisuSlave == 0) //
VisuSlave = SLAVE_NUM; //
// the visualized slave has changed
DrawSlaveFixedParts(); // draw the new slave fixed parts
} //
// check if it is the side menu
// increment slave button
if ((X>MENU_X) && (X<MENU_X+MENU_WIDTH/2) && (Y>MENU_HEIGHT) && (Y<MENU_HEIGHT*2))
{ //
VisuSlave++;
if (VisuSlave > SLAVE_NUM) //
VisuSlave = LAB_1; //
// the visualized slave has changed
DrawSlaveFixedParts(); // draw the new slave fixed parts
} //
}
switch (VisuSlave) // check which slave is visualized on the TFT
{
case (LAB_1): //-------------- slave 1 -----------------------
if(TouchWasReleased) // first check if the touch was
{ // not tapped in the previous rounds
TouchWasReleased = false; // because for the following fields
TimeTouchReleased = SysMilliS(); // we don't want autorepeat
TimeAutoRepeat = SysMilliS(); // reload the autorepeat time
// handle taps on the ">" and "<" buttons
if ((X>ALARM_X) && (X<ALARM_X+(ALARM_WIDTH/2)) && (Y>ALARM_Y+ALARM_HEIGHT) && (Y<ALARM_Y+ALARM_HEIGHT*2))
{ //
AlarmMinMax = true; //
DrawAlarmSettings(fAlarmThreshold, AlarmOnOff, AlarmMinMax);
} //
if ((X>ALARM_X+(ALARM_WIDTH/2)) && (X<ALARM_X+(ALARM_WIDTH)) && (Y>ALARM_Y+ALARM_HEIGHT) && (Y<ALARM_Y+ALARM_HEIGHT*2)) //
{ //
AlarmMinMax = false; //
DrawAlarmSettings(fAlarmThreshold, AlarmOnOff, AlarmMinMax);
}
// handle taps on the "ON" and "OFF" buttons
if ((X>ALARM_X) && (X<ALARM_X+(ALARM_WIDTH/2)) && (Y>ALARM_Y) && (Y<ALARM_Y+ALARM_HEIGHT))
{ //
AlarmOnOff = true; //
DrawAlarmSettings(fAlarmThreshold, AlarmOnOff, AlarmMinMax);
} //
if ((X>ALARM_X+(ALARM_WIDTH/2)) && (X<ALARM_X+(ALARM_WIDTH)) && (Y>ALARM_Y) && (Y<ALARM_Y+ALARM_HEIGHT))
{ //
AlarmOnOff = false; //
DrawAlarmSettings(fAlarmThreshold, AlarmOnOff, AlarmMinMax);
}
// handle taps on the "+" and "-" buttons
// here we don't use the autorepeat to increment
// or decrement the threshold by 0.1
if ((X>ALARM_X) && (X<ALARM_X+(ALARM_WIDTH/2)) && (Y>ALARM_Y+(ALARM_HEIGHT*3)) && (Y<ALARM_Y+(ALARM_HEIGHT*4)))
{ //
fAlarmThreshold += 0.1; //
DrawOnlyThreshold(fAlarmThreshold, AlarmOnOff, AlarmMinMax);
} //
if ((X>ALARM_X+(ALARM_WIDTH/2)) && (X<ALARM_X+(ALARM_WIDTH)) && (Y>ALARM_Y+(ALARM_HEIGHT*3)) && (Y<ALARM_Y+(ALARM_HEIGHT*4)))
{ //
fAlarmThreshold -= 0.1; //
DrawOnlyThreshold(fAlarmThreshold, AlarmOnOff, AlarmMinMax);
} //
}
else // autorepeat management
{ //
Time = SysMilliS(); //
if (Time-TimeAutoRepeat > TIME_AUTO_REP_START) //
{ //
if (Time-TimeAutoRepRepeat > TIME_AUTO_REP_REPEAT)
{ //
TimeAutoRepRepeat = Time; //
// handle taps on the "+" and "-" buttons
// here we use the autorepeat to increment
// or decrement the threshold by 1
if ((X>ALARM_X) && (X<ALARM_X+(ALARM_WIDTH/2)) && (Y>ALARM_Y+(ALARM_HEIGHT*3)) && (Y<ALARM_Y+(ALARM_HEIGHT*4)))
{ //
fAlarmThreshold += 1; //
DrawOnlyThreshold(fAlarmThreshold, AlarmOnOff, AlarmMinMax);
} //
if ((X>ALARM_X+(ALARM_WIDTH/2)) && (X<ALARM_X+(ALARM_WIDTH)) && (Y>ALARM_Y+(ALARM_HEIGHT*3)) && (Y<ALARM_Y+(ALARM_HEIGHT*4)))
{ //
fAlarmThreshold -= 1; //
DrawOnlyThreshold(fAlarmThreshold, AlarmOnOff, AlarmMinMax);
}
}
}
}
break; //-------------- end slave 1 -------------------
case (LAB_2): //-------------- slave 2 -----------------------
if(TouchWasReleased) // first check if the touch was
{ // not tapped in the previous rounds
TouchWasReleased = false; // because for the following fields
TimeTouchReleased = SysMilliS(); // we don't want autorepeat
uint8_t Mask = 0x08; // check if one of the segment is tapped
//
for (i=0; i<4; i++) //
{ //
if ((X>SEG_X+(i*SEG_STEP)) && (X<SEG_X+(i*SEG_STEP)+SEG_WIDTH) && (Y>SEG_Y) && (Y<SEG_Y+SEG_HEIGHT*3))
{ //
Segments ^= Mask >> i; //
} //
} //
}
break; //----------------- end slave 2 ----------------
}
}
else
{
if ( SysMilliS()-TimeTouchReleased > TIME_TOUCH_RELEASED) // if the touchscreen was not
{ // tapped for enought time
TouchWasReleased = true; // remember it
}
}
}
//----- draw the fixed part of the visualized slave ----------------------------
void DrawSlaveFixedParts()
{
int i;
int Offset;
TFT.cls(); // clear screen
DrawSideMenu(VisuSlave); // draw the side menu
switch (VisuSlave) // check which slave is visualized on the TFT
{
case (LAB_1): //-------------- slave 1 ---------------------------
TFT.foreground(Yellow); // draw the thermometer
TFT.circle(TERMO_X, TERMO_Y, 18, Yellow); // bowl
TFT.fillcircle(TERMO_X, TERMO_Y, 17, Red); //
// tube
TFT.rect(TERMO_X-8, TERMO_Y-185, TERMO_X+8, TERMO_Y-18, Yellow);
TFT.fillrect(TERMO_X-7, TERMO_Y-18, TERMO_X+7, TERMO_Y-16, Red);
for (i=0; i<8; i++) // scale
{ //
if (i <1) //
Offset = 5; //
else //
Offset = 0; //
//
TFT.line(TERMO_X-8, (TERMO_Y-28)-(i *20), TERMO_X-18,(TERMO_Y-28)-(i*20), Yellow);
TFT.locate(TERMO_X-48+Offset, TERMO_Y-(i*20)-32); //
TFT.set_font((unsigned char*) Arial12x12); //
TFT.printf("%3d",(i*10)-10); //
}
TFT.foreground(Green); //
TFT.set_font((unsigned char*) Arial28x28); //
TFT.locate(TERMO_X+25, TERMO_Y+28); //
TFT.printf("C"); //
// draw the alarm control panel
// frame
TFT.rect(ALARM_X, ALARM_Y, ALARM_X+ALARM_WIDTH, ALARM_Y+(ALARM_HEIGHT*4), Magenta);
//
for (i=0; i<3; i++) //
{ //
TFT.line(ALARM_X, ALARM_Y+((i+1)*ALARM_HEIGHT) , ALARM_X+ALARM_WIDTH, ALARM_Y+((i+1)*ALARM_HEIGHT), Magenta);
} //
//
TFT.line(ALARM_X+(ALARM_WIDTH/2), ALARM_Y, ALARM_X+(ALARM_WIDTH/2), ALARM_Y+(ALARM_HEIGHT*2), Magenta);
TFT.line(ALARM_X+(ALARM_WIDTH/2), ALARM_Y+(ALARM_HEIGHT*3), ALARM_X+(ALARM_WIDTH/2), ALARM_Y+(ALARM_HEIGHT*4), Magenta);
TFT.set_font((unsigned char*) Arial28x28); // "+" and "-"
TFT.foreground(Green); //
TFT.locate(ALARM_X+11, ALARM_Y+(ALARM_HEIGHT*3)+9); //
TFT.printf("+"); //
TFT.locate(ALARM_X+(ALARM_WIDTH/2)+14, ALARM_Y+(ALARM_HEIGHT*3)+9);
TFT.printf("-"); //
// alarm bar
TFT.rect(ALARM_X-30, ALARM_Y, ALARM_X-25, ALARM_Y+168, Yellow);
TFT.set_font((unsigned char*) Arial12x12); // draw "ALARM SETTINGS"
TFT.foreground(Yellow); //
TFT.locate(ALARM_X-35, ALARM_Y+14+(ALARM_HEIGHT*4)); //
TFT.printf("ALARM SETTINGS"); //
break;
case (LAB_2): //-------------- slave 2 ---------------------------
TFT.foreground(Yellow);
for (i=0; i<4; i++) // draw the segments fixed parts
{ //
TFT.rect(SEG_X+(i*SEG_STEP), SEG_Y, SEG_X+SEG_WIDTH+(i*SEG_STEP), SEG_Y+SEG_HEIGHT, Yellow);
//
TFT.set_font((unsigned char*) Arial12x12); //
TFT.locate(SEG_X+44, SEG_Y+SEG_HEIGHT+12); //
TFT.printf("MIDDLE SEGMENTS"); //
} //
for (i=0; i<3; i++) // draw the buttons fixed parts
{ //
TFT.circle(BUTTONS_X+(i*BUTTONS_STEP), BUTTONS_Y, BUTTONS_R, Red);
TFT.circle(BUTTONS_X+BUTTONS_WIDTH+(i*BUTTONS_STEP), BUTTONS_Y, BUTTONS_R, Red);
//
TFT.line(BUTTONS_X+(i*BUTTONS_STEP)-BUTTONS_R, BUTTONS_Y, BUTTONS_X+(i*BUTTONS_STEP)-BUTTONS_R-4, BUTTONS_Y, Red);
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);
} //
//
TFT.set_font((unsigned char*) Arial12x12); //
TFT.locate(BUTTONS_X+38, BUTTONS_Y+12); //
TFT.printf("BUTTONS"); //
// draw the potentiometer window fixed parts
TFT.rect(ANALOG_X, ANALOG_Y, ANALOG_X+ANALOG_WIDTH, ANALOG_Y+ANALOG_HEIGHT, Magenta);
//
TFT.set_font((unsigned char*) Arial12x12); //
TFT.locate(ANALOG_X+35, ANALOG_Y+ANALOG_HEIGHT+12); //
TFT.printf("POTENTIOMETER"); //
Graph_x = 0;
DrawSegmentsValue(Segments); // draw the segments status
DrawButtonsValue (Buttons); // draw the buttons status
DrawPotentiometerValue(PotValue); // draw the potentiometer value
break;
}
FirstRound = true;
}
//---- draw the menu on the upper right part of the TFT ------------------------
void DrawSideMenu(uint8_t Slave)
{ // draw the side menu frame
TFT.rect(MENU_X, MENU_Y, MENU_X+MENU_WIDTH, MENU_Y+MENU_HEIGHT*2, Green);
TFT.line(MENU_X, MENU_HEIGHT, MENU_X+MENU_WIDTH, MENU_HEIGHT, Green);
TFT.line(MENU_X+(MENU_WIDTH/2), MENU_Y+MENU_HEIGHT, MENU_X+(MENU_WIDTH/2), MENU_Y+(MENU_HEIGHT*2), Green);
// draw the slave number
TFT.fillrect(MENU_X+1, MENU_Y+1, MENU_X+MENU_WIDTH-1, MENU_Y+MENU_HEIGHT-1, Red);
TFT.set_font((unsigned char*) Arial12x12); //
TFT.foreground(Yellow); //
TFT.locate(MENU_X+8 , MENU_Y+18); //
TFT.background(Red); //
TFT.printf("SLAVE %d", Slave); //
TFT.background(Black); //
TFT.set_font((unsigned char*) Arial28x28); // draw "+" and "-"
TFT.foreground(Green); //
TFT.locate (MENU_X+11, MENU_Y+MENU_HEIGHT+9); //
TFT.printf("+"); //
TFT.locate (MENU_X+(MENU_WIDTH/2)+14, MENU_Y+MENU_HEIGHT+9); //
TFT.printf("-"); //
TFT.set_font((unsigned char*) Arial12x12); // draw the slave name
TFT.foreground(Red); //
//
TFT.locate(MENU_X, MENU_Y+(MENU_HEIGHT*2)+12); //
TFT.printf("%.9s", ec_slave[Slave].name); //
}
//---- draw the starting banner ------------------------------------------------
void DrawBanner()
{
TFT.set_font((unsigned char*) Arial24x23);
TFT.foreground(Red);
TFT.locate(30, 50);
TFT.printf("EasyCAT");
TFT.locate(30, 80);
TFT.printf("SOEM MASTER");
TFT.set_font((unsigned char*) Arial12x12);
TFT.foreground(Green);