fork/GramFirmware

git clone https://git.julia.blue/fork/GramFirmware

CarVacrebase to configurator818d962

gram
3.7 KiB107 linesraw
1#include "display/ConfigMenu.hpp"
2
3#include "core/Persistence.hpp"
4#include "core/config_utils.hpp"
5#include "core/mode_selection.hpp"
6#include "reboot.hpp"
7
8ConfigMenu::ConfigMenu(Config &config, CommunicationBackend **backends, size_t backends_count)
9    : _config(config),
10      _backends(backends),
11      _backends_count(backends_count) {}
12
13DisplayModeId ConfigMenu::GetId() {
14    return DISPLAY_MODE_CONFIG;
15}
16
17void ConfigMenu::HandleControls(
18    IntegratedDisplay *instance,
19    const DisplayControls &controls,
20    Button button
21) {
22    if (_current_menu_page == nullptr) {
23        _current_menu_page = _top_level_page;
24        _current_menu_offset = 0;
25        return;
26    }
27
28    if (button == controls.up) {
29        _highlighted_menu_item = max(0, _highlighted_menu_item - 1);
30    } else if (button == controls.down) {
31        _highlighted_menu_item =
32            min(_current_menu_page->items_count - 1, _highlighted_menu_item + 1);
33    } else if (button == controls.enter) {
34        // Bounds check.
35        if (_highlighted_menu_item > _current_menu_page->items_count) {
36            _highlighted_menu_item = 0;
37            return;
38        }
39
40        const MenuPage::MenuItem &selected_item = _current_menu_page->items[_highlighted_menu_item];
41
42        // If there's an action defined, perform it.
43        if (selected_item.action != nullptr) {
44            selected_item.action(instance, this, _config, selected_item.key);
45
46            // Go back up a level.
47            if (_current_menu_page->parent != nullptr) {
48                _current_menu_page = _current_menu_page->parent;
49                _current_menu_offset = 0;
50            }
51            _highlighted_menu_item = 0;
52            return;
53        }
54
55        // If there's a child page defined, drill into it.
56        if (selected_item.page != nullptr) {
57            _current_menu_page = selected_item.page;
58            _current_menu_offset = 0;
59            _highlighted_menu_item = 0;
60            return;
61        }
62    } else if (button == controls.back) {
63        // If at top-level page, go back to input viewer.
64        if (_current_menu_page->parent == nullptr) {
65            // Restore gamemode.
66            if (_backends[0] != nullptr) {
67                _backends[0]->SetGameMode(instance->CurrentGameMode());
68            }
69            ReturnToDashboard(instance);
70            return;
71        }
72
73        _current_menu_page = _current_menu_page->parent;
74        _highlighted_menu_item = 0;
75    }
76}
77
78void ConfigMenu::UpdateDisplay(IntegratedDisplay *instance, Adafruit_GFX &display) {
79    // Unset gamemode to prevent menu button presses being sent to console.
80    if (_backends[0] != nullptr && _backends[0]->CurrentGameMode() != nullptr) {
81        instance->SetGameMode(_backends[0]->CurrentGameMode());
82        _backends[0]->SetGameMode(nullptr);
83    }
84
85    uint8_t font_width = instance->font_width;
86    uint8_t font_height = instance->font_height;
87
88    if (_highlighted_menu_item - _current_menu_offset > max_visible_lines - 1) {
89        _current_menu_offset++;
90    } else if (_highlighted_menu_item < _current_menu_offset) {
91        _current_menu_offset--;
92    }
93    uint8_t last_item_to_display =
94        min(_current_menu_page->items_count - _current_menu_offset, max_visible_lines + 1);
95    for (size_t i = 0; i < last_item_to_display; i++) {
96        if (i + _current_menu_offset == _highlighted_menu_item) {
97            display.setCursor(0, i * (font_height + padding));
98            display.print(highlight_string);
99        }
100        display.setCursor(font_width + padding, i * (font_height + padding));
101        display.print(_current_menu_page->items[i + _current_menu_offset].text);
102    }
103}
104
105void ConfigMenu::ReturnToDashboard(IntegratedDisplay *instance) {
106    instance->SetDisplayMode(DISPLAY_MODE_VIEWER);
107}