diff --git a/Marlin/src/HAL/HAL_AVR/SanityCheck.h b/Marlin/src/HAL/HAL_AVR/SanityCheck.h index 1c5d545477..197c6554d7 100644 --- a/Marlin/src/HAL/HAL_AVR/SanityCheck.h +++ b/Marlin/src/HAL/HAL_AVR/SanityCheck.h @@ -46,7 +46,7 @@ * Sanity checks for Spindle / Laser */ #if ENABLED(SPINDLE_LASER_ENABLE) - #if DISABLED(SPINDLE_LASER_ENABLE) + #if PIN_EXISTS(SPINDLE_LASER_ENABLE) #error "SPINDLE_LASER_ENABLE requires SPINDLE_LASER_ENABLE_PIN." #elif SPINDLE_DIR_CHANGE && !PIN_EXISTS(SPINDLE_DIR) #error "SPINDLE_DIR_PIN not defined." diff --git a/Marlin/src/HAL/HAL_ESP32/HAL.cpp b/Marlin/src/HAL/HAL_ESP32/HAL.cpp index 76b961b7b9..5e281f2ef3 100644 --- a/Marlin/src/HAL/HAL_ESP32/HAL.cpp +++ b/Marlin/src/HAL/HAL_ESP32/HAL.cpp @@ -41,7 +41,10 @@ #endif #if ENABLED(WEBSUPPORT) #include "web.h" + #include "spiffs.h" #endif +#elif ENABLED(EEPROM_SETTINGS) + #include "spiffs.h" #endif // -------------------------------------------------------------------------- @@ -95,9 +98,12 @@ void HAL_init(void) { OTA_init(); #endif #if ENABLED(WEBSUPPORT) + spiffs_init(); web_init(); #endif server.begin(); + #elif ENABLED(EEPROM_SETTINGS) + spiffs_init(); #endif i2s_init(); diff --git a/Marlin/src/HAL/HAL_ESP32/persistent_store_spiffs.cpp b/Marlin/src/HAL/HAL_ESP32/persistent_store_spiffs.cpp new file mode 100644 index 0000000000..795edf6781 --- /dev/null +++ b/Marlin/src/HAL/HAL_ESP32/persistent_store_spiffs.cpp @@ -0,0 +1,93 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#ifdef ARDUINO_ARCH_ESP32 + +#include "../../inc/MarlinConfig.h" + +#if ENABLED(EEPROM_SETTINGS) && DISABLED(FLASH_EEPROM_EMULATION) + +#include "../shared/persistent_store_api.h" + +#include "SPIFFS.h" +#include "FS.h" +#include "spiffs.h" + +#define HAL_ESP32_EEPROM_SIZE 4096 + +File eeprom_file; + +bool PersistentStore::access_start() { + if (spiffs_initialized) { + eeprom_file = SPIFFS.open("/eeprom.dat", "r+"); + + size_t file_size = eeprom_file.size(); + if (file_size < HAL_ESP32_EEPROM_SIZE) { + bool write_ok = eeprom_file.seek(file_size); + + while (write_ok && file_size < HAL_ESP32_EEPROM_SIZE) { + write_ok = eeprom_file.write(0xFF) == 1; + file_size++; + } + } + return true; + } + return false; +} + +bool PersistentStore::access_finish() { + eeprom_file.close(); + return true; +} + +bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) { + if (!eeprom_file.seek(pos)) return true; // return true for any error + if (eeprom_file.write(value, size) != size) return true; + + crc16(crc, value, size); + pos += size; + + return false; +} + +bool PersistentStore::read_data(int &pos, uint8_t* value, size_t size, uint16_t *crc, const bool writing/*=true*/) { + if (!eeprom_file.seek(pos)) return true; // return true for any error + + if (writing) { + if (eeprom_file.read(value, size) != size) return true; + crc16(crc, value, size); + } + else { + uint8_t tmp[size]; + if (eeprom_file.read(tmp, size) != size) return true; + crc16(crc, tmp, size); + } + + pos += size; + + return false; +} + +size_t PersistentStore::capacity() { return HAL_ESP32_EEPROM_SIZE; } + +#endif // EEPROM_SETTINGS +#endif // ARDUINO_ARCH_ESP32 diff --git a/Marlin/src/HAL/HAL_ESP32/spiffs.cpp b/Marlin/src/HAL/HAL_ESP32/spiffs.cpp new file mode 100644 index 0000000000..c960f386d4 --- /dev/null +++ b/Marlin/src/HAL/HAL_ESP32/spiffs.cpp @@ -0,0 +1,44 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#ifdef ARDUINO_ARCH_ESP32 + +#include "../../inc/MarlinConfigPre.h" + +#if EITHER(WEBSUPPORT, EEPROM_SETTINGS) + +#include "../../core/serial.h" + +#include "FS.h" +#include "SPIFFS.h" + +bool spiffs_initialized; + +void spiffs_init() { + if (SPIFFS.begin()) + spiffs_initialized = true; + else + SERIAL_ECHO_MSG("SPIFFS mount failed"); +} + +#endif // WEBSUPPORT +#endif // ARDUINO_ARCH_ESP32 diff --git a/Marlin/src/HAL/HAL_ESP32/spiffs.h b/Marlin/src/HAL/HAL_ESP32/spiffs.h new file mode 100644 index 0000000000..e1573340cd --- /dev/null +++ b/Marlin/src/HAL/HAL_ESP32/spiffs.h @@ -0,0 +1,26 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +#pragma once + +extern bool spiffs_initialized; + +void spiffs_init(); diff --git a/Marlin/src/HAL/HAL_ESP32/web.cpp b/Marlin/src/HAL/HAL_ESP32/web.cpp index a3a6cce729..6bbc0d13aa 100644 --- a/Marlin/src/HAL/HAL_ESP32/web.cpp +++ b/Marlin/src/HAL/HAL_ESP32/web.cpp @@ -23,9 +23,6 @@ #if ENABLED(WEBSUPPORT) -#include "../../core/serial.h" - -#include "FS.h" #include "SPIFFS.h" #include "wifi.h" @@ -37,12 +34,8 @@ void onNotFound(AsyncWebServerRequest *request){ void web_init() { server.addHandler(&events); // attach AsyncEventSource - if (SPIFFS.begin()) { - server.serveStatic("/", SPIFFS, "/www").setDefaultFile("index.html"); - server.onNotFound(onNotFound); - } - else - SERIAL_ECHO_MSG("SPIFFS Mount Failed"); + server.serveStatic("/", SPIFFS, "/www").setDefaultFile("index.html"); + server.onNotFound(onNotFound); } #endif // WEBSUPPORT diff --git a/Marlin/src/Marlin.cpp b/Marlin/src/Marlin.cpp index 14e61d8460..4bdbe74b57 100644 --- a/Marlin/src/Marlin.cpp +++ b/Marlin/src/Marlin.cpp @@ -1045,7 +1045,7 @@ void setup() { ui.init(); ui.reset_status(); - #if ENABLED(SHOW_BOOTSCREEN) + #if HAS_SPI_LCD && ENABLED(SHOW_BOOTSCREEN) ui.show_bootscreen(); #endif @@ -1143,6 +1143,9 @@ void loop() { #if ENABLED(POWER_LOSS_RECOVERY) card.removeJobRecoveryFile(); #endif + #ifdef EVENT_GCODE_SD_STOP + enqueue_and_echo_commands_P(PSTR(EVENT_GCODE_SD_STOP)); + #endif } #endif // SDSUPPORT diff --git a/Marlin/src/feature/power_loss_recovery.cpp b/Marlin/src/feature/power_loss_recovery.cpp index 1154b941ec..a16218c29a 100644 --- a/Marlin/src/feature/power_loss_recovery.cpp +++ b/Marlin/src/feature/power_loss_recovery.cpp @@ -125,6 +125,9 @@ void PrintJobRecovery::save(const bool force/*=false*/, const bool save_queue/*= millis_t ms = millis(); #endif + // Did Z change since the last call? + const float zmoved = current_position[Z_AXIS] - info.current_position[Z_AXIS]; + if (force #if DISABLED(SAVE_EACH_CMD_MODE) // Always save state when enabled #if PIN_EXISTS(POWER_LOSS) // Save if power loss pin is triggered @@ -133,8 +136,8 @@ void PrintJobRecovery::save(const bool force/*=false*/, const bool save_queue/*= #if SAVE_INFO_INTERVAL_MS > 0 // Save if interval is elapsed || ELAPSED(ms, next_save_ms) #endif - // Save every time Z is higher than the last call - || current_position[Z_AXIS] > info.current_position[Z_AXIS] + || zmoved > 0 // Z moved up (including Z-hop) + || zmoved < -5 // Z moved down a lot (for some reason) #endif ) { diff --git a/Marlin/src/feature/runout.h b/Marlin/src/feature/runout.h index dfc0592ce3..3f1e5fa018 100644 --- a/Marlin/src/feature/runout.h +++ b/Marlin/src/feature/runout.h @@ -49,7 +49,7 @@ class FilamentMonitorBase { #if ENABLED(HOST_ACTION_COMMANDS) static bool host_handling; #else - constexpr static bool host_handling = false; + static constexpr bool host_handling = false; #endif }; diff --git a/Marlin/src/gcode/bedlevel/G26.cpp b/Marlin/src/gcode/bedlevel/G26.cpp index 726679f345..60979aad00 100644 --- a/Marlin/src/gcode/bedlevel/G26.cpp +++ b/Marlin/src/gcode/bedlevel/G26.cpp @@ -768,6 +768,7 @@ void GcodeSuite::G26() { #if ENABLED(ARC_SUPPORT) #define ARC_LENGTH(quarters) (INTERSECTION_CIRCLE_RADIUS * M_PI * (quarters) / 2) + #define INTERSECTION_CIRCLE_DIAM ((INTERSECTION_CIRCLE_RADIUS) * 2) float sx = circle_x + INTERSECTION_CIRCLE_RADIUS, // default to full circle ex = circle_x + INTERSECTION_CIRCLE_RADIUS, sy = circle_y, ey = circle_y, @@ -775,14 +776,8 @@ void GcodeSuite::G26() { // Figure out where to start and end the arc - we always print counterclockwise if (xi == 0) { // left edge - if (!f) { - sx = circle_x; - sy -= (INTERSECTION_CIRCLE_RADIUS); - } - if (!b) { - ex = circle_x; - ey += INTERSECTION_CIRCLE_RADIUS; - } + if (!f) { sx = circle_x; sy -= INTERSECTION_CIRCLE_RADIUS; } + if (!b) { ex = circle_x; ey += INTERSECTION_CIRCLE_RADIUS; } arc_length = (f || b) ? ARC_LENGTH(1) : ARC_LENGTH(2); } else if (r) { // right edge @@ -793,26 +788,23 @@ void GcodeSuite::G26() { arc_length = (f || b) ? ARC_LENGTH(1) : ARC_LENGTH(2); } else if (f) { - ex = circle_x - (INTERSECTION_CIRCLE_RADIUS); + ex -= INTERSECTION_CIRCLE_DIAM; arc_length = ARC_LENGTH(2); } else if (b) { - sx = circle_x - (INTERSECTION_CIRCLE_RADIUS); + sx -= INTERSECTION_CIRCLE_DIAM; arc_length = ARC_LENGTH(2); } - const float arc_offset[2] = { - circle_x - sx, - circle_y - sy - }; - const float dx_s = current_position[X_AXIS] - sx, // find our distance from the start of the actual circle + const float arc_offset[2] = { circle_x - sx, circle_y - sy }, + dx_s = current_position[X_AXIS] - sx, // find our distance from the start of the actual circle dy_s = current_position[Y_AXIS] - sy, - dist_start = HYPOT2(dx_s, dy_s); - const float endpoint[XYZE] = { - ex, ey, - g26_layer_height, - current_position[E_AXIS] + (arc_length * g26_e_axis_feedrate * g26_extrusion_multiplier) - }; + dist_start = HYPOT2(dx_s, dy_s), + endpoint[XYZE] = { + ex, ey, + g26_layer_height, + current_position[E_AXIS] + (arc_length * g26_e_axis_feedrate * g26_extrusion_multiplier) + }; if (dist_start > 2.0) { retract_filament(destination); diff --git a/Marlin/src/gcode/calibrate/G28.cpp b/Marlin/src/gcode/calibrate/G28.cpp index f0d05d832e..02c6e8a37d 100644 --- a/Marlin/src/gcode/calibrate/G28.cpp +++ b/Marlin/src/gcode/calibrate/G28.cpp @@ -182,7 +182,6 @@ * */ void GcodeSuite::G28(const bool always_home_all) { - if (DEBUGGING(LEVELING)) { DEBUG_ECHOLNPGM(">>> G28"); log_machine_info(); @@ -268,13 +267,16 @@ void GcodeSuite::G28(const bool always_home_all) { const bool homeX = always_home_all || parser.seen('X'), homeY = always_home_all || parser.seen('Y'), homeZ = always_home_all || parser.seen('Z'), - home_all = (!homeX && !homeY && !homeZ) || (homeX && homeY && homeZ); + home_all = (!homeX && !homeY && !homeZ) || (homeX && homeY && homeZ), + doX = home_all || homeX, + doY = home_all || homeY, + doZ = home_all || homeZ; set_destination_from_current(); #if Z_HOME_DIR > 0 // If homing away from BED do Z first - if (home_all || homeZ) homeaxis(Z_AXIS); + if (doZ) homeaxis(Z_AXIS); #endif @@ -285,7 +287,7 @@ void GcodeSuite::G28(const bool always_home_all) { (parser.seenval('R') ? parser.value_linear_units() : Z_HOMING_HEIGHT) ); - if (z_homing_height && (home_all || homeX || homeY)) { + if (z_homing_height && (doX || doY)) { // Raise Z before homing any other axes and z is not already high enough (never lower z) destination[Z_AXIS] = z_homing_height; if (destination[Z_AXIS] > current_position[Z_AXIS]) { @@ -296,25 +298,25 @@ void GcodeSuite::G28(const bool always_home_all) { #if ENABLED(QUICK_HOME) - if (home_all || (homeX && homeY)) quick_home_xy(); + if (doX && doY) quick_home_xy(); #endif // Home Y (before X) #if ENABLED(HOME_Y_BEFORE_X) - if (home_all || homeY + if (doY #if ENABLED(CODEPENDENT_XY_HOMING) - || homeX + || doX #endif ) homeaxis(Y_AXIS); #endif // Home X - if (home_all || homeX + if (doX #if ENABLED(CODEPENDENT_XY_HOMING) && DISABLED(HOME_Y_BEFORE_X) - || homeY + || doY #endif ) { @@ -345,12 +347,12 @@ void GcodeSuite::G28(const bool always_home_all) { // Home Y (after X) #if DISABLED(HOME_Y_BEFORE_X) - if (home_all || homeY) homeaxis(Y_AXIS); + if (doY) homeaxis(Y_AXIS); #endif // Home Z last if homing towards the bed #if Z_HOME_DIR < 0 - if (home_all || homeZ) { + if (doZ) { #if ENABLED(Z_SAFE_HOMING) home_z_safely(); #else @@ -361,7 +363,7 @@ void GcodeSuite::G28(const bool always_home_all) { move_z_after_probing(); #endif - } // home_all || homeZ + } // doZ #endif // Z_HOME_DIR < 0 sync_plan_position(); @@ -402,6 +404,16 @@ void GcodeSuite::G28(const bool always_home_all) { #endif // DUAL_X_CARRIAGE + #ifdef HOMING_BACKOFF_MM + endstops.enable(false); + constexpr float backoff[XYZ] = HOMING_BACKOFF_MM; + const float backoff_x = doX ? ABS(endstop_backoff[X_AXIS]) * (X_HOME_DIR) : 0, + backoff_y = doY ? ABS(endstop_backoff[Y_AXIS]) * (Y_HOME_DIR) : 0, + backoff_z = doZ ? ABS(endstop_backoff[Z_AXIS]) * (Z_HOME_DIR) : 0; + if (backoff_z) do_blocking_move_to_z(current_position[Z_AXIS] - backoff_z); + if (backoff_x || backoff_y) do_blocking_move_to_xy(current_position[X_AXIS] - backoff_x, current_position[Y_AXIS] - backoff_y); + #endif + endstops.not_homing(); #if BOTH(DELTA, DELTA_HOME_TO_SAFE_ZONE) @@ -430,9 +442,9 @@ void GcodeSuite::G28(const bool always_home_all) { report_current_position(); #if ENABLED(NANODLP_Z_SYNC) #if ENABLED(NANODLP_ALL_AXIS) - #define _HOME_SYNC true // For any axis, output sync text. + #define _HOME_SYNC true // For any axis, output sync text. #else - #define _HOME_SYNC (home_all || homeZ) // Only for Z-axis + #define _HOME_SYNC doZ // Only for Z-axis #endif if (_HOME_SYNC) SERIAL_ECHOLNPGM(MSG_Z_MOVE_COMP); diff --git a/Marlin/src/gcode/gcode.cpp b/Marlin/src/gcode/gcode.cpp index dc40a4a887..c8d8131770 100644 --- a/Marlin/src/gcode/gcode.cpp +++ b/Marlin/src/gcode/gcode.cpp @@ -105,7 +105,7 @@ void GcodeSuite::get_destination_from_command() { #if ENABLED(POWER_LOSS_RECOVERY) // Only update power loss recovery on moves with E - if ((seen[E_AXIS] || seen[Z_AXIS]) && IS_SD_PRINTING()) recovery.save(); + if (seen[E_AXIS] && (seen[X_AXIS] || seen[Y_AXIS]) && IS_SD_PRINTING()) recovery.save(); #endif if (parser.linearval('F') > 0) diff --git a/Marlin/src/gcode/queue.cpp b/Marlin/src/gcode/queue.cpp index 515502bdca..f448efb117 100644 --- a/Marlin/src/gcode/queue.cpp +++ b/Marlin/src/gcode/queue.cpp @@ -37,10 +37,6 @@ #include "../feature/leds/printer_event_leds.h" #endif -#if ENABLED(POWER_LOSS_RECOVERY) - #include "../feature/power_loss_recovery.h" -#endif - /** * GCode line number handling. Hosts may opt to include line numbers when * sending commands to Marlin, and lines will be checked for sequentiality. diff --git a/Marlin/src/inc/Conditionals_LCD.h b/Marlin/src/inc/Conditionals_LCD.h index 8a440d1563..5054b622dc 100644 --- a/Marlin/src/inc/Conditionals_LCD.h +++ b/Marlin/src/inc/Conditionals_LCD.h @@ -338,13 +338,6 @@ #endif #endif -// Boot screens -#if !HAS_SPI_LCD - #undef SHOW_BOOTSCREEN -#elif !defined(BOOTSCREEN_TIMEOUT) - #define BOOTSCREEN_TIMEOUT 2500 -#endif - /** * Extruders have some combination of stepper motors and hotends * so we separate these concepts into the defines: @@ -517,8 +510,11 @@ #define HAS_FILAMENT_SENSOR ENABLED(FILAMENT_RUNOUT_SENSOR) #define Z_MULTI_STEPPER_DRIVERS EITHER(Z_DUAL_STEPPER_DRIVERS, Z_TRIPLE_STEPPER_DRIVERS) -#define Z_MULTI_ENDSTOPS EITHER(Z_DUAL_ENDSTOPS, Z_TRIPLE_ENDSTOPS) -#define HAS_EXTRA_ENDSTOPS (EITHER(X_DUAL_ENDSTOPS, Y_DUAL_ENDSTOPS) || Z_MULTI_ENDSTOPS) +#define Z_MULTI_ENDSTOPS EITHER(Z_DUAL_ENDSTOPS, Z_TRIPLE_ENDSTOPS) +#define HAS_EXTRA_ENDSTOPS (EITHER(X_DUAL_ENDSTOPS, Y_DUAL_ENDSTOPS) || Z_MULTI_ENDSTOPS) + +#define HAS_GAMES ANY(MARLIN_BRICKOUT, MARLIN_INVADERS, MARLIN_SNAKE, MARLIN_MAZE) +#define HAS_GAME_MENU (1 < ENABLED(MARLIN_BRICKOUT) + ENABLED(MARLIN_INVADERS) + ENABLED(MARLIN_SNAKE) + ENABLED(MARLIN_MAZE)) #define IS_SCARA EITHER(MORGAN_SCARA, MAKERARM_SCARA) #define IS_KINEMATIC (ENABLED(DELTA) || IS_SCARA) diff --git a/Marlin/src/inc/SanityCheck.h b/Marlin/src/inc/SanityCheck.h index 2883cb7ad3..11de3fa178 100644 --- a/Marlin/src/inc/SanityCheck.h +++ b/Marlin/src/inc/SanityCheck.h @@ -551,6 +551,10 @@ static_assert(Y_MAX_LENGTH >= Y_BED_SIZE, "Movement bounds (Y_MIN_POS, Y_MAX_POS #endif #endif +#if defined(EVENT_GCODE_SD_STOP) && DISABLED(NOZZLE_PARK_FEATURE) + static_assert(NULL == strstr(EVENT_GCODE_SD_STOP, "G27"), "NOZZLE_PARK_FEATURE is required to use G27 in EVENT_GCODE_SD_STOP."); +#endif + /** * I2C Position Encoders */ diff --git a/Marlin/src/inc/Version.h b/Marlin/src/inc/Version.h index 908227d511..d724e1da28 100644 --- a/Marlin/src/inc/Version.h +++ b/Marlin/src/inc/Version.h @@ -39,7 +39,7 @@ /** * Marlin release version identifier */ - #define SHORT_BUILD_VERSION "2.0.x_TR11" + #define SHORT_BUILD_VERSION "2.0.x_TR12" /** * Verbose version identifier which should contain a reference to the location @@ -52,7 +52,7 @@ * here we define this default string as the date where the latest release * version was tagged. */ - #define STRING_DISTRIBUTION_DATE "2019-03-21" + #define STRING_DISTRIBUTION_DATE "2019-04-04" /** * Required minimum Configuration.h and Configuration_adv.h file versions. diff --git a/Marlin/src/lcd/dogm/dogm_Bootscreen.h b/Marlin/src/lcd/dogm/dogm_Bootscreen.h index 0d92483903..7361ca0d46 100644 --- a/Marlin/src/lcd/dogm/dogm_Bootscreen.h +++ b/Marlin/src/lcd/dogm/dogm_Bootscreen.h @@ -29,107 +29,99 @@ #include "../../inc/MarlinConfig.h" -#if ENABLED(SHOW_BOOTSCREEN) +//#define START_BMPHIGH // Costs 399 bytes more flash - //#define START_BMPHIGH // Costs 399 bytes more flash +#if ENABLED(SHOW_CUSTOM_BOOTSCREEN) - #if ENABLED(SHOW_CUSTOM_BOOTSCREEN) - - #include "../../../_Bootscreen.h" - - #ifndef CUSTOM_BOOTSCREEN_TIMEOUT - #define CUSTOM_BOOTSCREEN_TIMEOUT 2500 - #endif + #include "../../../_Bootscreen.h" + #ifndef CUSTOM_BOOTSCREEN_BMP_BYTEWIDTH + #define CUSTOM_BOOTSCREEN_BMP_BYTEWIDTH ((CUSTOM_BOOTSCREEN_BMPWIDTH + 7) / 8) #endif - - #if ENABLED(START_BMPHIGH) - - #define START_BMPWIDTH 112 - - const unsigned char start_bmp[] PROGMEM = { - B00000001,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111, - B00001111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111, - B00011110,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000111,B11111111,B11111111, - B00111000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000011,B11111111,B11111111, - B01110000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000001,B11111111,B11111111, - B01100000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B11111111,B11111111, - B01100000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B01111111,B11111111, - B11000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B01111000,B00000000,B00000000,B00111111,B11111111, - B11000000,B00001111,B11000000,B11111100,B00000000,B00000000,B00000000,B00000000,B00000000,B01111000,B00011000,B00000000,B00011111,B11111111, - B11000000,B00111111,B11100001,B11111111,B00000000,B00000000,B00000000,B00000000,B00000000,B01111000,B00111100,B00000000,B00001111,B11111111, - B11000000,B01111111,B11110011,B11111111,B10000000,B00000000,B00000000,B00000000,B00000000,B01111000,B00111100,B00000000,B00000111,B11111111, - B11000000,B11111111,B11111111,B11111111,B11000000,B00000000,B00000000,B00000000,B00000000,B01111000,B00111100,B00000000,B00000011,B11111111, - B11000001,B11111000,B01111111,B10000111,B11100000,B00000000,B00000000,B00000000,B00000000,B01111000,B00000000,B00000000,B00000001,B11111111, - B11000001,B11110000,B00111111,B00000011,B11100000,B00000000,B00000000,B00000000,B00000000,B01111000,B00000000,B00000000,B00000000,B11111111, - B11000001,B11100000,B00011110,B00000001,B11100000,B00011111,B00000000,B00000011,B11100000,B01111000,B00111100,B00000011,B11110000,B01111111, - B11000001,B11100000,B00011110,B00000001,B11100000,B01111111,B11000000,B00001111,B11111000,B01111000,B00111100,B00000111,B11111100,B00111111, - B11000001,B11100000,B00011110,B00000001,B11100001,B11111111,B11100000,B00011111,B11111100,B01111000,B00111100,B00001111,B11111110,B00011111, - B11000001,B11100000,B00011110,B00000001,B11100011,B11111111,B11110000,B00111111,B11111110,B01111000,B00111100,B00011111,B11111110,B00001111, - B11000001,B11100000,B00011110,B00000001,B11100011,B11110011,B11111000,B00111111,B00111110,B01111000,B00111100,B00111111,B00111111,B00000111, - B11000001,B11100000,B00011110,B00000001,B11100111,B11100000,B11111100,B01111100,B00011111,B01111000,B00111100,B00111110,B00011111,B00000111, - B11000001,B11100000,B00011110,B00000001,B11100111,B11000000,B01111100,B01111100,B00001111,B01111000,B00111100,B00111100,B00001111,B00000011, - B11000001,B11100000,B00011110,B00000001,B11100111,B10000000,B01111100,B01111000,B00001111,B01111000,B00111100,B00111100,B00001111,B00000011, - B11000001,B11100000,B00011110,B00000001,B11100111,B10000000,B00111100,B01111000,B00000000,B01111000,B00111100,B00111100,B00001111,B00000011, - B11000001,B11100000,B00011110,B00000001,B11100111,B10000000,B00111100,B01111000,B00000000,B01111000,B00111100,B00111100,B00001111,B00000011, - B11000001,B11100000,B00011110,B00000001,B11100111,B10000000,B00111100,B01111000,B00000000,B01111000,B00111100,B00111100,B00001111,B00000011, - B11000001,B11100000,B00011110,B00000001,B11100111,B11000000,B00111100,B01111000,B00000000,B01111000,B00111100,B00111100,B00001111,B00000011, - B11000001,B11100000,B00011110,B00000001,B11100011,B11100000,B00111100,B01111000,B00000000,B01111100,B00111100,B00111100,B00001111,B00000011, - B11000001,B11100000,B00011110,B00000001,B11100011,B11111111,B00111111,B11111000,B00000000,B01111111,B10111100,B00111100,B00001111,B00000011, - B11000001,B11100000,B00011110,B00000001,B11100001,B11111111,B00111111,B11111000,B00000000,B00111111,B10111111,B11111100,B00001111,B00000011, - B11000001,B11100000,B00011110,B00000001,B11100000,B11111111,B00111111,B11111000,B00000000,B00011111,B10111111,B11111100,B00001111,B00000011, - B11000001,B11100000,B00011110,B00000001,B11100000,B01111111,B00111111,B11111000,B00000000,B00001111,B10111111,B11111100,B00001111,B00000011, - B01100000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000111, - B01100000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000110, - B01110000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00001110, - B00111000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00011100, - B00011110,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B01111000, - B00001111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11110000, - B00000001,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B10000000 - }; - - #else - - #define START_BMPWIDTH 56 - - const unsigned char start_bmp[] PROGMEM = { - B00011111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111, - B01100000,B00000000,B00000000,B00000000,B00000000,B00000001,B11111111, - B01000000,B00000000,B00000000,B00000000,B00000000,B00000000,B11111111, - B10000000,B00000000,B00000000,B00000000,B00000000,B00000000,B01111111, - B10000011,B11001111,B00000000,B00000000,B00001100,B00110000,B00111111, - B10000111,B11111111,B10000000,B00000000,B00001100,B00110000,B00011111, - B10000110,B01111001,B10000000,B00000000,B00001100,B00000000,B00001111, - B10001100,B00110000,B11000111,B10000011,B10001100,B00110000,B11100111, - B10001100,B00110000,B11001111,B11000111,B11001100,B00110001,B11110011, - B10001100,B00110000,B11011100,B11101100,B11101100,B00110011,B10111001, - B10001100,B00110000,B11011000,B01101100,B01101100,B00110011,B00011001, - B10001100,B00110000,B11010000,B01101100,B00001100,B00110011,B00011001, - B10001100,B00110000,B11011000,B01101100,B00001100,B00110011,B00011001, - B10001100,B00110000,B11011100,B01101100,B00001110,B00111011,B00011001, - B10001100,B00110000,B11001111,B01111100,B00000111,B10011111,B00011001, - B10001100,B00110000,B11000111,B01111100,B00000011,B10001111,B00011001, - B01000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000010, - B01100000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000110, - B00011111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111000 - }; - + #ifndef CUSTOM_BOOTSCREEN_BMPHEIGHT + #define CUSTOM_BOOTSCREEN_BMPHEIGHT (sizeof(custom_start_bmp) / (CUSTOM_BOOTSCREEN_BMP_BYTEWIDTH)) #endif - #ifndef START_BMP_BYTEWIDTH - #define START_BMP_BYTEWIDTH ((START_BMPWIDTH + 7) / 8) - #endif - #ifndef START_BMPHEIGHT - #define START_BMPHEIGHT (sizeof(start_bmp) / (START_BMP_BYTEWIDTH)) - #endif - - static_assert(sizeof(start_bmp) == (START_BMP_BYTEWIDTH) * (START_BMPHEIGHT), "Bootscreen (start_bmp) dimensions don't match data."); - #endif -#ifndef CUSTOM_BOOTSCREEN_BMP_BYTEWIDTH - #define CUSTOM_BOOTSCREEN_BMP_BYTEWIDTH ((CUSTOM_BOOTSCREEN_BMPWIDTH + 7) / 8) +#if ENABLED(START_BMPHIGH) + + #define START_BMPWIDTH 112 + + const unsigned char start_bmp[] PROGMEM = { + B00000001,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111, + B00001111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111, + B00011110,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000111,B11111111,B11111111, + B00111000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000011,B11111111,B11111111, + B01110000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000001,B11111111,B11111111, + B01100000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B11111111,B11111111, + B01100000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B01111111,B11111111, + B11000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B01111000,B00000000,B00000000,B00111111,B11111111, + B11000000,B00001111,B11000000,B11111100,B00000000,B00000000,B00000000,B00000000,B00000000,B01111000,B00011000,B00000000,B00011111,B11111111, + B11000000,B00111111,B11100001,B11111111,B00000000,B00000000,B00000000,B00000000,B00000000,B01111000,B00111100,B00000000,B00001111,B11111111, + B11000000,B01111111,B11110011,B11111111,B10000000,B00000000,B00000000,B00000000,B00000000,B01111000,B00111100,B00000000,B00000111,B11111111, + B11000000,B11111111,B11111111,B11111111,B11000000,B00000000,B00000000,B00000000,B00000000,B01111000,B00111100,B00000000,B00000011,B11111111, + B11000001,B11111000,B01111111,B10000111,B11100000,B00000000,B00000000,B00000000,B00000000,B01111000,B00000000,B00000000,B00000001,B11111111, + B11000001,B11110000,B00111111,B00000011,B11100000,B00000000,B00000000,B00000000,B00000000,B01111000,B00000000,B00000000,B00000000,B11111111, + B11000001,B11100000,B00011110,B00000001,B11100000,B00011111,B00000000,B00000011,B11100000,B01111000,B00111100,B00000011,B11110000,B01111111, + B11000001,B11100000,B00011110,B00000001,B11100000,B01111111,B11000000,B00001111,B11111000,B01111000,B00111100,B00000111,B11111100,B00111111, + B11000001,B11100000,B00011110,B00000001,B11100001,B11111111,B11100000,B00011111,B11111100,B01111000,B00111100,B00001111,B11111110,B00011111, + B11000001,B11100000,B00011110,B00000001,B11100011,B11111111,B11110000,B00111111,B11111110,B01111000,B00111100,B00011111,B11111110,B00001111, + B11000001,B11100000,B00011110,B00000001,B11100011,B11110011,B11111000,B00111111,B00111110,B01111000,B00111100,B00111111,B00111111,B00000111, + B11000001,B11100000,B00011110,B00000001,B11100111,B11100000,B11111100,B01111100,B00011111,B01111000,B00111100,B00111110,B00011111,B00000111, + B11000001,B11100000,B00011110,B00000001,B11100111,B11000000,B01111100,B01111100,B00001111,B01111000,B00111100,B00111100,B00001111,B00000011, + B11000001,B11100000,B00011110,B00000001,B11100111,B10000000,B01111100,B01111000,B00001111,B01111000,B00111100,B00111100,B00001111,B00000011, + B11000001,B11100000,B00011110,B00000001,B11100111,B10000000,B00111100,B01111000,B00000000,B01111000,B00111100,B00111100,B00001111,B00000011, + B11000001,B11100000,B00011110,B00000001,B11100111,B10000000,B00111100,B01111000,B00000000,B01111000,B00111100,B00111100,B00001111,B00000011, + B11000001,B11100000,B00011110,B00000001,B11100111,B10000000,B00111100,B01111000,B00000000,B01111000,B00111100,B00111100,B00001111,B00000011, + B11000001,B11100000,B00011110,B00000001,B11100111,B11000000,B00111100,B01111000,B00000000,B01111000,B00111100,B00111100,B00001111,B00000011, + B11000001,B11100000,B00011110,B00000001,B11100011,B11100000,B00111100,B01111000,B00000000,B01111100,B00111100,B00111100,B00001111,B00000011, + B11000001,B11100000,B00011110,B00000001,B11100011,B11111111,B00111111,B11111000,B00000000,B01111111,B10111100,B00111100,B00001111,B00000011, + B11000001,B11100000,B00011110,B00000001,B11100001,B11111111,B00111111,B11111000,B00000000,B00111111,B10111111,B11111100,B00001111,B00000011, + B11000001,B11100000,B00011110,B00000001,B11100000,B11111111,B00111111,B11111000,B00000000,B00011111,B10111111,B11111100,B00001111,B00000011, + B11000001,B11100000,B00011110,B00000001,B11100000,B01111111,B00111111,B11111000,B00000000,B00001111,B10111111,B11111100,B00001111,B00000011, + B01100000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000111, + B01100000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000110, + B01110000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00001110, + B00111000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00011100, + B00011110,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B01111000, + B00001111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11110000, + B00000001,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B10000000 + }; + +#else + + #define START_BMPWIDTH 56 + + const unsigned char start_bmp[] PROGMEM = { + B00011111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111, + B01100000,B00000000,B00000000,B00000000,B00000000,B00000001,B11111111, + B01000000,B00000000,B00000000,B00000000,B00000000,B00000000,B11111111, + B10000000,B00000000,B00000000,B00000000,B00000000,B00000000,B01111111, + B10000011,B11001111,B00000000,B00000000,B00001100,B00110000,B00111111, + B10000111,B11111111,B10000000,B00000000,B00001100,B00110000,B00011111, + B10000110,B01111001,B10000000,B00000000,B00001100,B00000000,B00001111, + B10001100,B00110000,B11000111,B10000011,B10001100,B00110000,B11100111, + B10001100,B00110000,B11001111,B11000111,B11001100,B00110001,B11110011, + B10001100,B00110000,B11011100,B11101100,B11101100,B00110011,B10111001, + B10001100,B00110000,B11011000,B01101100,B01101100,B00110011,B00011001, + B10001100,B00110000,B11010000,B01101100,B00001100,B00110011,B00011001, + B10001100,B00110000,B11011000,B01101100,B00001100,B00110011,B00011001, + B10001100,B00110000,B11011100,B01101100,B00001110,B00111011,B00011001, + B10001100,B00110000,B11001111,B01111100,B00000111,B10011111,B00011001, + B10001100,B00110000,B11000111,B01111100,B00000011,B10001111,B00011001, + B01000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000010, + B01100000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000110, + B00011111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111000 + }; + #endif -#ifndef CUSTOM_BOOTSCREEN_BMPHEIGHT - #define CUSTOM_BOOTSCREEN_BMPHEIGHT (sizeof(custom_start_bmp) / (CUSTOM_BOOTSCREEN_BMP_BYTEWIDTH)) + +#ifndef START_BMP_BYTEWIDTH + #define START_BMP_BYTEWIDTH ((START_BMPWIDTH + 7) / 8) #endif +#ifndef START_BMPHEIGHT + #define START_BMPHEIGHT (sizeof(start_bmp) / (START_BMP_BYTEWIDTH)) +#endif + +static_assert(sizeof(start_bmp) == (START_BMP_BYTEWIDTH) * (START_BMPHEIGHT), "Bootscreen (start_bmp) dimensions don't match data."); diff --git a/Marlin/src/lcd/dogm/ultralcd_DOGM.cpp b/Marlin/src/lcd/dogm/ultralcd_DOGM.cpp index 60a74dba23..ca953f7b5d 100644 --- a/Marlin/src/lcd/dogm/ultralcd_DOGM.cpp +++ b/Marlin/src/lcd/dogm/ultralcd_DOGM.cpp @@ -41,7 +41,10 @@ #include "ultralcd_DOGM.h" #include "u8g_fontutf8.h" -#include "dogm_Bootscreen.h" + +#if ENABLED(SHOW_BOOTSCREEN) + #include "dogm_Bootscreen.h" +#endif #include "../lcdprint.h" #include "../fontutils.h" @@ -138,6 +141,9 @@ void MarlinUI::set_font(const MarlinFont font_nr) { #else draw_custom_bootscreen(custom_start_bmp); #endif + #ifndef CUSTOM_BOOTSCREEN_TIMEOUT + #define CUSTOM_BOOTSCREEN_TIMEOUT 2500 + #endif safe_delay(CUSTOM_BOOTSCREEN_TIMEOUT); } @@ -173,6 +179,9 @@ void MarlinUI::set_font(const MarlinFont font_nr) { u8g.drawStr(txt2X, height - (MENU_FONT_HEIGHT) * 1 / 2, STRING_SPLASH_LINE2); #endif } while (u8g.nextPage()); + #ifndef BOOTSCREEN_TIMEOUT + #define BOOTSCREEN_TIMEOUT 2500 + #endif safe_delay(BOOTSCREEN_TIMEOUT); } diff --git a/Marlin/src/lcd/language/language_en.h b/Marlin/src/lcd/language/language_en.h index de78f479df..862f8a1998 100644 --- a/Marlin/src/lcd/language/language_en.h +++ b/Marlin/src/lcd/language/language_en.h @@ -1277,6 +1277,18 @@ #ifndef MSG_END_Z #define MSG_END_Z _UxGT(" End Z") #endif +#ifndef MSG_BRICKOUT + #define MSG_BRICKOUT _UxGT("Brickout") +#endif +#ifndef MSG_INVADERS + #define MSG_INVADERS _UxGT("Invaders") +#endif +#ifndef MSG_SNAKE + #define MSG_SNAKE _UxGT("Sn4k3") +#endif +#ifndef MSG_MAZE + #define MSG_MAZE _UxGT("Maze") +#endif // // Filament Change screens show up to 3 lines on a 4-line display diff --git a/Marlin/src/lcd/menu/game/brickout.cpp b/Marlin/src/lcd/menu/game/brickout.cpp new file mode 100644 index 0000000000..4686072da7 --- /dev/null +++ b/Marlin/src/lcd/menu/game/brickout.cpp @@ -0,0 +1,213 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include "../../../inc/MarlinConfigPre.h" + +#if ENABLED(MARLIN_BRICKOUT) + +#include "game.h" + +#define BRICK_H 5 +#define BRICK_TOP MENU_FONT_ASCENT +#define BRICK_ROWS 4 +#define BRICK_COLS 16 + +#define PADDLE_H 2 +#define PADDLE_VEL 3 +#define PADDLE_W ((LCD_PIXEL_WIDTH) / 8) +#define PADDLE_Y (LCD_PIXEL_HEIGHT - 1 - PADDLE_H) + +#define BRICK_W ((LCD_PIXEL_WIDTH) / (BRICK_COLS)) +#define BRICK_BOT (BRICK_TOP + BRICK_H * BRICK_ROWS - 1) + +#define BRICK_COL(X) ((X) / (BRICK_W)) +#define BRICK_ROW(Y) ((Y - (BRICK_TOP)) / (BRICK_H)) + +uint8_t balls_left, brick_count; +uint16_t bricks[BRICK_ROWS]; + +inline void reset_bricks(const uint16_t v) { + brick_count = (BRICK_COLS) * (BRICK_ROWS); + LOOP_L_N(i, BRICK_ROWS) bricks[i] = v; +} + +int8_t paddle_x, hit_dir; +fixed_t ballx, bally, ballh, ballv; + +void reset_ball() { + constexpr uint8_t ball_dist = 24; + bally = BTOF(PADDLE_Y - ball_dist); + ballv = FTOP(1.3f); + ballh = -FTOP(1.25f); + uint8_t bx = paddle_x + (PADDLE_W) / 2 + ball_dist; + if (bx >= LCD_PIXEL_WIDTH - 10) { bx -= ball_dist * 2; ballh = -ballh; } + ballx = BTOF(bx); + hit_dir = -1; +} + +void BrickoutGame::game_screen() { + if (game_frame()) { // Run logic twice for finer resolution + // Update Paddle Position + paddle_x = (int8_t)ui.encoderPosition; + paddle_x = constrain(paddle_x, 0, (LCD_PIXEL_WIDTH - (PADDLE_W)) / (PADDLE_VEL)); + ui.encoderPosition = paddle_x; + paddle_x *= (PADDLE_VEL); + + // Run the ball logic + if (game_state) do { + + // Provisionally update the ball position + const fixed_t newx = ballx + ballh, newy = bally + ballv; // current next position + if (!WITHIN(newx, 0, BTOF(LCD_PIXEL_WIDTH - 1))) { // out in x? + ballh = -ballh; _BUZZ(5, 220); // bounce x + } + if (newy < 0) { // out in y? + ballv = -ballv; _BUZZ(5, 280); // bounce v + hit_dir = 1; + } + // Did the ball go below the bottom? + else if (newy > BTOF(LCD_PIXEL_HEIGHT)) { + BUZZ(500, 75); + if (--balls_left) reset_ball(); else game_state = 0; + break; // done + } + + // Is the ball colliding with a brick? + if (WITHIN(newy, BTOF(BRICK_TOP), BTOF(BRICK_BOT))) { + const int8_t bit = BRICK_COL(FTOB(newx)), row = BRICK_ROW(FTOB(newy)); + const uint16_t mask = _BV(bit); + if (bricks[row] & mask) { + // Yes. Remove it! + bricks[row] &= ~mask; + // Score! + score += BRICK_ROWS - row; + // If bricks are gone, go to reset state + if (!--brick_count) game_state = 2; + // Bounce the ball cleverly + if ((ballv < 0) == (hit_dir < 0)) { ballv = -ballv; ballh += fixed_t(random(-16, 16)); _BUZZ(5, 880); } + else { ballh = -ballh; ballv += fixed_t(random(-16, 16)); _BUZZ(5, 640); } + } + } + // Is the ball moving down and in paddle range? + else if (ballv > 0 && WITHIN(newy, BTOF(PADDLE_Y), BTOF(PADDLE_Y + PADDLE_H))) { + // Ball actually hitting paddle + const int8_t diff = FTOB(newx) - paddle_x; + if (WITHIN(diff, 0, PADDLE_W - 1)) { + + // Reverse Y direction + ballv = -ballv; _BUZZ(3, 880); + hit_dir = -1; + + // Near edges affects X velocity + const bool is_left_edge = (diff <= 1); + if (is_left_edge || diff >= PADDLE_W-1 - 1) { + if ((ballh > 0) == is_left_edge) ballh = -ballh; + } + else if (diff <= 3) { + ballh += fixed_t(random(-64, 0)); + NOLESS(ballh, BTOF(-2)); + NOMORE(ballh, BTOF(2)); + } + else if (diff >= PADDLE_W-1 - 3) { + ballh += fixed_t(random( 0, 64)); + NOLESS(ballh, BTOF(-2)); + NOMORE(ballh, BTOF(2)); + } + + // Paddle hit after clearing the board? Reset the board. + if (game_state == 2) { reset_bricks(0xFFFF); game_state = 1; } + } + } + + ballx += ballh; bally += ballv; // update with new velocity + + } while (false); + } + + u8g.setColorIndex(1); + + // Draw bricks + if (PAGE_CONTAINS(BRICK_TOP, BRICK_BOT)) { + for (uint8_t y = 0; y < BRICK_ROWS; ++y) { + const uint8_t yy = y * BRICK_H + BRICK_TOP; + if (PAGE_CONTAINS(yy, yy + BRICK_H - 1)) { + for (uint8_t x = 0; x < BRICK_COLS; ++x) { + if (TEST(bricks[y], x)) { + const uint8_t xx = x * BRICK_W; + for (uint8_t v = 0; v < BRICK_H - 1; ++v) + if (PAGE_CONTAINS(yy + v, yy + v)) + u8g.drawHLine(xx, yy + v, BRICK_W - 1); + } + } + } + } + } + + // Draw paddle + if (PAGE_CONTAINS(PADDLE_Y-1, PADDLE_Y)) { + u8g.drawHLine(paddle_x, PADDLE_Y, PADDLE_W); + #if PADDLE_H > 1 + u8g.drawHLine(paddle_x, PADDLE_Y-1, PADDLE_W); + #if PADDLE_H > 2 + u8g.drawHLine(paddle_x, PADDLE_Y-2, PADDLE_W); + #endif + #endif + } + + // Draw ball while game is running + if (game_state) { + const uint8_t by = FTOB(bally); + if (PAGE_CONTAINS(by, by+1)) + u8g.drawFrame(FTOB(ballx), by, 2, 2); + } + // Or draw GAME OVER + else + draw_game_over(); + + if (PAGE_UNDER(MENU_FONT_ASCENT)) { + // Score Digits + //const uint8_t sx = (LCD_PIXEL_WIDTH - (score >= 10 ? score >= 100 ? score >= 1000 ? 4 : 3 : 2 : 1) * MENU_FONT_WIDTH) / 2; + constexpr uint8_t sx = 0; + lcd_moveto(sx, MENU_FONT_ASCENT - 1); + lcd_put_int(score); + + // Balls Left + lcd_moveto(LCD_PIXEL_WIDTH - MENU_FONT_WIDTH * 3, MENU_FONT_ASCENT - 1); + PGM_P const ohs = PSTR("ooo\0\0"); + lcd_put_u8str_P(ohs + 3 - balls_left); + } + + // A click always exits this game + if (ui.use_click()) exit_game(); +} + +void BrickoutGame::enter_game() { + init_game(2, game_screen); // 2 = reset bricks on paddle hit + constexpr uint8_t paddle_start = SCREEN_M - (PADDLE_W) / 2; + paddle_x = paddle_start; + balls_left = 3; + reset_bricks(0x0000); + reset_ball(); + ui.encoderPosition = paddle_start / (PADDLE_VEL); +} + +#endif // MARLIN_BRICKOUT diff --git a/Marlin/src/lcd/menu/game/game.cpp b/Marlin/src/lcd/menu/game/game.cpp new file mode 100644 index 0000000000..1b1d838a8e --- /dev/null +++ b/Marlin/src/lcd/menu/game/game.cpp @@ -0,0 +1,69 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include "../../../inc/MarlinConfigPre.h" + +#if HAS_GAMES + +#include "game.h" + +int MarlinGame::score; +uint8_t MarlinGame::game_state; +millis_t MarlinGame::next_frame; + +bool MarlinGame::game_frame() { + static int8_t slew; + if (ui.first_page) slew = 2; + ui.refresh(LCDVIEW_CALL_NO_REDRAW); // Refresh as often as possible + return (game_state && slew-- > 0); +} + +void MarlinGame::draw_game_over() { + constexpr int8_t gowide = (MENU_FONT_WIDTH) * 9, + gohigh = MENU_FONT_ASCENT - 3, + lx = (LCD_PIXEL_WIDTH - gowide) / 2, + ly = (LCD_PIXEL_HEIGHT + gohigh) / 2; + if (PAGE_CONTAINS(ly - gohigh - 1, ly + 1)) { + u8g.setColorIndex(0); + u8g.drawBox(lx - 1, ly - gohigh - 1, gowide + 2, gohigh + 2); + u8g.setColorIndex(1); + if (ui.get_blink()) { + lcd_moveto(lx, ly); + lcd_put_u8str_P(PSTR("GAME OVER")); + } + } +} + +void MarlinGame::init_game(const uint8_t init_state, const screenFunc_t screen) { + score = 0; + game_state = init_state; + ui.encoder_direction_normal(); + ui.goto_screen(screen); + ui.defer_status_screen(); +} + +void MarlinGame::exit_game() { + ui.goto_previous_screen(); + ui.defer_status_screen(false); +} + +#endif // HAS_GAMES diff --git a/Marlin/src/lcd/menu/game/game.h b/Marlin/src/lcd/menu/game/game.h new file mode 100644 index 0000000000..a4cfda56d6 --- /dev/null +++ b/Marlin/src/lcd/menu/game/game.h @@ -0,0 +1,78 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +#pragma once + +#include "../../../inc/MarlinConfigPre.h" +#include "../../dogm/ultralcd_DOGM.h" +#include "../../lcdprint.h" +#include "../../ultralcd.h" + +//#define MUTE_GAMES + +#ifdef MUTE_GAMES + #define _BUZZ(D,F) NOOP +#else + #define _BUZZ(D,F) BUZZ(D,F) +#endif + +// Simple 8:8 fixed-point +typedef int16_t fixed_t; +#define FTOP(F) fixed_t((F)*256.0f) +#define PTOF(P) (float(P)*(1.0f/256.0f)) +#define BTOF(X) (fixed_t(X)<<8) +#define FTOB(X) int8_t(fixed_t(X)>>8) + +#define SCREEN_M ((LCD_PIXEL_WIDTH) / 2) + +#if HAS_GAME_MENU + void menu_game(); +#endif + +class MarlinGame { +protected: + static int score; + static uint8_t game_state; + static millis_t next_frame; + + static bool game_frame(); + static void draw_game_over(); + static void exit_game(); +public: + static void init_game(const uint8_t init_state, const screenFunc_t screen); +}; + +#if ENABLED(MARLIN_BRICKOUT) + class BrickoutGame : MarlinGame { public: static void enter_game(); static void game_screen(); }; + extern BrickoutGame brickout; +#endif +#if ENABLED(MARLIN_INVADERS) + class InvadersGame : MarlinGame { public: static void enter_game(); static void game_screen(); }; + extern InvadersGame invaders; +#endif +#if ENABLED(MARLIN_SNAKE) + class SnakeGame : MarlinGame { public: static void enter_game(); static void game_screen(); }; + extern SnakeGame snake; +#endif +#if ENABLED(MARLIN_MAZE) + class MazeGame : MarlinGame { public: static void enter_game(); static void game_screen(); }; + extern MazeGame maze; +#endif diff --git a/Marlin/src/lcd/menu/game/invaders.cpp b/Marlin/src/lcd/menu/game/invaders.cpp new file mode 100644 index 0000000000..4bb90f3565 --- /dev/null +++ b/Marlin/src/lcd/menu/game/invaders.cpp @@ -0,0 +1,466 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include "../../../inc/MarlinConfigPre.h" + +#if ENABLED(MARLIN_INVADERS) + +#include "game.h" + +// 11x8 +const unsigned char invader[3][2][16] PROGMEM = { + { { B00000110,B00000000, + B00001111,B00000000, + B00011111,B10000000, + B00110110,B11000000, + B00111111,B11000000, + B00001001,B00000000, + B00010110,B10000000, + B00101001,B01000000 + }, { + B00000110,B00000000, + B00001111,B00000000, + B00011111,B10000000, + B00110110,B11000000, + B00111111,B11000000, + B00010110,B10000000, + B00100000,B01000000, + B00010000,B10000000 + } + }, { + { B00010000,B01000000, + B00001000,B10000000, + B00011111,B11000000, + B00110111,B01100000, + B01111111,B11110000, + B01011111,B11010000, + B01010000,B01010000, + B00001101,B10000000 + }, { + B00010000,B01000000, + B01001000,B10010000, + B01011111,B11010000, + B01110111,B01110000, + B01111111,B11110000, + B00011111,B11000000, + B00010000,B01000000, + B00100000,B00100000 + } + }, { + { B00001111,B00000000, + B01111111,B11100000, + B11111111,B11110000, + B11100110,B01110000, + B11111111,B11110000, + B00011001,B10000000, + B00110110,B11000000, + B11000000,B00110000 + }, { + B00001111,B00000000, + B01111111,B11100000, + B11111111,B11110000, + B11100110,B01110000, + B11111111,B11110000, + B00011001,B10000000, + B00110110,B11000000, + B00011001,B10000000 + } + } +}; +const unsigned char cannon[] PROGMEM = { + B00000100,B00000000, + B00001110,B00000000, + B00001110,B00000000, + B01111111,B11000000, + B11111111,B11100000, + B11111111,B11100000, + B11111111,B11100000, + B11111111,B11100000 +}; +const unsigned char life[] PROGMEM = { + B00010000, + B01111100, + B11111110, + B11111110, + B11111110 +}; +const unsigned char explosion[] PROGMEM = { + B01000100,B01000000, + B00100100,B10000000, + B00000000,B00000000, + B00110001,B10000000, + B00000000,B00000000, + B00100100,B10000000, + B01000100,B01000000 +}; +const unsigned char ufo[] PROGMEM = { + B00011111,B11000000, + B01111111,B11110000, + B11011101,B11011000, + B11111111,B11111000, + B01111111,B11110000 +}; + +#define INVASION_SIZE 3 + +#if INVASION_SIZE == 3 + #define INVADER_COLS 5 +#elif INVASION_SIZE == 4 + #define INVADER_COLS 6 +#else + #define INVADER_COLS 8 + #undef INVASION_SIZE + #define INVASION_SIZE 5 +#endif + +#define INVADER_ROWS INVASION_SIZE + +constexpr uint8_t inv_type[] = { + #if INVADER_ROWS == 5 + 0, 1, 1, 2, 2 + #elif INVADER_ROWS == 4 + 0, 1, 1, 2 + #elif INVADER_ROWS == 3 + 0, 1, 2 + #else + #error "INVASION_SIZE must be 3, 4, or 5." + #endif +}; + +#define INVADER_RIGHT ((INVADER_COLS) * (COL_W)) + +#define CANNON_W 11 +#define CANNON_H 8 +#define CANNON_VEL 4 +#define CANNON_Y (LCD_PIXEL_HEIGHT - 1 - CANNON_H) + +#define COL_W 14 +#define INVADER_H 8 +#define ROW_H (INVADER_H + 2) +#define INVADER_VEL 3 + +#define INVADER_TOP MENU_FONT_ASCENT +#define INVADERS_WIDE ((COL_W) * (INVADER_COLS)) +#define INVADERS_HIGH ((ROW_H) * (INVADER_ROWS)) + +#define UFO_H 5 +#define UFO_W 13 + +#define LASER_H 4 +#define SHOT_H 3 +#define EXPL_W 11 +#define LIFE_W 8 +#define LIFE_H 5 + +#define INVADER_COL(X) ((X - invaders_x) / (COL_W)) +#define INVADER_ROW(Y) ((Y - invaders_y + 2) / (ROW_H)) + +#define INV_X_LEFT(C,T) (invaders_x + (C) * (COL_W) + inv_off[T]) +#define INV_X_CTR(C,T) (INV_X_LEFT(C,T) + inv_wide[T] / 2) +#define INV_Y_BOT(R) (invaders_y + (R + 1) * (ROW_H) - 2) + +typedef struct { int8_t x, y, v; } laser_t; + +uint8_t cannons_left; +int8_t cannon_x; +laser_t laser, expl, bullet[10]; +constexpr uint8_t inv_off[] = { 2, 1, 0 }, inv_wide[] = { 8, 11, 12 }; +int8_t invaders_x, invaders_y, invaders_dir, leftmost, rightmost, botmost; +uint8_t invader_count, quit_count, bugs[INVADER_ROWS], shooters[(INVADER_ROWS) * (INVADER_COLS)]; + +inline void update_invader_data() { + uint8_t inv_mask = 0; + // Get a list of all active invaders + uint8_t sc = 0; + LOOP_L_N(y, INVADER_ROWS) { + uint8_t m = bugs[y]; + if (m) botmost = y + 1; + inv_mask |= m; + for (uint8_t x = 0; x < INVADER_COLS; ++x) + if (TEST(m, x)) shooters[sc++] = (y << 4) | x; + } + leftmost = 0; + LOOP_L_N(i, INVADER_COLS) { if (TEST(inv_mask, i)) break; leftmost -= COL_W; } + rightmost = LCD_PIXEL_WIDTH - (INVADERS_WIDE); + for (uint8_t i = INVADER_COLS; i--;) { if (TEST(inv_mask, i)) break; rightmost += COL_W; } + if (invader_count == 2) invaders_dir = invaders_dir > 0 ? INVADER_VEL + 1 : -(INVADER_VEL + 1); +} + +inline void reset_bullets() { + LOOP_L_N(i, COUNT(bullet)) bullet[i].v = 0; +} + +inline void reset_invaders() { + invaders_x = 0; invaders_y = INVADER_TOP; + invaders_dir = INVADER_VEL; + invader_count = (INVADER_COLS) * (INVADER_ROWS); + LOOP_L_N(i, INVADER_ROWS) bugs[i] = _BV(INVADER_COLS) - 1; + update_invader_data(); + reset_bullets(); +} + +int8_t ufox, ufov; +inline void spawn_ufo() { + ufov = random(0, 2) ? 1 : -1; + ufox = ufov > 0 ? -(UFO_W) : LCD_PIXEL_WIDTH - 1; +} + +inline void reset_player() { + cannon_x = 0; + ui.encoderPosition = 0; +} + +inline void fire_cannon() { + laser.x = cannon_x + CANNON_W / 2; + laser.y = LCD_PIXEL_HEIGHT - CANNON_H - (LASER_H); + laser.v = -(LASER_H); +} + +inline void explode(const int8_t x, const int8_t y, const int8_t v=4) { + expl.x = x - (EXPL_W) / 2; expl.y = y; expl.v = v; +} + +inline void kill_cannon(uint8_t &game_state, const uint8_t st) { + reset_bullets(); + explode(cannon_x + (CANNON_W) / 2, CANNON_Y, 6); + _BUZZ(1000, 10); + if (--cannons_left) { + laser.v = 0; + game_state = st; + reset_player(); + } + else + game_state = 0; +} + +void InvadersGame::game_screen() { + static bool game_blink; + + ui.refresh(LCDVIEW_CALL_NO_REDRAW); // Call as often as possible + + // Run game logic once per full screen + if (ui.first_page) { + + // Update Cannon Position + int32_t ep = (int32_t)ui.encoderPosition; + ep = constrain(ep, 0, (LCD_PIXEL_WIDTH - (CANNON_W)) / (CANNON_VEL)); + ui.encoderPosition = ep; + + ep *= (CANNON_VEL); + if (ep > cannon_x) { cannon_x += CANNON_VEL - 1; if (ep - cannon_x < 2) cannon_x = ep; } + if (ep < cannon_x) { cannon_x -= CANNON_VEL - 1; if (cannon_x - ep < 2) cannon_x = ep; } + + // Run the game logic + if (game_state) do { + + // Move the UFO, if any + if (ufov) { ufox += ufov; if (!WITHIN(ufox, -(UFO_W), LCD_PIXEL_WIDTH - 1)) ufov = 0; } + + if (game_state > 1) { if (--game_state == 2) { reset_invaders(); } else if (game_state == 100) { game_state = 1; } break; } + + static uint8_t blink_count; + const bool did_blink = (++blink_count > invader_count >> 1); + if (did_blink) { + game_blink = !game_blink; + blink_count = 0; + } + + if (invader_count && did_blink) { + const int8_t newx = invaders_x + invaders_dir; + if (!WITHIN(newx, leftmost, rightmost)) { // Invaders reached the edge? + invaders_dir *= -1; // Invaders change direction + invaders_y += (ROW_H) / 2; // Invaders move down + invaders_x -= invaders_dir; // ...and only move down this time. + if (invaders_y + botmost * (ROW_H) - 2 >= CANNON_Y) // Invaders reached the bottom? + kill_cannon(game_state, 20); // Kill the cannon. Reset invaders. + } + + invaders_x += invaders_dir; // Invaders take one step left/right + + // Randomly shoot if invaders are listed + if (invader_count && !random(0, 20)) { + + // Find a free bullet + laser_t *b = NULL; + LOOP_L_N(i, COUNT(bullet)) if (!bullet[i].v) { b = &bullet[i]; break; } + if (b) { + // Pick a random shooter and update the bullet + //SERIAL_ECHOLNPGM("free bullet found"); + const uint8_t inv = shooters[random(0, invader_count + 1)], col = inv & 0x0F, row = inv >> 4, type = inv_type[row]; + b->x = INV_X_CTR(col, type); + b->y = INV_Y_BOT(row); + b->v = 2 + random(0, 2); + } + } + } + + // Update the laser position + if (laser.v) { + laser.y += laser.v; + if (laser.y < 0) laser.v = 0; + } + + // Did the laser collide with an invader? + if (laser.v && WITHIN(laser.y, invaders_y, invaders_y + INVADERS_HIGH - 1)) { + const int8_t col = INVADER_COL(laser.x); + if (WITHIN(col, 0, INVADER_COLS - 1)) { + const int8_t row = INVADER_ROW(laser.y); + if (WITHIN(row, 0, INVADER_ROWS - 1)) { + const uint8_t mask = _BV(col); + if (bugs[row] & mask) { + const uint8_t type = inv_type[row]; + const int8_t invx = INV_X_LEFT(col, type); + if (WITHIN(laser.x, invx, invx + inv_wide[type] - 1)) { + // Turn off laser + laser.v = 0; + // Remove the invader! + bugs[row] &= ~mask; + // Score! + score += INVADER_ROWS - row; + // Explode sound! + _BUZZ(40, 10); + // Explosion bitmap! + explode(invx + inv_wide[type] / 2, invaders_y + row * (ROW_H)); + // If invaders are gone, go to reset invaders state + if (--invader_count) update_invader_data(); else { game_state = 20; reset_bullets(); } + } // laser x hit + } // invader exists + } // good row + } // good col + } // laser in invader zone + + // Handle alien bullets + LOOP_L_N(s, COUNT(bullet)) { + laser_t *b = &bullet[s]; + if (b->v) { + // Update alien bullet position + b->y += b->v; + if (b->y >= LCD_PIXEL_HEIGHT) + b->v = 0; // Offscreen + else if (b->y >= CANNON_Y && WITHIN(b->x, cannon_x, cannon_x + CANNON_W - 1)) + kill_cannon(game_state, 120); // Hit the cannon + } + } + + // Randomly spawn a UFO + if (!ufov && !random(0,500)) spawn_ufo(); + + // Did the laser hit a ufo? + if (laser.v && ufov && laser.y < UFO_H + 2 && WITHIN(laser.x, ufox, ufox + UFO_W - 1)) { + // Turn off laser and UFO + laser.v = ufov = 0; + // Score! + score += 10; + // Explode! + _BUZZ(40, 10); + // Explosion bitmap + explode(ufox + (UFO_W) / 2, 1); + } + + } while (false); + + } + + // Click-and-hold to abort + if (ui.button_pressed()) --quit_count; else quit_count = 10; + + // Click to fire or exit + if (ui.use_click()) { + if (!game_state) + quit_count = 0; + else if (game_state == 1 && !laser.v) + fire_cannon(); + } + + if (!quit_count) exit_game(); + + u8g.setColorIndex(1); + + // Draw invaders + if (PAGE_CONTAINS(invaders_y, invaders_y + botmost * (ROW_H) - 2 - 1)) { + int8_t yy = invaders_y; + for (uint8_t y = 0; y < INVADER_ROWS; ++y) { + const uint8_t type = inv_type[y]; + if (PAGE_CONTAINS(yy, yy + INVADER_H - 1)) { + int8_t xx = invaders_x; + for (uint8_t x = 0; x < INVADER_COLS; ++x) { + if (TEST(bugs[y], x)) + u8g.drawBitmapP(xx, yy, 2, INVADER_H, invader[type][game_blink]); + xx += COL_W; + } + } + yy += ROW_H; + } + } + + // Draw UFO + if (ufov && PAGE_UNDER(UFO_H + 2)) + u8g.drawBitmapP(ufox, 2, 2, UFO_H, ufo); + + // Draw cannon + if (game_state && PAGE_CONTAINS(CANNON_Y, CANNON_Y + CANNON_H - 1) && (game_state < 2 || (game_state & 0x02))) + u8g.drawBitmapP(cannon_x, CANNON_Y, 2, CANNON_H, cannon); + + // Draw laser + if (laser.v && PAGE_CONTAINS(laser.y, laser.y + LASER_H - 1)) + u8g.drawVLine(laser.x, laser.y, LASER_H); + + // Draw invader bullets + LOOP_L_N (i, COUNT(bullet)) { + if (bullet[i].v && PAGE_CONTAINS(bullet[i].y - (SHOT_H - 1), bullet[i].y)) + u8g.drawVLine(bullet[i].x, bullet[i].y - (SHOT_H - 1), SHOT_H); + } + + // Draw explosion + if (expl.v && PAGE_CONTAINS(expl.y, expl.y + 7 - 1)) { + u8g.drawBitmapP(expl.x, expl.y, 2, 7, explosion); + --expl.v; + } + + // Blink GAME OVER when game is over + if (!game_state) draw_game_over(); + + if (PAGE_UNDER(MENU_FONT_ASCENT - 1)) { + // Draw Score + //const uint8_t sx = (LCD_PIXEL_WIDTH - (score >= 10 ? score >= 100 ? score >= 1000 ? 4 : 3 : 2 : 1) * MENU_FONT_WIDTH) / 2; + constexpr uint8_t sx = 0; + lcd_moveto(sx, MENU_FONT_ASCENT - 1); + lcd_put_int(score); + + // Draw lives + if (cannons_left) + for (uint8_t i = 1; i <= cannons_left; ++i) + u8g.drawBitmapP(LCD_PIXEL_WIDTH - i * (LIFE_W), 6 - (LIFE_H), 1, LIFE_H, life); + } + +} + +void InvadersGame::enter_game() { + init_game(20, game_screen); // countdown to reset invaders + cannons_left = 3; + quit_count = 10; + laser.v = 0; + reset_invaders(); + reset_player(); +} + +#endif // MARLIN_INVADERS diff --git a/Marlin/src/lcd/menu/game/maze.cpp b/Marlin/src/lcd/menu/game/maze.cpp new file mode 100644 index 0000000000..c84c504537 --- /dev/null +++ b/Marlin/src/lcd/menu/game/maze.cpp @@ -0,0 +1,137 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include "../../../inc/MarlinConfigPre.h" + +#if ENABLED(MARLIN_MAZE) + +#include "game.h" + +int8_t move_dir, last_move_dir, // NESW0 + prizex, prizey, prize_cnt, old_encoder; +fixed_t playerx, playery; + +// Up to 50 lines, then you win! +typedef struct { int8_t x, y; } pos_t; +uint8_t head_ind; +pos_t maze_walls[50] = { + { 0, 0 } +}; + +// Turn the player cw or ccw +inline void turn_player(const bool cw) { + if (move_dir == 4) move_dir = last_move_dir; + move_dir += cw ? 1 : -1; + move_dir &= 0x03; + last_move_dir = move_dir; +} + +// Reset the player for a new game +void player_reset() { + // Init position + playerx = BTOF(1); + playery = BTOF(GAME_H / 2); + + // Init motion with a ccw turn + move_dir = 0; + turn_player(false); + + // Clear prize flag + prize_cnt = 255; + + // Clear the controls + ui.encoderPosition = 0; + old_encoder = 0; +} + +void MazeGame::game_screen() { + // Run the sprite logic + if (game_frame()) do { // Run logic twice for finer resolution + + // Move the man one unit in the current direction + // Direction index 4 is for the stopped man + const int8_t oldx = FTOB(playerx), oldy = FTOB(playery); + pos_t dir_add[] = { { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, 0 } }; + playerx += dir_add[move_dir].x; + playery += dir_add[move_dir].y; + const int8_t x = FTOB(playerx), y = FTOB(playery); + + } while(0); + + u8g.setColorIndex(1); + + // Draw Score + if (PAGE_UNDER(HEADER_H)) { + lcd_moveto(0, HEADER_H - 1); + lcd_put_int(score); + } + + // Draw the maze + // for (uint8_t n = 0; n < head_ind; ++n) { + // const pos_t &p = maze_walls[n], &q = maze_walls[n + 1]; + // if (p.x == q.x) { + // const int8_t y1 = GAMEY(MIN(p.y, q.y)), y2 = GAMEY(MAX(p.y, q.y)); + // if (PAGE_CONTAINS(y1, y2)) + // u8g.drawVLine(GAMEX(p.x), y1, y2 - y1 + 1); + // } + // else if (PAGE_CONTAINS(GAMEY(p.y), GAMEY(p.y))) { + // const int8_t x1 = GAMEX(MIN(p.x, q.x)), x2 = GAMEX(MAX(p.x, q.x)); + // u8g.drawHLine(x1, GAMEY(p.y), x2 - x1 + 1); + // } + // } + + // Draw Man + // const int8_t fy = GAMEY(foody); + // if (PAGE_CONTAINS(fy, fy + FOOD_WH - 1)) { + // const int8_t fx = GAMEX(foodx); + // u8g.drawFrame(fx, fy, FOOD_WH, FOOD_WH); + // if (FOOD_WH == 5) u8g.drawPixel(fx + 2, fy + 2); + // } + + // Draw Ghosts + // const int8_t fy = GAMEY(foody); + // if (PAGE_CONTAINS(fy, fy + FOOD_WH - 1)) { + // const int8_t fx = GAMEX(foodx); + // u8g.drawFrame(fx, fy, FOOD_WH, FOOD_WH); + // if (FOOD_WH == 5) u8g.drawPixel(fx + 2, fy + 2); + // } + + // Draw Prize + // if (PAGE_CONTAINS(prizey, prizey + PRIZE_WH - 1)) { + // u8g.drawFrame(prizex, prizey, PRIZE_WH, PRIZE_WH); + // if (PRIZE_WH == 5) u8g.drawPixel(prizex + 2, prizey + 2); + // } + + // Draw GAME OVER + if (!game_state) draw_game_over(); + + // A click always exits this game + if (ui.use_click()) exit_game(); +} + +void MazeGame::enter_game() { + init_game(1, game_screen); // Game running + reset_player(); + reset_enemies(); +} + +#endif // MARLIN_MAZE diff --git a/Marlin/src/lcd/menu/game/snake.cpp b/Marlin/src/lcd/menu/game/snake.cpp new file mode 100644 index 0000000000..d34f6f4b11 --- /dev/null +++ b/Marlin/src/lcd/menu/game/snake.cpp @@ -0,0 +1,334 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include "../../../inc/MarlinConfigPre.h" + +#if ENABLED(MARLIN_SNAKE) + +#include "game.h" + +#define SNAKE_BOX 4 + +#define HEADER_H (MENU_FONT_ASCENT - 2) +#define SNAKE_WH (SNAKE_BOX + 1) + +#define IDEAL_L 2 +#define IDEAL_R (LCD_PIXEL_WIDTH - 1 - 2) +#define IDEAL_T (HEADER_H + 2) +#define IDEAL_B (LCD_PIXEL_HEIGHT - 1 - 2) +#define IDEAL_W (IDEAL_R - (IDEAL_L) + 1) +#define IDEAL_H (IDEAL_B - (IDEAL_T) + 1) + +#define GAME_W int((IDEAL_W) / (SNAKE_WH)) +#define GAME_H int((IDEAL_H) / (SNAKE_WH)) + +#define BOARD_W ((SNAKE_WH) * (GAME_W) + 1) +#define BOARD_H ((SNAKE_WH) * (GAME_H) + 1) +#define BOARD_L ((LCD_PIXEL_WIDTH - (BOARD_W) + 1) / 2) +#define BOARD_R (BOARD_L + BOARD_W - 1) +#define BOARD_T (((LCD_PIXEL_HEIGHT + IDEAL_T) - (BOARD_H)) / 2) +#define BOARD_B (BOARD_T + BOARD_H - 1) + +#define GAMEX(X) (BOARD_L + ((X) * (SNAKE_WH))) +#define GAMEY(Y) (BOARD_T + ((Y) * (SNAKE_WH))) + +#if SNAKE_BOX > 2 + #define FOOD_WH SNAKE_BOX +#else + #define FOOD_WH 2 +#endif + +#if SNAKE_BOX < 1 + #define SNAKE_SIZ 1 +#else + #define SNAKE_SIZ SNAKE_BOX +#endif + +constexpr fixed_t snakev = FTOP(0.20); + +int8_t snake_dir, // NESW + foodx, foody, food_cnt, + old_encoder; +fixed_t snakex, snakey; + +// Up to 50 lines, then you win! +typedef struct { int8_t x, y; } pos_t; +uint8_t head_ind; +pos_t snake_tail[50]; + +// Remove the first pixel from the tail. +// If needed, shift out the first segment. +void shorten_tail() { + pos_t &p = snake_tail[0], &q = snake_tail[1]; + bool shift = false; + if (p.x == q.x) { + // Vertical line + p.y += (q.y > p.y) ? 1 : -1; + shift = p.y == q.y; + } + else { + // Horizontal line + p.x += (q.x > p.x) ? 1 : -1; + shift = p.x == q.x; + } + if (shift) { + head_ind--; + for (uint8_t i = 0; i <= head_ind; ++i) + snake_tail[i] = snake_tail[i + 1]; + } +} + +// The food is on a line +inline bool food_on_line() { + for (uint8_t n = 0; n < head_ind; ++n) { + pos_t &p = snake_tail[n], &q = snake_tail[n + 1]; + if (p.x == q.x) { + if ((foodx == p.x - 1 || foodx == p.x) && WITHIN(foody, MIN(p.y, q.y), MAX(p.y, q.y))) + return true; + } + else if ((foody == p.y - 1 || foody == p.y) && WITHIN(foodx, MIN(p.x, q.x), MAX(p.x, q.x))) + return true; + } + return false; +} + +// Add a new food blob +void food_reset() { + do { + foodx = random(0, GAME_W); + foody = random(0, GAME_H); + } while (food_on_line()); +} + +// Turn the snake cw or ccw +inline void turn_snake(const bool cw) { + snake_dir += cw ? 1 : -1; + snake_dir &= 0x03; + head_ind++; + snake_tail[head_ind].x = FTOB(snakex); + snake_tail[head_ind].y = FTOB(snakey); +} + +// Reset the snake for a new game +void snake_reset() { + // Init the head and velocity + snakex = BTOF(1); + snakey = BTOF(GAME_H / 2); + //snakev = FTOP(0.25); + + // Init the tail with a cw turn + snake_dir = 0; + head_ind = 0; + snake_tail[0].x = 0; + snake_tail[0].y = GAME_H / 2; + turn_snake(true); + + // Clear food flag + food_cnt = 5; + + // Clear the controls + ui.encoderPosition = 0; + old_encoder = 0; +} + +// Check if head segment overlaps another +bool snake_overlap() { + // 4 lines must exist before a collision is possible + if (head_ind < 4) return false; + // Is the last segment crossing any others? + const pos_t &h1 = snake_tail[head_ind - 1], &h2 = snake_tail[head_ind]; + // VERTICAL head segment? + if (h1.x == h2.x) { + // Loop from oldest to segment two away from head + for (uint8_t n = 0; n < head_ind - 2; ++n) { + // Segment p to q + const pos_t &p = snake_tail[n], &q = snake_tail[n + 1]; + if (p.x != q.x) { + // Crossing horizontal segment + if (WITHIN(h1.x, MIN(p.x, q.x), MAX(p.x, q.x)) && (h1.y <= p.y) == (h2.y >= p.y)) return true; + } // Overlapping vertical segment + else if (h1.x == p.x && MIN(h1.y, h2.y) <= MAX(p.y, q.y) && MAX(h1.y, h2.y) >= MIN(p.y, q.y)) return true; + } + } + else { + // Loop from oldest to segment two away from head + for (uint8_t n = 0; n < head_ind - 2; ++n) { + // Segment p to q + const pos_t &p = snake_tail[n], &q = snake_tail[n + 1]; + if (p.y != q.y) { + // Crossing vertical segment + if (WITHIN(h1.y, MIN(p.y, q.y), MAX(p.y, q.y)) && (h1.x <= p.x) == (h2.x >= p.x)) return true; + } // Overlapping horizontal segment + else if (h1.y == p.y && MIN(h1.x, h2.x) <= MAX(p.x, q.x) && MAX(h1.x, h2.x) >= MIN(p.x, q.x)) return true; + } + } + return false; +} + +void SnakeGame::game_screen() { + // Run the snake logic + if (game_frame()) do { // Run logic twice for finer resolution + + // Move the snake's head one unit in the current direction + const int8_t oldx = FTOB(snakex), oldy = FTOB(snakey); + switch (snake_dir) { + case 0: snakey -= snakev; break; + case 1: snakex += snakev; break; + case 2: snakey += snakev; break; + case 3: snakex -= snakev; break; + } + const int8_t x = FTOB(snakex), y = FTOB(snakey); + + // If movement took place... + if (oldx != x || oldy != y) { + + if (!WITHIN(x, 0, GAME_W - 1) || !WITHIN(y, 0, GAME_H - 1)) { + game_state = 0; // Game Over + _BUZZ(400, 40); // Bzzzt! + break; // ...out of do-while + } + + snake_tail[head_ind].x = x; + snake_tail[head_ind].y = y; + + // Change snake direction if set + const int8_t enc = int8_t(ui.encoderPosition), diff = enc - old_encoder; + if (diff) { + old_encoder = enc; + turn_snake(diff > 0); + } + + if (food_cnt) --food_cnt; else shorten_tail(); + + // Did the snake collide with itself or go out of bounds? + if (snake_overlap()) { + game_state = 0; // Game Over + _BUZZ(400, 40); // Bzzzt! + } + // Is the snake at the food? + else if (x == foodx && y == foody) { + _BUZZ(5, 220); + _BUZZ(5, 280); + score++; + food_cnt = 2; + food_reset(); + } + } + + } while(0); + + u8g.setColorIndex(1); + + // Draw Score + if (PAGE_UNDER(HEADER_H)) { + lcd_moveto(0, HEADER_H - 1); + lcd_put_int(score); + } + + // DRAW THE PLAYFIELD BORDER + u8g.drawFrame(BOARD_L - 2, BOARD_T - 2, BOARD_R - BOARD_L + 4, BOARD_B - BOARD_T + 4); + + // Draw the snake (tail) + #if SNAKE_WH < 2 + + // At this scale just draw a line + for (uint8_t n = 0; n < head_ind; ++n) { + const pos_t &p = snake_tail[n], &q = snake_tail[n + 1]; + if (p.x == q.x) { + const int8_t y1 = GAMEY(MIN(p.y, q.y)), y2 = GAMEY(MAX(p.y, q.y)); + if (PAGE_CONTAINS(y1, y2)) + u8g.drawVLine(GAMEX(p.x), y1, y2 - y1 + 1); + } + else if (PAGE_CONTAINS(GAMEY(p.y), GAMEY(p.y))) { + const int8_t x1 = GAMEX(MIN(p.x, q.x)), x2 = GAMEX(MAX(p.x, q.x)); + u8g.drawHLine(x1, GAMEY(p.y), x2 - x1 + 1); + } + } + + #elif SNAKE_WH == 2 + + // At this scale draw two lines + for (uint8_t n = 0; n < head_ind; ++n) { + const pos_t &p = snake_tail[n], &q = snake_tail[n + 1]; + if (p.x == q.x) { + const int8_t y1 = GAMEY(MIN(p.y, q.y)), y2 = GAMEY(MAX(p.y, q.y)); + if (PAGE_CONTAINS(y1, y2 + 1)) + u8g.drawFrame(GAMEX(p.x), y1, 2, y2 - y1 + 1 + 1); + } + else { + const int8_t py = GAMEY(p.y); + if (PAGE_CONTAINS(py, py + 1)) { + const int8_t x1 = GAMEX(MIN(p.x, q.x)), x2 = GAMEX(MAX(p.x, q.x)); + u8g.drawFrame(x1, py, x2 - x1 + 1 + 1, 2); + } + } + } + + #else + + // Draw a series of boxes + for (uint8_t n = 0; n < head_ind; ++n) { + const pos_t &p = snake_tail[n], &q = snake_tail[n + 1]; + if (p.x == q.x) { + const int8_t y1 = MIN(p.y, q.y), y2 = MAX(p.y, q.y); + if (PAGE_CONTAINS(GAMEY(y1), GAMEY(y2) + SNAKE_SIZ - 1)) { + for (int8_t i = y1; i <= y2; ++i) { + const int8_t y = GAMEY(i); + if (PAGE_CONTAINS(y, y + SNAKE_SIZ - 1)) + u8g.drawBox(GAMEX(p.x), y, SNAKE_SIZ, SNAKE_SIZ); + } + } + } + else { + const int8_t py = GAMEY(p.y); + if (PAGE_CONTAINS(py, py + SNAKE_SIZ - 1)) { + const int8_t x1 = MIN(p.x, q.x), x2 = MAX(p.x, q.x); + for (int8_t i = x1; i <= x2; ++i) + u8g.drawBox(GAMEX(i), py, SNAKE_SIZ, SNAKE_SIZ); + } + } + } + + #endif + + // Draw food + const int8_t fy = GAMEY(foody); + if (PAGE_CONTAINS(fy, fy + FOOD_WH - 1)) { + const int8_t fx = GAMEX(foodx); + u8g.drawFrame(fx, fy, FOOD_WH, FOOD_WH); + if (FOOD_WH == 5) u8g.drawPixel(fx + 2, fy + 2); + } + + // Draw GAME OVER + if (!game_state) draw_game_over(); + + // A click always exits this game + if (ui.use_click()) exit_game(); +} + +void SnakeGame::enter_game() { + init_game(1, game_screen); // 1 = Game running + snake_reset(); + food_reset(); +} + +#endif // MARLIN_SNAKE diff --git a/Marlin/src/lcd/menu/menu.cpp b/Marlin/src/lcd/menu/menu.cpp index 77b2e18fa7..0365df2356 100644 --- a/Marlin/src/lcd/menu/menu.cpp +++ b/Marlin/src/lcd/menu/menu.cpp @@ -54,10 +54,12 @@ //////////////////////////////////////////// // Menu Navigation -int8_t encoderTopLine; +int8_t encoderTopLine, encoderLine, screen_items; + typedef struct { screenFunc_t menu_function; uint32_t encoder_position; + uint8_t top_line, items; } menuPosition; menuPosition screen_history[6]; uint8_t screen_history_depth = 0; @@ -80,20 +82,14 @@ bool no_reentry = false; void MarlinUI::return_to_status() { goto_screen(status_screen); } void MarlinUI::save_previous_screen() { - if (screen_history_depth < COUNT(screen_history)) { - screen_history[screen_history_depth].menu_function = currentScreen; - screen_history[screen_history_depth].encoder_position = encoderPosition; - ++screen_history_depth; - } + if (screen_history_depth < COUNT(screen_history)) + screen_history[screen_history_depth++] = { currentScreen, encoderPosition, encoderTopLine, screen_items }; } void MarlinUI::goto_previous_screen() { if (screen_history_depth > 0) { - --screen_history_depth; - goto_screen( - screen_history[screen_history_depth].menu_function, - screen_history[screen_history_depth].encoder_position - ); + menuPosition &sh = screen_history[--screen_history_depth]; + goto_screen(sh.menu_function, sh.encoder_position, sh.top_line, sh.items); } else return_to_status(); @@ -197,7 +193,7 @@ bool printer_busy() { /** * General function to go directly to a screen */ -void MarlinUI::goto_screen(screenFunc_t screen, const uint32_t encoder/*=0*/) { +void MarlinUI::goto_screen(screenFunc_t screen, const uint32_t encoder/*=0*/, const uint8_t top/*=0*/, const uint8_t items/*=0*/) { if (currentScreen != screen) { #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT) @@ -246,6 +242,8 @@ void MarlinUI::goto_screen(screenFunc_t screen, const uint32_t encoder/*=0*/) { currentScreen = screen; encoderPosition = encoder; + encoderTopLine = top; + screen_items = items; if (screen == status_screen) { defer_status_screen(false); #if ENABLED(AUTO_BED_LEVELING_UBL) @@ -314,7 +312,6 @@ void MarlinUI::synchronize(PGM_P const msg/*=NULL*/) { * _thisItemNr is the index of each MENU_ITEM or STATIC_ITEM * screen_items is the total number of items in the menu (after one call) */ -int8_t encoderLine, screen_items; void scroll_screen(const uint8_t limit, const bool is_menu) { ui.encoder_direction_menus(); ENCODER_RATE_MULTIPLY(false); diff --git a/Marlin/src/lcd/menu/menu.h b/Marlin/src/lcd/menu/menu.h index c17f116775..e695a02433 100644 --- a/Marlin/src/lcd/menu/menu.h +++ b/Marlin/src/lcd/menu/menu.h @@ -309,8 +309,8 @@ class MenuItem_bool { #define MENU_BACK(LABEL) MENU_ITEM(back, LABEL) #define MENU_ITEM_DUMMY() do { _thisItemNr++; }while(0) -#define MENU_ITEM_P(TYPE, PLABEL, ...) _MENU_ITEM_VARIANT_P(TYPE, , false, PLABEL, ## __VA_ARGS__) -#define MENU_ITEM(TYPE, LABEL, ...) _MENU_ITEM_VARIANT_P(TYPE, , false, PSTR(LABEL), ## __VA_ARGS__) +#define MENU_ITEM_P(TYPE, PLABEL, ...) _MENU_ITEM_VARIANT_P(TYPE, , false, PLABEL, ## __VA_ARGS__) +#define MENU_ITEM(TYPE, LABEL, ...) _MENU_ITEM_VARIANT_P(TYPE, , false, PSTR(LABEL), ## __VA_ARGS__) #define MENU_ITEM_EDIT(TYPE, LABEL, ...) _MENU_ITEM_VARIANT_P(TYPE, _edit, false, PSTR(LABEL), PSTR(LABEL), ## __VA_ARGS__) #define MENU_ITEM_EDIT_CALLBACK(TYPE, LABEL, ...) _MENU_ITEM_VARIANT_P(TYPE, _edit, false, PSTR(LABEL), PSTR(LABEL), ## __VA_ARGS__) #define MENU_MULTIPLIER_ITEM_EDIT(TYPE, LABEL, ...) _MENU_ITEM_VARIANT_P(TYPE, _edit, true, PSTR(LABEL), PSTR(LABEL), ## __VA_ARGS__) diff --git a/Marlin/src/lcd/menu/menu_game.cpp b/Marlin/src/lcd/menu/menu_game.cpp new file mode 100644 index 0000000000..9e0af2c7f0 --- /dev/null +++ b/Marlin/src/lcd/menu/menu_game.cpp @@ -0,0 +1,48 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include "../../inc/MarlinConfigPre.h" + +#if HAS_GAME_MENU + +#include "menu.h" +#include "game/game.h" + +void menu_game() { + START_MENU(); + MENU_BACK(MSG_MAIN); + #if ENABLED(MARLIN_BRICKOUT) + MENU_ITEM(submenu, MSG_BRICKOUT, brickout.enter_game); + #endif + #if ENABLED(MARLIN_INVADERS) + MENU_ITEM(submenu, MSG_INVADERS, invaders.enter_game); + #endif + #if ENABLED(MARLIN_SNAKE) + MENU_ITEM(submenu, MSG_SNAKE, snake.enter_game); + #endif + #if ENABLED(MARLIN_MAZE) + MENU_ITEM(submenu, MSG_MAZE, maze.enter_game); + #endif + END_MENU(); +} + +#endif // HAS_GAME_MENU diff --git a/Marlin/src/lcd/menu/menu_main.cpp b/Marlin/src/lcd/menu/menu_main.cpp index c3f5c2850d..ad7dc241a5 100644 --- a/Marlin/src/lcd/menu/menu_main.cpp +++ b/Marlin/src/lcd/menu/menu_main.cpp @@ -46,6 +46,10 @@ #include "../../feature/host_actions.h" #endif +#if HAS_GAMES + #include "game/game.h" +#endif + #define MACHINE_CAN_STOP (EITHER(SDSUPPORT, HOST_PROMPT_SUPPORT) || defined(ACTION_ON_CANCEL)) #define MACHINE_CAN_PAUSE (ANY(SDSUPPORT, HOST_PROMPT_SUPPORT, PARK_HEAD_ON_PAUSE) || defined(ACTION_ON_PAUSE)) @@ -276,6 +280,22 @@ void menu_main() { #endif #endif + #if ANY(MARLIN_BRICKOUT, MARLIN_INVADERS, MARLIN_SNAKE, MARLIN_MAZE) + MENU_ITEM(submenu, "Game", ( + #if HAS_GAME_MENU + menu_game + #elif ENABLED(MARLIN_BRICKOUT) + brickout.enter_game + #elif ENABLED(MARLIN_INVADERS) + invaders.enter_game + #elif ENABLED(MARLIN_SNAKE) + snake.enter_game + #elif ENABLED(MARLIN_MAZE) + maze.enter_game + #endif + )); + #endif + END_MENU(); } diff --git a/Marlin/src/lcd/ultralcd.h b/Marlin/src/lcd/ultralcd.h index f8899f1175..df36256ba1 100644 --- a/Marlin/src/lcd/ultralcd.h +++ b/Marlin/src/lcd/ultralcd.h @@ -419,7 +419,7 @@ public: static void synchronize(PGM_P const msg=NULL); static screenFunc_t currentScreen; - static void goto_screen(const screenFunc_t screen, const uint32_t encoder=0); + static void goto_screen(const screenFunc_t screen, const uint32_t encoder=0, const uint8_t top=0, const uint8_t items=0); static void save_previous_screen(); static void goto_previous_screen(); static void return_to_status(); diff --git a/Marlin/src/module/motion.cpp b/Marlin/src/module/motion.cpp index 2e1ed63fa9..c226c180aa 100644 --- a/Marlin/src/module/motion.cpp +++ b/Marlin/src/module/motion.cpp @@ -859,10 +859,9 @@ void clean_up_after_endstop_or_probe_move() { #if HAS_DUPLICATION_MODE bool extruder_duplication_enabled, mirrored_duplication_mode; -#endif - -#if ENABLED(MULTI_NOZZLE_DUPLICATION) && HOTENDS > 2 - uint8_t duplication_e_mask; // = 0 + #if ENABLED(MULTI_NOZZLE_DUPLICATION) + uint8_t duplication_e_mask; // = 0 + #endif #endif #if ENABLED(DUAL_X_CARRIAGE) diff --git a/Marlin/src/module/motion.h b/Marlin/src/module/motion.h index fcae47beef..2252f4cac9 100644 --- a/Marlin/src/module/motion.h +++ b/Marlin/src/module/motion.h @@ -318,10 +318,9 @@ void homeaxis(const AxisEnum axis); #if HAS_DUPLICATION_MODE extern bool extruder_duplication_enabled, // Used in Dual X mode 2 mirrored_duplication_mode; // Used in Dual X mode 3 -#endif - -#if ENABLED(MULTI_NOZZLE_DUPLICATION) && HOTENDS > 2 - uint8_t duplication_e_mask; + #if ENABLED(MULTI_NOZZLE_DUPLICATION) + extern uint8_t duplication_e_mask; + #endif #endif /** diff --git a/Marlin/src/module/stepper_indirection.h b/Marlin/src/module/stepper_indirection.h index 147a060fc9..39d4fceacb 100644 --- a/Marlin/src/module/stepper_indirection.h +++ b/Marlin/src/module/stepper_indirection.h @@ -618,14 +618,20 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset #define _REV_E_DIR(E) do{ if (E == 0) { E0_DIR_WRITE( INVERT_E0_DIR); } else { E1_DIR_WRITE( INVERT_E1_DIR); } }while(0) #endif - #if EITHER(DUAL_X_CARRIAGE, MULTI_NOZZLE_DUPLICATION) + #if HAS_DUPLICATION_MODE + + #if ENABLED(MULTI_NOZZLE_DUPLICATION) + #define _DUPE(N,T,V) do{ if (TEST(duplication_e_mask, N)) E##N##_##T##_WRITE(V); }while(0) + #else + #define _DUPE(N,T,V) E##N##_##T##_WRITE(V) + #endif + + #define NDIR(N) _DUPE(N,DIR,!INVERT_E##N##_DIR) + #define RDIR(N) _DUPE(N,DIR, INVERT_E##N##_DIR) - #define NDIR(N) _DUPE(DIR,!INVERT_E##N##_DIR) - #define RDIR(N) _DUPE(DIR, INVERT_E##N##_DIR) #define E_STEP_WRITE(E,V) do{ if (extruder_duplication_enabled) { DUPE(STEP,V); } else _E_STEP_WRITE(E,V); }while(0) #if E_STEPPERS > 2 - #define _DUPE(N,T,V) do{ if (duplication_e_mask <= (N)) E##N##_##T##_WRITE(V); }while(0) #if E_STEPPERS > 5 #define DUPE(T,V) do{ _DUPE(0,T,V); _DUPE(1,T,V); _DUPE(2,T,V); _DUPE(3,T,V); _DUPE(4,T,V); _DUPE(5,T,V); }while(0) #define NORM_E_DIR(E) do{ if (extruder_duplication_enabled) { NDIR(0); NDIR(1); NDIR(2); NDIR(3); NDIR(4); NDIR(5); } else _NORM_E_DIR(E); }while(0) @@ -644,8 +650,7 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset #define REV_E_DIR(E) do{ if (extruder_duplication_enabled) { RDIR(0); RDIR(1); RDIR(2); } else _REV_E_DIR(E); }while(0) #endif #else - #define _DUPE(T,V) do{ E0_##T##_WRITE(V); E1_##T##_WRITE(V); }while(0) - #define DUPE(T,V) _DUPE(T,V) + #define DUPE(T,V) do{ _DUPE(0,T,V); _DUPE(1,T,V); } while(0) #define NORM_E_DIR(E) do{ if (extruder_duplication_enabled) { NDIR(0); NDIR(1); } else _NORM_E_DIR(E); }while(0) #define REV_E_DIR(E) do{ if (extruder_duplication_enabled) { RDIR(0); RDIR(1); } else _REV_E_DIR(E); }while(0) #endif diff --git a/Marlin/src/module/temperature.h b/Marlin/src/module/temperature.h index 01dafa1df0..6de0bba660 100644 --- a/Marlin/src/module/temperature.h +++ b/Marlin/src/module/temperature.h @@ -572,7 +572,7 @@ class Temperature { #if HAS_HEATED_CHAMBER temp_chamber.target = #ifdef CHAMBER_MAXTEMP - min(celsius, CHAMBER_MAXTEMP) + MIN(celsius, CHAMBER_MAXTEMP) #else celsius #endif @@ -673,7 +673,7 @@ class Temperature { #if ENABLED(NO_FAN_SLOWING_IN_PID_TUNING) static bool adaptive_fan_slowing; #elif ENABLED(ADAPTIVE_FAN_SLOWING) - constexpr static bool adaptive_fan_slowing = true; + static constexpr bool adaptive_fan_slowing = true; #endif /** diff --git a/Marlin/src/sd/cardreader.cpp b/Marlin/src/sd/cardreader.cpp index f2f544809c..95527a9b85 100644 --- a/Marlin/src/sd/cardreader.cpp +++ b/Marlin/src/sd/cardreader.cpp @@ -813,7 +813,11 @@ void CardReader::setroot() { // Init sort order. for (uint16_t i = 0; i < fileCnt; i++) { - sort_order[i] = i; + sort_order[i] = ( + #if ENABLED(SDCARD_RATHERRECENTFIRST) + fileCnt - 1 - + #endif + i); // If using RAM then read all filenames now. #if ENABLED(SDSORT_USES_RAM) getfilename(i);