diff --git a/main/drivers/CMakeLists.txt b/main/drivers/CMakeLists.txt index ecca240..cc762d8 100644 --- a/main/drivers/CMakeLists.txt +++ b/main/drivers/CMakeLists.txt @@ -13,6 +13,7 @@ set(SOURCES "sd.cpp" "speaker.cpp" "sseg.cpp" + "star_code.cpp" "state_tracking.cpp" "tft.cpp" "wires.cpp" diff --git a/main/drivers/star_code.cpp b/main/drivers/star_code.cpp new file mode 100644 index 0000000..7ddfba1 --- /dev/null +++ b/main/drivers/star_code.cpp @@ -0,0 +1,74 @@ +#include "star_code.h" + +#include +#include +#include +#include + +static std::vector star_codes; + +/// Checks if a triggered code matches an expected code. +/// @return true iff the codes match, where '*'s in the expected code can match any character in the triggered code +static bool check_code_match(const char* triggered, const char* expected) { + size_t triggered_len = strlen(triggered); + size_t match_len = strlen(triggered); + + if (triggered_len != match_len) + return false; + + for (int i = 0; i < triggered_len; i++) { + if (!(expected[i] == '*' || expected[i] == triggered[i])) { + return false; + } + } + + return true; +} + +bool add_star_code(StarCodeEntry code) { + if (code.code == nullptr || strlen(code.code) > 8) { + return false; + } + if (code.display_text != nullptr && strlen(code.display_text) > 9) { + return false; + } + + // check for a existing entry + auto it = std::find_if(star_codes.begin(), star_codes.end(), [&](const StarCodeEntry& other) { + return check_code_match(code.code, other.code); + }); + + if (it != star_codes.end()) { + // existing star code found! + return false; + } + + star_codes.push_back(code); + return true; +} + +bool rm_star_code(char* code){ + auto it = std::find_if(star_codes.begin(), star_codes.end(), [&](const StarCodeEntry& star_code) { + return strcmp(code, star_code.code) == 0; + }); + + if (it == star_codes.end()) { + return false; + } + + star_codes.erase(it); + return true; +} + +bool trigger_star_code(char* code) { + auto it = std::find_if(star_codes.begin(), star_codes.end(), [&](const StarCodeEntry& other) { + return check_code_match(code, other.code); + }); + + if (it == star_codes.end()) { + return false; + } + + (it->callback)(); + return true; +} diff --git a/main/drivers/star_code.h b/main/drivers/star_code.h new file mode 100644 index 0000000..e4d09e3 --- /dev/null +++ b/main/drivers/star_code.h @@ -0,0 +1,39 @@ +#ifndef STAR_CODE_H +#define STAR_CODE_H + +#include + +/// @brief A handler for a specific star code +struct StarCodeEntry { + /// @brief The star code without the star + /// + /// This must be <= 8 characters. + /// + /// You may include a * in the code to match on any character + const char* code; + /// @brief The text to display when the star code is entered (or NULL). + /// + /// This must be <= 9 characters. + const char* display_text; + /// @brief The number of ticks to delay when the star code is entered before calling the handler. + uint32_t delay_ticks; + /// @brief The function to call when the star code is entered. + void (*callback)(void); +}; + +/// @brief Adds a star code to be handled. +/// @param code the star code to add (without the *) +/// @return true iff the star code was added +bool add_star_code(StarCodeEntry code); + +/// @brief removes a star code to stop handling it. +/// @param code the star code to remove (without the *) +/// @return true iff the star code was removed +bool rm_star_code(char* code); + +/// @brief Triggers the given starcode. +/// @param code the star code to trigger (without the *) +/// @return true iff a star code was triggered +bool trigger_star_code(char* code); + +#endif /* STAR_CODE_H */ \ No newline at end of file diff --git a/main/drivers/state_tracking.cpp b/main/drivers/state_tracking.cpp index e152d6d..8b0e750 100644 --- a/main/drivers/state_tracking.cpp +++ b/main/drivers/state_tracking.cpp @@ -25,7 +25,6 @@ TaskHandle_t playback_task_handle; static volatile state_t state = STATE_IDLE; - /// @brief Periodically flushes and syncs (if neccesary) the output stream. /// @param arg unused. static void flush_file_task(void* arg) { diff --git a/main/helper.h b/main/helper.h index 98697d9..ec27789 100644 --- a/main/helper.h +++ b/main/helper.h @@ -18,7 +18,6 @@ struct StarCodeHandler { }; // TODO: add something for RNG. -// TODO: add something for colors, to make everything consistant. /// Clears most persistant bomb state void clean_bomb(void);