Skip to content

Commit

Permalink
Arrays & spans
Browse files Browse the repository at this point in the history
  • Loading branch information
stojadin2701 committed Jul 16, 2023
1 parent ec20a07 commit 0d1a927
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 55 deletions.
12 changes: 6 additions & 6 deletions flight_computer/src/cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,8 @@ static void print_value_pointer(const char *cmdName, const cli_value_t *var, con
}
break;
case MODE_LOOKUP:
if (value < lookup_tables[var->config.lookup.table_index].value_count) {
cli_print(lookup_tables[var->config.lookup.table_index].values[value]);
if (static_cast<size_t>(value) < lookup_tables[var->config.lookup.table_index].size()) {
cli_print(lookup_tables[var->config.lookup.table_index][value]);
} else {
value_is_corrupted = true;
}
Expand Down Expand Up @@ -430,15 +430,15 @@ void cli_print_var_range(const cli_value_t *var) {
}
} break;
case (MODE_LOOKUP): {
const lookup_table_entry_t *tableEntry = &lookup_tables[var->config.lookup.table_index];
const std::span<const char *const> tableEntry = lookup_tables[var->config.lookup.table_index];
cli_print("Allowed values: ");
bool first_entry = true;
for (uint32_t i = 0; i < tableEntry->value_count; i++) {
if (tableEntry->values[i]) {
for (uint32_t i = 0; i < tableEntry.size(); i++) {
if (tableEntry[i]) {
if (!first_entry) {
cli_print(", ");
}
cli_printf("%s", tableEntry->values[i]);
cli_printf("%s", tableEntry[i]);
first_entry = false;
}
}
Expand Down
20 changes: 10 additions & 10 deletions flight_computer/src/cli/cli_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ static void cli_cmd_set(const char *cmd_name, char *args) {
} else {
tableIndex = val->config.lookup.table_index;
}
const lookup_table_entry_t *tableEntry = &lookup_tables[tableIndex];
const std::span<const char *const> &tableEntry = lookup_tables[tableIndex];
bool matched = false;
for (uint32_t tableValueIndex = 0; tableValueIndex < tableEntry->value_count && !matched; tableValueIndex++) {
matched = tableEntry->values[tableValueIndex] && strcasecmp(tableEntry->values[tableValueIndex], eqptr) == 0;
for (uint32_t tableValueIndex = 0; tableValueIndex < tableEntry.size() && !matched; tableValueIndex++) {
matched = tableEntry[tableValueIndex] && strcasecmp(tableEntry[tableValueIndex], eqptr) == 0;

if (matched) {
cli_set_var(val, tableValueIndex);
Expand Down Expand Up @@ -847,33 +847,33 @@ static void print_control_config() {
}

static void print_action_config() {
const lookup_table_entry_t *p_event_table = &lookup_tables[TABLE_EVENTS];
const lookup_table_entry_t *p_action_table = &lookup_tables[TABLE_ACTIONS];
const std::span<const char *const> &p_event_table = lookup_tables[TABLE_EVENTS];
const std::span<const char *const> &p_action_table = lookup_tables[TABLE_ACTIONS];

cli_printf("\n * ACTION CONFIGURATION *\n");
config_action_t action;
for (int i = 0; i < NUM_EVENTS; i++) {
int nr_actions = cc_get_num_actions((cats_event_e)(i));
if (nr_actions > 0) {
cli_printf("\n%s\n", p_event_table->values[i]);
cli_printf("\n%s\n", p_event_table[i]);
cli_printf(" Number of Actions: %d\n", nr_actions);
for (int j = 0; j < nr_actions; j++) {
cc_get_action((cats_event_e)(i), j, &action);
cli_printf(" %s - %d\n", p_action_table->values[action.action_idx], action.arg);
cli_printf(" %s - %d\n", p_action_table[action.action_idx], action.arg);
}
}
}
}

static void print_timer_config() {
const lookup_table_entry_t *p_event_table = &lookup_tables[TABLE_EVENTS];
const std::span<const char *const> &p_event_table = lookup_tables[TABLE_EVENTS];

cli_printf("\n\n * TIMER CONFIGURATION *\n");
for (int i = 0; i < NUM_TIMERS; i++) {
if (global_cats_config.timers[i].duration > 0) {
cli_printf("\nTIMER %d\n", i + 1);
cli_printf(" Start: %s\n", p_event_table->values[global_cats_config.timers[i].start_event]);
cli_printf(" Trigger: %s\n", p_event_table->values[global_cats_config.timers[i].trigger_event]);
cli_printf(" Start: %s\n", p_event_table[global_cats_config.timers[i].start_event]);
cli_printf(" Trigger: %s\n", p_event_table[global_cats_config.timers[i].trigger_event]);
cli_printf(" Duration: %lu ms\n", global_cats_config.timers[i].duration);
}
}
Expand Down
9 changes: 0 additions & 9 deletions flight_computer/src/cli/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@

#include <cstddef>

#define LOOKUP_TABLE_ENTRY(name) \
{ name, ARRAYLEN(name) }

const lookup_table_entry_t lookup_tables[] = {
LOOKUP_TABLE_ENTRY(event_map), LOOKUP_TABLE_ENTRY(action_map),
LOOKUP_TABLE_ENTRY(on_off_map), {(const char *const *)recorder_speed_map, NUM_REC_SPEEDS},
LOOKUP_TABLE_ENTRY(battery_map),
};

#undef LOOKUP_TABLE_ENTRY

const cli_value_t value_table[] = {
Expand Down
17 changes: 12 additions & 5 deletions flight_computer/src/cli/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,21 @@

#pragma once

#include <array>
#include <span>

#include "config/cats_config.hpp"
#include "util/enum_str_maps.hpp"

#define ARRAYLEN(x) (sizeof(x) / sizeof((x)[0]))

enum lookup_table_index_e { TABLE_EVENTS = 0, TABLE_ACTIONS, TABLE_POWER, TABLE_SPEEDS, TABLE_BATTERY };

struct lookup_table_entry_t {
const char *const *values;
const uint8_t value_count;
};
// // const char *const *values;
// struct lookup_table_entry_t {
// std::span<const char *> values;
// // const uint8_t value_count;
// };

#define VALUE_TYPE_OFFSET 0
#define VALUE_SECTION_OFFSET 3
Expand Down Expand Up @@ -102,7 +107,9 @@ struct cli_value_t {
callback_f cb;
} __attribute__((packed));

extern const lookup_table_entry_t lookup_tables[];
inline constexpr std::array<std::span<const char *const>, 5> lookup_tables{event_map, action_map, on_off_map,
recorder_speed_map, battery_map};

extern const uint16_t value_table_entry_count;

extern const cli_value_t value_table[];
Expand Down
21 changes: 2 additions & 19 deletions flight_computer/src/util/enum_str_maps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,11 @@
#include "config/globals.hpp"
#include "util/log.h"

#include <array>
#include <cstdio>

const char* const fsm_map[8] = {"INVALID", "CALIBRATING", "READY", "THRUSTING",
"COASTING", "DROGUE", "MAIN", "TOUCHDOWN"};

const char* const event_map[9] = {
"CALIBRATE", "READY", "LIFTOFF", "MAX_V", "APOGEE", "MAIN_DEPLOYMENT", "TOUCHDOWN", "CUSTOM_1", "CUSTOM_2",
};

const char* const action_map[8] = {
"NONE", "DELAY", "HC_ONE", "HC_TWO", "LL_ONE", "SERVO_ONE", "SERVO_TWO", "RECORDER",
};

const char* const battery_map[3] = {"LI-ION", "LI-PO", "ALKALINE"};

// Filled later depending on tick frequency
char* recorder_speed_map[NUM_REC_SPEEDS] = {};

const char* const on_off_map[2] = {
"OFF",
"ON",
};
std::array<char*, NUM_REC_SPEEDS> recorder_speed_map = {};

void init_recorder_speed_map() {
for (uint32_t i = 0; i < NUM_REC_SPEEDS; ++i) {
Expand Down
26 changes: 20 additions & 6 deletions flight_computer/src/util/enum_str_maps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,28 @@

#pragma once

#include <array>

#include "config/cats_config.hpp"

extern const char *const fsm_map[8];
extern const char *const event_map[9];
extern const char *const action_map[8];
extern const char *const on_off_map[2];
extern const char *const battery_map[3];
inline constexpr std::array<const char *, 8> fsm_map{"INVALID", "CALIBRATING", "READY", "THRUSTING",
"COASTING", "DROGUE", "MAIN", "TOUCHDOWN"};

inline constexpr std::array<const char *, 9> event_map{
"CALIBRATE", "READY", "LIFTOFF", "MAX_V", "APOGEE", "MAIN_DEPLOYMENT", "TOUCHDOWN", "CUSTOM_1", "CUSTOM_2",
};

inline constexpr std::array<const char *, 8> action_map{
"NONE", "DELAY", "HC_ONE", "HC_TWO", "LL_ONE", "SERVO_ONE", "SERVO_TWO", "RECORDER",
};

inline constexpr std::array<const char *, 2> on_off_map{
"OFF",
"ON",
};

inline constexpr std::array<const char *, 3> battery_map{"LI-ION", "LI-PO", "ALKALINE"};

extern char *recorder_speed_map[NUM_REC_SPEEDS];
extern std::array<char *, NUM_REC_SPEEDS> recorder_speed_map;

void init_recorder_speed_map();

0 comments on commit 0d1a927

Please sign in to comment.