It is a bit more complicated... usually I try to keep the problem simple when posting in a forum. This time I'm going to waste a bit of your time explaning the project.
This is a project for an hospital, they will use a Controllino as plc to manage a 4-way crossing inside the building.
There will be 4 doors, 2 allowed paths, when you enter from one door you can exit only to the opposite one and you cannot turn left or right. The door works like an elevator, there is a call button to enter if all other doors are closed, and there is a red/green semaphore and an electromagnet to lock/unlock the door.
There are some more signals, but the main logic is this:
-
when you call the btn, if all doors are closed and blocked, the door near the buttun will be unlocked, the semaphore turns green
-
when the door is unlocked there is a timer, if the door will be not opened before the timeout, this will be blocked again.
-
when the door is opened the timer stops and the lock is reinforced
-
when the door is closed the path is free again for other entrance BUT mind the next point!
-
one of the two paths is a "covid" path (door 3/4), this means that after the door closing the passage is blocked for a fixed amount of time to let the automatic disinfection. After the timer, the passage is free again for the next call.
This is more or less the project, as you can imagine, is not so straight. For example, the doors are not all the same. There is a manual door with electromagnet, but there is also an automatic door with signal to open and close and so on. This is why I wanted to use classes, now I have Door superclass, ManualDoor class, SlidingDoor class and so on.
Coming back to the question, inside my class I have to handle some timers (covid disinfection timer, door opening timeout, led blinking ...), for example the door opening timeout has to block the door again at the timeout. This is why I need to use a class method in the timer callback.
This is the example using gfvalvo solution:
class Door {
Timer<1> closeTimer;
void lock();
void eventCallBtn(){
closeTimer.cancel();
closeTimer.in(CALL_TIMEOUT, callback, this);
}
}
static bool callback(void *p) {
Door *ptr = (Door *)p;
return ptr->lock();
}
void lock(){
digitalWrite(LEDRED_PIN,HIGH);
}
}
class ManualDoor : public Door {
void lock(){
Door::lock();
digitalWrite(ELECTROMAGNET_PIN,HIGH);
}
}
class SlidingDoor : public Door {
void lock(){
Door::lock();
digitalWrite(CLOSEDOORSIGNAL_PIN,HIGH);
}
}