Skip to content

Commit 44b76d3

Browse files
committed
ErrorHandler using static array instead of vector
1 parent 533498e commit 44b76d3

File tree

3 files changed

+46
-19
lines changed

3 files changed

+46
-19
lines changed

Firmware/FFBoard/Inc/ErrorHandler.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77

88
#ifndef SRC_ERRORHANDLER_H_
99
#define SRC_ERRORHANDLER_H_
10-
#include <vector>
10+
#include <array>
1111
#include <string>
1212
#include "thread.hpp"
1313
#include "CommandHandler.h"
14+
#include <span>
15+
16+
#ifndef ERRORHANDLER_MAXERRORS
17+
#define ERRORHANDLER_MAXERRORS 16
18+
#endif
1419

1520
/*
1621
* Error code definitions
@@ -49,24 +54,25 @@ enum class ErrorCode : uint32_t{
4954
* critical errors cause a shutdown or have to be cleared externally
5055
*/
5156
enum class ErrorType : uint8_t{
52-
warning,critical,temporary
57+
none,warning,critical,temporary
5358
};
5459

5560
class Error{
5661
public:
5762
//Error(const Error &e);
63+
Error(){}; // No error
5864
Error(ErrorCode code, ErrorType type, std::string info) : code(code), type(type), info(info){};
5965
ErrorCode code = ErrorCode::none;
60-
ErrorType type = ErrorType::warning;
61-
std::string info = "";
66+
ErrorType type = ErrorType::none;
67+
std::string info;
6268

6369
std::string toString();
6470

6571
/*
6672
* Errors are equivalent if their code and type match
6773
*/
6874
bool operator ==(const Error &b) const {return (this->code == b.code && this->type == b.type);}
69-
75+
bool isError() const {return this->code != ErrorCode::none && this->type != ErrorType::none;}
7076
};
7177

7278

@@ -86,10 +92,11 @@ class ErrorHandler {
8692
static void clearTemp();
8793
static void clearAll();
8894

89-
static std::vector<Error>* getErrors(); // Returns a vector of active errors
95+
static std::span<Error> getErrors(); // Returns a vector of active errors
9096

9197
protected:
92-
static std::vector<Error> errors;
98+
static std::array<Error,ERRORHANDLER_MAXERRORS> errors;
99+
static void sortErrors();
93100
};
94101

95102
/*

Firmware/FFBoard/Src/ErrorHandler.cpp

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
#include "FFBoardMain.h"
1111
#include "cppmain.h"
1212
#include "critical.hpp"
13+
#include <span>
1314

1415
std::vector<ErrorHandler*> ErrorHandler::errorHandlers;
15-
std::vector<Error> ErrorHandler::errors;
16+
//std::vector<Error> ErrorHandler::errors;
17+
std::array<Error,ERRORHANDLER_MAXERRORS> ErrorHandler::errors;
1618

1719

1820
std::string Error::toString(){
@@ -37,7 +39,7 @@ std::string Error::toString(){
3739

3840

3941
ErrorHandler::ErrorHandler(){
40-
errors.reserve(10);
42+
// errors.reserve(10);
4143
addCallbackHandler(errorHandlers,this);
4244
}
4345

@@ -54,7 +56,7 @@ void ErrorHandler::clearAll(){
5456
for(Error& error : errors)
5557
e->errorCallback(error, true);
5658
}
57-
errors.clear();
59+
errors.fill(Error());
5860
}
5961

6062
/*
@@ -63,10 +65,12 @@ void ErrorHandler::clearAll(){
6365
void ErrorHandler::clearTemp(){
6466
for (uint8_t i = 0; i < errors.size(); i++){
6567
if((errors)[i].type == ErrorType::temporary){
66-
errors.erase(errors.begin()+i);
68+
//errors.erase(errors.begin()+i);
69+
errors[i] = Error(); // Empty
6770
break;
6871
}
6972
}
73+
sortErrors();
7074
}
7175

7276
void ErrorHandler::addError(const Error &error){
@@ -75,7 +79,12 @@ void ErrorHandler::addError(const Error &error){
7579
return;
7680
}
7781
}
78-
errors.push_back(error);
82+
// errors.push_back(error);
83+
auto errIt = std::find_if(errors.begin(), errors.end(), [](const Error& err){return !err.isError();});
84+
if(errIt != errors.end()){
85+
*errIt = error; // If buffer is full do not store error but still call callbacks.
86+
}
87+
7988

8089
// Call all error handler with this error
8190
//cpp_freertos::CriticalSection::SuspendScheduler();
@@ -88,7 +97,8 @@ void ErrorHandler::addError(const Error &error){
8897
void ErrorHandler::clearError(const Error &error){
8998
for (uint8_t i = 0; i < errors.size(); i++){
9099
if(errors[i] == error){
91-
errors.erase(errors.begin()+i);
100+
// errors.erase(errors.begin()+i);
101+
errors[i] = Error(); // Empty
92102
break;
93103
}
94104
}
@@ -97,6 +107,7 @@ void ErrorHandler::clearError(const Error &error){
97107
for(ErrorHandler* e : errorHandlers){
98108
e->errorCallback(error, true);
99109
}
110+
sortErrors();
100111
}
101112

102113
/*
@@ -105,22 +116,31 @@ void ErrorHandler::clearError(const Error &error){
105116
void ErrorHandler::clearError(ErrorCode errorcode){
106117
for (uint8_t i = 0; i < errors.size(); i++){
107118
if(errors[i].code == errorcode){
108-
errors.erase(errors.begin()+i);
119+
// errors.erase(errors.begin()+i);
120+
errors[i] = Error(); // Empty
109121
for(ErrorHandler* e : errorHandlers){
110122
e->errorCallback(errors[i], true);
111123
}
112124
}
113125
}
126+
sortErrors();
114127
}
115128

116-
std::vector<Error>* ErrorHandler::getErrors(){
117-
return &errors;
129+
std::span<Error> ErrorHandler::getErrors(){
130+
return std::span<Error>(errors.begin(), std::find_if(errors.begin(), errors.end(), [](const Error& err){return err.type == ErrorType::none;}));
118131
}
119132

120133
void ErrorHandler::errorCallback(const Error &error, bool cleared){
121134

122135
}
123136

137+
void ErrorHandler::sortErrors(){
138+
auto errSortFun = [](const Error& a, const Error& b){
139+
return a.code < b.code;
140+
};
141+
std::sort(errors.begin(),errors.end(),errSortFun);
142+
}
143+
124144

125145
//ClassIdentifier ErrorPrinter::info = {
126146
// .name = "Errorprinter",

Firmware/FFBoard/Src/SystemCommands.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,16 +319,16 @@ void SystemCommands::replyFlashDump(std::vector<CommandReply>& replies){
319319
* Prints a formatted list of error conditions
320320
*/
321321
void SystemCommands::replyErrors(std::vector<CommandReply>& replies){
322-
std::vector<Error>* errors = ErrorHandler::getErrors();
323-
if(errors->size() == 0){
322+
std::span<Error> errors = ErrorHandler::getErrors();
323+
if(errors.size() == 0){
324324
CommandReply reply;
325325
reply.reply += "None";
326326
reply.type = CommandReplyType::STRING_OR_INT;
327327
replies.push_back(reply);
328328
return;
329329
}
330330

331-
for(Error error : *errors){
331+
for(Error error : errors){
332332
CommandReply reply;
333333
reply.reply += error.toString() + "\n";
334334
reply.val = (uint32_t)error.code;

0 commit comments

Comments
 (0)