MRAS
Multi Rocket Avionics System
Loading...
Searching...
No Matches
Subsystem.h
1//
2// Created by Tom Danvers on 13/12/2022.
3//
4
5#ifndef MRAS_SUBSYSTEM_H
6#define MRAS_SUBSYSTEM_H
7
8#include <cstdint>
9#include "SystemMessage.h"
10
11// macro to define empty message handler for subsystems that don't want to receive messages
12#define SUBSYSTEM_NO_MESSAGE_HANDLER void on_message(SystemMessage *msg) override {};
13
14#define SUBSYSTEM_NAME(name) const char* get_name() override { return name; }
15
16
30class Subsystem {
31public:
36 virtual const char *get_name() = 0;
37
38 /*
39 * Children of subsystem should override this function and use it for running one-time setup code, analogous to
40 * the Arduino setup() function
41 *
42 * The function should return a status code, with 0 indicating success and any other value representing an error
43 */
44 virtual int8_t setup() = 0;
45
46 /*
47 * Children of subsystem should override this function and use it for running their main loop, analogous to
48 * the Arduino loop() function
49 *
50 * The function should return a status code, with 0 indicating success and any other value representing an error
51 */
52 virtual int8_t loop() = 0;
53
57 uint8_t get_id() const;
58
62 int8_t get_status() const;
63
68 int8_t self_test() const;
69
80 bool add_subscriber(Subsystem *subscriber);
81
86 explicit Subsystem(uint8_t id) : id(id) {};
87
88private:
89 uint8_t id;
90 Subsystem *subscribers[255] = {};
91 int16_t subscriber_count = 0;
92protected:
93 int8_t status = 0;
94 int8_t self_test_result = -1;
95
96 void log(const char fmt[], ...);
97
102 virtual void on_message(SystemMessage *msg) = 0;
103
108 void publish(SystemMessage *msg);
109
114 static void buzzer(uint16_t frequency, uint32_t duration, bool block = false);
115
116};
117
118
119#endif //MRAS_SUBSYSTEM_H
Definition: Subsystem.h:30
virtual const char * get_name()=0
bool add_subscriber(Subsystem *subscriber)
Definition: Subsystem.cpp:22
int8_t get_status() const
Definition: Subsystem.cpp:14
static void buzzer(uint16_t frequency, uint32_t duration, bool block=false)
Definition: Subsystem.cpp:60
uint8_t get_id() const
Definition: Subsystem.cpp:10
int8_t self_test() const
Definition: Subsystem.cpp:18
Subsystem(uint8_t id)
Definition: Subsystem.h:86
void publish(SystemMessage *msg)
Definition: Subsystem.cpp:51
virtual void on_message(SystemMessage *msg)=0
A base class for all system messages.
Definition: SystemMessage.h:32