-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlock.cpp
130 lines (116 loc) · 4.17 KB
/
Block.cpp
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
/*
* segments for railroad [- BLOCK.CXX -]
**************************************************************************
*/
#include "Block.h"
#include <QDebug>
#include <QBrush>
#include <QTimer>
#include <WiringPi/wiringPi/wiringPi.h>
#include <WiringPi/wiringPi/mcp23017.h>
Block::Block(QString name , Stellwerkstechnik *signaltechnik, Configuration *config){
setName( name );
technik = signaltechnik;
configuration = config;
}
void Block::setName( QString name ){
blockname = name;
}
void Block::setB_status( bool status ){
if( counter == 2){ //reset counter
counter = 0;
}
counter++; //every state change will be registered and counted
evaluateFreigabe(); //evaluation if segments can be unlocked or not
b_status = status; //change status
changeColor();
}
void Block::setFreigabe( bool free ){
freigabe = free;
changeColor();
}
bool Block::evaluateFreigabe(){
if( counter == 0){ //do nothing and give a callback false
return false;
}
if( counter == 1 ){ //Block is now occupied -> test if previous Block is also occupied and at the same time the signal in between shows green-->trigger zugpassiert
//Block becomes occupied: haspassiert? --> previous B_status? --> signal in between status?
//trigger: signal of pair is checked if Hp1 --> Hp1? if(prevBlock=occupied)-->singalpassiert; Hp0? do nothing
if( haspassiert ){
for( int i = 0; i < passiert.size(); i++){
if( technik->getS_pass_status( passiert.at(i).first ) && !passiert.at(i).second->getB_status() ){ //is signal green? (get method of Stellwerkstechnik) && previous Block occupied
QString signalInQuestion = passiert.at(i).first;
// WSignal or HSignal? trigger zugpassiertW or zugpassiert
if(signalInQuestion.at(0) == 'S'){
QTimer::singleShot(1700, [signalInQuestion, this]() {
emit this->zugpassiert(signalInQuestion);
});
for(int e = 0; e < this->passiert.size(); e++){
qDebug()<<"__Block__: Zugpassiert of this: "<<this->getName()<<passiert.at(e).first<<passiert.at(e).second->getName();
}
}
if(signalInQuestion.at(0) == 'W'){
QTimer::singleShot(1700, [signalInQuestion, this]() {
emit this->zugpassiertW(signalInQuestion);
});
}
}
}
} //otherwise: do nothing
return false;
}
// setting Freigabe
if(counter == 2 && !b_status){
freigabe = true; //here freigabe = true -->right cycle
changeColor();
return true;
}
return false;
}
void Block::addpassiert( QString grenzS, Block* prevBlock ){
QPair<QString, Block*> hilfspair(grenzS,prevBlock);
passiert.push_back(hilfspair);
haspassiert = true;
}
void Block::deletepassiert(){
haspassiert = false;
passiert.clear();
}
void Block::showPassiert(){
if(getHaspassiert()){
qInfo()<<"***************************************************************************";
qInfo()<<" passiert von "<<getName();
for( QPair pair : passiert ){
qInfo()<<" "<<pair.first<<": -"<<pair.second->getName();
}
qInfo()<<"***************************************************************************";
}
}
void Block::changeColor(){
for( int i = 0 ; i < blockitems.size() ; i++ ){
if(b_status && freigabe){ //if Block state free && and not occupied
blockitems.at(i)->setBrush(QColor(79,79,79));//rgb grey 79,79,79
}
if(b_status && !freigabe){ //if Block state free && and locked
blockitems.at(i)->setBrush(Qt::white);
}
if(!b_status){ //if Block state occupied
blockitems.at(i)->setBrush(Qt::red);
}
}
}
void Block::setGpio(int blockpin){
pin = blockpin;
}
int Block::getGpio(){
return pin;
}
int Block::readBlock(){
if( configuration->getWithHardware()){
return digitalRead(pin);
}
else return 1;
}
Block::~Block(){
//delete technik; //it`s a pointer, yes, but should live outside block --> no delete: happens in main
}