digint / digint/tinyfsm

How to use an event queue?

Open
#46 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
1.2k
Forks
193
PR merge metrics
No merged PRs in 30d

Description

I have adapted the `main.cpp` of the elevator example to use an event queue like this:
```cpp
#include "fsmlist.hpp"

#include

#include
std::queue event_queue;

int main()
{
fsm_list::start();

Call call;
FloorSensor sensor;

while(1)
{
char c;

std::cout << "c=Call, f=FloorSensor, a=Alarm, q=Quit ? ";
std::cin >> c;
switch(c) {
case 'c':
std::cout << "Floor ? ";
std::cin >> call.floor;
event_queue.push(call);
break;
case 'f':
std::cout << "Floor ? ";
std::cin >> sensor.floor;
event_queue.push(sensor);
break;
case 'a':
event_queue.push(Alarm());
break;
case 'q':
std::cout << "Thanks for playing!" << std::endl;
return 0;
default:
std::cout << "Invalid input" << std::endl;
};
send_event(event_queue.front());
event_queue.pop();
}
}
```
Now the state machine always calls the default
```cpp
void react(tinyfsm::Event const &) {};
```
event handler. Probably because the events are upcasted in the queue to `tinyfsm::Event`?
But how am I supposed to implement event queuing instead?

Reproducing:
* replace `main.cpp` with my adapted version
* press `a` to trigger the alarm event

Expected output:
```
*** calling firefighters ***
Motor: stopped
```
Actual output: Nothing

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.