Skip to content

Commit f486b94

Browse files
authored
Compile time State Machine (#549)
* Full of errors, but moving forward * Avanzando bien fino, casi implementado * Vamos bien encaminados * Bruh * Casi compila, casi * StateMachine compila, otras cosas no xd * Some error fixing * Se lleva el credito su prima en tanga * Compila por ahora?? * Compiles without state orders * Fixed StateOrder problems and some fixes on the state machine code * Now everything (nearly) is consteval * New concepts added to ensure StateEnum is a enum * Much more checks with concepts, implemented scheduler logic * Testing needed, but might work * Functional version? * State Machine now works!! * Moved static_vector to a seperate file
1 parent f62d715 commit f486b94

9 files changed

Lines changed: 608 additions & 702 deletions

File tree

Inc/C++Utilities/StaticVector.hpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#pragma once
2+
#include <array>
3+
#include "ErrorHandler/ErrorHandler.hpp"
4+
5+
template <typename T, size_t Capacity>
6+
class StaticVector {
7+
private:
8+
std::array<T, Capacity> data{};
9+
size_t size_ = 0;
10+
11+
public:
12+
constexpr StaticVector() = default;
13+
14+
template<typename... Args>
15+
constexpr StaticVector(Args&&... args) : data{std::forward<Args>(args)...}, size_(sizeof...(args)) {}
16+
17+
constexpr bool operator==(const StaticVector&) const = default;
18+
19+
constexpr void push_back(const T& value)
20+
{
21+
if (size_ >= Capacity)
22+
{
23+
ErrorHandler("StaticVector capacity exceeded");
24+
return;
25+
}
26+
data[size_] = value;
27+
size_++;
28+
}
29+
30+
constexpr auto begin() { return data.begin(); }
31+
constexpr auto begin() const { return data.begin(); }
32+
constexpr auto end() { return data.begin() + size_; }
33+
constexpr auto end() const { return data.begin() + size_; }
34+
35+
constexpr const std::array<T, Capacity>& get_array() const { return data; }
36+
constexpr size_t size() const { return size_; }
37+
constexpr T* get_data() { return data.data(); }
38+
constexpr const T* get_data() const { return data.data(); }
39+
constexpr T& operator[](size_t i) { return data[i]; }
40+
constexpr const T& operator[](size_t i) const { return data[i]; }
41+
constexpr bool contains(const T& value) const
42+
{
43+
for (size_t i = 0; i < size_; ++i)
44+
{
45+
if (data[i] == value)
46+
{
47+
return true;
48+
}
49+
}
50+
return false;
51+
}
52+
};
53+

Inc/ST-LIB_HIGH/Protections/ProtectionManager.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class ProtectionManager {
4242

4343
static void set_id(Boards::ID id);
4444

45-
static void link_state_machine(StateMachine& general_state_machine,
45+
static void link_state_machine(IStateMachine& general_state_machine,
4646
state_id fault_id);
4747

4848
template <class Type, ProtectionType... Protector,
@@ -84,7 +84,7 @@ class ProtectionManager {
8484
static Boards::ID board_id;
8585
static vector<Protection> low_frequency_protections;
8686
static vector<Protection> high_frequency_protections;
87-
static StateMachine* general_state_machine;
87+
static IStateMachine* general_state_machine;
8888
static state_id fault_state_id;
8989

9090
static Notification fault_notification;
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
#pragma once
22
#include "StateMachine/StateOrder.hpp"
33

4+
template<class StateMachineType>
45
class HeapStateOrder : public HeapOrder{
56
public:
6-
StateMachine& state_machine;
7-
StateMachine::state_id state;
7+
StateMachineType& state_machine;
8+
typename StateMachineType::state_id state;
89

910
template<class... Types>
10-
HeapStateOrder(uint16_t id,void(*callback)(void), StateMachine& state_machine, StateMachine::state_id state,Types*... values) : HeapOrder(id,callback,values...),
11+
HeapStateOrder(uint16_t id,void(*callback)(void), StateMachineType& state_machine, typename StateMachineType::state_id state,Types*... values) : HeapOrder(id,callback,values...),
1112
state_machine(state_machine), state(state) {
1213
if(not state_machine.get_states().contains(state)){
1314
ErrorHandler("State Machine does not contain state, cannot add StateOrder");
1415
return;
1516
}
16-
else state_machine.get_states()[state].add_state_order(id);
17+
else state_machine.get_states()[static_cast<size_t>(state)].add_state_order(id);
1718
orders[id] = this;
1819
}
1920

@@ -24,4 +25,7 @@ class HeapStateOrder : public HeapOrder{
2425
void parse(OrderProtocol* socket, uint8_t* data)override{
2526
if(state_machine.is_on && state_machine.current_state == state) HeapOrder::parse(data);
2627
}
27-
};
28+
};
29+
30+
template<class StateMachineType, class... Types>
31+
HeapStateOrder(uint16_t, void(*)(void), StateMachineType&, typename StateMachineType::state_id, Types*...) -> HeapStateOrder<StateMachineType>;
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
#pragma once
22
#include "StateMachine/StateOrder.hpp"
33

4-
template<size_t BufferLength,class... Types> requires NotCallablePack<Types*...>
4+
template<class StateMachineType, size_t BufferLength,class... Types> requires NotCallablePack<Types*...>
55
class StackStateOrder : public StackOrder<BufferLength,Types...>{
66
public:
7-
StateMachine& state_machine;
8-
StateMachine::state_id state;
9-
StackStateOrder(uint16_t id,void(*callback)(void), StateMachine& state_machine, StateMachine::state_id state,Types*... values) : StackOrder<BufferLength,Types...>(id,callback,values...),
7+
StateMachineType& state_machine;
8+
typename StateMachineType::state_id state;
9+
StackStateOrder(uint16_t id,void(*callback)(void), StateMachineType& state_machine, typename StateMachineType::state_id state,Types*... values) : StackOrder<BufferLength,Types...>(id,callback,values...),
1010
state_machine(state_machine), state(state) {
1111
if(not state_machine.get_states().contains(state)){
1212
ErrorHandler("State Machine does not contain state, cannot add StateOrder");
1313
return;
1414
}
15-
else state_machine.get_states()[state].add_state_order(id);
15+
else state_machine.get_states()[static_cast<size_t>(state)].add_state_order(id);
1616
Order::orders[id] = this;
1717
}
1818

@@ -26,6 +26,6 @@ class StackStateOrder : public StackOrder<BufferLength,Types...>{
2626
};
2727

2828
#if __cpp_deduction_guides >= 201606
29-
template<class... Types> requires NotCallablePack<Types*...>
30-
StackStateOrder(uint16_t id,void(*callback)(void), StateMachine& state_machine, StateMachine::state_id state,Types*... values)->StackStateOrder<(!has_container<Types...>::value)*total_sizeof<Types...>::value, Types...>;
31-
#endif
29+
template<class StateMachineType, class... Types> requires NotCallablePack<Types*...>
30+
StackStateOrder(uint16_t id,void(*callback)(void), StateMachineType& state_machine, typename StateMachineType::state_id state,Types*... values)->StackStateOrder<StateMachineType, (!has_container<Types...>::value)*total_sizeof<Types...>::value, Types...>;
31+
#endif

0 commit comments

Comments
 (0)