start impl on generic starcode handler
This commit is contained in:
parent
ee30fce074
commit
e1ce43d57c
@ -13,6 +13,7 @@ set(SOURCES
|
|||||||
"sd.cpp"
|
"sd.cpp"
|
||||||
"speaker.cpp"
|
"speaker.cpp"
|
||||||
"sseg.cpp"
|
"sseg.cpp"
|
||||||
|
"star_code.cpp"
|
||||||
"state_tracking.cpp"
|
"state_tracking.cpp"
|
||||||
"tft.cpp"
|
"tft.cpp"
|
||||||
"wires.cpp"
|
"wires.cpp"
|
||||||
|
|||||||
74
main/drivers/star_code.cpp
Normal file
74
main/drivers/star_code.cpp
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
#include "star_code.h"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
static std::vector<StarCodeEntry> 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;
|
||||||
|
}
|
||||||
39
main/drivers/star_code.h
Normal file
39
main/drivers/star_code.h
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
#ifndef STAR_CODE_H
|
||||||
|
#define STAR_CODE_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
/// @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 */
|
||||||
@ -25,7 +25,6 @@ TaskHandle_t playback_task_handle;
|
|||||||
|
|
||||||
static volatile state_t state = STATE_IDLE;
|
static volatile state_t state = STATE_IDLE;
|
||||||
|
|
||||||
|
|
||||||
/// @brief Periodically flushes and syncs (if neccesary) the output stream.
|
/// @brief Periodically flushes and syncs (if neccesary) the output stream.
|
||||||
/// @param arg unused.
|
/// @param arg unused.
|
||||||
static void flush_file_task(void* arg) {
|
static void flush_file_task(void* arg) {
|
||||||
|
|||||||
@ -18,7 +18,6 @@ struct StarCodeHandler {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// TODO: add something for RNG.
|
// TODO: add something for RNG.
|
||||||
// TODO: add something for colors, to make everything consistant.
|
|
||||||
|
|
||||||
/// Clears most persistant bomb state
|
/// Clears most persistant bomb state
|
||||||
void clean_bomb(void);
|
void clean_bomb(void);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user