Progress towards a better application FSM, testing
This commit is contained in:
55
firmware/main_mcu/lib/app_lib/src/DebounceFsm.cpp
Normal file
55
firmware/main_mcu/lib/app_lib/src/DebounceFsm.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "DebounceFsm.h"
|
||||
|
||||
DebounceFsm::DebounceFsm(Clock &clock,
|
||||
GpioInput &input,
|
||||
unsigned int debounce_millis,
|
||||
const bool active_value,
|
||||
const std::function<void()> &on_activate,
|
||||
const std::function<void()> &on_deactivate) :
|
||||
Fsm(inactive),
|
||||
timer(Timer(clock)),
|
||||
input(input),
|
||||
debounce_millis(debounce_millis),
|
||||
inactive(*this, to_active, !active_value),
|
||||
active(*this, to_inactive, active_value),
|
||||
to_inactive(*this, on_deactivate, !active_value, active, inactive),
|
||||
to_active(*this, on_activate, active_value, inactive, active)
|
||||
{}
|
||||
|
||||
|
||||
DebounceFsm::SteadyState::SteadyState(DebounceFsm &fsm, State &next_state, bool steadyPinValue) :
|
||||
fsm(fsm),
|
||||
next_state(next_state),
|
||||
steadyPinValue(steadyPinValue)
|
||||
{}
|
||||
|
||||
void DebounceFsm::SteadyState::step() {
|
||||
if (fsm.input.read() != steadyPinValue) {
|
||||
fsm.transition(next_state);
|
||||
}
|
||||
}
|
||||
|
||||
DebounceFsm::Debouncing::Debouncing(DebounceFsm &fsm,
|
||||
std::function<void()> callback,
|
||||
bool to_pin_value,
|
||||
State &previous_state,
|
||||
State &next_state) :
|
||||
fsm(fsm),
|
||||
callback(callback),
|
||||
to_pin_value(to_pin_value),
|
||||
previous_state(previous_state),
|
||||
next_state(next_state)
|
||||
{}
|
||||
|
||||
void DebounceFsm::Debouncing::onEntry() {
|
||||
fsm.timer.start();
|
||||
}
|
||||
|
||||
void DebounceFsm::Debouncing::step() {
|
||||
if (fsm.input.read() != to_pin_value) {
|
||||
fsm.transition(previous_state);
|
||||
} else if (fsm.timer.getRuntime() >= fsm.debounce_millis) {
|
||||
callback();
|
||||
fsm.transition(next_state);
|
||||
}
|
||||
}
|
||||
56
firmware/main_mcu/lib/app_lib/src/DebounceFsm.h
Normal file
56
firmware/main_mcu/lib/app_lib/src/DebounceFsm.h
Normal file
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include "Fsm.h"
|
||||
#include "Timer.h"
|
||||
#include "Gpio.h"
|
||||
|
||||
class DebounceFsm : public Fsm {
|
||||
|
||||
public:
|
||||
DebounceFsm(Clock &clock,
|
||||
GpioInput &input,
|
||||
unsigned int debouce_millis,
|
||||
const bool active_value,
|
||||
const std::function<void()> &on_activate,
|
||||
const std::function<void()> &on_deactivate);
|
||||
|
||||
private:
|
||||
class SteadyState : public State {
|
||||
public:
|
||||
SteadyState(DebounceFsm &fsm, State &next_state, bool steadyPinValue);
|
||||
virtual void step() override;
|
||||
private:
|
||||
DebounceFsm &fsm;
|
||||
State &next_state;
|
||||
const bool steadyPinValue;
|
||||
};
|
||||
|
||||
class Debouncing : public State {
|
||||
public:
|
||||
Debouncing(DebounceFsm &fsm,
|
||||
std::function<void()> callback,
|
||||
bool to_pin_value,
|
||||
State &previous_state,
|
||||
State &next_state);
|
||||
|
||||
virtual void onEntry() override;
|
||||
virtual void step() override;
|
||||
|
||||
private:
|
||||
DebounceFsm &fsm;
|
||||
std::function<void()> callback;
|
||||
const int to_pin_value;
|
||||
State &previous_state;
|
||||
State &next_state;
|
||||
};
|
||||
|
||||
Timer timer;
|
||||
GpioInput &input;
|
||||
const unsigned int debounce_millis;
|
||||
SteadyState inactive;
|
||||
SteadyState active;
|
||||
Debouncing to_inactive;
|
||||
Debouncing to_active;
|
||||
};
|
||||
23
firmware/main_mcu/lib/generic_lib/ArduinoGpio.cpp
Normal file
23
firmware/main_mcu/lib/generic_lib/ArduinoGpio.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "ArduinoGpio.h"
|
||||
|
||||
#ifdef ARDUINO
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
ArduinoGpioInput::ArduinoGpioInput(uint8_t pin) :
|
||||
pin(pin)
|
||||
{}
|
||||
|
||||
bool ArduinoGpioInput::read() {
|
||||
return digitalRead(pin);
|
||||
}
|
||||
|
||||
ArduinoGpioOutput::ArduinoGpioOutput(uint8_t pin) :
|
||||
pin(pin)
|
||||
{}
|
||||
|
||||
void ArduinoGpioOutput::write(bool value) {
|
||||
digitalWrite(pin, value);
|
||||
}
|
||||
|
||||
#endif
|
||||
13
firmware/main_mcu/lib/generic_lib/src/ArduinoClock.cpp
Normal file
13
firmware/main_mcu/lib/generic_lib/src/ArduinoClock.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#include "ArduinoClock.h"
|
||||
|
||||
#ifdef ARDUINO
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
ArduinoClock::ArduinoClock() {}
|
||||
|
||||
uint32_t ArduinoClock::getMillis() {
|
||||
return getMillis();
|
||||
}
|
||||
|
||||
#endif
|
||||
12
firmware/main_mcu/lib/generic_lib/src/ArduinoClock.h
Normal file
12
firmware/main_mcu/lib/generic_lib/src/ArduinoClock.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "Clock.h"
|
||||
#ifdef ARDUINO
|
||||
|
||||
class ArduinoClock : public Clock {
|
||||
public:
|
||||
ArduinoClock();
|
||||
uint32_t getMillis() override;
|
||||
};
|
||||
|
||||
#endif
|
||||
23
firmware/main_mcu/lib/generic_lib/src/ArduinoGpio.cpp
Normal file
23
firmware/main_mcu/lib/generic_lib/src/ArduinoGpio.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "ArduinoGpio.h"
|
||||
|
||||
#ifdef ARDUINO
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
ArduinoGpioInput::ArduinoGpioInput(uint8_t pin) :
|
||||
pin(pin)
|
||||
{}
|
||||
|
||||
bool ArduinoGpioInput::read() {
|
||||
return digitalRead(pin);
|
||||
}
|
||||
|
||||
ArduinoGpioOutput::ArduinoGpioOutput(uint8_t pin) :
|
||||
pin(pin)
|
||||
{}
|
||||
|
||||
void ArduinoGpioOutput::write(bool value) {
|
||||
digitalWrite(pin, value);
|
||||
}
|
||||
|
||||
#endif
|
||||
24
firmware/main_mcu/lib/generic_lib/src/ArduinoGpio.h
Normal file
24
firmware/main_mcu/lib/generic_lib/src/ArduinoGpio.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include "Gpio.h"
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef ARDUINO
|
||||
|
||||
class ArduinoGpioInput : public GpioInput {
|
||||
public:
|
||||
ArduinoGpioInput(uint8_t pin_number);
|
||||
virtual bool read() override;
|
||||
private:
|
||||
const uint8_t pin;
|
||||
};
|
||||
|
||||
class ArduinoGpioOutput : public GpioOutput {
|
||||
public:
|
||||
ArduinoGpioOutput(uint8_t pin_number);
|
||||
virtual void write(bool value) override;
|
||||
private:
|
||||
const uint8_t pin;
|
||||
};
|
||||
|
||||
#endif
|
||||
8
firmware/main_mcu/lib/generic_lib/src/Clock.h
Normal file
8
firmware/main_mcu/lib/generic_lib/src/Clock.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
class Clock {
|
||||
public:
|
||||
virtual uint32_t getMillis() = 0;
|
||||
};
|
||||
15
firmware/main_mcu/lib/generic_lib/src/Fsm.cpp
Normal file
15
firmware/main_mcu/lib/generic_lib/src/Fsm.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#include "Fsm.h"
|
||||
|
||||
Fsm::Fsm(State &initialState) :
|
||||
current_state(&initialState)
|
||||
{}
|
||||
|
||||
void Fsm::transition(State &state) {
|
||||
current_state->onExit();
|
||||
current_state = &state;
|
||||
current_state->onEntry();
|
||||
}
|
||||
|
||||
void Fsm::step() {
|
||||
current_state->step();
|
||||
}
|
||||
28
firmware/main_mcu/lib/generic_lib/src/Fsm.h
Normal file
28
firmware/main_mcu/lib/generic_lib/src/Fsm.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
class State {
|
||||
|
||||
public:
|
||||
virtual void step() {};
|
||||
virtual void onEntry() {};
|
||||
virtual void onExit() {};
|
||||
};
|
||||
|
||||
class Transition {
|
||||
public:
|
||||
Transition(State &toState) {}
|
||||
virtual bool evalutate() = 0;
|
||||
};
|
||||
|
||||
class Fsm {
|
||||
|
||||
protected:
|
||||
Fsm(State &initialState);
|
||||
void transition(State &state);
|
||||
|
||||
public:
|
||||
void step();
|
||||
|
||||
private:
|
||||
State *current_state;
|
||||
};
|
||||
11
firmware/main_mcu/lib/generic_lib/src/Gpio.h
Normal file
11
firmware/main_mcu/lib/generic_lib/src/Gpio.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
class GpioInput {
|
||||
public:
|
||||
virtual bool read() = 0;
|
||||
};
|
||||
|
||||
class GpioOutput {
|
||||
public:
|
||||
virtual void write(bool value) = 0;
|
||||
};
|
||||
25
firmware/main_mcu/lib/generic_lib/src/Timer.h
Normal file
25
firmware/main_mcu/lib/generic_lib/src/Timer.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include "Clock.h"
|
||||
|
||||
class Timer {
|
||||
public:
|
||||
|
||||
Timer(Clock &clock)
|
||||
: clock(clock)
|
||||
, start_time(0)
|
||||
{}
|
||||
|
||||
void start() {
|
||||
start_time = clock.getMillis();
|
||||
}
|
||||
|
||||
uint32_t getRuntime() {
|
||||
return start_time - clock.getMillis();
|
||||
}
|
||||
|
||||
private:
|
||||
Clock &clock;
|
||||
uint32_t start_time;
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user