Skip to content

Commit

Permalink
nope
Browse files Browse the repository at this point in the history
  • Loading branch information
ahdinosaur committed May 9, 2021
1 parent d7bc052 commit 131d447
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,30 +91,38 @@ struct Transitions {
}
};

template <typename StateVariant, typename EventVariant, typename Transitions>
template <typename Parameters, typename StateVariant, typename EventVariant, typename Transitions>
struct Machine {
StateVariant current_state;
Machine(Parameters parameters);
~Machine();

void dispatch(const EventVariant &Event)
{
std::optional<StateVariant> new_state = std::visit(Transitions{}, current_state, Event);
if (new_state)
current_state = *std::move(new_state);
}
Parameters parameters;
StateVariant current_state;

void dispatch(const EventVariant &Event) {
std::optional<StateVariant> new_state = std::visit(Transitions{}, current_state, Event);
if (new_state)
current_state = *std::move(new_state);
}
};

struct Params {
uint8_t led_pin;
uint16_t ticks_until_switch;
};

Params green_params = {
LED_BUILTIN, // led_pin
10 // ticks_until_switch
};

volatile Machine<Params, State, Event, Transitions> green_machine(green_params);

void TimerHandler()
{
ISR_Timer.run();
}

Machine<State, Event, Transitions> led;

void process_green () {
led.dispatch(EventTick{});
}

void setup()
{
Serial.begin(115200);
Expand All @@ -136,8 +144,8 @@ void setup()
Serial.println(F("Can't set ITimer. Select another freq. or timer"));
}

led.dispatch(EventStart{});
ISR_Timer.setInterval(TIMER_INTERVAL_TICK, [](){ led.dispatch(EventTick{}); });
green_machine.dispatch(EventStart{});
ISR_Timer.setInterval(TIMER_INTERVAL_TICK, [](){ green_machine.dispatch(EventTick{}); });
}

void loop()
Expand Down

0 comments on commit 131d447

Please sign in to comment.