diff --git a/firmware/standalone/pacman/main.cpp b/firmware/standalone/pacman/main.cpp index cfb1bbc40..8204f2302 100644 --- a/firmware/standalone/pacman/main.cpp +++ b/firmware/standalone/pacman/main.cpp @@ -23,11 +23,24 @@ #include "pacman.hpp" #include +void notouch(int, int, uint32_t) { + // do nothing +} +void nothing() { + // do nothing +} +bool noencoder(int32_t) { + return false; +} +bool nokeyboard(uint8_t) { + return false; +} + const standalone_application_api_t* _api; extern "C" { __attribute__((section(".standalone_application_information"), used)) standalone_application_information_t _standalone_application_information = { - /*.header_version = */ 1, + /*.header_version = */ 2, /*.app_name = */ "Pac-Man", /*.bitmap_data = */ { @@ -70,18 +83,17 @@ __attribute__((section(".standalone_application_information"), used)) standalone /*.initialize_app = */ initialize, /*.on_event = */ on_event, /*.shutdown = */ shutdown, - /*PaintViewMirror */ NULL, - /*OnTouchEvent */ NULL, - /*OnFocus */ NULL, - /*OnKeyEvent */ NULL, - /*OnEncoder */ NULL, - /*OnKeyboard */ NULL}; + /*PaintViewMirror */ nothing, + /*OnTouchEvent */ notouch, + /*OnFocus */ nothing, + /*OnKeyEvent */ on_key_event, + /*OnEncoder */ noencoder, + /*OnKeyboard */ nokeyboard}; } /* Implementing abort() eliminates requirement for _getpid(), _kill(), _exit(). */ extern "C" void abort() { - while (true) - ; + while (true); } // replace memory allocations to use heap from chibios diff --git a/firmware/standalone/pacman/pacman.cpp b/firmware/standalone/pacman/pacman.cpp index b0c23c277..f93ae8aff 100644 --- a/firmware/standalone/pacman/pacman.cpp +++ b/firmware/standalone/pacman/pacman.cpp @@ -39,8 +39,36 @@ void initialize(const standalone_application_api_t& api) { _api = &api; } +bool on_key_event(uint8_t key_val) { + ui::KeyEvent key = (ui::KeyEvent)key_val; + if (key == ui::KeyEvent::Right) { + but_RIGHT = true; + but_LEFT = false; + but_DOWN = false; + but_UP = false; + } else if (key == ui::KeyEvent::Left) { + but_LEFT = true; + but_RIGHT = false; + but_DOWN = false; + but_UP = false; + } else if (key == ui::KeyEvent::Down) { + but_DOWN = true; + but_RIGHT = false; + but_LEFT = false; + but_UP = false; + } else if (key == ui::KeyEvent::Up) { + but_UP = true; + but_RIGHT = false; + but_LEFT = false; + but_DOWN = false; + } else if (key == ui::KeyEvent::Select) { + but_A = true; + } + return true; +} + void on_event(const uint32_t& events) { - static bool wait_for_button_release{false}; + // static bool wait_for_button_release{false}; if (!_playfield) { _playfield = std::make_unique(); @@ -48,29 +76,30 @@ void on_event(const uint32_t& events) { } if (events & 1) { - auto switches_raw = _api->swizzled_switches() & ((1 << (int)Switch::Right) | (1 << (int)Switch::Left) | (1 << (int)Switch::Down) | (1 << (int)Switch::Up) | (1 << (int)Switch::Sel) | (1 << (int)Switch::Dfu)); - auto switches_debounced = _api->get_switches_state(); + /* + auto switches_raw = _api->swizzled_switches() & ((1 << (int)Switch::Right) | (1 << (int)Switch::Left) | (1 << (int)Switch::Down) | (1 << (int)Switch::Up) | (1 << (int)Switch::Sel) | (1 << (int)Switch::Dfu)); + auto switches_debounced = _api->get_switches_state(); - // For the Select (Start/Pause) button, wait for release to avoid repeat - uint8_t buttons_to_wait_for = (1 << (int)Switch::Sel); - if (wait_for_button_release) { - if ((switches_debounced & buttons_to_wait_for) == 0) - wait_for_button_release = false; - switches_debounced &= ~buttons_to_wait_for; - } else { - if (switches_debounced & buttons_to_wait_for) - wait_for_button_release = true; - } + // For the Select (Start/Pause) button, wait for release to avoid repeat + uint8_t buttons_to_wait_for = (1 << (int)Switch::Sel); + if (wait_for_button_release) { + if ((switches_debounced & buttons_to_wait_for) == 0) + wait_for_button_release = false; + switches_debounced &= ~buttons_to_wait_for; + } else { + if (switches_debounced & buttons_to_wait_for) + wait_for_button_release = true; + } - // For the directional buttons, use the raw inputs for fastest response time - but_RIGHT = (switches_raw & (1 << (int)Switch::Right)) != 0; - but_LEFT = (switches_raw & (1 << (int)Switch::Left)) != 0; - but_DOWN = (switches_raw & (1 << (int)Switch::Down)) != 0; - but_UP = (switches_raw & (1 << (int)Switch::Up)) != 0; - - // For the pause button, use the debounced input to avoid glitches, and OR in the value to make sure that we don't clear it before it's seen - but_A |= (switches_debounced & (1 << (int)Switch::Sel)) != 0; + // For the directional buttons, use the raw inputs for fastest response time + but_RIGHT = (switches_raw & (1 << (int)Switch::Right)) != 0; + but_LEFT = (switches_raw & (1 << (int)Switch::Left)) != 0; + but_DOWN = (switches_raw & (1 << (int)Switch::Down)) != 0; + but_UP = (switches_raw & (1 << (int)Switch::Up)) != 0; + // For the pause button, use the debounced input to avoid glitches, and OR in the value to make sure that we don't clear it before it's seen + but_A |= (switches_debounced & (1 << (int)Switch::Sel)) != 0; + */ _playfield->Step(); } } diff --git a/firmware/standalone/pacman/pacman.hpp b/firmware/standalone/pacman/pacman.hpp index 95ce09fa2..e5793aff5 100644 --- a/firmware/standalone/pacman/pacman.hpp +++ b/firmware/standalone/pacman/pacman.hpp @@ -26,6 +26,7 @@ void initialize(const standalone_application_api_t& api); void on_event(const uint32_t& events); +bool on_key_event(uint8_t key_val); void shutdown(); extern const standalone_application_api_t* _api; diff --git a/firmware/standalone/pacman/playfield.hpp b/firmware/standalone/pacman/playfield.hpp index a643ec5bd..45a02f100 100644 --- a/firmware/standalone/pacman/playfield.hpp +++ b/firmware/standalone/pacman/playfield.hpp @@ -133,39 +133,111 @@ const byte _initSprites[] = // Ghost colors const byte _palette2[] = { - 0, 11, 1, 15, // BINKY red - 0, 11, 3, 15, // PINKY pink - 0, 11, 5, 15, // INKY cyan - 0, 11, 7, 15, // CLYDE brown - 0, 11, 9, 9, // PACMAN yellow - 0, 11, 15, 15, // FRIGHTENED - 0, 11, 0, 15, // DEADEYES + 0, + 11, + 1, + 15, // BINKY red + 0, + 11, + 3, + 15, // PINKY pink + 0, + 11, + 5, + 15, // INKY cyan + 0, + 11, + 7, + 15, // CLYDE brown + 0, + 11, + 9, + 9, // PACMAN yellow + 0, + 11, + 15, + 15, // FRIGHTENED + 0, + 11, + 0, + 15, // DEADEYES - 0, 1, 15, 2, // cherry - 0, 1, 15, 12, // strawberry - 0, 7, 2, 12, // peach - 0, 9, 15, 0, // bell + 0, + 1, + 15, + 2, // cherry + 0, + 1, + 15, + 12, // strawberry + 0, + 7, + 2, + 12, // peach + 0, + 9, + 15, + 0, // bell - 0, 15, 1, 2, // apple - 0, 12, 15, 5, // grape - 0, 11, 9, 1, // galaxian - 0, 5, 15, 15, // key + 0, + 15, + 1, + 2, // apple + 0, + 12, + 15, + 5, // grape + 0, + 11, + 9, + 1, // galaxian + 0, + 5, + 15, + 15, // key }; const byte _paletteIcon2[] = { - 0, 9, 9, 9, // PACMAN + 0, + 9, + 9, + 9, // PACMAN - 0, 2, 15, 1, // cherry - 0, 12, 15, 1, // strawberry - 0, 12, 2, 7, // peach - 0, 0, 15, 9, // bell + 0, + 2, + 15, + 1, // cherry + 0, + 12, + 15, + 1, // strawberry + 0, + 12, + 2, + 7, // peach + 0, + 0, + 15, + 9, // bell - 0, 2, 15, 1, // apple - 0, 12, 15, 5, // grape - 0, 1, 9, 11, // galaxian - 0, 5, 15, 15, // key + 0, + 2, + 15, + 1, // apple + 0, + 12, + 15, + 5, // grape + 0, + 1, + 9, + 11, // galaxian + 0, + 5, + 15, + 15, // key }; #define PACMANICON 1 @@ -571,7 +643,7 @@ class Playfield { } #endif - x += (240 - 224) / 2; + x += (*(_api->screen_width) - 224) / 2; y += (320 - 288) / 2 + 8; // Should be a direct Graphics call