From b2495bdfacf8280c089148286129fd05b780e7b1 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Sat, 29 Jan 2022 17:28:20 +0700 Subject: [PATCH] add og firmware --- .gitignore | 54 + .travis.yml | 35 + CMakeLists.txt | 24 + Catch2/catch.hpp | 13287 ++++++++++++++++ Firmware/AutoDeplete.cpp | 79 + Firmware/AutoDeplete.h | 17 + Firmware/BlinkM.cpp | 29 + Firmware/BlinkM.h | 14 + Firmware/Configuration.cpp | 10 + Firmware/Configuration.h | 556 + Firmware/ConfigurationStore.cpp | 341 + Firmware/ConfigurationStore.h | 65 + Firmware/Configuration_adv.h | 454 + Firmware/Dcodes.cpp | 680 + Firmware/Dcodes.h | 28 + Firmware/Firmware.ino | 35 + Firmware/Marlin.h | 516 + Firmware/MarlinSerial.cpp | 385 + Firmware/MarlinSerial.h | 239 + Firmware/Marlin_main.cpp | 10573 ++++++++++++ Firmware/Sd2Card.cpp | 813 + Firmware/Sd2Card.h | 262 + Firmware/Sd2PinMap.h | 368 + Firmware/SdBaseFile.cpp | 1828 +++ Firmware/SdBaseFile.h | 483 + Firmware/SdFatConfig.h | 123 + Firmware/SdFatStructs.h | 646 + Firmware/SdFatUtil.cpp | 104 + Firmware/SdFatUtil.h | 51 + Firmware/SdFile.cpp | 95 + Firmware/SdFile.h | 54 + Firmware/SdInfo.h | 286 + Firmware/SdVolume.cpp | 405 + Firmware/SdVolume.h | 214 + Firmware/Servo.cpp | 344 + Firmware/Servo.h | 135 + Firmware/Timer.cpp | 68 + Firmware/Timer.h | 53 + Firmware/TimerRemaining.h | 56 + Firmware/adc.c | 94 + Firmware/adc.h | 45 + Firmware/boards.h | 14 + Firmware/bootapp.c | 55 + Firmware/bootapp.h | 41 + Firmware/cardreader.cpp | 1023 ++ Firmware/cardreader.h | 184 + Firmware/cmdqueue.cpp | 701 + Firmware/cmdqueue.h | 94 + Firmware/config.h | 55 + Firmware/conv2str.cpp | 292 + Firmware/conv2str.h | 28 + Firmware/doxyfile | 2494 +++ Firmware/eeprom.cpp | 187 + Firmware/eeprom.h | 248 + Firmware/fastio.h | 4058 +++++ Firmware/first_lay_cal.cpp | 213 + Firmware/first_lay_cal.h | 16 + Firmware/fsensor.cpp | 598 + Firmware/fsensor.h | 68 + Firmware/heatbed_pwm.cpp | 180 + Firmware/io_atmega2560.h | 374 + Firmware/language.c | 272 + Firmware/language.h | 151 + Firmware/lcd.cpp | 1004 ++ Firmware/lcd.h | 216 + Firmware/le.sh | 29 + Firmware/menu.cpp | 511 + Firmware/menu.h | 149 + Firmware/mesh_bed_calibration.cpp | 3190 ++++ Firmware/mesh_bed_calibration.h | 216 + Firmware/mesh_bed_leveling.cpp | 107 + Firmware/mesh_bed_leveling.h | 125 + Firmware/messages.c | 136 + Firmware/messages.h | 141 + Firmware/mmu.cpp | 1610 ++ Firmware/mmu.h | 141 + Firmware/motion_control.cpp | 145 + Firmware/motion_control.h | 32 + Firmware/optiboot_w25x20cl.cpp | 311 + Firmware/optiboot_w25x20cl.h | 6 + Firmware/pat9125.c | 281 + Firmware/pat9125.h | 31 + Firmware/pins.h | 88 + Firmware/pins_Einsy_1_0.h | 158 + Firmware/pins_Rambo_1_0.h | 130 + Firmware/pins_Rambo_1_3.h | 136 + Firmware/planner.cpp | 1375 ++ Firmware/planner.h | 263 + Firmware/printers.h | 40 + Firmware/qr_solve.cpp | 1930 +++ Firmware/qr_solve.h | 22 + Firmware/rbuf.c | 65 + Firmware/rbuf.h | 27 + Firmware/sm4.c | 195 + Firmware/sm4.h | 56 + Firmware/sound.cpp | 194 + Firmware/sound.h | 36 + Firmware/speed_lookuptable.h | 152 + Firmware/spi.c | 8 + Firmware/spi.h | 49 + Firmware/static_assert.h | 23 + Firmware/stepper.cpp | 1687 ++ Firmware/stepper.h | 112 + Firmware/stk500.h | 49 + Firmware/swi2c.c | 189 + Firmware/swi2c.h | 35 + Firmware/swspi.cpp | 93 + Firmware/swspi.h | 14 + Firmware/system_timer.h | 28 + Firmware/temperature.cpp | 2177 +++ Firmware/temperature.h | 267 + Firmware/thermistortables.h | 1387 ++ Firmware/timer02.c | 149 + Firmware/timer02.h | 37 + Firmware/tmc2130.cpp | 1111 ++ Firmware/tmc2130.h | 116 + Firmware/uart2.c | 84 + Firmware/uart2.h | 26 + Firmware/ultralcd.cpp | 8882 +++++++++++ Firmware/ultralcd.h | 247 + Firmware/uni_avr_rpi.h | 31 + Firmware/util.cpp | 606 + Firmware/util.h | 112 + .../variants/1_75mm_MK2-RAMBo10a-E3Dv6full.h | 461 + .../variants/1_75mm_MK2-RAMBo13a-E3Dv6full.h | 452 + .../variants/1_75mm_MK25-RAMBo10a-E3Dv6full.h | 511 + .../variants/1_75mm_MK25-RAMBo13a-E3Dv6full.h | 515 + .../1_75mm_MK25S-RAMBo10a-E3Dv6full.h | 518 + .../1_75mm_MK25S-RAMBo13a-E3Dv6full.h | 519 + .../variants/1_75mm_MK3-EINSy10a-E3Dv6full.h | 634 + .../variants/1_75mm_MK3S-EINSy10a-E3Dv6full.h | 644 + Firmware/vector_3.cpp | 167 + Firmware/vector_3.h | 62 + Firmware/w25x20cl.c | 184 + Firmware/w25x20cl.h | 44 + Firmware/xyzcal.cpp | 814 + Firmware/xyzcal.h | 41 + LICENSE | 677 + PF-build.sh | 723 + README.md | 197 +- README_cz.md | 56 + Tests/Arduino.h | 12 + Tests/AutoDeplete_test.cpp | 146 + Tests/Example_test.cpp | 12 + Tests/PrusaStatistics_test.cpp | 799 + Tests/Timer_test.cpp | 176 + Tests/tests.cpp | 2 + build.sh | 40 + lang/config.sh | 66 + lang/fw-build.sh | 186 + lang/fw-clean.sh | 63 + lang/iso639-1.txt | 10 + lang/lang-add.sh | 73 + lang/lang-build.sh | 142 + lang/lang-check.py | 75 + lang/lang-check.sh | 75 + lang/lang-clean.sh | 61 + lang/lang-export.sh | 133 + lang/lang-import.sh | 157 + lang/lang_en.txt | 1079 ++ lang/lang_en_cz.txt | 1439 ++ lang/lang_en_de.txt | 1439 ++ lang/lang_en_es.txt | 1439 ++ lang/lang_en_fr.txt | 1440 ++ lang/lang_en_it.txt | 1439 ++ lang/lang_en_pl.txt | 1439 ++ lang/po/Firmware.pot | 1812 +++ lang/po/Firmware_cs.po | 1812 +++ lang/po/Firmware_de.po | 1817 +++ lang/po/Firmware_es.po | 1812 +++ lang/po/Firmware_fr.po | 1817 +++ lang/po/Firmware_it.po | 1812 +++ lang/po/Firmware_pl.po | 1812 +++ lang/po/new/cs.po | 1812 +++ lang/po/new/de.po | 1817 +++ lang/po/new/es.po | 1812 +++ lang/po/new/fr.po | 1817 +++ lang/po/new/it.po | 1812 +++ lang/po/new/pl.po | 1812 +++ lang/progmem.sh | 116 + lang/readme.txt | 48 + lang/textaddr.sh | 68 + lang/translations.md | 252 + lang/update_lang.sh | 78 + prepare_winbuild.ps1 | 6 + test.sh | 12 + 186 files changed, 119377 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 CMakeLists.txt create mode 100644 Catch2/catch.hpp create mode 100644 Firmware/AutoDeplete.cpp create mode 100644 Firmware/AutoDeplete.h create mode 100644 Firmware/BlinkM.cpp create mode 100644 Firmware/BlinkM.h create mode 100644 Firmware/Configuration.cpp create mode 100644 Firmware/Configuration.h create mode 100644 Firmware/ConfigurationStore.cpp create mode 100644 Firmware/ConfigurationStore.h create mode 100644 Firmware/Configuration_adv.h create mode 100644 Firmware/Dcodes.cpp create mode 100644 Firmware/Dcodes.h create mode 100644 Firmware/Firmware.ino create mode 100644 Firmware/Marlin.h create mode 100644 Firmware/MarlinSerial.cpp create mode 100644 Firmware/MarlinSerial.h create mode 100644 Firmware/Marlin_main.cpp create mode 100644 Firmware/Sd2Card.cpp create mode 100644 Firmware/Sd2Card.h create mode 100644 Firmware/Sd2PinMap.h create mode 100644 Firmware/SdBaseFile.cpp create mode 100644 Firmware/SdBaseFile.h create mode 100644 Firmware/SdFatConfig.h create mode 100644 Firmware/SdFatStructs.h create mode 100644 Firmware/SdFatUtil.cpp create mode 100644 Firmware/SdFatUtil.h create mode 100644 Firmware/SdFile.cpp create mode 100644 Firmware/SdFile.h create mode 100644 Firmware/SdInfo.h create mode 100644 Firmware/SdVolume.cpp create mode 100644 Firmware/SdVolume.h create mode 100644 Firmware/Servo.cpp create mode 100644 Firmware/Servo.h create mode 100644 Firmware/Timer.cpp create mode 100644 Firmware/Timer.h create mode 100644 Firmware/TimerRemaining.h create mode 100644 Firmware/adc.c create mode 100644 Firmware/adc.h create mode 100644 Firmware/boards.h create mode 100644 Firmware/bootapp.c create mode 100644 Firmware/bootapp.h create mode 100644 Firmware/cardreader.cpp create mode 100644 Firmware/cardreader.h create mode 100644 Firmware/cmdqueue.cpp create mode 100644 Firmware/cmdqueue.h create mode 100644 Firmware/config.h create mode 100644 Firmware/conv2str.cpp create mode 100644 Firmware/conv2str.h create mode 100644 Firmware/doxyfile create mode 100644 Firmware/eeprom.cpp create mode 100644 Firmware/eeprom.h create mode 100644 Firmware/fastio.h create mode 100644 Firmware/first_lay_cal.cpp create mode 100644 Firmware/first_lay_cal.h create mode 100644 Firmware/fsensor.cpp create mode 100644 Firmware/fsensor.h create mode 100644 Firmware/heatbed_pwm.cpp create mode 100644 Firmware/io_atmega2560.h create mode 100644 Firmware/language.c create mode 100644 Firmware/language.h create mode 100644 Firmware/lcd.cpp create mode 100644 Firmware/lcd.h create mode 100644 Firmware/le.sh create mode 100644 Firmware/menu.cpp create mode 100644 Firmware/menu.h create mode 100644 Firmware/mesh_bed_calibration.cpp create mode 100644 Firmware/mesh_bed_calibration.h create mode 100644 Firmware/mesh_bed_leveling.cpp create mode 100644 Firmware/mesh_bed_leveling.h create mode 100644 Firmware/messages.c create mode 100644 Firmware/messages.h create mode 100644 Firmware/mmu.cpp create mode 100644 Firmware/mmu.h create mode 100644 Firmware/motion_control.cpp create mode 100644 Firmware/motion_control.h create mode 100644 Firmware/optiboot_w25x20cl.cpp create mode 100644 Firmware/optiboot_w25x20cl.h create mode 100644 Firmware/pat9125.c create mode 100644 Firmware/pat9125.h create mode 100644 Firmware/pins.h create mode 100644 Firmware/pins_Einsy_1_0.h create mode 100644 Firmware/pins_Rambo_1_0.h create mode 100644 Firmware/pins_Rambo_1_3.h create mode 100644 Firmware/planner.cpp create mode 100644 Firmware/planner.h create mode 100644 Firmware/printers.h create mode 100644 Firmware/qr_solve.cpp create mode 100644 Firmware/qr_solve.h create mode 100644 Firmware/rbuf.c create mode 100644 Firmware/rbuf.h create mode 100644 Firmware/sm4.c create mode 100644 Firmware/sm4.h create mode 100644 Firmware/sound.cpp create mode 100644 Firmware/sound.h create mode 100644 Firmware/speed_lookuptable.h create mode 100644 Firmware/spi.c create mode 100644 Firmware/spi.h create mode 100644 Firmware/static_assert.h create mode 100644 Firmware/stepper.cpp create mode 100644 Firmware/stepper.h create mode 100644 Firmware/stk500.h create mode 100644 Firmware/swi2c.c create mode 100644 Firmware/swi2c.h create mode 100644 Firmware/swspi.cpp create mode 100644 Firmware/swspi.h create mode 100644 Firmware/system_timer.h create mode 100644 Firmware/temperature.cpp create mode 100644 Firmware/temperature.h create mode 100644 Firmware/thermistortables.h create mode 100644 Firmware/timer02.c create mode 100644 Firmware/timer02.h create mode 100644 Firmware/tmc2130.cpp create mode 100644 Firmware/tmc2130.h create mode 100644 Firmware/uart2.c create mode 100644 Firmware/uart2.h create mode 100644 Firmware/ultralcd.cpp create mode 100644 Firmware/ultralcd.h create mode 100644 Firmware/uni_avr_rpi.h create mode 100644 Firmware/util.cpp create mode 100644 Firmware/util.h create mode 100644 Firmware/variants/1_75mm_MK2-RAMBo10a-E3Dv6full.h create mode 100644 Firmware/variants/1_75mm_MK2-RAMBo13a-E3Dv6full.h create mode 100644 Firmware/variants/1_75mm_MK25-RAMBo10a-E3Dv6full.h create mode 100644 Firmware/variants/1_75mm_MK25-RAMBo13a-E3Dv6full.h create mode 100644 Firmware/variants/1_75mm_MK25S-RAMBo10a-E3Dv6full.h create mode 100644 Firmware/variants/1_75mm_MK25S-RAMBo13a-E3Dv6full.h create mode 100644 Firmware/variants/1_75mm_MK3-EINSy10a-E3Dv6full.h create mode 100644 Firmware/variants/1_75mm_MK3S-EINSy10a-E3Dv6full.h create mode 100644 Firmware/vector_3.cpp create mode 100644 Firmware/vector_3.h create mode 100644 Firmware/w25x20cl.c create mode 100644 Firmware/w25x20cl.h create mode 100644 Firmware/xyzcal.cpp create mode 100644 Firmware/xyzcal.h create mode 100644 LICENSE create mode 100644 PF-build.sh create mode 100644 README_cz.md create mode 100644 Tests/Arduino.h create mode 100644 Tests/AutoDeplete_test.cpp create mode 100644 Tests/Example_test.cpp create mode 100644 Tests/PrusaStatistics_test.cpp create mode 100644 Tests/Timer_test.cpp create mode 100644 Tests/tests.cpp create mode 100644 build.sh create mode 100644 lang/config.sh create mode 100644 lang/fw-build.sh create mode 100644 lang/fw-clean.sh create mode 100644 lang/iso639-1.txt create mode 100644 lang/lang-add.sh create mode 100644 lang/lang-build.sh create mode 100644 lang/lang-check.py create mode 100644 lang/lang-check.sh create mode 100644 lang/lang-clean.sh create mode 100644 lang/lang-export.sh create mode 100644 lang/lang-import.sh create mode 100644 lang/lang_en.txt create mode 100644 lang/lang_en_cz.txt create mode 100644 lang/lang_en_de.txt create mode 100644 lang/lang_en_es.txt create mode 100644 lang/lang_en_fr.txt create mode 100644 lang/lang_en_it.txt create mode 100644 lang/lang_en_pl.txt create mode 100644 lang/po/Firmware.pot create mode 100644 lang/po/Firmware_cs.po create mode 100644 lang/po/Firmware_de.po create mode 100644 lang/po/Firmware_es.po create mode 100644 lang/po/Firmware_fr.po create mode 100644 lang/po/Firmware_it.po create mode 100644 lang/po/Firmware_pl.po create mode 100644 lang/po/new/cs.po create mode 100644 lang/po/new/de.po create mode 100644 lang/po/new/es.po create mode 100644 lang/po/new/fr.po create mode 100644 lang/po/new/it.po create mode 100644 lang/po/new/pl.po create mode 100644 lang/progmem.sh create mode 100644 lang/readme.txt create mode 100644 lang/textaddr.sh create mode 100644 lang/translations.md create mode 100644 lang/update_lang.sh create mode 100644 prepare_winbuild.ps1 create mode 100644 test.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dec628d --- /dev/null +++ b/.gitignore @@ -0,0 +1,54 @@ +.settings +.project +.cproject +Debug +Firmware/Configuration_prusa.h +Firmware/Doc +/Firmware/.vs/Firmware/v14 +/Firmware/__vm +/Firmware/Firmware.sln +/Firmware/Firmware.vcxproj +/Firmware/Firmware.vcxproj.filters +/Firmware/Firmware - Shortcut.lnk +/Firmware/variants/1_75mm_MK3-MMU-EINSy10a-E3Dv6full.h.bak +/Firmware/Marlin_main.cpp~RF12cfae7.TMP +/Firmware/variants/1_75mm_MK3-EINSy10a-E3Dv6full.h.bak +/html +/latex +/Doxyfile +/Firmware/builds/1_75mm_MK3-EINY04-E3Dv6full +/Firmware/Configuration_prusa.h.bak +/Firmware/Configuration_prusa_backup.h +/Firmware/ultralcd_implementation_hitachi_HD44780.h.bak +/Firmware/ultralcd.cpp.bak +/Firmware/temperature.cpp.bak +/Firmware/pins.h.bak +/Firmware/Marlin_main.cpp.bak +/Firmware/language_pl.h.bak +/Firmware/language_it.h.bak +/Firmware/language_es.h.bak +/Firmware/language_en.h.bak +/Firmware/language_de.h.bak +/Firmware/language_cz.h.bak +/Firmware/variants/1_75mm_MK2-MultiMaterial-RAMBo13a-E3Dv6full.h +/Firmware/variants/1_75mm_MK2-MultiMaterial-RAMBo10a-E3Dv6full.h +/Firmware/variants/1_75mm_MK2-EINY01-E3Dv6full.h.bak +/Firmware/variants/1_75mm_MK1-RAMBo13a-E3Dv6full.h +/Firmware/variants/1_75mm_MK1-RAMBo10a-E3Dv6full.h +/lang/*.bin +/lang/*.hex +/lang/*.dat +/lang/*.tmp +/lang/*.out +/lang/not_tran.txt +/lang/not_used.txt +/lang/progmem1.chr +/lang/progmem1.lss +/lang/progmem1.txt +/lang/progmem1.var +/lang/text.sym +/lang/textaddr.txt +/build-env/ +/Firmware/Firmware.vcxproj +/Firmware/Configuration_prusa_bckp.h +/Firmware/variants/printers.h diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..4d9039d --- /dev/null +++ b/.travis.yml @@ -0,0 +1,35 @@ +dist: trusty +before_install: + - sudo apt-get install -y ninja-build + # Arduino IDE adds a lot of noise caused by network traffic, trying to firewall it off + - sudo iptables -P INPUT DROP + - sudo iptables -P FORWARD DROP + - sudo iptables -P OUTPUT ACCEPT + - sudo iptables -A INPUT -i lo -j ACCEPT + - sudo iptables -A OUTPUT -o lo -j ACCEPT + - sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT +script: + - bash -x test.sh + - cp Firmware/variants/1_75mm_MK3S-EINSy10a-E3Dv6full.h Firmware/Configuration_prusa.h + - bash -x build.sh || { echo "1_75mm_MK3S-EINSy10a-E3Dv6full variant failed" && false; } + - rm Firmware/Configuration_prusa.h + - cp Firmware/variants/1_75mm_MK3-EINSy10a-E3Dv6full.h Firmware/Configuration_prusa.h + - bash -x build.sh || { echo "1_75mm_MK3-EINSy10a-E3Dv6full variant failed" && false; } + - rm Firmware/Configuration_prusa.h + - cp Firmware/variants/1_75mm_MK25S-RAMBo13a-E3Dv6full.h Firmware/Configuration_prusa.h + - bash -x build.sh || { echo "1_75mm_MK25S-RAMBo13a-E3Dv6full variant failed" && false; } + - rm Firmware/Configuration_prusa.h + - cp Firmware/variants/1_75mm_MK25S-RAMBo10a-E3Dv6full.h Firmware/Configuration_prusa.h + - bash -x build.sh || { echo "1_75mm_MK25S-RAMBo10a-E3Dv6full variant failed" && false; } + - rm Firmware/Configuration_prusa.h + - cp Firmware/variants/1_75mm_MK25-RAMBo13a-E3Dv6full.h Firmware/Configuration_prusa.h + - bash -x build.sh || { echo "1_75mm_MK25-RAMBo13a-E3Dv6full variant failed" && false; } + - rm Firmware/Configuration_prusa.h + - cp Firmware/variants/1_75mm_MK25-RAMBo10a-E3Dv6full.h Firmware/Configuration_prusa.h + - bash -x build.sh || { echo "1_75mm_MK25-RAMBo10a-E3Dv6full variant failed" && false; } + - rm Firmware/Configuration_prusa.h + - cp Firmware/variants/1_75mm_MK2-RAMBo13a-E3Dv6full.h Firmware/Configuration_prusa.h + - bash -x build.sh || { echo "1_75mm_MK2-RAMBo13a-E3Dv6full variant failed" && false; } + - rm Firmware/Configuration_prusa.h + - cp Firmware/variants/1_75mm_MK2-RAMBo10a-E3Dv6full.h Firmware/Configuration_prusa.h + - bash -x build.sh || { echo "1_75mm_MK2-RAMBo10a-E3Dv6full variant failed" && false; } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..4ca1d95 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,24 @@ +cmake_minimum_required(VERSION 3.1) + +set (CMAKE_CXX_STANDARD 11) + +project(cmake_test) + +# Prepare "Catch" library for other executables +set(CATCH_INCLUDE_DIR Catch2) +add_library(Catch INTERFACE) +target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR}) + +# Make test executable +set(TEST_SOURCES + Tests/tests.cpp + Tests/Example_test.cpp + Tests/Timer_test.cpp + Tests/AutoDeplete_test.cpp + Tests/PrusaStatistics_test.cpp + Firmware/Timer.cpp + Firmware/AutoDeplete.cpp +) +add_executable(tests ${TEST_SOURCES}) +target_include_directories(tests PRIVATE Tests) +target_link_libraries(tests Catch) diff --git a/Catch2/catch.hpp b/Catch2/catch.hpp new file mode 100644 index 0000000..28448dd --- /dev/null +++ b/Catch2/catch.hpp @@ -0,0 +1,13287 @@ +/* + * Catch v2.2.3 + * Generated: 2018-06-06 23:11:57.601416 + * ---------------------------------------------------------- + * This file has been merged from multiple headers. Please don't edit it directly + * Copyright (c) 2018 Two Blue Cubes Ltd. All rights reserved. + * + * Distributed under the Boost Software License, Version 1.0. (See accompanying + * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + */ +#ifndef TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED +#define TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED +// start catch.hpp + + +#define CATCH_VERSION_MAJOR 2 +#define CATCH_VERSION_MINOR 2 +#define CATCH_VERSION_PATCH 3 + +#ifdef __clang__ +# pragma clang system_header +#elif defined __GNUC__ +# pragma GCC system_header +#endif + +// start catch_suppress_warnings.h + +#ifdef __clang__ +# ifdef __ICC // icpc defines the __clang__ macro +# pragma warning(push) +# pragma warning(disable: 161 1682) +# else // __ICC +# pragma clang diagnostic ignored "-Wunused-variable" +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wpadded" +# pragma clang diagnostic ignored "-Wswitch-enum" +# pragma clang diagnostic ignored "-Wcovered-switch-default" +# endif +#elif defined __GNUC__ +# pragma GCC diagnostic ignored "-Wparentheses" +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wunused-variable" +# pragma GCC diagnostic ignored "-Wpadded" +#endif +// end catch_suppress_warnings.h +#if defined(CATCH_CONFIG_MAIN) || defined(CATCH_CONFIG_RUNNER) +# define CATCH_IMPL +# define CATCH_CONFIG_ALL_PARTS +#endif + +// In the impl file, we want to have access to all parts of the headers +// Can also be used to sanely support PCHs +#if defined(CATCH_CONFIG_ALL_PARTS) +# define CATCH_CONFIG_EXTERNAL_INTERFACES +# if defined(CATCH_CONFIG_DISABLE_MATCHERS) +# undef CATCH_CONFIG_DISABLE_MATCHERS +# endif +# define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER +#endif + +#if !defined(CATCH_CONFIG_IMPL_ONLY) +// start catch_platform.h + +#ifdef __APPLE__ +# include +# if TARGET_OS_OSX == 1 +# define CATCH_PLATFORM_MAC +# elif TARGET_OS_IPHONE == 1 +# define CATCH_PLATFORM_IPHONE +# endif + +#elif defined(linux) || defined(__linux) || defined(__linux__) +# define CATCH_PLATFORM_LINUX + +#elif defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) || defined(__MINGW32__) +# define CATCH_PLATFORM_WINDOWS +#endif + +// end catch_platform.h + +#ifdef CATCH_IMPL +# ifndef CLARA_CONFIG_MAIN +# define CLARA_CONFIG_MAIN_NOT_DEFINED +# define CLARA_CONFIG_MAIN +# endif +#endif + +// start catch_user_interfaces.h + +namespace Catch { + unsigned int rngSeed(); +} + +// end catch_user_interfaces.h +// start catch_tag_alias_autoregistrar.h + +// start catch_common.h + +// start catch_compiler_capabilities.h + +// Detect a number of compiler features - by compiler +// The following features are defined: +// +// CATCH_CONFIG_COUNTER : is the __COUNTER__ macro supported? +// CATCH_CONFIG_WINDOWS_SEH : is Windows SEH supported? +// CATCH_CONFIG_POSIX_SIGNALS : are POSIX signals supported? +// **************** +// Note to maintainers: if new toggles are added please document them +// in configuration.md, too +// **************** + +// In general each macro has a _NO_ form +// (e.g. CATCH_CONFIG_NO_POSIX_SIGNALS) which disables the feature. +// Many features, at point of detection, define an _INTERNAL_ macro, so they +// can be combined, en-mass, with the _NO_ forms later. + +#ifdef __cplusplus + +# if __cplusplus >= 201402L +# define CATCH_CPP14_OR_GREATER +# endif + +# if __cplusplus >= 201703L +# define CATCH_CPP17_OR_GREATER +# endif + +#endif + +#if defined(CATCH_CPP17_OR_GREATER) +# define CATCH_INTERNAL_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS +#endif + +#ifdef __clang__ + +# define CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + _Pragma( "clang diagnostic push" ) \ + _Pragma( "clang diagnostic ignored \"-Wexit-time-destructors\"" ) \ + _Pragma( "clang diagnostic ignored \"-Wglobal-constructors\"") +# define CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS \ + _Pragma( "clang diagnostic pop" ) + +# define CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS \ + _Pragma( "clang diagnostic push" ) \ + _Pragma( "clang diagnostic ignored \"-Wparentheses\"" ) +# define CATCH_INTERNAL_UNSUPPRESS_PARENTHESES_WARNINGS \ + _Pragma( "clang diagnostic pop" ) + +#endif // __clang__ + +//////////////////////////////////////////////////////////////////////////////// +// Assume that non-Windows platforms support posix signals by default +#if !defined(CATCH_PLATFORM_WINDOWS) + #define CATCH_INTERNAL_CONFIG_POSIX_SIGNALS +#endif + +//////////////////////////////////////////////////////////////////////////////// +// We know some environments not to support full POSIX signals +#if defined(__CYGWIN__) || defined(__QNX__) || defined(__EMSCRIPTEN__) || defined(__DJGPP__) + #define CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS +#endif + +#ifdef __OS400__ +# define CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS +# define CATCH_CONFIG_COLOUR_NONE +#endif + +//////////////////////////////////////////////////////////////////////////////// +// Android somehow still does not support std::to_string +#if defined(__ANDROID__) +# define CATCH_INTERNAL_CONFIG_NO_CPP11_TO_STRING +#endif + +//////////////////////////////////////////////////////////////////////////////// +// Not all Windows environments support SEH properly +#if defined(__MINGW32__) +# define CATCH_INTERNAL_CONFIG_NO_WINDOWS_SEH +#endif + +//////////////////////////////////////////////////////////////////////////////// +// Cygwin +#ifdef __CYGWIN__ + +// Required for some versions of Cygwin to declare gettimeofday +// see: http://stackoverflow.com/questions/36901803/gettimeofday-not-declared-in-this-scope-cygwin +# define _BSD_SOURCE + +#endif // __CYGWIN__ + +//////////////////////////////////////////////////////////////////////////////// +// Visual C++ +#ifdef _MSC_VER + +# if _MSC_VER >= 1900 // Visual Studio 2015 or newer +# define CATCH_INTERNAL_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS +# endif + +// Universal Windows platform does not support SEH +// Or console colours (or console at all...) +# if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP) +# define CATCH_CONFIG_COLOUR_NONE +# else +# define CATCH_INTERNAL_CONFIG_WINDOWS_SEH +# endif + +#endif // _MSC_VER + +//////////////////////////////////////////////////////////////////////////////// + +// DJGPP +#ifdef __DJGPP__ +# define CATCH_INTERNAL_CONFIG_NO_WCHAR +#endif // __DJGPP__ + +//////////////////////////////////////////////////////////////////////////////// + +// Use of __COUNTER__ is suppressed during code analysis in +// CLion/AppCode 2017.2.x and former, because __COUNTER__ is not properly +// handled by it. +// Otherwise all supported compilers support COUNTER macro, +// but user still might want to turn it off +#if ( !defined(__JETBRAINS_IDE__) || __JETBRAINS_IDE__ >= 20170300L ) + #define CATCH_INTERNAL_CONFIG_COUNTER +#endif + +#if defined(CATCH_INTERNAL_CONFIG_COUNTER) && !defined(CATCH_CONFIG_NO_COUNTER) && !defined(CATCH_CONFIG_COUNTER) +# define CATCH_CONFIG_COUNTER +#endif +#if defined(CATCH_INTERNAL_CONFIG_WINDOWS_SEH) && !defined(CATCH_CONFIG_NO_WINDOWS_SEH) && !defined(CATCH_CONFIG_WINDOWS_SEH) && !defined(CATCH_INTERNAL_CONFIG_NO_WINDOWS_SEH) +# define CATCH_CONFIG_WINDOWS_SEH +#endif +// This is set by default, because we assume that unix compilers are posix-signal-compatible by default. +#if defined(CATCH_INTERNAL_CONFIG_POSIX_SIGNALS) && !defined(CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS) && !defined(CATCH_CONFIG_NO_POSIX_SIGNALS) && !defined(CATCH_CONFIG_POSIX_SIGNALS) +# define CATCH_CONFIG_POSIX_SIGNALS +#endif +// This is set by default, because we assume that compilers with no wchar_t support are just rare exceptions. +#if !defined(CATCH_INTERNAL_CONFIG_NO_WCHAR) && !defined(CATCH_CONFIG_NO_WCHAR) && !defined(CATCH_CONFIG_WCHAR) +# define CATCH_CONFIG_WCHAR +#endif + +#if !defined(CATCH_INTERNAL_CONFIG_NO_CPP11_TO_STRING) && !defined(CATCH_CONFIG_NO_CPP11_TO_STRING) && !defined(CATCH_CONFIG_CPP11_TO_STRING) +# define CATCH_CONFIG_CPP11_TO_STRING +#endif + +#if defined(CATCH_INTERNAL_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS) && !defined(CATCH_CONFIG_NO_CPP17_UNCAUGHT_EXCEPTIONS) && !defined(CATCH_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS) +# define CATCH_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS +#endif + +#if !defined(CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS) +# define CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS +# define CATCH_INTERNAL_UNSUPPRESS_PARENTHESES_WARNINGS +#endif +#if !defined(CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS) +# define CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS +# define CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS +#endif + +// end catch_compiler_capabilities.h +#define INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line ) name##line +#define INTERNAL_CATCH_UNIQUE_NAME_LINE( name, line ) INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line ) +#ifdef CATCH_CONFIG_COUNTER +# define INTERNAL_CATCH_UNIQUE_NAME( name ) INTERNAL_CATCH_UNIQUE_NAME_LINE( name, __COUNTER__ ) +#else +# define INTERNAL_CATCH_UNIQUE_NAME( name ) INTERNAL_CATCH_UNIQUE_NAME_LINE( name, __LINE__ ) +#endif + +#include +#include +#include + +namespace Catch { + + struct CaseSensitive { enum Choice { + Yes, + No + }; }; + + class NonCopyable { + NonCopyable( NonCopyable const& ) = delete; + NonCopyable( NonCopyable && ) = delete; + NonCopyable& operator = ( NonCopyable const& ) = delete; + NonCopyable& operator = ( NonCopyable && ) = delete; + + protected: + NonCopyable(); + virtual ~NonCopyable(); + }; + + struct SourceLineInfo { + + SourceLineInfo() = delete; + SourceLineInfo( char const* _file, std::size_t _line ) noexcept + : file( _file ), + line( _line ) + {} + + SourceLineInfo( SourceLineInfo const& other ) = default; + SourceLineInfo( SourceLineInfo && ) = default; + SourceLineInfo& operator = ( SourceLineInfo const& ) = default; + SourceLineInfo& operator = ( SourceLineInfo && ) = default; + + bool empty() const noexcept; + bool operator == ( SourceLineInfo const& other ) const noexcept; + bool operator < ( SourceLineInfo const& other ) const noexcept; + + char const* file; + std::size_t line; + }; + + std::ostream& operator << ( std::ostream& os, SourceLineInfo const& info ); + + // Use this in variadic streaming macros to allow + // >> +StreamEndStop + // as well as + // >> stuff +StreamEndStop + struct StreamEndStop { + std::string operator+() const; + }; + template + T const& operator + ( T const& value, StreamEndStop ) { + return value; + } +} + +#define CATCH_INTERNAL_LINEINFO \ + ::Catch::SourceLineInfo( __FILE__, static_cast( __LINE__ ) ) + +// end catch_common.h +namespace Catch { + + struct RegistrarForTagAliases { + RegistrarForTagAliases( char const* alias, char const* tag, SourceLineInfo const& lineInfo ); + }; + +} // end namespace Catch + +#define CATCH_REGISTER_TAG_ALIAS( alias, spec ) \ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + namespace{ Catch::RegistrarForTagAliases INTERNAL_CATCH_UNIQUE_NAME( AutoRegisterTagAlias )( alias, spec, CATCH_INTERNAL_LINEINFO ); } \ + CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS + +// end catch_tag_alias_autoregistrar.h +// start catch_test_registry.h + +// start catch_interfaces_testcase.h + +#include +#include + +namespace Catch { + + class TestSpec; + + struct ITestInvoker { + virtual void invoke () const = 0; + virtual ~ITestInvoker(); + }; + + using ITestCasePtr = std::shared_ptr; + + class TestCase; + struct IConfig; + + struct ITestCaseRegistry { + virtual ~ITestCaseRegistry(); + virtual std::vector const& getAllTests() const = 0; + virtual std::vector const& getAllTestsSorted( IConfig const& config ) const = 0; + }; + + bool matchTest( TestCase const& testCase, TestSpec const& testSpec, IConfig const& config ); + std::vector filterTests( std::vector const& testCases, TestSpec const& testSpec, IConfig const& config ); + std::vector const& getAllTestCasesSorted( IConfig const& config ); + +} + +// end catch_interfaces_testcase.h +// start catch_stringref.h + +#include +#include +#include + +namespace Catch { + + class StringData; + + /// A non-owning string class (similar to the forthcoming std::string_view) + /// Note that, because a StringRef may be a substring of another string, + /// it may not be null terminated. c_str() must return a null terminated + /// string, however, and so the StringRef will internally take ownership + /// (taking a copy), if necessary. In theory this ownership is not externally + /// visible - but it does mean (substring) StringRefs should not be shared between + /// threads. + class StringRef { + public: + using size_type = std::size_t; + + private: + friend struct StringRefTestAccess; + + char const* m_start; + size_type m_size; + + char* m_data = nullptr; + + void takeOwnership(); + + static constexpr char const* const s_empty = ""; + + public: // construction/ assignment + StringRef() noexcept + : StringRef( s_empty, 0 ) + {} + + StringRef( StringRef const& other ) noexcept + : m_start( other.m_start ), + m_size( other.m_size ) + {} + + StringRef( StringRef&& other ) noexcept + : m_start( other.m_start ), + m_size( other.m_size ), + m_data( other.m_data ) + { + other.m_data = nullptr; + } + + StringRef( char const* rawChars ) noexcept; + + StringRef( char const* rawChars, size_type size ) noexcept + : m_start( rawChars ), + m_size( size ) + {} + + StringRef( std::string const& stdString ) noexcept + : m_start( stdString.c_str() ), + m_size( stdString.size() ) + {} + + ~StringRef() noexcept { + delete[] m_data; + } + + auto operator = ( StringRef const &other ) noexcept -> StringRef& { + delete[] m_data; + m_data = nullptr; + m_start = other.m_start; + m_size = other.m_size; + return *this; + } + + operator std::string() const; + + void swap( StringRef& other ) noexcept; + + public: // operators + auto operator == ( StringRef const& other ) const noexcept -> bool; + auto operator != ( StringRef const& other ) const noexcept -> bool; + + auto operator[] ( size_type index ) const noexcept -> char; + + public: // named queries + auto empty() const noexcept -> bool { + return m_size == 0; + } + auto size() const noexcept -> size_type { + return m_size; + } + + auto numberOfCharacters() const noexcept -> size_type; + auto c_str() const -> char const*; + + public: // substrings and searches + auto substr( size_type start, size_type size ) const noexcept -> StringRef; + + // Returns the current start pointer. + // Note that the pointer can change when if the StringRef is a substring + auto currentData() const noexcept -> char const*; + + private: // ownership queries - may not be consistent between calls + auto isOwned() const noexcept -> bool; + auto isSubstring() const noexcept -> bool; + }; + + auto operator + ( StringRef const& lhs, StringRef const& rhs ) -> std::string; + auto operator + ( StringRef const& lhs, char const* rhs ) -> std::string; + auto operator + ( char const* lhs, StringRef const& rhs ) -> std::string; + + auto operator += ( std::string& lhs, StringRef const& sr ) -> std::string&; + auto operator << ( std::ostream& os, StringRef const& sr ) -> std::ostream&; + + inline auto operator "" _sr( char const* rawChars, std::size_t size ) noexcept -> StringRef { + return StringRef( rawChars, size ); + } + +} // namespace Catch + +// end catch_stringref.h +namespace Catch { + +template +class TestInvokerAsMethod : public ITestInvoker { + void (C::*m_testAsMethod)(); +public: + TestInvokerAsMethod( void (C::*testAsMethod)() ) noexcept : m_testAsMethod( testAsMethod ) {} + + void invoke() const override { + C obj; + (obj.*m_testAsMethod)(); + } +}; + +auto makeTestInvoker( void(*testAsFunction)() ) noexcept -> ITestInvoker*; + +template +auto makeTestInvoker( void (C::*testAsMethod)() ) noexcept -> ITestInvoker* { + return new(std::nothrow) TestInvokerAsMethod( testAsMethod ); +} + +struct NameAndTags { + NameAndTags( StringRef const& name_ = StringRef(), StringRef const& tags_ = StringRef() ) noexcept; + StringRef name; + StringRef tags; +}; + +struct AutoReg : NonCopyable { + AutoReg( ITestInvoker* invoker, SourceLineInfo const& lineInfo, StringRef const& classOrMethod, NameAndTags const& nameAndTags ) noexcept; + ~AutoReg(); +}; + +} // end namespace Catch + +#define INTERNAL_CATCH_EXPAND1(param) INTERNAL_CATCH_EXPAND2(param) +#define INTERNAL_CATCH_EXPAND2(...) INTERNAL_CATCH_NO## __VA_ARGS__ +#define INTERNAL_CATCH_DEF(...) INTERNAL_CATCH_DEF __VA_ARGS__ +#define INTERNAL_CATCH_NOINTERNAL_CATCH_DEF + +#if defined(CATCH_CONFIG_DISABLE) + #define INTERNAL_CATCH_TESTCASE_NO_REGISTRATION( TestName, ... ) \ + static void TestName() + #define INTERNAL_CATCH_TESTCASE_METHOD_NO_REGISTRATION( TestName, ClassName, ... ) \ + namespace{ \ + struct TestName : INTERNAL_CATCH_EXPAND1(INTERNAL_CATCH_DEF ClassName) { \ + void test(); \ + }; \ + } \ + void TestName::test() + +#endif + + /////////////////////////////////////////////////////////////////////////////// + #define INTERNAL_CATCH_TESTCASE2( TestName, ... ) \ + static void TestName(); \ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( Catch::makeTestInvoker( &TestName ), CATCH_INTERNAL_LINEINFO, "", Catch::NameAndTags{ __VA_ARGS__ } ); } /* NOLINT */ \ + CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS \ + static void TestName() + #define INTERNAL_CATCH_TESTCASE( ... ) \ + INTERNAL_CATCH_TESTCASE2( INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ), __VA_ARGS__ ) + + /////////////////////////////////////////////////////////////////////////////// + #define INTERNAL_CATCH_METHOD_AS_TEST_CASE( QualifiedMethod, ... ) \ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( Catch::makeTestInvoker( &QualifiedMethod ), CATCH_INTERNAL_LINEINFO, "&" #QualifiedMethod, Catch::NameAndTags{ __VA_ARGS__ } ); } /* NOLINT */ \ + CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS + + /////////////////////////////////////////////////////////////////////////////// + #define INTERNAL_CATCH_TEST_CASE_METHOD2( TestName, ClassName, ... )\ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + namespace{ \ + struct TestName : INTERNAL_CATCH_EXPAND1(INTERNAL_CATCH_DEF ClassName) { \ + void test(); \ + }; \ + Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar ) ( Catch::makeTestInvoker( &TestName::test ), CATCH_INTERNAL_LINEINFO, #ClassName, Catch::NameAndTags{ __VA_ARGS__ } ); /* NOLINT */ \ + } \ + CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS \ + void TestName::test() + #define INTERNAL_CATCH_TEST_CASE_METHOD( ClassName, ... ) \ + INTERNAL_CATCH_TEST_CASE_METHOD2( INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ), ClassName, __VA_ARGS__ ) + + /////////////////////////////////////////////////////////////////////////////// + #define INTERNAL_CATCH_REGISTER_TESTCASE( Function, ... ) \ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( Catch::makeTestInvoker( Function ), CATCH_INTERNAL_LINEINFO, "", Catch::NameAndTags{ __VA_ARGS__ } ); /* NOLINT */ \ + CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS + +// end catch_test_registry.h +// start catch_capture.hpp + +// start catch_assertionhandler.h + +// start catch_assertioninfo.h + +// start catch_result_type.h + +namespace Catch { + + // ResultWas::OfType enum + struct ResultWas { enum OfType { + Unknown = -1, + Ok = 0, + Info = 1, + Warning = 2, + + FailureBit = 0x10, + + ExpressionFailed = FailureBit | 1, + ExplicitFailure = FailureBit | 2, + + Exception = 0x100 | FailureBit, + + ThrewException = Exception | 1, + DidntThrowException = Exception | 2, + + FatalErrorCondition = 0x200 | FailureBit + + }; }; + + bool isOk( ResultWas::OfType resultType ); + bool isJustInfo( int flags ); + + // ResultDisposition::Flags enum + struct ResultDisposition { enum Flags { + Normal = 0x01, + + ContinueOnFailure = 0x02, // Failures fail test, but execution continues + FalseTest = 0x04, // Prefix expression with ! + SuppressFail = 0x08 // Failures are reported but do not fail the test + }; }; + + ResultDisposition::Flags operator | ( ResultDisposition::Flags lhs, ResultDisposition::Flags rhs ); + + bool shouldContinueOnFailure( int flags ); + inline bool isFalseTest( int flags ) { return ( flags & ResultDisposition::FalseTest ) != 0; } + bool shouldSuppressFailure( int flags ); + +} // end namespace Catch + +// end catch_result_type.h +namespace Catch { + + struct AssertionInfo + { + StringRef macroName; + SourceLineInfo lineInfo; + StringRef capturedExpression; + ResultDisposition::Flags resultDisposition; + + // We want to delete this constructor but a compiler bug in 4.8 means + // the struct is then treated as non-aggregate + //AssertionInfo() = delete; + }; + +} // end namespace Catch + +// end catch_assertioninfo.h +// start catch_decomposer.h + +// start catch_tostring.h + +#include +#include +#include +#include +// start catch_stream.h + +#include +#include +#include + +namespace Catch { + + std::ostream& cout(); + std::ostream& cerr(); + std::ostream& clog(); + + class StringRef; + + struct IStream { + virtual ~IStream(); + virtual std::ostream& stream() const = 0; + }; + + auto makeStream( StringRef const &filename ) -> IStream const*; + + class ReusableStringStream { + std::size_t m_index; + std::ostream* m_oss; + public: + ReusableStringStream(); + ~ReusableStringStream(); + + auto str() const -> std::string; + + template + auto operator << ( T const& value ) -> ReusableStringStream& { + *m_oss << value; + return *this; + } + auto get() -> std::ostream& { return *m_oss; } + + static void cleanup(); + }; +} + +// end catch_stream.h + +#ifdef __OBJC__ +// start catch_objc_arc.hpp + +#import + +#ifdef __has_feature +#define CATCH_ARC_ENABLED __has_feature(objc_arc) +#else +#define CATCH_ARC_ENABLED 0 +#endif + +void arcSafeRelease( NSObject* obj ); +id performOptionalSelector( id obj, SEL sel ); + +#if !CATCH_ARC_ENABLED +inline void arcSafeRelease( NSObject* obj ) { + [obj release]; +} +inline id performOptionalSelector( id obj, SEL sel ) { + if( [obj respondsToSelector: sel] ) + return [obj performSelector: sel]; + return nil; +} +#define CATCH_UNSAFE_UNRETAINED +#define CATCH_ARC_STRONG +#else +inline void arcSafeRelease( NSObject* ){} +inline id performOptionalSelector( id obj, SEL sel ) { +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Warc-performSelector-leaks" +#endif + if( [obj respondsToSelector: sel] ) + return [obj performSelector: sel]; +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + return nil; +} +#define CATCH_UNSAFE_UNRETAINED __unsafe_unretained +#define CATCH_ARC_STRONG __strong +#endif + +// end catch_objc_arc.hpp +#endif + +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable:4180) // We attempt to stream a function (address) by const&, which MSVC complains about but is harmless +#endif + +// We need a dummy global operator<< so we can bring it into Catch namespace later +struct Catch_global_namespace_dummy {}; +std::ostream& operator<<(std::ostream&, Catch_global_namespace_dummy); + +namespace Catch { + // Bring in operator<< from global namespace into Catch namespace + using ::operator<<; + + namespace Detail { + + extern const std::string unprintableString; + + std::string rawMemoryToString( const void *object, std::size_t size ); + + template + std::string rawMemoryToString( const T& object ) { + return rawMemoryToString( &object, sizeof(object) ); + } + + template + class IsStreamInsertable { + template + static auto test(int) + -> decltype(std::declval() << std::declval(), std::true_type()); + + template + static auto test(...)->std::false_type; + + public: + static const bool value = decltype(test(0))::value; + }; + + template + std::string convertUnknownEnumToString( E e ); + + template + typename std::enable_if< + !std::is_enum::value && !std::is_base_of::value, + std::string>::type convertUnstreamable( T const& ) { + return Detail::unprintableString; + } + template + typename std::enable_if< + !std::is_enum::value && std::is_base_of::value, + std::string>::type convertUnstreamable(T const& ex) { + return ex.what(); + } + + template + typename std::enable_if< + std::is_enum::value + , std::string>::type convertUnstreamable( T const& value ) { + return convertUnknownEnumToString( value ); + } + +#if defined(_MANAGED) + //! Convert a CLR string to a utf8 std::string + template + std::string clrReferenceToString( T^ ref ) { + if (ref == nullptr) + return std::string("null"); + auto bytes = System::Text::Encoding::UTF8->GetBytes(ref->ToString()); + cli::pin_ptr p = &bytes[0]; + return std::string(reinterpret_cast(p), bytes->Length); + } +#endif + + } // namespace Detail + + // If we decide for C++14, change these to enable_if_ts + template + struct StringMaker { + template + static + typename std::enable_if<::Catch::Detail::IsStreamInsertable::value, std::string>::type + convert(const Fake& value) { + ReusableStringStream rss; + // NB: call using the function-like syntax to avoid ambiguity with + // user-defined templated operator<< under clang. + rss.operator<<(value); + return rss.str(); + } + + template + static + typename std::enable_if::value, std::string>::type + convert( const Fake& value ) { +#if !defined(CATCH_CONFIG_FALLBACK_STRINGIFIER) + return Detail::convertUnstreamable(value); +#else + return CATCH_CONFIG_FALLBACK_STRINGIFIER(value); +#endif + } + }; + + namespace Detail { + + // This function dispatches all stringification requests inside of Catch. + // Should be preferably called fully qualified, like ::Catch::Detail::stringify + template + std::string stringify(const T& e) { + return ::Catch::StringMaker::type>::type>::convert(e); + } + + template + std::string convertUnknownEnumToString( E e ) { + return ::Catch::Detail::stringify(static_cast::type>(e)); + } + +#if defined(_MANAGED) + template + std::string stringify( T^ e ) { + return ::Catch::StringMaker::convert(e); + } +#endif + + } // namespace Detail + + // Some predefined specializations + + template<> + struct StringMaker { + static std::string convert(const std::string& str); + }; +#ifdef CATCH_CONFIG_WCHAR + template<> + struct StringMaker { + static std::string convert(const std::wstring& wstr); + }; +#endif + + template<> + struct StringMaker { + static std::string convert(char const * str); + }; + template<> + struct StringMaker { + static std::string convert(char * str); + }; + +#ifdef CATCH_CONFIG_WCHAR + template<> + struct StringMaker { + static std::string convert(wchar_t const * str); + }; + template<> + struct StringMaker { + static std::string convert(wchar_t * str); + }; +#endif + + // TBD: Should we use `strnlen` to ensure that we don't go out of the buffer, + // while keeping string semantics? + template + struct StringMaker { + static std::string convert(char const* str) { + return ::Catch::Detail::stringify(std::string{ str }); + } + }; + template + struct StringMaker { + static std::string convert(signed char const* str) { + return ::Catch::Detail::stringify(std::string{ reinterpret_cast(str) }); + } + }; + template + struct StringMaker { + static std::string convert(unsigned char const* str) { + return ::Catch::Detail::stringify(std::string{ reinterpret_cast(str) }); + } + }; + + template<> + struct StringMaker { + static std::string convert(int value); + }; + template<> + struct StringMaker { + static std::string convert(long value); + }; + template<> + struct StringMaker { + static std::string convert(long long value); + }; + template<> + struct StringMaker { + static std::string convert(unsigned int value); + }; + template<> + struct StringMaker { + static std::string convert(unsigned long value); + }; + template<> + struct StringMaker { + static std::string convert(unsigned long long value); + }; + + template<> + struct StringMaker { + static std::string convert(bool b); + }; + + template<> + struct StringMaker { + static std::string convert(char c); + }; + template<> + struct StringMaker { + static std::string convert(signed char c); + }; + template<> + struct StringMaker { + static std::string convert(unsigned char c); + }; + + template<> + struct StringMaker { + static std::string convert(std::nullptr_t); + }; + + template<> + struct StringMaker { + static std::string convert(float value); + }; + template<> + struct StringMaker { + static std::string convert(double value); + }; + + template + struct StringMaker { + template + static std::string convert(U* p) { + if (p) { + return ::Catch::Detail::rawMemoryToString(p); + } else { + return "nullptr"; + } + } + }; + + template + struct StringMaker { + static std::string convert(R C::* p) { + if (p) { + return ::Catch::Detail::rawMemoryToString(p); + } else { + return "nullptr"; + } + } + }; + +#if defined(_MANAGED) + template + struct StringMaker { + static std::string convert( T^ ref ) { + return ::Catch::Detail::clrReferenceToString(ref); + } + }; +#endif + + namespace Detail { + template + std::string rangeToString(InputIterator first, InputIterator last) { + ReusableStringStream rss; + rss << "{ "; + if (first != last) { + rss << ::Catch::Detail::stringify(*first); + for (++first; first != last; ++first) + rss << ", " << ::Catch::Detail::stringify(*first); + } + rss << " }"; + return rss.str(); + } + } + +#ifdef __OBJC__ + template<> + struct StringMaker { + static std::string convert(NSString * nsstring) { + if (!nsstring) + return "nil"; + return std::string("@") + [nsstring UTF8String]; + } + }; + template<> + struct StringMaker { + static std::string convert(NSObject* nsObject) { + return ::Catch::Detail::stringify([nsObject description]); + } + + }; + namespace Detail { + inline std::string stringify( NSString* nsstring ) { + return StringMaker::convert( nsstring ); + } + + } // namespace Detail +#endif // __OBJC__ + +} // namespace Catch + +////////////////////////////////////////////////////// +// Separate std-lib types stringification, so it can be selectively enabled +// This means that we do not bring in + +#if defined(CATCH_CONFIG_ENABLE_ALL_STRINGMAKERS) +# define CATCH_CONFIG_ENABLE_PAIR_STRINGMAKER +# define CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER +# define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER +#endif + +// Separate std::pair specialization +#if defined(CATCH_CONFIG_ENABLE_PAIR_STRINGMAKER) +#include +namespace Catch { + template + struct StringMaker > { + static std::string convert(const std::pair& pair) { + ReusableStringStream rss; + rss << "{ " + << ::Catch::Detail::stringify(pair.first) + << ", " + << ::Catch::Detail::stringify(pair.second) + << " }"; + return rss.str(); + } + }; +} +#endif // CATCH_CONFIG_ENABLE_PAIR_STRINGMAKER + +// Separate std::tuple specialization +#if defined(CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER) +#include +namespace Catch { + namespace Detail { + template< + typename Tuple, + std::size_t N = 0, + bool = (N < std::tuple_size::value) + > + struct TupleElementPrinter { + static void print(const Tuple& tuple, std::ostream& os) { + os << (N ? ", " : " ") + << ::Catch::Detail::stringify(std::get(tuple)); + TupleElementPrinter::print(tuple, os); + } + }; + + template< + typename Tuple, + std::size_t N + > + struct TupleElementPrinter { + static void print(const Tuple&, std::ostream&) {} + }; + + } + + template + struct StringMaker> { + static std::string convert(const std::tuple& tuple) { + ReusableStringStream rss; + rss << '{'; + Detail::TupleElementPrinter>::print(tuple, rss.get()); + rss << " }"; + return rss.str(); + } + }; +} +#endif // CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER + +namespace Catch { + struct not_this_one {}; // Tag type for detecting which begin/ end are being selected + + // Import begin/ end from std here so they are considered alongside the fallback (...) overloads in this namespace + using std::begin; + using std::end; + + not_this_one begin( ... ); + not_this_one end( ... ); + + template + struct is_range { + static const bool value = + !std::is_same())), not_this_one>::value && + !std::is_same())), not_this_one>::value; + }; + +#if defined(_MANAGED) // Managed types are never ranges + template + struct is_range { + static const bool value = false; + }; +#endif + + template + std::string rangeToString( Range const& range ) { + return ::Catch::Detail::rangeToString( begin( range ), end( range ) ); + } + + // Handle vector specially + template + std::string rangeToString( std::vector const& v ) { + ReusableStringStream rss; + rss << "{ "; + bool first = true; + for( bool b : v ) { + if( first ) + first = false; + else + rss << ", "; + rss << ::Catch::Detail::stringify( b ); + } + rss << " }"; + return rss.str(); + } + + template + struct StringMaker::value && !::Catch::Detail::IsStreamInsertable::value>::type> { + static std::string convert( R const& range ) { + return rangeToString( range ); + } + }; + + template + struct StringMaker { + static std::string convert(T const(&arr)[SZ]) { + return rangeToString(arr); + } + }; + +} // namespace Catch + +// Separate std::chrono::duration specialization +#if defined(CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER) +#include +#include +#include + +namespace Catch { + +template +struct ratio_string { + static std::string symbol(); +}; + +template +std::string ratio_string::symbol() { + Catch::ReusableStringStream rss; + rss << '[' << Ratio::num << '/' + << Ratio::den << ']'; + return rss.str(); +} +template <> +struct ratio_string { + static std::string symbol(); +}; +template <> +struct ratio_string { + static std::string symbol(); +}; +template <> +struct ratio_string { + static std::string symbol(); +}; +template <> +struct ratio_string { + static std::string symbol(); +}; +template <> +struct ratio_string { + static std::string symbol(); +}; +template <> +struct ratio_string { + static std::string symbol(); +}; + + //////////// + // std::chrono::duration specializations + template + struct StringMaker> { + static std::string convert(std::chrono::duration const& duration) { + ReusableStringStream rss; + rss << duration.count() << ' ' << ratio_string::symbol() << 's'; + return rss.str(); + } + }; + template + struct StringMaker>> { + static std::string convert(std::chrono::duration> const& duration) { + ReusableStringStream rss; + rss << duration.count() << " s"; + return rss.str(); + } + }; + template + struct StringMaker>> { + static std::string convert(std::chrono::duration> const& duration) { + ReusableStringStream rss; + rss << duration.count() << " m"; + return rss.str(); + } + }; + template + struct StringMaker>> { + static std::string convert(std::chrono::duration> const& duration) { + ReusableStringStream rss; + rss << duration.count() << " h"; + return rss.str(); + } + }; + + //////////// + // std::chrono::time_point specialization + // Generic time_point cannot be specialized, only std::chrono::time_point + template + struct StringMaker> { + static std::string convert(std::chrono::time_point const& time_point) { + return ::Catch::Detail::stringify(time_point.time_since_epoch()) + " since epoch"; + } + }; + // std::chrono::time_point specialization + template + struct StringMaker> { + static std::string convert(std::chrono::time_point const& time_point) { + auto converted = std::chrono::system_clock::to_time_t(time_point); + +#ifdef _MSC_VER + std::tm timeInfo = {}; + gmtime_s(&timeInfo, &converted); +#else + std::tm* timeInfo = std::gmtime(&converted); +#endif + + auto const timeStampSize = sizeof("2017-01-16T17:06:45Z"); + char timeStamp[timeStampSize]; + const char * const fmt = "%Y-%m-%dT%H:%M:%SZ"; + +#ifdef _MSC_VER + std::strftime(timeStamp, timeStampSize, fmt, &timeInfo); +#else + std::strftime(timeStamp, timeStampSize, fmt, timeInfo); +#endif + return std::string(timeStamp); + } + }; +} +#endif // CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER + +#ifdef _MSC_VER +#pragma warning(pop) +#endif + +// end catch_tostring.h +#include + +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable:4389) // '==' : signed/unsigned mismatch +#pragma warning(disable:4018) // more "signed/unsigned mismatch" +#pragma warning(disable:4312) // Converting int to T* using reinterpret_cast (issue on x64 platform) +#pragma warning(disable:4180) // qualifier applied to function type has no meaning +#endif + +namespace Catch { + + struct ITransientExpression { + auto isBinaryExpression() const -> bool { return m_isBinaryExpression; } + auto getResult() const -> bool { return m_result; } + virtual void streamReconstructedExpression( std::ostream &os ) const = 0; + + ITransientExpression( bool isBinaryExpression, bool result ) + : m_isBinaryExpression( isBinaryExpression ), + m_result( result ) + {} + + // We don't actually need a virtual destructor, but many static analysers + // complain if it's not here :-( + virtual ~ITransientExpression(); + + bool m_isBinaryExpression; + bool m_result; + + }; + + void formatReconstructedExpression( std::ostream &os, std::string const& lhs, StringRef op, std::string const& rhs ); + + template + class BinaryExpr : public ITransientExpression { + LhsT m_lhs; + StringRef m_op; + RhsT m_rhs; + + void streamReconstructedExpression( std::ostream &os ) const override { + formatReconstructedExpression + ( os, Catch::Detail::stringify( m_lhs ), m_op, Catch::Detail::stringify( m_rhs ) ); + } + + public: + BinaryExpr( bool comparisonResult, LhsT lhs, StringRef op, RhsT rhs ) + : ITransientExpression{ true, comparisonResult }, + m_lhs( lhs ), + m_op( op ), + m_rhs( rhs ) + {} + }; + + template + class UnaryExpr : public ITransientExpression { + LhsT m_lhs; + + void streamReconstructedExpression( std::ostream &os ) const override { + os << Catch::Detail::stringify( m_lhs ); + } + + public: + explicit UnaryExpr( LhsT lhs ) + : ITransientExpression{ false, lhs ? true : false }, + m_lhs( lhs ) + {} + }; + + // Specialised comparison functions to handle equality comparisons between ints and pointers (NULL deduces as an int) + template + auto compareEqual( LhsT const& lhs, RhsT const& rhs ) -> bool { return static_cast(lhs == rhs); } + template + auto compareEqual( T* const& lhs, int rhs ) -> bool { return lhs == reinterpret_cast( rhs ); } + template + auto compareEqual( T* const& lhs, long rhs ) -> bool { return lhs == reinterpret_cast( rhs ); } + template + auto compareEqual( int lhs, T* const& rhs ) -> bool { return reinterpret_cast( lhs ) == rhs; } + template + auto compareEqual( long lhs, T* const& rhs ) -> bool { return reinterpret_cast( lhs ) == rhs; } + + template + auto compareNotEqual( LhsT const& lhs, RhsT&& rhs ) -> bool { return static_cast(lhs != rhs); } + template + auto compareNotEqual( T* const& lhs, int rhs ) -> bool { return lhs != reinterpret_cast( rhs ); } + template + auto compareNotEqual( T* const& lhs, long rhs ) -> bool { return lhs != reinterpret_cast( rhs ); } + template + auto compareNotEqual( int lhs, T* const& rhs ) -> bool { return reinterpret_cast( lhs ) != rhs; } + template + auto compareNotEqual( long lhs, T* const& rhs ) -> bool { return reinterpret_cast( lhs ) != rhs; } + + template + class ExprLhs { + LhsT m_lhs; + public: + explicit ExprLhs( LhsT lhs ) : m_lhs( lhs ) {} + + template + auto operator == ( RhsT const& rhs ) -> BinaryExpr const { + return { compareEqual( m_lhs, rhs ), m_lhs, "==", rhs }; + } + auto operator == ( bool rhs ) -> BinaryExpr const { + return { m_lhs == rhs, m_lhs, "==", rhs }; + } + + template + auto operator != ( RhsT const& rhs ) -> BinaryExpr const { + return { compareNotEqual( m_lhs, rhs ), m_lhs, "!=", rhs }; + } + auto operator != ( bool rhs ) -> BinaryExpr const { + return { m_lhs != rhs, m_lhs, "!=", rhs }; + } + + template + auto operator > ( RhsT const& rhs ) -> BinaryExpr const { + return { static_cast(m_lhs > rhs), m_lhs, ">", rhs }; + } + template + auto operator < ( RhsT const& rhs ) -> BinaryExpr const { + return { static_cast(m_lhs < rhs), m_lhs, "<", rhs }; + } + template + auto operator >= ( RhsT const& rhs ) -> BinaryExpr const { + return { static_cast(m_lhs >= rhs), m_lhs, ">=", rhs }; + } + template + auto operator <= ( RhsT const& rhs ) -> BinaryExpr const { + return { static_cast(m_lhs <= rhs), m_lhs, "<=", rhs }; + } + + auto makeUnaryExpr() const -> UnaryExpr { + return UnaryExpr{ m_lhs }; + } + }; + + void handleExpression( ITransientExpression const& expr ); + + template + void handleExpression( ExprLhs const& expr ) { + handleExpression( expr.makeUnaryExpr() ); + } + + struct Decomposer { + template + auto operator <= ( T const& lhs ) -> ExprLhs { + return ExprLhs{ lhs }; + } + + auto operator <=( bool value ) -> ExprLhs { + return ExprLhs{ value }; + } + }; + +} // end namespace Catch + +#ifdef _MSC_VER +#pragma warning(pop) +#endif + +// end catch_decomposer.h +// start catch_interfaces_capture.h + +#include + +namespace Catch { + + class AssertionResult; + struct AssertionInfo; + struct SectionInfo; + struct SectionEndInfo; + struct MessageInfo; + struct Counts; + struct BenchmarkInfo; + struct BenchmarkStats; + struct AssertionReaction; + + struct ITransientExpression; + + struct IResultCapture { + + virtual ~IResultCapture(); + + virtual bool sectionStarted( SectionInfo const& sectionInfo, + Counts& assertions ) = 0; + virtual void sectionEnded( SectionEndInfo const& endInfo ) = 0; + virtual void sectionEndedEarly( SectionEndInfo const& endInfo ) = 0; + + virtual void benchmarkStarting( BenchmarkInfo const& info ) = 0; + virtual void benchmarkEnded( BenchmarkStats const& stats ) = 0; + + virtual void pushScopedMessage( MessageInfo const& message ) = 0; + virtual void popScopedMessage( MessageInfo const& message ) = 0; + + virtual void handleFatalErrorCondition( StringRef message ) = 0; + + virtual void handleExpr + ( AssertionInfo const& info, + ITransientExpression const& expr, + AssertionReaction& reaction ) = 0; + virtual void handleMessage + ( AssertionInfo const& info, + ResultWas::OfType resultType, + StringRef const& message, + AssertionReaction& reaction ) = 0; + virtual void handleUnexpectedExceptionNotThrown + ( AssertionInfo const& info, + AssertionReaction& reaction ) = 0; + virtual void handleUnexpectedInflightException + ( AssertionInfo const& info, + std::string const& message, + AssertionReaction& reaction ) = 0; + virtual void handleIncomplete + ( AssertionInfo const& info ) = 0; + virtual void handleNonExpr + ( AssertionInfo const &info, + ResultWas::OfType resultType, + AssertionReaction &reaction ) = 0; + + virtual bool lastAssertionPassed() = 0; + virtual void assertionPassed() = 0; + + // Deprecated, do not use: + virtual std::string getCurrentTestName() const = 0; + virtual const AssertionResult* getLastResult() const = 0; + virtual void exceptionEarlyReported() = 0; + }; + + IResultCapture& getResultCapture(); +} + +// end catch_interfaces_capture.h +namespace Catch { + + struct TestFailureException{}; + struct AssertionResultData; + struct IResultCapture; + class RunContext; + + class LazyExpression { + friend class AssertionHandler; + friend struct AssertionStats; + friend class RunContext; + + ITransientExpression const* m_transientExpression = nullptr; + bool m_isNegated; + public: + LazyExpression( bool isNegated ); + LazyExpression( LazyExpression const& other ); + LazyExpression& operator = ( LazyExpression const& ) = delete; + + explicit operator bool() const; + + friend auto operator << ( std::ostream& os, LazyExpression const& lazyExpr ) -> std::ostream&; + }; + + struct AssertionReaction { + bool shouldDebugBreak = false; + bool shouldThrow = false; + }; + + class AssertionHandler { + AssertionInfo m_assertionInfo; + AssertionReaction m_reaction; + bool m_completed = false; + IResultCapture& m_resultCapture; + + public: + AssertionHandler + ( StringRef macroName, + SourceLineInfo const& lineInfo, + StringRef capturedExpression, + ResultDisposition::Flags resultDisposition ); + ~AssertionHandler() { + if ( !m_completed ) { + m_resultCapture.handleIncomplete( m_assertionInfo ); + } + } + + template + void handleExpr( ExprLhs const& expr ) { + handleExpr( expr.makeUnaryExpr() ); + } + void handleExpr( ITransientExpression const& expr ); + + void handleMessage(ResultWas::OfType resultType, StringRef const& message); + + void handleExceptionThrownAsExpected(); + void handleUnexpectedExceptionNotThrown(); + void handleExceptionNotThrownAsExpected(); + void handleThrowingCallSkipped(); + void handleUnexpectedInflightException(); + + void complete(); + void setCompleted(); + + // query + auto allowThrows() const -> bool; + }; + + void handleExceptionMatchExpr( AssertionHandler& handler, std::string const& str, StringRef matcherString ); + +} // namespace Catch + +// end catch_assertionhandler.h +// start catch_message.h + +#include + +namespace Catch { + + struct MessageInfo { + MessageInfo( std::string const& _macroName, + SourceLineInfo const& _lineInfo, + ResultWas::OfType _type ); + + std::string macroName; + std::string message; + SourceLineInfo lineInfo; + ResultWas::OfType type; + unsigned int sequence; + + bool operator == ( MessageInfo const& other ) const; + bool operator < ( MessageInfo const& other ) const; + private: + static unsigned int globalCount; + }; + + struct MessageStream { + + template + MessageStream& operator << ( T const& value ) { + m_stream << value; + return *this; + } + + ReusableStringStream m_stream; + }; + + struct MessageBuilder : MessageStream { + MessageBuilder( std::string const& macroName, + SourceLineInfo const& lineInfo, + ResultWas::OfType type ); + + template + MessageBuilder& operator << ( T const& value ) { + m_stream << value; + return *this; + } + + MessageInfo m_info; + }; + + class ScopedMessage { + public: + explicit ScopedMessage( MessageBuilder const& builder ); + ~ScopedMessage(); + + MessageInfo m_info; + }; + +} // end namespace Catch + +// end catch_message.h +#if !defined(CATCH_CONFIG_DISABLE) + +#if !defined(CATCH_CONFIG_DISABLE_STRINGIFICATION) + #define CATCH_INTERNAL_STRINGIFY(...) #__VA_ARGS__ +#else + #define CATCH_INTERNAL_STRINGIFY(...) "Disabled by CATCH_CONFIG_DISABLE_STRINGIFICATION" +#endif + +#if defined(CATCH_CONFIG_FAST_COMPILE) + +/////////////////////////////////////////////////////////////////////////////// +// Another way to speed-up compilation is to omit local try-catch for REQUIRE* +// macros. +#define INTERNAL_CATCH_TRY +#define INTERNAL_CATCH_CATCH( capturer ) + +#else // CATCH_CONFIG_FAST_COMPILE + +#define INTERNAL_CATCH_TRY try +#define INTERNAL_CATCH_CATCH( handler ) catch(...) { handler.handleUnexpectedInflightException(); } + +#endif + +#define INTERNAL_CATCH_REACT( handler ) handler.complete(); + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_TEST( macroName, resultDisposition, ... ) \ + do { \ + Catch::AssertionHandler catchAssertionHandler( macroName, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__), resultDisposition ); \ + INTERNAL_CATCH_TRY { \ + CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS \ + catchAssertionHandler.handleExpr( Catch::Decomposer() <= __VA_ARGS__ ); \ + CATCH_INTERNAL_UNSUPPRESS_PARENTHESES_WARNINGS \ + } INTERNAL_CATCH_CATCH( catchAssertionHandler ) \ + INTERNAL_CATCH_REACT( catchAssertionHandler ) \ + } while( (void)0, false && static_cast( !!(__VA_ARGS__) ) ) // the expression here is never evaluated at runtime but it forces the compiler to give it a look + // The double negation silences MSVC's C4800 warning, the static_cast forces short-circuit evaluation if the type has overloaded &&. + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_IF( macroName, resultDisposition, ... ) \ + INTERNAL_CATCH_TEST( macroName, resultDisposition, __VA_ARGS__ ); \ + if( Catch::getResultCapture().lastAssertionPassed() ) + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_ELSE( macroName, resultDisposition, ... ) \ + INTERNAL_CATCH_TEST( macroName, resultDisposition, __VA_ARGS__ ); \ + if( !Catch::getResultCapture().lastAssertionPassed() ) + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_NO_THROW( macroName, resultDisposition, ... ) \ + do { \ + Catch::AssertionHandler catchAssertionHandler( macroName, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__), resultDisposition ); \ + try { \ + static_cast(__VA_ARGS__); \ + catchAssertionHandler.handleExceptionNotThrownAsExpected(); \ + } \ + catch( ... ) { \ + catchAssertionHandler.handleUnexpectedInflightException(); \ + } \ + INTERNAL_CATCH_REACT( catchAssertionHandler ) \ + } while( false ) + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_THROWS( macroName, resultDisposition, ... ) \ + do { \ + Catch::AssertionHandler catchAssertionHandler( macroName, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__), resultDisposition); \ + if( catchAssertionHandler.allowThrows() ) \ + try { \ + static_cast(__VA_ARGS__); \ + catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \ + } \ + catch( ... ) { \ + catchAssertionHandler.handleExceptionThrownAsExpected(); \ + } \ + else \ + catchAssertionHandler.handleThrowingCallSkipped(); \ + INTERNAL_CATCH_REACT( catchAssertionHandler ) \ + } while( false ) + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_THROWS_AS( macroName, exceptionType, resultDisposition, expr ) \ + do { \ + Catch::AssertionHandler catchAssertionHandler( macroName, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(expr) ", " CATCH_INTERNAL_STRINGIFY(exceptionType), resultDisposition ); \ + if( catchAssertionHandler.allowThrows() ) \ + try { \ + static_cast(expr); \ + catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \ + } \ + catch( exceptionType const& ) { \ + catchAssertionHandler.handleExceptionThrownAsExpected(); \ + } \ + catch( ... ) { \ + catchAssertionHandler.handleUnexpectedInflightException(); \ + } \ + else \ + catchAssertionHandler.handleThrowingCallSkipped(); \ + INTERNAL_CATCH_REACT( catchAssertionHandler ) \ + } while( false ) + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_MSG( macroName, messageType, resultDisposition, ... ) \ + do { \ + Catch::AssertionHandler catchAssertionHandler( macroName, CATCH_INTERNAL_LINEINFO, "", resultDisposition ); \ + catchAssertionHandler.handleMessage( messageType, ( Catch::MessageStream() << __VA_ARGS__ + ::Catch::StreamEndStop() ).m_stream.str() ); \ + INTERNAL_CATCH_REACT( catchAssertionHandler ) \ + } while( false ) + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_INFO( macroName, log ) \ + Catch::ScopedMessage INTERNAL_CATCH_UNIQUE_NAME( scopedMessage )( Catch::MessageBuilder( macroName, CATCH_INTERNAL_LINEINFO, Catch::ResultWas::Info ) << log ); + +/////////////////////////////////////////////////////////////////////////////// +// Although this is matcher-based, it can be used with just a string +#define INTERNAL_CATCH_THROWS_STR_MATCHES( macroName, resultDisposition, matcher, ... ) \ + do { \ + Catch::AssertionHandler catchAssertionHandler( macroName, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__) ", " CATCH_INTERNAL_STRINGIFY(matcher), resultDisposition ); \ + if( catchAssertionHandler.allowThrows() ) \ + try { \ + static_cast(__VA_ARGS__); \ + catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \ + } \ + catch( ... ) { \ + Catch::handleExceptionMatchExpr( catchAssertionHandler, matcher, #matcher ); \ + } \ + else \ + catchAssertionHandler.handleThrowingCallSkipped(); \ + INTERNAL_CATCH_REACT( catchAssertionHandler ) \ + } while( false ) + +#endif // CATCH_CONFIG_DISABLE + +// end catch_capture.hpp +// start catch_section.h + +// start catch_section_info.h + +// start catch_totals.h + +#include + +namespace Catch { + + struct Counts { + Counts operator - ( Counts const& other ) const; + Counts& operator += ( Counts const& other ); + + std::size_t total() const; + bool allPassed() const; + bool allOk() const; + + std::size_t passed = 0; + std::size_t failed = 0; + std::size_t failedButOk = 0; + }; + + struct Totals { + + Totals operator - ( Totals const& other ) const; + Totals& operator += ( Totals const& other ); + + Totals delta( Totals const& prevTotals ) const; + + int error = 0; + Counts assertions; + Counts testCases; + }; +} + +// end catch_totals.h +#include + +namespace Catch { + + struct SectionInfo { + SectionInfo + ( SourceLineInfo const& _lineInfo, + std::string const& _name, + std::string const& _description = std::string() ); + + std::string name; + std::string description; + SourceLineInfo lineInfo; + }; + + struct SectionEndInfo { + SectionEndInfo( SectionInfo const& _sectionInfo, Counts const& _prevAssertions, double _durationInSeconds ); + + SectionInfo sectionInfo; + Counts prevAssertions; + double durationInSeconds; + }; + +} // end namespace Catch + +// end catch_section_info.h +// start catch_timer.h + +#include + +namespace Catch { + + auto getCurrentNanosecondsSinceEpoch() -> uint64_t; + auto getEstimatedClockResolution() -> uint64_t; + + class Timer { + uint64_t m_nanoseconds = 0; + public: + void start(); + auto getElapsedNanoseconds() const -> uint64_t; + auto getElapsedMicroseconds() const -> uint64_t; + auto getElapsedMilliseconds() const -> unsigned int; + auto getElapsedSeconds() const -> double; + }; + +} // namespace Catch + +// end catch_timer.h +#include + +namespace Catch { + + class Section : NonCopyable { + public: + Section( SectionInfo const& info ); + ~Section(); + + // This indicates whether the section should be executed or not + explicit operator bool() const; + + private: + SectionInfo m_info; + + std::string m_name; + Counts m_assertions; + bool m_sectionIncluded; + Timer m_timer; + }; + +} // end namespace Catch + + #define INTERNAL_CATCH_SECTION( ... ) \ + if( Catch::Section const& INTERNAL_CATCH_UNIQUE_NAME( catch_internal_Section ) = Catch::SectionInfo( CATCH_INTERNAL_LINEINFO, __VA_ARGS__ ) ) + +// end catch_section.h +// start catch_benchmark.h + +#include +#include + +namespace Catch { + + class BenchmarkLooper { + + std::string m_name; + std::size_t m_count = 0; + std::size_t m_iterationsToRun = 1; + uint64_t m_resolution; + Timer m_timer; + + static auto getResolution() -> uint64_t; + public: + // Keep most of this inline as it's on the code path that is being timed + BenchmarkLooper( StringRef name ) + : m_name( name ), + m_resolution( getResolution() ) + { + reportStart(); + m_timer.start(); + } + + explicit operator bool() { + if( m_count < m_iterationsToRun ) + return true; + return needsMoreIterations(); + } + + void increment() { + ++m_count; + } + + void reportStart(); + auto needsMoreIterations() -> bool; + }; + +} // end namespace Catch + +#define BENCHMARK( name ) \ + for( Catch::BenchmarkLooper looper( name ); looper; looper.increment() ) + +// end catch_benchmark.h +// start catch_interfaces_exception.h + +// start catch_interfaces_registry_hub.h + +#include +#include + +namespace Catch { + + class TestCase; + struct ITestCaseRegistry; + struct IExceptionTranslatorRegistry; + struct IExceptionTranslator; + struct IReporterRegistry; + struct IReporterFactory; + struct ITagAliasRegistry; + class StartupExceptionRegistry; + + using IReporterFactoryPtr = std::shared_ptr; + + struct IRegistryHub { + virtual ~IRegistryHub(); + + virtual IReporterRegistry const& getReporterRegistry() const = 0; + virtual ITestCaseRegistry const& getTestCaseRegistry() const = 0; + virtual ITagAliasRegistry const& getTagAliasRegistry() const = 0; + + virtual IExceptionTranslatorRegistry& getExceptionTranslatorRegistry() = 0; + + virtual StartupExceptionRegistry const& getStartupExceptionRegistry() const = 0; + }; + + struct IMutableRegistryHub { + virtual ~IMutableRegistryHub(); + virtual void registerReporter( std::string const& name, IReporterFactoryPtr const& factory ) = 0; + virtual void registerListener( IReporterFactoryPtr const& factory ) = 0; + virtual void registerTest( TestCase const& testInfo ) = 0; + virtual void registerTranslator( const IExceptionTranslator* translator ) = 0; + virtual void registerTagAlias( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) = 0; + virtual void registerStartupException() noexcept = 0; + }; + + IRegistryHub& getRegistryHub(); + IMutableRegistryHub& getMutableRegistryHub(); + void cleanUp(); + std::string translateActiveException(); + +} + +// end catch_interfaces_registry_hub.h +#if defined(CATCH_CONFIG_DISABLE) + #define INTERNAL_CATCH_TRANSLATE_EXCEPTION_NO_REG( translatorName, signature) \ + static std::string translatorName( signature ) +#endif + +#include +#include +#include + +namespace Catch { + using exceptionTranslateFunction = std::string(*)(); + + struct IExceptionTranslator; + using ExceptionTranslators = std::vector>; + + struct IExceptionTranslator { + virtual ~IExceptionTranslator(); + virtual std::string translate( ExceptionTranslators::const_iterator it, ExceptionTranslators::const_iterator itEnd ) const = 0; + }; + + struct IExceptionTranslatorRegistry { + virtual ~IExceptionTranslatorRegistry(); + + virtual std::string translateActiveException() const = 0; + }; + + class ExceptionTranslatorRegistrar { + template + class ExceptionTranslator : public IExceptionTranslator { + public: + + ExceptionTranslator( std::string(*translateFunction)( T& ) ) + : m_translateFunction( translateFunction ) + {} + + std::string translate( ExceptionTranslators::const_iterator it, ExceptionTranslators::const_iterator itEnd ) const override { + try { + if( it == itEnd ) + std::rethrow_exception(std::current_exception()); + else + return (*it)->translate( it+1, itEnd ); + } + catch( T& ex ) { + return m_translateFunction( ex ); + } + } + + protected: + std::string(*m_translateFunction)( T& ); + }; + + public: + template + ExceptionTranslatorRegistrar( std::string(*translateFunction)( T& ) ) { + getMutableRegistryHub().registerTranslator + ( new ExceptionTranslator( translateFunction ) ); + } + }; +} + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_TRANSLATE_EXCEPTION2( translatorName, signature ) \ + static std::string translatorName( signature ); \ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + namespace{ Catch::ExceptionTranslatorRegistrar INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionRegistrar )( &translatorName ); } \ + CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS \ + static std::string translatorName( signature ) + +#define INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION2( INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ), signature ) + +// end catch_interfaces_exception.h +// start catch_approx.h + +#include +#include + +namespace Catch { +namespace Detail { + + class Approx { + private: + bool equalityComparisonImpl(double other) const; + + public: + explicit Approx ( double value ); + + static Approx custom(); + + template ::value>::type> + Approx operator()( T const& value ) { + Approx approx( static_cast(value) ); + approx.epsilon( m_epsilon ); + approx.margin( m_margin ); + approx.scale( m_scale ); + return approx; + } + + template ::value>::type> + explicit Approx( T const& value ): Approx(static_cast(value)) + {} + + template ::value>::type> + friend bool operator == ( const T& lhs, Approx const& rhs ) { + auto lhs_v = static_cast(lhs); + return rhs.equalityComparisonImpl(lhs_v); + } + + template ::value>::type> + friend bool operator == ( Approx const& lhs, const T& rhs ) { + return operator==( rhs, lhs ); + } + + template ::value>::type> + friend bool operator != ( T const& lhs, Approx const& rhs ) { + return !operator==( lhs, rhs ); + } + + template ::value>::type> + friend bool operator != ( Approx const& lhs, T const& rhs ) { + return !operator==( rhs, lhs ); + } + + template ::value>::type> + friend bool operator <= ( T const& lhs, Approx const& rhs ) { + return static_cast(lhs) < rhs.m_value || lhs == rhs; + } + + template ::value>::type> + friend bool operator <= ( Approx const& lhs, T const& rhs ) { + return lhs.m_value < static_cast(rhs) || lhs == rhs; + } + + template ::value>::type> + friend bool operator >= ( T const& lhs, Approx const& rhs ) { + return static_cast(lhs) > rhs.m_value || lhs == rhs; + } + + template ::value>::type> + friend bool operator >= ( Approx const& lhs, T const& rhs ) { + return lhs.m_value > static_cast(rhs) || lhs == rhs; + } + + template ::value>::type> + Approx& epsilon( T const& newEpsilon ) { + double epsilonAsDouble = static_cast(newEpsilon); + if( epsilonAsDouble < 0 || epsilonAsDouble > 1.0 ) { + throw std::domain_error + ( "Invalid Approx::epsilon: " + + Catch::Detail::stringify( epsilonAsDouble ) + + ", Approx::epsilon has to be between 0 and 1" ); + } + m_epsilon = epsilonAsDouble; + return *this; + } + + template ::value>::type> + Approx& margin( T const& newMargin ) { + double marginAsDouble = static_cast(newMargin); + if( marginAsDouble < 0 ) { + throw std::domain_error + ( "Invalid Approx::margin: " + + Catch::Detail::stringify( marginAsDouble ) + + ", Approx::Margin has to be non-negative." ); + + } + m_margin = marginAsDouble; + return *this; + } + + template ::value>::type> + Approx& scale( T const& newScale ) { + m_scale = static_cast(newScale); + return *this; + } + + std::string toString() const; + + private: + double m_epsilon; + double m_margin; + double m_scale; + double m_value; + }; +} + +template<> +struct StringMaker { + static std::string convert(Catch::Detail::Approx const& value); +}; + +} // end namespace Catch + +// end catch_approx.h +// start catch_string_manip.h + +#include +#include + +namespace Catch { + + bool startsWith( std::string const& s, std::string const& prefix ); + bool startsWith( std::string const& s, char prefix ); + bool endsWith( std::string const& s, std::string const& suffix ); + bool endsWith( std::string const& s, char suffix ); + bool contains( std::string const& s, std::string const& infix ); + void toLowerInPlace( std::string& s ); + std::string toLower( std::string const& s ); + std::string trim( std::string const& str ); + bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis ); + + struct pluralise { + pluralise( std::size_t count, std::string const& label ); + + friend std::ostream& operator << ( std::ostream& os, pluralise const& pluraliser ); + + std::size_t m_count; + std::string m_label; + }; +} + +// end catch_string_manip.h +#ifndef CATCH_CONFIG_DISABLE_MATCHERS +// start catch_capture_matchers.h + +// start catch_matchers.h + +#include +#include + +namespace Catch { +namespace Matchers { + namespace Impl { + + template struct MatchAllOf; + template struct MatchAnyOf; + template struct MatchNotOf; + + class MatcherUntypedBase { + public: + MatcherUntypedBase() = default; + MatcherUntypedBase ( MatcherUntypedBase const& ) = default; + MatcherUntypedBase& operator = ( MatcherUntypedBase const& ) = delete; + std::string toString() const; + + protected: + virtual ~MatcherUntypedBase(); + virtual std::string describe() const = 0; + mutable std::string m_cachedToString; + }; + + template + struct MatcherMethod { + virtual bool match( ObjectT const& arg ) const = 0; + }; + template + struct MatcherMethod { + virtual bool match( PtrT* arg ) const = 0; + }; + + template + struct MatcherBase : MatcherUntypedBase, MatcherMethod { + + MatchAllOf operator && ( MatcherBase const& other ) const; + MatchAnyOf operator || ( MatcherBase const& other ) const; + MatchNotOf operator ! () const; + }; + + template + struct MatchAllOf : MatcherBase { + bool match( ArgT const& arg ) const override { + for( auto matcher : m_matchers ) { + if (!matcher->match(arg)) + return false; + } + return true; + } + std::string describe() const override { + std::string description; + description.reserve( 4 + m_matchers.size()*32 ); + description += "( "; + bool first = true; + for( auto matcher : m_matchers ) { + if( first ) + first = false; + else + description += " and "; + description += matcher->toString(); + } + description += " )"; + return description; + } + + MatchAllOf& operator && ( MatcherBase const& other ) { + m_matchers.push_back( &other ); + return *this; + } + + std::vector const*> m_matchers; + }; + template + struct MatchAnyOf : MatcherBase { + + bool match( ArgT const& arg ) const override { + for( auto matcher : m_matchers ) { + if (matcher->match(arg)) + return true; + } + return false; + } + std::string describe() const override { + std::string description; + description.reserve( 4 + m_matchers.size()*32 ); + description += "( "; + bool first = true; + for( auto matcher : m_matchers ) { + if( first ) + first = false; + else + description += " or "; + description += matcher->toString(); + } + description += " )"; + return description; + } + + MatchAnyOf& operator || ( MatcherBase const& other ) { + m_matchers.push_back( &other ); + return *this; + } + + std::vector const*> m_matchers; + }; + + template + struct MatchNotOf : MatcherBase { + + MatchNotOf( MatcherBase const& underlyingMatcher ) : m_underlyingMatcher( underlyingMatcher ) {} + + bool match( ArgT const& arg ) const override { + return !m_underlyingMatcher.match( arg ); + } + + std::string describe() const override { + return "not " + m_underlyingMatcher.toString(); + } + MatcherBase const& m_underlyingMatcher; + }; + + template + MatchAllOf MatcherBase::operator && ( MatcherBase const& other ) const { + return MatchAllOf() && *this && other; + } + template + MatchAnyOf MatcherBase::operator || ( MatcherBase const& other ) const { + return MatchAnyOf() || *this || other; + } + template + MatchNotOf MatcherBase::operator ! () const { + return MatchNotOf( *this ); + } + + } // namespace Impl + +} // namespace Matchers + +using namespace Matchers; +using Matchers::Impl::MatcherBase; + +} // namespace Catch + +// end catch_matchers.h +// start catch_matchers_floating.h + +#include +#include + +namespace Catch { +namespace Matchers { + + namespace Floating { + + enum class FloatingPointKind : uint8_t; + + struct WithinAbsMatcher : MatcherBase { + WithinAbsMatcher(double target, double margin); + bool match(double const& matchee) const override; + std::string describe() const override; + private: + double m_target; + double m_margin; + }; + + struct WithinUlpsMatcher : MatcherBase { + WithinUlpsMatcher(double target, int ulps, FloatingPointKind baseType); + bool match(double const& matchee) const override; + std::string describe() const override; + private: + double m_target; + int m_ulps; + FloatingPointKind m_type; + }; + + } // namespace Floating + + // The following functions create the actual matcher objects. + // This allows the types to be inferred + Floating::WithinUlpsMatcher WithinULP(double target, int maxUlpDiff); + Floating::WithinUlpsMatcher WithinULP(float target, int maxUlpDiff); + Floating::WithinAbsMatcher WithinAbs(double target, double margin); + +} // namespace Matchers +} // namespace Catch + +// end catch_matchers_floating.h +// start catch_matchers_generic.hpp + +#include +#include + +namespace Catch { +namespace Matchers { +namespace Generic { + +namespace Detail { + std::string finalizeDescription(const std::string& desc); +} + +template +class PredicateMatcher : public MatcherBase { + std::function m_predicate; + std::string m_description; +public: + + PredicateMatcher(std::function const& elem, std::string const& descr) + :m_predicate(std::move(elem)), + m_description(Detail::finalizeDescription(descr)) + {} + + bool match( T const& item ) const override { + return m_predicate(item); + } + + std::string describe() const override { + return m_description; + } +}; + +} // namespace Generic + + // The following functions create the actual matcher objects. + // The user has to explicitly specify type to the function, because + // infering std::function is hard (but possible) and + // requires a lot of TMP. + template + Generic::PredicateMatcher Predicate(std::function const& predicate, std::string const& description = "") { + return Generic::PredicateMatcher(predicate, description); + } + +} // namespace Matchers +} // namespace Catch + +// end catch_matchers_generic.hpp +// start catch_matchers_string.h + +#include + +namespace Catch { +namespace Matchers { + + namespace StdString { + + struct CasedString + { + CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity ); + std::string adjustString( std::string const& str ) const; + std::string caseSensitivitySuffix() const; + + CaseSensitive::Choice m_caseSensitivity; + std::string m_str; + }; + + struct StringMatcherBase : MatcherBase { + StringMatcherBase( std::string const& operation, CasedString const& comparator ); + std::string describe() const override; + + CasedString m_comparator; + std::string m_operation; + }; + + struct EqualsMatcher : StringMatcherBase { + EqualsMatcher( CasedString const& comparator ); + bool match( std::string const& source ) const override; + }; + struct ContainsMatcher : StringMatcherBase { + ContainsMatcher( CasedString const& comparator ); + bool match( std::string const& source ) const override; + }; + struct StartsWithMatcher : StringMatcherBase { + StartsWithMatcher( CasedString const& comparator ); + bool match( std::string const& source ) const override; + }; + struct EndsWithMatcher : StringMatcherBase { + EndsWithMatcher( CasedString const& comparator ); + bool match( std::string const& source ) const override; + }; + + struct RegexMatcher : MatcherBase { + RegexMatcher( std::string regex, CaseSensitive::Choice caseSensitivity ); + bool match( std::string const& matchee ) const override; + std::string describe() const override; + + private: + std::string m_regex; + CaseSensitive::Choice m_caseSensitivity; + }; + + } // namespace StdString + + // The following functions create the actual matcher objects. + // This allows the types to be inferred + + StdString::EqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); + StdString::ContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); + StdString::EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); + StdString::StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); + StdString::RegexMatcher Matches( std::string const& regex, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); + +} // namespace Matchers +} // namespace Catch + +// end catch_matchers_string.h +// start catch_matchers_vector.h + +#include + +namespace Catch { +namespace Matchers { + + namespace Vector { + namespace Detail { + template + size_t count(InputIterator first, InputIterator last, T const& item) { + size_t cnt = 0; + for (; first != last; ++first) { + if (*first == item) { + ++cnt; + } + } + return cnt; + } + template + bool contains(InputIterator first, InputIterator last, T const& item) { + for (; first != last; ++first) { + if (*first == item) { + return true; + } + } + return false; + } + } + + template + struct ContainsElementMatcher : MatcherBase> { + + ContainsElementMatcher(T const &comparator) : m_comparator( comparator) {} + + bool match(std::vector const &v) const override { + for (auto const& el : v) { + if (el == m_comparator) { + return true; + } + } + return false; + } + + std::string describe() const override { + return "Contains: " + ::Catch::Detail::stringify( m_comparator ); + } + + T const& m_comparator; + }; + + template + struct ContainsMatcher : MatcherBase> { + + ContainsMatcher(std::vector const &comparator) : m_comparator( comparator ) {} + + bool match(std::vector const &v) const override { + // !TBD: see note in EqualsMatcher + if (m_comparator.size() > v.size()) + return false; + for (auto const& comparator : m_comparator) { + auto present = false; + for (const auto& el : v) { + if (el == comparator) { + present = true; + break; + } + } + if (!present) { + return false; + } + } + return true; + } + std::string describe() const override { + return "Contains: " + ::Catch::Detail::stringify( m_comparator ); + } + + std::vector const& m_comparator; + }; + + template + struct EqualsMatcher : MatcherBase> { + + EqualsMatcher(std::vector const &comparator) : m_comparator( comparator ) {} + + bool match(std::vector const &v) const override { + // !TBD: This currently works if all elements can be compared using != + // - a more general approach would be via a compare template that defaults + // to using !=. but could be specialised for, e.g. std::vector etc + // - then just call that directly + if (m_comparator.size() != v.size()) + return false; + for (std::size_t i = 0; i < v.size(); ++i) + if (m_comparator[i] != v[i]) + return false; + return true; + } + std::string describe() const override { + return "Equals: " + ::Catch::Detail::stringify( m_comparator ); + } + std::vector const& m_comparator; + }; + + template + struct UnorderedEqualsMatcher : MatcherBase> { + UnorderedEqualsMatcher(std::vector const& target) : m_target(target) {} + bool match(std::vector const& vec) const override { + // Note: This is a reimplementation of std::is_permutation, + // because I don't want to include inside the common path + if (m_target.size() != vec.size()) { + return false; + } + auto lfirst = m_target.begin(), llast = m_target.end(); + auto rfirst = vec.begin(), rlast = vec.end(); + // Cut common prefix to optimize checking of permuted parts + while (lfirst != llast && *lfirst != *rfirst) { + ++lfirst; ++rfirst; + } + if (lfirst == llast) { + return true; + } + + for (auto mid = lfirst; mid != llast; ++mid) { + // Skip already counted items + if (Detail::contains(lfirst, mid, *mid)) { + continue; + } + size_t num_vec = Detail::count(rfirst, rlast, *mid); + if (num_vec == 0 || Detail::count(lfirst, llast, *mid) != num_vec) { + return false; + } + } + + return true; + } + + std::string describe() const override { + return "UnorderedEquals: " + ::Catch::Detail::stringify(m_target); + } + private: + std::vector const& m_target; + }; + + } // namespace Vector + + // The following functions create the actual matcher objects. + // This allows the types to be inferred + + template + Vector::ContainsMatcher Contains( std::vector const& comparator ) { + return Vector::ContainsMatcher( comparator ); + } + + template + Vector::ContainsElementMatcher VectorContains( T const& comparator ) { + return Vector::ContainsElementMatcher( comparator ); + } + + template + Vector::EqualsMatcher Equals( std::vector const& comparator ) { + return Vector::EqualsMatcher( comparator ); + } + + template + Vector::UnorderedEqualsMatcher UnorderedEquals(std::vector const& target) { + return Vector::UnorderedEqualsMatcher(target); + } + +} // namespace Matchers +} // namespace Catch + +// end catch_matchers_vector.h +namespace Catch { + + template + class MatchExpr : public ITransientExpression { + ArgT const& m_arg; + MatcherT m_matcher; + StringRef m_matcherString; + public: + MatchExpr( ArgT const& arg, MatcherT const& matcher, StringRef matcherString ) + : ITransientExpression{ true, matcher.match( arg ) }, + m_arg( arg ), + m_matcher( matcher ), + m_matcherString( matcherString ) + {} + + void streamReconstructedExpression( std::ostream &os ) const override { + auto matcherAsString = m_matcher.toString(); + os << Catch::Detail::stringify( m_arg ) << ' '; + if( matcherAsString == Detail::unprintableString ) + os << m_matcherString; + else + os << matcherAsString; + } + }; + + using StringMatcher = Matchers::Impl::MatcherBase; + + void handleExceptionMatchExpr( AssertionHandler& handler, StringMatcher const& matcher, StringRef matcherString ); + + template + auto makeMatchExpr( ArgT const& arg, MatcherT const& matcher, StringRef matcherString ) -> MatchExpr { + return MatchExpr( arg, matcher, matcherString ); + } + +} // namespace Catch + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CHECK_THAT( macroName, matcher, resultDisposition, arg ) \ + do { \ + Catch::AssertionHandler catchAssertionHandler( macroName, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(arg) ", " CATCH_INTERNAL_STRINGIFY(matcher), resultDisposition ); \ + INTERNAL_CATCH_TRY { \ + catchAssertionHandler.handleExpr( Catch::makeMatchExpr( arg, matcher, #matcher ) ); \ + } INTERNAL_CATCH_CATCH( catchAssertionHandler ) \ + INTERNAL_CATCH_REACT( catchAssertionHandler ) \ + } while( false ) + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_THROWS_MATCHES( macroName, exceptionType, resultDisposition, matcher, ... ) \ + do { \ + Catch::AssertionHandler catchAssertionHandler( macroName, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__) ", " CATCH_INTERNAL_STRINGIFY(exceptionType) ", " CATCH_INTERNAL_STRINGIFY(matcher), resultDisposition ); \ + if( catchAssertionHandler.allowThrows() ) \ + try { \ + static_cast(__VA_ARGS__ ); \ + catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \ + } \ + catch( exceptionType const& ex ) { \ + catchAssertionHandler.handleExpr( Catch::makeMatchExpr( ex, matcher, #matcher ) ); \ + } \ + catch( ... ) { \ + catchAssertionHandler.handleUnexpectedInflightException(); \ + } \ + else \ + catchAssertionHandler.handleThrowingCallSkipped(); \ + INTERNAL_CATCH_REACT( catchAssertionHandler ) \ + } while( false ) + +// end catch_capture_matchers.h +#endif + +// These files are included here so the single_include script doesn't put them +// in the conditionally compiled sections +// start catch_test_case_info.h + +#include +#include +#include + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" +#endif + +namespace Catch { + + struct ITestInvoker; + + struct TestCaseInfo { + enum SpecialProperties{ + None = 0, + IsHidden = 1 << 1, + ShouldFail = 1 << 2, + MayFail = 1 << 3, + Throws = 1 << 4, + NonPortable = 1 << 5, + Benchmark = 1 << 6 + }; + + TestCaseInfo( std::string const& _name, + std::string const& _className, + std::string const& _description, + std::vector const& _tags, + SourceLineInfo const& _lineInfo ); + + friend void setTags( TestCaseInfo& testCaseInfo, std::vector tags ); + + bool isHidden() const; + bool throws() const; + bool okToFail() const; + bool expectedToFail() const; + + std::string tagsAsString() const; + + std::string name; + std::string className; + std::string description; + std::vector tags; + std::vector lcaseTags; + SourceLineInfo lineInfo; + SpecialProperties properties; + }; + + class TestCase : public TestCaseInfo { + public: + + TestCase( ITestInvoker* testCase, TestCaseInfo&& info ); + + TestCase withName( std::string const& _newName ) const; + + void invoke() const; + + TestCaseInfo const& getTestCaseInfo() const; + + bool operator == ( TestCase const& other ) const; + bool operator < ( TestCase const& other ) const; + + private: + std::shared_ptr test; + }; + + TestCase makeTestCase( ITestInvoker* testCase, + std::string const& className, + NameAndTags const& nameAndTags, + SourceLineInfo const& lineInfo ); +} + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +// end catch_test_case_info.h +// start catch_interfaces_runner.h + +namespace Catch { + + struct IRunner { + virtual ~IRunner(); + virtual bool aborting() const = 0; + }; +} + +// end catch_interfaces_runner.h + +#ifdef __OBJC__ +// start catch_objc.hpp + +#import + +#include + +// NB. Any general catch headers included here must be included +// in catch.hpp first to make sure they are included by the single +// header for non obj-usage + +/////////////////////////////////////////////////////////////////////////////// +// This protocol is really only here for (self) documenting purposes, since +// all its methods are optional. +@protocol OcFixture + +@optional + +-(void) setUp; +-(void) tearDown; + +@end + +namespace Catch { + + class OcMethod : public ITestInvoker { + + public: + OcMethod( Class cls, SEL sel ) : m_cls( cls ), m_sel( sel ) {} + + virtual void invoke() const { + id obj = [[m_cls alloc] init]; + + performOptionalSelector( obj, @selector(setUp) ); + performOptionalSelector( obj, m_sel ); + performOptionalSelector( obj, @selector(tearDown) ); + + arcSafeRelease( obj ); + } + private: + virtual ~OcMethod() {} + + Class m_cls; + SEL m_sel; + }; + + namespace Detail{ + + inline std::string getAnnotation( Class cls, + std::string const& annotationName, + std::string const& testCaseName ) { + NSString* selStr = [[NSString alloc] initWithFormat:@"Catch_%s_%s", annotationName.c_str(), testCaseName.c_str()]; + SEL sel = NSSelectorFromString( selStr ); + arcSafeRelease( selStr ); + id value = performOptionalSelector( cls, sel ); + if( value ) + return [(NSString*)value UTF8String]; + return ""; + } + } + + inline std::size_t registerTestMethods() { + std::size_t noTestMethods = 0; + int noClasses = objc_getClassList( nullptr, 0 ); + + Class* classes = (CATCH_UNSAFE_UNRETAINED Class *)malloc( sizeof(Class) * noClasses); + objc_getClassList( classes, noClasses ); + + for( int c = 0; c < noClasses; c++ ) { + Class cls = classes[c]; + { + u_int count; + Method* methods = class_copyMethodList( cls, &count ); + for( u_int m = 0; m < count ; m++ ) { + SEL selector = method_getName(methods[m]); + std::string methodName = sel_getName(selector); + if( startsWith( methodName, "Catch_TestCase_" ) ) { + std::string testCaseName = methodName.substr( 15 ); + std::string name = Detail::getAnnotation( cls, "Name", testCaseName ); + std::string desc = Detail::getAnnotation( cls, "Description", testCaseName ); + const char* className = class_getName( cls ); + + getMutableRegistryHub().registerTest( makeTestCase( new OcMethod( cls, selector ), className, name.c_str(), desc.c_str(), SourceLineInfo("",0) ) ); + noTestMethods++; + } + } + free(methods); + } + } + return noTestMethods; + } + +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) + + namespace Matchers { + namespace Impl { + namespace NSStringMatchers { + + struct StringHolder : MatcherBase{ + StringHolder( NSString* substr ) : m_substr( [substr copy] ){} + StringHolder( StringHolder const& other ) : m_substr( [other.m_substr copy] ){} + StringHolder() { + arcSafeRelease( m_substr ); + } + + bool match( NSString* arg ) const override { + return false; + } + + NSString* CATCH_ARC_STRONG m_substr; + }; + + struct Equals : StringHolder { + Equals( NSString* substr ) : StringHolder( substr ){} + + bool match( NSString* str ) const override { + return (str != nil || m_substr == nil ) && + [str isEqualToString:m_substr]; + } + + std::string describe() const override { + return "equals string: " + Catch::Detail::stringify( m_substr ); + } + }; + + struct Contains : StringHolder { + Contains( NSString* substr ) : StringHolder( substr ){} + + bool match( NSString* str ) const { + return (str != nil || m_substr == nil ) && + [str rangeOfString:m_substr].location != NSNotFound; + } + + std::string describe() const override { + return "contains string: " + Catch::Detail::stringify( m_substr ); + } + }; + + struct StartsWith : StringHolder { + StartsWith( NSString* substr ) : StringHolder( substr ){} + + bool match( NSString* str ) const override { + return (str != nil || m_substr == nil ) && + [str rangeOfString:m_substr].location == 0; + } + + std::string describe() const override { + return "starts with: " + Catch::Detail::stringify( m_substr ); + } + }; + struct EndsWith : StringHolder { + EndsWith( NSString* substr ) : StringHolder( substr ){} + + bool match( NSString* str ) const override { + return (str != nil || m_substr == nil ) && + [str rangeOfString:m_substr].location == [str length] - [m_substr length]; + } + + std::string describe() const override { + return "ends with: " + Catch::Detail::stringify( m_substr ); + } + }; + + } // namespace NSStringMatchers + } // namespace Impl + + inline Impl::NSStringMatchers::Equals + Equals( NSString* substr ){ return Impl::NSStringMatchers::Equals( substr ); } + + inline Impl::NSStringMatchers::Contains + Contains( NSString* substr ){ return Impl::NSStringMatchers::Contains( substr ); } + + inline Impl::NSStringMatchers::StartsWith + StartsWith( NSString* substr ){ return Impl::NSStringMatchers::StartsWith( substr ); } + + inline Impl::NSStringMatchers::EndsWith + EndsWith( NSString* substr ){ return Impl::NSStringMatchers::EndsWith( substr ); } + + } // namespace Matchers + + using namespace Matchers; + +#endif // CATCH_CONFIG_DISABLE_MATCHERS + +} // namespace Catch + +/////////////////////////////////////////////////////////////////////////////// +#define OC_MAKE_UNIQUE_NAME( root, uniqueSuffix ) root##uniqueSuffix +#define OC_TEST_CASE2( name, desc, uniqueSuffix ) \ ++(NSString*) OC_MAKE_UNIQUE_NAME( Catch_Name_test_, uniqueSuffix ) \ +{ \ +return @ name; \ +} \ ++(NSString*) OC_MAKE_UNIQUE_NAME( Catch_Description_test_, uniqueSuffix ) \ +{ \ +return @ desc; \ +} \ +-(void) OC_MAKE_UNIQUE_NAME( Catch_TestCase_test_, uniqueSuffix ) + +#define OC_TEST_CASE( name, desc ) OC_TEST_CASE2( name, desc, __LINE__ ) + +// end catch_objc.hpp +#endif + +#ifdef CATCH_CONFIG_EXTERNAL_INTERFACES +// start catch_external_interfaces.h + +// start catch_reporter_bases.hpp + +// start catch_interfaces_reporter.h + +// start catch_config.hpp + +// start catch_test_spec_parser.h + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" +#endif + +// start catch_test_spec.h + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" +#endif + +// start catch_wildcard_pattern.h + +namespace Catch +{ + class WildcardPattern { + enum WildcardPosition { + NoWildcard = 0, + WildcardAtStart = 1, + WildcardAtEnd = 2, + WildcardAtBothEnds = WildcardAtStart | WildcardAtEnd + }; + + public: + + WildcardPattern( std::string const& pattern, CaseSensitive::Choice caseSensitivity ); + virtual ~WildcardPattern() = default; + virtual bool matches( std::string const& str ) const; + + private: + std::string adjustCase( std::string const& str ) const; + CaseSensitive::Choice m_caseSensitivity; + WildcardPosition m_wildcard = NoWildcard; + std::string m_pattern; + }; +} + +// end catch_wildcard_pattern.h +#include +#include +#include + +namespace Catch { + + class TestSpec { + struct Pattern { + virtual ~Pattern(); + virtual bool matches( TestCaseInfo const& testCase ) const = 0; + }; + using PatternPtr = std::shared_ptr; + + class NamePattern : public Pattern { + public: + NamePattern( std::string const& name ); + virtual ~NamePattern(); + virtual bool matches( TestCaseInfo const& testCase ) const override; + private: + WildcardPattern m_wildcardPattern; + }; + + class TagPattern : public Pattern { + public: + TagPattern( std::string const& tag ); + virtual ~TagPattern(); + virtual bool matches( TestCaseInfo const& testCase ) const override; + private: + std::string m_tag; + }; + + class ExcludedPattern : public Pattern { + public: + ExcludedPattern( PatternPtr const& underlyingPattern ); + virtual ~ExcludedPattern(); + virtual bool matches( TestCaseInfo const& testCase ) const override; + private: + PatternPtr m_underlyingPattern; + }; + + struct Filter { + std::vector m_patterns; + + bool matches( TestCaseInfo const& testCase ) const; + }; + + public: + bool hasFilters() const; + bool matches( TestCaseInfo const& testCase ) const; + + private: + std::vector m_filters; + + friend class TestSpecParser; + }; +} + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +// end catch_test_spec.h +// start catch_interfaces_tag_alias_registry.h + +#include + +namespace Catch { + + struct TagAlias; + + struct ITagAliasRegistry { + virtual ~ITagAliasRegistry(); + // Nullptr if not present + virtual TagAlias const* find( std::string const& alias ) const = 0; + virtual std::string expandAliases( std::string const& unexpandedTestSpec ) const = 0; + + static ITagAliasRegistry const& get(); + }; + +} // end namespace Catch + +// end catch_interfaces_tag_alias_registry.h +namespace Catch { + + class TestSpecParser { + enum Mode{ None, Name, QuotedName, Tag, EscapedName }; + Mode m_mode = None; + bool m_exclusion = false; + std::size_t m_start = std::string::npos, m_pos = 0; + std::string m_arg; + std::vector m_escapeChars; + TestSpec::Filter m_currentFilter; + TestSpec m_testSpec; + ITagAliasRegistry const* m_tagAliases = nullptr; + + public: + TestSpecParser( ITagAliasRegistry const& tagAliases ); + + TestSpecParser& parse( std::string const& arg ); + TestSpec testSpec(); + + private: + void visitChar( char c ); + void startNewMode( Mode mode, std::size_t start ); + void escape(); + std::string subString() const; + + template + void addPattern() { + std::string token = subString(); + for( std::size_t i = 0; i < m_escapeChars.size(); ++i ) + token = token.substr( 0, m_escapeChars[i]-m_start-i ) + token.substr( m_escapeChars[i]-m_start-i+1 ); + m_escapeChars.clear(); + if( startsWith( token, "exclude:" ) ) { + m_exclusion = true; + token = token.substr( 8 ); + } + if( !token.empty() ) { + TestSpec::PatternPtr pattern = std::make_shared( token ); + if( m_exclusion ) + pattern = std::make_shared( pattern ); + m_currentFilter.m_patterns.push_back( pattern ); + } + m_exclusion = false; + m_mode = None; + } + + void addFilter(); + }; + TestSpec parseTestSpec( std::string const& arg ); + +} // namespace Catch + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +// end catch_test_spec_parser.h +// start catch_interfaces_config.h + +#include +#include +#include +#include + +namespace Catch { + + enum class Verbosity { + Quiet = 0, + Normal, + High + }; + + struct WarnAbout { enum What { + Nothing = 0x00, + NoAssertions = 0x01, + NoTests = 0x02 + }; }; + + struct ShowDurations { enum OrNot { + DefaultForReporter, + Always, + Never + }; }; + struct RunTests { enum InWhatOrder { + InDeclarationOrder, + InLexicographicalOrder, + InRandomOrder + }; }; + struct UseColour { enum YesOrNo { + Auto, + Yes, + No + }; }; + struct WaitForKeypress { enum When { + Never, + BeforeStart = 1, + BeforeExit = 2, + BeforeStartAndExit = BeforeStart | BeforeExit + }; }; + + class TestSpec; + + struct IConfig : NonCopyable { + + virtual ~IConfig(); + + virtual bool allowThrows() const = 0; + virtual std::ostream& stream() const = 0; + virtual std::string name() const = 0; + virtual bool includeSuccessfulResults() const = 0; + virtual bool shouldDebugBreak() const = 0; + virtual bool warnAboutMissingAssertions() const = 0; + virtual bool warnAboutNoTests() const = 0; + virtual int abortAfter() const = 0; + virtual bool showInvisibles() const = 0; + virtual ShowDurations::OrNot showDurations() const = 0; + virtual TestSpec const& testSpec() const = 0; + virtual bool hasTestFilters() const = 0; + virtual RunTests::InWhatOrder runOrder() const = 0; + virtual unsigned int rngSeed() const = 0; + virtual int benchmarkResolutionMultiple() const = 0; + virtual UseColour::YesOrNo useColour() const = 0; + virtual std::vector const& getSectionsToRun() const = 0; + virtual Verbosity verbosity() const = 0; + }; + + using IConfigPtr = std::shared_ptr; +} + +// end catch_interfaces_config.h +// Libstdc++ doesn't like incomplete classes for unique_ptr + +#include +#include +#include + +#ifndef CATCH_CONFIG_CONSOLE_WIDTH +#define CATCH_CONFIG_CONSOLE_WIDTH 80 +#endif + +namespace Catch { + + struct IStream; + + struct ConfigData { + bool listTests = false; + bool listTags = false; + bool listReporters = false; + bool listTestNamesOnly = false; + + bool showSuccessfulTests = false; + bool shouldDebugBreak = false; + bool noThrow = false; + bool showHelp = false; + bool showInvisibles = false; + bool filenamesAsTags = false; + bool libIdentify = false; + + int abortAfter = -1; + unsigned int rngSeed = 0; + int benchmarkResolutionMultiple = 100; + + Verbosity verbosity = Verbosity::Normal; + WarnAbout::What warnings = WarnAbout::Nothing; + ShowDurations::OrNot showDurations = ShowDurations::DefaultForReporter; + RunTests::InWhatOrder runOrder = RunTests::InDeclarationOrder; + UseColour::YesOrNo useColour = UseColour::Auto; + WaitForKeypress::When waitForKeypress = WaitForKeypress::Never; + + std::string outputFilename; + std::string name; + std::string processName; +#ifndef CATCH_CONFIG_DEFAULT_REPORTER +#define CATCH_CONFIG_DEFAULT_REPORTER "console" +#endif + std::string reporterName = CATCH_CONFIG_DEFAULT_REPORTER; +#undef CATCH_CONFIG_DEFAULT_REPORTER + + std::vector testsOrTags; + std::vector sectionsToRun; + }; + + class Config : public IConfig { + public: + + Config() = default; + Config( ConfigData const& data ); + virtual ~Config() = default; + + std::string const& getFilename() const; + + bool listTests() const; + bool listTestNamesOnly() const; + bool listTags() const; + bool listReporters() const; + + std::string getProcessName() const; + std::string const& getReporterName() const; + + std::vector const& getTestsOrTags() const; + std::vector const& getSectionsToRun() const override; + + virtual TestSpec const& testSpec() const override; + bool hasTestFilters() const override; + + bool showHelp() const; + + // IConfig interface + bool allowThrows() const override; + std::ostream& stream() const override; + std::string name() const override; + bool includeSuccessfulResults() const override; + bool warnAboutMissingAssertions() const override; + bool warnAboutNoTests() const override; + ShowDurations::OrNot showDurations() const override; + RunTests::InWhatOrder runOrder() const override; + unsigned int rngSeed() const override; + int benchmarkResolutionMultiple() const override; + UseColour::YesOrNo useColour() const override; + bool shouldDebugBreak() const override; + int abortAfter() const override; + bool showInvisibles() const override; + Verbosity verbosity() const override; + + private: + + IStream const* openStream(); + ConfigData m_data; + + std::unique_ptr m_stream; + TestSpec m_testSpec; + bool m_hasTestFilters = false; + }; + +} // end namespace Catch + +// end catch_config.hpp +// start catch_assertionresult.h + +#include + +namespace Catch { + + struct AssertionResultData + { + AssertionResultData() = delete; + + AssertionResultData( ResultWas::OfType _resultType, LazyExpression const& _lazyExpression ); + + std::string message; + mutable std::string reconstructedExpression; + LazyExpression lazyExpression; + ResultWas::OfType resultType; + + std::string reconstructExpression() const; + }; + + class AssertionResult { + public: + AssertionResult() = delete; + AssertionResult( AssertionInfo const& info, AssertionResultData const& data ); + + bool isOk() const; + bool succeeded() const; + ResultWas::OfType getResultType() const; + bool hasExpression() const; + bool hasMessage() const; + std::string getExpression() const; + std::string getExpressionInMacro() const; + bool hasExpandedExpression() const; + std::string getExpandedExpression() const; + std::string getMessage() const; + SourceLineInfo getSourceInfo() const; + StringRef getTestMacroName() const; + + //protected: + AssertionInfo m_info; + AssertionResultData m_resultData; + }; + +} // end namespace Catch + +// end catch_assertionresult.h +// start catch_option.hpp + +namespace Catch { + + // An optional type + template + class Option { + public: + Option() : nullableValue( nullptr ) {} + Option( T const& _value ) + : nullableValue( new( storage ) T( _value ) ) + {} + Option( Option const& _other ) + : nullableValue( _other ? new( storage ) T( *_other ) : nullptr ) + {} + + ~Option() { + reset(); + } + + Option& operator= ( Option const& _other ) { + if( &_other != this ) { + reset(); + if( _other ) + nullableValue = new( storage ) T( *_other ); + } + return *this; + } + Option& operator = ( T const& _value ) { + reset(); + nullableValue = new( storage ) T( _value ); + return *this; + } + + void reset() { + if( nullableValue ) + nullableValue->~T(); + nullableValue = nullptr; + } + + T& operator*() { return *nullableValue; } + T const& operator*() const { return *nullableValue; } + T* operator->() { return nullableValue; } + const T* operator->() const { return nullableValue; } + + T valueOr( T const& defaultValue ) const { + return nullableValue ? *nullableValue : defaultValue; + } + + bool some() const { return nullableValue != nullptr; } + bool none() const { return nullableValue == nullptr; } + + bool operator !() const { return nullableValue == nullptr; } + explicit operator bool() const { + return some(); + } + + private: + T *nullableValue; + alignas(alignof(T)) char storage[sizeof(T)]; + }; + +} // end namespace Catch + +// end catch_option.hpp +#include +#include +#include +#include +#include + +namespace Catch { + + struct ReporterConfig { + explicit ReporterConfig( IConfigPtr const& _fullConfig ); + + ReporterConfig( IConfigPtr const& _fullConfig, std::ostream& _stream ); + + std::ostream& stream() const; + IConfigPtr fullConfig() const; + + private: + std::ostream* m_stream; + IConfigPtr m_fullConfig; + }; + + struct ReporterPreferences { + bool shouldRedirectStdOut = false; + }; + + template + struct LazyStat : Option { + LazyStat& operator=( T const& _value ) { + Option::operator=( _value ); + used = false; + return *this; + } + void reset() { + Option::reset(); + used = false; + } + bool used = false; + }; + + struct TestRunInfo { + TestRunInfo( std::string const& _name ); + std::string name; + }; + struct GroupInfo { + GroupInfo( std::string const& _name, + std::size_t _groupIndex, + std::size_t _groupsCount ); + + std::string name; + std::size_t groupIndex; + std::size_t groupsCounts; + }; + + struct AssertionStats { + AssertionStats( AssertionResult const& _assertionResult, + std::vector const& _infoMessages, + Totals const& _totals ); + + AssertionStats( AssertionStats const& ) = default; + AssertionStats( AssertionStats && ) = default; + AssertionStats& operator = ( AssertionStats const& ) = default; + AssertionStats& operator = ( AssertionStats && ) = default; + virtual ~AssertionStats(); + + AssertionResult assertionResult; + std::vector infoMessages; + Totals totals; + }; + + struct SectionStats { + SectionStats( SectionInfo const& _sectionInfo, + Counts const& _assertions, + double _durationInSeconds, + bool _missingAssertions ); + SectionStats( SectionStats const& ) = default; + SectionStats( SectionStats && ) = default; + SectionStats& operator = ( SectionStats const& ) = default; + SectionStats& operator = ( SectionStats && ) = default; + virtual ~SectionStats(); + + SectionInfo sectionInfo; + Counts assertions; + double durationInSeconds; + bool missingAssertions; + }; + + struct TestCaseStats { + TestCaseStats( TestCaseInfo const& _testInfo, + Totals const& _totals, + std::string const& _stdOut, + std::string const& _stdErr, + bool _aborting ); + + TestCaseStats( TestCaseStats const& ) = default; + TestCaseStats( TestCaseStats && ) = default; + TestCaseStats& operator = ( TestCaseStats const& ) = default; + TestCaseStats& operator = ( TestCaseStats && ) = default; + virtual ~TestCaseStats(); + + TestCaseInfo testInfo; + Totals totals; + std::string stdOut; + std::string stdErr; + bool aborting; + }; + + struct TestGroupStats { + TestGroupStats( GroupInfo const& _groupInfo, + Totals const& _totals, + bool _aborting ); + TestGroupStats( GroupInfo const& _groupInfo ); + + TestGroupStats( TestGroupStats const& ) = default; + TestGroupStats( TestGroupStats && ) = default; + TestGroupStats& operator = ( TestGroupStats const& ) = default; + TestGroupStats& operator = ( TestGroupStats && ) = default; + virtual ~TestGroupStats(); + + GroupInfo groupInfo; + Totals totals; + bool aborting; + }; + + struct TestRunStats { + TestRunStats( TestRunInfo const& _runInfo, + Totals const& _totals, + bool _aborting ); + + TestRunStats( TestRunStats const& ) = default; + TestRunStats( TestRunStats && ) = default; + TestRunStats& operator = ( TestRunStats const& ) = default; + TestRunStats& operator = ( TestRunStats && ) = default; + virtual ~TestRunStats(); + + TestRunInfo runInfo; + Totals totals; + bool aborting; + }; + + struct BenchmarkInfo { + std::string name; + }; + struct BenchmarkStats { + BenchmarkInfo info; + std::size_t iterations; + uint64_t elapsedTimeInNanoseconds; + }; + + struct IStreamingReporter { + virtual ~IStreamingReporter() = default; + + // Implementing class must also provide the following static methods: + // static std::string getDescription(); + // static std::set getSupportedVerbosities() + + virtual ReporterPreferences getPreferences() const = 0; + + virtual void noMatchingTestCases( std::string const& spec ) = 0; + + virtual void testRunStarting( TestRunInfo const& testRunInfo ) = 0; + virtual void testGroupStarting( GroupInfo const& groupInfo ) = 0; + + virtual void testCaseStarting( TestCaseInfo const& testInfo ) = 0; + virtual void sectionStarting( SectionInfo const& sectionInfo ) = 0; + + // *** experimental *** + virtual void benchmarkStarting( BenchmarkInfo const& ) {} + + virtual void assertionStarting( AssertionInfo const& assertionInfo ) = 0; + + // The return value indicates if the messages buffer should be cleared: + virtual bool assertionEnded( AssertionStats const& assertionStats ) = 0; + + // *** experimental *** + virtual void benchmarkEnded( BenchmarkStats const& ) {} + + virtual void sectionEnded( SectionStats const& sectionStats ) = 0; + virtual void testCaseEnded( TestCaseStats const& testCaseStats ) = 0; + virtual void testGroupEnded( TestGroupStats const& testGroupStats ) = 0; + virtual void testRunEnded( TestRunStats const& testRunStats ) = 0; + + virtual void skipTest( TestCaseInfo const& testInfo ) = 0; + + // Default empty implementation provided + virtual void fatalErrorEncountered( StringRef name ); + + virtual bool isMulti() const; + }; + using IStreamingReporterPtr = std::unique_ptr; + + struct IReporterFactory { + virtual ~IReporterFactory(); + virtual IStreamingReporterPtr create( ReporterConfig const& config ) const = 0; + virtual std::string getDescription() const = 0; + }; + using IReporterFactoryPtr = std::shared_ptr; + + struct IReporterRegistry { + using FactoryMap = std::map; + using Listeners = std::vector; + + virtual ~IReporterRegistry(); + virtual IStreamingReporterPtr create( std::string const& name, IConfigPtr const& config ) const = 0; + virtual FactoryMap const& getFactories() const = 0; + virtual Listeners const& getListeners() const = 0; + }; + +} // end namespace Catch + +// end catch_interfaces_reporter.h +#include +#include +#include +#include +#include +#include +#include + +namespace Catch { + void prepareExpandedExpression(AssertionResult& result); + + // Returns double formatted as %.3f (format expected on output) + std::string getFormattedDuration( double duration ); + + template + struct StreamingReporterBase : IStreamingReporter { + + StreamingReporterBase( ReporterConfig const& _config ) + : m_config( _config.fullConfig() ), + stream( _config.stream() ) + { + m_reporterPrefs.shouldRedirectStdOut = false; + if( !DerivedT::getSupportedVerbosities().count( m_config->verbosity() ) ) + throw std::domain_error( "Verbosity level not supported by this reporter" ); + } + + ReporterPreferences getPreferences() const override { + return m_reporterPrefs; + } + + static std::set getSupportedVerbosities() { + return { Verbosity::Normal }; + } + + ~StreamingReporterBase() override = default; + + void noMatchingTestCases(std::string const&) override {} + + void testRunStarting(TestRunInfo const& _testRunInfo) override { + currentTestRunInfo = _testRunInfo; + } + void testGroupStarting(GroupInfo const& _groupInfo) override { + currentGroupInfo = _groupInfo; + } + + void testCaseStarting(TestCaseInfo const& _testInfo) override { + currentTestCaseInfo = _testInfo; + } + void sectionStarting(SectionInfo const& _sectionInfo) override { + m_sectionStack.push_back(_sectionInfo); + } + + void sectionEnded(SectionStats const& /* _sectionStats */) override { + m_sectionStack.pop_back(); + } + void testCaseEnded(TestCaseStats const& /* _testCaseStats */) override { + currentTestCaseInfo.reset(); + } + void testGroupEnded(TestGroupStats const& /* _testGroupStats */) override { + currentGroupInfo.reset(); + } + void testRunEnded(TestRunStats const& /* _testRunStats */) override { + currentTestCaseInfo.reset(); + currentGroupInfo.reset(); + currentTestRunInfo.reset(); + } + + void skipTest(TestCaseInfo const&) override { + // Don't do anything with this by default. + // It can optionally be overridden in the derived class. + } + + IConfigPtr m_config; + std::ostream& stream; + + LazyStat currentTestRunInfo; + LazyStat currentGroupInfo; + LazyStat currentTestCaseInfo; + + std::vector m_sectionStack; + ReporterPreferences m_reporterPrefs; + }; + + template + struct CumulativeReporterBase : IStreamingReporter { + template + struct Node { + explicit Node( T const& _value ) : value( _value ) {} + virtual ~Node() {} + + using ChildNodes = std::vector>; + T value; + ChildNodes children; + }; + struct SectionNode { + explicit SectionNode(SectionStats const& _stats) : stats(_stats) {} + virtual ~SectionNode() = default; + + bool operator == (SectionNode const& other) const { + return stats.sectionInfo.lineInfo == other.stats.sectionInfo.lineInfo; + } + bool operator == (std::shared_ptr const& other) const { + return operator==(*other); + } + + SectionStats stats; + using ChildSections = std::vector>; + using Assertions = std::vector; + ChildSections childSections; + Assertions assertions; + std::string stdOut; + std::string stdErr; + }; + + struct BySectionInfo { + BySectionInfo( SectionInfo const& other ) : m_other( other ) {} + BySectionInfo( BySectionInfo const& other ) : m_other( other.m_other ) {} + bool operator() (std::shared_ptr const& node) const { + return ((node->stats.sectionInfo.name == m_other.name) && + (node->stats.sectionInfo.lineInfo == m_other.lineInfo)); + } + void operator=(BySectionInfo const&) = delete; + + private: + SectionInfo const& m_other; + }; + + using TestCaseNode = Node; + using TestGroupNode = Node; + using TestRunNode = Node; + + CumulativeReporterBase( ReporterConfig const& _config ) + : m_config( _config.fullConfig() ), + stream( _config.stream() ) + { + m_reporterPrefs.shouldRedirectStdOut = false; + if( !DerivedT::getSupportedVerbosities().count( m_config->verbosity() ) ) + throw std::domain_error( "Verbosity level not supported by this reporter" ); + } + ~CumulativeReporterBase() override = default; + + ReporterPreferences getPreferences() const override { + return m_reporterPrefs; + } + + static std::set getSupportedVerbosities() { + return { Verbosity::Normal }; + } + + void testRunStarting( TestRunInfo const& ) override {} + void testGroupStarting( GroupInfo const& ) override {} + + void testCaseStarting( TestCaseInfo const& ) override {} + + void sectionStarting( SectionInfo const& sectionInfo ) override { + SectionStats incompleteStats( sectionInfo, Counts(), 0, false ); + std::shared_ptr node; + if( m_sectionStack.empty() ) { + if( !m_rootSection ) + m_rootSection = std::make_shared( incompleteStats ); + node = m_rootSection; + } + else { + SectionNode& parentNode = *m_sectionStack.back(); + auto it = + std::find_if( parentNode.childSections.begin(), + parentNode.childSections.end(), + BySectionInfo( sectionInfo ) ); + if( it == parentNode.childSections.end() ) { + node = std::make_shared( incompleteStats ); + parentNode.childSections.push_back( node ); + } + else + node = *it; + } + m_sectionStack.push_back( node ); + m_deepestSection = std::move(node); + } + + void assertionStarting(AssertionInfo const&) override {} + + bool assertionEnded(AssertionStats const& assertionStats) override { + assert(!m_sectionStack.empty()); + // AssertionResult holds a pointer to a temporary DecomposedExpression, + // which getExpandedExpression() calls to build the expression string. + // Our section stack copy of the assertionResult will likely outlive the + // temporary, so it must be expanded or discarded now to avoid calling + // a destroyed object later. + prepareExpandedExpression(const_cast( assertionStats.assertionResult ) ); + SectionNode& sectionNode = *m_sectionStack.back(); + sectionNode.assertions.push_back(assertionStats); + return true; + } + void sectionEnded(SectionStats const& sectionStats) override { + assert(!m_sectionStack.empty()); + SectionNode& node = *m_sectionStack.back(); + node.stats = sectionStats; + m_sectionStack.pop_back(); + } + void testCaseEnded(TestCaseStats const& testCaseStats) override { + auto node = std::make_shared(testCaseStats); + assert(m_sectionStack.size() == 0); + node->children.push_back(m_rootSection); + m_testCases.push_back(node); + m_rootSection.reset(); + + assert(m_deepestSection); + m_deepestSection->stdOut = testCaseStats.stdOut; + m_deepestSection->stdErr = testCaseStats.stdErr; + } + void testGroupEnded(TestGroupStats const& testGroupStats) override { + auto node = std::make_shared(testGroupStats); + node->children.swap(m_testCases); + m_testGroups.push_back(node); + } + void testRunEnded(TestRunStats const& testRunStats) override { + auto node = std::make_shared(testRunStats); + node->children.swap(m_testGroups); + m_testRuns.push_back(node); + testRunEndedCumulative(); + } + virtual void testRunEndedCumulative() = 0; + + void skipTest(TestCaseInfo const&) override {} + + IConfigPtr m_config; + std::ostream& stream; + std::vector m_assertions; + std::vector>> m_sections; + std::vector> m_testCases; + std::vector> m_testGroups; + + std::vector> m_testRuns; + + std::shared_ptr m_rootSection; + std::shared_ptr m_deepestSection; + std::vector> m_sectionStack; + ReporterPreferences m_reporterPrefs; + }; + + template + char const* getLineOfChars() { + static char line[CATCH_CONFIG_CONSOLE_WIDTH] = {0}; + if( !*line ) { + std::memset( line, C, CATCH_CONFIG_CONSOLE_WIDTH-1 ); + line[CATCH_CONFIG_CONSOLE_WIDTH-1] = 0; + } + return line; + } + + struct TestEventListenerBase : StreamingReporterBase { + TestEventListenerBase( ReporterConfig const& _config ); + + void assertionStarting(AssertionInfo const&) override; + bool assertionEnded(AssertionStats const&) override; + }; + +} // end namespace Catch + +// end catch_reporter_bases.hpp +// start catch_console_colour.h + +namespace Catch { + + struct Colour { + enum Code { + None = 0, + + White, + Red, + Green, + Blue, + Cyan, + Yellow, + Grey, + + Bright = 0x10, + + BrightRed = Bright | Red, + BrightGreen = Bright | Green, + LightGrey = Bright | Grey, + BrightWhite = Bright | White, + BrightYellow = Bright | Yellow, + + // By intention + FileName = LightGrey, + Warning = BrightYellow, + ResultError = BrightRed, + ResultSuccess = BrightGreen, + ResultExpectedFailure = Warning, + + Error = BrightRed, + Success = Green, + + OriginalExpression = Cyan, + ReconstructedExpression = BrightYellow, + + SecondaryText = LightGrey, + Headers = White + }; + + // Use constructed object for RAII guard + Colour( Code _colourCode ); + Colour( Colour&& other ) noexcept; + Colour& operator=( Colour&& other ) noexcept; + ~Colour(); + + // Use static method for one-shot changes + static void use( Code _colourCode ); + + private: + bool m_moved = false; + }; + + std::ostream& operator << ( std::ostream& os, Colour const& ); + +} // end namespace Catch + +// end catch_console_colour.h +// start catch_reporter_registrars.hpp + + +namespace Catch { + + template + class ReporterRegistrar { + + class ReporterFactory : public IReporterFactory { + + virtual IStreamingReporterPtr create( ReporterConfig const& config ) const override { + return std::unique_ptr( new T( config ) ); + } + + virtual std::string getDescription() const override { + return T::getDescription(); + } + }; + + public: + + explicit ReporterRegistrar( std::string const& name ) { + getMutableRegistryHub().registerReporter( name, std::make_shared() ); + } + }; + + template + class ListenerRegistrar { + + class ListenerFactory : public IReporterFactory { + + virtual IStreamingReporterPtr create( ReporterConfig const& config ) const override { + return std::unique_ptr( new T( config ) ); + } + virtual std::string getDescription() const override { + return std::string(); + } + }; + + public: + + ListenerRegistrar() { + getMutableRegistryHub().registerListener( std::make_shared() ); + } + }; +} + +#if !defined(CATCH_CONFIG_DISABLE) + +#define CATCH_REGISTER_REPORTER( name, reporterType ) \ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + namespace{ Catch::ReporterRegistrar catch_internal_RegistrarFor##reporterType( name ); } \ + CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS + +#define CATCH_REGISTER_LISTENER( listenerType ) \ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + namespace{ Catch::ListenerRegistrar catch_internal_RegistrarFor##listenerType; } \ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS +#else // CATCH_CONFIG_DISABLE + +#define CATCH_REGISTER_REPORTER(name, reporterType) +#define CATCH_REGISTER_LISTENER(listenerType) + +#endif // CATCH_CONFIG_DISABLE + +// end catch_reporter_registrars.hpp +// Allow users to base their work off existing reporters +// start catch_reporter_compact.h + +namespace Catch { + + struct CompactReporter : StreamingReporterBase { + + using StreamingReporterBase::StreamingReporterBase; + + ~CompactReporter() override; + + static std::string getDescription(); + + ReporterPreferences getPreferences() const override; + + void noMatchingTestCases(std::string const& spec) override; + + void assertionStarting(AssertionInfo const&) override; + + bool assertionEnded(AssertionStats const& _assertionStats) override; + + void sectionEnded(SectionStats const& _sectionStats) override; + + void testRunEnded(TestRunStats const& _testRunStats) override; + + }; + +} // end namespace Catch + +// end catch_reporter_compact.h +// start catch_reporter_console.h + +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable:4061) // Not all labels are EXPLICITLY handled in switch + // Note that 4062 (not all labels are handled + // and default is missing) is enabled +#endif + +namespace Catch { + // Fwd decls + struct SummaryColumn; + class TablePrinter; + + struct ConsoleReporter : StreamingReporterBase { + std::unique_ptr m_tablePrinter; + + ConsoleReporter(ReporterConfig const& config); + ~ConsoleReporter() override; + static std::string getDescription(); + + void noMatchingTestCases(std::string const& spec) override; + + void assertionStarting(AssertionInfo const&) override; + + bool assertionEnded(AssertionStats const& _assertionStats) override; + + void sectionStarting(SectionInfo const& _sectionInfo) override; + void sectionEnded(SectionStats const& _sectionStats) override; + + void benchmarkStarting(BenchmarkInfo const& info) override; + void benchmarkEnded(BenchmarkStats const& stats) override; + + void testCaseEnded(TestCaseStats const& _testCaseStats) override; + void testGroupEnded(TestGroupStats const& _testGroupStats) override; + void testRunEnded(TestRunStats const& _testRunStats) override; + + private: + + void lazyPrint(); + + void lazyPrintWithoutClosingBenchmarkTable(); + void lazyPrintRunInfo(); + void lazyPrintGroupInfo(); + void printTestCaseAndSectionHeader(); + + void printClosedHeader(std::string const& _name); + void printOpenHeader(std::string const& _name); + + // if string has a : in first line will set indent to follow it on + // subsequent lines + void printHeaderString(std::string const& _string, std::size_t indent = 0); + + void printTotals(Totals const& totals); + void printSummaryRow(std::string const& label, std::vector const& cols, std::size_t row); + + void printTotalsDivider(Totals const& totals); + void printSummaryDivider(); + + private: + bool m_headerPrinted = false; + }; + +} // end namespace Catch + +#if defined(_MSC_VER) +#pragma warning(pop) +#endif + +// end catch_reporter_console.h +// start catch_reporter_junit.h + +// start catch_xmlwriter.h + +#include + +namespace Catch { + + class XmlEncode { + public: + enum ForWhat { ForTextNodes, ForAttributes }; + + XmlEncode( std::string const& str, ForWhat forWhat = ForTextNodes ); + + void encodeTo( std::ostream& os ) const; + + friend std::ostream& operator << ( std::ostream& os, XmlEncode const& xmlEncode ); + + private: + std::string m_str; + ForWhat m_forWhat; + }; + + class XmlWriter { + public: + + class ScopedElement { + public: + ScopedElement( XmlWriter* writer ); + + ScopedElement( ScopedElement&& other ) noexcept; + ScopedElement& operator=( ScopedElement&& other ) noexcept; + + ~ScopedElement(); + + ScopedElement& writeText( std::string const& text, bool indent = true ); + + template + ScopedElement& writeAttribute( std::string const& name, T const& attribute ) { + m_writer->writeAttribute( name, attribute ); + return *this; + } + + private: + mutable XmlWriter* m_writer = nullptr; + }; + + XmlWriter( std::ostream& os = Catch::cout() ); + ~XmlWriter(); + + XmlWriter( XmlWriter const& ) = delete; + XmlWriter& operator=( XmlWriter const& ) = delete; + + XmlWriter& startElement( std::string const& name ); + + ScopedElement scopedElement( std::string const& name ); + + XmlWriter& endElement(); + + XmlWriter& writeAttribute( std::string const& name, std::string const& attribute ); + + XmlWriter& writeAttribute( std::string const& name, bool attribute ); + + template + XmlWriter& writeAttribute( std::string const& name, T const& attribute ) { + ReusableStringStream rss; + rss << attribute; + return writeAttribute( name, rss.str() ); + } + + XmlWriter& writeText( std::string const& text, bool indent = true ); + + XmlWriter& writeComment( std::string const& text ); + + void writeStylesheetRef( std::string const& url ); + + XmlWriter& writeBlankLine(); + + void ensureTagClosed(); + + private: + + void writeDeclaration(); + + void newlineIfNecessary(); + + bool m_tagIsOpen = false; + bool m_needsNewline = false; + std::vector m_tags; + std::string m_indent; + std::ostream& m_os; + }; + +} + +// end catch_xmlwriter.h +namespace Catch { + + class JunitReporter : public CumulativeReporterBase { + public: + JunitReporter(ReporterConfig const& _config); + + ~JunitReporter() override; + + static std::string getDescription(); + + void noMatchingTestCases(std::string const& /*spec*/) override; + + void testRunStarting(TestRunInfo const& runInfo) override; + + void testGroupStarting(GroupInfo const& groupInfo) override; + + void testCaseStarting(TestCaseInfo const& testCaseInfo) override; + bool assertionEnded(AssertionStats const& assertionStats) override; + + void testCaseEnded(TestCaseStats const& testCaseStats) override; + + void testGroupEnded(TestGroupStats const& testGroupStats) override; + + void testRunEndedCumulative() override; + + void writeGroup(TestGroupNode const& groupNode, double suiteTime); + + void writeTestCase(TestCaseNode const& testCaseNode); + + void writeSection(std::string const& className, + std::string const& rootName, + SectionNode const& sectionNode); + + void writeAssertions(SectionNode const& sectionNode); + void writeAssertion(AssertionStats const& stats); + + XmlWriter xml; + Timer suiteTimer; + std::string stdOutForSuite; + std::string stdErrForSuite; + unsigned int unexpectedExceptions = 0; + bool m_okToFail = false; + }; + +} // end namespace Catch + +// end catch_reporter_junit.h +// start catch_reporter_xml.h + +namespace Catch { + class XmlReporter : public StreamingReporterBase { + public: + XmlReporter(ReporterConfig const& _config); + + ~XmlReporter() override; + + static std::string getDescription(); + + virtual std::string getStylesheetRef() const; + + void writeSourceInfo(SourceLineInfo const& sourceInfo); + + public: // StreamingReporterBase + + void noMatchingTestCases(std::string const& s) override; + + void testRunStarting(TestRunInfo const& testInfo) override; + + void testGroupStarting(GroupInfo const& groupInfo) override; + + void testCaseStarting(TestCaseInfo const& testInfo) override; + + void sectionStarting(SectionInfo const& sectionInfo) override; + + void assertionStarting(AssertionInfo const&) override; + + bool assertionEnded(AssertionStats const& assertionStats) override; + + void sectionEnded(SectionStats const& sectionStats) override; + + void testCaseEnded(TestCaseStats const& testCaseStats) override; + + void testGroupEnded(TestGroupStats const& testGroupStats) override; + + void testRunEnded(TestRunStats const& testRunStats) override; + + private: + Timer m_testCaseTimer; + XmlWriter m_xml; + int m_sectionDepth = 0; + }; + +} // end namespace Catch + +// end catch_reporter_xml.h + +// end catch_external_interfaces.h +#endif + +#endif // ! CATCH_CONFIG_IMPL_ONLY + +#ifdef CATCH_IMPL +// start catch_impl.hpp + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wweak-vtables" +#endif + +// Keep these here for external reporters +// start catch_test_case_tracker.h + +#include +#include +#include + +namespace Catch { +namespace TestCaseTracking { + + struct NameAndLocation { + std::string name; + SourceLineInfo location; + + NameAndLocation( std::string const& _name, SourceLineInfo const& _location ); + }; + + struct ITracker; + + using ITrackerPtr = std::shared_ptr; + + struct ITracker { + virtual ~ITracker(); + + // static queries + virtual NameAndLocation const& nameAndLocation() const = 0; + + // dynamic queries + virtual bool isComplete() const = 0; // Successfully completed or failed + virtual bool isSuccessfullyCompleted() const = 0; + virtual bool isOpen() const = 0; // Started but not complete + virtual bool hasChildren() const = 0; + + virtual ITracker& parent() = 0; + + // actions + virtual void close() = 0; // Successfully complete + virtual void fail() = 0; + virtual void markAsNeedingAnotherRun() = 0; + + virtual void addChild( ITrackerPtr const& child ) = 0; + virtual ITrackerPtr findChild( NameAndLocation const& nameAndLocation ) = 0; + virtual void openChild() = 0; + + // Debug/ checking + virtual bool isSectionTracker() const = 0; + virtual bool isIndexTracker() const = 0; + }; + + class TrackerContext { + + enum RunState { + NotStarted, + Executing, + CompletedCycle + }; + + ITrackerPtr m_rootTracker; + ITracker* m_currentTracker = nullptr; + RunState m_runState = NotStarted; + + public: + + static TrackerContext& instance(); + + ITracker& startRun(); + void endRun(); + + void startCycle(); + void completeCycle(); + + bool completedCycle() const; + ITracker& currentTracker(); + void setCurrentTracker( ITracker* tracker ); + }; + + class TrackerBase : public ITracker { + protected: + enum CycleState { + NotStarted, + Executing, + ExecutingChildren, + NeedsAnotherRun, + CompletedSuccessfully, + Failed + }; + + class TrackerHasName { + NameAndLocation m_nameAndLocation; + public: + TrackerHasName( NameAndLocation const& nameAndLocation ); + bool operator ()( ITrackerPtr const& tracker ) const; + }; + + using Children = std::vector; + NameAndLocation m_nameAndLocation; + TrackerContext& m_ctx; + ITracker* m_parent; + Children m_children; + CycleState m_runState = NotStarted; + + public: + TrackerBase( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent ); + + NameAndLocation const& nameAndLocation() const override; + bool isComplete() const override; + bool isSuccessfullyCompleted() const override; + bool isOpen() const override; + bool hasChildren() const override; + + void addChild( ITrackerPtr const& child ) override; + + ITrackerPtr findChild( NameAndLocation const& nameAndLocation ) override; + ITracker& parent() override; + + void openChild() override; + + bool isSectionTracker() const override; + bool isIndexTracker() const override; + + void open(); + + void close() override; + void fail() override; + void markAsNeedingAnotherRun() override; + + private: + void moveToParent(); + void moveToThis(); + }; + + class SectionTracker : public TrackerBase { + std::vector m_filters; + public: + SectionTracker( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent ); + + bool isSectionTracker() const override; + + static SectionTracker& acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation ); + + void tryOpen(); + + void addInitialFilters( std::vector const& filters ); + void addNextFilters( std::vector const& filters ); + }; + + class IndexTracker : public TrackerBase { + int m_size; + int m_index = -1; + public: + IndexTracker( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent, int size ); + + bool isIndexTracker() const override; + void close() override; + + static IndexTracker& acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation, int size ); + + int index() const; + + void moveNext(); + }; + +} // namespace TestCaseTracking + +using TestCaseTracking::ITracker; +using TestCaseTracking::TrackerContext; +using TestCaseTracking::SectionTracker; +using TestCaseTracking::IndexTracker; + +} // namespace Catch + +// end catch_test_case_tracker.h + +// start catch_leak_detector.h + +namespace Catch { + + struct LeakDetector { + LeakDetector(); + }; + +} +// end catch_leak_detector.h +// Cpp files will be included in the single-header file here +// start catch_approx.cpp + +#include +#include + +namespace { + +// Performs equivalent check of std::fabs(lhs - rhs) <= margin +// But without the subtraction to allow for INFINITY in comparison +bool marginComparison(double lhs, double rhs, double margin) { + return (lhs + margin >= rhs) && (rhs + margin >= lhs); +} + +} + +namespace Catch { +namespace Detail { + + Approx::Approx ( double value ) + : m_epsilon( std::numeric_limits::epsilon()*100 ), + m_margin( 0.0 ), + m_scale( 0.0 ), + m_value( value ) + {} + + Approx Approx::custom() { + return Approx( 0 ); + } + + std::string Approx::toString() const { + ReusableStringStream rss; + rss << "Approx( " << ::Catch::Detail::stringify( m_value ) << " )"; + return rss.str(); + } + + bool Approx::equalityComparisonImpl(const double other) const { + // First try with fixed margin, then compute margin based on epsilon, scale and Approx's value + // Thanks to Richard Harris for his help refining the scaled margin value + return marginComparison(m_value, other, m_margin) || marginComparison(m_value, other, m_epsilon * (m_scale + std::fabs(m_value))); + } + +} // end namespace Detail + +std::string StringMaker::convert(Catch::Detail::Approx const& value) { + return value.toString(); +} + +} // end namespace Catch +// end catch_approx.cpp +// start catch_assertionhandler.cpp + +// start catch_context.h + +#include + +namespace Catch { + + struct IResultCapture; + struct IRunner; + struct IConfig; + struct IMutableContext; + + using IConfigPtr = std::shared_ptr; + + struct IContext + { + virtual ~IContext(); + + virtual IResultCapture* getResultCapture() = 0; + virtual IRunner* getRunner() = 0; + virtual IConfigPtr const& getConfig() const = 0; + }; + + struct IMutableContext : IContext + { + virtual ~IMutableContext(); + virtual void setResultCapture( IResultCapture* resultCapture ) = 0; + virtual void setRunner( IRunner* runner ) = 0; + virtual void setConfig( IConfigPtr const& config ) = 0; + + private: + static IMutableContext *currentContext; + friend IMutableContext& getCurrentMutableContext(); + friend void cleanUpContext(); + static void createContext(); + }; + + inline IMutableContext& getCurrentMutableContext() + { + if( !IMutableContext::currentContext ) + IMutableContext::createContext(); + return *IMutableContext::currentContext; + } + + inline IContext& getCurrentContext() + { + return getCurrentMutableContext(); + } + + void cleanUpContext(); +} + +// end catch_context.h +// start catch_debugger.h + +namespace Catch { + bool isDebuggerActive(); +} + +#ifdef CATCH_PLATFORM_MAC + + #define CATCH_TRAP() __asm__("int $3\n" : : ) /* NOLINT */ + +#elif defined(CATCH_PLATFORM_LINUX) + // If we can use inline assembler, do it because this allows us to break + // directly at the location of the failing check instead of breaking inside + // raise() called from it, i.e. one stack frame below. + #if defined(__GNUC__) && (defined(__i386) || defined(__x86_64)) + #define CATCH_TRAP() asm volatile ("int $3") /* NOLINT */ + #else // Fall back to the generic way. + #include + + #define CATCH_TRAP() raise(SIGTRAP) + #endif +#elif defined(_MSC_VER) + #define CATCH_TRAP() __debugbreak() +#elif defined(__MINGW32__) + extern "C" __declspec(dllimport) void __stdcall DebugBreak(); + #define CATCH_TRAP() DebugBreak() +#endif + +#ifdef CATCH_TRAP + #define CATCH_BREAK_INTO_DEBUGGER() if( Catch::isDebuggerActive() ) { CATCH_TRAP(); } +#else + namespace Catch { + inline void doNothing() {} + } + #define CATCH_BREAK_INTO_DEBUGGER() Catch::doNothing() +#endif + +// end catch_debugger.h +// start catch_run_context.h + +// start catch_fatal_condition.h + +// start catch_windows_h_proxy.h + + +#if defined(CATCH_PLATFORM_WINDOWS) + +#if !defined(NOMINMAX) && !defined(CATCH_CONFIG_NO_NOMINMAX) +# define CATCH_DEFINED_NOMINMAX +# define NOMINMAX +#endif +#if !defined(WIN32_LEAN_AND_MEAN) && !defined(CATCH_CONFIG_NO_WIN32_LEAN_AND_MEAN) +# define CATCH_DEFINED_WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +#endif + +#ifdef __AFXDLL +#include +#else +#include +#endif + +#ifdef CATCH_DEFINED_NOMINMAX +# undef NOMINMAX +#endif +#ifdef CATCH_DEFINED_WIN32_LEAN_AND_MEAN +# undef WIN32_LEAN_AND_MEAN +#endif + +#endif // defined(CATCH_PLATFORM_WINDOWS) + +// end catch_windows_h_proxy.h +#if defined( CATCH_CONFIG_WINDOWS_SEH ) + +namespace Catch { + + struct FatalConditionHandler { + + static LONG CALLBACK handleVectoredException(PEXCEPTION_POINTERS ExceptionInfo); + FatalConditionHandler(); + static void reset(); + ~FatalConditionHandler(); + + private: + static bool isSet; + static ULONG guaranteeSize; + static PVOID exceptionHandlerHandle; + }; + +} // namespace Catch + +#elif defined ( CATCH_CONFIG_POSIX_SIGNALS ) + +#include + +namespace Catch { + + struct FatalConditionHandler { + + static bool isSet; + static struct sigaction oldSigActions[]; + static stack_t oldSigStack; + static char altStackMem[]; + + static void handleSignal( int sig ); + + FatalConditionHandler(); + ~FatalConditionHandler(); + static void reset(); + }; + +} // namespace Catch + +#else + +namespace Catch { + struct FatalConditionHandler { + void reset(); + }; +} + +#endif + +// end catch_fatal_condition.h +#include + +namespace Catch { + + struct IMutableContext; + + /////////////////////////////////////////////////////////////////////////// + + class RunContext : public IResultCapture, public IRunner { + + public: + RunContext( RunContext const& ) = delete; + RunContext& operator =( RunContext const& ) = delete; + + explicit RunContext( IConfigPtr const& _config, IStreamingReporterPtr&& reporter ); + + ~RunContext() override; + + void testGroupStarting( std::string const& testSpec, std::size_t groupIndex, std::size_t groupsCount ); + void testGroupEnded( std::string const& testSpec, Totals const& totals, std::size_t groupIndex, std::size_t groupsCount ); + + Totals runTest(TestCase const& testCase); + + IConfigPtr config() const; + IStreamingReporter& reporter() const; + + public: // IResultCapture + + // Assertion handlers + void handleExpr + ( AssertionInfo const& info, + ITransientExpression const& expr, + AssertionReaction& reaction ) override; + void handleMessage + ( AssertionInfo const& info, + ResultWas::OfType resultType, + StringRef const& message, + AssertionReaction& reaction ) override; + void handleUnexpectedExceptionNotThrown + ( AssertionInfo const& info, + AssertionReaction& reaction ) override; + void handleUnexpectedInflightException + ( AssertionInfo const& info, + std::string const& message, + AssertionReaction& reaction ) override; + void handleIncomplete + ( AssertionInfo const& info ) override; + void handleNonExpr + ( AssertionInfo const &info, + ResultWas::OfType resultType, + AssertionReaction &reaction ) override; + + bool sectionStarted( SectionInfo const& sectionInfo, Counts& assertions ) override; + + void sectionEnded( SectionEndInfo const& endInfo ) override; + void sectionEndedEarly( SectionEndInfo const& endInfo ) override; + + void benchmarkStarting( BenchmarkInfo const& info ) override; + void benchmarkEnded( BenchmarkStats const& stats ) override; + + void pushScopedMessage( MessageInfo const& message ) override; + void popScopedMessage( MessageInfo const& message ) override; + + std::string getCurrentTestName() const override; + + const AssertionResult* getLastResult() const override; + + void exceptionEarlyReported() override; + + void handleFatalErrorCondition( StringRef message ) override; + + bool lastAssertionPassed() override; + + void assertionPassed() override; + + public: + // !TBD We need to do this another way! + bool aborting() const final; + + private: + + void runCurrentTest( std::string& redirectedCout, std::string& redirectedCerr ); + void invokeActiveTestCase(); + + void resetAssertionInfo(); + bool testForMissingAssertions( Counts& assertions ); + + void assertionEnded( AssertionResult const& result ); + void reportExpr + ( AssertionInfo const &info, + ResultWas::OfType resultType, + ITransientExpression const *expr, + bool negated ); + + void populateReaction( AssertionReaction& reaction ); + + private: + + void handleUnfinishedSections(); + + TestRunInfo m_runInfo; + IMutableContext& m_context; + TestCase const* m_activeTestCase = nullptr; + ITracker* m_testCaseTracker; + Option m_lastResult; + + IConfigPtr m_config; + Totals m_totals; + IStreamingReporterPtr m_reporter; + std::vector m_messages; + AssertionInfo m_lastAssertionInfo; + std::vector m_unfinishedSections; + std::vector m_activeSections; + TrackerContext m_trackerContext; + bool m_lastAssertionPassed = false; + bool m_shouldReportUnexpected = true; + bool m_includeSuccessfulResults; + }; + +} // end namespace Catch + +// end catch_run_context.h +namespace Catch { + + auto operator <<( std::ostream& os, ITransientExpression const& expr ) -> std::ostream& { + expr.streamReconstructedExpression( os ); + return os; + } + + LazyExpression::LazyExpression( bool isNegated ) + : m_isNegated( isNegated ) + {} + + LazyExpression::LazyExpression( LazyExpression const& other ) : m_isNegated( other.m_isNegated ) {} + + LazyExpression::operator bool() const { + return m_transientExpression != nullptr; + } + + auto operator << ( std::ostream& os, LazyExpression const& lazyExpr ) -> std::ostream& { + if( lazyExpr.m_isNegated ) + os << "!"; + + if( lazyExpr ) { + if( lazyExpr.m_isNegated && lazyExpr.m_transientExpression->isBinaryExpression() ) + os << "(" << *lazyExpr.m_transientExpression << ")"; + else + os << *lazyExpr.m_transientExpression; + } + else { + os << "{** error - unchecked empty expression requested **}"; + } + return os; + } + + AssertionHandler::AssertionHandler + ( StringRef macroName, + SourceLineInfo const& lineInfo, + StringRef capturedExpression, + ResultDisposition::Flags resultDisposition ) + : m_assertionInfo{ macroName, lineInfo, capturedExpression, resultDisposition }, + m_resultCapture( getResultCapture() ) + {} + + void AssertionHandler::handleExpr( ITransientExpression const& expr ) { + m_resultCapture.handleExpr( m_assertionInfo, expr, m_reaction ); + } + void AssertionHandler::handleMessage(ResultWas::OfType resultType, StringRef const& message) { + m_resultCapture.handleMessage( m_assertionInfo, resultType, message, m_reaction ); + } + + auto AssertionHandler::allowThrows() const -> bool { + return getCurrentContext().getConfig()->allowThrows(); + } + + void AssertionHandler::complete() { + setCompleted(); + if( m_reaction.shouldDebugBreak ) { + + // If you find your debugger stopping you here then go one level up on the + // call-stack for the code that caused it (typically a failed assertion) + + // (To go back to the test and change execution, jump over the throw, next) + CATCH_BREAK_INTO_DEBUGGER(); + } + if( m_reaction.shouldThrow ) + throw Catch::TestFailureException(); + } + void AssertionHandler::setCompleted() { + m_completed = true; + } + + void AssertionHandler::handleUnexpectedInflightException() { + m_resultCapture.handleUnexpectedInflightException( m_assertionInfo, Catch::translateActiveException(), m_reaction ); + } + + void AssertionHandler::handleExceptionThrownAsExpected() { + m_resultCapture.handleNonExpr(m_assertionInfo, ResultWas::Ok, m_reaction); + } + void AssertionHandler::handleExceptionNotThrownAsExpected() { + m_resultCapture.handleNonExpr(m_assertionInfo, ResultWas::Ok, m_reaction); + } + + void AssertionHandler::handleUnexpectedExceptionNotThrown() { + m_resultCapture.handleUnexpectedExceptionNotThrown( m_assertionInfo, m_reaction ); + } + + void AssertionHandler::handleThrowingCallSkipped() { + m_resultCapture.handleNonExpr(m_assertionInfo, ResultWas::Ok, m_reaction); + } + + // This is the overload that takes a string and infers the Equals matcher from it + // The more general overload, that takes any string matcher, is in catch_capture_matchers.cpp + void handleExceptionMatchExpr( AssertionHandler& handler, std::string const& str, StringRef matcherString ) { + handleExceptionMatchExpr( handler, Matchers::Equals( str ), matcherString ); + } + +} // namespace Catch +// end catch_assertionhandler.cpp +// start catch_assertionresult.cpp + +namespace Catch { + AssertionResultData::AssertionResultData(ResultWas::OfType _resultType, LazyExpression const & _lazyExpression): + lazyExpression(_lazyExpression), + resultType(_resultType) {} + + std::string AssertionResultData::reconstructExpression() const { + + if( reconstructedExpression.empty() ) { + if( lazyExpression ) { + ReusableStringStream rss; + rss << lazyExpression; + reconstructedExpression = rss.str(); + } + } + return reconstructedExpression; + } + + AssertionResult::AssertionResult( AssertionInfo const& info, AssertionResultData const& data ) + : m_info( info ), + m_resultData( data ) + {} + + // Result was a success + bool AssertionResult::succeeded() const { + return Catch::isOk( m_resultData.resultType ); + } + + // Result was a success, or failure is suppressed + bool AssertionResult::isOk() const { + return Catch::isOk( m_resultData.resultType ) || shouldSuppressFailure( m_info.resultDisposition ); + } + + ResultWas::OfType AssertionResult::getResultType() const { + return m_resultData.resultType; + } + + bool AssertionResult::hasExpression() const { + return m_info.capturedExpression[0] != 0; + } + + bool AssertionResult::hasMessage() const { + return !m_resultData.message.empty(); + } + + std::string AssertionResult::getExpression() const { + if( isFalseTest( m_info.resultDisposition ) ) + return "!(" + m_info.capturedExpression + ")"; + else + return m_info.capturedExpression; + } + + std::string AssertionResult::getExpressionInMacro() const { + std::string expr; + if( m_info.macroName[0] == 0 ) + expr = m_info.capturedExpression; + else { + expr.reserve( m_info.macroName.size() + m_info.capturedExpression.size() + 4 ); + expr += m_info.macroName; + expr += "( "; + expr += m_info.capturedExpression; + expr += " )"; + } + return expr; + } + + bool AssertionResult::hasExpandedExpression() const { + return hasExpression() && getExpandedExpression() != getExpression(); + } + + std::string AssertionResult::getExpandedExpression() const { + std::string expr = m_resultData.reconstructExpression(); + return expr.empty() + ? getExpression() + : expr; + } + + std::string AssertionResult::getMessage() const { + return m_resultData.message; + } + SourceLineInfo AssertionResult::getSourceInfo() const { + return m_info.lineInfo; + } + + StringRef AssertionResult::getTestMacroName() const { + return m_info.macroName; + } + +} // end namespace Catch +// end catch_assertionresult.cpp +// start catch_benchmark.cpp + +namespace Catch { + + auto BenchmarkLooper::getResolution() -> uint64_t { + return getEstimatedClockResolution() * getCurrentContext().getConfig()->benchmarkResolutionMultiple(); + } + + void BenchmarkLooper::reportStart() { + getResultCapture().benchmarkStarting( { m_name } ); + } + auto BenchmarkLooper::needsMoreIterations() -> bool { + auto elapsed = m_timer.getElapsedNanoseconds(); + + // Exponentially increasing iterations until we're confident in our timer resolution + if( elapsed < m_resolution ) { + m_iterationsToRun *= 10; + return true; + } + + getResultCapture().benchmarkEnded( { { m_name }, m_count, elapsed } ); + return false; + } + +} // end namespace Catch +// end catch_benchmark.cpp +// start catch_capture_matchers.cpp + +namespace Catch { + + using StringMatcher = Matchers::Impl::MatcherBase; + + // This is the general overload that takes a any string matcher + // There is another overload, in catch_assertionhandler.h/.cpp, that only takes a string and infers + // the Equals matcher (so the header does not mention matchers) + void handleExceptionMatchExpr( AssertionHandler& handler, StringMatcher const& matcher, StringRef matcherString ) { + std::string exceptionMessage = Catch::translateActiveException(); + MatchExpr expr( exceptionMessage, matcher, matcherString ); + handler.handleExpr( expr ); + } + +} // namespace Catch +// end catch_capture_matchers.cpp +// start catch_commandline.cpp + +// start catch_commandline.h + +// start catch_clara.h + +// Use Catch's value for console width (store Clara's off to the side, if present) +#ifdef CLARA_CONFIG_CONSOLE_WIDTH +#define CATCH_TEMP_CLARA_CONFIG_CONSOLE_WIDTH CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH +#undef CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH +#endif +#define CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH CATCH_CONFIG_CONSOLE_WIDTH-1 + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wweak-vtables" +#pragma clang diagnostic ignored "-Wexit-time-destructors" +#pragma clang diagnostic ignored "-Wshadow" +#endif + +// start clara.hpp +// Copyright 2017 Two Blue Cubes Ltd. All rights reserved. +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See https://github.com/philsquared/Clara for more details + +// Clara v1.1.4 + + +#ifndef CATCH_CLARA_CONFIG_CONSOLE_WIDTH +#define CATCH_CLARA_CONFIG_CONSOLE_WIDTH 80 +#endif + +#ifndef CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH +#define CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH CATCH_CLARA_CONFIG_CONSOLE_WIDTH +#endif + +#ifndef CLARA_CONFIG_OPTIONAL_TYPE +#ifdef __has_include +#if __has_include() && __cplusplus >= 201703L +#include +#define CLARA_CONFIG_OPTIONAL_TYPE std::optional +#endif +#endif +#endif + +// ----------- #included from clara_textflow.hpp ----------- + +// TextFlowCpp +// +// A single-header library for wrapping and laying out basic text, by Phil Nash +// +// This work is licensed under the BSD 2-Clause license. +// See the accompanying LICENSE file, or the one at https://opensource.org/licenses/BSD-2-Clause +// +// This project is hosted at https://github.com/philsquared/textflowcpp + + +#include +#include +#include +#include + +#ifndef CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH +#define CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH 80 +#endif + +namespace Catch { namespace clara { namespace TextFlow { + + inline auto isWhitespace( char c ) -> bool { + static std::string chars = " \t\n\r"; + return chars.find( c ) != std::string::npos; + } + inline auto isBreakableBefore( char c ) -> bool { + static std::string chars = "[({<|"; + return chars.find( c ) != std::string::npos; + } + inline auto isBreakableAfter( char c ) -> bool { + static std::string chars = "])}>.,:;*+-=&/\\"; + return chars.find( c ) != std::string::npos; + } + + class Columns; + + class Column { + std::vector m_strings; + size_t m_width = CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH; + size_t m_indent = 0; + size_t m_initialIndent = std::string::npos; + + public: + class iterator { + friend Column; + + Column const& m_column; + size_t m_stringIndex = 0; + size_t m_pos = 0; + + size_t m_len = 0; + size_t m_end = 0; + bool m_suffix = false; + + iterator( Column const& column, size_t stringIndex ) + : m_column( column ), + m_stringIndex( stringIndex ) + {} + + auto line() const -> std::string const& { return m_column.m_strings[m_stringIndex]; } + + auto isBoundary( size_t at ) const -> bool { + assert( at > 0 ); + assert( at <= line().size() ); + + return at == line().size() || + ( isWhitespace( line()[at] ) && !isWhitespace( line()[at-1] ) ) || + isBreakableBefore( line()[at] ) || + isBreakableAfter( line()[at-1] ); + } + + void calcLength() { + assert( m_stringIndex < m_column.m_strings.size() ); + + m_suffix = false; + auto width = m_column.m_width-indent(); + m_end = m_pos; + while( m_end < line().size() && line()[m_end] != '\n' ) + ++m_end; + + if( m_end < m_pos + width ) { + m_len = m_end - m_pos; + } + else { + size_t len = width; + while (len > 0 && !isBoundary(m_pos + len)) + --len; + while (len > 0 && isWhitespace( line()[m_pos + len - 1] )) + --len; + + if (len > 0) { + m_len = len; + } else { + m_suffix = true; + m_len = width - 1; + } + } + } + + auto indent() const -> size_t { + auto initial = m_pos == 0 && m_stringIndex == 0 ? m_column.m_initialIndent : std::string::npos; + return initial == std::string::npos ? m_column.m_indent : initial; + } + + auto addIndentAndSuffix(std::string const &plain) const -> std::string { + return std::string( indent(), ' ' ) + (m_suffix ? plain + "-" : plain); + } + + public: + explicit iterator( Column const& column ) : m_column( column ) { + assert( m_column.m_width > m_column.m_indent ); + assert( m_column.m_initialIndent == std::string::npos || m_column.m_width > m_column.m_initialIndent ); + calcLength(); + if( m_len == 0 ) + m_stringIndex++; // Empty string + } + + auto operator *() const -> std::string { + assert( m_stringIndex < m_column.m_strings.size() ); + assert( m_pos <= m_end ); + if( m_pos + m_column.m_width < m_end ) + return addIndentAndSuffix(line().substr(m_pos, m_len)); + else + return addIndentAndSuffix(line().substr(m_pos, m_end - m_pos)); + } + + auto operator ++() -> iterator& { + m_pos += m_len; + if( m_pos < line().size() && line()[m_pos] == '\n' ) + m_pos += 1; + else + while( m_pos < line().size() && isWhitespace( line()[m_pos] ) ) + ++m_pos; + + if( m_pos == line().size() ) { + m_pos = 0; + ++m_stringIndex; + } + if( m_stringIndex < m_column.m_strings.size() ) + calcLength(); + return *this; + } + auto operator ++(int) -> iterator { + iterator prev( *this ); + operator++(); + return prev; + } + + auto operator ==( iterator const& other ) const -> bool { + return + m_pos == other.m_pos && + m_stringIndex == other.m_stringIndex && + &m_column == &other.m_column; + } + auto operator !=( iterator const& other ) const -> bool { + return !operator==( other ); + } + }; + using const_iterator = iterator; + + explicit Column( std::string const& text ) { m_strings.push_back( text ); } + + auto width( size_t newWidth ) -> Column& { + assert( newWidth > 0 ); + m_width = newWidth; + return *this; + } + auto indent( size_t newIndent ) -> Column& { + m_indent = newIndent; + return *this; + } + auto initialIndent( size_t newIndent ) -> Column& { + m_initialIndent = newIndent; + return *this; + } + + auto width() const -> size_t { return m_width; } + auto begin() const -> iterator { return iterator( *this ); } + auto end() const -> iterator { return { *this, m_strings.size() }; } + + inline friend std::ostream& operator << ( std::ostream& os, Column const& col ) { + bool first = true; + for( auto line : col ) { + if( first ) + first = false; + else + os << "\n"; + os << line; + } + return os; + } + + auto operator + ( Column const& other ) -> Columns; + + auto toString() const -> std::string { + std::ostringstream oss; + oss << *this; + return oss.str(); + } + }; + + class Spacer : public Column { + + public: + explicit Spacer( size_t spaceWidth ) : Column( "" ) { + width( spaceWidth ); + } + }; + + class Columns { + std::vector m_columns; + + public: + + class iterator { + friend Columns; + struct EndTag {}; + + std::vector const& m_columns; + std::vector m_iterators; + size_t m_activeIterators; + + iterator( Columns const& columns, EndTag ) + : m_columns( columns.m_columns ), + m_activeIterators( 0 ) + { + m_iterators.reserve( m_columns.size() ); + + for( auto const& col : m_columns ) + m_iterators.push_back( col.end() ); + } + + public: + explicit iterator( Columns const& columns ) + : m_columns( columns.m_columns ), + m_activeIterators( m_columns.size() ) + { + m_iterators.reserve( m_columns.size() ); + + for( auto const& col : m_columns ) + m_iterators.push_back( col.begin() ); + } + + auto operator ==( iterator const& other ) const -> bool { + return m_iterators == other.m_iterators; + } + auto operator !=( iterator const& other ) const -> bool { + return m_iterators != other.m_iterators; + } + auto operator *() const -> std::string { + std::string row, padding; + + for( size_t i = 0; i < m_columns.size(); ++i ) { + auto width = m_columns[i].width(); + if( m_iterators[i] != m_columns[i].end() ) { + std::string col = *m_iterators[i]; + row += padding + col; + if( col.size() < width ) + padding = std::string( width - col.size(), ' ' ); + else + padding = ""; + } + else { + padding += std::string( width, ' ' ); + } + } + return row; + } + auto operator ++() -> iterator& { + for( size_t i = 0; i < m_columns.size(); ++i ) { + if (m_iterators[i] != m_columns[i].end()) + ++m_iterators[i]; + } + return *this; + } + auto operator ++(int) -> iterator { + iterator prev( *this ); + operator++(); + return prev; + } + }; + using const_iterator = iterator; + + auto begin() const -> iterator { return iterator( *this ); } + auto end() const -> iterator { return { *this, iterator::EndTag() }; } + + auto operator += ( Column const& col ) -> Columns& { + m_columns.push_back( col ); + return *this; + } + auto operator + ( Column const& col ) -> Columns { + Columns combined = *this; + combined += col; + return combined; + } + + inline friend std::ostream& operator << ( std::ostream& os, Columns const& cols ) { + + bool first = true; + for( auto line : cols ) { + if( first ) + first = false; + else + os << "\n"; + os << line; + } + return os; + } + + auto toString() const -> std::string { + std::ostringstream oss; + oss << *this; + return oss.str(); + } + }; + + inline auto Column::operator + ( Column const& other ) -> Columns { + Columns cols; + cols += *this; + cols += other; + return cols; + } +}}} // namespace Catch::clara::TextFlow + +// ----------- end of #include from clara_textflow.hpp ----------- +// ........... back in clara.hpp + +#include +#include +#include + +#if !defined(CATCH_PLATFORM_WINDOWS) && ( defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) ) +#define CATCH_PLATFORM_WINDOWS +#endif + +namespace Catch { namespace clara { +namespace detail { + + // Traits for extracting arg and return type of lambdas (for single argument lambdas) + template + struct UnaryLambdaTraits : UnaryLambdaTraits {}; + + template + struct UnaryLambdaTraits { + static const bool isValid = false; + }; + + template + struct UnaryLambdaTraits { + static const bool isValid = true; + using ArgType = typename std::remove_const::type>::type; + using ReturnType = ReturnT; + }; + + class TokenStream; + + // Transport for raw args (copied from main args, or supplied via init list for testing) + class Args { + friend TokenStream; + std::string m_exeName; + std::vector m_args; + + public: + Args( int argc, char const* const* argv ) + : m_exeName(argv[0]), + m_args(argv + 1, argv + argc) {} + + Args( std::initializer_list args ) + : m_exeName( *args.begin() ), + m_args( args.begin()+1, args.end() ) + {} + + auto exeName() const -> std::string { + return m_exeName; + } + }; + + // Wraps a token coming from a token stream. These may not directly correspond to strings as a single string + // may encode an option + its argument if the : or = form is used + enum class TokenType { + Option, Argument + }; + struct Token { + TokenType type; + std::string token; + }; + + inline auto isOptPrefix( char c ) -> bool { + return c == '-' +#ifdef CATCH_PLATFORM_WINDOWS + || c == '/' +#endif + ; + } + + // Abstracts iterators into args as a stream of tokens, with option arguments uniformly handled + class TokenStream { + using Iterator = std::vector::const_iterator; + Iterator it; + Iterator itEnd; + std::vector m_tokenBuffer; + + void loadBuffer() { + m_tokenBuffer.resize( 0 ); + + // Skip any empty strings + while( it != itEnd && it->empty() ) + ++it; + + if( it != itEnd ) { + auto const &next = *it; + if( isOptPrefix( next[0] ) ) { + auto delimiterPos = next.find_first_of( " :=" ); + if( delimiterPos != std::string::npos ) { + m_tokenBuffer.push_back( { TokenType::Option, next.substr( 0, delimiterPos ) } ); + m_tokenBuffer.push_back( { TokenType::Argument, next.substr( delimiterPos + 1 ) } ); + } else { + if( next[1] != '-' && next.size() > 2 ) { + std::string opt = "- "; + for( size_t i = 1; i < next.size(); ++i ) { + opt[1] = next[i]; + m_tokenBuffer.push_back( { TokenType::Option, opt } ); + } + } else { + m_tokenBuffer.push_back( { TokenType::Option, next } ); + } + } + } else { + m_tokenBuffer.push_back( { TokenType::Argument, next } ); + } + } + } + + public: + explicit TokenStream( Args const &args ) : TokenStream( args.m_args.begin(), args.m_args.end() ) {} + + TokenStream( Iterator it, Iterator itEnd ) : it( it ), itEnd( itEnd ) { + loadBuffer(); + } + + explicit operator bool() const { + return !m_tokenBuffer.empty() || it != itEnd; + } + + auto count() const -> size_t { return m_tokenBuffer.size() + (itEnd - it); } + + auto operator*() const -> Token { + assert( !m_tokenBuffer.empty() ); + return m_tokenBuffer.front(); + } + + auto operator->() const -> Token const * { + assert( !m_tokenBuffer.empty() ); + return &m_tokenBuffer.front(); + } + + auto operator++() -> TokenStream & { + if( m_tokenBuffer.size() >= 2 ) { + m_tokenBuffer.erase( m_tokenBuffer.begin() ); + } else { + if( it != itEnd ) + ++it; + loadBuffer(); + } + return *this; + } + }; + + class ResultBase { + public: + enum Type { + Ok, LogicError, RuntimeError + }; + + protected: + ResultBase( Type type ) : m_type( type ) {} + virtual ~ResultBase() = default; + + virtual void enforceOk() const = 0; + + Type m_type; + }; + + template + class ResultValueBase : public ResultBase { + public: + auto value() const -> T const & { + enforceOk(); + return m_value; + } + + protected: + ResultValueBase( Type type ) : ResultBase( type ) {} + + ResultValueBase( ResultValueBase const &other ) : ResultBase( other ) { + if( m_type == ResultBase::Ok ) + new( &m_value ) T( other.m_value ); + } + + ResultValueBase( Type, T const &value ) : ResultBase( Ok ) { + new( &m_value ) T( value ); + } + + auto operator=( ResultValueBase const &other ) -> ResultValueBase & { + if( m_type == ResultBase::Ok ) + m_value.~T(); + ResultBase::operator=(other); + if( m_type == ResultBase::Ok ) + new( &m_value ) T( other.m_value ); + return *this; + } + + ~ResultValueBase() override { + if( m_type == Ok ) + m_value.~T(); + } + + union { + T m_value; + }; + }; + + template<> + class ResultValueBase : public ResultBase { + protected: + using ResultBase::ResultBase; + }; + + template + class BasicResult : public ResultValueBase { + public: + template + explicit BasicResult( BasicResult const &other ) + : ResultValueBase( other.type() ), + m_errorMessage( other.errorMessage() ) + { + assert( type() != ResultBase::Ok ); + } + + template + static auto ok( U const &value ) -> BasicResult { return { ResultBase::Ok, value }; } + static auto ok() -> BasicResult { return { ResultBase::Ok }; } + static auto logicError( std::string const &message ) -> BasicResult { return { ResultBase::LogicError, message }; } + static auto runtimeError( std::string const &message ) -> BasicResult { return { ResultBase::RuntimeError, message }; } + + explicit operator bool() const { return m_type == ResultBase::Ok; } + auto type() const -> ResultBase::Type { return m_type; } + auto errorMessage() const -> std::string { return m_errorMessage; } + + protected: + void enforceOk() const override { + + // Errors shouldn't reach this point, but if they do + // the actual error message will be in m_errorMessage + assert( m_type != ResultBase::LogicError ); + assert( m_type != ResultBase::RuntimeError ); + if( m_type != ResultBase::Ok ) + std::abort(); + } + + std::string m_errorMessage; // Only populated if resultType is an error + + BasicResult( ResultBase::Type type, std::string const &message ) + : ResultValueBase(type), + m_errorMessage(message) + { + assert( m_type != ResultBase::Ok ); + } + + using ResultValueBase::ResultValueBase; + using ResultBase::m_type; + }; + + enum class ParseResultType { + Matched, NoMatch, ShortCircuitAll, ShortCircuitSame + }; + + class ParseState { + public: + + ParseState( ParseResultType type, TokenStream const &remainingTokens ) + : m_type(type), + m_remainingTokens( remainingTokens ) + {} + + auto type() const -> ParseResultType { return m_type; } + auto remainingTokens() const -> TokenStream { return m_remainingTokens; } + + private: + ParseResultType m_type; + TokenStream m_remainingTokens; + }; + + using Result = BasicResult; + using ParserResult = BasicResult; + using InternalParseResult = BasicResult; + + struct HelpColumns { + std::string left; + std::string right; + }; + + template + inline auto convertInto( std::string const &source, T& target ) -> ParserResult { + std::stringstream ss; + ss << source; + ss >> target; + if( ss.fail() ) + return ParserResult::runtimeError( "Unable to convert '" + source + "' to destination type" ); + else + return ParserResult::ok( ParseResultType::Matched ); + } + inline auto convertInto( std::string const &source, std::string& target ) -> ParserResult { + target = source; + return ParserResult::ok( ParseResultType::Matched ); + } + inline auto convertInto( std::string const &source, bool &target ) -> ParserResult { + std::string srcLC = source; + std::transform( srcLC.begin(), srcLC.end(), srcLC.begin(), []( char c ) { return static_cast( ::tolower(c) ); } ); + if (srcLC == "y" || srcLC == "1" || srcLC == "true" || srcLC == "yes" || srcLC == "on") + target = true; + else if (srcLC == "n" || srcLC == "0" || srcLC == "false" || srcLC == "no" || srcLC == "off") + target = false; + else + return ParserResult::runtimeError( "Expected a boolean value but did not recognise: '" + source + "'" ); + return ParserResult::ok( ParseResultType::Matched ); + } +#ifdef CLARA_CONFIG_OPTIONAL_TYPE + template + inline auto convertInto( std::string const &source, CLARA_CONFIG_OPTIONAL_TYPE& target ) -> ParserResult { + T temp; + auto result = convertInto( source, temp ); + if( result ) + target = std::move(temp); + return result; + } +#endif // CLARA_CONFIG_OPTIONAL_TYPE + + struct NonCopyable { + NonCopyable() = default; + NonCopyable( NonCopyable const & ) = delete; + NonCopyable( NonCopyable && ) = delete; + NonCopyable &operator=( NonCopyable const & ) = delete; + NonCopyable &operator=( NonCopyable && ) = delete; + }; + + struct BoundRef : NonCopyable { + virtual ~BoundRef() = default; + virtual auto isContainer() const -> bool { return false; } + virtual auto isFlag() const -> bool { return false; } + }; + struct BoundValueRefBase : BoundRef { + virtual auto setValue( std::string const &arg ) -> ParserResult = 0; + }; + struct BoundFlagRefBase : BoundRef { + virtual auto setFlag( bool flag ) -> ParserResult = 0; + virtual auto isFlag() const -> bool { return true; } + }; + + template + struct BoundValueRef : BoundValueRefBase { + T &m_ref; + + explicit BoundValueRef( T &ref ) : m_ref( ref ) {} + + auto setValue( std::string const &arg ) -> ParserResult override { + return convertInto( arg, m_ref ); + } + }; + + template + struct BoundValueRef> : BoundValueRefBase { + std::vector &m_ref; + + explicit BoundValueRef( std::vector &ref ) : m_ref( ref ) {} + + auto isContainer() const -> bool override { return true; } + + auto setValue( std::string const &arg ) -> ParserResult override { + T temp; + auto result = convertInto( arg, temp ); + if( result ) + m_ref.push_back( temp ); + return result; + } + }; + + struct BoundFlagRef : BoundFlagRefBase { + bool &m_ref; + + explicit BoundFlagRef( bool &ref ) : m_ref( ref ) {} + + auto setFlag( bool flag ) -> ParserResult override { + m_ref = flag; + return ParserResult::ok( ParseResultType::Matched ); + } + }; + + template + struct LambdaInvoker { + static_assert( std::is_same::value, "Lambda must return void or clara::ParserResult" ); + + template + static auto invoke( L const &lambda, ArgType const &arg ) -> ParserResult { + return lambda( arg ); + } + }; + + template<> + struct LambdaInvoker { + template + static auto invoke( L const &lambda, ArgType const &arg ) -> ParserResult { + lambda( arg ); + return ParserResult::ok( ParseResultType::Matched ); + } + }; + + template + inline auto invokeLambda( L const &lambda, std::string const &arg ) -> ParserResult { + ArgType temp{}; + auto result = convertInto( arg, temp ); + return !result + ? result + : LambdaInvoker::ReturnType>::invoke( lambda, temp ); + } + + template + struct BoundLambda : BoundValueRefBase { + L m_lambda; + + static_assert( UnaryLambdaTraits::isValid, "Supplied lambda must take exactly one argument" ); + explicit BoundLambda( L const &lambda ) : m_lambda( lambda ) {} + + auto setValue( std::string const &arg ) -> ParserResult override { + return invokeLambda::ArgType>( m_lambda, arg ); + } + }; + + template + struct BoundFlagLambda : BoundFlagRefBase { + L m_lambda; + + static_assert( UnaryLambdaTraits::isValid, "Supplied lambda must take exactly one argument" ); + static_assert( std::is_same::ArgType, bool>::value, "flags must be boolean" ); + + explicit BoundFlagLambda( L const &lambda ) : m_lambda( lambda ) {} + + auto setFlag( bool flag ) -> ParserResult override { + return LambdaInvoker::ReturnType>::invoke( m_lambda, flag ); + } + }; + + enum class Optionality { Optional, Required }; + + struct Parser; + + class ParserBase { + public: + virtual ~ParserBase() = default; + virtual auto validate() const -> Result { return Result::ok(); } + virtual auto parse( std::string const& exeName, TokenStream const &tokens) const -> InternalParseResult = 0; + virtual auto cardinality() const -> size_t { return 1; } + + auto parse( Args const &args ) const -> InternalParseResult { + return parse( args.exeName(), TokenStream( args ) ); + } + }; + + template + class ComposableParserImpl : public ParserBase { + public: + template + auto operator|( T const &other ) const -> Parser; + + template + auto operator+( T const &other ) const -> Parser; + }; + + // Common code and state for Args and Opts + template + class ParserRefImpl : public ComposableParserImpl { + protected: + Optionality m_optionality = Optionality::Optional; + std::shared_ptr m_ref; + std::string m_hint; + std::string m_description; + + explicit ParserRefImpl( std::shared_ptr const &ref ) : m_ref( ref ) {} + + public: + template + ParserRefImpl( T &ref, std::string const &hint ) + : m_ref( std::make_shared>( ref ) ), + m_hint( hint ) + {} + + template + ParserRefImpl( LambdaT const &ref, std::string const &hint ) + : m_ref( std::make_shared>( ref ) ), + m_hint(hint) + {} + + auto operator()( std::string const &description ) -> DerivedT & { + m_description = description; + return static_cast( *this ); + } + + auto optional() -> DerivedT & { + m_optionality = Optionality::Optional; + return static_cast( *this ); + }; + + auto required() -> DerivedT & { + m_optionality = Optionality::Required; + return static_cast( *this ); + }; + + auto isOptional() const -> bool { + return m_optionality == Optionality::Optional; + } + + auto cardinality() const -> size_t override { + if( m_ref->isContainer() ) + return 0; + else + return 1; + } + + auto hint() const -> std::string { return m_hint; } + }; + + class ExeName : public ComposableParserImpl { + std::shared_ptr m_name; + std::shared_ptr m_ref; + + template + static auto makeRef(LambdaT const &lambda) -> std::shared_ptr { + return std::make_shared>( lambda) ; + } + + public: + ExeName() : m_name( std::make_shared( "" ) ) {} + + explicit ExeName( std::string &ref ) : ExeName() { + m_ref = std::make_shared>( ref ); + } + + template + explicit ExeName( LambdaT const& lambda ) : ExeName() { + m_ref = std::make_shared>( lambda ); + } + + // The exe name is not parsed out of the normal tokens, but is handled specially + auto parse( std::string const&, TokenStream const &tokens ) const -> InternalParseResult override { + return InternalParseResult::ok( ParseState( ParseResultType::NoMatch, tokens ) ); + } + + auto name() const -> std::string { return *m_name; } + auto set( std::string const& newName ) -> ParserResult { + + auto lastSlash = newName.find_last_of( "\\/" ); + auto filename = ( lastSlash == std::string::npos ) + ? newName + : newName.substr( lastSlash+1 ); + + *m_name = filename; + if( m_ref ) + return m_ref->setValue( filename ); + else + return ParserResult::ok( ParseResultType::Matched ); + } + }; + + class Arg : public ParserRefImpl { + public: + using ParserRefImpl::ParserRefImpl; + + auto parse( std::string const &, TokenStream const &tokens ) const -> InternalParseResult override { + auto validationResult = validate(); + if( !validationResult ) + return InternalParseResult( validationResult ); + + auto remainingTokens = tokens; + auto const &token = *remainingTokens; + if( token.type != TokenType::Argument ) + return InternalParseResult::ok( ParseState( ParseResultType::NoMatch, remainingTokens ) ); + + assert( !m_ref->isFlag() ); + auto valueRef = static_cast( m_ref.get() ); + + auto result = valueRef->setValue( remainingTokens->token ); + if( !result ) + return InternalParseResult( result ); + else + return InternalParseResult::ok( ParseState( ParseResultType::Matched, ++remainingTokens ) ); + } + }; + + inline auto normaliseOpt( std::string const &optName ) -> std::string { +#ifdef CATCH_PLATFORM_WINDOWS + if( optName[0] == '/' ) + return "-" + optName.substr( 1 ); + else +#endif + return optName; + } + + class Opt : public ParserRefImpl { + protected: + std::vector m_optNames; + + public: + template + explicit Opt( LambdaT const &ref ) : ParserRefImpl( std::make_shared>( ref ) ) {} + + explicit Opt( bool &ref ) : ParserRefImpl( std::make_shared( ref ) ) {} + + template + Opt( LambdaT const &ref, std::string const &hint ) : ParserRefImpl( ref, hint ) {} + + template + Opt( T &ref, std::string const &hint ) : ParserRefImpl( ref, hint ) {} + + auto operator[]( std::string const &optName ) -> Opt & { + m_optNames.push_back( optName ); + return *this; + } + + auto getHelpColumns() const -> std::vector { + std::ostringstream oss; + bool first = true; + for( auto const &opt : m_optNames ) { + if (first) + first = false; + else + oss << ", "; + oss << opt; + } + if( !m_hint.empty() ) + oss << " <" << m_hint << ">"; + return { { oss.str(), m_description } }; + } + + auto isMatch( std::string const &optToken ) const -> bool { + auto normalisedToken = normaliseOpt( optToken ); + for( auto const &name : m_optNames ) { + if( normaliseOpt( name ) == normalisedToken ) + return true; + } + return false; + } + + using ParserBase::parse; + + auto parse( std::string const&, TokenStream const &tokens ) const -> InternalParseResult override { + auto validationResult = validate(); + if( !validationResult ) + return InternalParseResult( validationResult ); + + auto remainingTokens = tokens; + if( remainingTokens && remainingTokens->type == TokenType::Option ) { + auto const &token = *remainingTokens; + if( isMatch(token.token ) ) { + if( m_ref->isFlag() ) { + auto flagRef = static_cast( m_ref.get() ); + auto result = flagRef->setFlag( true ); + if( !result ) + return InternalParseResult( result ); + if( result.value() == ParseResultType::ShortCircuitAll ) + return InternalParseResult::ok( ParseState( result.value(), remainingTokens ) ); + } else { + auto valueRef = static_cast( m_ref.get() ); + ++remainingTokens; + if( !remainingTokens ) + return InternalParseResult::runtimeError( "Expected argument following " + token.token ); + auto const &argToken = *remainingTokens; + if( argToken.type != TokenType::Argument ) + return InternalParseResult::runtimeError( "Expected argument following " + token.token ); + auto result = valueRef->setValue( argToken.token ); + if( !result ) + return InternalParseResult( result ); + if( result.value() == ParseResultType::ShortCircuitAll ) + return InternalParseResult::ok( ParseState( result.value(), remainingTokens ) ); + } + return InternalParseResult::ok( ParseState( ParseResultType::Matched, ++remainingTokens ) ); + } + } + return InternalParseResult::ok( ParseState( ParseResultType::NoMatch, remainingTokens ) ); + } + + auto validate() const -> Result override { + if( m_optNames.empty() ) + return Result::logicError( "No options supplied to Opt" ); + for( auto const &name : m_optNames ) { + if( name.empty() ) + return Result::logicError( "Option name cannot be empty" ); +#ifdef CATCH_PLATFORM_WINDOWS + if( name[0] != '-' && name[0] != '/' ) + return Result::logicError( "Option name must begin with '-' or '/'" ); +#else + if( name[0] != '-' ) + return Result::logicError( "Option name must begin with '-'" ); +#endif + } + return ParserRefImpl::validate(); + } + }; + + struct Help : Opt { + Help( bool &showHelpFlag ) + : Opt([&]( bool flag ) { + showHelpFlag = flag; + return ParserResult::ok( ParseResultType::ShortCircuitAll ); + }) + { + static_cast( *this ) + ("display usage information") + ["-?"]["-h"]["--help"] + .optional(); + } + }; + + struct Parser : ParserBase { + + mutable ExeName m_exeName; + std::vector m_options; + std::vector m_args; + + auto operator|=( ExeName const &exeName ) -> Parser & { + m_exeName = exeName; + return *this; + } + + auto operator|=( Arg const &arg ) -> Parser & { + m_args.push_back(arg); + return *this; + } + + auto operator|=( Opt const &opt ) -> Parser & { + m_options.push_back(opt); + return *this; + } + + auto operator|=( Parser const &other ) -> Parser & { + m_options.insert(m_options.end(), other.m_options.begin(), other.m_options.end()); + m_args.insert(m_args.end(), other.m_args.begin(), other.m_args.end()); + return *this; + } + + template + auto operator|( T const &other ) const -> Parser { + return Parser( *this ) |= other; + } + + // Forward deprecated interface with '+' instead of '|' + template + auto operator+=( T const &other ) -> Parser & { return operator|=( other ); } + template + auto operator+( T const &other ) const -> Parser { return operator|( other ); } + + auto getHelpColumns() const -> std::vector { + std::vector cols; + for (auto const &o : m_options) { + auto childCols = o.getHelpColumns(); + cols.insert( cols.end(), childCols.begin(), childCols.end() ); + } + return cols; + } + + void writeToStream( std::ostream &os ) const { + if (!m_exeName.name().empty()) { + os << "usage:\n" << " " << m_exeName.name() << " "; + bool required = true, first = true; + for( auto const &arg : m_args ) { + if (first) + first = false; + else + os << " "; + if( arg.isOptional() && required ) { + os << "["; + required = false; + } + os << "<" << arg.hint() << ">"; + if( arg.cardinality() == 0 ) + os << " ... "; + } + if( !required ) + os << "]"; + if( !m_options.empty() ) + os << " options"; + os << "\n\nwhere options are:" << std::endl; + } + + auto rows = getHelpColumns(); + size_t consoleWidth = CATCH_CLARA_CONFIG_CONSOLE_WIDTH; + size_t optWidth = 0; + for( auto const &cols : rows ) + optWidth = (std::max)(optWidth, cols.left.size() + 2); + + optWidth = (std::min)(optWidth, consoleWidth/2); + + for( auto const &cols : rows ) { + auto row = + TextFlow::Column( cols.left ).width( optWidth ).indent( 2 ) + + TextFlow::Spacer(4) + + TextFlow::Column( cols.right ).width( consoleWidth - 7 - optWidth ); + os << row << std::endl; + } + } + + friend auto operator<<( std::ostream &os, Parser const &parser ) -> std::ostream& { + parser.writeToStream( os ); + return os; + } + + auto validate() const -> Result override { + for( auto const &opt : m_options ) { + auto result = opt.validate(); + if( !result ) + return result; + } + for( auto const &arg : m_args ) { + auto result = arg.validate(); + if( !result ) + return result; + } + return Result::ok(); + } + + using ParserBase::parse; + + auto parse( std::string const& exeName, TokenStream const &tokens ) const -> InternalParseResult override { + + struct ParserInfo { + ParserBase const* parser = nullptr; + size_t count = 0; + }; + const size_t totalParsers = m_options.size() + m_args.size(); + assert( totalParsers < 512 ); + // ParserInfo parseInfos[totalParsers]; // <-- this is what we really want to do + ParserInfo parseInfos[512]; + + { + size_t i = 0; + for (auto const &opt : m_options) parseInfos[i++].parser = &opt; + for (auto const &arg : m_args) parseInfos[i++].parser = &arg; + } + + m_exeName.set( exeName ); + + auto result = InternalParseResult::ok( ParseState( ParseResultType::NoMatch, tokens ) ); + while( result.value().remainingTokens() ) { + bool tokenParsed = false; + + for( size_t i = 0; i < totalParsers; ++i ) { + auto& parseInfo = parseInfos[i]; + if( parseInfo.parser->cardinality() == 0 || parseInfo.count < parseInfo.parser->cardinality() ) { + result = parseInfo.parser->parse(exeName, result.value().remainingTokens()); + if (!result) + return result; + if (result.value().type() != ParseResultType::NoMatch) { + tokenParsed = true; + ++parseInfo.count; + break; + } + } + } + + if( result.value().type() == ParseResultType::ShortCircuitAll ) + return result; + if( !tokenParsed ) + return InternalParseResult::runtimeError( "Unrecognised token: " + result.value().remainingTokens()->token ); + } + // !TBD Check missing required options + return result; + } + }; + + template + template + auto ComposableParserImpl::operator|( T const &other ) const -> Parser { + return Parser() | static_cast( *this ) | other; + } +} // namespace detail + +// A Combined parser +using detail::Parser; + +// A parser for options +using detail::Opt; + +// A parser for arguments +using detail::Arg; + +// Wrapper for argc, argv from main() +using detail::Args; + +// Specifies the name of the executable +using detail::ExeName; + +// Convenience wrapper for option parser that specifies the help option +using detail::Help; + +// enum of result types from a parse +using detail::ParseResultType; + +// Result type for parser operation +using detail::ParserResult; + +}} // namespace Catch::clara + +// end clara.hpp +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +// Restore Clara's value for console width, if present +#ifdef CATCH_TEMP_CLARA_CONFIG_CONSOLE_WIDTH +#define CATCH_CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH CATCH_TEMP_CLARA_CONFIG_CONSOLE_WIDTH +#undef CATCH_TEMP_CLARA_CONFIG_CONSOLE_WIDTH +#endif + +// end catch_clara.h +namespace Catch { + + clara::Parser makeCommandLineParser( ConfigData& config ); + +} // end namespace Catch + +// end catch_commandline.h +#include +#include + +namespace Catch { + + clara::Parser makeCommandLineParser( ConfigData& config ) { + + using namespace clara; + + auto const setWarning = [&]( std::string const& warning ) { + auto warningSet = [&]() { + if( warning == "NoAssertions" ) + return WarnAbout::NoAssertions; + + if ( warning == "NoTests" ) + return WarnAbout::NoTests; + + return WarnAbout::Nothing; + }(); + + if (warningSet == WarnAbout::Nothing) + return ParserResult::runtimeError( "Unrecognised warning: '" + warning + "'" ); + config.warnings = static_cast( config.warnings | warningSet ); + return ParserResult::ok( ParseResultType::Matched ); + }; + auto const loadTestNamesFromFile = [&]( std::string const& filename ) { + std::ifstream f( filename.c_str() ); + if( !f.is_open() ) + return ParserResult::runtimeError( "Unable to load input file: '" + filename + "'" ); + + std::string line; + while( std::getline( f, line ) ) { + line = trim(line); + if( !line.empty() && !startsWith( line, '#' ) ) { + if( !startsWith( line, '"' ) ) + line = '"' + line + '"'; + config.testsOrTags.push_back( line + ',' ); + } + } + return ParserResult::ok( ParseResultType::Matched ); + }; + auto const setTestOrder = [&]( std::string const& order ) { + if( startsWith( "declared", order ) ) + config.runOrder = RunTests::InDeclarationOrder; + else if( startsWith( "lexical", order ) ) + config.runOrder = RunTests::InLexicographicalOrder; + else if( startsWith( "random", order ) ) + config.runOrder = RunTests::InRandomOrder; + else + return clara::ParserResult::runtimeError( "Unrecognised ordering: '" + order + "'" ); + return ParserResult::ok( ParseResultType::Matched ); + }; + auto const setRngSeed = [&]( std::string const& seed ) { + if( seed != "time" ) + return clara::detail::convertInto( seed, config.rngSeed ); + config.rngSeed = static_cast( std::time(nullptr) ); + return ParserResult::ok( ParseResultType::Matched ); + }; + auto const setColourUsage = [&]( std::string const& useColour ) { + auto mode = toLower( useColour ); + + if( mode == "yes" ) + config.useColour = UseColour::Yes; + else if( mode == "no" ) + config.useColour = UseColour::No; + else if( mode == "auto" ) + config.useColour = UseColour::Auto; + else + return ParserResult::runtimeError( "colour mode must be one of: auto, yes or no. '" + useColour + "' not recognised" ); + return ParserResult::ok( ParseResultType::Matched ); + }; + auto const setWaitForKeypress = [&]( std::string const& keypress ) { + auto keypressLc = toLower( keypress ); + if( keypressLc == "start" ) + config.waitForKeypress = WaitForKeypress::BeforeStart; + else if( keypressLc == "exit" ) + config.waitForKeypress = WaitForKeypress::BeforeExit; + else if( keypressLc == "both" ) + config.waitForKeypress = WaitForKeypress::BeforeStartAndExit; + else + return ParserResult::runtimeError( "keypress argument must be one of: start, exit or both. '" + keypress + "' not recognised" ); + return ParserResult::ok( ParseResultType::Matched ); + }; + auto const setVerbosity = [&]( std::string const& verbosity ) { + auto lcVerbosity = toLower( verbosity ); + if( lcVerbosity == "quiet" ) + config.verbosity = Verbosity::Quiet; + else if( lcVerbosity == "normal" ) + config.verbosity = Verbosity::Normal; + else if( lcVerbosity == "high" ) + config.verbosity = Verbosity::High; + else + return ParserResult::runtimeError( "Unrecognised verbosity, '" + verbosity + "'" ); + return ParserResult::ok( ParseResultType::Matched ); + }; + + auto cli + = ExeName( config.processName ) + | Help( config.showHelp ) + | Opt( config.listTests ) + ["-l"]["--list-tests"] + ( "list all/matching test cases" ) + | Opt( config.listTags ) + ["-t"]["--list-tags"] + ( "list all/matching tags" ) + | Opt( config.showSuccessfulTests ) + ["-s"]["--success"] + ( "include successful tests in output" ) + | Opt( config.shouldDebugBreak ) + ["-b"]["--break"] + ( "break into debugger on failure" ) + | Opt( config.noThrow ) + ["-e"]["--nothrow"] + ( "skip exception tests" ) + | Opt( config.showInvisibles ) + ["-i"]["--invisibles"] + ( "show invisibles (tabs, newlines)" ) + | Opt( config.outputFilename, "filename" ) + ["-o"]["--out"] + ( "output filename" ) + | Opt( config.reporterName, "name" ) + ["-r"]["--reporter"] + ( "reporter to use (defaults to console)" ) + | Opt( config.name, "name" ) + ["-n"]["--name"] + ( "suite name" ) + | Opt( [&]( bool ){ config.abortAfter = 1; } ) + ["-a"]["--abort"] + ( "abort at first failure" ) + | Opt( [&]( int x ){ config.abortAfter = x; }, "no. failures" ) + ["-x"]["--abortx"] + ( "abort after x failures" ) + | Opt( setWarning, "warning name" ) + ["-w"]["--warn"] + ( "enable warnings" ) + | Opt( [&]( bool flag ) { config.showDurations = flag ? ShowDurations::Always : ShowDurations::Never; }, "yes|no" ) + ["-d"]["--durations"] + ( "show test durations" ) + | Opt( loadTestNamesFromFile, "filename" ) + ["-f"]["--input-file"] + ( "load test names to run from a file" ) + | Opt( config.filenamesAsTags ) + ["-#"]["--filenames-as-tags"] + ( "adds a tag for the filename" ) + | Opt( config.sectionsToRun, "section name" ) + ["-c"]["--section"] + ( "specify section to run" ) + | Opt( setVerbosity, "quiet|normal|high" ) + ["-v"]["--verbosity"] + ( "set output verbosity" ) + | Opt( config.listTestNamesOnly ) + ["--list-test-names-only"] + ( "list all/matching test cases names only" ) + | Opt( config.listReporters ) + ["--list-reporters"] + ( "list all reporters" ) + | Opt( setTestOrder, "decl|lex|rand" ) + ["--order"] + ( "test case order (defaults to decl)" ) + | Opt( setRngSeed, "'time'|number" ) + ["--rng-seed"] + ( "set a specific seed for random numbers" ) + | Opt( setColourUsage, "yes|no" ) + ["--use-colour"] + ( "should output be colourised" ) + | Opt( config.libIdentify ) + ["--libidentify"] + ( "report name and version according to libidentify standard" ) + | Opt( setWaitForKeypress, "start|exit|both" ) + ["--wait-for-keypress"] + ( "waits for a keypress before exiting" ) + | Opt( config.benchmarkResolutionMultiple, "multiplier" ) + ["--benchmark-resolution-multiple"] + ( "multiple of clock resolution to run benchmarks" ) + + | Arg( config.testsOrTags, "test name|pattern|tags" ) + ( "which test or tests to use" ); + + return cli; + } + +} // end namespace Catch +// end catch_commandline.cpp +// start catch_common.cpp + +#include +#include + +namespace Catch { + + bool SourceLineInfo::empty() const noexcept { + return file[0] == '\0'; + } + bool SourceLineInfo::operator == ( SourceLineInfo const& other ) const noexcept { + return line == other.line && (file == other.file || std::strcmp(file, other.file) == 0); + } + bool SourceLineInfo::operator < ( SourceLineInfo const& other ) const noexcept { + return line < other.line || ( line == other.line && (std::strcmp(file, other.file) < 0)); + } + + std::ostream& operator << ( std::ostream& os, SourceLineInfo const& info ) { +#ifndef __GNUG__ + os << info.file << '(' << info.line << ')'; +#else + os << info.file << ':' << info.line; +#endif + return os; + } + + std::string StreamEndStop::operator+() const { + return std::string(); + } + + NonCopyable::NonCopyable() = default; + NonCopyable::~NonCopyable() = default; + +} +// end catch_common.cpp +// start catch_config.cpp + +// start catch_enforce.h + +#include + +#define CATCH_PREPARE_EXCEPTION( type, msg ) \ + type( ( Catch::ReusableStringStream() << msg ).str() ) +#define CATCH_INTERNAL_ERROR( msg ) \ + throw CATCH_PREPARE_EXCEPTION( std::logic_error, CATCH_INTERNAL_LINEINFO << ": Internal Catch error: " << msg); +#define CATCH_ERROR( msg ) \ + throw CATCH_PREPARE_EXCEPTION( std::domain_error, msg ) +#define CATCH_ENFORCE( condition, msg ) \ + do{ if( !(condition) ) CATCH_ERROR( msg ); } while(false) + +// end catch_enforce.h +namespace Catch { + + Config::Config( ConfigData const& data ) + : m_data( data ), + m_stream( openStream() ) + { + TestSpecParser parser(ITagAliasRegistry::get()); + if (data.testsOrTags.empty()) { + parser.parse("~[.]"); // All not hidden tests + } + else { + m_hasTestFilters = true; + for( auto const& testOrTags : data.testsOrTags ) + parser.parse( testOrTags ); + } + m_testSpec = parser.testSpec(); + } + + std::string const& Config::getFilename() const { + return m_data.outputFilename ; + } + + bool Config::listTests() const { return m_data.listTests; } + bool Config::listTestNamesOnly() const { return m_data.listTestNamesOnly; } + bool Config::listTags() const { return m_data.listTags; } + bool Config::listReporters() const { return m_data.listReporters; } + + std::string Config::getProcessName() const { return m_data.processName; } + std::string const& Config::getReporterName() const { return m_data.reporterName; } + + std::vector const& Config::getTestsOrTags() const { return m_data.testsOrTags; } + std::vector const& Config::getSectionsToRun() const { return m_data.sectionsToRun; } + + TestSpec const& Config::testSpec() const { return m_testSpec; } + bool Config::hasTestFilters() const { return m_hasTestFilters; } + + bool Config::showHelp() const { return m_data.showHelp; } + + // IConfig interface + bool Config::allowThrows() const { return !m_data.noThrow; } + std::ostream& Config::stream() const { return m_stream->stream(); } + std::string Config::name() const { return m_data.name.empty() ? m_data.processName : m_data.name; } + bool Config::includeSuccessfulResults() const { return m_data.showSuccessfulTests; } + bool Config::warnAboutMissingAssertions() const { return !!(m_data.warnings & WarnAbout::NoAssertions); } + bool Config::warnAboutNoTests() const { return !!(m_data.warnings & WarnAbout::NoTests); } + ShowDurations::OrNot Config::showDurations() const { return m_data.showDurations; } + RunTests::InWhatOrder Config::runOrder() const { return m_data.runOrder; } + unsigned int Config::rngSeed() const { return m_data.rngSeed; } + int Config::benchmarkResolutionMultiple() const { return m_data.benchmarkResolutionMultiple; } + UseColour::YesOrNo Config::useColour() const { return m_data.useColour; } + bool Config::shouldDebugBreak() const { return m_data.shouldDebugBreak; } + int Config::abortAfter() const { return m_data.abortAfter; } + bool Config::showInvisibles() const { return m_data.showInvisibles; } + Verbosity Config::verbosity() const { return m_data.verbosity; } + + IStream const* Config::openStream() { + return Catch::makeStream(m_data.outputFilename); + } + +} // end namespace Catch +// end catch_config.cpp +// start catch_console_colour.cpp + +#if defined(__clang__) +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wexit-time-destructors" +#endif + +// start catch_errno_guard.h + +namespace Catch { + + class ErrnoGuard { + public: + ErrnoGuard(); + ~ErrnoGuard(); + private: + int m_oldErrno; + }; + +} + +// end catch_errno_guard.h +#include + +namespace Catch { + namespace { + + struct IColourImpl { + virtual ~IColourImpl() = default; + virtual void use( Colour::Code _colourCode ) = 0; + }; + + struct NoColourImpl : IColourImpl { + void use( Colour::Code ) {} + + static IColourImpl* instance() { + static NoColourImpl s_instance; + return &s_instance; + } + }; + + } // anon namespace +} // namespace Catch + +#if !defined( CATCH_CONFIG_COLOUR_NONE ) && !defined( CATCH_CONFIG_COLOUR_WINDOWS ) && !defined( CATCH_CONFIG_COLOUR_ANSI ) +# ifdef CATCH_PLATFORM_WINDOWS +# define CATCH_CONFIG_COLOUR_WINDOWS +# else +# define CATCH_CONFIG_COLOUR_ANSI +# endif +#endif + +#if defined ( CATCH_CONFIG_COLOUR_WINDOWS ) ///////////////////////////////////////// + +namespace Catch { +namespace { + + class Win32ColourImpl : public IColourImpl { + public: + Win32ColourImpl() : stdoutHandle( GetStdHandle(STD_OUTPUT_HANDLE) ) + { + CONSOLE_SCREEN_BUFFER_INFO csbiInfo; + GetConsoleScreenBufferInfo( stdoutHandle, &csbiInfo ); + originalForegroundAttributes = csbiInfo.wAttributes & ~( BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_INTENSITY ); + originalBackgroundAttributes = csbiInfo.wAttributes & ~( FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY ); + } + + virtual void use( Colour::Code _colourCode ) override { + switch( _colourCode ) { + case Colour::None: return setTextAttribute( originalForegroundAttributes ); + case Colour::White: return setTextAttribute( FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE ); + case Colour::Red: return setTextAttribute( FOREGROUND_RED ); + case Colour::Green: return setTextAttribute( FOREGROUND_GREEN ); + case Colour::Blue: return setTextAttribute( FOREGROUND_BLUE ); + case Colour::Cyan: return setTextAttribute( FOREGROUND_BLUE | FOREGROUND_GREEN ); + case Colour::Yellow: return setTextAttribute( FOREGROUND_RED | FOREGROUND_GREEN ); + case Colour::Grey: return setTextAttribute( 0 ); + + case Colour::LightGrey: return setTextAttribute( FOREGROUND_INTENSITY ); + case Colour::BrightRed: return setTextAttribute( FOREGROUND_INTENSITY | FOREGROUND_RED ); + case Colour::BrightGreen: return setTextAttribute( FOREGROUND_INTENSITY | FOREGROUND_GREEN ); + case Colour::BrightWhite: return setTextAttribute( FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE ); + case Colour::BrightYellow: return setTextAttribute( FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN ); + + case Colour::Bright: CATCH_INTERNAL_ERROR( "not a colour" ); + + default: + CATCH_ERROR( "Unknown colour requested" ); + } + } + + private: + void setTextAttribute( WORD _textAttribute ) { + SetConsoleTextAttribute( stdoutHandle, _textAttribute | originalBackgroundAttributes ); + } + HANDLE stdoutHandle; + WORD originalForegroundAttributes; + WORD originalBackgroundAttributes; + }; + + IColourImpl* platformColourInstance() { + static Win32ColourImpl s_instance; + + IConfigPtr config = getCurrentContext().getConfig(); + UseColour::YesOrNo colourMode = config + ? config->useColour() + : UseColour::Auto; + if( colourMode == UseColour::Auto ) + colourMode = UseColour::Yes; + return colourMode == UseColour::Yes + ? &s_instance + : NoColourImpl::instance(); + } + +} // end anon namespace +} // end namespace Catch + +#elif defined( CATCH_CONFIG_COLOUR_ANSI ) ////////////////////////////////////// + +#include + +namespace Catch { +namespace { + + // use POSIX/ ANSI console terminal codes + // Thanks to Adam Strzelecki for original contribution + // (http://github.com/nanoant) + // https://github.com/philsquared/Catch/pull/131 + class PosixColourImpl : public IColourImpl { + public: + virtual void use( Colour::Code _colourCode ) override { + switch( _colourCode ) { + case Colour::None: + case Colour::White: return setColour( "[0m" ); + case Colour::Red: return setColour( "[0;31m" ); + case Colour::Green: return setColour( "[0;32m" ); + case Colour::Blue: return setColour( "[0;34m" ); + case Colour::Cyan: return setColour( "[0;36m" ); + case Colour::Yellow: return setColour( "[0;33m" ); + case Colour::Grey: return setColour( "[1;30m" ); + + case Colour::LightGrey: return setColour( "[0;37m" ); + case Colour::BrightRed: return setColour( "[1;31m" ); + case Colour::BrightGreen: return setColour( "[1;32m" ); + case Colour::BrightWhite: return setColour( "[1;37m" ); + case Colour::BrightYellow: return setColour( "[1;33m" ); + + case Colour::Bright: CATCH_INTERNAL_ERROR( "not a colour" ); + default: CATCH_INTERNAL_ERROR( "Unknown colour requested" ); + } + } + static IColourImpl* instance() { + static PosixColourImpl s_instance; + return &s_instance; + } + + private: + void setColour( const char* _escapeCode ) { + Catch::cout() << '\033' << _escapeCode; + } + }; + + bool useColourOnPlatform() { + return +#ifdef CATCH_PLATFORM_MAC + !isDebuggerActive() && +#endif +#if !(defined(__DJGPP__) && defined(__STRICT_ANSI__)) + isatty(STDOUT_FILENO) +#else + false +#endif + ; + } + IColourImpl* platformColourInstance() { + ErrnoGuard guard; + IConfigPtr config = getCurrentContext().getConfig(); + UseColour::YesOrNo colourMode = config + ? config->useColour() + : UseColour::Auto; + if( colourMode == UseColour::Auto ) + colourMode = useColourOnPlatform() + ? UseColour::Yes + : UseColour::No; + return colourMode == UseColour::Yes + ? PosixColourImpl::instance() + : NoColourImpl::instance(); + } + +} // end anon namespace +} // end namespace Catch + +#else // not Windows or ANSI /////////////////////////////////////////////// + +namespace Catch { + + static IColourImpl* platformColourInstance() { return NoColourImpl::instance(); } + +} // end namespace Catch + +#endif // Windows/ ANSI/ None + +namespace Catch { + + Colour::Colour( Code _colourCode ) { use( _colourCode ); } + Colour::Colour( Colour&& rhs ) noexcept { + m_moved = rhs.m_moved; + rhs.m_moved = true; + } + Colour& Colour::operator=( Colour&& rhs ) noexcept { + m_moved = rhs.m_moved; + rhs.m_moved = true; + return *this; + } + + Colour::~Colour(){ if( !m_moved ) use( None ); } + + void Colour::use( Code _colourCode ) { + static IColourImpl* impl = platformColourInstance(); + impl->use( _colourCode ); + } + + std::ostream& operator << ( std::ostream& os, Colour const& ) { + return os; + } + +} // end namespace Catch + +#if defined(__clang__) +# pragma clang diagnostic pop +#endif + +// end catch_console_colour.cpp +// start catch_context.cpp + +namespace Catch { + + class Context : public IMutableContext, NonCopyable { + + public: // IContext + virtual IResultCapture* getResultCapture() override { + return m_resultCapture; + } + virtual IRunner* getRunner() override { + return m_runner; + } + + virtual IConfigPtr const& getConfig() const override { + return m_config; + } + + virtual ~Context() override; + + public: // IMutableContext + virtual void setResultCapture( IResultCapture* resultCapture ) override { + m_resultCapture = resultCapture; + } + virtual void setRunner( IRunner* runner ) override { + m_runner = runner; + } + virtual void setConfig( IConfigPtr const& config ) override { + m_config = config; + } + + friend IMutableContext& getCurrentMutableContext(); + + private: + IConfigPtr m_config; + IRunner* m_runner = nullptr; + IResultCapture* m_resultCapture = nullptr; + }; + + IMutableContext *IMutableContext::currentContext = nullptr; + + void IMutableContext::createContext() + { + currentContext = new Context(); + } + + void cleanUpContext() { + delete IMutableContext::currentContext; + IMutableContext::currentContext = nullptr; + } + IContext::~IContext() = default; + IMutableContext::~IMutableContext() = default; + Context::~Context() = default; +} +// end catch_context.cpp +// start catch_debug_console.cpp + +// start catch_debug_console.h + +#include + +namespace Catch { + void writeToDebugConsole( std::string const& text ); +} + +// end catch_debug_console.h +#ifdef CATCH_PLATFORM_WINDOWS + + namespace Catch { + void writeToDebugConsole( std::string const& text ) { + ::OutputDebugStringA( text.c_str() ); + } + } + +#else + + namespace Catch { + void writeToDebugConsole( std::string const& text ) { + // !TBD: Need a version for Mac/ XCode and other IDEs + Catch::cout() << text; + } + } + +#endif // Platform +// end catch_debug_console.cpp +// start catch_debugger.cpp + +#ifdef CATCH_PLATFORM_MAC + +# include +# include +# include +# include +# include +# include +# include + +namespace Catch { + + // The following function is taken directly from the following technical note: + // http://developer.apple.com/library/mac/#qa/qa2004/qa1361.html + + // Returns true if the current process is being debugged (either + // running under the debugger or has a debugger attached post facto). + bool isDebuggerActive(){ + + int mib[4]; + struct kinfo_proc info; + std::size_t size; + + // Initialize the flags so that, if sysctl fails for some bizarre + // reason, we get a predictable result. + + info.kp_proc.p_flag = 0; + + // Initialize mib, which tells sysctl the info we want, in this case + // we're looking for information about a specific process ID. + + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = KERN_PROC_PID; + mib[3] = getpid(); + + // Call sysctl. + + size = sizeof(info); + if( sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, nullptr, 0) != 0 ) { + Catch::cerr() << "\n** Call to sysctl failed - unable to determine if debugger is active **\n" << std::endl; + return false; + } + + // We're being debugged if the P_TRACED flag is set. + + return ( (info.kp_proc.p_flag & P_TRACED) != 0 ); + } + } // namespace Catch + +#elif defined(CATCH_PLATFORM_LINUX) + #include + #include + + namespace Catch{ + // The standard POSIX way of detecting a debugger is to attempt to + // ptrace() the process, but this needs to be done from a child and not + // this process itself to still allow attaching to this process later + // if wanted, so is rather heavy. Under Linux we have the PID of the + // "debugger" (which doesn't need to be gdb, of course, it could also + // be strace, for example) in /proc/$PID/status, so just get it from + // there instead. + bool isDebuggerActive(){ + // Libstdc++ has a bug, where std::ifstream sets errno to 0 + // This way our users can properly assert over errno values + ErrnoGuard guard; + std::ifstream in("/proc/self/status"); + for( std::string line; std::getline(in, line); ) { + static const int PREFIX_LEN = 11; + if( line.compare(0, PREFIX_LEN, "TracerPid:\t") == 0 ) { + // We're traced if the PID is not 0 and no other PID starts + // with 0 digit, so it's enough to check for just a single + // character. + return line.length() > PREFIX_LEN && line[PREFIX_LEN] != '0'; + } + } + + return false; + } + } // namespace Catch +#elif defined(_MSC_VER) + extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent(); + namespace Catch { + bool isDebuggerActive() { + return IsDebuggerPresent() != 0; + } + } +#elif defined(__MINGW32__) + extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent(); + namespace Catch { + bool isDebuggerActive() { + return IsDebuggerPresent() != 0; + } + } +#else + namespace Catch { + bool isDebuggerActive() { return false; } + } +#endif // Platform +// end catch_debugger.cpp +// start catch_decomposer.cpp + +namespace Catch { + + ITransientExpression::~ITransientExpression() = default; + + void formatReconstructedExpression( std::ostream &os, std::string const& lhs, StringRef op, std::string const& rhs ) { + if( lhs.size() + rhs.size() < 40 && + lhs.find('\n') == std::string::npos && + rhs.find('\n') == std::string::npos ) + os << lhs << " " << op << " " << rhs; + else + os << lhs << "\n" << op << "\n" << rhs; + } +} +// end catch_decomposer.cpp +// start catch_errno_guard.cpp + +#include + +namespace Catch { + ErrnoGuard::ErrnoGuard():m_oldErrno(errno){} + ErrnoGuard::~ErrnoGuard() { errno = m_oldErrno; } +} +// end catch_errno_guard.cpp +// start catch_exception_translator_registry.cpp + +// start catch_exception_translator_registry.h + +#include +#include +#include + +namespace Catch { + + class ExceptionTranslatorRegistry : public IExceptionTranslatorRegistry { + public: + ~ExceptionTranslatorRegistry(); + virtual void registerTranslator( const IExceptionTranslator* translator ); + virtual std::string translateActiveException() const override; + std::string tryTranslators() const; + + private: + std::vector> m_translators; + }; +} + +// end catch_exception_translator_registry.h +#ifdef __OBJC__ +#import "Foundation/Foundation.h" +#endif + +namespace Catch { + + ExceptionTranslatorRegistry::~ExceptionTranslatorRegistry() { + } + + void ExceptionTranslatorRegistry::registerTranslator( const IExceptionTranslator* translator ) { + m_translators.push_back( std::unique_ptr( translator ) ); + } + + std::string ExceptionTranslatorRegistry::translateActiveException() const { + try { +#ifdef __OBJC__ + // In Objective-C try objective-c exceptions first + @try { + return tryTranslators(); + } + @catch (NSException *exception) { + return Catch::Detail::stringify( [exception description] ); + } +#else + // Compiling a mixed mode project with MSVC means that CLR + // exceptions will be caught in (...) as well. However, these + // do not fill-in std::current_exception and thus lead to crash + // when attempting rethrow. + // /EHa switch also causes structured exceptions to be caught + // here, but they fill-in current_exception properly, so + // at worst the output should be a little weird, instead of + // causing a crash. + if (std::current_exception() == nullptr) { + return "Non C++ exception. Possibly a CLR exception."; + } + return tryTranslators(); +#endif + } + catch( TestFailureException& ) { + std::rethrow_exception(std::current_exception()); + } + catch( std::exception& ex ) { + return ex.what(); + } + catch( std::string& msg ) { + return msg; + } + catch( const char* msg ) { + return msg; + } + catch(...) { + return "Unknown exception"; + } + } + + std::string ExceptionTranslatorRegistry::tryTranslators() const { + if( m_translators.empty() ) + std::rethrow_exception(std::current_exception()); + else + return m_translators[0]->translate( m_translators.begin()+1, m_translators.end() ); + } +} +// end catch_exception_translator_registry.cpp +// start catch_fatal_condition.cpp + +#if defined(__GNUC__) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wmissing-field-initializers" +#endif + +#if defined( CATCH_CONFIG_WINDOWS_SEH ) || defined( CATCH_CONFIG_POSIX_SIGNALS ) + +namespace { + // Report the error condition + void reportFatal( char const * const message ) { + Catch::getCurrentContext().getResultCapture()->handleFatalErrorCondition( message ); + } +} + +#endif // signals/SEH handling + +#if defined( CATCH_CONFIG_WINDOWS_SEH ) + +namespace Catch { + struct SignalDefs { DWORD id; const char* name; }; + + // There is no 1-1 mapping between signals and windows exceptions. + // Windows can easily distinguish between SO and SigSegV, + // but SigInt, SigTerm, etc are handled differently. + static SignalDefs signalDefs[] = { + { EXCEPTION_ILLEGAL_INSTRUCTION, "SIGILL - Illegal instruction signal" }, + { EXCEPTION_STACK_OVERFLOW, "SIGSEGV - Stack overflow" }, + { EXCEPTION_ACCESS_VIOLATION, "SIGSEGV - Segmentation violation signal" }, + { EXCEPTION_INT_DIVIDE_BY_ZERO, "Divide by zero error" }, + }; + + LONG CALLBACK FatalConditionHandler::handleVectoredException(PEXCEPTION_POINTERS ExceptionInfo) { + for (auto const& def : signalDefs) { + if (ExceptionInfo->ExceptionRecord->ExceptionCode == def.id) { + reportFatal(def.name); + } + } + // If its not an exception we care about, pass it along. + // This stops us from eating debugger breaks etc. + return EXCEPTION_CONTINUE_SEARCH; + } + + FatalConditionHandler::FatalConditionHandler() { + isSet = true; + // 32k seems enough for Catch to handle stack overflow, + // but the value was found experimentally, so there is no strong guarantee + guaranteeSize = 32 * 1024; + exceptionHandlerHandle = nullptr; + // Register as first handler in current chain + exceptionHandlerHandle = AddVectoredExceptionHandler(1, handleVectoredException); + // Pass in guarantee size to be filled + SetThreadStackGuarantee(&guaranteeSize); + } + + void FatalConditionHandler::reset() { + if (isSet) { + RemoveVectoredExceptionHandler(exceptionHandlerHandle); + SetThreadStackGuarantee(&guaranteeSize); + exceptionHandlerHandle = nullptr; + isSet = false; + } + } + + FatalConditionHandler::~FatalConditionHandler() { + reset(); + } + +bool FatalConditionHandler::isSet = false; +ULONG FatalConditionHandler::guaranteeSize = 0; +PVOID FatalConditionHandler::exceptionHandlerHandle = nullptr; + +} // namespace Catch + +#elif defined( CATCH_CONFIG_POSIX_SIGNALS ) + +namespace Catch { + + struct SignalDefs { + int id; + const char* name; + }; + + // 32kb for the alternate stack seems to be sufficient. However, this value + // is experimentally determined, so that's not guaranteed. + constexpr static std::size_t sigStackSize = 32768 >= MINSIGSTKSZ ? 32768 : MINSIGSTKSZ; + + static SignalDefs signalDefs[] = { + { SIGINT, "SIGINT - Terminal interrupt signal" }, + { SIGILL, "SIGILL - Illegal instruction signal" }, + { SIGFPE, "SIGFPE - Floating point error signal" }, + { SIGSEGV, "SIGSEGV - Segmentation violation signal" }, + { SIGTERM, "SIGTERM - Termination request signal" }, + { SIGABRT, "SIGABRT - Abort (abnormal termination) signal" } + }; + + void FatalConditionHandler::handleSignal( int sig ) { + char const * name = ""; + for (auto const& def : signalDefs) { + if (sig == def.id) { + name = def.name; + break; + } + } + reset(); + reportFatal(name); + raise( sig ); + } + + FatalConditionHandler::FatalConditionHandler() { + isSet = true; + stack_t sigStack; + sigStack.ss_sp = altStackMem; + sigStack.ss_size = sigStackSize; + sigStack.ss_flags = 0; + sigaltstack(&sigStack, &oldSigStack); + struct sigaction sa = { }; + + sa.sa_handler = handleSignal; + sa.sa_flags = SA_ONSTACK; + for (std::size_t i = 0; i < sizeof(signalDefs)/sizeof(SignalDefs); ++i) { + sigaction(signalDefs[i].id, &sa, &oldSigActions[i]); + } + } + + FatalConditionHandler::~FatalConditionHandler() { + reset(); + } + + void FatalConditionHandler::reset() { + if( isSet ) { + // Set signals back to previous values -- hopefully nobody overwrote them in the meantime + for( std::size_t i = 0; i < sizeof(signalDefs)/sizeof(SignalDefs); ++i ) { + sigaction(signalDefs[i].id, &oldSigActions[i], nullptr); + } + // Return the old stack + sigaltstack(&oldSigStack, nullptr); + isSet = false; + } + } + + bool FatalConditionHandler::isSet = false; + struct sigaction FatalConditionHandler::oldSigActions[sizeof(signalDefs)/sizeof(SignalDefs)] = {}; + stack_t FatalConditionHandler::oldSigStack = {}; + char FatalConditionHandler::altStackMem[sigStackSize] = {}; + +} // namespace Catch + +#else + +namespace Catch { + void FatalConditionHandler::reset() {} +} + +#endif // signals/SEH handling + +#if defined(__GNUC__) +# pragma GCC diagnostic pop +#endif +// end catch_fatal_condition.cpp +// start catch_interfaces_capture.cpp + +namespace Catch { + IResultCapture::~IResultCapture() = default; +} +// end catch_interfaces_capture.cpp +// start catch_interfaces_config.cpp + +namespace Catch { + IConfig::~IConfig() = default; +} +// end catch_interfaces_config.cpp +// start catch_interfaces_exception.cpp + +namespace Catch { + IExceptionTranslator::~IExceptionTranslator() = default; + IExceptionTranslatorRegistry::~IExceptionTranslatorRegistry() = default; +} +// end catch_interfaces_exception.cpp +// start catch_interfaces_registry_hub.cpp + +namespace Catch { + IRegistryHub::~IRegistryHub() = default; + IMutableRegistryHub::~IMutableRegistryHub() = default; +} +// end catch_interfaces_registry_hub.cpp +// start catch_interfaces_reporter.cpp + +// start catch_reporter_listening.h + +namespace Catch { + + class ListeningReporter : public IStreamingReporter { + using Reporters = std::vector; + Reporters m_listeners; + IStreamingReporterPtr m_reporter = nullptr; + + public: + void addListener( IStreamingReporterPtr&& listener ); + void addReporter( IStreamingReporterPtr&& reporter ); + + public: // IStreamingReporter + + ReporterPreferences getPreferences() const override; + + void noMatchingTestCases( std::string const& spec ) override; + + static std::set getSupportedVerbosities(); + + void benchmarkStarting( BenchmarkInfo const& benchmarkInfo ) override; + void benchmarkEnded( BenchmarkStats const& benchmarkStats ) override; + + void testRunStarting( TestRunInfo const& testRunInfo ) override; + void testGroupStarting( GroupInfo const& groupInfo ) override; + void testCaseStarting( TestCaseInfo const& testInfo ) override; + void sectionStarting( SectionInfo const& sectionInfo ) override; + void assertionStarting( AssertionInfo const& assertionInfo ) override; + + // The return value indicates if the messages buffer should be cleared: + bool assertionEnded( AssertionStats const& assertionStats ) override; + void sectionEnded( SectionStats const& sectionStats ) override; + void testCaseEnded( TestCaseStats const& testCaseStats ) override; + void testGroupEnded( TestGroupStats const& testGroupStats ) override; + void testRunEnded( TestRunStats const& testRunStats ) override; + + void skipTest( TestCaseInfo const& testInfo ) override; + bool isMulti() const override; + + }; + +} // end namespace Catch + +// end catch_reporter_listening.h +namespace Catch { + + ReporterConfig::ReporterConfig( IConfigPtr const& _fullConfig ) + : m_stream( &_fullConfig->stream() ), m_fullConfig( _fullConfig ) {} + + ReporterConfig::ReporterConfig( IConfigPtr const& _fullConfig, std::ostream& _stream ) + : m_stream( &_stream ), m_fullConfig( _fullConfig ) {} + + std::ostream& ReporterConfig::stream() const { return *m_stream; } + IConfigPtr ReporterConfig::fullConfig() const { return m_fullConfig; } + + TestRunInfo::TestRunInfo( std::string const& _name ) : name( _name ) {} + + GroupInfo::GroupInfo( std::string const& _name, + std::size_t _groupIndex, + std::size_t _groupsCount ) + : name( _name ), + groupIndex( _groupIndex ), + groupsCounts( _groupsCount ) + {} + + AssertionStats::AssertionStats( AssertionResult const& _assertionResult, + std::vector const& _infoMessages, + Totals const& _totals ) + : assertionResult( _assertionResult ), + infoMessages( _infoMessages ), + totals( _totals ) + { + assertionResult.m_resultData.lazyExpression.m_transientExpression = _assertionResult.m_resultData.lazyExpression.m_transientExpression; + + if( assertionResult.hasMessage() ) { + // Copy message into messages list. + // !TBD This should have been done earlier, somewhere + MessageBuilder builder( assertionResult.getTestMacroName(), assertionResult.getSourceInfo(), assertionResult.getResultType() ); + builder << assertionResult.getMessage(); + builder.m_info.message = builder.m_stream.str(); + + infoMessages.push_back( builder.m_info ); + } + } + + AssertionStats::~AssertionStats() = default; + + SectionStats::SectionStats( SectionInfo const& _sectionInfo, + Counts const& _assertions, + double _durationInSeconds, + bool _missingAssertions ) + : sectionInfo( _sectionInfo ), + assertions( _assertions ), + durationInSeconds( _durationInSeconds ), + missingAssertions( _missingAssertions ) + {} + + SectionStats::~SectionStats() = default; + + TestCaseStats::TestCaseStats( TestCaseInfo const& _testInfo, + Totals const& _totals, + std::string const& _stdOut, + std::string const& _stdErr, + bool _aborting ) + : testInfo( _testInfo ), + totals( _totals ), + stdOut( _stdOut ), + stdErr( _stdErr ), + aborting( _aborting ) + {} + + TestCaseStats::~TestCaseStats() = default; + + TestGroupStats::TestGroupStats( GroupInfo const& _groupInfo, + Totals const& _totals, + bool _aborting ) + : groupInfo( _groupInfo ), + totals( _totals ), + aborting( _aborting ) + {} + + TestGroupStats::TestGroupStats( GroupInfo const& _groupInfo ) + : groupInfo( _groupInfo ), + aborting( false ) + {} + + TestGroupStats::~TestGroupStats() = default; + + TestRunStats::TestRunStats( TestRunInfo const& _runInfo, + Totals const& _totals, + bool _aborting ) + : runInfo( _runInfo ), + totals( _totals ), + aborting( _aborting ) + {} + + TestRunStats::~TestRunStats() = default; + + void IStreamingReporter::fatalErrorEncountered( StringRef ) {} + bool IStreamingReporter::isMulti() const { return false; } + + IReporterFactory::~IReporterFactory() = default; + IReporterRegistry::~IReporterRegistry() = default; + +} // end namespace Catch +// end catch_interfaces_reporter.cpp +// start catch_interfaces_runner.cpp + +namespace Catch { + IRunner::~IRunner() = default; +} +// end catch_interfaces_runner.cpp +// start catch_interfaces_testcase.cpp + +namespace Catch { + ITestInvoker::~ITestInvoker() = default; + ITestCaseRegistry::~ITestCaseRegistry() = default; +} +// end catch_interfaces_testcase.cpp +// start catch_leak_detector.cpp + +#ifdef CATCH_CONFIG_WINDOWS_CRTDBG +#include + +namespace Catch { + + LeakDetector::LeakDetector() { + int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); + flag |= _CRTDBG_LEAK_CHECK_DF; + flag |= _CRTDBG_ALLOC_MEM_DF; + _CrtSetDbgFlag(flag); + _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG); + _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR); + // Change this to leaking allocation's number to break there + _CrtSetBreakAlloc(-1); + } +} + +#else + + Catch::LeakDetector::LeakDetector() {} + +#endif +// end catch_leak_detector.cpp +// start catch_list.cpp + +// start catch_list.h + +#include + +namespace Catch { + + std::size_t listTests( Config const& config ); + + std::size_t listTestsNamesOnly( Config const& config ); + + struct TagInfo { + void add( std::string const& spelling ); + std::string all() const; + + std::set spellings; + std::size_t count = 0; + }; + + std::size_t listTags( Config const& config ); + + std::size_t listReporters( Config const& /*config*/ ); + + Option list( Config const& config ); + +} // end namespace Catch + +// end catch_list.h +// start catch_text.h + +namespace Catch { + using namespace clara::TextFlow; +} + +// end catch_text.h +#include +#include +#include + +namespace Catch { + + std::size_t listTests( Config const& config ) { + TestSpec testSpec = config.testSpec(); + if( config.hasTestFilters() ) + Catch::cout() << "Matching test cases:\n"; + else { + Catch::cout() << "All available test cases:\n"; + } + + auto matchedTestCases = filterTests( getAllTestCasesSorted( config ), testSpec, config ); + for( auto const& testCaseInfo : matchedTestCases ) { + Colour::Code colour = testCaseInfo.isHidden() + ? Colour::SecondaryText + : Colour::None; + Colour colourGuard( colour ); + + Catch::cout() << Column( testCaseInfo.name ).initialIndent( 2 ).indent( 4 ) << "\n"; + if( config.verbosity() >= Verbosity::High ) { + Catch::cout() << Column( Catch::Detail::stringify( testCaseInfo.lineInfo ) ).indent(4) << std::endl; + std::string description = testCaseInfo.description; + if( description.empty() ) + description = "(NO DESCRIPTION)"; + Catch::cout() << Column( description ).indent(4) << std::endl; + } + if( !testCaseInfo.tags.empty() ) + Catch::cout() << Column( testCaseInfo.tagsAsString() ).indent( 6 ) << "\n"; + } + + if( !config.hasTestFilters() ) + Catch::cout() << pluralise( matchedTestCases.size(), "test case" ) << '\n' << std::endl; + else + Catch::cout() << pluralise( matchedTestCases.size(), "matching test case" ) << '\n' << std::endl; + return matchedTestCases.size(); + } + + std::size_t listTestsNamesOnly( Config const& config ) { + TestSpec testSpec = config.testSpec(); + std::size_t matchedTests = 0; + std::vector matchedTestCases = filterTests( getAllTestCasesSorted( config ), testSpec, config ); + for( auto const& testCaseInfo : matchedTestCases ) { + matchedTests++; + if( startsWith( testCaseInfo.name, '#' ) ) + Catch::cout() << '"' << testCaseInfo.name << '"'; + else + Catch::cout() << testCaseInfo.name; + if ( config.verbosity() >= Verbosity::High ) + Catch::cout() << "\t@" << testCaseInfo.lineInfo; + Catch::cout() << std::endl; + } + return matchedTests; + } + + void TagInfo::add( std::string const& spelling ) { + ++count; + spellings.insert( spelling ); + } + + std::string TagInfo::all() const { + std::string out; + for( auto const& spelling : spellings ) + out += "[" + spelling + "]"; + return out; + } + + std::size_t listTags( Config const& config ) { + TestSpec testSpec = config.testSpec(); + if( config.hasTestFilters() ) + Catch::cout() << "Tags for matching test cases:\n"; + else { + Catch::cout() << "All available tags:\n"; + } + + std::map tagCounts; + + std::vector matchedTestCases = filterTests( getAllTestCasesSorted( config ), testSpec, config ); + for( auto const& testCase : matchedTestCases ) { + for( auto const& tagName : testCase.getTestCaseInfo().tags ) { + std::string lcaseTagName = toLower( tagName ); + auto countIt = tagCounts.find( lcaseTagName ); + if( countIt == tagCounts.end() ) + countIt = tagCounts.insert( std::make_pair( lcaseTagName, TagInfo() ) ).first; + countIt->second.add( tagName ); + } + } + + for( auto const& tagCount : tagCounts ) { + ReusableStringStream rss; + rss << " " << std::setw(2) << tagCount.second.count << " "; + auto str = rss.str(); + auto wrapper = Column( tagCount.second.all() ) + .initialIndent( 0 ) + .indent( str.size() ) + .width( CATCH_CONFIG_CONSOLE_WIDTH-10 ); + Catch::cout() << str << wrapper << '\n'; + } + Catch::cout() << pluralise( tagCounts.size(), "tag" ) << '\n' << std::endl; + return tagCounts.size(); + } + + std::size_t listReporters( Config const& /*config*/ ) { + Catch::cout() << "Available reporters:\n"; + IReporterRegistry::FactoryMap const& factories = getRegistryHub().getReporterRegistry().getFactories(); + std::size_t maxNameLen = 0; + for( auto const& factoryKvp : factories ) + maxNameLen = (std::max)( maxNameLen, factoryKvp.first.size() ); + + for( auto const& factoryKvp : factories ) { + Catch::cout() + << Column( factoryKvp.first + ":" ) + .indent(2) + .width( 5+maxNameLen ) + + Column( factoryKvp.second->getDescription() ) + .initialIndent(0) + .indent(2) + .width( CATCH_CONFIG_CONSOLE_WIDTH - maxNameLen-8 ) + << "\n"; + } + Catch::cout() << std::endl; + return factories.size(); + } + + Option list( Config const& config ) { + Option listedCount; + if( config.listTests() ) + listedCount = listedCount.valueOr(0) + listTests( config ); + if( config.listTestNamesOnly() ) + listedCount = listedCount.valueOr(0) + listTestsNamesOnly( config ); + if( config.listTags() ) + listedCount = listedCount.valueOr(0) + listTags( config ); + if( config.listReporters() ) + listedCount = listedCount.valueOr(0) + listReporters( config ); + return listedCount; + } + +} // end namespace Catch +// end catch_list.cpp +// start catch_matchers.cpp + +namespace Catch { +namespace Matchers { + namespace Impl { + + std::string MatcherUntypedBase::toString() const { + if( m_cachedToString.empty() ) + m_cachedToString = describe(); + return m_cachedToString; + } + + MatcherUntypedBase::~MatcherUntypedBase() = default; + + } // namespace Impl +} // namespace Matchers + +using namespace Matchers; +using Matchers::Impl::MatcherBase; + +} // namespace Catch +// end catch_matchers.cpp +// start catch_matchers_floating.cpp + +// start catch_to_string.hpp + +#include + +namespace Catch { + template + std::string to_string(T const& t) { +#if defined(CATCH_CONFIG_CPP11_TO_STRING) + return std::to_string(t); +#else + ReusableStringStream rss; + rss << t; + return rss.str(); +#endif + } +} // end namespace Catch + +// end catch_to_string.hpp +#include +#include +#include +#include + +namespace Catch { +namespace Matchers { +namespace Floating { +enum class FloatingPointKind : uint8_t { + Float, + Double +}; +} +} +} + +namespace { + +template +struct Converter; + +template <> +struct Converter { + static_assert(sizeof(float) == sizeof(int32_t), "Important ULP matcher assumption violated"); + Converter(float f) { + std::memcpy(&i, &f, sizeof(f)); + } + int32_t i; +}; + +template <> +struct Converter { + static_assert(sizeof(double) == sizeof(int64_t), "Important ULP matcher assumption violated"); + Converter(double d) { + std::memcpy(&i, &d, sizeof(d)); + } + int64_t i; +}; + +template +auto convert(T t) -> Converter { + return Converter(t); +} + +template +bool almostEqualUlps(FP lhs, FP rhs, int maxUlpDiff) { + // Comparison with NaN should always be false. + // This way we can rule it out before getting into the ugly details + if (std::isnan(lhs) || std::isnan(rhs)) { + return false; + } + + auto lc = convert(lhs); + auto rc = convert(rhs); + + if ((lc.i < 0) != (rc.i < 0)) { + // Potentially we can have +0 and -0 + return lhs == rhs; + } + + auto ulpDiff = std::abs(lc.i - rc.i); + return ulpDiff <= maxUlpDiff; +} + +} + +namespace Catch { +namespace Matchers { +namespace Floating { + WithinAbsMatcher::WithinAbsMatcher(double target, double margin) + :m_target{ target }, m_margin{ margin } { + if (m_margin < 0) { + throw std::domain_error("Allowed margin difference has to be >= 0"); + } + } + + // Performs equivalent check of std::fabs(lhs - rhs) <= margin + // But without the subtraction to allow for INFINITY in comparison + bool WithinAbsMatcher::match(double const& matchee) const { + return (matchee + m_margin >= m_target) && (m_target + m_margin >= matchee); + } + + std::string WithinAbsMatcher::describe() const { + return "is within " + ::Catch::Detail::stringify(m_margin) + " of " + ::Catch::Detail::stringify(m_target); + } + + WithinUlpsMatcher::WithinUlpsMatcher(double target, int ulps, FloatingPointKind baseType) + :m_target{ target }, m_ulps{ ulps }, m_type{ baseType } { + if (m_ulps < 0) { + throw std::domain_error("Allowed ulp difference has to be >= 0"); + } + } + + bool WithinUlpsMatcher::match(double const& matchee) const { + switch (m_type) { + case FloatingPointKind::Float: + return almostEqualUlps(static_cast(matchee), static_cast(m_target), m_ulps); + case FloatingPointKind::Double: + return almostEqualUlps(matchee, m_target, m_ulps); + default: + throw std::domain_error("Unknown FloatingPointKind value"); + } + } + + std::string WithinUlpsMatcher::describe() const { + return "is within " + Catch::to_string(m_ulps) + " ULPs of " + ::Catch::Detail::stringify(m_target) + ((m_type == FloatingPointKind::Float)? "f" : ""); + } + +}// namespace Floating + +Floating::WithinUlpsMatcher WithinULP(double target, int maxUlpDiff) { + return Floating::WithinUlpsMatcher(target, maxUlpDiff, Floating::FloatingPointKind::Double); +} + +Floating::WithinUlpsMatcher WithinULP(float target, int maxUlpDiff) { + return Floating::WithinUlpsMatcher(target, maxUlpDiff, Floating::FloatingPointKind::Float); +} + +Floating::WithinAbsMatcher WithinAbs(double target, double margin) { + return Floating::WithinAbsMatcher(target, margin); +} + +} // namespace Matchers +} // namespace Catch + +// end catch_matchers_floating.cpp +// start catch_matchers_generic.cpp + +std::string Catch::Matchers::Generic::Detail::finalizeDescription(const std::string& desc) { + if (desc.empty()) { + return "matches undescribed predicate"; + } else { + return "matches predicate: \"" + desc + '"'; + } +} +// end catch_matchers_generic.cpp +// start catch_matchers_string.cpp + +#include + +namespace Catch { +namespace Matchers { + + namespace StdString { + + CasedString::CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity ) + : m_caseSensitivity( caseSensitivity ), + m_str( adjustString( str ) ) + {} + std::string CasedString::adjustString( std::string const& str ) const { + return m_caseSensitivity == CaseSensitive::No + ? toLower( str ) + : str; + } + std::string CasedString::caseSensitivitySuffix() const { + return m_caseSensitivity == CaseSensitive::No + ? " (case insensitive)" + : std::string(); + } + + StringMatcherBase::StringMatcherBase( std::string const& operation, CasedString const& comparator ) + : m_comparator( comparator ), + m_operation( operation ) { + } + + std::string StringMatcherBase::describe() const { + std::string description; + description.reserve(5 + m_operation.size() + m_comparator.m_str.size() + + m_comparator.caseSensitivitySuffix().size()); + description += m_operation; + description += ": \""; + description += m_comparator.m_str; + description += "\""; + description += m_comparator.caseSensitivitySuffix(); + return description; + } + + EqualsMatcher::EqualsMatcher( CasedString const& comparator ) : StringMatcherBase( "equals", comparator ) {} + + bool EqualsMatcher::match( std::string const& source ) const { + return m_comparator.adjustString( source ) == m_comparator.m_str; + } + + ContainsMatcher::ContainsMatcher( CasedString const& comparator ) : StringMatcherBase( "contains", comparator ) {} + + bool ContainsMatcher::match( std::string const& source ) const { + return contains( m_comparator.adjustString( source ), m_comparator.m_str ); + } + + StartsWithMatcher::StartsWithMatcher( CasedString const& comparator ) : StringMatcherBase( "starts with", comparator ) {} + + bool StartsWithMatcher::match( std::string const& source ) const { + return startsWith( m_comparator.adjustString( source ), m_comparator.m_str ); + } + + EndsWithMatcher::EndsWithMatcher( CasedString const& comparator ) : StringMatcherBase( "ends with", comparator ) {} + + bool EndsWithMatcher::match( std::string const& source ) const { + return endsWith( m_comparator.adjustString( source ), m_comparator.m_str ); + } + + RegexMatcher::RegexMatcher(std::string regex, CaseSensitive::Choice caseSensitivity): m_regex(std::move(regex)), m_caseSensitivity(caseSensitivity) {} + + bool RegexMatcher::match(std::string const& matchee) const { + auto flags = std::regex::ECMAScript; // ECMAScript is the default syntax option anyway + if (m_caseSensitivity == CaseSensitive::Choice::No) { + flags |= std::regex::icase; + } + auto reg = std::regex(m_regex, flags); + return std::regex_match(matchee, reg); + } + + std::string RegexMatcher::describe() const { + return "matches " + ::Catch::Detail::stringify(m_regex) + ((m_caseSensitivity == CaseSensitive::Choice::Yes)? " case sensitively" : " case insensitively"); + } + + } // namespace StdString + + StdString::EqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity ) { + return StdString::EqualsMatcher( StdString::CasedString( str, caseSensitivity) ); + } + StdString::ContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity ) { + return StdString::ContainsMatcher( StdString::CasedString( str, caseSensitivity) ); + } + StdString::EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) { + return StdString::EndsWithMatcher( StdString::CasedString( str, caseSensitivity) ); + } + StdString::StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) { + return StdString::StartsWithMatcher( StdString::CasedString( str, caseSensitivity) ); + } + + StdString::RegexMatcher Matches(std::string const& regex, CaseSensitive::Choice caseSensitivity) { + return StdString::RegexMatcher(regex, caseSensitivity); + } + +} // namespace Matchers +} // namespace Catch +// end catch_matchers_string.cpp +// start catch_message.cpp + +// start catch_uncaught_exceptions.h + +namespace Catch { + bool uncaught_exceptions(); +} // end namespace Catch + +// end catch_uncaught_exceptions.h +namespace Catch { + + MessageInfo::MessageInfo( std::string const& _macroName, + SourceLineInfo const& _lineInfo, + ResultWas::OfType _type ) + : macroName( _macroName ), + lineInfo( _lineInfo ), + type( _type ), + sequence( ++globalCount ) + {} + + bool MessageInfo::operator==( MessageInfo const& other ) const { + return sequence == other.sequence; + } + + bool MessageInfo::operator<( MessageInfo const& other ) const { + return sequence < other.sequence; + } + + // This may need protecting if threading support is added + unsigned int MessageInfo::globalCount = 0; + + //////////////////////////////////////////////////////////////////////////// + + Catch::MessageBuilder::MessageBuilder( std::string const& macroName, + SourceLineInfo const& lineInfo, + ResultWas::OfType type ) + :m_info(macroName, lineInfo, type) {} + + //////////////////////////////////////////////////////////////////////////// + + ScopedMessage::ScopedMessage( MessageBuilder const& builder ) + : m_info( builder.m_info ) + { + m_info.message = builder.m_stream.str(); + getResultCapture().pushScopedMessage( m_info ); + } + + ScopedMessage::~ScopedMessage() { + if ( !uncaught_exceptions() ){ + getResultCapture().popScopedMessage(m_info); + } + } +} // end namespace Catch +// end catch_message.cpp +// start catch_output_redirect.cpp + +// start catch_output_redirect.h +#ifndef TWOBLUECUBES_CATCH_OUTPUT_REDIRECT_H +#define TWOBLUECUBES_CATCH_OUTPUT_REDIRECT_H + +#include +#include +#include + +namespace Catch { + + class RedirectedStream { + std::ostream& m_originalStream; + std::ostream& m_redirectionStream; + std::streambuf* m_prevBuf; + + public: + RedirectedStream( std::ostream& originalStream, std::ostream& redirectionStream ); + ~RedirectedStream(); + }; + + class RedirectedStdOut { + ReusableStringStream m_rss; + RedirectedStream m_cout; + public: + RedirectedStdOut(); + auto str() const -> std::string; + }; + + // StdErr has two constituent streams in C++, std::cerr and std::clog + // This means that we need to redirect 2 streams into 1 to keep proper + // order of writes + class RedirectedStdErr { + ReusableStringStream m_rss; + RedirectedStream m_cerr; + RedirectedStream m_clog; + public: + RedirectedStdErr(); + auto str() const -> std::string; + }; + + // Windows's implementation of std::tmpfile is terrible (it tries + // to create a file inside system folder, thus requiring elevated + // privileges for the binary), so we have to use tmpnam(_s) and + // create the file ourselves there. + class TempFile { + public: + TempFile(TempFile const&) = delete; + TempFile& operator=(TempFile const&) = delete; + TempFile(TempFile&&) = delete; + TempFile& operator=(TempFile&&) = delete; + + TempFile(); + ~TempFile(); + + std::FILE* getFile(); + std::string getContents(); + + private: + std::FILE* m_file = nullptr; + #if defined(_MSC_VER) + char m_buffer[L_tmpnam] = { 0 }; + #endif + }; + + class OutputRedirect { + public: + OutputRedirect(OutputRedirect const&) = delete; + OutputRedirect& operator=(OutputRedirect const&) = delete; + OutputRedirect(OutputRedirect&&) = delete; + OutputRedirect& operator=(OutputRedirect&&) = delete; + + OutputRedirect(std::string& stdout_dest, std::string& stderr_dest); + ~OutputRedirect(); + + private: + int m_originalStdout = -1; + int m_originalStderr = -1; + TempFile m_stdoutFile; + TempFile m_stderrFile; + std::string& m_stdoutDest; + std::string& m_stderrDest; + }; + +} // end namespace Catch + +#endif // TWOBLUECUBES_CATCH_OUTPUT_REDIRECT_H +// end catch_output_redirect.h +#include +#include +#include +#include +#include + +#if defined(_MSC_VER) +#include //_dup and _dup2 +#define dup _dup +#define dup2 _dup2 +#define fileno _fileno +#else +#include // dup and dup2 +#endif + +namespace Catch { + + RedirectedStream::RedirectedStream( std::ostream& originalStream, std::ostream& redirectionStream ) + : m_originalStream( originalStream ), + m_redirectionStream( redirectionStream ), + m_prevBuf( m_originalStream.rdbuf() ) + { + m_originalStream.rdbuf( m_redirectionStream.rdbuf() ); + } + + RedirectedStream::~RedirectedStream() { + m_originalStream.rdbuf( m_prevBuf ); + } + + RedirectedStdOut::RedirectedStdOut() : m_cout( Catch::cout(), m_rss.get() ) {} + auto RedirectedStdOut::str() const -> std::string { return m_rss.str(); } + + RedirectedStdErr::RedirectedStdErr() + : m_cerr( Catch::cerr(), m_rss.get() ), + m_clog( Catch::clog(), m_rss.get() ) + {} + auto RedirectedStdErr::str() const -> std::string { return m_rss.str(); } + +#if defined(_MSC_VER) + TempFile::TempFile() { + if (tmpnam_s(m_buffer)) { + throw std::runtime_error("Could not get a temp filename"); + } + if (fopen_s(&m_file, m_buffer, "w")) { + char buffer[100]; + if (strerror_s(buffer, errno)) { + throw std::runtime_error("Could not translate errno to string"); + } + throw std::runtime_error("Could not open the temp file: " + std::string(m_buffer) + buffer); + } + } +#else + TempFile::TempFile() { + m_file = std::tmpfile(); + if (!m_file) { + throw std::runtime_error("Could not create a temp file."); + } + } + +#endif + + TempFile::~TempFile() { + // TBD: What to do about errors here? + std::fclose(m_file); + // We manually create the file on Windows only, on Linux + // it will be autodeleted +#if defined(_MSC_VER) + std::remove(m_buffer); +#endif + } + + FILE* TempFile::getFile() { + return m_file; + } + + std::string TempFile::getContents() { + std::stringstream sstr; + char buffer[100] = {}; + std::rewind(m_file); + while (std::fgets(buffer, sizeof(buffer), m_file)) { + sstr << buffer; + } + return sstr.str(); + } + + OutputRedirect::OutputRedirect(std::string& stdout_dest, std::string& stderr_dest) : + m_originalStdout(dup(1)), + m_originalStderr(dup(2)), + m_stdoutDest(stdout_dest), + m_stderrDest(stderr_dest) { + dup2(fileno(m_stdoutFile.getFile()), 1); + dup2(fileno(m_stderrFile.getFile()), 2); + } + + OutputRedirect::~OutputRedirect() { + Catch::cout() << std::flush; + fflush(stdout); + // Since we support overriding these streams, we flush cerr + // even though std::cerr is unbuffered + Catch::cerr() << std::flush; + Catch::clog() << std::flush; + fflush(stderr); + + dup2(m_originalStdout, 1); + dup2(m_originalStderr, 2); + + m_stdoutDest += m_stdoutFile.getContents(); + m_stderrDest += m_stderrFile.getContents(); + } + +} // namespace Catch + +#if defined(_MSC_VER) +#undef dup +#undef dup2 +#undef fileno +#endif +// end catch_output_redirect.cpp +// start catch_random_number_generator.cpp + +// start catch_random_number_generator.h + +#include + +namespace Catch { + + struct IConfig; + + void seedRng( IConfig const& config ); + + unsigned int rngSeed(); + + struct RandomNumberGenerator { + using result_type = unsigned int; + + static constexpr result_type (min)() { return 0; } + static constexpr result_type (max)() { return 1000000; } + + result_type operator()( result_type n ) const; + result_type operator()() const; + + template + static void shuffle( V& vector ) { + RandomNumberGenerator rng; + std::shuffle( vector.begin(), vector.end(), rng ); + } + }; + +} + +// end catch_random_number_generator.h +#include + +namespace Catch { + + void seedRng( IConfig const& config ) { + if( config.rngSeed() != 0 ) + std::srand( config.rngSeed() ); + } + unsigned int rngSeed() { + return getCurrentContext().getConfig()->rngSeed(); + } + + RandomNumberGenerator::result_type RandomNumberGenerator::operator()( result_type n ) const { + return std::rand() % n; + } + RandomNumberGenerator::result_type RandomNumberGenerator::operator()() const { + return std::rand() % (max)(); + } + +} +// end catch_random_number_generator.cpp +// start catch_registry_hub.cpp + +// start catch_test_case_registry_impl.h + +#include +#include +#include +#include + +namespace Catch { + + class TestCase; + struct IConfig; + + std::vector sortTests( IConfig const& config, std::vector const& unsortedTestCases ); + bool matchTest( TestCase const& testCase, TestSpec const& testSpec, IConfig const& config ); + + void enforceNoDuplicateTestCases( std::vector const& functions ); + + std::vector filterTests( std::vector const& testCases, TestSpec const& testSpec, IConfig const& config ); + std::vector const& getAllTestCasesSorted( IConfig const& config ); + + class TestRegistry : public ITestCaseRegistry { + public: + virtual ~TestRegistry() = default; + + virtual void registerTest( TestCase const& testCase ); + + std::vector const& getAllTests() const override; + std::vector const& getAllTestsSorted( IConfig const& config ) const override; + + private: + std::vector m_functions; + mutable RunTests::InWhatOrder m_currentSortOrder = RunTests::InDeclarationOrder; + mutable std::vector m_sortedFunctions; + std::size_t m_unnamedCount = 0; + std::ios_base::Init m_ostreamInit; // Forces cout/ cerr to be initialised + }; + + /////////////////////////////////////////////////////////////////////////// + + class TestInvokerAsFunction : public ITestInvoker { + void(*m_testAsFunction)(); + public: + TestInvokerAsFunction( void(*testAsFunction)() ) noexcept; + + void invoke() const override; + }; + + std::string extractClassName( StringRef const& classOrQualifiedMethodName ); + + /////////////////////////////////////////////////////////////////////////// + +} // end namespace Catch + +// end catch_test_case_registry_impl.h +// start catch_reporter_registry.h + +#include + +namespace Catch { + + class ReporterRegistry : public IReporterRegistry { + + public: + + ~ReporterRegistry() override; + + IStreamingReporterPtr create( std::string const& name, IConfigPtr const& config ) const override; + + void registerReporter( std::string const& name, IReporterFactoryPtr const& factory ); + void registerListener( IReporterFactoryPtr const& factory ); + + FactoryMap const& getFactories() const override; + Listeners const& getListeners() const override; + + private: + FactoryMap m_factories; + Listeners m_listeners; + }; +} + +// end catch_reporter_registry.h +// start catch_tag_alias_registry.h + +// start catch_tag_alias.h + +#include + +namespace Catch { + + struct TagAlias { + TagAlias(std::string const& _tag, SourceLineInfo _lineInfo); + + std::string tag; + SourceLineInfo lineInfo; + }; + +} // end namespace Catch + +// end catch_tag_alias.h +#include + +namespace Catch { + + class TagAliasRegistry : public ITagAliasRegistry { + public: + ~TagAliasRegistry() override; + TagAlias const* find( std::string const& alias ) const override; + std::string expandAliases( std::string const& unexpandedTestSpec ) const override; + void add( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ); + + private: + std::map m_registry; + }; + +} // end namespace Catch + +// end catch_tag_alias_registry.h +// start catch_startup_exception_registry.h + +#include +#include + +namespace Catch { + + class StartupExceptionRegistry { + public: + void add(std::exception_ptr const& exception) noexcept; + std::vector const& getExceptions() const noexcept; + private: + std::vector m_exceptions; + }; + +} // end namespace Catch + +// end catch_startup_exception_registry.h +namespace Catch { + + namespace { + + class RegistryHub : public IRegistryHub, public IMutableRegistryHub, + private NonCopyable { + + public: // IRegistryHub + RegistryHub() = default; + IReporterRegistry const& getReporterRegistry() const override { + return m_reporterRegistry; + } + ITestCaseRegistry const& getTestCaseRegistry() const override { + return m_testCaseRegistry; + } + IExceptionTranslatorRegistry& getExceptionTranslatorRegistry() override { + return m_exceptionTranslatorRegistry; + } + ITagAliasRegistry const& getTagAliasRegistry() const override { + return m_tagAliasRegistry; + } + StartupExceptionRegistry const& getStartupExceptionRegistry() const override { + return m_exceptionRegistry; + } + + public: // IMutableRegistryHub + void registerReporter( std::string const& name, IReporterFactoryPtr const& factory ) override { + m_reporterRegistry.registerReporter( name, factory ); + } + void registerListener( IReporterFactoryPtr const& factory ) override { + m_reporterRegistry.registerListener( factory ); + } + void registerTest( TestCase const& testInfo ) override { + m_testCaseRegistry.registerTest( testInfo ); + } + void registerTranslator( const IExceptionTranslator* translator ) override { + m_exceptionTranslatorRegistry.registerTranslator( translator ); + } + void registerTagAlias( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) override { + m_tagAliasRegistry.add( alias, tag, lineInfo ); + } + void registerStartupException() noexcept override { + m_exceptionRegistry.add(std::current_exception()); + } + + private: + TestRegistry m_testCaseRegistry; + ReporterRegistry m_reporterRegistry; + ExceptionTranslatorRegistry m_exceptionTranslatorRegistry; + TagAliasRegistry m_tagAliasRegistry; + StartupExceptionRegistry m_exceptionRegistry; + }; + + // Single, global, instance + RegistryHub*& getTheRegistryHub() { + static RegistryHub* theRegistryHub = nullptr; + if( !theRegistryHub ) + theRegistryHub = new RegistryHub(); + return theRegistryHub; + } + } + + IRegistryHub& getRegistryHub() { + return *getTheRegistryHub(); + } + IMutableRegistryHub& getMutableRegistryHub() { + return *getTheRegistryHub(); + } + void cleanUp() { + delete getTheRegistryHub(); + getTheRegistryHub() = nullptr; + cleanUpContext(); + ReusableStringStream::cleanup(); + } + std::string translateActiveException() { + return getRegistryHub().getExceptionTranslatorRegistry().translateActiveException(); + } + +} // end namespace Catch +// end catch_registry_hub.cpp +// start catch_reporter_registry.cpp + +namespace Catch { + + ReporterRegistry::~ReporterRegistry() = default; + + IStreamingReporterPtr ReporterRegistry::create( std::string const& name, IConfigPtr const& config ) const { + auto it = m_factories.find( name ); + if( it == m_factories.end() ) + return nullptr; + return it->second->create( ReporterConfig( config ) ); + } + + void ReporterRegistry::registerReporter( std::string const& name, IReporterFactoryPtr const& factory ) { + m_factories.emplace(name, factory); + } + void ReporterRegistry::registerListener( IReporterFactoryPtr const& factory ) { + m_listeners.push_back( factory ); + } + + IReporterRegistry::FactoryMap const& ReporterRegistry::getFactories() const { + return m_factories; + } + IReporterRegistry::Listeners const& ReporterRegistry::getListeners() const { + return m_listeners; + } + +} +// end catch_reporter_registry.cpp +// start catch_result_type.cpp + +namespace Catch { + + bool isOk( ResultWas::OfType resultType ) { + return ( resultType & ResultWas::FailureBit ) == 0; + } + bool isJustInfo( int flags ) { + return flags == ResultWas::Info; + } + + ResultDisposition::Flags operator | ( ResultDisposition::Flags lhs, ResultDisposition::Flags rhs ) { + return static_cast( static_cast( lhs ) | static_cast( rhs ) ); + } + + bool shouldContinueOnFailure( int flags ) { return ( flags & ResultDisposition::ContinueOnFailure ) != 0; } + bool shouldSuppressFailure( int flags ) { return ( flags & ResultDisposition::SuppressFail ) != 0; } + +} // end namespace Catch +// end catch_result_type.cpp +// start catch_run_context.cpp + +#include +#include +#include + +namespace Catch { + + RunContext::RunContext(IConfigPtr const& _config, IStreamingReporterPtr&& reporter) + : m_runInfo(_config->name()), + m_context(getCurrentMutableContext()), + m_config(_config), + m_reporter(std::move(reporter)), + m_lastAssertionInfo{ StringRef(), SourceLineInfo("",0), StringRef(), ResultDisposition::Normal }, + m_includeSuccessfulResults( m_config->includeSuccessfulResults() ) + { + m_context.setRunner(this); + m_context.setConfig(m_config); + m_context.setResultCapture(this); + m_reporter->testRunStarting(m_runInfo); + } + + RunContext::~RunContext() { + m_reporter->testRunEnded(TestRunStats(m_runInfo, m_totals, aborting())); + } + + void RunContext::testGroupStarting(std::string const& testSpec, std::size_t groupIndex, std::size_t groupsCount) { + m_reporter->testGroupStarting(GroupInfo(testSpec, groupIndex, groupsCount)); + } + + void RunContext::testGroupEnded(std::string const& testSpec, Totals const& totals, std::size_t groupIndex, std::size_t groupsCount) { + m_reporter->testGroupEnded(TestGroupStats(GroupInfo(testSpec, groupIndex, groupsCount), totals, aborting())); + } + + Totals RunContext::runTest(TestCase const& testCase) { + Totals prevTotals = m_totals; + + std::string redirectedCout; + std::string redirectedCerr; + + auto const& testInfo = testCase.getTestCaseInfo(); + + m_reporter->testCaseStarting(testInfo); + + m_activeTestCase = &testCase; + + ITracker& rootTracker = m_trackerContext.startRun(); + assert(rootTracker.isSectionTracker()); + static_cast(rootTracker).addInitialFilters(m_config->getSectionsToRun()); + do { + m_trackerContext.startCycle(); + m_testCaseTracker = &SectionTracker::acquire(m_trackerContext, TestCaseTracking::NameAndLocation(testInfo.name, testInfo.lineInfo)); + runCurrentTest(redirectedCout, redirectedCerr); + } while (!m_testCaseTracker->isSuccessfullyCompleted() && !aborting()); + + Totals deltaTotals = m_totals.delta(prevTotals); + if (testInfo.expectedToFail() && deltaTotals.testCases.passed > 0) { + deltaTotals.assertions.failed++; + deltaTotals.testCases.passed--; + deltaTotals.testCases.failed++; + } + m_totals.testCases += deltaTotals.testCases; + m_reporter->testCaseEnded(TestCaseStats(testInfo, + deltaTotals, + redirectedCout, + redirectedCerr, + aborting())); + + m_activeTestCase = nullptr; + m_testCaseTracker = nullptr; + + return deltaTotals; + } + + IConfigPtr RunContext::config() const { + return m_config; + } + + IStreamingReporter& RunContext::reporter() const { + return *m_reporter; + } + + void RunContext::assertionEnded(AssertionResult const & result) { + if (result.getResultType() == ResultWas::Ok) { + m_totals.assertions.passed++; + m_lastAssertionPassed = true; + } else if (!result.isOk()) { + m_lastAssertionPassed = false; + if( m_activeTestCase->getTestCaseInfo().okToFail() ) + m_totals.assertions.failedButOk++; + else + m_totals.assertions.failed++; + } + else { + m_lastAssertionPassed = true; + } + + // We have no use for the return value (whether messages should be cleared), because messages were made scoped + // and should be let to clear themselves out. + static_cast(m_reporter->assertionEnded(AssertionStats(result, m_messages, m_totals))); + + // Reset working state + resetAssertionInfo(); + m_lastResult = result; + } + void RunContext::resetAssertionInfo() { + m_lastAssertionInfo.macroName = StringRef(); + m_lastAssertionInfo.capturedExpression = "{Unknown expression after the reported line}"_sr; + } + + bool RunContext::sectionStarted(SectionInfo const & sectionInfo, Counts & assertions) { + ITracker& sectionTracker = SectionTracker::acquire(m_trackerContext, TestCaseTracking::NameAndLocation(sectionInfo.name, sectionInfo.lineInfo)); + if (!sectionTracker.isOpen()) + return false; + m_activeSections.push_back(§ionTracker); + + m_lastAssertionInfo.lineInfo = sectionInfo.lineInfo; + + m_reporter->sectionStarting(sectionInfo); + + assertions = m_totals.assertions; + + return true; + } + + bool RunContext::testForMissingAssertions(Counts& assertions) { + if (assertions.total() != 0) + return false; + if (!m_config->warnAboutMissingAssertions()) + return false; + if (m_trackerContext.currentTracker().hasChildren()) + return false; + m_totals.assertions.failed++; + assertions.failed++; + return true; + } + + void RunContext::sectionEnded(SectionEndInfo const & endInfo) { + Counts assertions = m_totals.assertions - endInfo.prevAssertions; + bool missingAssertions = testForMissingAssertions(assertions); + + if (!m_activeSections.empty()) { + m_activeSections.back()->close(); + m_activeSections.pop_back(); + } + + m_reporter->sectionEnded(SectionStats(endInfo.sectionInfo, assertions, endInfo.durationInSeconds, missingAssertions)); + m_messages.clear(); + } + + void RunContext::sectionEndedEarly(SectionEndInfo const & endInfo) { + if (m_unfinishedSections.empty()) + m_activeSections.back()->fail(); + else + m_activeSections.back()->close(); + m_activeSections.pop_back(); + + m_unfinishedSections.push_back(endInfo); + } + void RunContext::benchmarkStarting( BenchmarkInfo const& info ) { + m_reporter->benchmarkStarting( info ); + } + void RunContext::benchmarkEnded( BenchmarkStats const& stats ) { + m_reporter->benchmarkEnded( stats ); + } + + void RunContext::pushScopedMessage(MessageInfo const & message) { + m_messages.push_back(message); + } + + void RunContext::popScopedMessage(MessageInfo const & message) { + m_messages.erase(std::remove(m_messages.begin(), m_messages.end(), message), m_messages.end()); + } + + std::string RunContext::getCurrentTestName() const { + return m_activeTestCase + ? m_activeTestCase->getTestCaseInfo().name + : std::string(); + } + + const AssertionResult * RunContext::getLastResult() const { + return &(*m_lastResult); + } + + void RunContext::exceptionEarlyReported() { + m_shouldReportUnexpected = false; + } + + void RunContext::handleFatalErrorCondition( StringRef message ) { + // First notify reporter that bad things happened + m_reporter->fatalErrorEncountered(message); + + // Don't rebuild the result -- the stringification itself can cause more fatal errors + // Instead, fake a result data. + AssertionResultData tempResult( ResultWas::FatalErrorCondition, { false } ); + tempResult.message = message; + AssertionResult result(m_lastAssertionInfo, tempResult); + + assertionEnded(result); + + handleUnfinishedSections(); + + // Recreate section for test case (as we will lose the one that was in scope) + auto const& testCaseInfo = m_activeTestCase->getTestCaseInfo(); + SectionInfo testCaseSection(testCaseInfo.lineInfo, testCaseInfo.name, testCaseInfo.description); + + Counts assertions; + assertions.failed = 1; + SectionStats testCaseSectionStats(testCaseSection, assertions, 0, false); + m_reporter->sectionEnded(testCaseSectionStats); + + auto const& testInfo = m_activeTestCase->getTestCaseInfo(); + + Totals deltaTotals; + deltaTotals.testCases.failed = 1; + deltaTotals.assertions.failed = 1; + m_reporter->testCaseEnded(TestCaseStats(testInfo, + deltaTotals, + std::string(), + std::string(), + false)); + m_totals.testCases.failed++; + testGroupEnded(std::string(), m_totals, 1, 1); + m_reporter->testRunEnded(TestRunStats(m_runInfo, m_totals, false)); + } + + bool RunContext::lastAssertionPassed() { + return m_lastAssertionPassed; + } + + void RunContext::assertionPassed() { + m_lastAssertionPassed = true; + ++m_totals.assertions.passed; + resetAssertionInfo(); + } + + bool RunContext::aborting() const { + return m_totals.assertions.failed == static_cast(m_config->abortAfter()); + } + + void RunContext::runCurrentTest(std::string & redirectedCout, std::string & redirectedCerr) { + auto const& testCaseInfo = m_activeTestCase->getTestCaseInfo(); + SectionInfo testCaseSection(testCaseInfo.lineInfo, testCaseInfo.name, testCaseInfo.description); + m_reporter->sectionStarting(testCaseSection); + Counts prevAssertions = m_totals.assertions; + double duration = 0; + m_shouldReportUnexpected = true; + m_lastAssertionInfo = { "TEST_CASE"_sr, testCaseInfo.lineInfo, StringRef(), ResultDisposition::Normal }; + + seedRng(*m_config); + + Timer timer; + try { + if (m_reporter->getPreferences().shouldRedirectStdOut) { +#if !defined(CATCH_CONFIG_EXPERIMENTAL_REDIRECT) + RedirectedStdOut redirectedStdOut; + RedirectedStdErr redirectedStdErr; + + timer.start(); + invokeActiveTestCase(); + redirectedCout += redirectedStdOut.str(); + redirectedCerr += redirectedStdErr.str(); +#else + OutputRedirect r(redirectedCout, redirectedCerr); + timer.start(); + invokeActiveTestCase(); +#endif + } else { + timer.start(); + invokeActiveTestCase(); + } + duration = timer.getElapsedSeconds(); + } catch (TestFailureException&) { + // This just means the test was aborted due to failure + } catch (...) { + // Under CATCH_CONFIG_FAST_COMPILE, unexpected exceptions under REQUIRE assertions + // are reported without translation at the point of origin. + if( m_shouldReportUnexpected ) { + AssertionReaction dummyReaction; + handleUnexpectedInflightException( m_lastAssertionInfo, translateActiveException(), dummyReaction ); + } + } + Counts assertions = m_totals.assertions - prevAssertions; + bool missingAssertions = testForMissingAssertions(assertions); + + m_testCaseTracker->close(); + handleUnfinishedSections(); + m_messages.clear(); + + SectionStats testCaseSectionStats(testCaseSection, assertions, duration, missingAssertions); + m_reporter->sectionEnded(testCaseSectionStats); + } + + void RunContext::invokeActiveTestCase() { + FatalConditionHandler fatalConditionHandler; // Handle signals + m_activeTestCase->invoke(); + fatalConditionHandler.reset(); + } + + void RunContext::handleUnfinishedSections() { + // If sections ended prematurely due to an exception we stored their + // infos here so we can tear them down outside the unwind process. + for (auto it = m_unfinishedSections.rbegin(), + itEnd = m_unfinishedSections.rend(); + it != itEnd; + ++it) + sectionEnded(*it); + m_unfinishedSections.clear(); + } + + void RunContext::handleExpr( + AssertionInfo const& info, + ITransientExpression const& expr, + AssertionReaction& reaction + ) { + m_reporter->assertionStarting( info ); + + bool negated = isFalseTest( info.resultDisposition ); + bool result = expr.getResult() != negated; + + if( result ) { + if (!m_includeSuccessfulResults) { + assertionPassed(); + } + else { + reportExpr(info, ResultWas::Ok, &expr, negated); + } + } + else { + reportExpr(info, ResultWas::ExpressionFailed, &expr, negated ); + populateReaction( reaction ); + } + } + void RunContext::reportExpr( + AssertionInfo const &info, + ResultWas::OfType resultType, + ITransientExpression const *expr, + bool negated ) { + + m_lastAssertionInfo = info; + AssertionResultData data( resultType, LazyExpression( negated ) ); + + AssertionResult assertionResult{ info, data }; + assertionResult.m_resultData.lazyExpression.m_transientExpression = expr; + + assertionEnded( assertionResult ); + } + + void RunContext::handleMessage( + AssertionInfo const& info, + ResultWas::OfType resultType, + StringRef const& message, + AssertionReaction& reaction + ) { + m_reporter->assertionStarting( info ); + + m_lastAssertionInfo = info; + + AssertionResultData data( resultType, LazyExpression( false ) ); + data.message = message; + AssertionResult assertionResult{ m_lastAssertionInfo, data }; + assertionEnded( assertionResult ); + if( !assertionResult.isOk() ) + populateReaction( reaction ); + } + void RunContext::handleUnexpectedExceptionNotThrown( + AssertionInfo const& info, + AssertionReaction& reaction + ) { + handleNonExpr(info, Catch::ResultWas::DidntThrowException, reaction); + } + + void RunContext::handleUnexpectedInflightException( + AssertionInfo const& info, + std::string const& message, + AssertionReaction& reaction + ) { + m_lastAssertionInfo = info; + + AssertionResultData data( ResultWas::ThrewException, LazyExpression( false ) ); + data.message = message; + AssertionResult assertionResult{ info, data }; + assertionEnded( assertionResult ); + populateReaction( reaction ); + } + + void RunContext::populateReaction( AssertionReaction& reaction ) { + reaction.shouldDebugBreak = m_config->shouldDebugBreak(); + reaction.shouldThrow = aborting() || (m_lastAssertionInfo.resultDisposition & ResultDisposition::Normal); + } + + void RunContext::handleIncomplete( + AssertionInfo const& info + ) { + m_lastAssertionInfo = info; + + AssertionResultData data( ResultWas::ThrewException, LazyExpression( false ) ); + data.message = "Exception translation was disabled by CATCH_CONFIG_FAST_COMPILE"; + AssertionResult assertionResult{ info, data }; + assertionEnded( assertionResult ); + } + void RunContext::handleNonExpr( + AssertionInfo const &info, + ResultWas::OfType resultType, + AssertionReaction &reaction + ) { + m_lastAssertionInfo = info; + + AssertionResultData data( resultType, LazyExpression( false ) ); + AssertionResult assertionResult{ info, data }; + assertionEnded( assertionResult ); + + if( !assertionResult.isOk() ) + populateReaction( reaction ); + } + + IResultCapture& getResultCapture() { + if (auto* capture = getCurrentContext().getResultCapture()) + return *capture; + else + CATCH_INTERNAL_ERROR("No result capture instance"); + } +} +// end catch_run_context.cpp +// start catch_section.cpp + +namespace Catch { + + Section::Section( SectionInfo const& info ) + : m_info( info ), + m_sectionIncluded( getResultCapture().sectionStarted( m_info, m_assertions ) ) + { + m_timer.start(); + } + + Section::~Section() { + if( m_sectionIncluded ) { + SectionEndInfo endInfo( m_info, m_assertions, m_timer.getElapsedSeconds() ); + if( uncaught_exceptions() ) + getResultCapture().sectionEndedEarly( endInfo ); + else + getResultCapture().sectionEnded( endInfo ); + } + } + + // This indicates whether the section should be executed or not + Section::operator bool() const { + return m_sectionIncluded; + } + +} // end namespace Catch +// end catch_section.cpp +// start catch_section_info.cpp + +namespace Catch { + + SectionInfo::SectionInfo + ( SourceLineInfo const& _lineInfo, + std::string const& _name, + std::string const& _description ) + : name( _name ), + description( _description ), + lineInfo( _lineInfo ) + {} + + SectionEndInfo::SectionEndInfo( SectionInfo const& _sectionInfo, Counts const& _prevAssertions, double _durationInSeconds ) + : sectionInfo( _sectionInfo ), prevAssertions( _prevAssertions ), durationInSeconds( _durationInSeconds ) + {} + +} // end namespace Catch +// end catch_section_info.cpp +// start catch_session.cpp + +// start catch_session.h + +#include + +namespace Catch { + + class Session : NonCopyable { + public: + + Session(); + ~Session() override; + + void showHelp() const; + void libIdentify(); + + int applyCommandLine( int argc, char const * const * argv ); + + void useConfigData( ConfigData const& configData ); + + int run( int argc, char* argv[] ); + #if defined(CATCH_CONFIG_WCHAR) && defined(WIN32) && defined(UNICODE) + int run( int argc, wchar_t* const argv[] ); + #endif + int run(); + + clara::Parser const& cli() const; + void cli( clara::Parser const& newParser ); + ConfigData& configData(); + Config& config(); + private: + int runInternal(); + + clara::Parser m_cli; + ConfigData m_configData; + std::shared_ptr m_config; + bool m_startupExceptions = false; + }; + +} // end namespace Catch + +// end catch_session.h +// start catch_version.h + +#include + +namespace Catch { + + // Versioning information + struct Version { + Version( Version const& ) = delete; + Version& operator=( Version const& ) = delete; + Version( unsigned int _majorVersion, + unsigned int _minorVersion, + unsigned int _patchNumber, + char const * const _branchName, + unsigned int _buildNumber ); + + unsigned int const majorVersion; + unsigned int const minorVersion; + unsigned int const patchNumber; + + // buildNumber is only used if branchName is not null + char const * const branchName; + unsigned int const buildNumber; + + friend std::ostream& operator << ( std::ostream& os, Version const& version ); + }; + + Version const& libraryVersion(); +} + +// end catch_version.h +#include +#include + +namespace Catch { + + namespace { + const int MaxExitCode = 255; + + IStreamingReporterPtr createReporter(std::string const& reporterName, IConfigPtr const& config) { + auto reporter = Catch::getRegistryHub().getReporterRegistry().create(reporterName, config); + CATCH_ENFORCE(reporter, "No reporter registered with name: '" << reporterName << "'"); + + return reporter; + } + + IStreamingReporterPtr makeReporter(std::shared_ptr const& config) { + if (Catch::getRegistryHub().getReporterRegistry().getListeners().empty()) { + return createReporter(config->getReporterName(), config); + } + + auto multi = std::unique_ptr(new ListeningReporter); + + auto const& listeners = Catch::getRegistryHub().getReporterRegistry().getListeners(); + for (auto const& listener : listeners) { + multi->addListener(listener->create(Catch::ReporterConfig(config))); + } + multi->addReporter(createReporter(config->getReporterName(), config)); + return std::move(multi); + } + + Catch::Totals runTests(std::shared_ptr const& config) { + // FixMe: Add listeners in order first, then add reporters. + + auto reporter = makeReporter(config); + + RunContext context(config, std::move(reporter)); + + Totals totals; + + context.testGroupStarting(config->name(), 1, 1); + + TestSpec testSpec = config->testSpec(); + + auto const& allTestCases = getAllTestCasesSorted(*config); + for (auto const& testCase : allTestCases) { + if (!context.aborting() && matchTest(testCase, testSpec, *config)) + totals += context.runTest(testCase); + else + context.reporter().skipTest(testCase); + } + + if (config->warnAboutNoTests() && totals.testCases.total() == 0) { + ReusableStringStream testConfig; + + bool first = true; + for (const auto& input : config->getTestsOrTags()) { + if (!first) { testConfig << ' '; } + first = false; + testConfig << input; + } + + context.reporter().noMatchingTestCases(testConfig.str()); + totals.error = -1; + } + + context.testGroupEnded(config->name(), totals, 1, 1); + return totals; + } + + void applyFilenamesAsTags(Catch::IConfig const& config) { + auto& tests = const_cast&>(getAllTestCasesSorted(config)); + for (auto& testCase : tests) { + auto tags = testCase.tags; + + std::string filename = testCase.lineInfo.file; + auto lastSlash = filename.find_last_of("\\/"); + if (lastSlash != std::string::npos) { + filename.erase(0, lastSlash); + filename[0] = '#'; + } + + auto lastDot = filename.find_last_of('.'); + if (lastDot != std::string::npos) { + filename.erase(lastDot); + } + + tags.push_back(std::move(filename)); + setTags(testCase, tags); + } + } + + } // anon namespace + + Session::Session() { + static bool alreadyInstantiated = false; + if( alreadyInstantiated ) { + try { CATCH_INTERNAL_ERROR( "Only one instance of Catch::Session can ever be used" ); } + catch(...) { getMutableRegistryHub().registerStartupException(); } + } + + const auto& exceptions = getRegistryHub().getStartupExceptionRegistry().getExceptions(); + if ( !exceptions.empty() ) { + m_startupExceptions = true; + Colour colourGuard( Colour::Red ); + Catch::cerr() << "Errors occurred during startup!" << '\n'; + // iterate over all exceptions and notify user + for ( const auto& ex_ptr : exceptions ) { + try { + std::rethrow_exception(ex_ptr); + } catch ( std::exception const& ex ) { + Catch::cerr() << Column( ex.what() ).indent(2) << '\n'; + } + } + } + + alreadyInstantiated = true; + m_cli = makeCommandLineParser( m_configData ); + } + Session::~Session() { + Catch::cleanUp(); + } + + void Session::showHelp() const { + Catch::cout() + << "\nCatch v" << libraryVersion() << "\n" + << m_cli << std::endl + << "For more detailed usage please see the project docs\n" << std::endl; + } + void Session::libIdentify() { + Catch::cout() + << std::left << std::setw(16) << "description: " << "A Catch test executable\n" + << std::left << std::setw(16) << "category: " << "testframework\n" + << std::left << std::setw(16) << "framework: " << "Catch Test\n" + << std::left << std::setw(16) << "version: " << libraryVersion() << std::endl; + } + + int Session::applyCommandLine( int argc, char const * const * argv ) { + if( m_startupExceptions ) + return 1; + + auto result = m_cli.parse( clara::Args( argc, argv ) ); + if( !result ) { + Catch::cerr() + << Colour( Colour::Red ) + << "\nError(s) in input:\n" + << Column( result.errorMessage() ).indent( 2 ) + << "\n\n"; + Catch::cerr() << "Run with -? for usage\n" << std::endl; + return MaxExitCode; + } + + if( m_configData.showHelp ) + showHelp(); + if( m_configData.libIdentify ) + libIdentify(); + m_config.reset(); + return 0; + } + + void Session::useConfigData( ConfigData const& configData ) { + m_configData = configData; + m_config.reset(); + } + + int Session::run( int argc, char* argv[] ) { + if( m_startupExceptions ) + return 1; + int returnCode = applyCommandLine( argc, argv ); + if( returnCode == 0 ) + returnCode = run(); + return returnCode; + } + +#if defined(CATCH_CONFIG_WCHAR) && defined(WIN32) && defined(UNICODE) + int Session::run( int argc, wchar_t* const argv[] ) { + + char **utf8Argv = new char *[ argc ]; + + for ( int i = 0; i < argc; ++i ) { + int bufSize = WideCharToMultiByte( CP_UTF8, 0, argv[i], -1, NULL, 0, NULL, NULL ); + + utf8Argv[ i ] = new char[ bufSize ]; + + WideCharToMultiByte( CP_UTF8, 0, argv[i], -1, utf8Argv[i], bufSize, NULL, NULL ); + } + + int returnCode = run( argc, utf8Argv ); + + for ( int i = 0; i < argc; ++i ) + delete [] utf8Argv[ i ]; + + delete [] utf8Argv; + + return returnCode; + } +#endif + int Session::run() { + if( ( m_configData.waitForKeypress & WaitForKeypress::BeforeStart ) != 0 ) { + Catch::cout() << "...waiting for enter/ return before starting" << std::endl; + static_cast(std::getchar()); + } + int exitCode = runInternal(); + if( ( m_configData.waitForKeypress & WaitForKeypress::BeforeExit ) != 0 ) { + Catch::cout() << "...waiting for enter/ return before exiting, with code: " << exitCode << std::endl; + static_cast(std::getchar()); + } + return exitCode; + } + + clara::Parser const& Session::cli() const { + return m_cli; + } + void Session::cli( clara::Parser const& newParser ) { + m_cli = newParser; + } + ConfigData& Session::configData() { + return m_configData; + } + Config& Session::config() { + if( !m_config ) + m_config = std::make_shared( m_configData ); + return *m_config; + } + + int Session::runInternal() { + if( m_startupExceptions ) + return 1; + + if( m_configData.showHelp || m_configData.libIdentify ) + return 0; + + try + { + config(); // Force config to be constructed + + seedRng( *m_config ); + + if( m_configData.filenamesAsTags ) + applyFilenamesAsTags( *m_config ); + + // Handle list request + if( Option listed = list( config() ) ) + return static_cast( *listed ); + + auto totals = runTests( m_config ); + // Note that on unices only the lower 8 bits are usually used, clamping + // the return value to 255 prevents false negative when some multiple + // of 256 tests has failed + return (std::min) (MaxExitCode, (std::max) (totals.error, static_cast(totals.assertions.failed))); + } + catch( std::exception& ex ) { + Catch::cerr() << ex.what() << std::endl; + return MaxExitCode; + } + } + +} // end namespace Catch +// end catch_session.cpp +// start catch_startup_exception_registry.cpp + +namespace Catch { + void StartupExceptionRegistry::add( std::exception_ptr const& exception ) noexcept { + try { + m_exceptions.push_back(exception); + } + catch(...) { + // If we run out of memory during start-up there's really not a lot more we can do about it + std::terminate(); + } + } + + std::vector const& StartupExceptionRegistry::getExceptions() const noexcept { + return m_exceptions; + } + +} // end namespace Catch +// end catch_startup_exception_registry.cpp +// start catch_stream.cpp + +#include +#include +#include +#include +#include +#include + +#if defined(__clang__) +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wexit-time-destructors" +#endif + +namespace Catch { + + Catch::IStream::~IStream() = default; + + namespace detail { namespace { + template + class StreamBufImpl : public std::streambuf { + char data[bufferSize]; + WriterF m_writer; + + public: + StreamBufImpl() { + setp( data, data + sizeof(data) ); + } + + ~StreamBufImpl() noexcept { + StreamBufImpl::sync(); + } + + private: + int overflow( int c ) override { + sync(); + + if( c != EOF ) { + if( pbase() == epptr() ) + m_writer( std::string( 1, static_cast( c ) ) ); + else + sputc( static_cast( c ) ); + } + return 0; + } + + int sync() override { + if( pbase() != pptr() ) { + m_writer( std::string( pbase(), static_cast( pptr() - pbase() ) ) ); + setp( pbase(), epptr() ); + } + return 0; + } + }; + + /////////////////////////////////////////////////////////////////////////// + + struct OutputDebugWriter { + + void operator()( std::string const&str ) { + writeToDebugConsole( str ); + } + }; + + /////////////////////////////////////////////////////////////////////////// + + class FileStream : public IStream { + mutable std::ofstream m_ofs; + public: + FileStream( StringRef filename ) { + m_ofs.open( filename.c_str() ); + CATCH_ENFORCE( !m_ofs.fail(), "Unable to open file: '" << filename << "'" ); + } + ~FileStream() override = default; + public: // IStream + std::ostream& stream() const override { + return m_ofs; + } + }; + + /////////////////////////////////////////////////////////////////////////// + + class CoutStream : public IStream { + mutable std::ostream m_os; + public: + // Store the streambuf from cout up-front because + // cout may get redirected when running tests + CoutStream() : m_os( Catch::cout().rdbuf() ) {} + ~CoutStream() override = default; + + public: // IStream + std::ostream& stream() const override { return m_os; } + }; + + /////////////////////////////////////////////////////////////////////////// + + class DebugOutStream : public IStream { + std::unique_ptr> m_streamBuf; + mutable std::ostream m_os; + public: + DebugOutStream() + : m_streamBuf( new StreamBufImpl() ), + m_os( m_streamBuf.get() ) + {} + + ~DebugOutStream() override = default; + + public: // IStream + std::ostream& stream() const override { return m_os; } + }; + + }} // namespace anon::detail + + /////////////////////////////////////////////////////////////////////////// + + auto makeStream( StringRef const &filename ) -> IStream const* { + if( filename.empty() ) + return new detail::CoutStream(); + else if( filename[0] == '%' ) { + if( filename == "%debug" ) + return new detail::DebugOutStream(); + else + CATCH_ERROR( "Unrecognised stream: '" << filename << "'" ); + } + else + return new detail::FileStream( filename ); + } + + // This class encapsulates the idea of a pool of ostringstreams that can be reused. + struct StringStreams { + std::vector> m_streams; + std::vector m_unused; + std::ostringstream m_referenceStream; // Used for copy state/ flags from + static StringStreams* s_instance; + + auto add() -> std::size_t { + if( m_unused.empty() ) { + m_streams.push_back( std::unique_ptr( new std::ostringstream ) ); + return m_streams.size()-1; + } + else { + auto index = m_unused.back(); + m_unused.pop_back(); + return index; + } + } + + void release( std::size_t index ) { + m_streams[index]->copyfmt( m_referenceStream ); // Restore initial flags and other state + m_unused.push_back(index); + } + + // !TBD: put in TLS + static auto instance() -> StringStreams& { + if( !s_instance ) + s_instance = new StringStreams(); + return *s_instance; + } + static void cleanup() { + delete s_instance; + s_instance = nullptr; + } + }; + + StringStreams* StringStreams::s_instance = nullptr; + + void ReusableStringStream::cleanup() { + StringStreams::cleanup(); + } + + ReusableStringStream::ReusableStringStream() + : m_index( StringStreams::instance().add() ), + m_oss( StringStreams::instance().m_streams[m_index].get() ) + {} + + ReusableStringStream::~ReusableStringStream() { + static_cast( m_oss )->str(""); + m_oss->clear(); + StringStreams::instance().release( m_index ); + } + + auto ReusableStringStream::str() const -> std::string { + return static_cast( m_oss )->str(); + } + + /////////////////////////////////////////////////////////////////////////// + +#ifndef CATCH_CONFIG_NOSTDOUT // If you #define this you must implement these functions + std::ostream& cout() { return std::cout; } + std::ostream& cerr() { return std::cerr; } + std::ostream& clog() { return std::clog; } +#endif +} + +#if defined(__clang__) +# pragma clang diagnostic pop +#endif +// end catch_stream.cpp +// start catch_string_manip.cpp + +#include +#include +#include +#include + +namespace Catch { + + bool startsWith( std::string const& s, std::string const& prefix ) { + return s.size() >= prefix.size() && std::equal(prefix.begin(), prefix.end(), s.begin()); + } + bool startsWith( std::string const& s, char prefix ) { + return !s.empty() && s[0] == prefix; + } + bool endsWith( std::string const& s, std::string const& suffix ) { + return s.size() >= suffix.size() && std::equal(suffix.rbegin(), suffix.rend(), s.rbegin()); + } + bool endsWith( std::string const& s, char suffix ) { + return !s.empty() && s[s.size()-1] == suffix; + } + bool contains( std::string const& s, std::string const& infix ) { + return s.find( infix ) != std::string::npos; + } + char toLowerCh(char c) { + return static_cast( std::tolower( c ) ); + } + void toLowerInPlace( std::string& s ) { + std::transform( s.begin(), s.end(), s.begin(), toLowerCh ); + } + std::string toLower( std::string const& s ) { + std::string lc = s; + toLowerInPlace( lc ); + return lc; + } + std::string trim( std::string const& str ) { + static char const* whitespaceChars = "\n\r\t "; + std::string::size_type start = str.find_first_not_of( whitespaceChars ); + std::string::size_type end = str.find_last_not_of( whitespaceChars ); + + return start != std::string::npos ? str.substr( start, 1+end-start ) : std::string(); + } + + bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis ) { + bool replaced = false; + std::size_t i = str.find( replaceThis ); + while( i != std::string::npos ) { + replaced = true; + str = str.substr( 0, i ) + withThis + str.substr( i+replaceThis.size() ); + if( i < str.size()-withThis.size() ) + i = str.find( replaceThis, i+withThis.size() ); + else + i = std::string::npos; + } + return replaced; + } + + pluralise::pluralise( std::size_t count, std::string const& label ) + : m_count( count ), + m_label( label ) + {} + + std::ostream& operator << ( std::ostream& os, pluralise const& pluraliser ) { + os << pluraliser.m_count << ' ' << pluraliser.m_label; + if( pluraliser.m_count != 1 ) + os << 's'; + return os; + } + +} +// end catch_string_manip.cpp +// start catch_stringref.cpp + +#if defined(__clang__) +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wexit-time-destructors" +#endif + +#include +#include +#include + +namespace { + const uint32_t byte_2_lead = 0xC0; + const uint32_t byte_3_lead = 0xE0; + const uint32_t byte_4_lead = 0xF0; +} + +namespace Catch { + StringRef::StringRef( char const* rawChars ) noexcept + : StringRef( rawChars, static_cast(std::strlen(rawChars) ) ) + {} + + StringRef::operator std::string() const { + return std::string( m_start, m_size ); + } + + void StringRef::swap( StringRef& other ) noexcept { + std::swap( m_start, other.m_start ); + std::swap( m_size, other.m_size ); + std::swap( m_data, other.m_data ); + } + + auto StringRef::c_str() const -> char const* { + if( isSubstring() ) + const_cast( this )->takeOwnership(); + return m_start; + } + auto StringRef::currentData() const noexcept -> char const* { + return m_start; + } + + auto StringRef::isOwned() const noexcept -> bool { + return m_data != nullptr; + } + auto StringRef::isSubstring() const noexcept -> bool { + return m_start[m_size] != '\0'; + } + + void StringRef::takeOwnership() { + if( !isOwned() ) { + m_data = new char[m_size+1]; + memcpy( m_data, m_start, m_size ); + m_data[m_size] = '\0'; + m_start = m_data; + } + } + auto StringRef::substr( size_type start, size_type size ) const noexcept -> StringRef { + if( start < m_size ) + return StringRef( m_start+start, size ); + else + return StringRef(); + } + auto StringRef::operator == ( StringRef const& other ) const noexcept -> bool { + return + size() == other.size() && + (std::strncmp( m_start, other.m_start, size() ) == 0); + } + auto StringRef::operator != ( StringRef const& other ) const noexcept -> bool { + return !operator==( other ); + } + + auto StringRef::operator[](size_type index) const noexcept -> char { + return m_start[index]; + } + + auto StringRef::numberOfCharacters() const noexcept -> size_type { + size_type noChars = m_size; + // Make adjustments for uft encodings + for( size_type i=0; i < m_size; ++i ) { + char c = m_start[i]; + if( ( c & byte_2_lead ) == byte_2_lead ) { + noChars--; + if (( c & byte_3_lead ) == byte_3_lead ) + noChars--; + if( ( c & byte_4_lead ) == byte_4_lead ) + noChars--; + } + } + return noChars; + } + + auto operator + ( StringRef const& lhs, StringRef const& rhs ) -> std::string { + std::string str; + str.reserve( lhs.size() + rhs.size() ); + str += lhs; + str += rhs; + return str; + } + auto operator + ( StringRef const& lhs, const char* rhs ) -> std::string { + return std::string( lhs ) + std::string( rhs ); + } + auto operator + ( char const* lhs, StringRef const& rhs ) -> std::string { + return std::string( lhs ) + std::string( rhs ); + } + + auto operator << ( std::ostream& os, StringRef const& str ) -> std::ostream& { + return os.write(str.currentData(), str.size()); + } + + auto operator+=( std::string& lhs, StringRef const& rhs ) -> std::string& { + lhs.append(rhs.currentData(), rhs.size()); + return lhs; + } + +} // namespace Catch + +#if defined(__clang__) +# pragma clang diagnostic pop +#endif +// end catch_stringref.cpp +// start catch_tag_alias.cpp + +namespace Catch { + TagAlias::TagAlias(std::string const & _tag, SourceLineInfo _lineInfo): tag(_tag), lineInfo(_lineInfo) {} +} +// end catch_tag_alias.cpp +// start catch_tag_alias_autoregistrar.cpp + +namespace Catch { + + RegistrarForTagAliases::RegistrarForTagAliases(char const* alias, char const* tag, SourceLineInfo const& lineInfo) { + try { + getMutableRegistryHub().registerTagAlias(alias, tag, lineInfo); + } catch (...) { + // Do not throw when constructing global objects, instead register the exception to be processed later + getMutableRegistryHub().registerStartupException(); + } + } + +} +// end catch_tag_alias_autoregistrar.cpp +// start catch_tag_alias_registry.cpp + +#include + +namespace Catch { + + TagAliasRegistry::~TagAliasRegistry() {} + + TagAlias const* TagAliasRegistry::find( std::string const& alias ) const { + auto it = m_registry.find( alias ); + if( it != m_registry.end() ) + return &(it->second); + else + return nullptr; + } + + std::string TagAliasRegistry::expandAliases( std::string const& unexpandedTestSpec ) const { + std::string expandedTestSpec = unexpandedTestSpec; + for( auto const& registryKvp : m_registry ) { + std::size_t pos = expandedTestSpec.find( registryKvp.first ); + if( pos != std::string::npos ) { + expandedTestSpec = expandedTestSpec.substr( 0, pos ) + + registryKvp.second.tag + + expandedTestSpec.substr( pos + registryKvp.first.size() ); + } + } + return expandedTestSpec; + } + + void TagAliasRegistry::add( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) { + CATCH_ENFORCE( startsWith(alias, "[@") && endsWith(alias, ']'), + "error: tag alias, '" << alias << "' is not of the form [@alias name].\n" << lineInfo ); + + CATCH_ENFORCE( m_registry.insert(std::make_pair(alias, TagAlias(tag, lineInfo))).second, + "error: tag alias, '" << alias << "' already registered.\n" + << "\tFirst seen at: " << find(alias)->lineInfo << "\n" + << "\tRedefined at: " << lineInfo ); + } + + ITagAliasRegistry::~ITagAliasRegistry() {} + + ITagAliasRegistry const& ITagAliasRegistry::get() { + return getRegistryHub().getTagAliasRegistry(); + } + +} // end namespace Catch +// end catch_tag_alias_registry.cpp +// start catch_test_case_info.cpp + +#include +#include +#include +#include + +namespace Catch { + + TestCaseInfo::SpecialProperties parseSpecialTag( std::string const& tag ) { + if( startsWith( tag, '.' ) || + tag == "!hide" ) + return TestCaseInfo::IsHidden; + else if( tag == "!throws" ) + return TestCaseInfo::Throws; + else if( tag == "!shouldfail" ) + return TestCaseInfo::ShouldFail; + else if( tag == "!mayfail" ) + return TestCaseInfo::MayFail; + else if( tag == "!nonportable" ) + return TestCaseInfo::NonPortable; + else if( tag == "!benchmark" ) + return static_cast( TestCaseInfo::Benchmark | TestCaseInfo::IsHidden ); + else + return TestCaseInfo::None; + } + bool isReservedTag( std::string const& tag ) { + return parseSpecialTag( tag ) == TestCaseInfo::None && tag.size() > 0 && !std::isalnum( static_cast(tag[0]) ); + } + void enforceNotReservedTag( std::string const& tag, SourceLineInfo const& _lineInfo ) { + CATCH_ENFORCE( !isReservedTag(tag), + "Tag name: [" << tag << "] is not allowed.\n" + << "Tag names starting with non alpha-numeric characters are reserved\n" + << _lineInfo ); + } + + TestCase makeTestCase( ITestInvoker* _testCase, + std::string const& _className, + NameAndTags const& nameAndTags, + SourceLineInfo const& _lineInfo ) + { + bool isHidden = false; + + // Parse out tags + std::vector tags; + std::string desc, tag; + bool inTag = false; + std::string _descOrTags = nameAndTags.tags; + for (char c : _descOrTags) { + if( !inTag ) { + if( c == '[' ) + inTag = true; + else + desc += c; + } + else { + if( c == ']' ) { + TestCaseInfo::SpecialProperties prop = parseSpecialTag( tag ); + if( ( prop & TestCaseInfo::IsHidden ) != 0 ) + isHidden = true; + else if( prop == TestCaseInfo::None ) + enforceNotReservedTag( tag, _lineInfo ); + + tags.push_back( tag ); + tag.clear(); + inTag = false; + } + else + tag += c; + } + } + if( isHidden ) { + tags.push_back( "." ); + } + + TestCaseInfo info( nameAndTags.name, _className, desc, tags, _lineInfo ); + return TestCase( _testCase, std::move(info) ); + } + + void setTags( TestCaseInfo& testCaseInfo, std::vector tags ) { + std::sort(begin(tags), end(tags)); + tags.erase(std::unique(begin(tags), end(tags)), end(tags)); + testCaseInfo.lcaseTags.clear(); + + for( auto const& tag : tags ) { + std::string lcaseTag = toLower( tag ); + testCaseInfo.properties = static_cast( testCaseInfo.properties | parseSpecialTag( lcaseTag ) ); + testCaseInfo.lcaseTags.push_back( lcaseTag ); + } + testCaseInfo.tags = std::move(tags); + } + + TestCaseInfo::TestCaseInfo( std::string const& _name, + std::string const& _className, + std::string const& _description, + std::vector const& _tags, + SourceLineInfo const& _lineInfo ) + : name( _name ), + className( _className ), + description( _description ), + lineInfo( _lineInfo ), + properties( None ) + { + setTags( *this, _tags ); + } + + bool TestCaseInfo::isHidden() const { + return ( properties & IsHidden ) != 0; + } + bool TestCaseInfo::throws() const { + return ( properties & Throws ) != 0; + } + bool TestCaseInfo::okToFail() const { + return ( properties & (ShouldFail | MayFail ) ) != 0; + } + bool TestCaseInfo::expectedToFail() const { + return ( properties & (ShouldFail ) ) != 0; + } + + std::string TestCaseInfo::tagsAsString() const { + std::string ret; + // '[' and ']' per tag + std::size_t full_size = 2 * tags.size(); + for (const auto& tag : tags) { + full_size += tag.size(); + } + ret.reserve(full_size); + for (const auto& tag : tags) { + ret.push_back('['); + ret.append(tag); + ret.push_back(']'); + } + + return ret; + } + + TestCase::TestCase( ITestInvoker* testCase, TestCaseInfo&& info ) : TestCaseInfo( std::move(info) ), test( testCase ) {} + + TestCase TestCase::withName( std::string const& _newName ) const { + TestCase other( *this ); + other.name = _newName; + return other; + } + + void TestCase::invoke() const { + test->invoke(); + } + + bool TestCase::operator == ( TestCase const& other ) const { + return test.get() == other.test.get() && + name == other.name && + className == other.className; + } + + bool TestCase::operator < ( TestCase const& other ) const { + return name < other.name; + } + + TestCaseInfo const& TestCase::getTestCaseInfo() const + { + return *this; + } + +} // end namespace Catch +// end catch_test_case_info.cpp +// start catch_test_case_registry_impl.cpp + +#include + +namespace Catch { + + std::vector sortTests( IConfig const& config, std::vector const& unsortedTestCases ) { + + std::vector sorted = unsortedTestCases; + + switch( config.runOrder() ) { + case RunTests::InLexicographicalOrder: + std::sort( sorted.begin(), sorted.end() ); + break; + case RunTests::InRandomOrder: + seedRng( config ); + RandomNumberGenerator::shuffle( sorted ); + break; + case RunTests::InDeclarationOrder: + // already in declaration order + break; + } + return sorted; + } + bool matchTest( TestCase const& testCase, TestSpec const& testSpec, IConfig const& config ) { + return testSpec.matches( testCase ) && ( config.allowThrows() || !testCase.throws() ); + } + + void enforceNoDuplicateTestCases( std::vector const& functions ) { + std::set seenFunctions; + for( auto const& function : functions ) { + auto prev = seenFunctions.insert( function ); + CATCH_ENFORCE( prev.second, + "error: TEST_CASE( \"" << function.name << "\" ) already defined.\n" + << "\tFirst seen at " << prev.first->getTestCaseInfo().lineInfo << "\n" + << "\tRedefined at " << function.getTestCaseInfo().lineInfo ); + } + } + + std::vector filterTests( std::vector const& testCases, TestSpec const& testSpec, IConfig const& config ) { + std::vector filtered; + filtered.reserve( testCases.size() ); + for( auto const& testCase : testCases ) + if( matchTest( testCase, testSpec, config ) ) + filtered.push_back( testCase ); + return filtered; + } + std::vector const& getAllTestCasesSorted( IConfig const& config ) { + return getRegistryHub().getTestCaseRegistry().getAllTestsSorted( config ); + } + + void TestRegistry::registerTest( TestCase const& testCase ) { + std::string name = testCase.getTestCaseInfo().name; + if( name.empty() ) { + ReusableStringStream rss; + rss << "Anonymous test case " << ++m_unnamedCount; + return registerTest( testCase.withName( rss.str() ) ); + } + m_functions.push_back( testCase ); + } + + std::vector const& TestRegistry::getAllTests() const { + return m_functions; + } + std::vector const& TestRegistry::getAllTestsSorted( IConfig const& config ) const { + if( m_sortedFunctions.empty() ) + enforceNoDuplicateTestCases( m_functions ); + + if( m_currentSortOrder != config.runOrder() || m_sortedFunctions.empty() ) { + m_sortedFunctions = sortTests( config, m_functions ); + m_currentSortOrder = config.runOrder(); + } + return m_sortedFunctions; + } + + /////////////////////////////////////////////////////////////////////////// + TestInvokerAsFunction::TestInvokerAsFunction( void(*testAsFunction)() ) noexcept : m_testAsFunction( testAsFunction ) {} + + void TestInvokerAsFunction::invoke() const { + m_testAsFunction(); + } + + std::string extractClassName( StringRef const& classOrQualifiedMethodName ) { + std::string className = classOrQualifiedMethodName; + if( startsWith( className, '&' ) ) + { + std::size_t lastColons = className.rfind( "::" ); + std::size_t penultimateColons = className.rfind( "::", lastColons-1 ); + if( penultimateColons == std::string::npos ) + penultimateColons = 1; + className = className.substr( penultimateColons, lastColons-penultimateColons ); + } + return className; + } + +} // end namespace Catch +// end catch_test_case_registry_impl.cpp +// start catch_test_case_tracker.cpp + +#include +#include +#include +#include +#include + +#if defined(__clang__) +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wexit-time-destructors" +#endif + +namespace Catch { +namespace TestCaseTracking { + + NameAndLocation::NameAndLocation( std::string const& _name, SourceLineInfo const& _location ) + : name( _name ), + location( _location ) + {} + + ITracker::~ITracker() = default; + + TrackerContext& TrackerContext::instance() { + static TrackerContext s_instance; + return s_instance; + } + + ITracker& TrackerContext::startRun() { + m_rootTracker = std::make_shared( NameAndLocation( "{root}", CATCH_INTERNAL_LINEINFO ), *this, nullptr ); + m_currentTracker = nullptr; + m_runState = Executing; + return *m_rootTracker; + } + + void TrackerContext::endRun() { + m_rootTracker.reset(); + m_currentTracker = nullptr; + m_runState = NotStarted; + } + + void TrackerContext::startCycle() { + m_currentTracker = m_rootTracker.get(); + m_runState = Executing; + } + void TrackerContext::completeCycle() { + m_runState = CompletedCycle; + } + + bool TrackerContext::completedCycle() const { + return m_runState == CompletedCycle; + } + ITracker& TrackerContext::currentTracker() { + return *m_currentTracker; + } + void TrackerContext::setCurrentTracker( ITracker* tracker ) { + m_currentTracker = tracker; + } + + TrackerBase::TrackerHasName::TrackerHasName( NameAndLocation const& nameAndLocation ) : m_nameAndLocation( nameAndLocation ) {} + bool TrackerBase::TrackerHasName::operator ()( ITrackerPtr const& tracker ) const { + return + tracker->nameAndLocation().name == m_nameAndLocation.name && + tracker->nameAndLocation().location == m_nameAndLocation.location; + } + + TrackerBase::TrackerBase( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent ) + : m_nameAndLocation( nameAndLocation ), + m_ctx( ctx ), + m_parent( parent ) + {} + + NameAndLocation const& TrackerBase::nameAndLocation() const { + return m_nameAndLocation; + } + bool TrackerBase::isComplete() const { + return m_runState == CompletedSuccessfully || m_runState == Failed; + } + bool TrackerBase::isSuccessfullyCompleted() const { + return m_runState == CompletedSuccessfully; + } + bool TrackerBase::isOpen() const { + return m_runState != NotStarted && !isComplete(); + } + bool TrackerBase::hasChildren() const { + return !m_children.empty(); + } + + void TrackerBase::addChild( ITrackerPtr const& child ) { + m_children.push_back( child ); + } + + ITrackerPtr TrackerBase::findChild( NameAndLocation const& nameAndLocation ) { + auto it = std::find_if( m_children.begin(), m_children.end(), TrackerHasName( nameAndLocation ) ); + return( it != m_children.end() ) + ? *it + : nullptr; + } + ITracker& TrackerBase::parent() { + assert( m_parent ); // Should always be non-null except for root + return *m_parent; + } + + void TrackerBase::openChild() { + if( m_runState != ExecutingChildren ) { + m_runState = ExecutingChildren; + if( m_parent ) + m_parent->openChild(); + } + } + + bool TrackerBase::isSectionTracker() const { return false; } + bool TrackerBase::isIndexTracker() const { return false; } + + void TrackerBase::open() { + m_runState = Executing; + moveToThis(); + if( m_parent ) + m_parent->openChild(); + } + + void TrackerBase::close() { + + // Close any still open children (e.g. generators) + while( &m_ctx.currentTracker() != this ) + m_ctx.currentTracker().close(); + + switch( m_runState ) { + case NeedsAnotherRun: + break; + + case Executing: + m_runState = CompletedSuccessfully; + break; + case ExecutingChildren: + if( m_children.empty() || m_children.back()->isComplete() ) + m_runState = CompletedSuccessfully; + break; + + case NotStarted: + case CompletedSuccessfully: + case Failed: + CATCH_INTERNAL_ERROR( "Illogical state: " << m_runState ); + + default: + CATCH_INTERNAL_ERROR( "Unknown state: " << m_runState ); + } + moveToParent(); + m_ctx.completeCycle(); + } + void TrackerBase::fail() { + m_runState = Failed; + if( m_parent ) + m_parent->markAsNeedingAnotherRun(); + moveToParent(); + m_ctx.completeCycle(); + } + void TrackerBase::markAsNeedingAnotherRun() { + m_runState = NeedsAnotherRun; + } + + void TrackerBase::moveToParent() { + assert( m_parent ); + m_ctx.setCurrentTracker( m_parent ); + } + void TrackerBase::moveToThis() { + m_ctx.setCurrentTracker( this ); + } + + SectionTracker::SectionTracker( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent ) + : TrackerBase( nameAndLocation, ctx, parent ) + { + if( parent ) { + while( !parent->isSectionTracker() ) + parent = &parent->parent(); + + SectionTracker& parentSection = static_cast( *parent ); + addNextFilters( parentSection.m_filters ); + } + } + + bool SectionTracker::isSectionTracker() const { return true; } + + SectionTracker& SectionTracker::acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation ) { + std::shared_ptr section; + + ITracker& currentTracker = ctx.currentTracker(); + if( ITrackerPtr childTracker = currentTracker.findChild( nameAndLocation ) ) { + assert( childTracker ); + assert( childTracker->isSectionTracker() ); + section = std::static_pointer_cast( childTracker ); + } + else { + section = std::make_shared( nameAndLocation, ctx, ¤tTracker ); + currentTracker.addChild( section ); + } + if( !ctx.completedCycle() ) + section->tryOpen(); + return *section; + } + + void SectionTracker::tryOpen() { + if( !isComplete() && (m_filters.empty() || m_filters[0].empty() || m_filters[0] == m_nameAndLocation.name ) ) + open(); + } + + void SectionTracker::addInitialFilters( std::vector const& filters ) { + if( !filters.empty() ) { + m_filters.push_back(""); // Root - should never be consulted + m_filters.push_back(""); // Test Case - not a section filter + m_filters.insert( m_filters.end(), filters.begin(), filters.end() ); + } + } + void SectionTracker::addNextFilters( std::vector const& filters ) { + if( filters.size() > 1 ) + m_filters.insert( m_filters.end(), ++filters.begin(), filters.end() ); + } + + IndexTracker::IndexTracker( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent, int size ) + : TrackerBase( nameAndLocation, ctx, parent ), + m_size( size ) + {} + + bool IndexTracker::isIndexTracker() const { return true; } + + IndexTracker& IndexTracker::acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation, int size ) { + std::shared_ptr tracker; + + ITracker& currentTracker = ctx.currentTracker(); + if( ITrackerPtr childTracker = currentTracker.findChild( nameAndLocation ) ) { + assert( childTracker ); + assert( childTracker->isIndexTracker() ); + tracker = std::static_pointer_cast( childTracker ); + } + else { + tracker = std::make_shared( nameAndLocation, ctx, ¤tTracker, size ); + currentTracker.addChild( tracker ); + } + + if( !ctx.completedCycle() && !tracker->isComplete() ) { + if( tracker->m_runState != ExecutingChildren && tracker->m_runState != NeedsAnotherRun ) + tracker->moveNext(); + tracker->open(); + } + + return *tracker; + } + + int IndexTracker::index() const { return m_index; } + + void IndexTracker::moveNext() { + m_index++; + m_children.clear(); + } + + void IndexTracker::close() { + TrackerBase::close(); + if( m_runState == CompletedSuccessfully && m_index < m_size-1 ) + m_runState = Executing; + } + +} // namespace TestCaseTracking + +using TestCaseTracking::ITracker; +using TestCaseTracking::TrackerContext; +using TestCaseTracking::SectionTracker; +using TestCaseTracking::IndexTracker; + +} // namespace Catch + +#if defined(__clang__) +# pragma clang diagnostic pop +#endif +// end catch_test_case_tracker.cpp +// start catch_test_registry.cpp + +namespace Catch { + + auto makeTestInvoker( void(*testAsFunction)() ) noexcept -> ITestInvoker* { + return new(std::nothrow) TestInvokerAsFunction( testAsFunction ); + } + + NameAndTags::NameAndTags( StringRef const& name_ , StringRef const& tags_ ) noexcept : name( name_ ), tags( tags_ ) {} + + AutoReg::AutoReg( ITestInvoker* invoker, SourceLineInfo const& lineInfo, StringRef const& classOrMethod, NameAndTags const& nameAndTags ) noexcept { + try { + getMutableRegistryHub() + .registerTest( + makeTestCase( + invoker, + extractClassName( classOrMethod ), + nameAndTags, + lineInfo)); + } catch (...) { + // Do not throw when constructing global objects, instead register the exception to be processed later + getMutableRegistryHub().registerStartupException(); + } + } + + AutoReg::~AutoReg() = default; +} +// end catch_test_registry.cpp +// start catch_test_spec.cpp + +#include +#include +#include +#include + +namespace Catch { + + TestSpec::Pattern::~Pattern() = default; + TestSpec::NamePattern::~NamePattern() = default; + TestSpec::TagPattern::~TagPattern() = default; + TestSpec::ExcludedPattern::~ExcludedPattern() = default; + + TestSpec::NamePattern::NamePattern( std::string const& name ) + : m_wildcardPattern( toLower( name ), CaseSensitive::No ) + {} + bool TestSpec::NamePattern::matches( TestCaseInfo const& testCase ) const { + return m_wildcardPattern.matches( toLower( testCase.name ) ); + } + + TestSpec::TagPattern::TagPattern( std::string const& tag ) : m_tag( toLower( tag ) ) {} + bool TestSpec::TagPattern::matches( TestCaseInfo const& testCase ) const { + return std::find(begin(testCase.lcaseTags), + end(testCase.lcaseTags), + m_tag) != end(testCase.lcaseTags); + } + + TestSpec::ExcludedPattern::ExcludedPattern( PatternPtr const& underlyingPattern ) : m_underlyingPattern( underlyingPattern ) {} + bool TestSpec::ExcludedPattern::matches( TestCaseInfo const& testCase ) const { return !m_underlyingPattern->matches( testCase ); } + + bool TestSpec::Filter::matches( TestCaseInfo const& testCase ) const { + // All patterns in a filter must match for the filter to be a match + for( auto const& pattern : m_patterns ) { + if( !pattern->matches( testCase ) ) + return false; + } + return true; + } + + bool TestSpec::hasFilters() const { + return !m_filters.empty(); + } + bool TestSpec::matches( TestCaseInfo const& testCase ) const { + // A TestSpec matches if any filter matches + for( auto const& filter : m_filters ) + if( filter.matches( testCase ) ) + return true; + return false; + } +} +// end catch_test_spec.cpp +// start catch_test_spec_parser.cpp + +namespace Catch { + + TestSpecParser::TestSpecParser( ITagAliasRegistry const& tagAliases ) : m_tagAliases( &tagAliases ) {} + + TestSpecParser& TestSpecParser::parse( std::string const& arg ) { + m_mode = None; + m_exclusion = false; + m_start = std::string::npos; + m_arg = m_tagAliases->expandAliases( arg ); + m_escapeChars.clear(); + for( m_pos = 0; m_pos < m_arg.size(); ++m_pos ) + visitChar( m_arg[m_pos] ); + if( m_mode == Name ) + addPattern(); + return *this; + } + TestSpec TestSpecParser::testSpec() { + addFilter(); + return m_testSpec; + } + + void TestSpecParser::visitChar( char c ) { + if( m_mode == None ) { + switch( c ) { + case ' ': return; + case '~': m_exclusion = true; return; + case '[': return startNewMode( Tag, ++m_pos ); + case '"': return startNewMode( QuotedName, ++m_pos ); + case '\\': return escape(); + default: startNewMode( Name, m_pos ); break; + } + } + if( m_mode == Name ) { + if( c == ',' ) { + addPattern(); + addFilter(); + } + else if( c == '[' ) { + if( subString() == "exclude:" ) + m_exclusion = true; + else + addPattern(); + startNewMode( Tag, ++m_pos ); + } + else if( c == '\\' ) + escape(); + } + else if( m_mode == EscapedName ) + m_mode = Name; + else if( m_mode == QuotedName && c == '"' ) + addPattern(); + else if( m_mode == Tag && c == ']' ) + addPattern(); + } + void TestSpecParser::startNewMode( Mode mode, std::size_t start ) { + m_mode = mode; + m_start = start; + } + void TestSpecParser::escape() { + if( m_mode == None ) + m_start = m_pos; + m_mode = EscapedName; + m_escapeChars.push_back( m_pos ); + } + std::string TestSpecParser::subString() const { return m_arg.substr( m_start, m_pos - m_start ); } + + void TestSpecParser::addFilter() { + if( !m_currentFilter.m_patterns.empty() ) { + m_testSpec.m_filters.push_back( m_currentFilter ); + m_currentFilter = TestSpec::Filter(); + } + } + + TestSpec parseTestSpec( std::string const& arg ) { + return TestSpecParser( ITagAliasRegistry::get() ).parse( arg ).testSpec(); + } + +} // namespace Catch +// end catch_test_spec_parser.cpp +// start catch_timer.cpp + +#include + +static const uint64_t nanosecondsInSecond = 1000000000; + +namespace Catch { + + auto getCurrentNanosecondsSinceEpoch() -> uint64_t { + return std::chrono::duration_cast( std::chrono::high_resolution_clock::now().time_since_epoch() ).count(); + } + + auto estimateClockResolution() -> uint64_t { + uint64_t sum = 0; + static const uint64_t iterations = 1000000; + + auto startTime = getCurrentNanosecondsSinceEpoch(); + + for( std::size_t i = 0; i < iterations; ++i ) { + + uint64_t ticks; + uint64_t baseTicks = getCurrentNanosecondsSinceEpoch(); + do { + ticks = getCurrentNanosecondsSinceEpoch(); + } while( ticks == baseTicks ); + + auto delta = ticks - baseTicks; + sum += delta; + + // If we have been calibrating for over 3 seconds -- the clock + // is terrible and we should move on. + // TBD: How to signal that the measured resolution is probably wrong? + if (ticks > startTime + 3 * nanosecondsInSecond) { + return sum / i; + } + } + + // We're just taking the mean, here. To do better we could take the std. dev and exclude outliers + // - and potentially do more iterations if there's a high variance. + return sum/iterations; + } + auto getEstimatedClockResolution() -> uint64_t { + static auto s_resolution = estimateClockResolution(); + return s_resolution; + } + + void Timer::start() { + m_nanoseconds = getCurrentNanosecondsSinceEpoch(); + } + auto Timer::getElapsedNanoseconds() const -> uint64_t { + return getCurrentNanosecondsSinceEpoch() - m_nanoseconds; + } + auto Timer::getElapsedMicroseconds() const -> uint64_t { + return getElapsedNanoseconds()/1000; + } + auto Timer::getElapsedMilliseconds() const -> unsigned int { + return static_cast(getElapsedMicroseconds()/1000); + } + auto Timer::getElapsedSeconds() const -> double { + return getElapsedMicroseconds()/1000000.0; + } + +} // namespace Catch +// end catch_timer.cpp +// start catch_tostring.cpp + +#if defined(__clang__) +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wexit-time-destructors" +# pragma clang diagnostic ignored "-Wglobal-constructors" +#endif + +// Enable specific decls locally +#if !defined(CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER) +#define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER +#endif + +#include +#include + +namespace Catch { + +namespace Detail { + + const std::string unprintableString = "{?}"; + + namespace { + const int hexThreshold = 255; + + struct Endianness { + enum Arch { Big, Little }; + + static Arch which() { + union _{ + int asInt; + char asChar[sizeof (int)]; + } u; + + u.asInt = 1; + return ( u.asChar[sizeof(int)-1] == 1 ) ? Big : Little; + } + }; + } + + std::string rawMemoryToString( const void *object, std::size_t size ) { + // Reverse order for little endian architectures + int i = 0, end = static_cast( size ), inc = 1; + if( Endianness::which() == Endianness::Little ) { + i = end-1; + end = inc = -1; + } + + unsigned char const *bytes = static_cast(object); + ReusableStringStream rss; + rss << "0x" << std::setfill('0') << std::hex; + for( ; i != end; i += inc ) + rss << std::setw(2) << static_cast(bytes[i]); + return rss.str(); + } +} + +template +std::string fpToString( T value, int precision ) { + if (std::isnan(value)) { + return "nan"; + } + + ReusableStringStream rss; + rss << std::setprecision( precision ) + << std::fixed + << value; + std::string d = rss.str(); + std::size_t i = d.find_last_not_of( '0' ); + if( i != std::string::npos && i != d.size()-1 ) { + if( d[i] == '.' ) + i++; + d = d.substr( 0, i+1 ); + } + return d; +} + +//// ======================================================= //// +// +// Out-of-line defs for full specialization of StringMaker +// +//// ======================================================= //// + +std::string StringMaker::convert(const std::string& str) { + if (!getCurrentContext().getConfig()->showInvisibles()) { + return '"' + str + '"'; + } + + std::string s("\""); + for (char c : str) { + switch (c) { + case '\n': + s.append("\\n"); + break; + case '\t': + s.append("\\t"); + break; + default: + s.push_back(c); + break; + } + } + s.append("\""); + return s; +} + +#ifdef CATCH_CONFIG_WCHAR +std::string StringMaker::convert(const std::wstring& wstr) { + std::string s; + s.reserve(wstr.size()); + for (auto c : wstr) { + s += (c <= 0xff) ? static_cast(c) : '?'; + } + return ::Catch::Detail::stringify(s); +} +#endif + +std::string StringMaker::convert(char const* str) { + if (str) { + return ::Catch::Detail::stringify(std::string{ str }); + } else { + return{ "{null string}" }; + } +} +std::string StringMaker::convert(char* str) { + if (str) { + return ::Catch::Detail::stringify(std::string{ str }); + } else { + return{ "{null string}" }; + } +} +#ifdef CATCH_CONFIG_WCHAR +std::string StringMaker::convert(wchar_t const * str) { + if (str) { + return ::Catch::Detail::stringify(std::wstring{ str }); + } else { + return{ "{null string}" }; + } +} +std::string StringMaker::convert(wchar_t * str) { + if (str) { + return ::Catch::Detail::stringify(std::wstring{ str }); + } else { + return{ "{null string}" }; + } +} +#endif + +std::string StringMaker::convert(int value) { + return ::Catch::Detail::stringify(static_cast(value)); +} +std::string StringMaker::convert(long value) { + return ::Catch::Detail::stringify(static_cast(value)); +} +std::string StringMaker::convert(long long value) { + ReusableStringStream rss; + rss << value; + if (value > Detail::hexThreshold) { + rss << " (0x" << std::hex << value << ')'; + } + return rss.str(); +} + +std::string StringMaker::convert(unsigned int value) { + return ::Catch::Detail::stringify(static_cast(value)); +} +std::string StringMaker::convert(unsigned long value) { + return ::Catch::Detail::stringify(static_cast(value)); +} +std::string StringMaker::convert(unsigned long long value) { + ReusableStringStream rss; + rss << value; + if (value > Detail::hexThreshold) { + rss << " (0x" << std::hex << value << ')'; + } + return rss.str(); +} + +std::string StringMaker::convert(bool b) { + return b ? "true" : "false"; +} + +std::string StringMaker::convert(char value) { + if (value == '\r') { + return "'\\r'"; + } else if (value == '\f') { + return "'\\f'"; + } else if (value == '\n') { + return "'\\n'"; + } else if (value == '\t') { + return "'\\t'"; + } else if ('\0' <= value && value < ' ') { + return ::Catch::Detail::stringify(static_cast(value)); + } else { + char chstr[] = "' '"; + chstr[1] = value; + return chstr; + } +} +std::string StringMaker::convert(signed char c) { + return ::Catch::Detail::stringify(static_cast(c)); +} +std::string StringMaker::convert(unsigned char c) { + return ::Catch::Detail::stringify(static_cast(c)); +} + +std::string StringMaker::convert(std::nullptr_t) { + return "nullptr"; +} + +std::string StringMaker::convert(float value) { + return fpToString(value, 5) + 'f'; +} +std::string StringMaker::convert(double value) { + return fpToString(value, 10); +} + +std::string ratio_string::symbol() { return "a"; } +std::string ratio_string::symbol() { return "f"; } +std::string ratio_string::symbol() { return "p"; } +std::string ratio_string::symbol() { return "n"; } +std::string ratio_string::symbol() { return "u"; } +std::string ratio_string::symbol() { return "m"; } + +} // end namespace Catch + +#if defined(__clang__) +# pragma clang diagnostic pop +#endif + +// end catch_tostring.cpp +// start catch_totals.cpp + +namespace Catch { + + Counts Counts::operator - ( Counts const& other ) const { + Counts diff; + diff.passed = passed - other.passed; + diff.failed = failed - other.failed; + diff.failedButOk = failedButOk - other.failedButOk; + return diff; + } + + Counts& Counts::operator += ( Counts const& other ) { + passed += other.passed; + failed += other.failed; + failedButOk += other.failedButOk; + return *this; + } + + std::size_t Counts::total() const { + return passed + failed + failedButOk; + } + bool Counts::allPassed() const { + return failed == 0 && failedButOk == 0; + } + bool Counts::allOk() const { + return failed == 0; + } + + Totals Totals::operator - ( Totals const& other ) const { + Totals diff; + diff.assertions = assertions - other.assertions; + diff.testCases = testCases - other.testCases; + return diff; + } + + Totals& Totals::operator += ( Totals const& other ) { + assertions += other.assertions; + testCases += other.testCases; + return *this; + } + + Totals Totals::delta( Totals const& prevTotals ) const { + Totals diff = *this - prevTotals; + if( diff.assertions.failed > 0 ) + ++diff.testCases.failed; + else if( diff.assertions.failedButOk > 0 ) + ++diff.testCases.failedButOk; + else + ++diff.testCases.passed; + return diff; + } + +} +// end catch_totals.cpp +// start catch_uncaught_exceptions.cpp + +#include + +namespace Catch { + bool uncaught_exceptions() { +#if defined(CATCH_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS) + return std::uncaught_exceptions() > 0; +#else + return std::uncaught_exception(); +#endif + } +} // end namespace Catch +// end catch_uncaught_exceptions.cpp +// start catch_version.cpp + +#include + +namespace Catch { + + Version::Version + ( unsigned int _majorVersion, + unsigned int _minorVersion, + unsigned int _patchNumber, + char const * const _branchName, + unsigned int _buildNumber ) + : majorVersion( _majorVersion ), + minorVersion( _minorVersion ), + patchNumber( _patchNumber ), + branchName( _branchName ), + buildNumber( _buildNumber ) + {} + + std::ostream& operator << ( std::ostream& os, Version const& version ) { + os << version.majorVersion << '.' + << version.minorVersion << '.' + << version.patchNumber; + // branchName is never null -> 0th char is \0 if it is empty + if (version.branchName[0]) { + os << '-' << version.branchName + << '.' << version.buildNumber; + } + return os; + } + + Version const& libraryVersion() { + static Version version( 2, 2, 3, "", 0 ); + return version; + } + +} +// end catch_version.cpp +// start catch_wildcard_pattern.cpp + +#include + +namespace Catch { + + WildcardPattern::WildcardPattern( std::string const& pattern, + CaseSensitive::Choice caseSensitivity ) + : m_caseSensitivity( caseSensitivity ), + m_pattern( adjustCase( pattern ) ) + { + if( startsWith( m_pattern, '*' ) ) { + m_pattern = m_pattern.substr( 1 ); + m_wildcard = WildcardAtStart; + } + if( endsWith( m_pattern, '*' ) ) { + m_pattern = m_pattern.substr( 0, m_pattern.size()-1 ); + m_wildcard = static_cast( m_wildcard | WildcardAtEnd ); + } + } + + bool WildcardPattern::matches( std::string const& str ) const { + switch( m_wildcard ) { + case NoWildcard: + return m_pattern == adjustCase( str ); + case WildcardAtStart: + return endsWith( adjustCase( str ), m_pattern ); + case WildcardAtEnd: + return startsWith( adjustCase( str ), m_pattern ); + case WildcardAtBothEnds: + return contains( adjustCase( str ), m_pattern ); + default: + CATCH_INTERNAL_ERROR( "Unknown enum" ); + } + } + + std::string WildcardPattern::adjustCase( std::string const& str ) const { + return m_caseSensitivity == CaseSensitive::No ? toLower( str ) : str; + } +} +// end catch_wildcard_pattern.cpp +// start catch_xmlwriter.cpp + +#include + +using uchar = unsigned char; + +namespace Catch { + +namespace { + + size_t trailingBytes(unsigned char c) { + if ((c & 0xE0) == 0xC0) { + return 2; + } + if ((c & 0xF0) == 0xE0) { + return 3; + } + if ((c & 0xF8) == 0xF0) { + return 4; + } + CATCH_INTERNAL_ERROR("Invalid multibyte utf-8 start byte encountered"); + } + + uint32_t headerValue(unsigned char c) { + if ((c & 0xE0) == 0xC0) { + return c & 0x1F; + } + if ((c & 0xF0) == 0xE0) { + return c & 0x0F; + } + if ((c & 0xF8) == 0xF0) { + return c & 0x07; + } + CATCH_INTERNAL_ERROR("Invalid multibyte utf-8 start byte encountered"); + } + + void hexEscapeChar(std::ostream& os, unsigned char c) { + os << "\\x" + << std::uppercase << std::hex << std::setfill('0') << std::setw(2) + << static_cast(c); + } + +} // anonymous namespace + + XmlEncode::XmlEncode( std::string const& str, ForWhat forWhat ) + : m_str( str ), + m_forWhat( forWhat ) + {} + + void XmlEncode::encodeTo( std::ostream& os ) const { + // Apostrophe escaping not necessary if we always use " to write attributes + // (see: http://www.w3.org/TR/xml/#syntax) + + for( std::size_t idx = 0; idx < m_str.size(); ++ idx ) { + uchar c = m_str[idx]; + switch (c) { + case '<': os << "<"; break; + case '&': os << "&"; break; + + case '>': + // See: http://www.w3.org/TR/xml/#syntax + if (idx > 2 && m_str[idx - 1] == ']' && m_str[idx - 2] == ']') + os << ">"; + else + os << c; + break; + + case '\"': + if (m_forWhat == ForAttributes) + os << """; + else + os << c; + break; + + default: + // Check for control characters and invalid utf-8 + + // Escape control characters in standard ascii + // see http://stackoverflow.com/questions/404107/why-are-control-characters-illegal-in-xml-1-0 + if (c < 0x09 || (c > 0x0D && c < 0x20) || c == 0x7F) { + hexEscapeChar(os, c); + break; + } + + // Plain ASCII: Write it to stream + if (c < 0x7F) { + os << c; + break; + } + + // UTF-8 territory + // Check if the encoding is valid and if it is not, hex escape bytes. + // Important: We do not check the exact decoded values for validity, only the encoding format + // First check that this bytes is a valid lead byte: + // This means that it is not encoded as 1111 1XXX + // Or as 10XX XXXX + if (c < 0xC0 || + c >= 0xF8) { + hexEscapeChar(os, c); + break; + } + + auto encBytes = trailingBytes(c); + // Are there enough bytes left to avoid accessing out-of-bounds memory? + if (idx + encBytes - 1 >= m_str.size()) { + hexEscapeChar(os, c); + break; + } + // The header is valid, check data + // The next encBytes bytes must together be a valid utf-8 + // This means: bitpattern 10XX XXXX and the extracted value is sane (ish) + bool valid = true; + uint32_t value = headerValue(c); + for (std::size_t n = 1; n < encBytes; ++n) { + uchar nc = m_str[idx + n]; + valid &= ((nc & 0xC0) == 0x80); + value = (value << 6) | (nc & 0x3F); + } + + if ( + // Wrong bit pattern of following bytes + (!valid) || + // Overlong encodings + (value < 0x80) || + (0x80 <= value && value < 0x800 && encBytes > 2) || + (0x800 < value && value < 0x10000 && encBytes > 3) || + // Encoded value out of range + (value >= 0x110000) + ) { + hexEscapeChar(os, c); + break; + } + + // If we got here, this is in fact a valid(ish) utf-8 sequence + for (std::size_t n = 0; n < encBytes; ++n) { + os << m_str[idx + n]; + } + idx += encBytes - 1; + break; + } + } + } + + std::ostream& operator << ( std::ostream& os, XmlEncode const& xmlEncode ) { + xmlEncode.encodeTo( os ); + return os; + } + + XmlWriter::ScopedElement::ScopedElement( XmlWriter* writer ) + : m_writer( writer ) + {} + + XmlWriter::ScopedElement::ScopedElement( ScopedElement&& other ) noexcept + : m_writer( other.m_writer ){ + other.m_writer = nullptr; + } + XmlWriter::ScopedElement& XmlWriter::ScopedElement::operator=( ScopedElement&& other ) noexcept { + if ( m_writer ) { + m_writer->endElement(); + } + m_writer = other.m_writer; + other.m_writer = nullptr; + return *this; + } + + XmlWriter::ScopedElement::~ScopedElement() { + if( m_writer ) + m_writer->endElement(); + } + + XmlWriter::ScopedElement& XmlWriter::ScopedElement::writeText( std::string const& text, bool indent ) { + m_writer->writeText( text, indent ); + return *this; + } + + XmlWriter::XmlWriter( std::ostream& os ) : m_os( os ) + { + writeDeclaration(); + } + + XmlWriter::~XmlWriter() { + while( !m_tags.empty() ) + endElement(); + } + + XmlWriter& XmlWriter::startElement( std::string const& name ) { + ensureTagClosed(); + newlineIfNecessary(); + m_os << m_indent << '<' << name; + m_tags.push_back( name ); + m_indent += " "; + m_tagIsOpen = true; + return *this; + } + + XmlWriter::ScopedElement XmlWriter::scopedElement( std::string const& name ) { + ScopedElement scoped( this ); + startElement( name ); + return scoped; + } + + XmlWriter& XmlWriter::endElement() { + newlineIfNecessary(); + m_indent = m_indent.substr( 0, m_indent.size()-2 ); + if( m_tagIsOpen ) { + m_os << "/>"; + m_tagIsOpen = false; + } + else { + m_os << m_indent << ""; + } + m_os << std::endl; + m_tags.pop_back(); + return *this; + } + + XmlWriter& XmlWriter::writeAttribute( std::string const& name, std::string const& attribute ) { + if( !name.empty() && !attribute.empty() ) + m_os << ' ' << name << "=\"" << XmlEncode( attribute, XmlEncode::ForAttributes ) << '"'; + return *this; + } + + XmlWriter& XmlWriter::writeAttribute( std::string const& name, bool attribute ) { + m_os << ' ' << name << "=\"" << ( attribute ? "true" : "false" ) << '"'; + return *this; + } + + XmlWriter& XmlWriter::writeText( std::string const& text, bool indent ) { + if( !text.empty() ){ + bool tagWasOpen = m_tagIsOpen; + ensureTagClosed(); + if( tagWasOpen && indent ) + m_os << m_indent; + m_os << XmlEncode( text ); + m_needsNewline = true; + } + return *this; + } + + XmlWriter& XmlWriter::writeComment( std::string const& text ) { + ensureTagClosed(); + m_os << m_indent << ""; + m_needsNewline = true; + return *this; + } + + void XmlWriter::writeStylesheetRef( std::string const& url ) { + m_os << "\n"; + } + + XmlWriter& XmlWriter::writeBlankLine() { + ensureTagClosed(); + m_os << '\n'; + return *this; + } + + void XmlWriter::ensureTagClosed() { + if( m_tagIsOpen ) { + m_os << ">" << std::endl; + m_tagIsOpen = false; + } + } + + void XmlWriter::writeDeclaration() { + m_os << "\n"; + } + + void XmlWriter::newlineIfNecessary() { + if( m_needsNewline ) { + m_os << std::endl; + m_needsNewline = false; + } + } +} +// end catch_xmlwriter.cpp +// start catch_reporter_bases.cpp + +#include +#include +#include +#include +#include + +namespace Catch { + void prepareExpandedExpression(AssertionResult& result) { + result.getExpandedExpression(); + } + + // Because formatting using c++ streams is stateful, drop down to C is required + // Alternatively we could use stringstream, but its performance is... not good. + std::string getFormattedDuration( double duration ) { + // Max exponent + 1 is required to represent the whole part + // + 1 for decimal point + // + 3 for the 3 decimal places + // + 1 for null terminator + const std::size_t maxDoubleSize = DBL_MAX_10_EXP + 1 + 1 + 3 + 1; + char buffer[maxDoubleSize]; + + // Save previous errno, to prevent sprintf from overwriting it + ErrnoGuard guard; +#ifdef _MSC_VER + sprintf_s(buffer, "%.3f", duration); +#else + sprintf(buffer, "%.3f", duration); +#endif + return std::string(buffer); + } + + TestEventListenerBase::TestEventListenerBase(ReporterConfig const & _config) + :StreamingReporterBase(_config) {} + + void TestEventListenerBase::assertionStarting(AssertionInfo const &) {} + + bool TestEventListenerBase::assertionEnded(AssertionStats const &) { + return false; + } + +} // end namespace Catch +// end catch_reporter_bases.cpp +// start catch_reporter_compact.cpp + +namespace { + +#ifdef CATCH_PLATFORM_MAC + const char* failedString() { return "FAILED"; } + const char* passedString() { return "PASSED"; } +#else + const char* failedString() { return "failed"; } + const char* passedString() { return "passed"; } +#endif + + // Colour::LightGrey + Catch::Colour::Code dimColour() { return Catch::Colour::FileName; } + + std::string bothOrAll( std::size_t count ) { + return count == 1 ? std::string() : + count == 2 ? "both " : "all " ; + } + +} // anon namespace + +namespace Catch { +namespace { +// Colour, message variants: +// - white: No tests ran. +// - red: Failed [both/all] N test cases, failed [both/all] M assertions. +// - white: Passed [both/all] N test cases (no assertions). +// - red: Failed N tests cases, failed M assertions. +// - green: Passed [both/all] N tests cases with M assertions. +void printTotals(std::ostream& out, const Totals& totals) { + if (totals.testCases.total() == 0) { + out << "No tests ran."; + } else if (totals.testCases.failed == totals.testCases.total()) { + Colour colour(Colour::ResultError); + const std::string qualify_assertions_failed = + totals.assertions.failed == totals.assertions.total() ? + bothOrAll(totals.assertions.failed) : std::string(); + out << + "Failed " << bothOrAll(totals.testCases.failed) + << pluralise(totals.testCases.failed, "test case") << ", " + "failed " << qualify_assertions_failed << + pluralise(totals.assertions.failed, "assertion") << '.'; + } else if (totals.assertions.total() == 0) { + out << + "Passed " << bothOrAll(totals.testCases.total()) + << pluralise(totals.testCases.total(), "test case") + << " (no assertions)."; + } else if (totals.assertions.failed) { + Colour colour(Colour::ResultError); + out << + "Failed " << pluralise(totals.testCases.failed, "test case") << ", " + "failed " << pluralise(totals.assertions.failed, "assertion") << '.'; + } else { + Colour colour(Colour::ResultSuccess); + out << + "Passed " << bothOrAll(totals.testCases.passed) + << pluralise(totals.testCases.passed, "test case") << + " with " << pluralise(totals.assertions.passed, "assertion") << '.'; + } +} + +// Implementation of CompactReporter formatting +class AssertionPrinter { +public: + AssertionPrinter& operator= (AssertionPrinter const&) = delete; + AssertionPrinter(AssertionPrinter const&) = delete; + AssertionPrinter(std::ostream& _stream, AssertionStats const& _stats, bool _printInfoMessages) + : stream(_stream) + , result(_stats.assertionResult) + , messages(_stats.infoMessages) + , itMessage(_stats.infoMessages.begin()) + , printInfoMessages(_printInfoMessages) {} + + void print() { + printSourceInfo(); + + itMessage = messages.begin(); + + switch (result.getResultType()) { + case ResultWas::Ok: + printResultType(Colour::ResultSuccess, passedString()); + printOriginalExpression(); + printReconstructedExpression(); + if (!result.hasExpression()) + printRemainingMessages(Colour::None); + else + printRemainingMessages(); + break; + case ResultWas::ExpressionFailed: + if (result.isOk()) + printResultType(Colour::ResultSuccess, failedString() + std::string(" - but was ok")); + else + printResultType(Colour::Error, failedString()); + printOriginalExpression(); + printReconstructedExpression(); + printRemainingMessages(); + break; + case ResultWas::ThrewException: + printResultType(Colour::Error, failedString()); + printIssue("unexpected exception with message:"); + printMessage(); + printExpressionWas(); + printRemainingMessages(); + break; + case ResultWas::FatalErrorCondition: + printResultType(Colour::Error, failedString()); + printIssue("fatal error condition with message:"); + printMessage(); + printExpressionWas(); + printRemainingMessages(); + break; + case ResultWas::DidntThrowException: + printResultType(Colour::Error, failedString()); + printIssue("expected exception, got none"); + printExpressionWas(); + printRemainingMessages(); + break; + case ResultWas::Info: + printResultType(Colour::None, "info"); + printMessage(); + printRemainingMessages(); + break; + case ResultWas::Warning: + printResultType(Colour::None, "warning"); + printMessage(); + printRemainingMessages(); + break; + case ResultWas::ExplicitFailure: + printResultType(Colour::Error, failedString()); + printIssue("explicitly"); + printRemainingMessages(Colour::None); + break; + // These cases are here to prevent compiler warnings + case ResultWas::Unknown: + case ResultWas::FailureBit: + case ResultWas::Exception: + printResultType(Colour::Error, "** internal error **"); + break; + } + } + +private: + void printSourceInfo() const { + Colour colourGuard(Colour::FileName); + stream << result.getSourceInfo() << ':'; + } + + void printResultType(Colour::Code colour, std::string const& passOrFail) const { + if (!passOrFail.empty()) { + { + Colour colourGuard(colour); + stream << ' ' << passOrFail; + } + stream << ':'; + } + } + + void printIssue(std::string const& issue) const { + stream << ' ' << issue; + } + + void printExpressionWas() { + if (result.hasExpression()) { + stream << ';'; + { + Colour colour(dimColour()); + stream << " expression was:"; + } + printOriginalExpression(); + } + } + + void printOriginalExpression() const { + if (result.hasExpression()) { + stream << ' ' << result.getExpression(); + } + } + + void printReconstructedExpression() const { + if (result.hasExpandedExpression()) { + { + Colour colour(dimColour()); + stream << " for: "; + } + stream << result.getExpandedExpression(); + } + } + + void printMessage() { + if (itMessage != messages.end()) { + stream << " '" << itMessage->message << '\''; + ++itMessage; + } + } + + void printRemainingMessages(Colour::Code colour = dimColour()) { + if (itMessage == messages.end()) + return; + + // using messages.end() directly yields (or auto) compilation error: + std::vector::const_iterator itEnd = messages.end(); + const std::size_t N = static_cast(std::distance(itMessage, itEnd)); + + { + Colour colourGuard(colour); + stream << " with " << pluralise(N, "message") << ':'; + } + + for (; itMessage != itEnd; ) { + // If this assertion is a warning ignore any INFO messages + if (printInfoMessages || itMessage->type != ResultWas::Info) { + stream << " '" << itMessage->message << '\''; + if (++itMessage != itEnd) { + Colour colourGuard(dimColour()); + stream << " and"; + } + } + } + } + +private: + std::ostream& stream; + AssertionResult const& result; + std::vector messages; + std::vector::const_iterator itMessage; + bool printInfoMessages; +}; + +} // anon namespace + + std::string CompactReporter::getDescription() { + return "Reports test results on a single line, suitable for IDEs"; + } + + ReporterPreferences CompactReporter::getPreferences() const { + ReporterPreferences prefs; + prefs.shouldRedirectStdOut = false; + return prefs; + } + + void CompactReporter::noMatchingTestCases( std::string const& spec ) { + stream << "No test cases matched '" << spec << '\'' << std::endl; + } + + void CompactReporter::assertionStarting( AssertionInfo const& ) {} + + bool CompactReporter::assertionEnded( AssertionStats const& _assertionStats ) { + AssertionResult const& result = _assertionStats.assertionResult; + + bool printInfoMessages = true; + + // Drop out if result was successful and we're not printing those + if( !m_config->includeSuccessfulResults() && result.isOk() ) { + if( result.getResultType() != ResultWas::Warning ) + return false; + printInfoMessages = false; + } + + AssertionPrinter printer( stream, _assertionStats, printInfoMessages ); + printer.print(); + + stream << std::endl; + return true; + } + + void CompactReporter::sectionEnded(SectionStats const& _sectionStats) { + if (m_config->showDurations() == ShowDurations::Always) { + stream << getFormattedDuration(_sectionStats.durationInSeconds) << " s: " << _sectionStats.sectionInfo.name << std::endl; + } + } + + void CompactReporter::testRunEnded( TestRunStats const& _testRunStats ) { + printTotals( stream, _testRunStats.totals ); + stream << '\n' << std::endl; + StreamingReporterBase::testRunEnded( _testRunStats ); + } + + CompactReporter::~CompactReporter() {} + + CATCH_REGISTER_REPORTER( "compact", CompactReporter ) + +} // end namespace Catch +// end catch_reporter_compact.cpp +// start catch_reporter_console.cpp + +#include +#include + +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable:4061) // Not all labels are EXPLICITLY handled in switch + // Note that 4062 (not all labels are handled + // and default is missing) is enabled +#endif + +namespace Catch { + +namespace { + +// Formatter impl for ConsoleReporter +class ConsoleAssertionPrinter { +public: + ConsoleAssertionPrinter& operator= (ConsoleAssertionPrinter const&) = delete; + ConsoleAssertionPrinter(ConsoleAssertionPrinter const&) = delete; + ConsoleAssertionPrinter(std::ostream& _stream, AssertionStats const& _stats, bool _printInfoMessages) + : stream(_stream), + stats(_stats), + result(_stats.assertionResult), + colour(Colour::None), + message(result.getMessage()), + messages(_stats.infoMessages), + printInfoMessages(_printInfoMessages) { + switch (result.getResultType()) { + case ResultWas::Ok: + colour = Colour::Success; + passOrFail = "PASSED"; + //if( result.hasMessage() ) + if (_stats.infoMessages.size() == 1) + messageLabel = "with message"; + if (_stats.infoMessages.size() > 1) + messageLabel = "with messages"; + break; + case ResultWas::ExpressionFailed: + if (result.isOk()) { + colour = Colour::Success; + passOrFail = "FAILED - but was ok"; + } else { + colour = Colour::Error; + passOrFail = "FAILED"; + } + if (_stats.infoMessages.size() == 1) + messageLabel = "with message"; + if (_stats.infoMessages.size() > 1) + messageLabel = "with messages"; + break; + case ResultWas::ThrewException: + colour = Colour::Error; + passOrFail = "FAILED"; + messageLabel = "due to unexpected exception with "; + if (_stats.infoMessages.size() == 1) + messageLabel += "message"; + if (_stats.infoMessages.size() > 1) + messageLabel += "messages"; + break; + case ResultWas::FatalErrorCondition: + colour = Colour::Error; + passOrFail = "FAILED"; + messageLabel = "due to a fatal error condition"; + break; + case ResultWas::DidntThrowException: + colour = Colour::Error; + passOrFail = "FAILED"; + messageLabel = "because no exception was thrown where one was expected"; + break; + case ResultWas::Info: + messageLabel = "info"; + break; + case ResultWas::Warning: + messageLabel = "warning"; + break; + case ResultWas::ExplicitFailure: + passOrFail = "FAILED"; + colour = Colour::Error; + if (_stats.infoMessages.size() == 1) + messageLabel = "explicitly with message"; + if (_stats.infoMessages.size() > 1) + messageLabel = "explicitly with messages"; + break; + // These cases are here to prevent compiler warnings + case ResultWas::Unknown: + case ResultWas::FailureBit: + case ResultWas::Exception: + passOrFail = "** internal error **"; + colour = Colour::Error; + break; + } + } + + void print() const { + printSourceInfo(); + if (stats.totals.assertions.total() > 0) { + if (result.isOk()) + stream << '\n'; + printResultType(); + printOriginalExpression(); + printReconstructedExpression(); + } else { + stream << '\n'; + } + printMessage(); + } + +private: + void printResultType() const { + if (!passOrFail.empty()) { + Colour colourGuard(colour); + stream << passOrFail << ":\n"; + } + } + void printOriginalExpression() const { + if (result.hasExpression()) { + Colour colourGuard(Colour::OriginalExpression); + stream << " "; + stream << result.getExpressionInMacro(); + stream << '\n'; + } + } + void printReconstructedExpression() const { + if (result.hasExpandedExpression()) { + stream << "with expansion:\n"; + Colour colourGuard(Colour::ReconstructedExpression); + stream << Column(result.getExpandedExpression()).indent(2) << '\n'; + } + } + void printMessage() const { + if (!messageLabel.empty()) + stream << messageLabel << ':' << '\n'; + for (auto const& msg : messages) { + // If this assertion is a warning ignore any INFO messages + if (printInfoMessages || msg.type != ResultWas::Info) + stream << Column(msg.message).indent(2) << '\n'; + } + } + void printSourceInfo() const { + Colour colourGuard(Colour::FileName); + stream << result.getSourceInfo() << ": "; + } + + std::ostream& stream; + AssertionStats const& stats; + AssertionResult const& result; + Colour::Code colour; + std::string passOrFail; + std::string messageLabel; + std::string message; + std::vector messages; + bool printInfoMessages; +}; + +std::size_t makeRatio(std::size_t number, std::size_t total) { + std::size_t ratio = total > 0 ? CATCH_CONFIG_CONSOLE_WIDTH * number / total : 0; + return (ratio == 0 && number > 0) ? 1 : ratio; +} + +std::size_t& findMax(std::size_t& i, std::size_t& j, std::size_t& k) { + if (i > j && i > k) + return i; + else if (j > k) + return j; + else + return k; +} + +struct ColumnInfo { + enum Justification { Left, Right }; + std::string name; + int width; + Justification justification; +}; +struct ColumnBreak {}; +struct RowBreak {}; + +class Duration { + enum class Unit { + Auto, + Nanoseconds, + Microseconds, + Milliseconds, + Seconds, + Minutes + }; + static const uint64_t s_nanosecondsInAMicrosecond = 1000; + static const uint64_t s_nanosecondsInAMillisecond = 1000 * s_nanosecondsInAMicrosecond; + static const uint64_t s_nanosecondsInASecond = 1000 * s_nanosecondsInAMillisecond; + static const uint64_t s_nanosecondsInAMinute = 60 * s_nanosecondsInASecond; + + uint64_t m_inNanoseconds; + Unit m_units; + +public: + explicit Duration(uint64_t inNanoseconds, Unit units = Unit::Auto) + : m_inNanoseconds(inNanoseconds), + m_units(units) { + if (m_units == Unit::Auto) { + if (m_inNanoseconds < s_nanosecondsInAMicrosecond) + m_units = Unit::Nanoseconds; + else if (m_inNanoseconds < s_nanosecondsInAMillisecond) + m_units = Unit::Microseconds; + else if (m_inNanoseconds < s_nanosecondsInASecond) + m_units = Unit::Milliseconds; + else if (m_inNanoseconds < s_nanosecondsInAMinute) + m_units = Unit::Seconds; + else + m_units = Unit::Minutes; + } + + } + + auto value() const -> double { + switch (m_units) { + case Unit::Microseconds: + return m_inNanoseconds / static_cast(s_nanosecondsInAMicrosecond); + case Unit::Milliseconds: + return m_inNanoseconds / static_cast(s_nanosecondsInAMillisecond); + case Unit::Seconds: + return m_inNanoseconds / static_cast(s_nanosecondsInASecond); + case Unit::Minutes: + return m_inNanoseconds / static_cast(s_nanosecondsInAMinute); + default: + return static_cast(m_inNanoseconds); + } + } + auto unitsAsString() const -> std::string { + switch (m_units) { + case Unit::Nanoseconds: + return "ns"; + case Unit::Microseconds: + return "µs"; + case Unit::Milliseconds: + return "ms"; + case Unit::Seconds: + return "s"; + case Unit::Minutes: + return "m"; + default: + return "** internal error **"; + } + + } + friend auto operator << (std::ostream& os, Duration const& duration) -> std::ostream& { + return os << duration.value() << " " << duration.unitsAsString(); + } +}; +} // end anon namespace + +class TablePrinter { + std::ostream& m_os; + std::vector m_columnInfos; + std::ostringstream m_oss; + int m_currentColumn = -1; + bool m_isOpen = false; + +public: + TablePrinter( std::ostream& os, std::vector columnInfos ) + : m_os( os ), + m_columnInfos( std::move( columnInfos ) ) {} + + auto columnInfos() const -> std::vector const& { + return m_columnInfos; + } + + void open() { + if (!m_isOpen) { + m_isOpen = true; + *this << RowBreak(); + for (auto const& info : m_columnInfos) + *this << info.name << ColumnBreak(); + *this << RowBreak(); + m_os << Catch::getLineOfChars<'-'>() << "\n"; + } + } + void close() { + if (m_isOpen) { + *this << RowBreak(); + m_os << std::endl; + m_isOpen = false; + } + } + + template + friend TablePrinter& operator << (TablePrinter& tp, T const& value) { + tp.m_oss << value; + return tp; + } + + friend TablePrinter& operator << (TablePrinter& tp, ColumnBreak) { + auto colStr = tp.m_oss.str(); + // This takes account of utf8 encodings + auto strSize = Catch::StringRef(colStr).numberOfCharacters(); + tp.m_oss.str(""); + tp.open(); + if (tp.m_currentColumn == static_cast(tp.m_columnInfos.size() - 1)) { + tp.m_currentColumn = -1; + tp.m_os << "\n"; + } + tp.m_currentColumn++; + + auto colInfo = tp.m_columnInfos[tp.m_currentColumn]; + auto padding = (strSize + 2 < static_cast(colInfo.width)) + ? std::string(colInfo.width - (strSize + 2), ' ') + : std::string(); + if (colInfo.justification == ColumnInfo::Left) + tp.m_os << colStr << padding << " "; + else + tp.m_os << padding << colStr << " "; + return tp; + } + + friend TablePrinter& operator << (TablePrinter& tp, RowBreak) { + if (tp.m_currentColumn > 0) { + tp.m_os << "\n"; + tp.m_currentColumn = -1; + } + return tp; + } +}; + +ConsoleReporter::ConsoleReporter(ReporterConfig const& config) + : StreamingReporterBase(config), + m_tablePrinter(new TablePrinter(config.stream(), + { + { "benchmark name", CATCH_CONFIG_CONSOLE_WIDTH - 32, ColumnInfo::Left }, + { "iters", 8, ColumnInfo::Right }, + { "elapsed ns", 14, ColumnInfo::Right }, + { "average", 14, ColumnInfo::Right } + })) {} +ConsoleReporter::~ConsoleReporter() = default; + +std::string ConsoleReporter::getDescription() { + return "Reports test results as plain lines of text"; +} + +void ConsoleReporter::noMatchingTestCases(std::string const& spec) { + stream << "No test cases matched '" << spec << '\'' << std::endl; +} + +void ConsoleReporter::assertionStarting(AssertionInfo const&) {} + +bool ConsoleReporter::assertionEnded(AssertionStats const& _assertionStats) { + AssertionResult const& result = _assertionStats.assertionResult; + + bool includeResults = m_config->includeSuccessfulResults() || !result.isOk(); + + // Drop out if result was successful but we're not printing them. + if (!includeResults && result.getResultType() != ResultWas::Warning) + return false; + + lazyPrint(); + + ConsoleAssertionPrinter printer(stream, _assertionStats, includeResults); + printer.print(); + stream << std::endl; + return true; +} + +void ConsoleReporter::sectionStarting(SectionInfo const& _sectionInfo) { + m_headerPrinted = false; + StreamingReporterBase::sectionStarting(_sectionInfo); +} +void ConsoleReporter::sectionEnded(SectionStats const& _sectionStats) { + m_tablePrinter->close(); + if (_sectionStats.missingAssertions) { + lazyPrint(); + Colour colour(Colour::ResultError); + if (m_sectionStack.size() > 1) + stream << "\nNo assertions in section"; + else + stream << "\nNo assertions in test case"; + stream << " '" << _sectionStats.sectionInfo.name << "'\n" << std::endl; + } + if (m_config->showDurations() == ShowDurations::Always) { + stream << getFormattedDuration(_sectionStats.durationInSeconds) << " s: " << _sectionStats.sectionInfo.name << std::endl; + } + if (m_headerPrinted) { + m_headerPrinted = false; + } + StreamingReporterBase::sectionEnded(_sectionStats); +} + +void ConsoleReporter::benchmarkStarting(BenchmarkInfo const& info) { + lazyPrintWithoutClosingBenchmarkTable(); + + auto nameCol = Column( info.name ).width( static_cast( m_tablePrinter->columnInfos()[0].width - 2 ) ); + + bool firstLine = true; + for (auto line : nameCol) { + if (!firstLine) + (*m_tablePrinter) << ColumnBreak() << ColumnBreak() << ColumnBreak(); + else + firstLine = false; + + (*m_tablePrinter) << line << ColumnBreak(); + } +} +void ConsoleReporter::benchmarkEnded(BenchmarkStats const& stats) { + Duration average(stats.elapsedTimeInNanoseconds / stats.iterations); + (*m_tablePrinter) + << stats.iterations << ColumnBreak() + << stats.elapsedTimeInNanoseconds << ColumnBreak() + << average << ColumnBreak(); +} + +void ConsoleReporter::testCaseEnded(TestCaseStats const& _testCaseStats) { + m_tablePrinter->close(); + StreamingReporterBase::testCaseEnded(_testCaseStats); + m_headerPrinted = false; +} +void ConsoleReporter::testGroupEnded(TestGroupStats const& _testGroupStats) { + if (currentGroupInfo.used) { + printSummaryDivider(); + stream << "Summary for group '" << _testGroupStats.groupInfo.name << "':\n"; + printTotals(_testGroupStats.totals); + stream << '\n' << std::endl; + } + StreamingReporterBase::testGroupEnded(_testGroupStats); +} +void ConsoleReporter::testRunEnded(TestRunStats const& _testRunStats) { + printTotalsDivider(_testRunStats.totals); + printTotals(_testRunStats.totals); + stream << std::endl; + StreamingReporterBase::testRunEnded(_testRunStats); +} + +void ConsoleReporter::lazyPrint() { + + m_tablePrinter->close(); + lazyPrintWithoutClosingBenchmarkTable(); +} + +void ConsoleReporter::lazyPrintWithoutClosingBenchmarkTable() { + + if (!currentTestRunInfo.used) + lazyPrintRunInfo(); + if (!currentGroupInfo.used) + lazyPrintGroupInfo(); + + if (!m_headerPrinted) { + printTestCaseAndSectionHeader(); + m_headerPrinted = true; + } +} +void ConsoleReporter::lazyPrintRunInfo() { + stream << '\n' << getLineOfChars<'~'>() << '\n'; + Colour colour(Colour::SecondaryText); + stream << currentTestRunInfo->name + << " is a Catch v" << libraryVersion() << " host application.\n" + << "Run with -? for options\n\n"; + + if (m_config->rngSeed() != 0) + stream << "Randomness seeded to: " << m_config->rngSeed() << "\n\n"; + + currentTestRunInfo.used = true; +} +void ConsoleReporter::lazyPrintGroupInfo() { + if (!currentGroupInfo->name.empty() && currentGroupInfo->groupsCounts > 1) { + printClosedHeader("Group: " + currentGroupInfo->name); + currentGroupInfo.used = true; + } +} +void ConsoleReporter::printTestCaseAndSectionHeader() { + assert(!m_sectionStack.empty()); + printOpenHeader(currentTestCaseInfo->name); + + if (m_sectionStack.size() > 1) { + Colour colourGuard(Colour::Headers); + + auto + it = m_sectionStack.begin() + 1, // Skip first section (test case) + itEnd = m_sectionStack.end(); + for (; it != itEnd; ++it) + printHeaderString(it->name, 2); + } + + SourceLineInfo lineInfo = m_sectionStack.back().lineInfo; + + if (!lineInfo.empty()) { + stream << getLineOfChars<'-'>() << '\n'; + Colour colourGuard(Colour::FileName); + stream << lineInfo << '\n'; + } + stream << getLineOfChars<'.'>() << '\n' << std::endl; +} + +void ConsoleReporter::printClosedHeader(std::string const& _name) { + printOpenHeader(_name); + stream << getLineOfChars<'.'>() << '\n'; +} +void ConsoleReporter::printOpenHeader(std::string const& _name) { + stream << getLineOfChars<'-'>() << '\n'; + { + Colour colourGuard(Colour::Headers); + printHeaderString(_name); + } +} + +// if string has a : in first line will set indent to follow it on +// subsequent lines +void ConsoleReporter::printHeaderString(std::string const& _string, std::size_t indent) { + std::size_t i = _string.find(": "); + if (i != std::string::npos) + i += 2; + else + i = 0; + stream << Column(_string).indent(indent + i).initialIndent(indent) << '\n'; +} + +struct SummaryColumn { + + SummaryColumn( std::string _label, Colour::Code _colour ) + : label( std::move( _label ) ), + colour( _colour ) {} + SummaryColumn addRow( std::size_t count ) { + ReusableStringStream rss; + rss << count; + std::string row = rss.str(); + for (auto& oldRow : rows) { + while (oldRow.size() < row.size()) + oldRow = ' ' + oldRow; + while (oldRow.size() > row.size()) + row = ' ' + row; + } + rows.push_back(row); + return *this; + } + + std::string label; + Colour::Code colour; + std::vector rows; + +}; + +void ConsoleReporter::printTotals( Totals const& totals ) { + if (totals.testCases.total() == 0) { + stream << Colour(Colour::Warning) << "No tests ran\n"; + } else if (totals.assertions.total() > 0 && totals.testCases.allPassed()) { + stream << Colour(Colour::ResultSuccess) << "All tests passed"; + stream << " (" + << pluralise(totals.assertions.passed, "assertion") << " in " + << pluralise(totals.testCases.passed, "test case") << ')' + << '\n'; + } else { + + std::vector columns; + columns.push_back(SummaryColumn("", Colour::None) + .addRow(totals.testCases.total()) + .addRow(totals.assertions.total())); + columns.push_back(SummaryColumn("passed", Colour::Success) + .addRow(totals.testCases.passed) + .addRow(totals.assertions.passed)); + columns.push_back(SummaryColumn("failed", Colour::ResultError) + .addRow(totals.testCases.failed) + .addRow(totals.assertions.failed)); + columns.push_back(SummaryColumn("failed as expected", Colour::ResultExpectedFailure) + .addRow(totals.testCases.failedButOk) + .addRow(totals.assertions.failedButOk)); + + printSummaryRow("test cases", columns, 0); + printSummaryRow("assertions", columns, 1); + } +} +void ConsoleReporter::printSummaryRow(std::string const& label, std::vector const& cols, std::size_t row) { + for (auto col : cols) { + std::string value = col.rows[row]; + if (col.label.empty()) { + stream << label << ": "; + if (value != "0") + stream << value; + else + stream << Colour(Colour::Warning) << "- none -"; + } else if (value != "0") { + stream << Colour(Colour::LightGrey) << " | "; + stream << Colour(col.colour) + << value << ' ' << col.label; + } + } + stream << '\n'; +} + +void ConsoleReporter::printTotalsDivider(Totals const& totals) { + if (totals.testCases.total() > 0) { + std::size_t failedRatio = makeRatio(totals.testCases.failed, totals.testCases.total()); + std::size_t failedButOkRatio = makeRatio(totals.testCases.failedButOk, totals.testCases.total()); + std::size_t passedRatio = makeRatio(totals.testCases.passed, totals.testCases.total()); + while (failedRatio + failedButOkRatio + passedRatio < CATCH_CONFIG_CONSOLE_WIDTH - 1) + findMax(failedRatio, failedButOkRatio, passedRatio)++; + while (failedRatio + failedButOkRatio + passedRatio > CATCH_CONFIG_CONSOLE_WIDTH - 1) + findMax(failedRatio, failedButOkRatio, passedRatio)--; + + stream << Colour(Colour::Error) << std::string(failedRatio, '='); + stream << Colour(Colour::ResultExpectedFailure) << std::string(failedButOkRatio, '='); + if (totals.testCases.allPassed()) + stream << Colour(Colour::ResultSuccess) << std::string(passedRatio, '='); + else + stream << Colour(Colour::Success) << std::string(passedRatio, '='); + } else { + stream << Colour(Colour::Warning) << std::string(CATCH_CONFIG_CONSOLE_WIDTH - 1, '='); + } + stream << '\n'; +} +void ConsoleReporter::printSummaryDivider() { + stream << getLineOfChars<'-'>() << '\n'; +} + +CATCH_REGISTER_REPORTER("console", ConsoleReporter) + +} // end namespace Catch + +#if defined(_MSC_VER) +#pragma warning(pop) +#endif +// end catch_reporter_console.cpp +// start catch_reporter_junit.cpp + +#include +#include +#include +#include + +namespace Catch { + + namespace { + std::string getCurrentTimestamp() { + // Beware, this is not reentrant because of backward compatibility issues + // Also, UTC only, again because of backward compatibility (%z is C++11) + time_t rawtime; + std::time(&rawtime); + auto const timeStampSize = sizeof("2017-01-16T17:06:45Z"); + +#ifdef _MSC_VER + std::tm timeInfo = {}; + gmtime_s(&timeInfo, &rawtime); +#else + std::tm* timeInfo; + timeInfo = std::gmtime(&rawtime); +#endif + + char timeStamp[timeStampSize]; + const char * const fmt = "%Y-%m-%dT%H:%M:%SZ"; + +#ifdef _MSC_VER + std::strftime(timeStamp, timeStampSize, fmt, &timeInfo); +#else + std::strftime(timeStamp, timeStampSize, fmt, timeInfo); +#endif + return std::string(timeStamp); + } + + std::string fileNameTag(const std::vector &tags) { + auto it = std::find_if(begin(tags), + end(tags), + [] (std::string const& tag) {return tag.front() == '#'; }); + if (it != tags.end()) + return it->substr(1); + return std::string(); + } + } // anonymous namespace + + JunitReporter::JunitReporter( ReporterConfig const& _config ) + : CumulativeReporterBase( _config ), + xml( _config.stream() ) + { + m_reporterPrefs.shouldRedirectStdOut = true; + } + + JunitReporter::~JunitReporter() {} + + std::string JunitReporter::getDescription() { + return "Reports test results in an XML format that looks like Ant's junitreport target"; + } + + void JunitReporter::noMatchingTestCases( std::string const& /*spec*/ ) {} + + void JunitReporter::testRunStarting( TestRunInfo const& runInfo ) { + CumulativeReporterBase::testRunStarting( runInfo ); + xml.startElement( "testsuites" ); + } + + void JunitReporter::testGroupStarting( GroupInfo const& groupInfo ) { + suiteTimer.start(); + stdOutForSuite.clear(); + stdErrForSuite.clear(); + unexpectedExceptions = 0; + CumulativeReporterBase::testGroupStarting( groupInfo ); + } + + void JunitReporter::testCaseStarting( TestCaseInfo const& testCaseInfo ) { + m_okToFail = testCaseInfo.okToFail(); + } + + bool JunitReporter::assertionEnded( AssertionStats const& assertionStats ) { + if( assertionStats.assertionResult.getResultType() == ResultWas::ThrewException && !m_okToFail ) + unexpectedExceptions++; + return CumulativeReporterBase::assertionEnded( assertionStats ); + } + + void JunitReporter::testCaseEnded( TestCaseStats const& testCaseStats ) { + stdOutForSuite += testCaseStats.stdOut; + stdErrForSuite += testCaseStats.stdErr; + CumulativeReporterBase::testCaseEnded( testCaseStats ); + } + + void JunitReporter::testGroupEnded( TestGroupStats const& testGroupStats ) { + double suiteTime = suiteTimer.getElapsedSeconds(); + CumulativeReporterBase::testGroupEnded( testGroupStats ); + writeGroup( *m_testGroups.back(), suiteTime ); + } + + void JunitReporter::testRunEndedCumulative() { + xml.endElement(); + } + + void JunitReporter::writeGroup( TestGroupNode const& groupNode, double suiteTime ) { + XmlWriter::ScopedElement e = xml.scopedElement( "testsuite" ); + TestGroupStats const& stats = groupNode.value; + xml.writeAttribute( "name", stats.groupInfo.name ); + xml.writeAttribute( "errors", unexpectedExceptions ); + xml.writeAttribute( "failures", stats.totals.assertions.failed-unexpectedExceptions ); + xml.writeAttribute( "tests", stats.totals.assertions.total() ); + xml.writeAttribute( "hostname", "tbd" ); // !TBD + if( m_config->showDurations() == ShowDurations::Never ) + xml.writeAttribute( "time", "" ); + else + xml.writeAttribute( "time", suiteTime ); + xml.writeAttribute( "timestamp", getCurrentTimestamp() ); + + // Write test cases + for( auto const& child : groupNode.children ) + writeTestCase( *child ); + + xml.scopedElement( "system-out" ).writeText( trim( stdOutForSuite ), false ); + xml.scopedElement( "system-err" ).writeText( trim( stdErrForSuite ), false ); + } + + void JunitReporter::writeTestCase( TestCaseNode const& testCaseNode ) { + TestCaseStats const& stats = testCaseNode.value; + + // All test cases have exactly one section - which represents the + // test case itself. That section may have 0-n nested sections + assert( testCaseNode.children.size() == 1 ); + SectionNode const& rootSection = *testCaseNode.children.front(); + + std::string className = stats.testInfo.className; + + if( className.empty() ) { + className = fileNameTag(stats.testInfo.tags); + if ( className.empty() ) + className = "global"; + } + + if ( !m_config->name().empty() ) + className = m_config->name() + "." + className; + + writeSection( className, "", rootSection ); + } + + void JunitReporter::writeSection( std::string const& className, + std::string const& rootName, + SectionNode const& sectionNode ) { + std::string name = trim( sectionNode.stats.sectionInfo.name ); + if( !rootName.empty() ) + name = rootName + '/' + name; + + if( !sectionNode.assertions.empty() || + !sectionNode.stdOut.empty() || + !sectionNode.stdErr.empty() ) { + XmlWriter::ScopedElement e = xml.scopedElement( "testcase" ); + if( className.empty() ) { + xml.writeAttribute( "classname", name ); + xml.writeAttribute( "name", "root" ); + } + else { + xml.writeAttribute( "classname", className ); + xml.writeAttribute( "name", name ); + } + xml.writeAttribute( "time", ::Catch::Detail::stringify( sectionNode.stats.durationInSeconds ) ); + + writeAssertions( sectionNode ); + + if( !sectionNode.stdOut.empty() ) + xml.scopedElement( "system-out" ).writeText( trim( sectionNode.stdOut ), false ); + if( !sectionNode.stdErr.empty() ) + xml.scopedElement( "system-err" ).writeText( trim( sectionNode.stdErr ), false ); + } + for( auto const& childNode : sectionNode.childSections ) + if( className.empty() ) + writeSection( name, "", *childNode ); + else + writeSection( className, name, *childNode ); + } + + void JunitReporter::writeAssertions( SectionNode const& sectionNode ) { + for( auto const& assertion : sectionNode.assertions ) + writeAssertion( assertion ); + } + + void JunitReporter::writeAssertion( AssertionStats const& stats ) { + AssertionResult const& result = stats.assertionResult; + if( !result.isOk() ) { + std::string elementName; + switch( result.getResultType() ) { + case ResultWas::ThrewException: + case ResultWas::FatalErrorCondition: + elementName = "error"; + break; + case ResultWas::ExplicitFailure: + elementName = "failure"; + break; + case ResultWas::ExpressionFailed: + elementName = "failure"; + break; + case ResultWas::DidntThrowException: + elementName = "failure"; + break; + + // We should never see these here: + case ResultWas::Info: + case ResultWas::Warning: + case ResultWas::Ok: + case ResultWas::Unknown: + case ResultWas::FailureBit: + case ResultWas::Exception: + elementName = "internalError"; + break; + } + + XmlWriter::ScopedElement e = xml.scopedElement( elementName ); + + xml.writeAttribute( "message", result.getExpandedExpression() ); + xml.writeAttribute( "type", result.getTestMacroName() ); + + ReusableStringStream rss; + if( !result.getMessage().empty() ) + rss << result.getMessage() << '\n'; + for( auto const& msg : stats.infoMessages ) + if( msg.type == ResultWas::Info ) + rss << msg.message << '\n'; + + rss << "at " << result.getSourceInfo(); + xml.writeText( rss.str(), false ); + } + } + + CATCH_REGISTER_REPORTER( "junit", JunitReporter ) + +} // end namespace Catch +// end catch_reporter_junit.cpp +// start catch_reporter_listening.cpp + +#include + +namespace Catch { + + void ListeningReporter::addListener( IStreamingReporterPtr&& listener ) { + m_listeners.push_back( std::move( listener ) ); + } + + void ListeningReporter::addReporter(IStreamingReporterPtr&& reporter) { + assert(!m_reporter && "Listening reporter can wrap only 1 real reporter"); + m_reporter = std::move( reporter ); + } + + ReporterPreferences ListeningReporter::getPreferences() const { + return m_reporter->getPreferences(); + } + + std::set ListeningReporter::getSupportedVerbosities() { + return std::set{ }; + } + + void ListeningReporter::noMatchingTestCases( std::string const& spec ) { + for ( auto const& listener : m_listeners ) { + listener->noMatchingTestCases( spec ); + } + m_reporter->noMatchingTestCases( spec ); + } + + void ListeningReporter::benchmarkStarting( BenchmarkInfo const& benchmarkInfo ) { + for ( auto const& listener : m_listeners ) { + listener->benchmarkStarting( benchmarkInfo ); + } + m_reporter->benchmarkStarting( benchmarkInfo ); + } + void ListeningReporter::benchmarkEnded( BenchmarkStats const& benchmarkStats ) { + for ( auto const& listener : m_listeners ) { + listener->benchmarkEnded( benchmarkStats ); + } + m_reporter->benchmarkEnded( benchmarkStats ); + } + + void ListeningReporter::testRunStarting( TestRunInfo const& testRunInfo ) { + for ( auto const& listener : m_listeners ) { + listener->testRunStarting( testRunInfo ); + } + m_reporter->testRunStarting( testRunInfo ); + } + + void ListeningReporter::testGroupStarting( GroupInfo const& groupInfo ) { + for ( auto const& listener : m_listeners ) { + listener->testGroupStarting( groupInfo ); + } + m_reporter->testGroupStarting( groupInfo ); + } + + void ListeningReporter::testCaseStarting( TestCaseInfo const& testInfo ) { + for ( auto const& listener : m_listeners ) { + listener->testCaseStarting( testInfo ); + } + m_reporter->testCaseStarting( testInfo ); + } + + void ListeningReporter::sectionStarting( SectionInfo const& sectionInfo ) { + for ( auto const& listener : m_listeners ) { + listener->sectionStarting( sectionInfo ); + } + m_reporter->sectionStarting( sectionInfo ); + } + + void ListeningReporter::assertionStarting( AssertionInfo const& assertionInfo ) { + for ( auto const& listener : m_listeners ) { + listener->assertionStarting( assertionInfo ); + } + m_reporter->assertionStarting( assertionInfo ); + } + + // The return value indicates if the messages buffer should be cleared: + bool ListeningReporter::assertionEnded( AssertionStats const& assertionStats ) { + for( auto const& listener : m_listeners ) { + static_cast( listener->assertionEnded( assertionStats ) ); + } + return m_reporter->assertionEnded( assertionStats ); + } + + void ListeningReporter::sectionEnded( SectionStats const& sectionStats ) { + for ( auto const& listener : m_listeners ) { + listener->sectionEnded( sectionStats ); + } + m_reporter->sectionEnded( sectionStats ); + } + + void ListeningReporter::testCaseEnded( TestCaseStats const& testCaseStats ) { + for ( auto const& listener : m_listeners ) { + listener->testCaseEnded( testCaseStats ); + } + m_reporter->testCaseEnded( testCaseStats ); + } + + void ListeningReporter::testGroupEnded( TestGroupStats const& testGroupStats ) { + for ( auto const& listener : m_listeners ) { + listener->testGroupEnded( testGroupStats ); + } + m_reporter->testGroupEnded( testGroupStats ); + } + + void ListeningReporter::testRunEnded( TestRunStats const& testRunStats ) { + for ( auto const& listener : m_listeners ) { + listener->testRunEnded( testRunStats ); + } + m_reporter->testRunEnded( testRunStats ); + } + + void ListeningReporter::skipTest( TestCaseInfo const& testInfo ) { + for ( auto const& listener : m_listeners ) { + listener->skipTest( testInfo ); + } + m_reporter->skipTest( testInfo ); + } + + bool ListeningReporter::isMulti() const { + return true; + } + +} // end namespace Catch +// end catch_reporter_listening.cpp +// start catch_reporter_xml.cpp + +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable:4061) // Not all labels are EXPLICITLY handled in switch + // Note that 4062 (not all labels are handled + // and default is missing) is enabled +#endif + +namespace Catch { + XmlReporter::XmlReporter( ReporterConfig const& _config ) + : StreamingReporterBase( _config ), + m_xml(_config.stream()) + { + m_reporterPrefs.shouldRedirectStdOut = true; + } + + XmlReporter::~XmlReporter() = default; + + std::string XmlReporter::getDescription() { + return "Reports test results as an XML document"; + } + + std::string XmlReporter::getStylesheetRef() const { + return std::string(); + } + + void XmlReporter::writeSourceInfo( SourceLineInfo const& sourceInfo ) { + m_xml + .writeAttribute( "filename", sourceInfo.file ) + .writeAttribute( "line", sourceInfo.line ); + } + + void XmlReporter::noMatchingTestCases( std::string const& s ) { + StreamingReporterBase::noMatchingTestCases( s ); + } + + void XmlReporter::testRunStarting( TestRunInfo const& testInfo ) { + StreamingReporterBase::testRunStarting( testInfo ); + std::string stylesheetRef = getStylesheetRef(); + if( !stylesheetRef.empty() ) + m_xml.writeStylesheetRef( stylesheetRef ); + m_xml.startElement( "Catch" ); + if( !m_config->name().empty() ) + m_xml.writeAttribute( "name", m_config->name() ); + } + + void XmlReporter::testGroupStarting( GroupInfo const& groupInfo ) { + StreamingReporterBase::testGroupStarting( groupInfo ); + m_xml.startElement( "Group" ) + .writeAttribute( "name", groupInfo.name ); + } + + void XmlReporter::testCaseStarting( TestCaseInfo const& testInfo ) { + StreamingReporterBase::testCaseStarting(testInfo); + m_xml.startElement( "TestCase" ) + .writeAttribute( "name", trim( testInfo.name ) ) + .writeAttribute( "description", testInfo.description ) + .writeAttribute( "tags", testInfo.tagsAsString() ); + + writeSourceInfo( testInfo.lineInfo ); + + if ( m_config->showDurations() == ShowDurations::Always ) + m_testCaseTimer.start(); + m_xml.ensureTagClosed(); + } + + void XmlReporter::sectionStarting( SectionInfo const& sectionInfo ) { + StreamingReporterBase::sectionStarting( sectionInfo ); + if( m_sectionDepth++ > 0 ) { + m_xml.startElement( "Section" ) + .writeAttribute( "name", trim( sectionInfo.name ) ) + .writeAttribute( "description", sectionInfo.description ); + writeSourceInfo( sectionInfo.lineInfo ); + m_xml.ensureTagClosed(); + } + } + + void XmlReporter::assertionStarting( AssertionInfo const& ) { } + + bool XmlReporter::assertionEnded( AssertionStats const& assertionStats ) { + + AssertionResult const& result = assertionStats.assertionResult; + + bool includeResults = m_config->includeSuccessfulResults() || !result.isOk(); + + if( includeResults || result.getResultType() == ResultWas::Warning ) { + // Print any info messages in tags. + for( auto const& msg : assertionStats.infoMessages ) { + if( msg.type == ResultWas::Info && includeResults ) { + m_xml.scopedElement( "Info" ) + .writeText( msg.message ); + } else if ( msg.type == ResultWas::Warning ) { + m_xml.scopedElement( "Warning" ) + .writeText( msg.message ); + } + } + } + + // Drop out if result was successful but we're not printing them. + if( !includeResults && result.getResultType() != ResultWas::Warning ) + return true; + + // Print the expression if there is one. + if( result.hasExpression() ) { + m_xml.startElement( "Expression" ) + .writeAttribute( "success", result.succeeded() ) + .writeAttribute( "type", result.getTestMacroName() ); + + writeSourceInfo( result.getSourceInfo() ); + + m_xml.scopedElement( "Original" ) + .writeText( result.getExpression() ); + m_xml.scopedElement( "Expanded" ) + .writeText( result.getExpandedExpression() ); + } + + // And... Print a result applicable to each result type. + switch( result.getResultType() ) { + case ResultWas::ThrewException: + m_xml.startElement( "Exception" ); + writeSourceInfo( result.getSourceInfo() ); + m_xml.writeText( result.getMessage() ); + m_xml.endElement(); + break; + case ResultWas::FatalErrorCondition: + m_xml.startElement( "FatalErrorCondition" ); + writeSourceInfo( result.getSourceInfo() ); + m_xml.writeText( result.getMessage() ); + m_xml.endElement(); + break; + case ResultWas::Info: + m_xml.scopedElement( "Info" ) + .writeText( result.getMessage() ); + break; + case ResultWas::Warning: + // Warning will already have been written + break; + case ResultWas::ExplicitFailure: + m_xml.startElement( "Failure" ); + writeSourceInfo( result.getSourceInfo() ); + m_xml.writeText( result.getMessage() ); + m_xml.endElement(); + break; + default: + break; + } + + if( result.hasExpression() ) + m_xml.endElement(); + + return true; + } + + void XmlReporter::sectionEnded( SectionStats const& sectionStats ) { + StreamingReporterBase::sectionEnded( sectionStats ); + if( --m_sectionDepth > 0 ) { + XmlWriter::ScopedElement e = m_xml.scopedElement( "OverallResults" ); + e.writeAttribute( "successes", sectionStats.assertions.passed ); + e.writeAttribute( "failures", sectionStats.assertions.failed ); + e.writeAttribute( "expectedFailures", sectionStats.assertions.failedButOk ); + + if ( m_config->showDurations() == ShowDurations::Always ) + e.writeAttribute( "durationInSeconds", sectionStats.durationInSeconds ); + + m_xml.endElement(); + } + } + + void XmlReporter::testCaseEnded( TestCaseStats const& testCaseStats ) { + StreamingReporterBase::testCaseEnded( testCaseStats ); + XmlWriter::ScopedElement e = m_xml.scopedElement( "OverallResult" ); + e.writeAttribute( "success", testCaseStats.totals.assertions.allOk() ); + + if ( m_config->showDurations() == ShowDurations::Always ) + e.writeAttribute( "durationInSeconds", m_testCaseTimer.getElapsedSeconds() ); + + if( !testCaseStats.stdOut.empty() ) + m_xml.scopedElement( "StdOut" ).writeText( trim( testCaseStats.stdOut ), false ); + if( !testCaseStats.stdErr.empty() ) + m_xml.scopedElement( "StdErr" ).writeText( trim( testCaseStats.stdErr ), false ); + + m_xml.endElement(); + } + + void XmlReporter::testGroupEnded( TestGroupStats const& testGroupStats ) { + StreamingReporterBase::testGroupEnded( testGroupStats ); + // TODO: Check testGroupStats.aborting and act accordingly. + m_xml.scopedElement( "OverallResults" ) + .writeAttribute( "successes", testGroupStats.totals.assertions.passed ) + .writeAttribute( "failures", testGroupStats.totals.assertions.failed ) + .writeAttribute( "expectedFailures", testGroupStats.totals.assertions.failedButOk ); + m_xml.endElement(); + } + + void XmlReporter::testRunEnded( TestRunStats const& testRunStats ) { + StreamingReporterBase::testRunEnded( testRunStats ); + m_xml.scopedElement( "OverallResults" ) + .writeAttribute( "successes", testRunStats.totals.assertions.passed ) + .writeAttribute( "failures", testRunStats.totals.assertions.failed ) + .writeAttribute( "expectedFailures", testRunStats.totals.assertions.failedButOk ); + m_xml.endElement(); + } + + CATCH_REGISTER_REPORTER( "xml", XmlReporter ) + +} // end namespace Catch + +#if defined(_MSC_VER) +#pragma warning(pop) +#endif +// end catch_reporter_xml.cpp + +namespace Catch { + LeakDetector leakDetector; +} + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +// end catch_impl.hpp +#endif + +#ifdef CATCH_CONFIG_MAIN +// start catch_default_main.hpp + +#ifndef __OBJC__ + +#if defined(CATCH_CONFIG_WCHAR) && defined(WIN32) && defined(_UNICODE) && !defined(DO_NOT_USE_WMAIN) +// Standard C/C++ Win32 Unicode wmain entry point +extern "C" int wmain (int argc, wchar_t * argv[], wchar_t * []) { +#else +// Standard C/C++ main entry point +int main (int argc, char * argv[]) { +#endif + + return Catch::Session().run( argc, argv ); +} + +#else // __OBJC__ + +// Objective-C entry point +int main (int argc, char * const argv[]) { +#if !CATCH_ARC_ENABLED + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; +#endif + + Catch::registerTestMethods(); + int result = Catch::Session().run( argc, (char**)argv ); + +#if !CATCH_ARC_ENABLED + [pool drain]; +#endif + + return result; +} + +#endif // __OBJC__ + +// end catch_default_main.hpp +#endif + +#if !defined(CATCH_CONFIG_IMPL_ONLY) + +#ifdef CLARA_CONFIG_MAIN_NOT_DEFINED +# undef CLARA_CONFIG_MAIN +#endif + +#if !defined(CATCH_CONFIG_DISABLE) +////// +// If this config identifier is defined then all CATCH macros are prefixed with CATCH_ +#ifdef CATCH_CONFIG_PREFIX_ALL + +#define CATCH_REQUIRE( ... ) INTERNAL_CATCH_TEST( "CATCH_REQUIRE", Catch::ResultDisposition::Normal, __VA_ARGS__ ) +#define CATCH_REQUIRE_FALSE( ... ) INTERNAL_CATCH_TEST( "CATCH_REQUIRE_FALSE", Catch::ResultDisposition::Normal | Catch::ResultDisposition::FalseTest, __VA_ARGS__ ) + +#define CATCH_REQUIRE_THROWS( ... ) INTERNAL_CATCH_THROWS( "CATCH_REQUIRE_THROWS", Catch::ResultDisposition::Normal, "", __VA_ARGS__ ) +#define CATCH_REQUIRE_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( "CATCH_REQUIRE_THROWS_AS", exceptionType, Catch::ResultDisposition::Normal, expr ) +#define CATCH_REQUIRE_THROWS_WITH( expr, matcher ) INTERNAL_CATCH_THROWS_STR_MATCHES( "CATCH_REQUIRE_THROWS_WITH", Catch::ResultDisposition::Normal, matcher, expr ) +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) +#define CATCH_REQUIRE_THROWS_MATCHES( expr, exceptionType, matcher ) INTERNAL_CATCH_THROWS_MATCHES( "CATCH_REQUIRE_THROWS_MATCHES", exceptionType, Catch::ResultDisposition::Normal, matcher, expr ) +#endif// CATCH_CONFIG_DISABLE_MATCHERS +#define CATCH_REQUIRE_NOTHROW( ... ) INTERNAL_CATCH_NO_THROW( "CATCH_REQUIRE_NOTHROW", Catch::ResultDisposition::Normal, __VA_ARGS__ ) + +#define CATCH_CHECK( ... ) INTERNAL_CATCH_TEST( "CATCH_CHECK", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) +#define CATCH_CHECK_FALSE( ... ) INTERNAL_CATCH_TEST( "CATCH_CHECK_FALSE", Catch::ResultDisposition::ContinueOnFailure | Catch::ResultDisposition::FalseTest, __VA_ARGS__ ) +#define CATCH_CHECKED_IF( ... ) INTERNAL_CATCH_IF( "CATCH_CHECKED_IF", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) +#define CATCH_CHECKED_ELSE( ... ) INTERNAL_CATCH_ELSE( "CATCH_CHECKED_ELSE", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) +#define CATCH_CHECK_NOFAIL( ... ) INTERNAL_CATCH_TEST( "CATCH_CHECK_NOFAIL", Catch::ResultDisposition::ContinueOnFailure | Catch::ResultDisposition::SuppressFail, __VA_ARGS__ ) + +#define CATCH_CHECK_THROWS( ... ) INTERNAL_CATCH_THROWS( "CATCH_CHECK_THROWS", Catch::ResultDisposition::ContinueOnFailure, "", __VA_ARGS__ ) +#define CATCH_CHECK_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( "CATCH_CHECK_THROWS_AS", exceptionType, Catch::ResultDisposition::ContinueOnFailure, expr ) +#define CATCH_CHECK_THROWS_WITH( expr, matcher ) INTERNAL_CATCH_THROWS_STR_MATCHES( "CATCH_CHECK_THROWS_WITH", Catch::ResultDisposition::ContinueOnFailure, matcher, expr ) +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) +#define CATCH_CHECK_THROWS_MATCHES( expr, exceptionType, matcher ) INTERNAL_CATCH_THROWS_MATCHES( "CATCH_CHECK_THROWS_MATCHES", exceptionType, Catch::ResultDisposition::ContinueOnFailure, matcher, expr ) +#endif // CATCH_CONFIG_DISABLE_MATCHERS +#define CATCH_CHECK_NOTHROW( ... ) INTERNAL_CATCH_NO_THROW( "CATCH_CHECK_NOTHROW", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) + +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) +#define CATCH_CHECK_THAT( arg, matcher ) INTERNAL_CHECK_THAT( "CATCH_CHECK_THAT", matcher, Catch::ResultDisposition::ContinueOnFailure, arg ) + +#define CATCH_REQUIRE_THAT( arg, matcher ) INTERNAL_CHECK_THAT( "CATCH_REQUIRE_THAT", matcher, Catch::ResultDisposition::Normal, arg ) +#endif // CATCH_CONFIG_DISABLE_MATCHERS + +#define CATCH_INFO( msg ) INTERNAL_CATCH_INFO( "CATCH_INFO", msg ) +#define CATCH_WARN( msg ) INTERNAL_CATCH_MSG( "CATCH_WARN", Catch::ResultWas::Warning, Catch::ResultDisposition::ContinueOnFailure, msg ) +#define CATCH_CAPTURE( msg ) INTERNAL_CATCH_INFO( "CATCH_CAPTURE", #msg " := " << ::Catch::Detail::stringify(msg) ) + +#define CATCH_TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE( __VA_ARGS__ ) +#define CATCH_TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, __VA_ARGS__ ) +#define CATCH_METHOD_AS_TEST_CASE( method, ... ) INTERNAL_CATCH_METHOD_AS_TEST_CASE( method, __VA_ARGS__ ) +#define CATCH_REGISTER_TEST_CASE( Function, ... ) INTERNAL_CATCH_REGISTER_TESTCASE( Function, __VA_ARGS__ ) +#define CATCH_SECTION( ... ) INTERNAL_CATCH_SECTION( __VA_ARGS__ ) +#define CATCH_FAIL( ... ) INTERNAL_CATCH_MSG( "CATCH_FAIL", Catch::ResultWas::ExplicitFailure, Catch::ResultDisposition::Normal, __VA_ARGS__ ) +#define CATCH_FAIL_CHECK( ... ) INTERNAL_CATCH_MSG( "CATCH_FAIL_CHECK", Catch::ResultWas::ExplicitFailure, Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) +#define CATCH_SUCCEED( ... ) INTERNAL_CATCH_MSG( "CATCH_SUCCEED", Catch::ResultWas::Ok, Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) + +#define CATCH_ANON_TEST_CASE() INTERNAL_CATCH_TESTCASE() + +// "BDD-style" convenience wrappers +#define CATCH_SCENARIO( ... ) CATCH_TEST_CASE( "Scenario: " __VA_ARGS__ ) +#define CATCH_SCENARIO_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, "Scenario: " __VA_ARGS__ ) +#define CATCH_GIVEN( desc ) CATCH_SECTION( std::string( "Given: ") + desc ) +#define CATCH_WHEN( desc ) CATCH_SECTION( std::string( " When: ") + desc ) +#define CATCH_AND_WHEN( desc ) CATCH_SECTION( std::string( " And: ") + desc ) +#define CATCH_THEN( desc ) CATCH_SECTION( std::string( " Then: ") + desc ) +#define CATCH_AND_THEN( desc ) CATCH_SECTION( std::string( " And: ") + desc ) + +// If CATCH_CONFIG_PREFIX_ALL is not defined then the CATCH_ prefix is not required +#else + +#define REQUIRE( ... ) INTERNAL_CATCH_TEST( "REQUIRE", Catch::ResultDisposition::Normal, __VA_ARGS__ ) +#define REQUIRE_FALSE( ... ) INTERNAL_CATCH_TEST( "REQUIRE_FALSE", Catch::ResultDisposition::Normal | Catch::ResultDisposition::FalseTest, __VA_ARGS__ ) + +#define REQUIRE_THROWS( ... ) INTERNAL_CATCH_THROWS( "REQUIRE_THROWS", Catch::ResultDisposition::Normal, __VA_ARGS__ ) +#define REQUIRE_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( "REQUIRE_THROWS_AS", exceptionType, Catch::ResultDisposition::Normal, expr ) +#define REQUIRE_THROWS_WITH( expr, matcher ) INTERNAL_CATCH_THROWS_STR_MATCHES( "REQUIRE_THROWS_WITH", Catch::ResultDisposition::Normal, matcher, expr ) +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) +#define REQUIRE_THROWS_MATCHES( expr, exceptionType, matcher ) INTERNAL_CATCH_THROWS_MATCHES( "REQUIRE_THROWS_MATCHES", exceptionType, Catch::ResultDisposition::Normal, matcher, expr ) +#endif // CATCH_CONFIG_DISABLE_MATCHERS +#define REQUIRE_NOTHROW( ... ) INTERNAL_CATCH_NO_THROW( "REQUIRE_NOTHROW", Catch::ResultDisposition::Normal, __VA_ARGS__ ) + +#define CHECK( ... ) INTERNAL_CATCH_TEST( "CHECK", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) +#define CHECK_FALSE( ... ) INTERNAL_CATCH_TEST( "CHECK_FALSE", Catch::ResultDisposition::ContinueOnFailure | Catch::ResultDisposition::FalseTest, __VA_ARGS__ ) +#define CHECKED_IF( ... ) INTERNAL_CATCH_IF( "CHECKED_IF", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) +#define CHECKED_ELSE( ... ) INTERNAL_CATCH_ELSE( "CHECKED_ELSE", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) +#define CHECK_NOFAIL( ... ) INTERNAL_CATCH_TEST( "CHECK_NOFAIL", Catch::ResultDisposition::ContinueOnFailure | Catch::ResultDisposition::SuppressFail, __VA_ARGS__ ) + +#define CHECK_THROWS( ... ) INTERNAL_CATCH_THROWS( "CHECK_THROWS", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) +#define CHECK_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( "CHECK_THROWS_AS", exceptionType, Catch::ResultDisposition::ContinueOnFailure, expr ) +#define CHECK_THROWS_WITH( expr, matcher ) INTERNAL_CATCH_THROWS_STR_MATCHES( "CHECK_THROWS_WITH", Catch::ResultDisposition::ContinueOnFailure, matcher, expr ) +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) +#define CHECK_THROWS_MATCHES( expr, exceptionType, matcher ) INTERNAL_CATCH_THROWS_MATCHES( "CHECK_THROWS_MATCHES", exceptionType, Catch::ResultDisposition::ContinueOnFailure, matcher, expr ) +#endif // CATCH_CONFIG_DISABLE_MATCHERS +#define CHECK_NOTHROW( ... ) INTERNAL_CATCH_NO_THROW( "CHECK_NOTHROW", Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) + +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) +#define CHECK_THAT( arg, matcher ) INTERNAL_CHECK_THAT( "CHECK_THAT", matcher, Catch::ResultDisposition::ContinueOnFailure, arg ) + +#define REQUIRE_THAT( arg, matcher ) INTERNAL_CHECK_THAT( "REQUIRE_THAT", matcher, Catch::ResultDisposition::Normal, arg ) +#endif // CATCH_CONFIG_DISABLE_MATCHERS + +#define INFO( msg ) INTERNAL_CATCH_INFO( "INFO", msg ) +#define WARN( msg ) INTERNAL_CATCH_MSG( "WARN", Catch::ResultWas::Warning, Catch::ResultDisposition::ContinueOnFailure, msg ) +#define CAPTURE( msg ) INTERNAL_CATCH_INFO( "CAPTURE", #msg " := " << ::Catch::Detail::stringify(msg) ) + +#define TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE( __VA_ARGS__ ) +#define TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, __VA_ARGS__ ) +#define METHOD_AS_TEST_CASE( method, ... ) INTERNAL_CATCH_METHOD_AS_TEST_CASE( method, __VA_ARGS__ ) +#define REGISTER_TEST_CASE( Function, ... ) INTERNAL_CATCH_REGISTER_TESTCASE( Function, __VA_ARGS__ ) +#define SECTION( ... ) INTERNAL_CATCH_SECTION( __VA_ARGS__ ) +#define FAIL( ... ) INTERNAL_CATCH_MSG( "FAIL", Catch::ResultWas::ExplicitFailure, Catch::ResultDisposition::Normal, __VA_ARGS__ ) +#define FAIL_CHECK( ... ) INTERNAL_CATCH_MSG( "FAIL_CHECK", Catch::ResultWas::ExplicitFailure, Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) +#define SUCCEED( ... ) INTERNAL_CATCH_MSG( "SUCCEED", Catch::ResultWas::Ok, Catch::ResultDisposition::ContinueOnFailure, __VA_ARGS__ ) +#define ANON_TEST_CASE() INTERNAL_CATCH_TESTCASE() + +#endif + +#define CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) + +// "BDD-style" convenience wrappers +#define SCENARIO( ... ) TEST_CASE( "Scenario: " __VA_ARGS__ ) +#define SCENARIO_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, "Scenario: " __VA_ARGS__ ) + +#define GIVEN( desc ) SECTION( std::string(" Given: ") + desc ) +#define WHEN( desc ) SECTION( std::string(" When: ") + desc ) +#define AND_WHEN( desc ) SECTION( std::string("And when: ") + desc ) +#define THEN( desc ) SECTION( std::string(" Then: ") + desc ) +#define AND_THEN( desc ) SECTION( std::string(" And: ") + desc ) + +using Catch::Detail::Approx; + +#else +////// +// If this config identifier is defined then all CATCH macros are prefixed with CATCH_ +#ifdef CATCH_CONFIG_PREFIX_ALL + +#define CATCH_REQUIRE( ... ) (void)(0) +#define CATCH_REQUIRE_FALSE( ... ) (void)(0) + +#define CATCH_REQUIRE_THROWS( ... ) (void)(0) +#define CATCH_REQUIRE_THROWS_AS( expr, exceptionType ) (void)(0) +#define CATCH_REQUIRE_THROWS_WITH( expr, matcher ) (void)(0) +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) +#define CATCH_REQUIRE_THROWS_MATCHES( expr, exceptionType, matcher ) (void)(0) +#endif// CATCH_CONFIG_DISABLE_MATCHERS +#define CATCH_REQUIRE_NOTHROW( ... ) (void)(0) + +#define CATCH_CHECK( ... ) (void)(0) +#define CATCH_CHECK_FALSE( ... ) (void)(0) +#define CATCH_CHECKED_IF( ... ) if (__VA_ARGS__) +#define CATCH_CHECKED_ELSE( ... ) if (!(__VA_ARGS__)) +#define CATCH_CHECK_NOFAIL( ... ) (void)(0) + +#define CATCH_CHECK_THROWS( ... ) (void)(0) +#define CATCH_CHECK_THROWS_AS( expr, exceptionType ) (void)(0) +#define CATCH_CHECK_THROWS_WITH( expr, matcher ) (void)(0) +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) +#define CATCH_CHECK_THROWS_MATCHES( expr, exceptionType, matcher ) (void)(0) +#endif // CATCH_CONFIG_DISABLE_MATCHERS +#define CATCH_CHECK_NOTHROW( ... ) (void)(0) + +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) +#define CATCH_CHECK_THAT( arg, matcher ) (void)(0) + +#define CATCH_REQUIRE_THAT( arg, matcher ) (void)(0) +#endif // CATCH_CONFIG_DISABLE_MATCHERS + +#define CATCH_INFO( msg ) (void)(0) +#define CATCH_WARN( msg ) (void)(0) +#define CATCH_CAPTURE( msg ) (void)(0) + +#define CATCH_TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )) +#define CATCH_TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )) +#define CATCH_METHOD_AS_TEST_CASE( method, ... ) +#define CATCH_REGISTER_TEST_CASE( Function, ... ) (void)(0) +#define CATCH_SECTION( ... ) +#define CATCH_FAIL( ... ) (void)(0) +#define CATCH_FAIL_CHECK( ... ) (void)(0) +#define CATCH_SUCCEED( ... ) (void)(0) + +#define CATCH_ANON_TEST_CASE() INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )) + +// "BDD-style" convenience wrappers +#define CATCH_SCENARIO( ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )) +#define CATCH_SCENARIO_METHOD( className, ... ) INTERNAL_CATCH_TESTCASE_METHOD_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ), className ) +#define CATCH_GIVEN( desc ) +#define CATCH_WHEN( desc ) +#define CATCH_AND_WHEN( desc ) +#define CATCH_THEN( desc ) +#define CATCH_AND_THEN( desc ) + +// If CATCH_CONFIG_PREFIX_ALL is not defined then the CATCH_ prefix is not required +#else + +#define REQUIRE( ... ) (void)(0) +#define REQUIRE_FALSE( ... ) (void)(0) + +#define REQUIRE_THROWS( ... ) (void)(0) +#define REQUIRE_THROWS_AS( expr, exceptionType ) (void)(0) +#define REQUIRE_THROWS_WITH( expr, matcher ) (void)(0) +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) +#define REQUIRE_THROWS_MATCHES( expr, exceptionType, matcher ) (void)(0) +#endif // CATCH_CONFIG_DISABLE_MATCHERS +#define REQUIRE_NOTHROW( ... ) (void)(0) + +#define CHECK( ... ) (void)(0) +#define CHECK_FALSE( ... ) (void)(0) +#define CHECKED_IF( ... ) if (__VA_ARGS__) +#define CHECKED_ELSE( ... ) if (!(__VA_ARGS__)) +#define CHECK_NOFAIL( ... ) (void)(0) + +#define CHECK_THROWS( ... ) (void)(0) +#define CHECK_THROWS_AS( expr, exceptionType ) (void)(0) +#define CHECK_THROWS_WITH( expr, matcher ) (void)(0) +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) +#define CHECK_THROWS_MATCHES( expr, exceptionType, matcher ) (void)(0) +#endif // CATCH_CONFIG_DISABLE_MATCHERS +#define CHECK_NOTHROW( ... ) (void)(0) + +#if !defined(CATCH_CONFIG_DISABLE_MATCHERS) +#define CHECK_THAT( arg, matcher ) (void)(0) + +#define REQUIRE_THAT( arg, matcher ) (void)(0) +#endif // CATCH_CONFIG_DISABLE_MATCHERS + +#define INFO( msg ) (void)(0) +#define WARN( msg ) (void)(0) +#define CAPTURE( msg ) (void)(0) + +#define TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )) +#define TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )) +#define METHOD_AS_TEST_CASE( method, ... ) +#define REGISTER_TEST_CASE( Function, ... ) (void)(0) +#define SECTION( ... ) +#define FAIL( ... ) (void)(0) +#define FAIL_CHECK( ... ) (void)(0) +#define SUCCEED( ... ) (void)(0) +#define ANON_TEST_CASE() INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )) + +#endif + +#define CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION_NO_REG( INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ), signature ) + +// "BDD-style" convenience wrappers +#define SCENARIO( ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ) ) +#define SCENARIO_METHOD( className, ... ) INTERNAL_CATCH_TESTCASE_METHOD_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ), className ) + +#define GIVEN( desc ) +#define WHEN( desc ) +#define AND_WHEN( desc ) +#define THEN( desc ) +#define AND_THEN( desc ) + +using Catch::Detail::Approx; + +#endif + +#endif // ! CATCH_CONFIG_IMPL_ONLY + +// start catch_reenable_warnings.h + + +#ifdef __clang__ +# ifdef __ICC // icpc defines the __clang__ macro +# pragma warning(pop) +# else +# pragma clang diagnostic pop +# endif +#elif defined __GNUC__ +# pragma GCC diagnostic pop +#endif + +// end catch_reenable_warnings.h +// end catch.hpp +#endif // TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED + diff --git a/Firmware/AutoDeplete.cpp b/Firmware/AutoDeplete.cpp new file mode 100644 index 0000000..9c4340f --- /dev/null +++ b/Firmware/AutoDeplete.cpp @@ -0,0 +1,79 @@ +//! @file +//! @author: Marek Bel +//! @date Jan 3, 2019 + +#include "AutoDeplete.h" +#include "assert.h" + +//! @brief bit field marking depleted filaments +//! +//! binary 1 marks filament as depleted +//! Zero initialized value means, that no filament is depleted. +static uint8_t depleted; +static const uint8_t filamentCount = 5; + +//! @return binary 1 for all filaments +//! @par fCount number of filaments +static constexpr uint8_t allDepleted(uint8_t fCount) +{ + return fCount == 1 ? 1 : ((1 << (fCount - 1)) | allDepleted(fCount - 1)); +} + +//! @brief Is filament available for printing? +//! @par filament Filament number to be checked +//! @retval true Filament is available for printing. +//! @retval false Filament is not available for printing. +static bool loaded(uint8_t filament) +{ + if (depleted & (1 << filament)) return false; + return true; +} + +//! @brief Mark filament as not available for printing. +//! @par filament filament to be marked +void ad_markDepleted(uint8_t filament) +{ + assert(filament < filamentCount); + if (filament < filamentCount) + { + depleted |= 1 << filament; + } +} + +//! @brief Mark filament as available for printing. +//! @par filament filament to be marked +void ad_markLoaded(uint8_t filament) +{ + assert(filament < filamentCount); + if (filament < filamentCount) + { + depleted &= ~(1 << filament); + } +} + +//! @brief Get alternative filament, which is not depleted +//! @par filament filament +//! @return Filament, if it is depleted, returns next available, +//! if all filaments are depleted, returns filament function parameter. +uint8_t ad_getAlternative(uint8_t filament) +{ + assert(filament < filamentCount); + for (uint8_t i = 0; i + +void ad_markDepleted(uint8_t filament); +void ad_markLoaded(uint8_t filament); +uint8_t ad_getAlternative(uint8_t filament); +bool ad_allDepleted(); + +#endif /* AUTODEPLETE_H */ diff --git a/Firmware/BlinkM.cpp b/Firmware/BlinkM.cpp new file mode 100644 index 0000000..de604ec --- /dev/null +++ b/Firmware/BlinkM.cpp @@ -0,0 +1,29 @@ +/* + BlinkM.cpp - Library for controlling a BlinkM over i2c + Created by Tim Koster, August 21 2013. +*/ +#include "Marlin.h" +#ifdef BLINKM + +#if (ARDUINO >= 100) + # include "Arduino.h" +#else + # include "WProgram.h" +#endif + +#include "BlinkM.h" + +void SendColors(byte red, byte grn, byte blu) +{ + Wire.begin(); + Wire.beginTransmission(0x09); + Wire.write('o'); //to disable ongoing script, only needs to be used once + Wire.write('n'); + Wire.write(red); + Wire.write(grn); + Wire.write(blu); + Wire.endTransmission(); +} + +#endif //BLINKM + diff --git a/Firmware/BlinkM.h b/Firmware/BlinkM.h new file mode 100644 index 0000000..5136828 --- /dev/null +++ b/Firmware/BlinkM.h @@ -0,0 +1,14 @@ +/* + BlinkM.h + Library header file for BlinkM library + */ +#if (ARDUINO >= 100) + # include "Arduino.h" +#else + # include "WProgram.h" +#endif + +#include "Wire.h" + +void SendColors(byte red, byte grn, byte blu); + diff --git a/Firmware/Configuration.cpp b/Firmware/Configuration.cpp new file mode 100644 index 0000000..f878492 --- /dev/null +++ b/Firmware/Configuration.cpp @@ -0,0 +1,10 @@ +#include "Configuration.h" +#include "Configuration_prusa.h" + +const uint16_t _nPrinterType PROGMEM=PRINTER_TYPE; +const char _sPrinterName[] PROGMEM=PRINTER_NAME; +const uint16_t _nPrinterMmuType PROGMEM=PRINTER_MMU_TYPE; +const char _sPrinterMmuName[] PROGMEM=PRINTER_MMU_NAME; + +uint16_t nPrinterType; +PGM_P sPrinterName; \ No newline at end of file diff --git a/Firmware/Configuration.h b/Firmware/Configuration.h new file mode 100644 index 0000000..589d428 --- /dev/null +++ b/Firmware/Configuration.h @@ -0,0 +1,556 @@ +#ifndef CONFIGURATION_H +#define CONFIGURATION_H + +#include "boards.h" + +#define STR_HELPER(x) #x +#define STR(x) STR_HELPER(x) + +//-// +#include +extern const uint16_t _nPrinterType; +extern const char _sPrinterName[] PROGMEM; +extern const uint16_t _nPrinterMmuType; +extern const char _sPrinterMmuName[] PROGMEM; +extern uint16_t nPrinterType; +extern PGM_P sPrinterName; + +// Firmware version +#define FW_VERSION "3.8.1" +#define FW_COMMIT_NR 2869 +// FW_VERSION_UNKNOWN means this is an unofficial build. +// The firmware should only be checked into github with this symbol. +#define FW_DEV_VERSION FW_VERSION_UNKNOWN +#define FW_REPOSITORY "Unknown" +#define FW_VERSION_FULL FW_VERSION "-" STR(FW_COMMIT_NR) + +// G-code language level +#define GCODE_LEVEL 1 + +// Debug version has debugging enabled (the symbol DEBUG_BUILD is set). +// The debug build may be a bit slower than the non-debug build, therefore the debug build should +// not be shipped to a customer. +#define FW_VERSION_DEBUG 6 +// This is a development build. A development build is either built from an unofficial git repository, +// or from an unofficial branch, or it does not have a label set. Only the build server should set this build type. +#define FW_VERSION_DEVEL 5 +// This is an alpha release. Only the build server should set this build type. +#define FW_VERSION_ALPHA 4 +// This is a beta release. Only the build server should set this build type. +#define FW_VERSION_BETA 3 +// This is a release candidate build. Only the build server should set this build type. +#define FW_VERSION_RC 2 +// This is a final release. Only the build server should set this build type. +#define FW_VERSION_GOLD 1 +// This is an unofficial build. The firmware should only be checked into github with this symbol, +// the build server shall never produce builds with this build type. +#define FW_VERSION_UNKNOWN 0 + +#if FW_DEV_VERSION == FW_VERSION_DEBUG +#define DEBUG_BUILD +#else +#undef DEBUG_BUILD +#endif + +#include "Configuration_prusa.h" + +#define FW_PRUSA3D_MAGIC "PRUSA3DFW" +#define FW_PRUSA3D_MAGIC_LEN 10 + +#include "eeprom.h" + +// This configuration file contains the basic settings. +// Advanced settings can be found in Configuration_adv.h +// BASIC SETTINGS: select your board type, temperature sensor type, axis scaling, and endstop configuration + +// User-specified version info of this build to display in [Pronterface, etc] terminal window during +// startup. Implementation of an idea by Prof Braino to inform user that any changes made to this +// build by the user have been successfully uploaded into firmware. + +//#define STRING_VERSION "1.0.2" + +#define STRING_VERSION_CONFIG_H __DATE__ " " __TIME__ // build date and time +#define STRING_CONFIG_H_AUTHOR "(none, default config)" // Who made the changes. + +// SERIAL_PORT selects which serial port should be used for communication with the host. +// This allows the connection of wireless adapters (for instance) to non-default port pins. +// Serial port 0 is still used by the Arduino bootloader regardless of this setting. +#define SERIAL_PORT 0 + +// This determines the communication speed of the printer +#define BAUDRATE 115200 + +// This enables the serial port associated to the Bluetooth interface +//#define BTENABLED // Enable BT interface on AT90USB devices + +// The following define selects which electronics board you have. +// Please choose the name from boards.h that matches your setup + + + + + + + +// Define this to set a unique identifier for this printer, (Used by some programs to differentiate between machines) +// You can use an online service to generate a random UUID. (eg http://www.uuidgenerator.net/version4) +// #define MACHINE_UUID "00000000-0000-0000-0000-000000000000" + +// This defines the number of extruders +#define EXTRUDERS 1 + +//// The following define selects which power supply you have. Please choose the one that matches your setup +// 1 = ATX +// 2 = X-Box 360 203Watts (the blue wire connected to PS_ON and the red wire to VCC) + +#define POWER_SUPPLY 1 + + + + + +// Define this to have the electronics keep the power supply off on startup. If you don't know what this is leave it. +// #define PS_DEFAULT_OFF + + +// Actual temperature must be close to target for this long before M109 returns success +#define TEMP_RESIDENCY_TIME 3 // (seconds) +#define TEMP_HYSTERESIS 5 // (degC) range of +/- temperatures considered "close" to the target one +#define TEMP_WINDOW 1 // (degC) Window around target to start the residency timer x degC early. + + + +// If your bed has low resistance e.g. .6 ohm and throws the fuse you can duty cycle it to reduce the +// average current. The value should be an integer and the heat bed will be turned on for 1 interval of +// HEATER_BED_DUTY_CYCLE_DIVIDER intervals. +//#define HEATER_BED_DUTY_CYCLE_DIVIDER 4 + +// If you want the M105 heater power reported in watts, define the BED_WATTS, and (shared for all extruders) EXTRUDER_WATTS +//#define EXTRUDER_WATTS (12.0*12.0/6.7) // P=I^2/R +//#define BED_WATTS (12.0*12.0/1.1) // P=I^2/R + +// PID settings: +// Comment the following line to disable PID and enable bang-bang. +#define PIDTEMP +#define BANG_MAX 255 // limits current to nozzle while in bang-bang mode; 255=full current +#define PID_MAX BANG_MAX // limits current to nozzle while PID is active; 255=full current +#ifdef PIDTEMP + //#define PID_DEBUG // Sends debug data to the serial port. + //#define PID_OPENLOOP 1 // Puts PID in open loop. M104/M140 sets the output power from 0 to PID_MAX + //#define SLOW_PWM_HEATERS // PWM with very low frequency (roughly 0.125Hz=8s) and minimum state time of approximately 1s useful for heaters driven by a relay + #define PID_INTEGRAL_DRIVE_MAX PID_MAX //limit for the integral term + #define PID_K1 0.95 //smoothing factor within the PID + #define PID_dT ((OVERSAMPLENR * 10.0)/(F_CPU / 64.0 / 256.0)) //sampling period of the temperature routine + +// If you are using a pre-configured hotend then you can use one of the value sets by uncommenting it +// Ultimaker + + +// MakerGear +// #define DEFAULT_Kp 7.0 +// #define DEFAULT_Ki 0.1 +// #define DEFAULT_Kd 12 + +// Mendel Parts V9 on 12V +// #define DEFAULT_Kp 63.0 +// #define DEFAULT_Ki 2.25 +// #define DEFAULT_Kd 440 +#endif // PIDTEMP + + +//this prevents dangerous Extruder moves, i.e. if the temperature is under the limit +//can be software-disabled for whatever purposes by +#define PREVENT_DANGEROUS_EXTRUDE +//if PREVENT_DANGEROUS_EXTRUDE is on, you can still disable (uncomment) very long bits of extrusion separately. +#define PREVENT_LENGTHY_EXTRUDE + +#ifdef DEBUG_DISABLE_PREVENT_EXTRUDER +#undef PREVENT_DANGEROUS_EXTRUDE +#undef PREVENT_LENGTHY_EXTRUDE +#endif //DEBUG_DISABLE_PREVENT_EXTRUDER + +#define EXTRUDE_MAXLENGTH (X_MAX_LENGTH+Y_MAX_LENGTH) //prevent extrusion of very large distances. + +/*================== Thermal Runaway Protection ============================== +This is a feature to protect your printer from burn up in flames if it has +a thermistor coming off place (this happened to a friend of mine recently and +motivated me writing this feature). + +The issue: If a thermistor come off, it will read a lower temperature than actual. +The system will turn the heater on forever, burning up the filament and anything +else around. + +After the temperature reaches the target for the first time, this feature will +start measuring for how long the current temperature stays below the target +minus _HYSTERESIS (set_temperature - THERMAL_RUNAWAY_PROTECTION_HYSTERESIS). + +If it stays longer than _PERIOD, it means the thermistor temperature +cannot catch up with the target, so something *may be* wrong. Then, to be on the +safe side, the system will he halt. + +Bear in mind the count down will just start AFTER the first time the +thermistor temperature is over the target, so you will have no problem if +your extruder heater takes 2 minutes to hit the target on heating. + +*/ +// If you want to enable this feature for all your extruder heaters, +// uncomment the 2 defines below: + +// Parameters for all extruder heaters +//#define THERMAL_RUNAWAY_PROTECTION_PERIOD 40 //in seconds +//#define THERMAL_RUNAWAY_PROTECTION_HYSTERESIS 4 // in degree Celsius + +// If you want to enable this feature for your bed heater, +// uncomment the 2 defines below: + +// Parameters for the bed heater +//#define THERMAL_RUNAWAY_PROTECTION_BED_PERIOD 20 //in seconds +//#define THERMAL_RUNAWAY_PROTECTION_BED_HYSTERESIS 2 // in degree Celsius +//=========================================================================== + + +//=========================================================================== +//=============================Mechanical Settings=========================== +//=========================================================================== + +// Uncomment the following line to enable CoreXY kinematics +// #define COREXY + +// coarse Endstop Settings +#define ENDSTOPPULLUPS // Comment this out (using // at the start of the line) to disable the endstop pullup resistors + +#ifndef ENDSTOPPULLUPS + // fine endstop settings: Individual pullups. will be ignored if ENDSTOPPULLUPS is defined + // #define ENDSTOPPULLUP_XMAX + // #define ENDSTOPPULLUP_YMAX + // #define ENDSTOPPULLUP_ZMAX + // #define ENDSTOPPULLUP_XMIN + // #define ENDSTOPPULLUP_YMIN + // #define ENDSTOPPULLUP_ZMIN +#endif + +#ifdef ENDSTOPPULLUPS + #define ENDSTOPPULLUP_XMAX + #define ENDSTOPPULLUP_YMAX + #define ENDSTOPPULLUP_ZMAX + #define ENDSTOPPULLUP_XMIN + #define ENDSTOPPULLUP_YMIN + #define ENDSTOPPULLUP_ZMIN +#endif + +// The pullups are needed if you directly connect a mechanical endswitch between the signal and ground pins. + +#define X_MAX_ENDSTOP_INVERTING 0 // set to 1 to invert the logic of the endstop. +#define Y_MAX_ENDSTOP_INVERTING 0 // set to 1 to invert the logic of the endstop. +#define Z_MAX_ENDSTOP_INVERTING 1 // set to 1 to invert the logic of the endstop. +//#define DISABLE_MAX_ENDSTOPS +//#define DISABLE_MIN_ENDSTOPS + +// Disable max endstops for compatibility with endstop checking routine +#if defined(COREXY) && !defined(DISABLE_MAX_ENDSTOPS) + #define DISABLE_MAX_ENDSTOPS +#endif + +// For Inverting Stepper Enable Pins (Active Low) use 0, Non Inverting (Active High) use 1 +#define X_ENABLE_ON 0 +#define Y_ENABLE_ON 0 +#define Z_ENABLE_ON 0 +#define E_ENABLE_ON 0 // For all extruders + +// Disables axis when it's not being used. +#define DISABLE_X 0 +#define DISABLE_Y 0 +#define DISABLE_Z 0 +#define DISABLE_E 0// For all extruders +#define DISABLE_INACTIVE_EXTRUDER 1 //disable only inactive extruders and keep active extruder enabled + + +// ENDSTOP SETTINGS: +// Sets direction of endstops when homing; 1=MAX, -1=MIN +#define X_HOME_DIR -1 +#define Y_HOME_DIR -1 +#define Z_HOME_DIR -1 + +#ifdef DEBUG_DISABLE_SWLIMITS +#define min_software_endstops 0 +#define max_software_endstops 0 +#else +#define min_software_endstops 1 // If true, axis won't move to coordinates less than HOME_POS. +#define max_software_endstops 1 // If true, axis won't move to coordinates greater than the defined lengths below. +#endif //DEBUG_DISABLE_SWLIMITS + + +#define X_MAX_LENGTH (X_MAX_POS - X_MIN_POS) +#define Y_MAX_LENGTH (Y_MAX_POS - Y_MIN_POS) +#define Z_MAX_LENGTH (Z_MAX_POS - Z_MIN_POS) + +#define Z_HEIGHT_HIDE_LIVE_ADJUST_MENU 2.0f + +#define HOME_Z_SEARCH_THRESHOLD 0.15f // Threshold of the Z height in calibration + +//============================= Bed Auto Leveling =========================== + +//#define ENABLE_AUTO_BED_LEVELING // Delete the comment to enable (remove // at the start of the line) +#define Z_PROBE_REPEATABILITY_TEST // If not commented out, Z-Probe Repeatability test will be included if Auto Bed Leveling is Enabled. + +#ifdef ENABLE_AUTO_BED_LEVELING + +// There are 2 different ways to pick the X and Y locations to probe: + +// - "grid" mode +// Probe every point in a rectangular grid +// You must specify the rectangle, and the density of sample points +// This mode is preferred because there are more measurements. +// It used to be called ACCURATE_BED_LEVELING but "grid" is more descriptive + +// - "3-point" mode +// Probe 3 arbitrary points on the bed (that aren't colinear) +// You must specify the X & Y coordinates of all 3 points + + #define AUTO_BED_LEVELING_GRID + // with AUTO_BED_LEVELING_GRID, the bed is sampled in a + // AUTO_BED_LEVELING_GRID_POINTSxAUTO_BED_LEVELING_GRID_POINTS grid + // and least squares solution is calculated + // Note: this feature occupies 10'206 byte + #ifdef AUTO_BED_LEVELING_GRID + + // set the rectangle in which to probe + #define LEFT_PROBE_BED_POSITION 15 + #define RIGHT_PROBE_BED_POSITION 170 + #define BACK_PROBE_BED_POSITION 180 + #define FRONT_PROBE_BED_POSITION 20 + + // set the number of grid points per dimension + // I wouldn't see a reason to go above 3 (=9 probing points on the bed) + #define AUTO_BED_LEVELING_GRID_POINTS 2 + + + #else // not AUTO_BED_LEVELING_GRID + // with no grid, just probe 3 arbitrary points. A simple cross-product + // is used to esimate the plane of the print bed + + #define ABL_PROBE_PT_1_X 15 + #define ABL_PROBE_PT_1_Y 180 + #define ABL_PROBE_PT_2_X 15 + #define ABL_PROBE_PT_2_Y 20 + #define ABL_PROBE_PT_3_X 170 + #define ABL_PROBE_PT_3_Y 20 + + #endif // AUTO_BED_LEVELING_GRID + + + // these are the offsets to the probe relative to the extruder tip (Hotend - Probe) + // X and Y offsets must be integers + #define X_PROBE_OFFSET_FROM_EXTRUDER -25 + #define Y_PROBE_OFFSET_FROM_EXTRUDER -29 + #define Z_PROBE_OFFSET_FROM_EXTRUDER -12.35 + + #define Z_RAISE_BEFORE_HOMING 4 // (in mm) Raise Z before homing (G28) for Probe Clearance. + // Be sure you have this distance over your Z_MAX_POS in case + + #define XY_TRAVEL_SPEED 8000 // X and Y axis travel speed between probes, in mm/min + + #define Z_RAISE_BEFORE_PROBING 15 //How much the extruder will be raised before traveling to the first probing point. + #define Z_RAISE_BETWEEN_PROBINGS 5 //How much the extruder will be raised when traveling from between next probing points + + //#define Z_PROBE_SLED // turn on if you have a z-probe mounted on a sled like those designed by Charles Bell + //#define SLED_DOCKING_OFFSET 5 // the extra distance the X axis must travel to pickup the sled. 0 should be fine but you can push it further if you'd like. + + //If defined, the Probe servo will be turned on only during movement and then turned off to avoid jerk + //The value is the delay to turn the servo off after powered on - depends on the servo speed; 300ms is good value, but you can try lower it. + // You MUST HAVE the SERVO_ENDSTOPS defined to use here a value higher than zero otherwise your code will not compile. + +// #define PROBE_SERVO_DEACTIVATION_DELAY 300 + + +//If you have enabled the Bed Auto Leveling and are using the same Z Probe for Z Homing, +//it is highly recommended you let this Z_SAFE_HOMING enabled! + + //#define Z_SAFE_HOMING // This feature is meant to avoid Z homing with probe outside the bed area. + // When defined, it will: + // - Allow Z homing only after X and Y homing AND stepper drivers still enabled + // - If stepper drivers timeout, it will need X and Y homing again before Z homing + // - Position the probe in a defined XY point before Z Homing when homing all axis (G28) + // - Block Z homing only when the probe is outside bed area. + + #ifdef Z_SAFE_HOMING + + #define Z_SAFE_HOMING_X_POINT (X_MAX_LENGTH/2) // X point for Z homing when homing all axis (G28) + #define Z_SAFE_HOMING_Y_POINT (Y_MAX_LENGTH/2) // Y point for Z homing when homing all axis (G28) + + #endif + + #ifdef AUTO_BED_LEVELING_GRID // Check if Probe_Offset * Grid Points is greater than Probing Range + #if X_PROBE_OFFSET_FROM_EXTRUDER < 0 + #if (-(X_PROBE_OFFSET_FROM_EXTRUDER * AUTO_BED_LEVELING_GRID_POINTS) >= (RIGHT_PROBE_BED_POSITION - LEFT_PROBE_BED_POSITION)) + #error "The X axis probing range is not enough to fit all the points defined in AUTO_BED_LEVELING_GRID_POINTS" + #endif + #else + #if ((X_PROBE_OFFSET_FROM_EXTRUDER * AUTO_BED_LEVELING_GRID_POINTS) >= (RIGHT_PROBE_BED_POSITION - LEFT_PROBE_BED_POSITION)) + #error "The X axis probing range is not enough to fit all the points defined in AUTO_BED_LEVELING_GRID_POINTS" + #endif + #endif + #if Y_PROBE_OFFSET_FROM_EXTRUDER < 0 + #if (-(Y_PROBE_OFFSET_FROM_EXTRUDER * AUTO_BED_LEVELING_GRID_POINTS) >= (BACK_PROBE_BED_POSITION - FRONT_PROBE_BED_POSITION)) + #error "The Y axis probing range is not enough to fit all the points defined in AUTO_BED_LEVELING_GRID_POINTS" + #endif + #else + #if ((Y_PROBE_OFFSET_FROM_EXTRUDER * AUTO_BED_LEVELING_GRID_POINTS) >= (BACK_PROBE_BED_POSITION - FRONT_PROBE_BED_POSITION)) + #error "The Y axis probing range is not enough to fit all the points defined in AUTO_BED_LEVELING_GRID_POINTS" + #endif + #endif + + + #endif + +#endif // ENABLE_AUTO_BED_LEVELING + + +// The position of the homing switches +//#define MANUAL_HOME_POSITIONS // If defined, MANUAL_*_HOME_POS below will be used +//#define BED_CENTER_AT_0_0 // If defined, the center of the bed is at (X=0, Y=0) + +//Manual homing switch locations: +// For deltabots this means top and center of the Cartesian print volume. + + +// Offset of the extruders (uncomment if using more than one and relying on firmware to position when changing). +// The offset has to be X=0, Y=0 for the extruder 0 hotend (default extruder). +// For the other hotends it is their distance from the extruder 0 hotend. +// #define EXTRUDER_OFFSET_X {0.0, 20.00} // (in mm) for each extruder, offset of the hotend on the X axis +// #define EXTRUDER_OFFSET_Y {0.0, 5.00} // (in mm) for each extruder, offset of the hotend on the Y axis + +// The speed change that does not require acceleration (i.e. the software might assume it can be done instantaneously) +#define DEFAULT_XJERK 10 // (mm/sec) +#define DEFAULT_YJERK 10 // (mm/sec) +#define DEFAULT_ZJERK 0.4 // (mm/sec) +#define DEFAULT_EJERK 2.5 // (mm/sec) + +//=========================================================================== +//=============================Additional Features=========================== +//=========================================================================== + +// Custom M code points +#define CUSTOM_M_CODES +#ifdef CUSTOM_M_CODES + #define CUSTOM_M_CODE_SET_Z_PROBE_OFFSET 851 + #define Z_PROBE_OFFSET_RANGE_MIN -15 + #define Z_PROBE_OFFSET_RANGE_MAX -5 +#endif + + +// EEPROM +// The microcontroller can store settings in the EEPROM, e.g. max velocity... +// M500 - stores parameters in EEPROM +// M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily). +// M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to. +//define this to enable EEPROM support +//#define EEPROM_SETTINGS +//to disable EEPROM Serial responses and decrease program space by ~1700 byte: comment this out: +// please keep turned on if you can. +//#define EEPROM_CHITCHAT + +// Host Keepalive +// +// When enabled Marlin will send a busy status message to the host +// every couple of seconds when it can't accept commands. +// +#ifndef HEATBED_ANALYSIS +#define HOST_KEEPALIVE_FEATURE // Disable this if your host doesn't like keepalive messages +#endif //HEATBED_ANALYSIS +#define HOST_KEEPALIVE_INTERVAL 2 // Number of seconds between "busy" messages. Set with M113. + +//LCD and SD support +#define SDSUPPORT // Enable SD Card Support in Hardware Console +//#define SDSLOW // Use slower SD transfer mode (not normally needed - uncomment if you're getting volume init error) +#define SD_CHECK_AND_RETRY // Use CRC checks and retries on the SD communication +#define ENCODER_PULSES_PER_STEP 4 // Increase if you have a high resolution encoder +//#define ENCODER_STEPS_PER_MENU_ITEM 1 // Set according to ENCODER_PULSES_PER_STEP or your liking + +// The RepRapDiscount Smart Controller (white PCB) +// http://reprap.org/wiki/RepRapDiscount_Smart_Controller +#define REPRAP_DISCOUNT_SMART_CONTROLLER +#define SDSUPPORT +#define LCD_WIDTH 20 +#define LCD_HEIGHT 4 + + +// Increase the FAN pwm frequency. Removes the PWM noise but increases heating in the FET/Arduino +//#define FAST_PWM_FAN + +// Temperature status LEDs that display the hotend and bet temperature. +// If all hotends and bed temperature and temperature setpoint are < 54C then the BLUE led is on. +// Otherwise the RED led is on. There is 1C hysteresis. +//#define TEMP_STAT_LEDS + +// Use software PWM to drive the fan, as for the heaters. This uses a very low frequency +// which is not ass annoying as with the hardware PWM. On the other hand, if this frequency +// is too low, you should also increment SOFT_PWM_SCALE. +#define FAN_SOFT_PWM +#define FAN_SOFT_PWM_BITS 4 //PWM bit resolution = 4bits, freq = 62.5Hz + +// Bed soft pwm +#define HEATER_BED_SOFT_PWM_BITS 5 //PWM bit resolution = 5bits, freq = 31.25Hz + +// Incrementing this by 1 will double the software PWM frequency, +// affecting heaters, and the fan if FAN_SOFT_PWM is enabled. +// However, control resolution will be halved for each increment; +// at zero value, there are 128 effective control positions. +#define SOFT_PWM_SCALE 0 + +// M240 Triggers a camera by emulating a Canon RC-1 Remote +// Data from: http://www.doc-diy.net/photo/rc-1_hacked/ +// #define PHOTOGRAPH_PIN 23 + +// SF send wrong arc g-codes when using Arc Point as fillet procedure +//#define SF_ARC_FIX + +//define BlinkM/CyzRgb Support +//#define BLINKM + +/*********************************************************************\ +* R/C SERVO support +* Sponsored by TrinityLabs, Reworked by codexmas +**********************************************************************/ + +// Number of servos +// +// If you select a configuration below, this will receive a default value and does not need to be set manually +// set it manually if you have more servos than extruders and wish to manually control some +// leaving it undefined or defining as 0 will disable the servo subsystem +// If unsure, leave commented / disabled +// +//#define NUM_SERVOS 3 // Servo index starts with 0 for M280 command + +#define DEFAULT_NOMINAL_FILAMENT_DIA 1.75 //Enter the diameter (in mm) of the filament generally used (3.0 mm or 1.75 mm). Used by the volumetric extrusion. + +// Calibration status of the machine, to be stored into the EEPROM, +// (unsigned char*)EEPROM_CALIBRATION_STATUS +enum CalibrationStatus +{ + // Freshly assembled, needs to peform a self-test and the XYZ calibration. + CALIBRATION_STATUS_ASSEMBLED = 255, + + // For the wizard: self test has been performed, now the XYZ calibration is needed. + CALIBRATION_STATUS_XYZ_CALIBRATION = 250, + + // For the wizard: factory assembled, needs to run Z calibration. + CALIBRATION_STATUS_Z_CALIBRATION = 240, + + // The XYZ calibration has been performed, now it remains to run the V2Calibration.gcode. + CALIBRATION_STATUS_LIVE_ADJUST = 230, + + // Calibrated, ready to print. + CALIBRATION_STATUS_CALIBRATED = 1, + + // Legacy: resetted by issuing a G86 G-code. + // This value can only be expected after an upgrade from the initial MK2 firmware releases. + // Currently the G86 sets the calibration status to + CALIBRATION_STATUS_UNKNOWN = 0, +}; + +#include "Configuration_adv.h" +#include "thermistortables.h" + + +#endif //__CONFIGURATION_H diff --git a/Firmware/ConfigurationStore.cpp b/Firmware/ConfigurationStore.cpp new file mode 100644 index 0000000..f2df8fc --- /dev/null +++ b/Firmware/ConfigurationStore.cpp @@ -0,0 +1,341 @@ +//! @file + +#include "Marlin.h" +#include "planner.h" +#include "temperature.h" +#include "ultralcd.h" +#include "ConfigurationStore.h" +#include "Configuration_prusa.h" + +#ifdef MESH_BED_LEVELING +#include "mesh_bed_leveling.h" +#endif + +#ifdef TMC2130 +#include "tmc2130.h" +#endif + + +M500_conf cs; + +//! @brief Write data to EEPROM +//! @param pos destination in EEPROM, 0 is start +//! @param value value to be written +//! @param size size of type pointed by value +//! @param name name of variable written, used only for debug input if DEBUG_EEPROM_WRITE defined +//! @retval true success +//! @retval false failed +#ifdef DEBUG_EEPROM_WRITE +static bool EEPROM_writeData(uint8_t* pos, uint8_t* value, uint8_t size, const char* name) +#else //DEBUG_EEPROM_WRITE +static bool EEPROM_writeData(uint8_t* pos, uint8_t* value, uint8_t size, const char*) +#endif //DEBUG_EEPROM_WRITE +{ +#ifdef DEBUG_EEPROM_WRITE + printf_P(PSTR("EEPROM_WRITE_VAR addr=0x%04x size=0x%02hhx name=%s\n"), pos, size, name); +#endif //DEBUG_EEPROM_WRITE + while (size--) + { + + eeprom_update_byte(pos, *value); + if (eeprom_read_byte(pos) != *value) { + SERIAL_ECHOLNPGM("EEPROM Error"); + return false; + } + + pos++; + value++; + } + return true; +} + +#ifdef DEBUG_EEPROM_READ +static void EEPROM_readData(uint8_t* pos, uint8_t* value, uint8_t size, const char* name) +#else //DEBUG_EEPROM_READ +static void EEPROM_readData(uint8_t* pos, uint8_t* value, uint8_t size, const char*) +#endif //DEBUG_EEPROM_READ +{ +#ifdef DEBUG_EEPROM_READ + printf_P(PSTR("EEPROM_READ_VAR addr=0x%04x size=0x%02hhx name=%s\n"), pos, size, name); +#endif //DEBUG_EEPROM_READ + while(size--) + { + *value = eeprom_read_byte(pos); + pos++; + value++; + } +} + +#define EEPROM_VERSION "V2" + +#ifdef EEPROM_SETTINGS +void Config_StoreSettings() +{ + strcpy(cs.version,"000"); //!< invalidate data first @TODO use erase to save one erase cycle + + if (EEPROM_writeData(reinterpret_cast(EEPROM_M500_base),reinterpret_cast(&cs),sizeof(cs),0), "cs, invalid version") + { + strcpy(cs.version,EEPROM_VERSION); //!< validate data if write succeed + EEPROM_writeData(reinterpret_cast(EEPROM_M500_base->version), reinterpret_cast(cs.version), sizeof(cs.version), "cs.version valid"); + } + + SERIAL_ECHO_START; + SERIAL_ECHOLNPGM("Settings Stored"); +} +#endif //EEPROM_SETTINGS + + +#ifndef DISABLE_M503 +void Config_PrintSettings(uint8_t level) +{ // Always have this function, even with EEPROM_SETTINGS disabled, the current values will be shown +#ifdef TMC2130 + printf_P(PSTR( + "%SSteps per unit:\n%S M92 X%.2f Y%.2f Z%.2f E%.2f\n" + "%SUStep resolution: \n%S M350 X%d Y%d Z%d E%d\n" + "%SMaximum feedrates - normal (mm/s):\n%S M203 X%.2f Y%.2f Z%.2f E%.2f\n" + "%SMaximum feedrates - stealth (mm/s):\n%S M203 X%.2f Y%.2f Z%.2f E%.2f\n" + "%SMaximum acceleration - normal (mm/s2):\n%S M201 X%lu Y%lu Z%lu E%lu\n" + "%SMaximum acceleration - stealth (mm/s2):\n%S M201 X%lu Y%lu Z%lu E%lu\n" + "%SAcceleration: S=acceleration, T=retract acceleration\n%S M204 S%.2f T%.2f\n" + "%SAdvanced variables: S=Min feedrate (mm/s), T=Min travel feedrate (mm/s), B=minimum segment time (ms), X=maximum XY jerk (mm/s), Z=maximum Z jerk (mm/s), E=maximum E jerk (mm/s)\n%S M205 S%.2f T%.2f B%.2f X%.2f Y%.2f Z%.2f E%.2f\n" + "%SHome offset (mm):\n%S M206 X%.2f Y%.2f Z%.2f\n" + ), + echomagic, echomagic, cs.axis_steps_per_unit[X_AXIS], cs.axis_steps_per_unit[Y_AXIS], cs.axis_steps_per_unit[Z_AXIS], cs.axis_steps_per_unit[E_AXIS], + echomagic, echomagic, cs.axis_ustep_resolution[X_AXIS], cs.axis_ustep_resolution[Y_AXIS], cs.axis_ustep_resolution[Z_AXIS], cs.axis_ustep_resolution[E_AXIS], + echomagic, echomagic, cs.max_feedrate_normal[X_AXIS], cs.max_feedrate_normal[Y_AXIS], cs.max_feedrate_normal[Z_AXIS], cs.max_feedrate_normal[E_AXIS], + echomagic, echomagic, cs.max_feedrate_silent[X_AXIS], cs.max_feedrate_silent[Y_AXIS], cs.max_feedrate_silent[Z_AXIS], cs.max_feedrate_silent[E_AXIS], + echomagic, echomagic, cs.max_acceleration_units_per_sq_second_normal[X_AXIS], cs.max_acceleration_units_per_sq_second_normal[Y_AXIS], cs.max_acceleration_units_per_sq_second_normal[Z_AXIS], cs.max_acceleration_units_per_sq_second_normal[E_AXIS], + echomagic, echomagic, cs.max_acceleration_units_per_sq_second_silent[X_AXIS], cs.max_acceleration_units_per_sq_second_silent[Y_AXIS], cs.max_acceleration_units_per_sq_second_silent[Z_AXIS], cs.max_acceleration_units_per_sq_second_silent[E_AXIS], + echomagic, echomagic, cs.acceleration, cs.retract_acceleration, + echomagic, echomagic, cs.minimumfeedrate, cs.mintravelfeedrate, cs.minsegmenttime, cs.max_jerk[X_AXIS], cs.max_jerk[Y_AXIS], cs.max_jerk[Z_AXIS], cs.max_jerk[E_AXIS], + echomagic, echomagic, cs.add_homing[X_AXIS], cs.add_homing[Y_AXIS], cs.add_homing[Z_AXIS] +#else //TMC2130 + printf_P(PSTR( + "%SSteps per unit:\n%S M92 X%.2f Y%.2f Z%.2f E%.2f\n" + "%SMaximum feedrates (mm/s):\n%S M203 X%.2f Y%.2f Z%.2f E%.2f\n" + "%SMaximum acceleration (mm/s2):\n%S M201 X%lu Y%lu Z%lu E%lu\n" + "%SAcceleration: S=acceleration, T=retract acceleration\n%S M204 S%.2f T%.2f\n" + "%SAdvanced variables: S=Min feedrate (mm/s), T=Min travel feedrate (mm/s), B=minimum segment time (ms), X=maximum XY jerk (mm/s), Z=maximum Z jerk (mm/s), E=maximum E jerk (mm/s)\n%S M205 S%.2f T%.2f B%.2f X%.2f Y%.2f Z%.2f E%.2f\n" + "%SHome offset (mm):\n%S M206 X%.2f Y%.2f Z%.2f\n" + ), + echomagic, echomagic, cs.axis_steps_per_unit[X_AXIS], cs.axis_steps_per_unit[Y_AXIS], cs.axis_steps_per_unit[Z_AXIS], cs.axis_steps_per_unit[E_AXIS], + echomagic, echomagic, max_feedrate[X_AXIS], max_feedrate[Y_AXIS], max_feedrate[Z_AXIS], max_feedrate[E_AXIS], + echomagic, echomagic, max_acceleration_units_per_sq_second[X_AXIS], max_acceleration_units_per_sq_second[Y_AXIS], max_acceleration_units_per_sq_second[Z_AXIS], max_acceleration_units_per_sq_second[E_AXIS], + echomagic, echomagic, cs.acceleration, cs.retract_acceleration, + echomagic, echomagic, cs.minimumfeedrate, cs.mintravelfeedrate, cs.minsegmenttime, cs.max_jerk[X_AXIS], cs.max_jerk[Y_AXIS], cs.max_jerk[Z_AXIS], cs.max_jerk[E_AXIS], + echomagic, echomagic, cs.add_homing[X_AXIS], cs.add_homing[Y_AXIS], cs.add_homing[Z_AXIS] +#endif //TMC2130 + ); +#ifdef PIDTEMP + printf_P(PSTR("%SPID settings:\n%S M301 P%.2f I%.2f D%.2f\n"), + echomagic, echomagic, cs.Kp, unscalePID_i(cs.Ki), unscalePID_d(cs.Kd)); +#endif +#ifdef PIDTEMPBED + printf_P(PSTR("%SPID heatbed settings:\n%S M304 P%.2f I%.2f D%.2f\n"), + echomagic, echomagic, cs.bedKp, unscalePID_i(cs.bedKi), unscalePID_d(cs.bedKd)); +#endif +#ifdef FWRETRACT + printf_P(PSTR( + "%SRetract: S=Length (mm) F:Speed (mm/m) Z: ZLift (mm)\n%S M207 S%.2f F%.2f Z%.2f\n" + "%SRecover: S=Extra length (mm) F:Speed (mm/m)\n%S M208 S%.2f F%.2f\n" + "%SAuto-Retract: S=0 to disable, 1 to interpret extrude-only moves as retracts or recoveries\n%S M209 S%d\n" + ), + echomagic, echomagic, cs.retract_length, cs.retract_feedrate*60, cs.retract_zlift, + echomagic, echomagic, cs.retract_recover_length, cs.retract_recover_feedrate*60, + echomagic, echomagic, (cs.autoretract_enabled ? 1 : 0) + ); +#if EXTRUDERS > 1 + printf_P(PSTR("%SMulti-extruder settings:\n%S Swap retract length (mm): %.2f\n%S Swap rec. addl. length (mm): %.2f\n"), + echomagic, echomagic, retract_length_swap, echomagic, retract_recover_length_swap); +#endif + if (cs.volumetric_enabled) { + printf_P(PSTR("%SFilament settings:\n%S M200 D%.2f\n"), + echomagic, echomagic, cs.filament_size[0]); +#if EXTRUDERS > 1 + printf_P(PSTR("%S M200 T1 D%.2f\n"), + echomagic, echomagic, cs.filament_size[1]); +#if EXTRUDERS > 2 + printf_P(PSTR("%S M200 T1 D%.2f\n"), + echomagic, echomagic, cs.filament_size[2]); +#endif +#endif + } else { + printf_P(PSTR("%SFilament settings: Disabled\n"), echomagic); + } +#endif + if (level >= 10) { +#ifdef LIN_ADVANCE + printf_P(PSTR("%SLinear advance settings:\n M900 K%.2f E/D = %.2f\n"), + echomagic, extruder_advance_k, advance_ed_ratio); +#endif //LIN_ADVANCE + } +} +#endif + + +#ifdef EEPROM_SETTINGS + +static_assert (EXTRUDERS == 1, "ConfigurationStore M500_conf not implemented for more extruders, fix filament_size array size."); +static_assert (NUM_AXIS == 4, "ConfigurationStore M500_conf not implemented for more axis." + "Fix axis_steps_per_unit max_feedrate_normal max_acceleration_units_per_sq_second_normal max_jerk max_feedrate_silent" + " max_acceleration_units_per_sq_second_silent array size."); +#ifdef ENABLE_AUTO_BED_LEVELING +static_assert (false, "zprobe_zoffset was not initialized in printers in field to -(Z_PROBE_OFFSET_FROM_EXTRUDER), so it contains" + "0.0, if this is not acceptable, increment EEPROM_VERSION to force use default_conf"); +#endif + +static_assert (sizeof(M500_conf) == 192, "sizeof(M500_conf) has changed, ensure that EEPROM_VERSION has been incremented, " + "or if you added members in the end of struct, ensure that historically uninitialized values will be initialized." + "If this is caused by change to more then 8bit processor, decide whether make this struct packed to save EEPROM," + "leave as it is to keep fast code, or reorder struct members to pack more tightly."); + +static const M500_conf default_conf PROGMEM = +{ + EEPROM_VERSION, + DEFAULT_AXIS_STEPS_PER_UNIT, + DEFAULT_MAX_FEEDRATE, + DEFAULT_MAX_ACCELERATION, + DEFAULT_ACCELERATION, + DEFAULT_RETRACT_ACCELERATION, + DEFAULT_MINIMUMFEEDRATE, + DEFAULT_MINTRAVELFEEDRATE, + DEFAULT_MINSEGMENTTIME, + {DEFAULT_XJERK, DEFAULT_YJERK, DEFAULT_ZJERK, DEFAULT_EJERK}, + {0,0,0}, + -(Z_PROBE_OFFSET_FROM_EXTRUDER), + DEFAULT_Kp, + DEFAULT_Ki*PID_dT, + DEFAULT_Kd/PID_dT, + DEFAULT_bedKp, + DEFAULT_bedKi*PID_dT, + DEFAULT_bedKd/PID_dT, + 0, + false, + RETRACT_LENGTH, + RETRACT_FEEDRATE, + RETRACT_ZLIFT, + RETRACT_RECOVER_LENGTH, + RETRACT_RECOVER_FEEDRATE, + false, + {DEFAULT_NOMINAL_FILAMENT_DIA, +#if EXTRUDERS > 1 + DEFAULT_NOMINAL_FILAMENT_DIA, +#if EXTRUDERS > 2 + DEFAULT_NOMINAL_FILAMENT_DIA, +#endif +#endif + }, + DEFAULT_MAX_FEEDRATE_SILENT, + DEFAULT_MAX_ACCELERATION_SILENT, +#ifdef TMC2130 + { TMC2130_USTEPS_XY, TMC2130_USTEPS_XY, TMC2130_USTEPS_Z, TMC2130_USTEPS_E }, +#else // TMC2130 + {16,16,16,16}, +#endif +}; + +//! @brief Read M500 configuration +//! @retval true Succeeded. Stored settings retrieved or default settings retrieved in case EEPROM has been erased. +//! @retval false Failed. Default settings has been retrieved, because of older version or corrupted data. +bool Config_RetrieveSettings() +{ + bool previous_settings_retrieved = true; + char ver[4]=EEPROM_VERSION; + EEPROM_readData(reinterpret_cast(EEPROM_M500_base->version), reinterpret_cast(cs.version), sizeof(cs.version), "cs.version"); //read stored version + // SERIAL_ECHOLN("Version: [" << ver << "] Stored version: [" << cs.version << "]"); + if (strncmp(ver,cs.version,3) == 0) // version number match + { + + EEPROM_readData(reinterpret_cast(EEPROM_M500_base), reinterpret_cast(&cs), sizeof(cs), "cs"); + + + if (cs.max_jerk[X_AXIS] > DEFAULT_XJERK) cs.max_jerk[X_AXIS] = DEFAULT_XJERK; + if (cs.max_jerk[Y_AXIS] > DEFAULT_YJERK) cs.max_jerk[Y_AXIS] = DEFAULT_YJERK; + calculate_extruder_multipliers(); + + //if max_feedrate_silent and max_acceleration_units_per_sq_second_silent were never stored to eeprom, use default values: + for (uint8_t i = 0; i < (sizeof(cs.max_feedrate_silent)/sizeof(cs.max_feedrate_silent[0])); ++i) + { + const uint32_t erased = 0xffffffff; + bool initialized = false; + + for(uint8_t j = 0; j < sizeof(float); ++j) + { + if(0xff != reinterpret_cast(&(cs.max_feedrate_silent[i]))[j]) initialized = true; + } + if (!initialized) memcpy_P(&cs.max_feedrate_silent[i],&default_conf.max_feedrate_silent[i], sizeof(cs.max_feedrate_silent[i])); + if (erased == cs.max_acceleration_units_per_sq_second_silent[i]) { + memcpy_P(&cs.max_acceleration_units_per_sq_second_silent[i],&default_conf.max_acceleration_units_per_sq_second_silent[i],sizeof(cs.max_acceleration_units_per_sq_second_silent[i])); + } + } + +#ifdef TMC2130 + for (uint8_t j = X_AXIS; j <= Y_AXIS; j++) + { + if (cs.max_feedrate_normal[j] > NORMAL_MAX_FEEDRATE_XY) + cs.max_feedrate_normal[j] = NORMAL_MAX_FEEDRATE_XY; + if (cs.max_feedrate_silent[j] > SILENT_MAX_FEEDRATE_XY) + cs.max_feedrate_silent[j] = SILENT_MAX_FEEDRATE_XY; + if (cs.max_acceleration_units_per_sq_second_normal[j] > NORMAL_MAX_ACCEL_XY) + cs.max_acceleration_units_per_sq_second_normal[j] = NORMAL_MAX_ACCEL_XY; + if (cs.max_acceleration_units_per_sq_second_silent[j] > SILENT_MAX_ACCEL_XY) + cs.max_acceleration_units_per_sq_second_silent[j] = SILENT_MAX_ACCEL_XY; + } + + if(cs.axis_ustep_resolution[X_AXIS] == 0xff){ cs.axis_ustep_resolution[X_AXIS] = TMC2130_USTEPS_XY; } + if(cs.axis_ustep_resolution[Y_AXIS] == 0xff){ cs.axis_ustep_resolution[Y_AXIS] = TMC2130_USTEPS_XY; } + if(cs.axis_ustep_resolution[Z_AXIS] == 0xff){ cs.axis_ustep_resolution[Z_AXIS] = TMC2130_USTEPS_Z; } + if(cs.axis_ustep_resolution[E_AXIS] == 0xff){ cs.axis_ustep_resolution[E_AXIS] = TMC2130_USTEPS_E; } + + tmc2130_set_res(X_AXIS, cs.axis_ustep_resolution[X_AXIS]); + tmc2130_set_res(Y_AXIS, cs.axis_ustep_resolution[Y_AXIS]); + tmc2130_set_res(Z_AXIS, cs.axis_ustep_resolution[Z_AXIS]); + tmc2130_set_res(E_AXIS, cs.axis_ustep_resolution[E_AXIS]); +#endif //TMC2130 + + reset_acceleration_rates(); + + // Call updatePID (similar to when we have processed M301) + updatePID(); + SERIAL_ECHO_START; + SERIAL_ECHOLNPGM("Stored settings retrieved"); + } + else + { + Config_ResetDefault(); + //Return false to inform user that eeprom version was changed and firmware is using default hardcoded settings now. + //In case that storing to eeprom was not used yet, do not inform user that hardcoded settings are used. + if (eeprom_read_byte(reinterpret_cast(&(EEPROM_M500_base->version[0]))) != 0xFF || + eeprom_read_byte(reinterpret_cast(&(EEPROM_M500_base->version[1]))) != 0xFF || + eeprom_read_byte(reinterpret_cast(&(EEPROM_M500_base->version[2]))) != 0xFF) + { + previous_settings_retrieved = false; + } + } + #ifdef EEPROM_CHITCHAT + Config_PrintSettings(); + #endif + return previous_settings_retrieved; +} +#endif + +void Config_ResetDefault() +{ + memcpy_P(&cs,&default_conf, sizeof(cs)); + + // steps per sq second need to be updated to agree with the units per sq second + reset_acceleration_rates(); + +#ifdef PIDTEMP + updatePID(); +#ifdef PID_ADD_EXTRUSION_RATE + Kc = DEFAULT_Kc; //this is not stored by Config_StoreSettings +#endif//PID_ADD_EXTRUSION_RATE +#endif//PIDTEMP + + calculate_extruder_multipliers(); + +SERIAL_ECHO_START; +SERIAL_ECHOLNPGM("Hardcoded Default Settings Loaded"); + +} diff --git a/Firmware/ConfigurationStore.h b/Firmware/ConfigurationStore.h new file mode 100644 index 0000000..b9dca36 --- /dev/null +++ b/Firmware/ConfigurationStore.h @@ -0,0 +1,65 @@ +#ifndef CONFIG_STORE_H +#define CONFIG_STORE_H +#define EEPROM_SETTINGS + +#include "Configuration.h" +#include +#include + +typedef struct +{ + char version[4]; + float axis_steps_per_unit[4]; + float max_feedrate_normal[4]; + unsigned long max_acceleration_units_per_sq_second_normal[4]; + float acceleration; //!< Normal acceleration mm/s^2 THIS IS THE DEFAULT ACCELERATION for all moves. M204 SXXXX + float retract_acceleration; //!< mm/s^2 filament pull-pack and push-forward while standing still in the other axis M204 TXXXX + float minimumfeedrate; + float mintravelfeedrate; + unsigned long minsegmenttime; + float max_jerk[4]; //!< Jerk is a maximum immediate velocity change. + float add_homing[3]; + float zprobe_zoffset; + float Kp; + float Ki; + float Kd; + float bedKp; + float bedKi; + float bedKd; + int lcd_contrast; //!< unused + bool autoretract_enabled; + float retract_length; + float retract_feedrate; + float retract_zlift; + float retract_recover_length; + float retract_recover_feedrate; + bool volumetric_enabled; + float filament_size[1]; //!< cross-sectional area of filament (in millimeters), typically around 1.75 or 2.85, 0 disables the volumetric calculations for the extruder. + float max_feedrate_silent[4]; //!< max speeds for silent mode + unsigned long max_acceleration_units_per_sq_second_silent[4]; + unsigned char axis_ustep_resolution[4]; +} M500_conf; + +extern M500_conf cs; + +void Config_ResetDefault(); + +#ifndef DISABLE_M503 +void Config_PrintSettings(uint8_t level = 0); +#else +FORCE_INLINE void Config_PrintSettings() {} +#endif + +#ifdef EEPROM_SETTINGS +void Config_StoreSettings(); +bool Config_RetrieveSettings(); +#else +FORCE_INLINE void Config_StoreSettings() {} +FORCE_INLINE void Config_RetrieveSettings() { Config_ResetDefault(); Config_PrintSettings(); } +#endif + +inline uint8_t calibration_status() { return eeprom_read_byte((uint8_t*)EEPROM_CALIBRATION_STATUS); } +inline void calibration_status_store(uint8_t status) { eeprom_update_byte((uint8_t*)EEPROM_CALIBRATION_STATUS, status); } +inline bool calibration_status_pinda() { return eeprom_read_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA); } + +#endif//CONFIG_STORE_H diff --git a/Firmware/Configuration_adv.h b/Firmware/Configuration_adv.h new file mode 100644 index 0000000..dd77c91 --- /dev/null +++ b/Firmware/Configuration_adv.h @@ -0,0 +1,454 @@ +#ifndef CONFIGURATION_ADV_H +#define CONFIGURATION_ADV_H + +//=========================================================================== +//=============================Thermal Settings ============================ +//=========================================================================== + +#ifdef BED_LIMIT_SWITCHING + #define BED_HYSTERESIS 2 //only disable heating if T>target+BED_HYSTERESIS and enable heating if T>target-BED_HYSTERESIS +#endif +#define BED_CHECK_INTERVAL 5000 //ms between checks in bang-bang control + +#ifdef PIDTEMP + // this adds an experimental additional term to the heating power, proportional to the extrusion speed. + // if Kc is chosen well, the additional required power due to increased melting should be compensated. + #define PID_ADD_EXTRUSION_RATE + #ifdef PID_ADD_EXTRUSION_RATE + #define DEFAULT_Kc (1) //heating power=Kc*(e_speed) + #endif +#endif + + +//automatic temperature: The hot end target temperature is calculated by all the buffered lines of gcode. +//The maximum buffered steps/sec of the extruder motor are called "se". +//You enter the autotemp mode by a M109 S B F +// the target temperature is set to mintemp+factor*se[steps/sec] and limited by mintemp and maxtemp +// you exit the value by any M109 without F* +// Also, if the temperature is set to a value 1 && defined HEATERS_PARALLEL + #error "You cannot use HEATERS_PARALLEL if EXTRUDERS > 1" +#endif + +#if TEMP_SENSOR_0 > 0 + #define THERMISTORHEATER_0 TEMP_SENSOR_0 + #define HEATER_0_USES_THERMISTOR +#endif +#if TEMP_SENSOR_1 > 0 + #define THERMISTORHEATER_1 TEMP_SENSOR_1 + #define HEATER_1_USES_THERMISTOR +#endif +#if TEMP_SENSOR_2 > 0 + #define THERMISTORHEATER_2 TEMP_SENSOR_2 + #define HEATER_2_USES_THERMISTOR +#endif +#if TEMP_SENSOR_BED > 0 + #define THERMISTORBED TEMP_SENSOR_BED + #define BED_USES_THERMISTOR +#endif +#if TEMP_SENSOR_PINDA > 0 + #define THERMISTORPINDA TEMP_SENSOR_PINDA +#endif +#if TEMP_SENSOR_AMBIENT > 0 + #define THERMISTORAMBIENT TEMP_SENSOR_AMBIENT +#endif +#if TEMP_SENSOR_0 == -1 + #define HEATER_0_USES_AD595 +#endif +#if TEMP_SENSOR_1 == -1 + #define HEATER_1_USES_AD595 +#endif +#if TEMP_SENSOR_2 == -1 + #define HEATER_2_USES_AD595 +#endif +#if TEMP_SENSOR_BED == -1 + #define BED_USES_AD595 +#endif +#if TEMP_SENSOR_0 == -2 + #define HEATER_0_USES_MAX6675 +#endif +#if TEMP_SENSOR_0 == 0 + #undef HEATER_0_MINTEMP + #undef HEATER_0_MAXTEMP +#endif +#if TEMP_SENSOR_1 == 0 + #undef HEATER_1_MINTEMP + #undef HEATER_1_MAXTEMP +#endif +#if TEMP_SENSOR_2 == 0 + #undef HEATER_2_MINTEMP + #undef HEATER_2_MAXTEMP +#endif +#if TEMP_SENSOR_BED == 0 + #undef BED_MINTEMP + #undef BED_MAXTEMP +#endif + + +#endif //__CONFIGURATION_ADV_H diff --git a/Firmware/Dcodes.cpp b/Firmware/Dcodes.cpp new file mode 100644 index 0000000..d2016de --- /dev/null +++ b/Firmware/Dcodes.cpp @@ -0,0 +1,680 @@ +#include "Dcodes.h" +//#include "Marlin.h" +#include "language.h" +#include "cmdqueue.h" +#include +#include + +#define SHOW_TEMP_ADC_VALUES +#include "temperature.h" + + +#define DBG(args...) printf_P(args) + +inline void print_hex_nibble(uint8_t val) +{ + putchar((val > 9)?(val - 10 + 'a'):(val + '0')); +} + +void print_hex_byte(uint8_t val) +{ + print_hex_nibble(val >> 4); + print_hex_nibble(val & 15); +} + +void print_hex_word(uint16_t val) +{ + print_hex_byte(val >> 8); + print_hex_byte(val & 255); +} + +void print_eeprom(uint16_t address, uint16_t count, uint8_t countperline = 16) +{ + while (count) + { + print_hex_word(address); + putchar(' '); + uint8_t count_line = countperline; + while (count && count_line) + { + putchar(' '); + print_hex_byte(eeprom_read_byte((uint8_t*)address++)); + count_line--; + count--; + } + putchar('\n'); + } +} + +int parse_hex(char* hex, uint8_t* data, int count) +{ + int parsed = 0; + while (*hex) + { + if (count && (parsed >= count)) break; + char c = *(hex++); + if (c == ' ') continue; + if (c == '\n') break; + uint8_t val = 0x00; + if ((c >= '0') && (c <= '9')) val |= ((c - '0') << 4); + else if ((c >= 'a') && (c <= 'f')) val |= ((c - 'a' + 10) << 4); + else return -parsed; + c = *(hex++); + if ((c >= '0') && (c <= '9')) val |= (c - '0'); + else if ((c >= 'a') && (c <= 'f')) val |= (c - 'a' + 10); + else return -parsed; + data[parsed] = val; + parsed++; + } + return parsed; +} + + +void print_mem(uint32_t address, uint16_t count, uint8_t type, uint8_t countperline = 16) +{ + while (count) + { + if (type == 2) + print_hex_nibble(address >> 16); + print_hex_word(address); + putchar(' '); + uint8_t count_line = countperline; + while (count && count_line) + { + uint8_t data = 0; + switch (type) + { + case 0: data = *((uint8_t*)address++); break; + case 1: data = eeprom_read_byte((uint8_t*)address++); break; + case 2: data = pgm_read_byte_far((uint8_t*)address++); break; + } + putchar(' '); + print_hex_byte(data); + count_line--; + count--; + } + putchar('\n'); + } +} + +#ifdef DEBUG_DCODE3 +#define EEPROM_SIZE 0x1000 +void dcode_3() +{ + DBG(_N("D3 - Read/Write EEPROM\n")); + uint16_t address = 0x0000; //default 0x0000 + uint16_t count = EEPROM_SIZE; //default 0x1000 (entire eeprom) + if (code_seen('A')) // Address (0x0000-0x0fff) + address = (strchr_pointer[1] == 'x')?strtol(strchr_pointer + 2, 0, 16):(int)code_value(); + if (code_seen('C')) // Count (0x0001-0x1000) + count = (int)code_value(); + address &= 0x1fff; + if (count > EEPROM_SIZE) count = EEPROM_SIZE; + if ((address + count) > EEPROM_SIZE) count = EEPROM_SIZE - address; + if (code_seen('X')) // Data + { + uint8_t data[16]; + count = parse_hex(strchr_pointer + 1, data, 16); + if (count > 0) + { + for (uint16_t i = 0; i < count; i++) + eeprom_write_byte((uint8_t*)(address + i), data[i]); + printf_P(_N("%d bytes written to EEPROM at address 0x%04x"), count, address); + putchar('\n'); + } + else + count = 0; + } + print_mem(address, count, 1); +/* while (count) + { + print_hex_word(address); + putchar(' '); + uint8_t countperline = 16; + while (count && countperline) + { + uint8_t data = eeprom_read_byte((uint8_t*)address++); + putchar(' '); + print_hex_byte(data); + countperline--; + count--; + } + putchar('\n'); + }*/ +} +#endif //DEBUG_DCODE3 + + +#include "ConfigurationStore.h" +#include "cmdqueue.h" +#include "pat9125.h" +#include "adc.h" +#include "temperature.h" +#include +#include "bootapp.h" + +#if 0 +#define FLASHSIZE 0x40000 + +#define RAMSIZE 0x2000 +#define boot_src_addr (*((uint32_t*)(RAMSIZE - 16))) +#define boot_dst_addr (*((uint32_t*)(RAMSIZE - 12))) +#define boot_copy_size (*((uint16_t*)(RAMSIZE - 8))) +#define boot_reserved (*((uint8_t*)(RAMSIZE - 6))) +#define boot_app_flags (*((uint8_t*)(RAMSIZE - 5))) +#define boot_app_magic (*((uint32_t*)(RAMSIZE - 4))) +#define BOOT_APP_FLG_ERASE 0x01 +#define BOOT_APP_FLG_COPY 0x02 +#define BOOT_APP_FLG_FLASH 0x04 + +extern uint8_t fsensor_log; +extern float current_temperature_pinda; +extern float axis_steps_per_unit[NUM_AXIS]; + + +#define LOG(args...) printf(args) +#endif //0 +#define LOG(args...) + +void dcode__1() +{ + printf_P(PSTR("D-1 - Endless loop\n")); +// cli(); + while (1); +} + +#ifdef DEBUG_DCODES + +void dcode_0() +{ + if (*(strchr_pointer + 1) == 0) return; + LOG("D0 - Reset\n"); + if (code_seen('B')) //bootloader + { + cli(); + wdt_enable(WDTO_15MS); + while(1); + } + else //reset + { +#ifndef _NO_ASM + asm volatile("jmp 0x00000"); +#endif //_NO_ASM + } +} + +void dcode_1() +{ + LOG("D1 - Clear EEPROM and RESET\n"); + cli(); + for (int i = 0; i < 8192; i++) + eeprom_write_byte((unsigned char*)i, (unsigned char)0xff); + wdt_enable(WDTO_15MS); + while(1); +} + +void dcode_2() +{ + LOG("D2 - Read/Write RAM\n"); + uint16_t address = 0x0000; //default 0x0000 + uint16_t count = 0x2000; //default 0x2000 (entire ram) + if (code_seen('A')) // Address (0x0000-0x1fff) + address = (strchr_pointer[1] == 'x')?strtol(strchr_pointer + 2, 0, 16):(int)code_value(); + if (code_seen('C')) // Count (0x0001-0x2000) + count = (int)code_value(); + address &= 0x1fff; + if (count > 0x2000) count = 0x2000; + if ((address + count) > 0x2000) count = 0x2000 - address; + if (code_seen('X')) // Data + { + uint8_t data[16]; + count = parse_hex(strchr_pointer + 1, data, 16); + if (count > 0) + { + for (uint16_t i = 0; i < count; i++) + *((uint8_t*)(address + i)) = data[i]; + LOG("%d bytes written to RAM at address %04x", count, address); + } + else + count = 0; + } + print_mem(address, count, 0); +/* while (count) + { + print_hex_word(address); + putchar(' '); + uint8_t countperline = 16; + while (count && countperline) + { + uint8_t data = *((uint8_t*)address++); + putchar(' '); + print_hex_byte(data); + countperline--; + count--; + } + putchar('\n'); + }*/ +} + +void dcode_4() +{ + LOG("D4 - Read/Write PIN\n"); + if (code_seen('P')) // Pin (0-255) + { + int pin = (int)code_value(); + if ((pin >= 0) && (pin <= 255)) + { + if (code_seen('F')) // Function in/out (0/1) + { + int fnc = (int)code_value(); + if (fnc == 0) pinMode(pin, INPUT); + else if (fnc == 1) pinMode(pin, OUTPUT); + } + if (code_seen('V')) // Value (0/1) + { + int val = (int)code_value(); + if (val == 0) digitalWrite(pin, LOW); + else if (val == 1) digitalWrite(pin, HIGH); + } + else + { + int val = (digitalRead(pin) != LOW)?1:0; + printf("PIN%d=%d", pin, val); + } + } + } +} +#endif //DEBUG_DCODES + +#ifdef DEBUG_DCODE5 + +void dcode_5() +{ + printf_P(PSTR("D5 - Read/Write FLASH\n")); + uint32_t address = 0x0000; //default 0x0000 + uint16_t count = 0x0400; //default 0x0400 (1kb block) + if (code_seen('A')) // Address (0x00000-0x3ffff) + address = (strchr_pointer[1] == 'x')?strtol(strchr_pointer + 2, 0, 16):(int)code_value(); + if (code_seen('C')) // Count (0x0001-0x2000) + count = (int)code_value(); + address &= 0x3ffff; + if (count > 0x2000) count = 0x2000; + if ((address + count) > 0x40000) count = 0x40000 - address; + bool bErase = false; + bool bCopy = false; + if (code_seen('E')) //Erase + bErase = true; + uint8_t data[16]; + if (code_seen('X')) // Data + { + count = parse_hex(strchr_pointer + 1, data, 16); + if (count > 0) bCopy = true; + } + if (bErase || bCopy) + { + if (bErase) + { + printf_P(PSTR("%d bytes of FLASH at address %05x will be erased\n"), count, address); + } + if (bCopy) + { + printf_P(PSTR("%d bytes will be written to FLASH at address %05x\n"), count, address); + } + cli(); + boot_app_magic = 0x55aa55aa; + boot_app_flags = (bErase?(BOOT_APP_FLG_ERASE):0) | (bCopy?(BOOT_APP_FLG_COPY):0); + boot_copy_size = (uint16_t)count; + boot_dst_addr = (uint32_t)address; + boot_src_addr = (uint32_t)(&data); + bootapp_print_vars(); + wdt_enable(WDTO_15MS); + while(1); + } + while (count) + { + print_hex_nibble(address >> 16); + print_hex_word(address); + putchar(' '); + uint8_t countperline = 16; + while (count && countperline) + { + uint8_t data = pgm_read_byte_far((uint8_t*)address++); + putchar(' '); + print_hex_byte(data); + countperline--; + count--; + } + putchar('\n'); + } +} +#endif //DEBUG_DCODE5 + +#ifdef DEBUG_DCODES + +void dcode_6() +{ + LOG("D6 - Read/Write external FLASH\n"); +} + +void dcode_7() +{ + LOG("D7 - Read/Write Bootloader\n"); +/* + cli(); + boot_app_magic = 0x55aa55aa; + boot_app_flags = BOOT_APP_FLG_ERASE | BOOT_APP_FLG_COPY | BOOT_APP_FLG_FLASH; + boot_copy_size = (uint16_t)0xc00; + boot_src_addr = (uint32_t)0x0003e400; + boot_dst_addr = (uint32_t)0x0003f400; + wdt_enable(WDTO_15MS); + while(1); +*/ +} + +void dcode_8() +{ + printf_P(PSTR("D8 - Read/Write PINDA\n")); + uint8_t cal_status = calibration_status_pinda(); + float temp_pinda = current_temperature_pinda; + float offset_z = temp_compensation_pinda_thermistor_offset(temp_pinda); + if ((strchr_pointer[1+1] == '?') || (strchr_pointer[1+1] == 0)) + { + printf_P(PSTR("cal_status=%d\n"), cal_status?1:0); + for (uint8_t i = 0; i < 6; i++) + { + uint16_t offs = 0; + if (i > 0) offs = eeprom_read_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + (i - 1)); + float foffs = ((float)offs) / cs.axis_steps_per_unit[Z_AXIS]; + offs = 1000 * foffs; + printf_P(PSTR("temp_pinda=%dC temp_shift=%dum\n"), 35 + i * 5, offs); + } + } + else if (strchr_pointer[1+1] == '!') + { + cal_status = 1; + eeprom_write_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, cal_status); + eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 0, 8); //40C - 20um - 8usteps + eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 1, 24); //45C - 60um - 24usteps + eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 2, 48); //50C - 120um - 48usteps + eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 3, 80); //55C - 200um - 80usteps + eeprom_write_word(((uint16_t*)EEPROM_PROBE_TEMP_SHIFT) + 4, 120); //60C - 300um - 120usteps + } + else + { + if (code_seen('P')) // Pinda temperature [C] + temp_pinda = code_value(); + offset_z = temp_compensation_pinda_thermistor_offset(temp_pinda); + if (code_seen('Z')) // Z Offset [mm] + { + offset_z = code_value(); + } + } + printf_P(PSTR("temp_pinda=%d offset_z=%d.%03d\n"), (int)temp_pinda, (int)offset_z, ((int)(1000 * offset_z) % 1000)); +} + +const char* dcode_9_ADC_name(uint8_t i) +{ + switch (i) + { + case 0: return PSTR("TEMP_HEATER0"); + case 1: return PSTR("TEMP_HEATER1"); + case 2: return PSTR("TEMP_BED"); + case 3: return PSTR("TEMP_PINDA"); + case 4: return PSTR("VOLT_PWR"); + case 5: return PSTR("TEMP_AMBIENT"); + case 6: return PSTR("VOLT_BED"); + } + return 0; +} + +#ifdef AMBIENT_THERMISTOR +extern int current_temperature_raw_ambient; +#endif //AMBIENT_THERMISTOR + +#ifdef VOLT_PWR_PIN +extern int current_voltage_raw_pwr; +#endif //VOLT_PWR_PIN + +#ifdef VOLT_BED_PIN +extern int current_voltage_raw_bed; +#endif //VOLT_BED_PIN + +uint16_t dcode_9_ADC_val(uint8_t i) +{ + switch (i) + { + case 0: return current_temperature_raw[0]; + case 1: return 0; + case 2: return current_temperature_bed_raw; + case 3: return current_temperature_raw_pinda; +#ifdef VOLT_PWR_PIN + case 4: return current_voltage_raw_pwr; +#endif //VOLT_PWR_PIN +#ifdef AMBIENT_THERMISTOR + case 5: return current_temperature_raw_ambient; +#endif //AMBIENT_THERMISTOR +#ifdef VOLT_BED_PIN + case 6: return current_voltage_raw_bed; +#endif //VOLT_BED_PIN + } + return 0; +} + +void dcode_9() +{ + printf_P(PSTR("D9 - Read/Write ADC\n")); + if ((strchr_pointer[1+1] == '?') || (strchr_pointer[1+1] == 0)) + { + for (uint8_t i = 0; i < ADC_CHAN_CNT; i++) + printf_P(PSTR("\tADC%d=%4d\t(%S)\n"), i, dcode_9_ADC_val(i) >> 4, dcode_9_ADC_name(i)); + } + else + { + uint8_t index = 0xff; + if (code_seen('I')) // index (index of used channel, not avr channel index) + index = code_value(); + if (index < ADC_CHAN_CNT) + { + if (code_seen('V')) // value to be written as simulated + { + adc_sim_mask |= (1 << index); + adc_values[index] = (((int)code_value()) << 4); + printf_P(PSTR("ADC%d=%4d\n"), index, adc_values[index] >> 4); + } + } + } +} + +void dcode_10() +{//Tell the printer that XYZ calibration went OK + LOG("D10 - XYZ calibration = OK\n"); + calibration_status_store(CALIBRATION_STATUS_LIVE_ADJUST); +} + +void dcode_12() +{//Time + LOG("D12 - Time\n"); + +} + + +#ifdef TMC2130 +#include "planner.h" +#include "tmc2130.h" +extern void st_synchronize(); +/** + * @brief D2130 Trinamic stepper controller + * D2130[subcommand][value] + * * Axis + * * * 'X' + * * * 'Y' + * * * 'Z' + * * * 'E' + * * command + * * * '0' current off + * * * '1' current on + * * * '+' single step + * * * * value sereval steps + * * * '-' dtto oposite direction + * * * '?' read register + * * * * "mres" + * * * * "step" + * * * * "mscnt" + * * * * "mscuract" + * * * * "wave" + * * * '!' set register + * * * * "mres" + * * * * "step" + * * * * "wave" + * * * * *0, 180..250 meaning: off, 0.9..1.25, recommended value is 1.1 + * * * '@' home calibrate axis + * + * Example: + * D2130E?wave //print extruder microstep linearity compensation curve + * D2130E!wave0 //disable extruder linearity compensation curve, (sine curve is used) + * D2130E!wave220 // (sin(x))^1.1 extruder microstep compensation curve used + */ +void dcode_2130() +{ + printf_P(PSTR("D2130 - TMC2130\n")); + uint8_t axis = 0xff; + switch (strchr_pointer[1+4]) + { + case 'X': axis = X_AXIS; break; + case 'Y': axis = Y_AXIS; break; + case 'Z': axis = Z_AXIS; break; + case 'E': axis = E_AXIS; break; + } + if (axis != 0xff) + { + char ch_axis = strchr_pointer[1+4]; + if (strchr_pointer[1+5] == '0') { tmc2130_set_pwr(axis, 0); } + else if (strchr_pointer[1+5] == '1') { tmc2130_set_pwr(axis, 1); } + else if (strchr_pointer[1+5] == '+') + { + if (strchr_pointer[1+6] == 0) + { + tmc2130_set_dir(axis, 0); + tmc2130_do_step(axis); + } + else + { + uint8_t steps = atoi(strchr_pointer + 1 + 6); + tmc2130_do_steps(axis, steps, 0, 1000); + } + } + else if (strchr_pointer[1+5] == '-') + { + if (strchr_pointer[1+6] == 0) + { + tmc2130_set_dir(axis, 1); + tmc2130_do_step(axis); + } + else + { + uint8_t steps = atoi(strchr_pointer + 1 + 6); + tmc2130_do_steps(axis, steps, 1, 1000); + } + } + else if (strchr_pointer[1+5] == '?') + { + if (strcmp(strchr_pointer + 7, "mres") == 0) printf_P(PSTR("%c mres=%d\n"), ch_axis, tmc2130_mres[axis]); + else if (strcmp(strchr_pointer + 7, "step") == 0) printf_P(PSTR("%c step=%d\n"), ch_axis, tmc2130_rd_MSCNT(axis) >> tmc2130_mres[axis]); + else if (strcmp(strchr_pointer + 7, "mscnt") == 0) printf_P(PSTR("%c MSCNT=%d\n"), ch_axis, tmc2130_rd_MSCNT(axis)); + else if (strcmp(strchr_pointer + 7, "mscuract") == 0) + { + uint32_t val = tmc2130_rd_MSCURACT(axis); + int curA = (val & 0xff); + int curB = ((val >> 16) & 0xff); + if ((val << 7) & 0x8000) curA -= 256; + if ((val >> 9) & 0x8000) curB -= 256; + printf_P(PSTR("%c MSCURACT=0x%08lx A=%d B=%d\n"), ch_axis, val, curA, curB); + } + else if (strcmp(strchr_pointer + 7, "wave") == 0) + { + tmc2130_get_wave(axis, 0, stdout); + } + } + else if (strchr_pointer[1+5] == '!') + { + if (strncmp(strchr_pointer + 7, "step", 4) == 0) + { + uint8_t step = atoi(strchr_pointer + 11); + uint16_t res = tmc2130_get_res(axis); + tmc2130_goto_step(axis, step & (4*res - 1), 2, 1000, res); + } + else if (strncmp(strchr_pointer + 7, "mres", 4) == 0) + { + uint8_t mres = strchr_pointer[11] - '0'; + if (mres <= 8) + { + st_synchronize(); + uint16_t res = tmc2130_get_res(axis); + uint16_t res_new = tmc2130_mres2usteps(mres); + tmc2130_set_res(axis, res_new); + if (res_new > res) + cs.axis_steps_per_unit[axis] *= (res_new / res); + else + cs.axis_steps_per_unit[axis] /= (res / res_new); + } + } + else if (strncmp(strchr_pointer + 7, "wave", 4) == 0) + { + uint8_t fac1000 = atoi(strchr_pointer + 11) & 0xffff; + if (fac1000 < TMC2130_WAVE_FAC1000_MIN) fac1000 = 0; + if (fac1000 > TMC2130_WAVE_FAC1000_MAX) fac1000 = TMC2130_WAVE_FAC1000_MAX; + tmc2130_set_wave(axis, 247, fac1000); + tmc2130_wave_fac[axis] = fac1000; + } + } + else if (strchr_pointer[1+5] == '@') + { + tmc2130_home_calibrate(axis); + } + } +} +#endif //TMC2130 + +#ifdef PAT9125 +void dcode_9125() +{ + LOG("D9125 - PAT9125\n"); + if ((strchr_pointer[1+4] == '?') || (strchr_pointer[1+4] == 0)) + { +// printf("res_x=%d res_y=%d x=%d y=%d b=%d s=%d\n", pat9125_xres, pat9125_yres, pat9125_x, pat9125_y, pat9125_b, pat9125_s); + printf("x=%d y=%d b=%d s=%d\n", pat9125_x, pat9125_y, pat9125_b, pat9125_s); + return; + } + if (strchr_pointer[1+4] == '!') + { + pat9125_update(); + printf("x=%d y=%d b=%d s=%d\n", pat9125_x, pat9125_y, pat9125_b, pat9125_s); + return; + } +/* + if (code_seen('R')) + { + unsigned char res = (int)code_value(); + LOG("pat9125_init(xres=yres=%d)=%d\n", res, pat9125_init(res, res)); + } +*/ + if (code_seen('X')) + { + pat9125_x = (int)code_value(); + LOG("pat9125_x=%d\n", pat9125_x); + } + if (code_seen('Y')) + { + pat9125_y = (int)code_value(); + LOG("pat9125_y=%d\n", pat9125_y); + } + if (code_seen('L')) + { + fsensor_log = (int)code_value(); + LOG("fsensor_log=%d\n", fsensor_log); + } +} +#endif //PAT9125 + + +#endif //DEBUG_DCODES diff --git a/Firmware/Dcodes.h b/Firmware/Dcodes.h new file mode 100644 index 0000000..eaf849e --- /dev/null +++ b/Firmware/Dcodes.h @@ -0,0 +1,28 @@ +#ifndef DCODES_H +#define DCODES_H + +extern void dcode__1(); //D-1 - Endless loop (to simulate deadlock) + +extern void dcode_0(); //D0 - Reset +extern void dcode_1(); //D1 - Clear EEPROM +extern void dcode_2(); //D2 - Read/Write RAM +extern void dcode_3(); //D3 - Read/Write EEPROM +extern void dcode_4(); //D4 - Read/Write PIN +extern void dcode_5(); //D5 - Read/Write FLASH +extern void dcode_6(); //D6 - Read/Write external FLASH +extern void dcode_7(); //D7 - Read/Write Bootloader +extern void dcode_8(); //D8 - Read/Write PINDA +extern void dcode_9(); //D9 - Read/Write ADC (Write=enable simulated, Read=disable simulated) + +extern void dcode_10(); //D10 - XYZ calibration = OK + +#ifdef TMC2130 +extern void dcode_2130(); //D2130 - TMC2130 +#endif //TMC2130 + +#ifdef PAT9125 +extern void dcode_9125(); //D9125 - PAT9125 +#endif //PAT9125 + + +#endif //DCODES_H diff --git a/Firmware/Firmware.ino b/Firmware/Firmware.ino new file mode 100644 index 0000000..98f2e69 --- /dev/null +++ b/Firmware/Firmware.ino @@ -0,0 +1,35 @@ +/* -*- c++ -*- */ + +/* + Reprap firmware 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 . + */ + +/* + This firmware is a mashup between Sprinter and grbl. + (https://github.com/kliment/Sprinter) + (https://github.com/simen/grbl/tree) + + It has preliminary support for Matthew Roberts advance algorithm + http://reprap.org/pipermail/reprap-dev/2011-May/003323.html + */ + +/* All the implementation is done in *.cpp files to get better compatibility with avr-gcc without the Arduino IDE */ +/* Use this file to help the Arduino IDE find which Arduino libraries are needed and to keep documentation on GCode */ + +#include "Configuration.h" +#include "pins.h" + diff --git a/Firmware/Marlin.h b/Firmware/Marlin.h new file mode 100644 index 0000000..c1a7b56 --- /dev/null +++ b/Firmware/Marlin.h @@ -0,0 +1,516 @@ +// Tonokip RepRap firmware rewrite based off of Hydra-mmm firmware. +// License: GPL + +#ifndef MARLIN_H +#define MARLIN_H + +#define FORCE_INLINE __attribute__((always_inline)) inline + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include "system_timer.h" +#include "fastio.h" +#include "Configuration.h" +#include "pins.h" +#include "Timer.h" +extern uint8_t mbl_z_probe_nr; + +#ifndef AT90USB +#define HardwareSerial_h // trick to disable the standard HWserial +#endif + +#if (ARDUINO >= 100) +# include "Arduino.h" +#else +# include "WProgram.h" +#endif + +// Arduino < 1.0.0 does not define this, so we need to do it ourselves +#ifndef analogInputToDigitalPin +# define analogInputToDigitalPin(p) ((p) + A0) +#endif + +#ifdef AT90USB +#include "HardwareSerial.h" +#endif + +#include "MarlinSerial.h" + +#ifndef cbi +#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) +#endif +#ifndef sbi +#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) +#endif + +//#include "WString.h" + +#ifdef AT90USB + #ifdef BTENABLED + #define MYSERIAL bt + #else + #define MYSERIAL Serial + #endif // BTENABLED +#else + #define MYSERIAL MSerial +#endif + +#include "lcd.h" + +#ifdef __cplusplus +extern "C" { +#endif +extern FILE _uartout; +#ifdef __cplusplus +} +#endif + +#define uartout (&_uartout) + +#define SERIAL_PROTOCOL(x) (MYSERIAL.print(x)) +#define SERIAL_PROTOCOL_F(x,y) (MYSERIAL.print(x,y)) +#define SERIAL_PROTOCOLPGM(x) (serialprintPGM(PSTR(x))) +#define SERIAL_PROTOCOLRPGM(x) (serialprintPGM((x))) +#define SERIAL_PROTOCOLLN(x) (MYSERIAL.println(x)/*,MYSERIAL.write('\n')*/) +#define SERIAL_PROTOCOLLNPGM(x) (serialprintPGM(PSTR(x)),MYSERIAL.println()/*write('\n')*/) +#define SERIAL_PROTOCOLLNRPGM(x) (serialprintPGM((x)),MYSERIAL.println()/*write('\n')*/) + + +extern const char errormagic[] PROGMEM; +extern const char echomagic[] PROGMEM; + +#define SERIAL_ERROR_START (serialprintPGM(errormagic)) +#define SERIAL_ERROR(x) SERIAL_PROTOCOL(x) +#define SERIAL_ERRORPGM(x) SERIAL_PROTOCOLPGM(x) +#define SERIAL_ERRORRPGM(x) SERIAL_PROTOCOLRPGM(x) +#define SERIAL_ERRORLN(x) SERIAL_PROTOCOLLN(x) +#define SERIAL_ERRORLNPGM(x) SERIAL_PROTOCOLLNPGM(x) +#define SERIAL_ERRORLNRPGM(x) SERIAL_PROTOCOLLNRPGM(x) + +#define SERIAL_ECHO_START (serialprintPGM(echomagic)) +#define SERIAL_ECHO(x) SERIAL_PROTOCOL(x) +#define SERIAL_ECHOPGM(x) SERIAL_PROTOCOLPGM(x) +#define SERIAL_ECHORPGM(x) SERIAL_PROTOCOLRPGM(x) +#define SERIAL_ECHOLN(x) SERIAL_PROTOCOLLN(x) +#define SERIAL_ECHOLNPGM(x) SERIAL_PROTOCOLLNPGM(x) +#define SERIAL_ECHOLNRPGM(x) SERIAL_PROTOCOLLNRPGM(x) + +#define SERIAL_ECHOPAIR(name,value) (serial_echopair_P(PSTR(name),(value))) + +void serial_echopair_P(const char *s_P, float v); +void serial_echopair_P(const char *s_P, double v); +void serial_echopair_P(const char *s_P, unsigned long v); + + +//Things to write to serial from Program memory. Saves 400 to 2k of RAM. +// Making this FORCE_INLINE is not a good idea when running out of FLASH +// I'd rather skip a few CPU ticks than 5.5KB (!!) of FLASH +void serialprintPGM(const char *str); + +bool is_buffer_empty(); +void get_command(); +void process_commands(); +void ramming(); + +void manage_inactivity(bool ignore_stepper_queue=false); + +#if defined(X_ENABLE_PIN) && X_ENABLE_PIN > -1 + #define enable_x() WRITE(X_ENABLE_PIN, X_ENABLE_ON) + #define disable_x() { WRITE(X_ENABLE_PIN,!X_ENABLE_ON); axis_known_position[X_AXIS] = false; } +#else + #define enable_x() ; + #define disable_x() ; +#endif + +#if defined(Y_ENABLE_PIN) && Y_ENABLE_PIN > -1 + #ifdef Y_DUAL_STEPPER_DRIVERS + #define enable_y() { WRITE(Y_ENABLE_PIN, Y_ENABLE_ON); WRITE(Y2_ENABLE_PIN, Y_ENABLE_ON); } + #define disable_y() { WRITE(Y_ENABLE_PIN,!Y_ENABLE_ON); WRITE(Y2_ENABLE_PIN, !Y_ENABLE_ON); axis_known_position[Y_AXIS] = false; } + #else + #define enable_y() WRITE(Y_ENABLE_PIN, Y_ENABLE_ON) + #define disable_y() { WRITE(Y_ENABLE_PIN,!Y_ENABLE_ON); axis_known_position[Y_AXIS] = false; } + #endif +#else + #define enable_y() ; + #define disable_y() ; +#endif + +#if defined(Z_ENABLE_PIN) && Z_ENABLE_PIN > -1 + #if defined(Z_AXIS_ALWAYS_ON) + #ifdef Z_DUAL_STEPPER_DRIVERS + #define enable_z() { WRITE(Z_ENABLE_PIN, Z_ENABLE_ON); WRITE(Z2_ENABLE_PIN, Z_ENABLE_ON); } + #define disable_z() { WRITE(Z_ENABLE_PIN,!Z_ENABLE_ON); WRITE(Z2_ENABLE_PIN,!Z_ENABLE_ON); axis_known_position[Z_AXIS] = false; } + #else + #define enable_z() WRITE(Z_ENABLE_PIN, Z_ENABLE_ON) + #define disable_z() {} + #endif + #else + #ifdef Z_DUAL_STEPPER_DRIVERS + #define enable_z() { WRITE(Z_ENABLE_PIN, Z_ENABLE_ON); WRITE(Z2_ENABLE_PIN, Z_ENABLE_ON); } + #define disable_z() { WRITE(Z_ENABLE_PIN,!Z_ENABLE_ON); WRITE(Z2_ENABLE_PIN,!Z_ENABLE_ON); axis_known_position[Z_AXIS] = false; } + #else + #define enable_z() WRITE(Z_ENABLE_PIN, Z_ENABLE_ON) + #define disable_z() { WRITE(Z_ENABLE_PIN,!Z_ENABLE_ON); axis_known_position[Z_AXIS] = false; } + #endif + #endif +#else + #define enable_z() {} + #define disable_z() {} +#endif + +#ifdef PSU_Delta + void init_force_z(); + void check_force_z(); + #undef disable_z + #define disable_z() disable_force_z() + void disable_force_z(); + #undef enable_z + #define enable_z() enable_force_z() + void enable_force_z(); +#endif // PSU_Delta + + + + +//#if defined(Z_ENABLE_PIN) && Z_ENABLE_PIN > -1 +//#ifdef Z_DUAL_STEPPER_DRIVERS +//#define enable_z() { WRITE(Z_ENABLE_PIN, Z_ENABLE_ON); WRITE(Z2_ENABLE_PIN, Z_ENABLE_ON); } +//#define disable_z() { WRITE(Z_ENABLE_PIN,!Z_ENABLE_ON); WRITE(Z2_ENABLE_PIN,!Z_ENABLE_ON); axis_known_position[Z_AXIS] = false; } +//#else +//#define enable_z() WRITE(Z_ENABLE_PIN, Z_ENABLE_ON) +//#define disable_z() { WRITE(Z_ENABLE_PIN,!Z_ENABLE_ON); axis_known_position[Z_AXIS] = false; } +//#endif +//#else +//#define enable_z() ; +//#define disable_z() ; +//#endif + + +#if defined(E0_ENABLE_PIN) && (E0_ENABLE_PIN > -1) + #define enable_e0() WRITE(E0_ENABLE_PIN, E_ENABLE_ON) + #define disable_e0() WRITE(E0_ENABLE_PIN,!E_ENABLE_ON) +#else + #define enable_e0() /* nothing */ + #define disable_e0() /* nothing */ +#endif + +#if (EXTRUDERS > 1) && defined(E1_ENABLE_PIN) && (E1_ENABLE_PIN > -1) + #define enable_e1() WRITE(E1_ENABLE_PIN, E_ENABLE_ON) + #define disable_e1() WRITE(E1_ENABLE_PIN,!E_ENABLE_ON) +#else + #define enable_e1() /* nothing */ + #define disable_e1() /* nothing */ +#endif + +#if (EXTRUDERS > 2) && defined(E2_ENABLE_PIN) && (E2_ENABLE_PIN > -1) + #define enable_e2() WRITE(E2_ENABLE_PIN, E_ENABLE_ON) + #define disable_e2() WRITE(E2_ENABLE_PIN,!E_ENABLE_ON) +#else + #define enable_e2() /* nothing */ + #define disable_e2() /* nothing */ +#endif + + +#define FARM_FILAMENT_COLOR_NONE 99; + + +enum AxisEnum {X_AXIS=0, Y_AXIS=1, Z_AXIS=2, E_AXIS=3, X_HEAD=4, Y_HEAD=5}; +#define X_AXIS_MASK 1 +#define Y_AXIS_MASK 2 +#define Z_AXIS_MASK 4 +#define E_AXIS_MASK 8 +#define X_HEAD_MASK 16 +#define Y_HEAD_MASK 32 + + +void FlushSerialRequestResend(); +void ClearToSend(); +void update_currents(); + +void get_coordinates(); +void prepare_move(); +void kill(const char *full_screen_message = NULL, unsigned char id = 0); +void Stop(); + +bool IsStopped(); + +//put an ASCII command at the end of the current buffer. +void enquecommand(const char *cmd, bool from_progmem = false); + +//put an ASCII command at the end of the current buffer, read from flash +#define enquecommand_P(cmd) enquecommand(cmd, true) + +//put an ASCII command at the begin of the current buffer +void enquecommand_front(const char *cmd, bool from_progmem = false); + +//put an ASCII command at the begin of the current buffer, read from flash +#define enquecommand_front_P(cmd) enquecommand_front(cmd, true) + +void repeatcommand_front(); + +// Remove all lines from the command queue. +void cmdqueue_reset(); + +void prepare_arc_move(char isclockwise); +void clamp_to_software_endstops(float target[3]); +void refresh_cmd_timeout(void); + +// Timer counter, incremented by the 1ms Arduino timer. +// The standard Arduino timer() function returns this value atomically +// by disabling / enabling interrupts. This is costly, if the interrupts are known +// to be disabled. +#ifdef SYSTEM_TIMER_2 +extern volatile unsigned long timer2_millis; +#else //SYSTEM_TIMER_2 +extern volatile unsigned long timer0_millis; +#endif //SYSTEM_TIMER_2 + +// An unsynchronized equivalent to a standard Arduino _millis() function. +// To be used inside an interrupt routine. + +FORCE_INLINE unsigned long millis_nc() { +#ifdef SYSTEM_TIMER_2 + return timer2_millis; +#else //SYSTEM_TIMER_2 + return timer0_millis; +#endif //SYSTEM_TIMER_2 +} + +#ifdef FAST_PWM_FAN +void setPwmFrequency(uint8_t pin, int val); +#endif + +#ifndef CRITICAL_SECTION_START + #define CRITICAL_SECTION_START unsigned char _sreg = SREG; cli(); + #define CRITICAL_SECTION_END SREG = _sreg; +#endif //CRITICAL_SECTION_START + +extern bool fans_check_enabled; +extern float homing_feedrate[]; +extern bool axis_relative_modes[]; +extern int feedmultiply; +extern int extrudemultiply; // Sets extrude multiply factor (in percent) for all extruders +extern int extruder_multiply[EXTRUDERS]; // sets extrude multiply factor (in percent) for each extruder individually +extern float volumetric_multiplier[EXTRUDERS]; // reciprocal of cross-sectional area of filament (in square millimeters), stored this way to reduce computational burden in planner +extern float current_position[NUM_AXIS] ; +extern float destination[NUM_AXIS] ; +extern float min_pos[3]; +extern float max_pos[3]; +extern bool axis_known_position[3]; +extern int fanSpeed; +extern int8_t lcd_change_fil_state; + +#ifdef TMC2130 +void homeaxis(int axis, uint8_t cnt = 1, uint8_t* pstep = 0); +#else +void homeaxis(int axis, uint8_t cnt = 1); +#endif //TMC2130 + + +#ifdef FAN_SOFT_PWM +extern unsigned char fanSpeedSoftPwm; +#endif + +#ifdef FWRETRACT +extern bool retracted[EXTRUDERS]; +extern float retract_length_swap; +extern float retract_recover_length_swap; +#endif + + +extern uint8_t host_keepalive_interval; + +extern unsigned long starttime; +extern unsigned long stoptime; +extern int bowden_length[4]; +extern bool is_usb_printing; +extern bool homing_flag; +extern bool temp_cal_active; +extern bool loading_flag; +extern unsigned int usb_printing_counter; + +extern unsigned long kicktime; + +extern unsigned long total_filament_used; +void save_statistics(unsigned long _total_filament_used, unsigned long _total_print_time); +extern unsigned int heating_status; +extern unsigned int status_number; +extern unsigned int heating_status_counter; +extern char snmm_filaments_used; +extern unsigned long PingTime; +extern unsigned long NcTime; +extern bool no_response; +extern uint8_t important_status; +extern uint8_t saved_filament_type; + +extern bool fan_state[2]; +extern int fan_edge_counter[2]; +extern int fan_speed[2]; + +// Handling multiple extruders pins +extern uint8_t active_extruder; + + +#endif + +//Long pause +extern unsigned long pause_time; +extern unsigned long start_pause_print; +extern unsigned long t_fan_rising_edge; + +extern bool mesh_bed_leveling_flag; +extern bool mesh_bed_run_from_menu; + +extern bool sortAlpha; + +extern char dir_names[3][9]; + +extern int8_t lcd_change_fil_state; +// save/restore printing +extern bool saved_printing; +extern uint8_t saved_printing_type; +#define PRINTING_TYPE_SD 0 +#define PRINTING_TYPE_USB 1 +#define PRINTING_TYPE_NONE 2 + +//save/restore printing in case that mmu is not responding +extern bool mmu_print_saved; + +//estimated time to end of the print +extern uint8_t print_percent_done_normal; +extern uint16_t print_time_remaining_normal; +extern uint8_t print_percent_done_silent; +extern uint16_t print_time_remaining_silent; + +#define PRINT_TIME_REMAINING_INIT 0xffff + +extern uint16_t mcode_in_progress; +extern uint16_t gcode_in_progress; + +extern LongTimer safetyTimer; + +#define PRINT_PERCENT_DONE_INIT 0xff +#define PRINTER_ACTIVE (IS_SD_PRINTING || is_usb_printing || isPrintPaused || (custom_message_type == CustomMsg::TempCal) || saved_printing || (lcd_commands_type == LcdCommands::Layer1Cal) || card.paused || mmu_print_saved) + +//! Beware - mcode_in_progress is set as soon as the command gets really processed, +//! which is not the same as posting the M600 command into the command queue +//! There can be a considerable lag between posting M600 and its real processing which might result +//! in posting multiple M600's into the command queue +//! Instead, the fsensor uses another state variable :( , which is set to true, when the M600 command is enqued +//! and is reset to false when the fsensor returns into its filament runout finished handler +//! I'd normally change this macro, but who knows what would happen in the MMU :) +#define CHECK_FSENSOR ((IS_SD_PRINTING || is_usb_printing) && (mcode_in_progress != 600) && !saved_printing && e_active()) + +extern void calculate_extruder_multipliers(); + +// Similar to the default Arduino delay function, +// but it keeps the background tasks running. +extern void delay_keep_alive(unsigned int ms); + +extern void check_babystep(); + +extern void long_pause(); +extern void crashdet_stop_and_save_print(); + +#ifdef HEATBED_ANALYSIS +void d_setup(); +float d_ReadData(); +void bed_analysis(float x_dimension, float y_dimension, int x_points_num, int y_points_num, float shift_x, float shift_y); +void bed_check(float x_dimension, float y_dimension, int x_points_num, int y_points_num, float shift_x, float shift_y); +#endif //HEATBED_ANALYSIS +float temp_comp_interpolation(float temperature); +void show_fw_version_warnings(); +uint8_t check_printer_version(); + +#ifdef PINDA_THERMISTOR +float temp_compensation_pinda_thermistor_offset(float temperature_pinda); +#endif //PINDA_THERMISTOR + +void serialecho_temperatures(); +bool check_commands(); + +void uvlo_(); +void uvlo_tiny(); +void recover_print(uint8_t automatic); +void setup_uvlo_interrupt(); + +#if defined(TACH_1) && TACH_1 >-1 +void setup_fan_interrupt(); +#endif + +//extern void recover_machine_state_after_power_panic(); +extern void recover_machine_state_after_power_panic(bool bTiny); +extern void restore_print_from_eeprom(); +extern void position_menu(); + +extern void print_world_coordinates(); +extern void print_physical_coordinates(); +extern void print_mesh_bed_leveling_table(); + +extern void stop_and_save_print_to_ram(float z_move, float e_move); +extern void restore_print_from_ram_and_continue(float e_move); + + +//estimated time to end of the print +extern uint16_t print_time_remaining(); +extern uint8_t calc_percent_done(); + + + +// States for managing Marlin and host communication +// Marlin sends messages if blocked or busy +/*enum MarlinBusyState { + NOT_BUSY, // Not in a handler + IN_HANDLER, // Processing a GCode + IN_PROCESS, // Known to be blocking command input (as in G29) + PAUSED_FOR_USER, // Blocking pending any input + PAUSED_FOR_INPUT // Blocking pending text input (concept) +};*/ + +#define NOT_BUSY 1 +#define IN_HANDLER 2 +#define IN_PROCESS 3 +#define PAUSED_FOR_USER 4 +#define PAUSED_FOR_INPUT 5 + +#define KEEPALIVE_STATE(n) do { busy_state = n;} while (0) +extern void host_keepalive(); +//extern MarlinBusyState busy_state; +extern int8_t busy_state; + + +#ifdef TMC2130 + +#define FORCE_HIGH_POWER_START force_high_power_mode(true) +#define FORCE_HIGH_POWER_END force_high_power_mode(false) + +void force_high_power_mode(bool start_high_power_section); + +#endif //TMC2130 + +// G-codes + +bool gcode_M45(bool onlyZ, int8_t verbosity_level); +void gcode_M114(); +void gcode_M701(); + +#define UVLO !(PINE & (1<<4)) + +void proc_commands(); + + +void M600_load_filament(); +void M600_load_filament_movements(); +void M600_wait_for_user(float HotendTempBckp); +void M600_check_state(float nozzle_temp); +void load_filament_final_feed(); +void marlin_wait_for_click(); +void marlin_rise_z(void); diff --git a/Firmware/MarlinSerial.cpp b/Firmware/MarlinSerial.cpp new file mode 100644 index 0000000..d3ffdfb --- /dev/null +++ b/Firmware/MarlinSerial.cpp @@ -0,0 +1,385 @@ +/* + HardwareSerial.cpp - Hardware serial library for Wiring + Copyright (c) 2006 Nicholas Zambetti. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Modified 23 November 2006 by David A. Mellis + Modified 28 September 2010 by Mark Sproul +*/ + +#include "Marlin.h" +#include "MarlinSerial.h" + +uint8_t selectedSerialPort = 0; + +#ifndef AT90USB +// this next line disables the entire HardwareSerial.cpp, +// this is so I can support Attiny series and any other chip without a UART +#if defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H) + +#if UART_PRESENT(SERIAL_PORT) + ring_buffer rx_buffer = { { 0 }, 0, 0 }; +#endif + +FORCE_INLINE void store_char(unsigned char c) +{ + int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE; + + // if we should be storing the received character into the location + // just before the tail (meaning that the head would advance to the + // current location of the tail), we're about to overflow the buffer + // and so we don't write the character or advance the head. + if (i != rx_buffer.tail) { + rx_buffer.buffer[rx_buffer.head] = c; + rx_buffer.head = i; + } +} + + +#if defined(M_USARTx_RX_vect) +// The serial line receive interrupt routine for a baud rate 115200 +// ticks at maximum 11.76 kHz and blocks for 2.688 us at each tick. +// If the serial line is fully utilized, this corresponds to 3.16% +// loading of the CPU (the interrupt invocation overhead not taken into account). +// As the serial line is not fully utilized, the CPU load is likely around 1%. +ISR(M_USARTx_RX_vect) +{ + // Test for a framing error. + if (M_UCSRxA & (1<> 8; + M_UBRRxL = baud_setting; + + sbi(M_UCSRxB, M_RXENx); + sbi(M_UCSRxB, M_TXENx); + sbi(M_UCSRxB, M_RXCIEx); + +#ifndef SNMM + + if (selectedSerialPort == 1) { //set up also the second serial port + if (useU2X) { + UCSR1A = 1 << U2X1; + baud_setting = (F_CPU / 4 / baud - 1) / 2; + } else { + UCSR1A = 0; + baud_setting = (F_CPU / 8 / baud - 1) / 2; + } + + // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register) + UBRR1H = baud_setting >> 8; + UBRR1L = baud_setting; + + sbi(UCSR1B, RXEN1); + sbi(UCSR1B, TXEN1); + sbi(UCSR1B, RXCIE1); + } +#endif +} + +void MarlinSerial::end() +{ + cbi(M_UCSRxB, M_RXENx); + cbi(M_UCSRxB, M_TXENx); + cbi(M_UCSRxB, M_RXCIEx); + +#ifndef SNMM + cbi(UCSR1B, RXEN1); + cbi(UCSR1B, TXEN1); + cbi(UCSR1B, RXCIE1); +#endif +} + + + +int MarlinSerial::peek(void) +{ + if (rx_buffer.head == rx_buffer.tail) { + return -1; + } else { + return rx_buffer.buffer[rx_buffer.tail]; + } +} + +int MarlinSerial::read(void) +{ + // if the head isn't ahead of the tail, we don't have any characters + if (rx_buffer.head == rx_buffer.tail) { + return -1; + } else { + unsigned char c = rx_buffer.buffer[rx_buffer.tail]; + rx_buffer.tail = (unsigned int)(rx_buffer.tail + 1) % RX_BUFFER_SIZE; + return c; + } +} + +void MarlinSerial::flush() +{ + // don't reverse this or there may be problems if the RX interrupt + // occurs after reading the value of rx_buffer_head but before writing + // the value to rx_buffer_tail; the previous value of rx_buffer_head + // may be written to rx_buffer_tail, making it appear as if the buffer + // were full, not empty. + rx_buffer.head = rx_buffer.tail; +} + + + + +/// imports from print.h + + + + +void MarlinSerial::print(char c, int base) +{ + print((long) c, base); +} + +void MarlinSerial::print(unsigned char b, int base) +{ + print((unsigned long) b, base); +} + +void MarlinSerial::print(int n, int base) +{ + print((long) n, base); +} + +void MarlinSerial::print(unsigned int n, int base) +{ + print((unsigned long) n, base); +} + +void MarlinSerial::print(long n, int base) +{ + if (base == 0) { + write(n); + } else if (base == 10) { + if (n < 0) { + print('-'); + n = -n; + } + printNumber(n, 10); + } else { + printNumber(n, base); + } +} + +void MarlinSerial::print(unsigned long n, int base) +{ + if (base == 0) write(n); + else printNumber(n, base); +} + +void MarlinSerial::print(double n, int digits) +{ + printFloat(n, digits); +} + +void MarlinSerial::println(void) +{ + print('\r'); + print('\n'); +} + +/*void MarlinSerial::println(const String &s) +{ + print(s); + println(); +}*/ + +void MarlinSerial::println(const char c[]) +{ + print(c); + println(); +} + +void MarlinSerial::println(char c, int base) +{ + print(c, base); + println(); +} + +void MarlinSerial::println(unsigned char b, int base) +{ + print(b, base); + println(); +} + +void MarlinSerial::println(int n, int base) +{ + print(n, base); + println(); +} + +void MarlinSerial::println(unsigned int n, int base) +{ + print(n, base); + println(); +} + +void MarlinSerial::println(long n, int base) +{ + print(n, base); + println(); +} + +void MarlinSerial::println(unsigned long n, int base) +{ + print(n, base); + println(); +} + +void MarlinSerial::println(double n, int digits) +{ + print(n, digits); + println(); +} + +// Private Methods ///////////////////////////////////////////////////////////// + +void MarlinSerial::printNumber(unsigned long n, uint8_t base) +{ + unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars. + unsigned long i = 0; + + if (n == 0) { + print('0'); + return; + } + + while (n > 0) { + buf[i++] = n % base; + n /= base; + } + + for (; i > 0; i--) + print((char) (buf[i - 1] < 10 ? + '0' + buf[i - 1] : + 'A' + buf[i - 1] - 10)); +} + +void MarlinSerial::printFloat(double number, uint8_t digits) +{ + // Handle negative numbers + if (number < 0.0) + { + print('-'); + number = -number; + } + + // Round correctly so that print(1.999, 2) prints as "2.00" + double rounding = 0.5; + for (uint8_t i=0; i 0) + print("."); + + // Extract digits from the remainder one at a time + while (digits-- > 0) + { + remainder *= 10.0; + int toPrint = int(remainder); + print(toPrint); + remainder -= toPrint; + } +} +// Preinstantiate Objects ////////////////////////////////////////////////////// + + +MarlinSerial MSerial; + +#endif // whole file +#endif // !AT90USB + +// For AT90USB targets use the UART for BT interfacing +#if defined(AT90USB) && defined (BTENABLED) + HardwareSerial bt; +#endif + diff --git a/Firmware/MarlinSerial.h b/Firmware/MarlinSerial.h new file mode 100644 index 0000000..27e722b --- /dev/null +++ b/Firmware/MarlinSerial.h @@ -0,0 +1,239 @@ +/* + HardwareSerial.h - Hardware serial library for Wiring + Copyright (c) 2006 Nicholas Zambetti. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Modified 28 September 2010 by Mark Sproul +*/ + +#ifndef MarlinSerial_h +#define MarlinSerial_h +#include "Marlin.h" + +#if !defined(SERIAL_PORT) +#define SERIAL_PORT 0 +#endif + +// The presence of the UBRRH register is used to detect a UART. +#define UART_PRESENT(port) ((port == 0 && (defined(UBRRH) || defined(UBRR0H))) || \ + (port == 1 && defined(UBRR1H)) || (port == 2 && defined(UBRR2H)) || \ + (port == 3 && defined(UBRR3H))) + +// These are macros to build serial port register names for the selected SERIAL_PORT (C preprocessor +// requires two levels of indirection to expand macro values properly) +#define SERIAL_REGNAME(registerbase,number,suffix) SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) +#if SERIAL_PORT == 0 && (!defined(UBRR0H) || !defined(UDR0)) // use un-numbered registers if necessary +#define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##suffix +#else +#define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##number##suffix +#endif + +// Registers used by MarlinSerial class (these are expanded +// depending on selected serial port +#define M_UCSRxA SERIAL_REGNAME(UCSR,SERIAL_PORT,A) // defines M_UCSRxA to be UCSRnA where n is the serial port number +#define M_UCSRxB SERIAL_REGNAME(UCSR,SERIAL_PORT,B) +#define M_RXENx SERIAL_REGNAME(RXEN,SERIAL_PORT,) +#define M_TXENx SERIAL_REGNAME(TXEN,SERIAL_PORT,) +#define M_RXCIEx SERIAL_REGNAME(RXCIE,SERIAL_PORT,) +#define M_UDREx SERIAL_REGNAME(UDRE,SERIAL_PORT,) +#define M_UDRx SERIAL_REGNAME(UDR,SERIAL_PORT,) +#define M_UBRRxH SERIAL_REGNAME(UBRR,SERIAL_PORT,H) +#define M_UBRRxL SERIAL_REGNAME(UBRR,SERIAL_PORT,L) +#define M_RXCx SERIAL_REGNAME(RXC,SERIAL_PORT,) +#define M_FEx SERIAL_REGNAME(FE,SERIAL_PORT,) +#define M_USARTx_RX_vect SERIAL_REGNAME(USART,SERIAL_PORT,_RX_vect) +#define M_U2Xx SERIAL_REGNAME(U2X,SERIAL_PORT,) + + + +#define DEC 10 +#define HEX 16 +#define OCT 8 +#define BIN 2 +#define BYTE 0 + + +#ifndef AT90USB +// Define constants and variables for buffering incoming serial data. We're +// using a ring buffer (I think), in which rx_buffer_head is the index of the +// location to which to write the next incoming character and rx_buffer_tail +// is the index of the location from which to read. +#define RX_BUFFER_SIZE 128 + +extern uint8_t selectedSerialPort; + +struct ring_buffer +{ + unsigned char buffer[RX_BUFFER_SIZE]; + int head; + int tail; +}; + +#if UART_PRESENT(SERIAL_PORT) + extern ring_buffer rx_buffer; +#endif + +class MarlinSerial //: public Stream +{ + + public: + static void begin(long); + static void end(); + static int peek(void); + static int read(void); + static void flush(void); + + static /*FORCE_INLINE*/ int available(void) + { + return (unsigned int)(RX_BUFFER_SIZE + rx_buffer.head - rx_buffer.tail) % RX_BUFFER_SIZE; + } + /* + FORCE_INLINE void write(uint8_t c) + { + while (!((M_UCSRxA) & (1 << M_UDREx))) + ; + + M_UDRx = c; + } + */ + static void write(uint8_t c) + { + if (selectedSerialPort == 0) + { + while (!((M_UCSRxA) & (1 << M_UDREx))); + M_UDRx = c; + } + else if (selectedSerialPort == 1) + { + while (!((UCSR1A) & (1 << UDRE1))); + UDR1 = c; + } + } + + static void checkRx(void) + { + if (selectedSerialPort == 0) { + if((M_UCSRxA & (1<. + * + * @section notes_sec Notes + * + * * Do not create static objects in global functions. + * Otherwise constructor guard against concurrent calls is generated costing + * about 8B RAM and 14B flash. + * + * + */ + +//-// +#include "Configuration.h" +#include "Marlin.h" + +#ifdef ENABLE_AUTO_BED_LEVELING +#include "vector_3.h" + #ifdef AUTO_BED_LEVELING_GRID + #include "qr_solve.h" + #endif +#endif // ENABLE_AUTO_BED_LEVELING + +#ifdef MESH_BED_LEVELING + #include "mesh_bed_leveling.h" + #include "mesh_bed_calibration.h" +#endif + +#include "printers.h" + +#include "menu.h" +#include "ultralcd.h" + +#include "planner.h" +#include "stepper.h" +#include "temperature.h" +#include "motion_control.h" +#include "cardreader.h" +#include "ConfigurationStore.h" +#include "language.h" +#include "pins_arduino.h" +#include "math.h" +#include "util.h" +#include "Timer.h" + +#include +#include + +#include "Dcodes.h" +#include "AutoDeplete.h" + + +#ifdef SWSPI +#include "swspi.h" +#endif //SWSPI + +#include "spi.h" + +#ifdef SWI2C +#include "swi2c.h" +#endif //SWI2C + +#ifdef FILAMENT_SENSOR +#include "fsensor.h" +#endif //FILAMENT_SENSOR + +#ifdef TMC2130 +#include "tmc2130.h" +#endif //TMC2130 + +#ifdef W25X20CL +#include "w25x20cl.h" +#include "optiboot_w25x20cl.h" +#endif //W25X20CL + +#ifdef BLINKM +#include "BlinkM.h" +#include "Wire.h" +#endif + +#ifdef ULTRALCD +#include "ultralcd.h" +#endif + +#if NUM_SERVOS > 0 +#include "Servo.h" +#endif + +#if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1 +#include +#endif + +#include "mmu.h" + +#define VERSION_STRING "1.0.2" + + +#include "ultralcd.h" +#include "sound.h" + +#include "cmdqueue.h" +#include "io_atmega2560.h" + +// Macros for bit masks +#define BIT(b) (1<<(b)) +#define TEST(n,b) (((n)&BIT(b))!=0) +#define SET_BIT(n,b,value) (n) ^= ((-value)^(n)) & (BIT(b)) + +//Macro for print fan speed +#define FAN_PULSE_WIDTH_LIMIT ((fanSpeed > 100) ? 3 : 4) //time in ms + +//filament types +#define FILAMENT_DEFAULT 0 +#define FILAMENT_FLEX 1 +#define FILAMENT_PVA 2 +#define FILAMENT_UNDEFINED 255 + +//Stepper Movement Variables + +//=========================================================================== +//=============================imported variables============================ +//=========================================================================== + +//=========================================================================== +//=============================public variables============================= +//=========================================================================== +#ifdef SDSUPPORT +CardReader card; +#endif + +unsigned long PingTime = _millis(); +unsigned long NcTime; + +uint8_t mbl_z_probe_nr = 3; //numer of Z measurements for each point in mesh bed leveling calibration + +//used for PINDA temp calibration and pause print +#define DEFAULT_RETRACTION 1 +#define DEFAULT_RETRACTION_MM 4 //MM + +float default_retraction = DEFAULT_RETRACTION; + + +float homing_feedrate[] = HOMING_FEEDRATE; +// Currently only the extruder axis may be switched to a relative mode. +// Other axes are always absolute or relative based on the common relative_mode flag. +bool axis_relative_modes[] = AXIS_RELATIVE_MODES; +int feedmultiply=100; //100->1 200->2 +int extrudemultiply=100; //100->1 200->2 +int extruder_multiply[EXTRUDERS] = {100 + #if EXTRUDERS > 1 + , 100 + #if EXTRUDERS > 2 + , 100 + #endif + #endif +}; + +int bowden_length[4] = {385, 385, 385, 385}; + +bool is_usb_printing = false; +bool homing_flag = false; + +bool temp_cal_active = false; + +unsigned long kicktime = _millis()+100000; + +unsigned int usb_printing_counter; + +int8_t lcd_change_fil_state = 0; + +unsigned long pause_time = 0; +unsigned long start_pause_print = _millis(); +unsigned long t_fan_rising_edge = _millis(); +LongTimer safetyTimer; +static LongTimer crashDetTimer; + +//unsigned long load_filament_time; + +bool mesh_bed_leveling_flag = false; +bool mesh_bed_run_from_menu = false; + +bool prusa_sd_card_upload = false; + +unsigned int status_number = 0; + +unsigned long total_filament_used; +unsigned int heating_status; +unsigned int heating_status_counter; +bool loading_flag = false; + + + +char snmm_filaments_used = 0; + + +bool fan_state[2]; +int fan_edge_counter[2]; +int fan_speed[2]; + +char dir_names[3][9]; + +bool sortAlpha = false; + + +float extruder_multiplier[EXTRUDERS] = {1.0 + #if EXTRUDERS > 1 + , 1.0 + #if EXTRUDERS > 2 + , 1.0 + #endif + #endif +}; + +float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0 }; +//shortcuts for more readable code +#define _x current_position[X_AXIS] +#define _y current_position[Y_AXIS] +#define _z current_position[Z_AXIS] +#define _e current_position[E_AXIS] + +float min_pos[3] = { X_MIN_POS, Y_MIN_POS, Z_MIN_POS }; +float max_pos[3] = { X_MAX_POS, Y_MAX_POS, Z_MAX_POS }; +bool axis_known_position[3] = {false, false, false}; + +// Extruder offset +#if EXTRUDERS > 1 + #define NUM_EXTRUDER_OFFSETS 2 // only in XY plane +float extruder_offset[NUM_EXTRUDER_OFFSETS][EXTRUDERS] = { +#if defined(EXTRUDER_OFFSET_X) && defined(EXTRUDER_OFFSET_Y) + EXTRUDER_OFFSET_X, EXTRUDER_OFFSET_Y +#endif +}; +#endif + +uint8_t active_extruder = 0; +int fanSpeed=0; + +#ifdef FWRETRACT + bool retracted[EXTRUDERS]={false + #if EXTRUDERS > 1 + , false + #if EXTRUDERS > 2 + , false + #endif + #endif + }; + bool retracted_swap[EXTRUDERS]={false + #if EXTRUDERS > 1 + , false + #if EXTRUDERS > 2 + , false + #endif + #endif + }; + + float retract_length_swap = RETRACT_LENGTH_SWAP; + float retract_recover_length_swap = RETRACT_RECOVER_LENGTH_SWAP; +#endif + + #ifdef PS_DEFAULT_OFF + bool powersupply = false; + #else + bool powersupply = true; + #endif + +bool cancel_heatup = false ; + +int8_t busy_state = NOT_BUSY; +static long prev_busy_signal_ms = -1; +uint8_t host_keepalive_interval = HOST_KEEPALIVE_INTERVAL; + +const char errormagic[] PROGMEM = "Error:"; +const char echomagic[] PROGMEM = "echo:"; + +bool no_response = false; +uint8_t important_status; +uint8_t saved_filament_type; + + +// save/restore printing in case that mmu was not responding +bool mmu_print_saved = false; + +// storing estimated time to end of print counted by slicer +uint8_t print_percent_done_normal = PRINT_PERCENT_DONE_INIT; +uint16_t print_time_remaining_normal = PRINT_TIME_REMAINING_INIT; //estimated remaining print time in minutes +uint8_t print_percent_done_silent = PRINT_PERCENT_DONE_INIT; +uint16_t print_time_remaining_silent = PRINT_TIME_REMAINING_INIT; //estimated remaining print time in minutes + +//=========================================================================== +//=============================Private Variables============================= +//=========================================================================== +#define MSG_BED_LEVELING_FAILED_TIMEOUT 30 + +const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'}; +float destination[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0}; + +// For tracing an arc +static float offset[3] = {0.0, 0.0, 0.0}; +static float feedrate = 1500.0, next_feedrate, saved_feedrate; + +// Determines Absolute or Relative Coordinates. +// Also there is bool axis_relative_modes[] per axis flag. +static bool relative_mode = false; + +const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42 + +//static float tt = 0; +//static float bt = 0; + +//Inactivity shutdown variables +static unsigned long previous_millis_cmd = 0; +unsigned long max_inactive_time = 0; +static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME*1000l; +static unsigned long safetytimer_inactive_time = DEFAULT_SAFETYTIMER_TIME_MINS*60*1000ul; + +unsigned long starttime=0; +unsigned long stoptime=0; +unsigned long _usb_timer = 0; + +bool extruder_under_pressure = true; + + +bool Stopped=false; + +#if NUM_SERVOS > 0 + Servo servos[NUM_SERVOS]; +#endif + +bool target_direction; + +//Insert variables if CHDK is defined +#ifdef CHDK +unsigned long chdkHigh = 0; +boolean chdkActive = false; +#endif + +//! @name RAM save/restore printing +//! @{ +bool saved_printing = false; //!< Print is paused and saved in RAM +static uint32_t saved_sdpos = 0; //!< SD card position, or line number in case of USB printing +uint8_t saved_printing_type = PRINTING_TYPE_SD; +static float saved_pos[4] = { 0, 0, 0, 0 }; +//! Feedrate hopefully derived from an active block of the planner at the time the print has been canceled, in mm/min. +static float saved_feedrate2 = 0; +static uint8_t saved_active_extruder = 0; +static float saved_extruder_temperature = 0.0; //!< Active extruder temperature +static bool saved_extruder_under_pressure = false; +static bool saved_extruder_relative_mode = false; +static int saved_fanSpeed = 0; //!< Print fan speed +//! @} + +static int saved_feedmultiply_mm = 100; + +//=========================================================================== +//=============================Routines====================================== +//=========================================================================== + +static void get_arc_coordinates(); +static bool setTargetedHotend(int code, uint8_t &extruder); +static void print_time_remaining_init(); +static void wait_for_heater(long codenum, uint8_t extruder); +static void gcode_G28(bool home_x_axis, bool home_y_axis, bool home_z_axis); +static void temp_compensation_start(); +static void temp_compensation_apply(); + + +uint16_t gcode_in_progress = 0; +uint16_t mcode_in_progress = 0; + +void serial_echopair_P(const char *s_P, float v) + { serialprintPGM(s_P); SERIAL_ECHO(v); } +void serial_echopair_P(const char *s_P, double v) + { serialprintPGM(s_P); SERIAL_ECHO(v); } +void serial_echopair_P(const char *s_P, unsigned long v) + { serialprintPGM(s_P); SERIAL_ECHO(v); } + +/*FORCE_INLINE*/ void serialprintPGM(const char *str) +{ +#if 0 + char ch=pgm_read_byte(str); + while(ch) + { + MYSERIAL.write(ch); + ch=pgm_read_byte(++str); + } +#else + // hmm, same size as the above version, the compiler did a good job optimizing the above + while( uint8_t ch = pgm_read_byte(str) ){ + MYSERIAL.write((char)ch); + ++str; + } +#endif +} + +#ifdef SDSUPPORT + #include "SdFatUtil.h" + int freeMemory() { return SdFatUtil::FreeRam(); } +#else + extern "C" { + extern unsigned int __bss_end; + extern unsigned int __heap_start; + extern void *__brkval; + + int freeMemory() { + int free_memory; + + if ((int)__brkval == 0) + free_memory = ((int)&free_memory) - ((int)&__bss_end); + else + free_memory = ((int)&free_memory) - ((int)__brkval); + + return free_memory; + } + } +#endif //!SDSUPPORT + +void setup_killpin() +{ + #if defined(KILL_PIN) && KILL_PIN > -1 + SET_INPUT(KILL_PIN); + WRITE(KILL_PIN,HIGH); + #endif +} + +// Set home pin +void setup_homepin(void) +{ +#if defined(HOME_PIN) && HOME_PIN > -1 + SET_INPUT(HOME_PIN); + WRITE(HOME_PIN,HIGH); +#endif +} + +void setup_photpin() +{ + #if defined(PHOTOGRAPH_PIN) && PHOTOGRAPH_PIN > -1 + SET_OUTPUT(PHOTOGRAPH_PIN); + WRITE(PHOTOGRAPH_PIN, LOW); + #endif +} + +void setup_powerhold() +{ + #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1 + SET_OUTPUT(SUICIDE_PIN); + WRITE(SUICIDE_PIN, HIGH); + #endif + #if defined(PS_ON_PIN) && PS_ON_PIN > -1 + SET_OUTPUT(PS_ON_PIN); + #if defined(PS_DEFAULT_OFF) + WRITE(PS_ON_PIN, PS_ON_ASLEEP); + #else + WRITE(PS_ON_PIN, PS_ON_AWAKE); + #endif + #endif +} + +void suicide() +{ + #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1 + SET_OUTPUT(SUICIDE_PIN); + WRITE(SUICIDE_PIN, LOW); + #endif +} + +void servo_init() +{ + #if (NUM_SERVOS >= 1) && defined(SERVO0_PIN) && (SERVO0_PIN > -1) + servos[0].attach(SERVO0_PIN); + #endif + #if (NUM_SERVOS >= 2) && defined(SERVO1_PIN) && (SERVO1_PIN > -1) + servos[1].attach(SERVO1_PIN); + #endif + #if (NUM_SERVOS >= 3) && defined(SERVO2_PIN) && (SERVO2_PIN > -1) + servos[2].attach(SERVO2_PIN); + #endif + #if (NUM_SERVOS >= 4) && defined(SERVO3_PIN) && (SERVO3_PIN > -1) + servos[3].attach(SERVO3_PIN); + #endif + #if (NUM_SERVOS >= 5) + #error "TODO: enter initalisation code for more servos" + #endif +} + + +bool fans_check_enabled = true; + +#ifdef TMC2130 + +void crashdet_stop_and_save_print() +{ + stop_and_save_print_to_ram(10, -default_retraction); //XY - no change, Z 10mm up, E -1mm retract +} + +void crashdet_restore_print_and_continue() +{ + restore_print_from_ram_and_continue(default_retraction); //XYZ = orig, E +1mm unretract +// babystep_apply(); +} + + +void crashdet_stop_and_save_print2() +{ + cli(); + planner_abort_hard(); //abort printing + cmdqueue_reset(); //empty cmdqueue + card.sdprinting = false; + card.closefile(); + // Reset and re-enable the stepper timer just before the global interrupts are enabled. + st_reset_timer(); + sei(); +} + +void crashdet_detected(uint8_t mask) +{ + st_synchronize(); + static uint8_t crashDet_counter = 0; + bool automatic_recovery_after_crash = true; + + if (crashDet_counter++ == 0) { + crashDetTimer.start(); + } + else if (crashDetTimer.expired(CRASHDET_TIMER * 1000ul)){ + crashDetTimer.stop(); + crashDet_counter = 0; + } + else if(crashDet_counter == CRASHDET_COUNTER_MAX){ + automatic_recovery_after_crash = false; + crashDetTimer.stop(); + crashDet_counter = 0; + } + else { + crashDetTimer.start(); + } + + lcd_update_enable(true); + lcd_clear(); + lcd_update(2); + + if (mask & X_AXIS_MASK) + { + eeprom_update_byte((uint8_t*)EEPROM_CRASH_COUNT_X, eeprom_read_byte((uint8_t*)EEPROM_CRASH_COUNT_X) + 1); + eeprom_update_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT, eeprom_read_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT) + 1); + } + if (mask & Y_AXIS_MASK) + { + eeprom_update_byte((uint8_t*)EEPROM_CRASH_COUNT_Y, eeprom_read_byte((uint8_t*)EEPROM_CRASH_COUNT_Y) + 1); + eeprom_update_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT, eeprom_read_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT) + 1); + } + + + + lcd_update_enable(true); + lcd_update(2); + lcd_setstatuspgm(_T(MSG_CRASH_DETECTED)); + gcode_G28(true, true, false); //home X and Y + st_synchronize(); + + if (automatic_recovery_after_crash) { + enquecommand_P(PSTR("CRASH_RECOVER")); + }else{ + setTargetHotend(0, active_extruder); + bool yesno = lcd_show_fullscreen_message_yes_no_and_wait_P(_i("Crash detected. Resume print?"), false); + lcd_update_enable(true); + if (yesno) + { + enquecommand_P(PSTR("CRASH_RECOVER")); + } + else + { + enquecommand_P(PSTR("CRASH_CANCEL")); + } + } +} + +void crashdet_recover() +{ + crashdet_restore_print_and_continue(); + if (lcd_crash_detect_enabled()) tmc2130_sg_stop_on_crash = true; +} + +void crashdet_cancel() +{ + saved_printing = false; + tmc2130_sg_stop_on_crash = true; + if (saved_printing_type == PRINTING_TYPE_SD) { + lcd_print_stop(); + }else if(saved_printing_type == PRINTING_TYPE_USB){ + SERIAL_ECHOLNRPGM(MSG_OCTOPRINT_CANCEL); //for Octoprint: works the same as clicking "Abort" button in Octoprint GUI + SERIAL_PROTOCOLLNRPGM(MSG_OK); + } +} + +#endif //TMC2130 + +void failstats_reset_print() +{ + eeprom_update_byte((uint8_t *)EEPROM_CRASH_COUNT_X, 0); + eeprom_update_byte((uint8_t *)EEPROM_CRASH_COUNT_Y, 0); + eeprom_update_byte((uint8_t *)EEPROM_FERROR_COUNT, 0); + eeprom_update_byte((uint8_t *)EEPROM_POWER_COUNT, 0); + eeprom_update_byte((uint8_t *)EEPROM_MMU_FAIL, 0); + eeprom_update_byte((uint8_t *)EEPROM_MMU_LOAD_FAIL, 0); +} + + + +#ifdef MESH_BED_LEVELING + enum MeshLevelingState { MeshReport, MeshStart, MeshNext, MeshSet }; +#endif + + +// Factory reset function +// This function is used to erase parts or whole EEPROM memory which is used for storing calibration and and so on. +// Level input parameter sets depth of reset +int er_progress = 0; +static void factory_reset(char level) +{ + lcd_clear(); + switch (level) { + + // Level 0: Language reset + case 0: + Sound_MakeCustom(100,0,false); + lang_reset(); + break; + + //Level 1: Reset statistics + case 1: + Sound_MakeCustom(100,0,false); + eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, 0); + eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, 0); + + eeprom_update_byte((uint8_t *)EEPROM_CRASH_COUNT_X, 0); + eeprom_update_byte((uint8_t *)EEPROM_CRASH_COUNT_Y, 0); + eeprom_update_byte((uint8_t *)EEPROM_FERROR_COUNT, 0); + eeprom_update_byte((uint8_t *)EEPROM_POWER_COUNT, 0); + + eeprom_update_word((uint16_t *)EEPROM_CRASH_COUNT_X_TOT, 0); + eeprom_update_word((uint16_t *)EEPROM_CRASH_COUNT_Y_TOT, 0); + eeprom_update_word((uint16_t *)EEPROM_FERROR_COUNT_TOT, 0); + eeprom_update_word((uint16_t *)EEPROM_POWER_COUNT_TOT, 0); + + eeprom_update_word((uint16_t *)EEPROM_MMU_FAIL_TOT, 0); + eeprom_update_word((uint16_t *)EEPROM_MMU_LOAD_FAIL_TOT, 0); + eeprom_update_byte((uint8_t *)EEPROM_MMU_FAIL, 0); + eeprom_update_byte((uint8_t *)EEPROM_MMU_LOAD_FAIL, 0); + + + lcd_menu_statistics(); + + break; + + // Level 2: Prepare for shipping + case 2: + //lcd_puts_P(PSTR("Factory RESET")); + //lcd_puts_at_P(1,2,PSTR("Shipping prep")); + + // Force language selection at the next boot up. + lang_reset(); + // Force the "Follow calibration flow" message at the next boot up. + calibration_status_store(CALIBRATION_STATUS_Z_CALIBRATION); + eeprom_write_byte((uint8_t*)EEPROM_WIZARD_ACTIVE, 1); //run wizard + farm_no = 0; + farm_mode = false; + eeprom_update_byte((uint8_t*)EEPROM_FARM_MODE, farm_mode); + EEPROM_save_B(EEPROM_FARM_NUMBER, &farm_no); + + eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, 0); + eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, 0); + eeprom_update_word((uint16_t *)EEPROM_CRASH_COUNT_X_TOT, 0); + eeprom_update_word((uint16_t *)EEPROM_CRASH_COUNT_Y_TOT, 0); + eeprom_update_word((uint16_t *)EEPROM_FERROR_COUNT_TOT, 0); + eeprom_update_word((uint16_t *)EEPROM_POWER_COUNT_TOT, 0); + + eeprom_update_word((uint16_t *)EEPROM_MMU_FAIL_TOT, 0); + eeprom_update_word((uint16_t *)EEPROM_MMU_LOAD_FAIL_TOT, 0); + eeprom_update_byte((uint8_t *)EEPROM_MMU_FAIL, 0); + eeprom_update_byte((uint8_t *)EEPROM_MMU_LOAD_FAIL, 0); + +#ifdef FILAMENT_SENSOR + fsensor_enable(); + fsensor_autoload_set(true); +#endif //FILAMENT_SENSOR + Sound_MakeCustom(100,0,false); + //_delay_ms(2000); + break; + + // Level 3: erase everything, whole EEPROM will be set to 0xFF + + case 3: + lcd_puts_P(PSTR("Factory RESET")); + lcd_puts_at_P(1, 2, PSTR("ERASING all data")); + + Sound_MakeCustom(100,0,false); + er_progress = 0; + lcd_puts_at_P(3, 3, PSTR(" ")); + lcd_set_cursor(3, 3); + lcd_print(er_progress); + + // Erase EEPROM + for (int i = 0; i < 4096; i++) { + eeprom_update_byte((uint8_t*)i, 0xFF); + + if (i % 41 == 0) { + er_progress++; + lcd_puts_at_P(3, 3, PSTR(" ")); + lcd_set_cursor(3, 3); + lcd_print(er_progress); + lcd_puts_P(PSTR("%")); + } + + } + + + break; + case 4: + bowden_menu(); + break; + + default: + break; + } + + +} + +extern "C" { +FILE _uartout; //= {0}; Global variable is always zero initialized. No need to explicitly state this. +} + +int uart_putchar(char c, FILE *) +{ + MYSERIAL.write(c); + return 0; +} + + +void lcd_splash() +{ + lcd_clear(); // clears display and homes screen + lcd_puts_P(PSTR("\n Original Prusa i3\n Prusa Research")); +} + + +void factory_reset() +{ + KEEPALIVE_STATE(PAUSED_FOR_USER); + if (!READ(BTN_ENC)) + { + _delay_ms(1000); + if (!READ(BTN_ENC)) + { + lcd_clear(); + + + lcd_puts_P(PSTR("Factory RESET")); + + + SET_OUTPUT(BEEPER); + if(eSoundMode!=e_SOUND_MODE_SILENT) + WRITE(BEEPER, HIGH); + + while (!READ(BTN_ENC)); + + WRITE(BEEPER, LOW); + + + + _delay_ms(2000); + + char level = reset_menu(); + factory_reset(level); + + switch (level) { + case 0: _delay_ms(0); break; + case 1: _delay_ms(0); break; + case 2: _delay_ms(0); break; + case 3: _delay_ms(0); break; + } + + } + } + KEEPALIVE_STATE(IN_HANDLER); +} + +void show_fw_version_warnings() { + if (FW_DEV_VERSION == FW_VERSION_GOLD || FW_DEV_VERSION == FW_VERSION_RC) return; + switch (FW_DEV_VERSION) { + case(FW_VERSION_ALPHA): lcd_show_fullscreen_message_and_wait_P(_i("You are using firmware alpha version. This is development version. Using this version is not recommended and may cause printer damage.")); break;////MSG_FW_VERSION_ALPHA c=20 r=8 + case(FW_VERSION_BETA): lcd_show_fullscreen_message_and_wait_P(_i("You are using firmware beta version. This is development version. Using this version is not recommended and may cause printer damage.")); break;////MSG_FW_VERSION_BETA c=20 r=8 + case(FW_VERSION_DEVEL): + case(FW_VERSION_DEBUG): + lcd_update_enable(false); + lcd_clear(); + #if FW_DEV_VERSION == FW_VERSION_DEVEL + lcd_puts_at_P(0, 0, PSTR("Development build !!")); + #else + lcd_puts_at_P(0, 0, PSTR("Debbugging build !!!")); + #endif + lcd_puts_at_P(0, 1, PSTR("May destroy printer!")); + lcd_puts_at_P(0, 2, PSTR("ver ")); lcd_puts_P(PSTR(FW_VERSION_FULL)); + lcd_puts_at_P(0, 3, PSTR(FW_REPOSITORY)); + lcd_wait_for_click(); + break; +// default: lcd_show_fullscreen_message_and_wait_P(_i("WARNING: This is an unofficial, unsupported build. Use at your own risk!")); break;////MSG_FW_VERSION_UNKNOWN c=20 r=8 + } + lcd_update_enable(true); +} + +//! @brief try to check if firmware is on right type of printer +static void check_if_fw_is_on_right_printer(){ +#ifdef FILAMENT_SENSOR + if((PRINTER_TYPE == PRINTER_MK3) || (PRINTER_TYPE == PRINTER_MK3S)){ + #ifdef IR_SENSOR + swi2c_init(); + const uint8_t pat9125_detected = swi2c_readByte_A8(PAT9125_I2C_ADDR,0x00,NULL); + if (pat9125_detected){ + lcd_show_fullscreen_message_and_wait_P(_i("MK3S firmware detected on MK3 printer"));} + #endif //IR_SENSOR + + #ifdef PAT9125 + //will return 1 only if IR can detect filament in bondtech extruder so this may fail even when we have IR sensor + const uint8_t ir_detected = !(PIN_GET(IR_SENSOR_PIN)); + if (ir_detected){ + lcd_show_fullscreen_message_and_wait_P(_i("MK3 firmware detected on MK3S printer"));} + #endif //PAT9125 + } +#endif //FILAMENT_SENSOR +} + +uint8_t check_printer_version() +{ + uint8_t version_changed = 0; + uint16_t printer_type = eeprom_read_word((uint16_t*)EEPROM_PRINTER_TYPE); + uint16_t motherboard = eeprom_read_word((uint16_t*)EEPROM_BOARD_TYPE); + + if (printer_type != PRINTER_TYPE) { + if (printer_type == 0xffff) eeprom_write_word((uint16_t*)EEPROM_PRINTER_TYPE, PRINTER_TYPE); + else version_changed |= 0b10; + } + if (motherboard != MOTHERBOARD) { + if(motherboard == 0xffff) eeprom_write_word((uint16_t*)EEPROM_BOARD_TYPE, MOTHERBOARD); + else version_changed |= 0b01; + } + return version_changed; +} + +#ifdef BOOTAPP +#include "bootapp.h" //bootloader support +#endif //BOOTAPP + +#if (LANG_MODE != 0) //secondary language support + +#ifdef W25X20CL + + +// language update from external flash +#define LANGBOOT_BLOCKSIZE 0x1000u +#define LANGBOOT_RAMBUFFER 0x0800 + +void update_sec_lang_from_external_flash() +{ + if ((boot_app_magic == BOOT_APP_MAGIC) && (boot_app_flags & BOOT_APP_FLG_USER0)) + { + uint8_t lang = boot_reserved >> 4; + uint8_t state = boot_reserved & 0xf; + lang_table_header_t header; + uint32_t src_addr; + if (lang_get_header(lang, &header, &src_addr)) + { + lcd_puts_at_P(1,3,PSTR("Language update.")); + for (uint8_t i = 0; i < state; i++) fputc('.', lcdout); + _delay(100); + boot_reserved = (state + 1) | (lang << 4); + if ((state * LANGBOOT_BLOCKSIZE) < header.size) + { + cli(); + uint16_t size = header.size - state * LANGBOOT_BLOCKSIZE; + if (size > LANGBOOT_BLOCKSIZE) size = LANGBOOT_BLOCKSIZE; + w25x20cl_rd_data(src_addr + state * LANGBOOT_BLOCKSIZE, (uint8_t*)LANGBOOT_RAMBUFFER, size); + if (state == 0) + { + //TODO - check header integrity + } + bootapp_ram2flash(LANGBOOT_RAMBUFFER, _SEC_LANG_TABLE + state * LANGBOOT_BLOCKSIZE, size); + } + else + { + //TODO - check sec lang data integrity + eeprom_update_byte((unsigned char *)EEPROM_LANG, LANG_ID_SEC); + } + } + } + boot_app_flags &= ~BOOT_APP_FLG_USER0; +} + + +#ifdef DEBUG_W25X20CL + +uint8_t lang_xflash_enum_codes(uint16_t* codes) +{ + lang_table_header_t header; + uint8_t count = 0; + uint32_t addr = 0x00000; + while (1) + { + printf_P(_n("LANGTABLE%d:"), count); + w25x20cl_rd_data(addr, (uint8_t*)&header, sizeof(lang_table_header_t)); + if (header.magic != LANG_MAGIC) + { + printf_P(_n("NG!\n")); + break; + } + printf_P(_n("OK\n")); + printf_P(_n(" _lt_magic = 0x%08lx %S\n"), header.magic, (header.magic==LANG_MAGIC)?_n("OK"):_n("NA")); + printf_P(_n(" _lt_size = 0x%04x (%d)\n"), header.size, header.size); + printf_P(_n(" _lt_count = 0x%04x (%d)\n"), header.count, header.count); + printf_P(_n(" _lt_chsum = 0x%04x\n"), header.checksum); + printf_P(_n(" _lt_code = 0x%04x (%c%c)\n"), header.code, header.code >> 8, header.code & 0xff); + printf_P(_n(" _lt_sign = 0x%08lx\n"), header.signature); + + addr += header.size; + codes[count] = header.code; + count ++; + } + return count; +} + +void list_sec_lang_from_external_flash() +{ + uint16_t codes[8]; + uint8_t count = lang_xflash_enum_codes(codes); + printf_P(_n("XFlash lang count = %hhd\n"), count); +} + +#endif //DEBUG_W25X20CL + +#endif //W25X20CL + +#endif //(LANG_MODE != 0) + + +static void w25x20cl_err_msg() +{ + lcd_clear(); + lcd_puts_P(_n("External SPI flash\nW25X20CL is not res-\nponding. Language\nswitch unavailable.")); +} + +// "Setup" function is called by the Arduino framework on startup. +// Before startup, the Timers-functions (PWM)/Analog RW and HardwareSerial provided by the Arduino-code +// are initialized by the main() routine provided by the Arduino framework. +void setup() +{ + mmu_init(); + + ultralcd_init(); + +#if (LCD_BL_PIN != -1) && defined (LCD_BL_PIN) + analogWrite(LCD_BL_PIN, 255); //set full brightnes +#endif //(LCD_BL_PIN != -1) && defined (LCD_BL_PIN) + + spi_init(); + + lcd_splash(); + Sound_Init(); // also guarantee "SET_OUTPUT(BEEPER)" + +#ifdef W25X20CL + bool w25x20cl_success = w25x20cl_init(); + if (w25x20cl_success) + { + optiboot_w25x20cl_enter(); +#if (LANG_MODE != 0) //secondary language support + update_sec_lang_from_external_flash(); +#endif //(LANG_MODE != 0) + } + else + { + w25x20cl_err_msg(); + } +#else + const bool w25x20cl_success = true; +#endif //W25X20CL + + + setup_killpin(); + setup_powerhold(); + + farm_mode = eeprom_read_byte((uint8_t*)EEPROM_FARM_MODE); + EEPROM_read_B(EEPROM_FARM_NUMBER, &farm_no); + if ((farm_mode == 0xFF && farm_no == 0) || ((uint16_t)farm_no == 0xFFFF)) + farm_mode = false; //if farm_mode has not been stored to eeprom yet and farm number is set to zero or EEPROM is fresh, deactivate farm mode + if ((uint16_t)farm_no == 0xFFFF) farm_no = 0; + + selectedSerialPort = eeprom_read_byte((uint8_t*)EEPROM_SECOND_SERIAL_ACTIVE); + if (selectedSerialPort == 0xFF) selectedSerialPort = 0; + if (farm_mode) + { + no_response = true; //we need confirmation by recieving PRUSA thx + important_status = 8; + prusa_statistics(8); + selectedSerialPort = 1; +#ifdef TMC2130 + //increased extruder current (PFW363) + tmc2130_current_h[E_AXIS] = 36; + tmc2130_current_r[E_AXIS] = 36; +#endif //TMC2130 +#ifdef FILAMENT_SENSOR + //disabled filament autoload (PFW360) + fsensor_autoload_set(false); +#endif //FILAMENT_SENSOR + // ~ FanCheck -> on + if(!(eeprom_read_byte((uint8_t*)EEPROM_FAN_CHECK_ENABLED))) + eeprom_update_byte((unsigned char *)EEPROM_FAN_CHECK_ENABLED,true); + } + MYSERIAL.begin(BAUDRATE); + fdev_setup_stream(uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE); //setup uart out stream +#ifndef W25X20CL + SERIAL_PROTOCOLLNPGM("start"); +#endif //W25X20CL + stdout = uartout; + SERIAL_ECHO_START; + printf_P(PSTR(" " FW_VERSION_FULL "\n")); + + //SERIAL_ECHOPAIR("Active sheet before:", static_cast(eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)))); + +#ifdef DEBUG_SEC_LANG + lang_table_header_t header; + uint32_t src_addr = 0x00000; + if (lang_get_header(1, &header, &src_addr)) + { +//this is comparsion of some printing-methods regarding to flash space usage and code size/readability +#define LT_PRINT_TEST 2 +// flash usage +// total p.test +//0 252718 t+c text code +//1 253142 424 170 254 +//2 253040 322 164 158 +//3 253248 530 135 395 +#if (LT_PRINT_TEST==1) //not optimized printf + printf_P(_n(" _src_addr = 0x%08lx\n"), src_addr); + printf_P(_n(" _lt_magic = 0x%08lx %S\n"), header.magic, (header.magic==LANG_MAGIC)?_n("OK"):_n("NA")); + printf_P(_n(" _lt_size = 0x%04x (%d)\n"), header.size, header.size); + printf_P(_n(" _lt_count = 0x%04x (%d)\n"), header.count, header.count); + printf_P(_n(" _lt_chsum = 0x%04x\n"), header.checksum); + printf_P(_n(" _lt_code = 0x%04x (%c%c)\n"), header.code, header.code >> 8, header.code & 0xff); + printf_P(_n(" _lt_sign = 0x%08lx\n"), header.signature); +#elif (LT_PRINT_TEST==2) //optimized printf + printf_P( + _n( + " _src_addr = 0x%08lx\n" + " _lt_magic = 0x%08lx %S\n" + " _lt_size = 0x%04x (%d)\n" + " _lt_count = 0x%04x (%d)\n" + " _lt_chsum = 0x%04x\n" + " _lt_code = 0x%04x (%c%c)\n" + " _lt_resv1 = 0x%08lx\n" + ), + src_addr, + header.magic, (header.magic==LANG_MAGIC)?_n("OK"):_n("NA"), + header.size, header.size, + header.count, header.count, + header.checksum, + header.code, header.code >> 8, header.code & 0xff, + header.signature + ); +#elif (LT_PRINT_TEST==3) //arduino print/println (leading zeros not solved) + MYSERIAL.print(" _src_addr = 0x"); + MYSERIAL.println(src_addr, 16); + MYSERIAL.print(" _lt_magic = 0x"); + MYSERIAL.print(header.magic, 16); + MYSERIAL.println((header.magic==LANG_MAGIC)?" OK":" NA"); + MYSERIAL.print(" _lt_size = 0x"); + MYSERIAL.print(header.size, 16); + MYSERIAL.print(" ("); + MYSERIAL.print(header.size, 10); + MYSERIAL.println(")"); + MYSERIAL.print(" _lt_count = 0x"); + MYSERIAL.print(header.count, 16); + MYSERIAL.print(" ("); + MYSERIAL.print(header.count, 10); + MYSERIAL.println(")"); + MYSERIAL.print(" _lt_chsum = 0x"); + MYSERIAL.println(header.checksum, 16); + MYSERIAL.print(" _lt_code = 0x"); + MYSERIAL.print(header.code, 16); + MYSERIAL.print(" ("); + MYSERIAL.print((char)(header.code >> 8), 0); + MYSERIAL.print((char)(header.code & 0xff), 0); + MYSERIAL.println(")"); + MYSERIAL.print(" _lt_resv1 = 0x"); + MYSERIAL.println(header.signature, 16); +#endif //(LT_PRINT_TEST==) +#undef LT_PRINT_TEST + +#if 0 + w25x20cl_rd_data(0x25ba, (uint8_t*)&block_buffer, 1024); + for (uint16_t i = 0; i < 1024; i++) + { + if ((i % 16) == 0) printf_P(_n("%04x:"), 0x25ba+i); + printf_P(_n(" %02x"), ((uint8_t*)&block_buffer)[i]); + if ((i % 16) == 15) putchar('\n'); + } +#endif + uint16_t sum = 0; + for (uint16_t i = 0; i < header.size; i++) + sum += (uint16_t)pgm_read_byte((uint8_t*)(_SEC_LANG_TABLE + i)) << ((i & 1)?0:8); + printf_P(_n("_SEC_LANG_TABLE checksum = %04x\n"), sum); + sum -= header.checksum; //subtract checksum + printf_P(_n("_SEC_LANG_TABLE checksum = %04x\n"), sum); + sum = (sum >> 8) | ((sum & 0xff) << 8); //swap bytes + if (sum == header.checksum) + printf_P(_n("Checksum OK\n"), sum); + else + printf_P(_n("Checksum NG\n"), sum); + } + else + printf_P(_n("lang_get_header failed!\n")); + +#if 0 + for (uint16_t i = 0; i < 1024*10; i++) + { + if ((i % 16) == 0) printf_P(_n("%04x:"), _SEC_LANG_TABLE+i); + printf_P(_n(" %02x"), pgm_read_byte((uint8_t*)(_SEC_LANG_TABLE+i))); + if ((i % 16) == 15) putchar('\n'); + } +#endif + +#if 0 + SERIAL_ECHOLN("Reading eeprom from 0 to 100: start"); + for (int i = 0; i < 4096; ++i) { + int b = eeprom_read_byte((unsigned char*)i); + if (b != 255) { + SERIAL_ECHO(i); + SERIAL_ECHO(":"); + SERIAL_ECHO(b); + SERIAL_ECHOLN(""); + } + } + SERIAL_ECHOLN("Reading eeprom from 0 to 100: done"); +#endif + +#endif //DEBUG_SEC_LANG + + // Check startup - does nothing if bootloader sets MCUSR to 0 + byte mcu = MCUSR; +/* if (mcu & 1) SERIAL_ECHOLNRPGM(MSG_POWERUP); + if (mcu & 2) SERIAL_ECHOLNRPGM(MSG_EXTERNAL_RESET); + if (mcu & 4) SERIAL_ECHOLNRPGM(MSG_BROWNOUT_RESET); + if (mcu & 8) SERIAL_ECHOLNRPGM(MSG_WATCHDOG_RESET); + if (mcu & 32) SERIAL_ECHOLNRPGM(MSG_SOFTWARE_RESET);*/ + if (mcu & 1) puts_P(MSG_POWERUP); + if (mcu & 2) puts_P(MSG_EXTERNAL_RESET); + if (mcu & 4) puts_P(MSG_BROWNOUT_RESET); + if (mcu & 8) puts_P(MSG_WATCHDOG_RESET); + if (mcu & 32) puts_P(MSG_SOFTWARE_RESET); + MCUSR = 0; + + //SERIAL_ECHORPGM(MSG_MARLIN); + //SERIAL_ECHOLNRPGM(VERSION_STRING); + +#ifdef STRING_VERSION_CONFIG_H +#ifdef STRING_CONFIG_H_AUTHOR + SERIAL_ECHO_START; + SERIAL_ECHORPGM(_n(" Last Updated: "));////MSG_CONFIGURATION_VER + SERIAL_ECHOPGM(STRING_VERSION_CONFIG_H); + SERIAL_ECHORPGM(_n(" | Author: "));////MSG_AUTHOR + SERIAL_ECHOLNPGM(STRING_CONFIG_H_AUTHOR); + SERIAL_ECHOPGM("Compiled: "); + SERIAL_ECHOLNPGM(__DATE__); +#endif +#endif + + SERIAL_ECHO_START; + SERIAL_ECHORPGM(_n(" Free Memory: "));////MSG_FREE_MEMORY + SERIAL_ECHO(freeMemory()); + SERIAL_ECHORPGM(_n(" PlannerBufferBytes: "));////MSG_PLANNER_BUFFER_BYTES + SERIAL_ECHOLN((int)sizeof(block_t)*BLOCK_BUFFER_SIZE); + //lcd_update_enable(false); // why do we need this?? - andre + // loads data from EEPROM if available else uses defaults (and resets step acceleration rate) + + bool previous_settings_retrieved = false; + uint8_t hw_changed = check_printer_version(); + if (!(hw_changed & 0b10)) { //if printer version wasn't changed, check for eeprom version and retrieve settings from eeprom in case that version wasn't changed + previous_settings_retrieved = Config_RetrieveSettings(); + } + else { //printer version was changed so use default settings + Config_ResetDefault(); + } + SdFatUtil::set_stack_guard(); //writes magic number at the end of static variables to protect against overwriting static memory by stack + + tp_init(); // Initialize temperature loop + + if (w25x20cl_success) lcd_splash(); // we need to do this again, because tp_init() kills lcd + else + { + w25x20cl_err_msg(); + printf_P(_n("W25X20CL not responding.\n")); + } + + plan_init(); // Initialize planner; + + factory_reset(); + if (eeprom_read_dword((uint32_t*)(EEPROM_TOP - 4)) == 0x0ffffffff && + eeprom_read_dword((uint32_t*)(EEPROM_TOP - 8)) == 0x0ffffffff) + { + // Maiden startup. The firmware has been loaded and first started on a virgin RAMBo board, + // where all the EEPROM entries are set to 0x0ff. + // Once a firmware boots up, it forces at least a language selection, which changes + // EEPROM_LANG to number lower than 0x0ff. + // 1) Set a high power mode. + eeprom_update_byte((uint8_t*)EEPROM_SILENT, SILENT_MODE_OFF); +#ifdef TMC2130 + tmc2130_mode = TMC2130_MODE_NORMAL; +#endif //TMC2130 + eeprom_write_byte((uint8_t*)EEPROM_WIZARD_ACTIVE, 1); //run wizard + } + + lcd_encoder_diff=0; + +#ifdef TMC2130 + uint8_t silentMode = eeprom_read_byte((uint8_t*)EEPROM_SILENT); + if (silentMode == 0xff) silentMode = 0; + tmc2130_mode = TMC2130_MODE_NORMAL; + + if (lcd_crash_detect_enabled() && !farm_mode) + { + lcd_crash_detect_enable(); + puts_P(_N("CrashDetect ENABLED!")); + } + else + { + lcd_crash_detect_disable(); + puts_P(_N("CrashDetect DISABLED")); + } + +#ifdef TMC2130_LINEARITY_CORRECTION +#ifdef TMC2130_LINEARITY_CORRECTION_XYZ + tmc2130_wave_fac[X_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_WAVE_X_FAC); + tmc2130_wave_fac[Y_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_WAVE_Y_FAC); + tmc2130_wave_fac[Z_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_WAVE_Z_FAC); +#endif //TMC2130_LINEARITY_CORRECTION_XYZ + tmc2130_wave_fac[E_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_WAVE_E_FAC); + if (tmc2130_wave_fac[X_AXIS] == 0xff) tmc2130_wave_fac[X_AXIS] = 0; + if (tmc2130_wave_fac[Y_AXIS] == 0xff) tmc2130_wave_fac[Y_AXIS] = 0; + if (tmc2130_wave_fac[Z_AXIS] == 0xff) tmc2130_wave_fac[Z_AXIS] = 0; + if (tmc2130_wave_fac[E_AXIS] == 0xff) tmc2130_wave_fac[E_AXIS] = 0; +#endif //TMC2130_LINEARITY_CORRECTION + +#ifdef TMC2130_VARIABLE_RESOLUTION + tmc2130_mres[X_AXIS] = tmc2130_usteps2mres(cs.axis_ustep_resolution[X_AXIS]); + tmc2130_mres[Y_AXIS] = tmc2130_usteps2mres(cs.axis_ustep_resolution[Y_AXIS]); + tmc2130_mres[Z_AXIS] = tmc2130_usteps2mres(cs.axis_ustep_resolution[Z_AXIS]); + tmc2130_mres[E_AXIS] = tmc2130_usteps2mres(cs.axis_ustep_resolution[E_AXIS]); +#else //TMC2130_VARIABLE_RESOLUTION + tmc2130_mres[X_AXIS] = tmc2130_usteps2mres(TMC2130_USTEPS_XY); + tmc2130_mres[Y_AXIS] = tmc2130_usteps2mres(TMC2130_USTEPS_XY); + tmc2130_mres[Z_AXIS] = tmc2130_usteps2mres(TMC2130_USTEPS_Z); + tmc2130_mres[E_AXIS] = tmc2130_usteps2mres(TMC2130_USTEPS_E); +#endif //TMC2130_VARIABLE_RESOLUTION + +#endif //TMC2130 + + st_init(); // Initialize stepper, this enables interrupts! + +#ifdef UVLO_SUPPORT + setup_uvlo_interrupt(); +#endif //UVLO_SUPPORT + +#ifdef TMC2130 + tmc2130_mode = silentMode?TMC2130_MODE_SILENT:TMC2130_MODE_NORMAL; + update_mode_profile(); + tmc2130_init(); +#endif //TMC2130 +#ifdef PSU_Delta + init_force_z(); // ! important for correct Z-axis initialization +#endif // PSU_Delta + + setup_photpin(); + + servo_init(); + // Reset the machine correction matrix. + // It does not make sense to load the correction matrix until the machine is homed. + world2machine_reset(); + +#ifdef FILAMENT_SENSOR + fsensor_init(); +#endif //FILAMENT_SENSOR + + +#if defined(CONTROLLERFAN_PIN) && (CONTROLLERFAN_PIN > -1) + SET_OUTPUT(CONTROLLERFAN_PIN); //Set pin used for driver cooling fan +#endif + + + setup_homepin(); + +#ifdef TMC2130 + + if (1) { + // try to run to zero phase before powering the Z motor. + // Move in negative direction + WRITE(Z_DIR_PIN,INVERT_Z_DIR); + // Round the current micro-micro steps to micro steps. + for (uint16_t phase = (tmc2130_rd_MSCNT(Z_AXIS) + 8) >> 4; phase > 0; -- phase) { + // Until the phase counter is reset to zero. + WRITE(Z_STEP_PIN, !INVERT_Z_STEP_PIN); + _delay(2); + WRITE(Z_STEP_PIN, INVERT_Z_STEP_PIN); + _delay(2); + } + } +#endif //TMC2130 + +#if defined(Z_AXIS_ALWAYS_ON) && !defined(PSU_Delta) + enable_z(); +#endif + farm_mode = eeprom_read_byte((uint8_t*)EEPROM_FARM_MODE); + EEPROM_read_B(EEPROM_FARM_NUMBER, &farm_no); + if ((farm_mode == 0xFF && farm_no == 0) || (farm_no == static_cast(0xFFFF))) farm_mode = false; //if farm_mode has not been stored to eeprom yet and farm number is set to zero or EEPROM is fresh, deactivate farm mode + if (farm_no == static_cast(0xFFFF)) farm_no = 0; + if (farm_mode) + { + prusa_statistics(8); + } + + // Enable Toshiba FlashAir SD card / WiFi enahanced card. + card.ToshibaFlashAir_enable(eeprom_read_byte((unsigned char*)EEPROM_TOSHIBA_FLASH_AIR_COMPATIBLITY) == 1); + + // Force SD card update. Otherwise the SD card update is done from loop() on card.checkautostart(false), + // but this times out if a blocking dialog is shown in setup(). + card.initsd(); +#ifdef DEBUG_SD_SPEED_TEST + if (card.cardOK) + { + uint8_t* buff = (uint8_t*)block_buffer; + uint32_t block = 0; + uint32_t sumr = 0; + uint32_t sumw = 0; + for (int i = 0; i < 1024; i++) + { + uint32_t u = _micros(); + bool res = card.card.readBlock(i, buff); + u = _micros() - u; + if (res) + { + printf_P(PSTR("readBlock %4d 512 bytes %lu us\n"), i, u); + sumr += u; + u = _micros(); + res = card.card.writeBlock(i, buff); + u = _micros() - u; + if (res) + { + printf_P(PSTR("writeBlock %4d 512 bytes %lu us\n"), i, u); + sumw += u; + } + else + { + printf_P(PSTR("writeBlock %4d error\n"), i); + break; + } + } + else + { + printf_P(PSTR("readBlock %4d error\n"), i); + break; + } + } + uint32_t avg_rspeed = (1024 * 1000000) / (sumr / 512); + uint32_t avg_wspeed = (1024 * 1000000) / (sumw / 512); + printf_P(PSTR("avg read speed %lu bytes/s\n"), avg_rspeed); + printf_P(PSTR("avg write speed %lu bytes/s\n"), avg_wspeed); + } + else + printf_P(PSTR("Card NG!\n")); +#endif //DEBUG_SD_SPEED_TEST + + eeprom_init(); +#ifdef SNMM + if (eeprom_read_dword((uint32_t*)EEPROM_BOWDEN_LENGTH) == 0x0ffffffff) { //bowden length used for SNMM + int _z = BOWDEN_LENGTH; + for(int i = 0; i<4; i++) EEPROM_save_B(EEPROM_BOWDEN_LENGTH + i * 2, &_z); + } +#endif + + // In the future, somewhere here would one compare the current firmware version against the firmware version stored in the EEPROM. + // If they differ, an update procedure may need to be performed. At the end of this block, the current firmware version + // is being written into the EEPROM, so the update procedure will be triggered only once. + + +#if (LANG_MODE != 0) //secondary language support + +#ifdef DEBUG_W25X20CL + W25X20CL_SPI_ENTER(); + uint8_t uid[8]; // 64bit unique id + w25x20cl_rd_uid(uid); + puts_P(_n("W25X20CL UID=")); + for (uint8_t i = 0; i < 8; i ++) + printf_P(PSTR("%02hhx"), uid[i]); + putchar('\n'); + list_sec_lang_from_external_flash(); +#endif //DEBUG_W25X20CL + +// lang_reset(); + if (!lang_select(eeprom_read_byte((uint8_t*)EEPROM_LANG))) + lcd_language(); + +#ifdef DEBUG_SEC_LANG + + uint16_t sec_lang_code = lang_get_code(1); + uint16_t ui = _SEC_LANG_TABLE; //table pointer + printf_P(_n("lang_selected=%d\nlang_table=0x%04x\nSEC_LANG_CODE=0x%04x (%c%c)\n"), lang_selected, ui, sec_lang_code, sec_lang_code >> 8, sec_lang_code & 0xff); + + lang_print_sec_lang(uartout); +#endif //DEBUG_SEC_LANG + +#endif //(LANG_MODE != 0) + + if (eeprom_read_byte((uint8_t*)EEPROM_TEMP_CAL_ACTIVE) == 255) { + eeprom_write_byte((uint8_t*)EEPROM_TEMP_CAL_ACTIVE, 0); + temp_cal_active = false; + } else temp_cal_active = eeprom_read_byte((uint8_t*)EEPROM_TEMP_CAL_ACTIVE); + + if (eeprom_read_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA) == 255) { + //eeprom_write_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, 0); + eeprom_write_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, 1); + int16_t z_shift = 0; + for (uint8_t i = 0; i < 5; i++) EEPROM_save_B(EEPROM_PROBE_TEMP_SHIFT + i * 2, &z_shift); + eeprom_write_byte((uint8_t*)EEPROM_TEMP_CAL_ACTIVE, 0); + temp_cal_active = false; + } + if (eeprom_read_byte((uint8_t*)EEPROM_UVLO) == 255) { + eeprom_write_byte((uint8_t*)EEPROM_UVLO, 0); + } + if (eeprom_read_byte((uint8_t*)EEPROM_SD_SORT) == 255) { + eeprom_write_byte((uint8_t*)EEPROM_SD_SORT, 0); + } + //mbl_mode_init(); + mbl_settings_init(); + SilentModeMenu_MMU = eeprom_read_byte((uint8_t*)EEPROM_MMU_STEALTH); + if (SilentModeMenu_MMU == 255) { + SilentModeMenu_MMU = 1; + eeprom_write_byte((uint8_t*)EEPROM_MMU_STEALTH, SilentModeMenu_MMU); + } + +#if !defined(DEBUG_DISABLE_FANCHECK) && defined(FANCHECK) && defined(TACH_1) && TACH_1 >-1 + setup_fan_interrupt(); +#endif //DEBUG_DISABLE_FANCHECK + +#ifdef PAT9125 + fsensor_setup_interrupt(); +#endif //PAT9125 + for (int i = 0; i<4; i++) EEPROM_read_B(EEPROM_BOWDEN_LENGTH + i * 2, &bowden_length[i]); + +#ifndef DEBUG_DISABLE_STARTMSGS + KEEPALIVE_STATE(PAUSED_FOR_USER); + + if (!farm_mode) { + check_if_fw_is_on_right_printer(); + show_fw_version_warnings(); + } + + switch (hw_changed) { + //if motherboard or printer type was changed inform user as it can indicate flashing wrong firmware version + //if user confirms with knob, new hw version (printer and/or motherboard) is written to eeprom and message will be not shown next time + case(0b01): + lcd_show_fullscreen_message_and_wait_P(_i("Warning: motherboard type changed.")); ////MSG_CHANGED_MOTHERBOARD c=20 r=4 + eeprom_write_word((uint16_t*)EEPROM_BOARD_TYPE, MOTHERBOARD); + break; + case(0b10): + lcd_show_fullscreen_message_and_wait_P(_i("Warning: printer type changed.")); ////MSG_CHANGED_PRINTER c=20 r=4 + eeprom_write_word((uint16_t*)EEPROM_PRINTER_TYPE, PRINTER_TYPE); + break; + case(0b11): + lcd_show_fullscreen_message_and_wait_P(_i("Warning: both printer type and motherboard type changed.")); ////MSG_CHANGED_BOTH c=20 r=4 + eeprom_write_word((uint16_t*)EEPROM_PRINTER_TYPE, PRINTER_TYPE); + eeprom_write_word((uint16_t*)EEPROM_BOARD_TYPE, MOTHERBOARD); + break; + default: break; //no change, show no message + } + + if (!previous_settings_retrieved) { + lcd_show_fullscreen_message_and_wait_P(_i("Old settings found. Default PID, Esteps etc. will be set.")); //if EEPROM version or printer type was changed, inform user that default setting were loaded////MSG_DEFAULT_SETTINGS_LOADED c=20 r=4 + Config_StoreSettings(); + } + if (eeprom_read_byte((uint8_t*)EEPROM_WIZARD_ACTIVE) == 1) { + lcd_wizard(WizState::Run); + } + if (eeprom_read_byte((uint8_t*)EEPROM_WIZARD_ACTIVE) == 0) { //dont show calibration status messages if wizard is currently active + if (calibration_status() == CALIBRATION_STATUS_ASSEMBLED || + calibration_status() == CALIBRATION_STATUS_UNKNOWN || + calibration_status() == CALIBRATION_STATUS_XYZ_CALIBRATION) { + // Reset the babystepping values, so the printer will not move the Z axis up when the babystepping is enabled. + eeprom_update_word(reinterpret_cast(&(EEPROM_Sheets_base->s[(eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)))].z_offset)),0); + // Show the message. + lcd_show_fullscreen_message_and_wait_P(_T(MSG_FOLLOW_CALIBRATION_FLOW)); + } + else if (calibration_status() == CALIBRATION_STATUS_LIVE_ADJUST) { + // Show the message. + lcd_show_fullscreen_message_and_wait_P(_T(MSG_BABYSTEP_Z_NOT_SET)); + lcd_update_enable(true); + } + else if (calibration_status() == CALIBRATION_STATUS_CALIBRATED && temp_cal_active == true && calibration_status_pinda() == false) { + //lcd_show_fullscreen_message_and_wait_P(_i("Temperature calibration has not been run yet"));////MSG_PINDA_NOT_CALIBRATED c=20 r=4 + lcd_update_enable(true); + } + else if (calibration_status() == CALIBRATION_STATUS_Z_CALIBRATION) { + // Show the message. + lcd_show_fullscreen_message_and_wait_P(_T(MSG_FOLLOW_Z_CALIBRATION_FLOW)); + } + } + +#if !defined (DEBUG_DISABLE_FORCE_SELFTEST) && defined (TMC2130) + if (force_selftest_if_fw_version() && calibration_status() < CALIBRATION_STATUS_ASSEMBLED) { + lcd_show_fullscreen_message_and_wait_P(_i("Selftest will be run to calibrate accurate sensorless rehoming."));////MSG_FORCE_SELFTEST c=20 r=8 + update_current_firmware_version_to_eeprom(); + lcd_selftest(); + } +#endif //TMC2130 && !DEBUG_DISABLE_FORCE_SELFTEST + + KEEPALIVE_STATE(IN_PROCESS); +#endif //DEBUG_DISABLE_STARTMSGS + lcd_update_enable(true); + lcd_clear(); + lcd_update(2); + // Store the currently running firmware into an eeprom, + // so the next time the firmware gets updated, it will know from which version it has been updated. + update_current_firmware_version_to_eeprom(); + +#ifdef TMC2130 + tmc2130_home_origin[X_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_X_ORIGIN); + tmc2130_home_bsteps[X_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_X_BSTEPS); + tmc2130_home_fsteps[X_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_X_FSTEPS); + if (tmc2130_home_origin[X_AXIS] == 0xff) tmc2130_home_origin[X_AXIS] = 0; + if (tmc2130_home_bsteps[X_AXIS] == 0xff) tmc2130_home_bsteps[X_AXIS] = 48; + if (tmc2130_home_fsteps[X_AXIS] == 0xff) tmc2130_home_fsteps[X_AXIS] = 48; + + tmc2130_home_origin[Y_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_Y_ORIGIN); + tmc2130_home_bsteps[Y_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_Y_BSTEPS); + tmc2130_home_fsteps[Y_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_Y_FSTEPS); + if (tmc2130_home_origin[Y_AXIS] == 0xff) tmc2130_home_origin[Y_AXIS] = 0; + if (tmc2130_home_bsteps[Y_AXIS] == 0xff) tmc2130_home_bsteps[Y_AXIS] = 48; + if (tmc2130_home_fsteps[Y_AXIS] == 0xff) tmc2130_home_fsteps[Y_AXIS] = 48; + + tmc2130_home_enabled = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_ENABLED); + if (tmc2130_home_enabled == 0xff) tmc2130_home_enabled = 0; +#endif //TMC2130 + +#ifdef UVLO_SUPPORT + if (eeprom_read_byte((uint8_t*)EEPROM_UVLO) != 0) { //previous print was terminated by UVLO +/* + if (lcd_show_fullscreen_message_yes_no_and_wait_P(_T(MSG_RECOVER_PRINT), false)) recover_print(); + else { + eeprom_update_byte((uint8_t*)EEPROM_UVLO, 0); + lcd_update_enable(true); + lcd_update(2); + lcd_setstatuspgm(_T(WELCOME_MSG)); + } +*/ + manage_heater(); // Update temperatures +#ifdef DEBUG_UVLO_AUTOMATIC_RECOVER + printf_P(_N("Power panic detected!\nCurrent bed temp:%d\nSaved bed temp:%d\n"), (int)degBed(), eeprom_read_byte((uint8_t*)EEPROM_UVLO_TARGET_BED)); +#endif + if ( degBed() > ( (float)eeprom_read_byte((uint8_t*)EEPROM_UVLO_TARGET_BED) - AUTOMATIC_UVLO_BED_TEMP_OFFSET) ){ + #ifdef DEBUG_UVLO_AUTOMATIC_RECOVER + puts_P(_N("Automatic recovery!")); + #endif + recover_print(1); + } + else{ + #ifdef DEBUG_UVLO_AUTOMATIC_RECOVER + puts_P(_N("Normal recovery!")); + #endif + if ( lcd_show_fullscreen_message_yes_no_and_wait_P(_T(MSG_RECOVER_PRINT), false) ) recover_print(0); + else { + eeprom_update_byte((uint8_t*)EEPROM_UVLO, 0); + lcd_update_enable(true); + lcd_update(2); + lcd_setstatuspgm(_T(WELCOME_MSG)); + } + + } + + + } +#endif //UVLO_SUPPORT + fCheckModeInit(); + fSetMmuMode(mmu_enabled); + KEEPALIVE_STATE(NOT_BUSY); +#ifdef WATCHDOG + wdt_enable(WDTO_4S); +#endif //WATCHDOG +} + + +void trace(); + +#define CHUNK_SIZE 64 // bytes +#define SAFETY_MARGIN 1 +char chunk[CHUNK_SIZE+SAFETY_MARGIN]; +int chunkHead = 0; + +void serial_read_stream() { + + setAllTargetHotends(0); + setTargetBed(0); + + lcd_clear(); + lcd_puts_P(PSTR(" Upload in progress")); + + // first wait for how many bytes we will receive + uint32_t bytesToReceive; + + // receive the four bytes + char bytesToReceiveBuffer[4]; + for (int i=0; i<4; i++) { + int data; + while ((data = MYSERIAL.read()) == -1) {}; + bytesToReceiveBuffer[i] = data; + + } + + // make it a uint32 + memcpy(&bytesToReceive, &bytesToReceiveBuffer, 4); + + // we're ready, notify the sender + MYSERIAL.write('+'); + + // lock in the routine + uint32_t receivedBytes = 0; + while (prusa_sd_card_upload) { + int i; + for (i=0; i 0) && ((_millis()-_usb_timer) > 1000)) + { + is_usb_printing = true; + usb_printing_counter--; + _usb_timer = _millis(); + } + if (usb_printing_counter == 0) + { + is_usb_printing = false; + } + if (isPrintPaused && saved_printing_type == PRINTING_TYPE_USB) //keep believing that usb is being printed. Prevents accessing dangerous menus while pausing. + { + is_usb_printing = true; + } + +#ifdef FANCHECK + if (fan_check_error && isPrintPaused) + { + KEEPALIVE_STATE(PAUSED_FOR_USER); + host_keepalive(); //prevent timeouts since usb processing is disabled until print is resumed. This is for a crude way of pausing a print on all hosts. + } +#endif + + if (prusa_sd_card_upload) + { + //we read byte-by byte + serial_read_stream(); + } + else + { + + get_command(); + + #ifdef SDSUPPORT + card.checkautostart(false); + #endif + if(buflen) + { + cmdbuffer_front_already_processed = false; + #ifdef SDSUPPORT + if(card.saving) + { + // Saving a G-code file onto an SD-card is in progress. + // Saving starts with M28, saving until M29 is seen. + if(strstr_P(CMDBUFFER_CURRENT_STRING, PSTR("M29")) == NULL) { + card.write_command(CMDBUFFER_CURRENT_STRING); + if(card.logging) + process_commands(); + else + SERIAL_PROTOCOLLNRPGM(MSG_OK); + } else { + card.closefile(); + SERIAL_PROTOCOLLNRPGM(MSG_FILE_SAVED); + } + } else { + process_commands(); + } + #else + process_commands(); + #endif //SDSUPPORT + + if (! cmdbuffer_front_already_processed && buflen) + { + // ptr points to the start of the block currently being processed. + // The first character in the block is the block type. + char *ptr = cmdbuffer + bufindr; + if (*ptr == CMDBUFFER_CURRENT_TYPE_SDCARD) { + // To support power panic, move the lenght of the command on the SD card to a planner buffer. + union { + struct { + char lo; + char hi; + } lohi; + uint16_t value; + } sdlen; + sdlen.value = 0; + { + // This block locks the interrupts globally for 3.25 us, + // which corresponds to a maximum repeat frequency of 307.69 kHz. + // This blocking is safe in the context of a 10kHz stepper driver interrupt + // or a 115200 Bd serial line receive interrupt, which will not trigger faster than 12kHz. + cli(); + // Reset the command to something, which will be ignored by the power panic routine, + // so this buffer length will not be counted twice. + *ptr ++ = CMDBUFFER_CURRENT_TYPE_TO_BE_REMOVED; + // Extract the current buffer length. + sdlen.lohi.lo = *ptr ++; + sdlen.lohi.hi = *ptr; + // and pass it to the planner queue. + planner_add_sd_length(sdlen.value); + sei(); + } + } + else if((*ptr == CMDBUFFER_CURRENT_TYPE_USB_WITH_LINENR) && !IS_SD_PRINTING){ + + cli(); + *ptr ++ = CMDBUFFER_CURRENT_TYPE_TO_BE_REMOVED; + // and one for each command to previous block in the planner queue. + planner_add_sd_length(1); + sei(); + } + // Now it is safe to release the already processed command block. If interrupted by the power panic now, + // this block's SD card length will not be counted twice as its command type has been replaced + // by CMDBUFFER_CURRENT_TYPE_TO_BE_REMOVED. + cmdqueue_pop_front(); + } + host_keepalive(); + } +} + //check heater every n milliseconds + manage_heater(); + isPrintPaused ? manage_inactivity(true) : manage_inactivity(false); + checkHitEndstops(); + lcd_update(0); +#ifdef TMC2130 + tmc2130_check_overtemp(); + if (tmc2130_sg_crash) + { + uint8_t crash = tmc2130_sg_crash; + tmc2130_sg_crash = 0; +// crashdet_stop_and_save_print(); + switch (crash) + { + case 1: enquecommand_P((PSTR("CRASH_DETECTEDX"))); break; + case 2: enquecommand_P((PSTR("CRASH_DETECTEDY"))); break; + case 3: enquecommand_P((PSTR("CRASH_DETECTEDXY"))); break; + } + } +#endif //TMC2130 + mmu_loop(); +} + +#define DEFINE_PGM_READ_ANY(type, reader) \ + static inline type pgm_read_any(const type *p) \ + { return pgm_read_##reader##_near(p); } + +DEFINE_PGM_READ_ANY(float, float); +DEFINE_PGM_READ_ANY(signed char, byte); + +#define XYZ_CONSTS_FROM_CONFIG(type, array, CONFIG) \ +static const PROGMEM type array##_P[3] = \ + { X_##CONFIG, Y_##CONFIG, Z_##CONFIG }; \ +static inline type array(int axis) \ + { return pgm_read_any(&array##_P[axis]); } \ +type array##_ext(int axis) \ + { return pgm_read_any(&array##_P[axis]); } + +XYZ_CONSTS_FROM_CONFIG(float, base_min_pos, MIN_POS); +XYZ_CONSTS_FROM_CONFIG(float, base_max_pos, MAX_POS); +XYZ_CONSTS_FROM_CONFIG(float, base_home_pos, HOME_POS); +XYZ_CONSTS_FROM_CONFIG(float, max_length, MAX_LENGTH); +XYZ_CONSTS_FROM_CONFIG(float, home_retract_mm, HOME_RETRACT_MM); +XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR); + +static void axis_is_at_home(int axis) { + current_position[axis] = base_home_pos(axis) + cs.add_homing[axis]; + min_pos[axis] = base_min_pos(axis) + cs.add_homing[axis]; + max_pos[axis] = base_max_pos(axis) + cs.add_homing[axis]; +} + + +inline void set_current_to_destination() { memcpy(current_position, destination, sizeof(current_position)); } +inline void set_destination_to_current() { memcpy(destination, current_position, sizeof(destination)); } + +//! @return original feedmultiply +static int setup_for_endstop_move(bool enable_endstops_now = true) { + saved_feedrate = feedrate; + int l_feedmultiply = feedmultiply; + feedmultiply = 100; + previous_millis_cmd = _millis(); + + enable_endstops(enable_endstops_now); + return l_feedmultiply; +} + +//! @param original_feedmultiply feedmultiply to restore +static void clean_up_after_endstop_move(int original_feedmultiply) { +#ifdef ENDSTOPS_ONLY_FOR_HOMING + enable_endstops(false); +#endif + + feedrate = saved_feedrate; + feedmultiply = original_feedmultiply; + previous_millis_cmd = _millis(); +} + + + +#ifdef ENABLE_AUTO_BED_LEVELING +#ifdef AUTO_BED_LEVELING_GRID +static void set_bed_level_equation_lsq(double *plane_equation_coefficients) +{ + vector_3 planeNormal = vector_3(-plane_equation_coefficients[0], -plane_equation_coefficients[1], 1); + planeNormal.debug("planeNormal"); + plan_bed_level_matrix = matrix_3x3::create_look_at(planeNormal); + //bedLevel.debug("bedLevel"); + + //plan_bed_level_matrix.debug("bed level before"); + //vector_3 uncorrected_position = plan_get_position_mm(); + //uncorrected_position.debug("position before"); + + vector_3 corrected_position = plan_get_position(); +// corrected_position.debug("position after"); + current_position[X_AXIS] = corrected_position.x; + current_position[Y_AXIS] = corrected_position.y; + current_position[Z_AXIS] = corrected_position.z; + + // put the bed at 0 so we don't go below it. + current_position[Z_AXIS] = cs.zprobe_zoffset; // in the lsq we reach here after raising the extruder due to the loop structure + + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); +} + +#else // not AUTO_BED_LEVELING_GRID + +static void set_bed_level_equation_3pts(float z_at_pt_1, float z_at_pt_2, float z_at_pt_3) { + + plan_bed_level_matrix.set_to_identity(); + + vector_3 pt1 = vector_3(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, z_at_pt_1); + vector_3 pt2 = vector_3(ABL_PROBE_PT_2_X, ABL_PROBE_PT_2_Y, z_at_pt_2); + vector_3 pt3 = vector_3(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, z_at_pt_3); + + vector_3 from_2_to_1 = (pt1 - pt2).get_normal(); + vector_3 from_2_to_3 = (pt3 - pt2).get_normal(); + vector_3 planeNormal = vector_3::cross(from_2_to_1, from_2_to_3).get_normal(); + planeNormal = vector_3(planeNormal.x, planeNormal.y, abs(planeNormal.z)); + + plan_bed_level_matrix = matrix_3x3::create_look_at(planeNormal); + + vector_3 corrected_position = plan_get_position(); + current_position[X_AXIS] = corrected_position.x; + current_position[Y_AXIS] = corrected_position.y; + current_position[Z_AXIS] = corrected_position.z; + + // put the bed at 0 so we don't go below it. + current_position[Z_AXIS] = cs.zprobe_zoffset; + + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + +} + +#endif // AUTO_BED_LEVELING_GRID + +static void run_z_probe() { + plan_bed_level_matrix.set_to_identity(); + feedrate = homing_feedrate[Z_AXIS]; + + // move down until you find the bed + float zPosition = -10; + plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder); + st_synchronize(); + + // we have to let the planner know where we are right now as it is not where we said to go. + zPosition = st_get_position_mm(Z_AXIS); + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS]); + + // move up the retract distance + zPosition += home_retract_mm(Z_AXIS); + plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder); + st_synchronize(); + + // move back down slowly to find bed + feedrate = homing_feedrate[Z_AXIS]/4; + zPosition -= home_retract_mm(Z_AXIS) * 2; + plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder); + st_synchronize(); + + current_position[Z_AXIS] = st_get_position_mm(Z_AXIS); + // make sure the planner knows where we are as it may be a bit different than we last said to move to + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); +} + +static void do_blocking_move_to(float x, float y, float z) { + float oldFeedRate = feedrate; + + feedrate = homing_feedrate[Z_AXIS]; + + current_position[Z_AXIS] = z; + plan_buffer_line_curposXYZE(feedrate/60, active_extruder); + st_synchronize(); + + feedrate = XY_TRAVEL_SPEED; + + current_position[X_AXIS] = x; + current_position[Y_AXIS] = y; + plan_buffer_line_curposXYZE(feedrate/60, active_extruder); + st_synchronize(); + + feedrate = oldFeedRate; +} + +static void do_blocking_move_relative(float offset_x, float offset_y, float offset_z) { + do_blocking_move_to(current_position[X_AXIS] + offset_x, current_position[Y_AXIS] + offset_y, current_position[Z_AXIS] + offset_z); +} + + +/// Probe bed height at position (x,y), returns the measured z value +static float probe_pt(float x, float y, float z_before) { + // move to right place + do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], z_before); + do_blocking_move_to(x - X_PROBE_OFFSET_FROM_EXTRUDER, y - Y_PROBE_OFFSET_FROM_EXTRUDER, current_position[Z_AXIS]); + + run_z_probe(); + float measured_z = current_position[Z_AXIS]; + + SERIAL_PROTOCOLRPGM(_T(MSG_BED)); + SERIAL_PROTOCOLPGM(" x: "); + SERIAL_PROTOCOL(x); + SERIAL_PROTOCOLPGM(" y: "); + SERIAL_PROTOCOL(y); + SERIAL_PROTOCOLPGM(" z: "); + SERIAL_PROTOCOL(measured_z); + SERIAL_PROTOCOLPGM("\n"); + return measured_z; +} + +#endif // #ifdef ENABLE_AUTO_BED_LEVELING + +#ifdef LIN_ADVANCE + /** + * M900: Set and/or Get advance K factor and WH/D ratio + * + * K Set advance K factor + * R Set ratio directly (overrides WH/D) + * W H D Set ratio from WH/D + */ +inline void gcode_M900() { + st_synchronize(); + + const float newK = code_seen('K') ? code_value_float() : -1; + if (newK >= 0) extruder_advance_k = newK; + + float newR = code_seen('R') ? code_value_float() : -1; + if (newR < 0) { + const float newD = code_seen('D') ? code_value_float() : -1, + newW = code_seen('W') ? code_value_float() : -1, + newH = code_seen('H') ? code_value_float() : -1; + if (newD >= 0 && newW >= 0 && newH >= 0) + newR = newD ? (newW * newH) / (sq(newD * 0.5) * M_PI) : 0; + } + if (newR >= 0) advance_ed_ratio = newR; + + SERIAL_ECHO_START; + SERIAL_ECHOPGM("Advance K="); + SERIAL_ECHOLN(extruder_advance_k); + SERIAL_ECHOPGM(" E/D="); + const float ratio = advance_ed_ratio; + if (ratio) SERIAL_ECHOLN(ratio); else SERIAL_ECHOLNPGM("Auto"); + } +#endif // LIN_ADVANCE + +bool check_commands() { + bool end_command_found = false; + + while (buflen) + { + if ((code_seen("M84")) || (code_seen("M 84"))) end_command_found = true; + if (!cmdbuffer_front_already_processed) + cmdqueue_pop_front(); + cmdbuffer_front_already_processed = false; + } + return end_command_found; + +} + +#ifdef TMC2130 +bool calibrate_z_auto() +{ + //lcd_display_message_fullscreen_P(_T(MSG_CALIBRATE_Z_AUTO)); + lcd_clear(); + lcd_puts_at_P(0, 1, _T(MSG_CALIBRATE_Z_AUTO)); + bool endstops_enabled = enable_endstops(true); + int axis_up_dir = -home_dir(Z_AXIS); + tmc2130_home_enter(Z_AXIS_MASK); + current_position[Z_AXIS] = 0; + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + set_destination_to_current(); + destination[Z_AXIS] += (1.1 * max_length(Z_AXIS) * axis_up_dir); + feedrate = homing_feedrate[Z_AXIS]; + plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate / 60, active_extruder); + st_synchronize(); + // current_position[axis] = 0; + // plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + tmc2130_home_exit(); + enable_endstops(false); + current_position[Z_AXIS] = 0; + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + set_destination_to_current(); + destination[Z_AXIS] += 10 * axis_up_dir; //10mm up + feedrate = homing_feedrate[Z_AXIS] / 2; + plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate / 60, active_extruder); + st_synchronize(); + enable_endstops(endstops_enabled); + if (PRINTER_TYPE == PRINTER_MK3) { + current_position[Z_AXIS] = Z_MAX_POS + 2.0; + } + else { + current_position[Z_AXIS] = Z_MAX_POS + 9.0; + } + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + return true; +} +#endif //TMC2130 + +#ifdef TMC2130 +void homeaxis(int axis, uint8_t cnt, uint8_t* pstep) +#else +void homeaxis(int axis, uint8_t cnt) +#endif //TMC2130 +{ + bool endstops_enabled = enable_endstops(true); //RP: endstops should be allways enabled durring homing +#define HOMEAXIS_DO(LETTER) \ +((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1)) + if ((axis==X_AXIS)?HOMEAXIS_DO(X):(axis==Y_AXIS)?HOMEAXIS_DO(Y):0) + { + int axis_home_dir = home_dir(axis); + feedrate = homing_feedrate[axis]; + +#ifdef TMC2130 + tmc2130_home_enter(X_AXIS_MASK << axis); +#endif //TMC2130 + + + // Move away a bit, so that the print head does not touch the end position, + // and the following movement to endstop has a chance to achieve the required velocity + // for the stall guard to work. + current_position[axis] = 0; + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + set_destination_to_current(); +// destination[axis] = 11.f; + destination[axis] = -3.f * axis_home_dir; + plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); + st_synchronize(); + // Move away from the possible collision with opposite endstop with the collision detection disabled. + endstops_hit_on_purpose(); + enable_endstops(false); + current_position[axis] = 0; + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + destination[axis] = 1. * axis_home_dir; + plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); + st_synchronize(); + // Now continue to move up to the left end stop with the collision detection enabled. + enable_endstops(true); + destination[axis] = 1.1 * axis_home_dir * max_length(axis); + plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); + st_synchronize(); + for (uint8_t i = 0; i < cnt; i++) + { + // Move away from the collision to a known distance from the left end stop with the collision detection disabled. + endstops_hit_on_purpose(); + enable_endstops(false); + current_position[axis] = 0; + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + destination[axis] = -10.f * axis_home_dir; + plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); + st_synchronize(); + endstops_hit_on_purpose(); + // Now move left up to the collision, this time with a repeatable velocity. + enable_endstops(true); + destination[axis] = 11.f * axis_home_dir; +#ifdef TMC2130 + feedrate = homing_feedrate[axis]; +#else //TMC2130 + feedrate = homing_feedrate[axis] / 2; +#endif //TMC2130 + plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); + st_synchronize(); +#ifdef TMC2130 + uint16_t mscnt = tmc2130_rd_MSCNT(axis); + if (pstep) pstep[i] = mscnt >> 4; + printf_P(PSTR("%3d step=%2d mscnt=%4d\n"), i, mscnt >> 4, mscnt); +#endif //TMC2130 + } + endstops_hit_on_purpose(); + enable_endstops(false); + +#ifdef TMC2130 + uint8_t orig = tmc2130_home_origin[axis]; + uint8_t back = tmc2130_home_bsteps[axis]; + if (tmc2130_home_enabled && (orig <= 63)) + { + tmc2130_goto_step(axis, orig, 2, 1000, tmc2130_get_res(axis)); + if (back > 0) + tmc2130_do_steps(axis, back, -axis_home_dir, 1000); + } + else + tmc2130_do_steps(axis, 8, -axis_home_dir, 1000); + tmc2130_home_exit(); +#endif //TMC2130 + + axis_is_at_home(axis); + axis_known_position[axis] = true; + // Move from minimum +#ifdef TMC2130 + float dist = - axis_home_dir * 0.01f * tmc2130_home_fsteps[axis]; +#else //TMC2130 + float dist = - axis_home_dir * 0.01f * 64; +#endif //TMC2130 + current_position[axis] -= dist; + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + current_position[axis] += dist; + destination[axis] = current_position[axis]; + plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], 0.5f*feedrate/60, active_extruder); + st_synchronize(); + + feedrate = 0.0; + } + else if ((axis==Z_AXIS)?HOMEAXIS_DO(Z):0) + { +#ifdef TMC2130 + FORCE_HIGH_POWER_START; +#endif + int axis_home_dir = home_dir(axis); + current_position[axis] = 0; + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + destination[axis] = 1.5 * max_length(axis) * axis_home_dir; + feedrate = homing_feedrate[axis]; + plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); + st_synchronize(); +#ifdef TMC2130 + if (READ(Z_TMC2130_DIAG) != 0) { //Z crash + FORCE_HIGH_POWER_END; + kill(_T(MSG_BED_LEVELING_FAILED_POINT_LOW)); + return; + } +#endif //TMC2130 + current_position[axis] = 0; + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + destination[axis] = -home_retract_mm(axis) * axis_home_dir; + plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); + st_synchronize(); + destination[axis] = 2*home_retract_mm(axis) * axis_home_dir; + feedrate = homing_feedrate[axis]/2 ; + plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); + st_synchronize(); +#ifdef TMC2130 + if (READ(Z_TMC2130_DIAG) != 0) { //Z crash + FORCE_HIGH_POWER_END; + kill(_T(MSG_BED_LEVELING_FAILED_POINT_LOW)); + return; + } +#endif //TMC2130 + axis_is_at_home(axis); + destination[axis] = current_position[axis]; + feedrate = 0.0; + endstops_hit_on_purpose(); + axis_known_position[axis] = true; +#ifdef TMC2130 + FORCE_HIGH_POWER_END; +#endif + } + enable_endstops(endstops_enabled); +} + +/**/ +void home_xy() +{ + set_destination_to_current(); + homeaxis(X_AXIS); + homeaxis(Y_AXIS); + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + endstops_hit_on_purpose(); +} + +void refresh_cmd_timeout(void) +{ + previous_millis_cmd = _millis(); +} + +#ifdef FWRETRACT + void retract(bool retracting, bool swapretract = false) { + if(retracting && !retracted[active_extruder]) { + destination[X_AXIS]=current_position[X_AXIS]; + destination[Y_AXIS]=current_position[Y_AXIS]; + destination[Z_AXIS]=current_position[Z_AXIS]; + destination[E_AXIS]=current_position[E_AXIS]; + current_position[E_AXIS]+=(swapretract?retract_length_swap:cs.retract_length)*float(extrudemultiply)*0.01f; + plan_set_e_position(current_position[E_AXIS]); + float oldFeedrate = feedrate; + feedrate=cs.retract_feedrate*60; + retracted[active_extruder]=true; + prepare_move(); + current_position[Z_AXIS]-=cs.retract_zlift; + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + prepare_move(); + feedrate = oldFeedrate; + } else if(!retracting && retracted[active_extruder]) { + destination[X_AXIS]=current_position[X_AXIS]; + destination[Y_AXIS]=current_position[Y_AXIS]; + destination[Z_AXIS]=current_position[Z_AXIS]; + destination[E_AXIS]=current_position[E_AXIS]; + current_position[Z_AXIS]+=cs.retract_zlift; + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + current_position[E_AXIS]-=(swapretract?(retract_length_swap+retract_recover_length_swap):(cs.retract_length+cs.retract_recover_length))*float(extrudemultiply)*0.01f; + plan_set_e_position(current_position[E_AXIS]); + float oldFeedrate = feedrate; + feedrate=cs.retract_recover_feedrate*60; + retracted[active_extruder]=false; + prepare_move(); + feedrate = oldFeedrate; + } + } //retract +#endif //FWRETRACT + +void trace() { + Sound_MakeCustom(25,440,true); +} +/* +void ramming() { +// float tmp[4] = DEFAULT_MAX_FEEDRATE; + if (current_temperature[0] < 230) { + //PLA + + max_feedrate[E_AXIS] = 50; + //current_position[E_AXIS] -= 8; + //plan_buffer_line_curposXYZE(2100 / 60, active_extruder); + //current_position[E_AXIS] += 8; + //plan_buffer_line_curposXYZE(2100 / 60, active_extruder); + current_position[E_AXIS] += 5.4; + plan_buffer_line_curposXYZE(2800 / 60, active_extruder); + current_position[E_AXIS] += 3.2; + plan_buffer_line_curposXYZE(3000 / 60, active_extruder); + current_position[E_AXIS] += 3; + plan_buffer_line_curposXYZE(3400 / 60, active_extruder); + st_synchronize(); + max_feedrate[E_AXIS] = 80; + current_position[E_AXIS] -= 82; + plan_buffer_line_curposXYZE(9500 / 60, active_extruder); + max_feedrate[E_AXIS] = 50;//tmp[E_AXIS]; + current_position[E_AXIS] -= 20; + plan_buffer_line_curposXYZE(1200 / 60, active_extruder); + current_position[E_AXIS] += 5; + plan_buffer_line_curposXYZE(400 / 60, active_extruder); + current_position[E_AXIS] += 5; + plan_buffer_line_curposXYZE(600 / 60, active_extruder); + current_position[E_AXIS] -= 10; + st_synchronize(); + plan_buffer_line_curposXYZE(600 / 60, active_extruder); + current_position[E_AXIS] += 10; + plan_buffer_line_curposXYZE(600 / 60, active_extruder); + current_position[E_AXIS] -= 10; + plan_buffer_line_curposXYZE(800 / 60, active_extruder); + current_position[E_AXIS] += 10; + plan_buffer_line_curposXYZE(800 / 60, active_extruder); + current_position[E_AXIS] -= 10; + plan_buffer_line_curposXYZE(800 / 60, active_extruder); + st_synchronize(); + } + else { + //ABS + max_feedrate[E_AXIS] = 50; + //current_position[E_AXIS] -= 8; + //plan_buffer_line_curposXYZE(2100 / 60, active_extruder); + //current_position[E_AXIS] += 8; + //plan_buffer_line_curposXYZE(2100 / 60, active_extruder); + current_position[E_AXIS] += 3.1; + plan_buffer_line_curposXYZE(2000 / 60, active_extruder); + current_position[E_AXIS] += 3.1; + plan_buffer_line_curposXYZE(2500 / 60, active_extruder); + current_position[E_AXIS] += 4; + plan_buffer_line_curposXYZE(3000 / 60, active_extruder); + st_synchronize(); + //current_position[X_AXIS] += 23; //delay + //plan_buffer_line_curposXYZE(600/60, active_extruder); //delay + //current_position[X_AXIS] -= 23; //delay + //plan_buffer_line_curposXYZE(600/60, active_extruder); //delay + _delay(4700); + max_feedrate[E_AXIS] = 80; + current_position[E_AXIS] -= 92; + plan_buffer_line_curposXYZE(9900 / 60, active_extruder); + max_feedrate[E_AXIS] = 50;//tmp[E_AXIS]; + current_position[E_AXIS] -= 5; + plan_buffer_line_curposXYZE(800 / 60, active_extruder); + current_position[E_AXIS] += 5; + plan_buffer_line_curposXYZE(400 / 60, active_extruder); + current_position[E_AXIS] -= 5; + plan_buffer_line_curposXYZE(600 / 60, active_extruder); + st_synchronize(); + current_position[E_AXIS] += 5; + plan_buffer_line_curposXYZE(600 / 60, active_extruder); + current_position[E_AXIS] -= 5; + plan_buffer_line_curposXYZE(600 / 60, active_extruder); + current_position[E_AXIS] += 5; + plan_buffer_line_curposXYZE(600 / 60, active_extruder); + current_position[E_AXIS] -= 5; + plan_buffer_line_curposXYZE(600 / 60, active_extruder); + st_synchronize(); + + } + } +*/ + +#ifdef TMC2130 +void force_high_power_mode(bool start_high_power_section) { +#ifdef PSU_Delta + if (start_high_power_section == true) enable_force_z(); +#endif //PSU_Delta + uint8_t silent; + silent = eeprom_read_byte((uint8_t*)EEPROM_SILENT); + if (silent == 1) { + //we are in silent mode, set to normal mode to enable crash detection + + // Wait for the planner queue to drain and for the stepper timer routine to reach an idle state. + st_synchronize(); + cli(); + tmc2130_mode = (start_high_power_section == true) ? TMC2130_MODE_NORMAL : TMC2130_MODE_SILENT; + update_mode_profile(); + tmc2130_init(); + // We may have missed a stepper timer interrupt due to the time spent in the tmc2130_init() routine. + // Be safe than sorry, reset the stepper timer before re-enabling interrupts. + st_reset_timer(); + sei(); + } +} +#endif //TMC2130 + +#ifdef TMC2130 +static void gcode_G28(bool home_x_axis, long home_x_value, bool home_y_axis, long home_y_value, bool home_z_axis, long home_z_value, bool calib, bool without_mbl) +#else +static void gcode_G28(bool home_x_axis, long home_x_value, bool home_y_axis, long home_y_value, bool home_z_axis, long home_z_value, bool without_mbl) +#endif //TMC2130 +{ + st_synchronize(); + +#if 0 + SERIAL_ECHOPGM("G28, initial "); print_world_coordinates(); + SERIAL_ECHOPGM("G28, initial "); print_physical_coordinates(); +#endif + + // Flag for the display update routine and to disable the print cancelation during homing. + homing_flag = true; + + // Which axes should be homed? + bool home_x = home_x_axis; + bool home_y = home_y_axis; + bool home_z = home_z_axis; + + // Either all X,Y,Z codes are present, or none of them. + bool home_all_axes = home_x == home_y && home_x == home_z; + if (home_all_axes) + // No X/Y/Z code provided means to home all axes. + home_x = home_y = home_z = true; + + //if we are homing all axes, first move z higher to protect heatbed/steel sheet + if (home_all_axes) { + current_position[Z_AXIS] += MESH_HOME_Z_SEARCH; + feedrate = homing_feedrate[Z_AXIS]; + plan_buffer_line_curposXYZE(feedrate / 60, active_extruder); + st_synchronize(); + } +#ifdef ENABLE_AUTO_BED_LEVELING + plan_bed_level_matrix.set_to_identity(); //Reset the plane ("erase" all leveling data) +#endif //ENABLE_AUTO_BED_LEVELING + + // Reset world2machine_rotation_and_skew and world2machine_shift, therefore + // the planner will not perform any adjustments in the XY plane. + // Wait for the motors to stop and update the current position with the absolute values. + world2machine_revert_to_uncorrected(); + + // For mesh bed leveling deactivate the matrix temporarily. + // It is necessary to disable the bed leveling for the X and Y homing moves, so that the move is performed + // in a single axis only. + // In case of re-homing the X or Y axes only, the mesh bed leveling is restored after G28. +#ifdef MESH_BED_LEVELING + uint8_t mbl_was_active = mbl.active; + mbl.active = 0; + current_position[Z_AXIS] = st_get_position_mm(Z_AXIS); +#endif + + // Reset baby stepping to zero, if the babystepping has already been loaded before. The babystepsTodo value will be + // consumed during the first movements following this statement. + if (home_z) + babystep_undo(); + + saved_feedrate = feedrate; + int l_feedmultiply = feedmultiply; + feedmultiply = 100; + previous_millis_cmd = _millis(); + + enable_endstops(true); + + memcpy(destination, current_position, sizeof(destination)); + feedrate = 0.0; + + #if Z_HOME_DIR > 0 // If homing away from BED do Z first + if(home_z) + homeaxis(Z_AXIS); + #endif + + #ifdef QUICK_HOME + // In the quick mode, if both x and y are to be homed, a diagonal move will be performed initially. + if(home_x && home_y) //first diagonal move + { + current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0; + + int x_axis_home_dir = home_dir(X_AXIS); + + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + destination[X_AXIS] = 1.5 * max_length(X_AXIS) * x_axis_home_dir;destination[Y_AXIS] = 1.5 * max_length(Y_AXIS) * home_dir(Y_AXIS); + feedrate = homing_feedrate[X_AXIS]; + if(homing_feedrate[Y_AXIS] max_length(Y_AXIS)) { + feedrate *= sqrt(pow(max_length(Y_AXIS) / max_length(X_AXIS), 2) + 1); + } else { + feedrate *= sqrt(pow(max_length(X_AXIS) / max_length(Y_AXIS), 2) + 1); + } + plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); + st_synchronize(); + + axis_is_at_home(X_AXIS); + axis_is_at_home(Y_AXIS); + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + destination[X_AXIS] = current_position[X_AXIS]; + destination[Y_AXIS] = current_position[Y_AXIS]; + plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); + feedrate = 0.0; + st_synchronize(); + endstops_hit_on_purpose(); + + current_position[X_AXIS] = destination[X_AXIS]; + current_position[Y_AXIS] = destination[Y_AXIS]; + current_position[Z_AXIS] = destination[Z_AXIS]; + } + #endif /* QUICK_HOME */ + +#ifdef TMC2130 + if(home_x) + { + if (!calib) + homeaxis(X_AXIS); + else + tmc2130_home_calibrate(X_AXIS); + } + + if(home_y) + { + if (!calib) + homeaxis(Y_AXIS); + else + tmc2130_home_calibrate(Y_AXIS); + } +#else //TMC2130 + if(home_x) homeaxis(X_AXIS); + if(home_y) homeaxis(Y_AXIS); +#endif //TMC2130 + + + if(home_x_axis && home_x_value != 0) + current_position[X_AXIS]=home_x_value+cs.add_homing[X_AXIS]; + + if(home_y_axis && home_y_value != 0) + current_position[Y_AXIS]=home_y_value+cs.add_homing[Y_AXIS]; + + #if Z_HOME_DIR < 0 // If homing towards BED do Z last + #ifndef Z_SAFE_HOMING + if(home_z) { + #if defined (Z_RAISE_BEFORE_HOMING) && (Z_RAISE_BEFORE_HOMING > 0) + destination[Z_AXIS] = Z_RAISE_BEFORE_HOMING * home_dir(Z_AXIS) * (-1); // Set destination away from bed + feedrate = max_feedrate[Z_AXIS]; + plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate, active_extruder); + st_synchronize(); + #endif // defined (Z_RAISE_BEFORE_HOMING) && (Z_RAISE_BEFORE_HOMING > 0) + #if (defined(MESH_BED_LEVELING) && !defined(MK1BP)) // If Mesh bed leveling, move X&Y to safe position for home + if (!(axis_known_position[X_AXIS] && axis_known_position[Y_AXIS] )) + { + homeaxis(X_AXIS); + homeaxis(Y_AXIS); + } + // 1st mesh bed leveling measurement point, corrected. + world2machine_initialize(); + world2machine(pgm_read_float(bed_ref_points_4), pgm_read_float(bed_ref_points_4+1), destination[X_AXIS], destination[Y_AXIS]); + world2machine_reset(); + if (destination[Y_AXIS] < Y_MIN_POS) + destination[Y_AXIS] = Y_MIN_POS; + destination[Z_AXIS] = MESH_HOME_Z_SEARCH; // Set destination away from bed + feedrate = homing_feedrate[Z_AXIS]/10; + current_position[Z_AXIS] = 0; + enable_endstops(false); +#ifdef DEBUG_BUILD + SERIAL_ECHOLNPGM("plan_set_position()"); + MYSERIAL.println(current_position[X_AXIS]);MYSERIAL.println(current_position[Y_AXIS]); + MYSERIAL.println(current_position[Z_AXIS]);MYSERIAL.println(current_position[E_AXIS]); +#endif + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); +#ifdef DEBUG_BUILD + SERIAL_ECHOLNPGM("plan_buffer_line()"); + MYSERIAL.println(destination[X_AXIS]);MYSERIAL.println(destination[Y_AXIS]); + MYSERIAL.println(destination[Z_AXIS]);MYSERIAL.println(destination[E_AXIS]); + MYSERIAL.println(feedrate);MYSERIAL.println(active_extruder); +#endif + plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate, active_extruder); + st_synchronize(); + current_position[X_AXIS] = destination[X_AXIS]; + current_position[Y_AXIS] = destination[Y_AXIS]; + enable_endstops(true); + endstops_hit_on_purpose(); + homeaxis(Z_AXIS); + #else // MESH_BED_LEVELING + homeaxis(Z_AXIS); + #endif // MESH_BED_LEVELING + } + #else // defined(Z_SAFE_HOMING): Z Safe mode activated. + if(home_all_axes) { + destination[X_AXIS] = round(Z_SAFE_HOMING_X_POINT - X_PROBE_OFFSET_FROM_EXTRUDER); + destination[Y_AXIS] = round(Z_SAFE_HOMING_Y_POINT - Y_PROBE_OFFSET_FROM_EXTRUDER); + destination[Z_AXIS] = Z_RAISE_BEFORE_HOMING * home_dir(Z_AXIS) * (-1); // Set destination away from bed + feedrate = XY_TRAVEL_SPEED/60; + current_position[Z_AXIS] = 0; + + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate, active_extruder); + st_synchronize(); + current_position[X_AXIS] = destination[X_AXIS]; + current_position[Y_AXIS] = destination[Y_AXIS]; + + homeaxis(Z_AXIS); + } + // Let's see if X and Y are homed and probe is inside bed area. + if(home_z) { + if ( (axis_known_position[X_AXIS]) && (axis_known_position[Y_AXIS]) \ + && (current_position[X_AXIS]+X_PROBE_OFFSET_FROM_EXTRUDER >= X_MIN_POS) \ + && (current_position[X_AXIS]+X_PROBE_OFFSET_FROM_EXTRUDER <= X_MAX_POS) \ + && (current_position[Y_AXIS]+Y_PROBE_OFFSET_FROM_EXTRUDER >= Y_MIN_POS) \ + && (current_position[Y_AXIS]+Y_PROBE_OFFSET_FROM_EXTRUDER <= Y_MAX_POS)) { + + current_position[Z_AXIS] = 0; + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + destination[Z_AXIS] = Z_RAISE_BEFORE_HOMING * home_dir(Z_AXIS) * (-1); // Set destination away from bed + feedrate = max_feedrate[Z_AXIS]; + plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate, active_extruder); + st_synchronize(); + + homeaxis(Z_AXIS); + } else if (!((axis_known_position[X_AXIS]) && (axis_known_position[Y_AXIS]))) { + LCD_MESSAGERPGM(MSG_POSITION_UNKNOWN); + SERIAL_ECHO_START; + SERIAL_ECHOLNRPGM(MSG_POSITION_UNKNOWN); + } else { + LCD_MESSAGERPGM(MSG_ZPROBE_OUT); + SERIAL_ECHO_START; + SERIAL_ECHOLNRPGM(MSG_ZPROBE_OUT); + } + } + #endif // Z_SAFE_HOMING + #endif // Z_HOME_DIR < 0 + + if(home_z_axis && home_z_value != 0) + current_position[Z_AXIS]=home_z_value+cs.add_homing[Z_AXIS]; + #ifdef ENABLE_AUTO_BED_LEVELING + if(home_z) + current_position[Z_AXIS] += cs.zprobe_zoffset; //Add Z_Probe offset (the distance is negative) + #endif + + // Set the planner and stepper routine positions. + // At this point the mesh bed leveling and world2machine corrections are disabled and current_position + // contains the machine coordinates. + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + + #ifdef ENDSTOPS_ONLY_FOR_HOMING + enable_endstops(false); + #endif + + feedrate = saved_feedrate; + feedmultiply = l_feedmultiply; + previous_millis_cmd = _millis(); + endstops_hit_on_purpose(); +#ifndef MESH_BED_LEVELING +//-// Oct 2019 :: this part of code is (from) now probably un-compilable + // If MESH_BED_LEVELING is not active, then it is the original Prusa i3. + // Offer the user to load the baby step value, which has been adjusted at the previous print session. + if(card.sdprinting && eeprom_read_word((uint16_t *)EEPROM_BABYSTEP_Z)) + lcd_adjust_z(); +#endif + + // Load the machine correction matrix + world2machine_initialize(); + // and correct the current_position XY axes to match the transformed coordinate system. + world2machine_update_current(); + +#if (defined(MESH_BED_LEVELING) && !defined(MK1BP)) + if (home_x_axis || home_y_axis || without_mbl || home_z_axis) + { + if (! home_z && mbl_was_active) { + // Re-enable the mesh bed leveling if only the X and Y axes were re-homed. + mbl.active = true; + // and re-adjust the current logical Z axis with the bed leveling offset applicable at the current XY position. + current_position[Z_AXIS] -= mbl.get_z(st_get_position_mm(X_AXIS), st_get_position_mm(Y_AXIS)); + } + } + else + { + st_synchronize(); + homing_flag = false; + } +#endif + + if (farm_mode) { prusa_statistics(20); }; + + homing_flag = false; +#if 0 + SERIAL_ECHOPGM("G28, final "); print_world_coordinates(); + SERIAL_ECHOPGM("G28, final "); print_physical_coordinates(); + SERIAL_ECHOPGM("G28, final "); print_mesh_bed_leveling_table(); +#endif +} + +static void gcode_G28(bool home_x_axis, bool home_y_axis, bool home_z_axis) +{ +#ifdef TMC2130 + gcode_G28(home_x_axis, 0, home_y_axis, 0, home_z_axis, 0, false, true); +#else + gcode_G28(home_x_axis, 0, home_y_axis, 0, home_z_axis, 0, true); +#endif //TMC2130 +} + +void adjust_bed_reset() +{ + eeprom_update_byte((unsigned char*)EEPROM_BED_CORRECTION_VALID, 1); + eeprom_update_byte((unsigned char*)EEPROM_BED_CORRECTION_LEFT, 0); + eeprom_update_byte((unsigned char*)EEPROM_BED_CORRECTION_RIGHT, 0); + eeprom_update_byte((unsigned char*)EEPROM_BED_CORRECTION_FRONT, 0); + eeprom_update_byte((unsigned char*)EEPROM_BED_CORRECTION_REAR, 0); +} + +//! @brief Calibrate XYZ +//! @param onlyZ if true, calibrate only Z axis +//! @param verbosity_level +//! @retval true Succeeded +//! @retval false Failed +bool gcode_M45(bool onlyZ, int8_t verbosity_level) +{ + bool final_result = false; + #ifdef TMC2130 + FORCE_HIGH_POWER_START; + #endif // TMC2130 + // Only Z calibration? + if (!onlyZ) + { + setTargetBed(0); + setAllTargetHotends(0); + adjust_bed_reset(); //reset bed level correction + } + + // Disable the default update procedure of the display. We will do a modal dialog. + lcd_update_enable(false); + // Let the planner use the uncorrected coordinates. + mbl.reset(); + // Reset world2machine_rotation_and_skew and world2machine_shift, therefore + // the planner will not perform any adjustments in the XY plane. + // Wait for the motors to stop and update the current position with the absolute values. + world2machine_revert_to_uncorrected(); + // Reset the baby step value applied without moving the axes. + babystep_reset(); + // Mark all axes as in a need for homing. + memset(axis_known_position, 0, sizeof(axis_known_position)); + + // Home in the XY plane. + //set_destination_to_current(); + int l_feedmultiply = setup_for_endstop_move(); + lcd_display_message_fullscreen_P(_T(MSG_AUTO_HOME)); + home_xy(); + + enable_endstops(false); + current_position[X_AXIS] += 5; + current_position[Y_AXIS] += 5; + plan_buffer_line_curposXYZE(homing_feedrate[Z_AXIS] / 40, active_extruder); + st_synchronize(); + + // Let the user move the Z axes up to the end stoppers. +#ifdef TMC2130 + if (calibrate_z_auto()) + { +#else //TMC2130 + if (lcd_calibrate_z_end_stop_manual(onlyZ)) + { +#endif //TMC2130 + + lcd_show_fullscreen_message_and_wait_P(_T(MSG_CONFIRM_NOZZLE_CLEAN)); + if(onlyZ){ + lcd_display_message_fullscreen_P(_T(MSG_MEASURE_BED_REFERENCE_HEIGHT_LINE1)); + lcd_set_cursor(0, 3); + lcd_print(1); + lcd_puts_P(_T(MSG_MEASURE_BED_REFERENCE_HEIGHT_LINE2)); + }else{ + //lcd_show_fullscreen_message_and_wait_P(_T(MSG_PAPER)); + lcd_display_message_fullscreen_P(_T(MSG_FIND_BED_OFFSET_AND_SKEW_LINE1)); + lcd_set_cursor(0, 2); + lcd_print(1); + lcd_puts_P(_T(MSG_FIND_BED_OFFSET_AND_SKEW_LINE2)); + } + + refresh_cmd_timeout(); + #ifndef STEEL_SHEET + if (((degHotend(0) > MAX_HOTEND_TEMP_CALIBRATION) || (degBed() > MAX_BED_TEMP_CALIBRATION)) && (!onlyZ)) + { + lcd_wait_for_cool_down(); + } + #endif //STEEL_SHEET + if(!onlyZ) + { + KEEPALIVE_STATE(PAUSED_FOR_USER); + #ifdef STEEL_SHEET + bool result = lcd_show_fullscreen_message_yes_no_and_wait_P(_T(MSG_STEEL_SHEET_CHECK), false, false); + if(result) lcd_show_fullscreen_message_and_wait_P(_T(MSG_REMOVE_STEEL_SHEET)); + #endif //STEEL_SHEET + lcd_show_fullscreen_message_and_wait_P(_T(MSG_PAPER)); + KEEPALIVE_STATE(IN_HANDLER); + lcd_display_message_fullscreen_P(_T(MSG_FIND_BED_OFFSET_AND_SKEW_LINE1)); + lcd_set_cursor(0, 2); + lcd_print(1); + lcd_puts_P(_T(MSG_FIND_BED_OFFSET_AND_SKEW_LINE2)); + } + + bool endstops_enabled = enable_endstops(false); + current_position[Z_AXIS] -= 1; //move 1mm down with disabled endstop + plan_buffer_line_curposXYZE(homing_feedrate[Z_AXIS] / 40, active_extruder); + st_synchronize(); + + // Move the print head close to the bed. + current_position[Z_AXIS] = MESH_HOME_Z_SEARCH; + + enable_endstops(true); +#ifdef TMC2130 + tmc2130_home_enter(Z_AXIS_MASK); +#endif //TMC2130 + + plan_buffer_line_curposXYZE(homing_feedrate[Z_AXIS] / 40, active_extruder); + + st_synchronize(); +#ifdef TMC2130 + tmc2130_home_exit(); +#endif //TMC2130 + enable_endstops(endstops_enabled); + + if ((st_get_position_mm(Z_AXIS) <= (MESH_HOME_Z_SEARCH + HOME_Z_SEARCH_THRESHOLD)) && + (st_get_position_mm(Z_AXIS) >= (MESH_HOME_Z_SEARCH - HOME_Z_SEARCH_THRESHOLD))) + { + if (onlyZ) + { + clean_up_after_endstop_move(l_feedmultiply); + // Z only calibration. + // Load the machine correction matrix + world2machine_initialize(); + // and correct the current_position to match the transformed coordinate system. + world2machine_update_current(); + //FIXME + bool result = sample_mesh_and_store_reference(); + if (result) + { + if (calibration_status() == CALIBRATION_STATUS_Z_CALIBRATION) + // Shipped, the nozzle height has been set already. The user can start printing now. + calibration_status_store(CALIBRATION_STATUS_CALIBRATED); + final_result = true; + // babystep_apply(); + } + } + else + { + // Reset the baby step value and the baby step applied flag. + calibration_status_store(CALIBRATION_STATUS_XYZ_CALIBRATION); + eeprom_update_word(reinterpret_cast(&(EEPROM_Sheets_base->s[(eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)))].z_offset)),0); + // Complete XYZ calibration. + uint8_t point_too_far_mask = 0; + BedSkewOffsetDetectionResultType result = find_bed_offset_and_skew(verbosity_level, point_too_far_mask); + clean_up_after_endstop_move(l_feedmultiply); + // Print head up. + current_position[Z_AXIS] = MESH_HOME_Z_SEARCH; + plan_buffer_line_curposXYZE(homing_feedrate[Z_AXIS] / 40, active_extruder); + st_synchronize(); +//#ifndef NEW_XYZCAL + if (result >= 0) + { + #ifdef HEATBED_V2 + sample_z(); + #else //HEATBED_V2 + point_too_far_mask = 0; + // Second half: The fine adjustment. + // Let the planner use the uncorrected coordinates. + mbl.reset(); + world2machine_reset(); + // Home in the XY plane. + int l_feedmultiply = setup_for_endstop_move(); + home_xy(); + result = improve_bed_offset_and_skew(1, verbosity_level, point_too_far_mask); + clean_up_after_endstop_move(l_feedmultiply); + // Print head up. + current_position[Z_AXIS] = MESH_HOME_Z_SEARCH; + plan_buffer_line_curposXYZE(homing_feedrate[Z_AXIS] / 40, active_extruder); + st_synchronize(); + // if (result >= 0) babystep_apply(); + #endif //HEATBED_V2 + } +//#endif //NEW_XYZCAL + lcd_update_enable(true); + lcd_update(2); + + lcd_bed_calibration_show_result(result, point_too_far_mask); + if (result >= 0) + { + // Calibration valid, the machine should be able to print. Advise the user to run the V2Calibration.gcode. + calibration_status_store(CALIBRATION_STATUS_LIVE_ADJUST); + if (eeprom_read_byte((uint8_t*)EEPROM_WIZARD_ACTIVE) != 1) lcd_show_fullscreen_message_and_wait_P(_T(MSG_BABYSTEP_Z_NOT_SET)); + final_result = true; + } + } +#ifdef TMC2130 + tmc2130_home_exit(); +#endif + } + else + { + lcd_show_fullscreen_message_and_wait_P(PSTR("Calibration failed! Check the axes and run again.")); + final_result = false; + } + } + else + { + // Timeouted. + } + lcd_update_enable(true); +#ifdef TMC2130 + FORCE_HIGH_POWER_END; +#endif // TMC2130 + return final_result; +} + +void gcode_M114() +{ + SERIAL_PROTOCOLPGM("X:"); + SERIAL_PROTOCOL(current_position[X_AXIS]); + SERIAL_PROTOCOLPGM(" Y:"); + SERIAL_PROTOCOL(current_position[Y_AXIS]); + SERIAL_PROTOCOLPGM(" Z:"); + SERIAL_PROTOCOL(current_position[Z_AXIS]); + SERIAL_PROTOCOLPGM(" E:"); + SERIAL_PROTOCOL(current_position[E_AXIS]); + + SERIAL_PROTOCOLRPGM(_n(" Count X: "));////MSG_COUNT_X + SERIAL_PROTOCOL(float(st_get_position(X_AXIS)) / cs.axis_steps_per_unit[X_AXIS]); + SERIAL_PROTOCOLPGM(" Y:"); + SERIAL_PROTOCOL(float(st_get_position(Y_AXIS)) / cs.axis_steps_per_unit[Y_AXIS]); + SERIAL_PROTOCOLPGM(" Z:"); + SERIAL_PROTOCOL(float(st_get_position(Z_AXIS)) / cs.axis_steps_per_unit[Z_AXIS]); + SERIAL_PROTOCOLPGM(" E:"); + SERIAL_PROTOCOL(float(st_get_position(E_AXIS)) / cs.axis_steps_per_unit[E_AXIS]); + + SERIAL_PROTOCOLLN(""); +} + +//! extracted code to compute z_shift for M600 in case of filament change operation +//! requested from fsensors. +//! The function ensures, that the printhead lifts to at least 25mm above the heat bed +//! unlike the previous implementation, which was adding 25mm even when the head was +//! printing at e.g. 24mm height. +//! A safety margin of FILAMENTCHANGE_ZADD is added in all cases to avoid touching +//! the printout. +//! This function is templated to enable fast change of computation data type. +//! @return new z_shift value +template +static T gcode_M600_filament_change_z_shift() +{ +#ifdef FILAMENTCHANGE_ZADD + static_assert(Z_MAX_POS < (255 - FILAMENTCHANGE_ZADD), "Z-range too high, change the T type from uint8_t to uint16_t"); + // avoid floating point arithmetics when not necessary - results in shorter code + T ztmp = T( current_position[Z_AXIS] ); + T z_shift = 0; + if(ztmp < T(25)){ + z_shift = T(25) - ztmp; // make sure to be at least 25mm above the heat bed + } + return z_shift + T(FILAMENTCHANGE_ZADD); // always move above printout +#else + return T(0); +#endif +} + +static void gcode_M600(bool automatic, float x_position, float y_position, float z_shift, float e_shift, float /*e_shift_late*/) +{ + st_synchronize(); + float lastpos[4]; + + if (farm_mode) + { + prusa_statistics(22); + } + + //First backup current position and settings + int feedmultiplyBckp = feedmultiply; + float HotendTempBckp = degTargetHotend(active_extruder); + int fanSpeedBckp = fanSpeed; + + lastpos[X_AXIS] = current_position[X_AXIS]; + lastpos[Y_AXIS] = current_position[Y_AXIS]; + lastpos[Z_AXIS] = current_position[Z_AXIS]; + lastpos[E_AXIS] = current_position[E_AXIS]; + + //Retract E + current_position[E_AXIS] += e_shift; + plan_buffer_line_curposXYZE(FILAMENTCHANGE_RFEED, active_extruder); + st_synchronize(); + + //Lift Z + current_position[Z_AXIS] += z_shift; + plan_buffer_line_curposXYZE(FILAMENTCHANGE_ZFEED, active_extruder); + st_synchronize(); + + //Move XY to side + current_position[X_AXIS] = x_position; + current_position[Y_AXIS] = y_position; + plan_buffer_line_curposXYZE(FILAMENTCHANGE_XYFEED, active_extruder); + st_synchronize(); + + //Beep, manage nozzle heater and wait for user to start unload filament + if(!mmu_enabled) M600_wait_for_user(HotendTempBckp); + + lcd_change_fil_state = 0; + + // Unload filament + if (mmu_enabled) extr_unload(); //unload just current filament for multimaterial printers (used also in M702) + else unload_filament(); //unload filament for single material (used also in M702) + //finish moves + st_synchronize(); + + if (!mmu_enabled) + { + KEEPALIVE_STATE(PAUSED_FOR_USER); + lcd_change_fil_state = lcd_show_fullscreen_message_yes_no_and_wait_P(_i("Was filament unload successful?"), + false, true); ////MSG_UNLOAD_SUCCESSFUL c=20 r=2 + if (lcd_change_fil_state == 0) + { + lcd_clear(); + lcd_set_cursor(0, 2); + lcd_puts_P(_T(MSG_PLEASE_WAIT)); + current_position[X_AXIS] -= 100; + plan_buffer_line_curposXYZE(FILAMENTCHANGE_XYFEED, active_extruder); + st_synchronize(); + lcd_show_fullscreen_message_and_wait_P(_i("Please open idler and remove filament manually."));////MSG_CHECK_IDLER c=20 r=4 + } + } + + if (mmu_enabled) + { + if (!automatic) { + if (saved_printing) mmu_eject_filament(mmu_extruder, false); //if M600 was invoked by filament senzor (FINDA) eject filament so user can easily remove it + mmu_M600_wait_and_beep(); + if (saved_printing) { + + lcd_clear(); + lcd_set_cursor(0, 2); + lcd_puts_P(_T(MSG_PLEASE_WAIT)); + + mmu_command(MmuCmd::R0); + manage_response(false, false); + } + } + mmu_M600_load_filament(automatic, HotendTempBckp); + } + else + M600_load_filament(); + + if (!automatic) M600_check_state(HotendTempBckp); + + lcd_update_enable(true); + + //Not let's go back to print + fanSpeed = fanSpeedBckp; + + //Feed a little of filament to stabilize pressure + if (!automatic) + { + current_position[E_AXIS] += FILAMENTCHANGE_RECFEED; + plan_buffer_line_curposXYZE(FILAMENTCHANGE_EXFEED, active_extruder); + } + + //Move XY back + plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], + FILAMENTCHANGE_XYFEED, active_extruder); + st_synchronize(); + //Move Z back + plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], current_position[E_AXIS], + FILAMENTCHANGE_ZFEED, active_extruder); + st_synchronize(); + + //Set E position to original + plan_set_e_position(lastpos[E_AXIS]); + + memcpy(current_position, lastpos, sizeof(lastpos)); + memcpy(destination, current_position, sizeof(current_position)); + + //Recover feed rate + feedmultiply = feedmultiplyBckp; + char cmd[9]; + sprintf_P(cmd, PSTR("M220 S%i"), feedmultiplyBckp); + enquecommand(cmd); + +#ifdef IR_SENSOR + //this will set fsensor_watch_autoload to correct value and prevent possible M701 gcode enqueuing when M600 is finished + fsensor_check_autoload(); +#endif //IR_SENSOR + + lcd_setstatuspgm(_T(WELCOME_MSG)); + custom_message_type = CustomMsg::Status; +} + +//! @brief Rise Z if too low to avoid blob/jam before filament loading +//! +//! It doesn't plan_buffer_line(), as it expects plan_buffer_line() to be called after +//! during extruding (loading) filament. +void marlin_rise_z(void) +{ + if (current_position[Z_AXIS] < 20) current_position[Z_AXIS] += 30; +} + +void gcode_M701() +{ + printf_P(PSTR("gcode_M701 begin\n")); + + if (farm_mode) + { + prusa_statistics(22); + } + + if (mmu_enabled) + { + extr_adj(tmp_extruder);//loads current extruder + mmu_extruder = tmp_extruder; + } + else + { + enable_z(); + custom_message_type = CustomMsg::FilamentLoading; + +#ifdef FSENSOR_QUALITY + fsensor_oq_meassure_start(40); +#endif //FSENSOR_QUALITY + + lcd_setstatuspgm(_T(MSG_LOADING_FILAMENT)); + current_position[E_AXIS] += 40; + plan_buffer_line_curposXYZE(400 / 60, active_extruder); //fast sequence + st_synchronize(); + + marlin_rise_z(); + current_position[E_AXIS] += 30; + plan_buffer_line_curposXYZE(400 / 60, active_extruder); //fast sequence + + load_filament_final_feed(); //slow sequence + st_synchronize(); + + Sound_MakeCustom(50,500,false); + + if (!farm_mode && loading_flag) { + lcd_load_filament_color_check(); + } + lcd_update_enable(true); + lcd_update(2); + lcd_setstatuspgm(_T(WELCOME_MSG)); + disable_z(); + loading_flag = false; + custom_message_type = CustomMsg::Status; + +#ifdef FSENSOR_QUALITY + fsensor_oq_meassure_stop(); + + if (!fsensor_oq_result()) + { + bool disable = lcd_show_fullscreen_message_yes_no_and_wait_P(_i("Fil. sensor response is poor, disable it?"), false, true); + lcd_update_enable(true); + lcd_update(2); + if (disable) + fsensor_disable(); + } +#endif //FSENSOR_QUALITY + } +} +/** + * @brief Get serial number from 32U2 processor + * + * Typical format of S/N is:CZPX0917X003XC13518 + * + * Command operates only in farm mode, if not in farm mode, "Not in farm mode." is written to MYSERIAL. + * + * Send command ;S to serial port 0 to retrieve serial number stored in 32U2 processor, + * reply is transmitted to serial port 1 character by character. + * Operation takes typically 23 ms. If the retransmit is not finished until 100 ms, + * it is interrupted, so less, or no characters are retransmitted, only newline character is send + * in any case. + */ +static void gcode_PRUSA_SN() +{ + if (farm_mode) { + selectedSerialPort = 0; + putchar(';'); + putchar('S'); + int numbersRead = 0; + ShortTimer timeout; + timeout.start(); + + while (numbersRead < 19) { + while (MSerial.available() > 0) { + uint8_t serial_char = MSerial.read(); + selectedSerialPort = 1; + putchar(serial_char); + numbersRead++; + selectedSerialPort = 0; + } + if (timeout.expired(100u)) break; + } + selectedSerialPort = 1; + putchar('\n'); +#if 0 + for (int b = 0; b < 3; b++) { + _tone(BEEPER, 110); + _delay(50); + _noTone(BEEPER); + _delay(50); + } +#endif + } else { + puts_P(_N("Not in farm mode.")); + } +} +//! Detection of faulty RAMBo 1.1b boards equipped with bigger capacitors +//! at the TACH_1 pin, which causes bad detection of print fan speed. +//! Warning: This function is not to be used by ordinary users, it is here only for automated testing purposes, +//! it may even interfere with other functions of the printer! You have been warned! +//! The test idea is to measure the time necessary to charge the capacitor. +//! So the algorithm is as follows: +//! 1. Set TACH_1 pin to INPUT mode and LOW +//! 2. Wait a few ms +//! 3. disable interrupts and measure the time until the TACH_1 pin reaches HIGH +//! Repeat 1.-3. several times +//! Good RAMBo's times are in the range of approx. 260-320 us +//! Bad RAMBo's times are approx. 260-1200 us +//! So basically we are interested in maximum time, the minima are mostly the same. +//! May be that's why the bad RAMBo's still produce some fan RPM reading, but not corresponding to reality +static void gcode_PRUSA_BadRAMBoFanTest(){ + //printf_P(PSTR("Enter fan pin test\n")); +#if !defined(DEBUG_DISABLE_FANCHECK) && defined(FANCHECK) && defined(TACH_1) && TACH_1 >-1 + fan_measuring = false; // prevent EXTINT7 breaking into the measurement + unsigned long tach1max = 0; + uint8_t tach1cntr = 0; + for( /* nothing */; tach1cntr < 100; ++tach1cntr){ + //printf_P(PSTR("TACH_1: %d\n"), tach1cntr); + SET_OUTPUT(TACH_1); + WRITE(TACH_1, LOW); + _delay(20); // the delay may be lower + unsigned long tachMeasure = _micros(); + cli(); + SET_INPUT(TACH_1); + // just wait brutally in an endless cycle until we reach HIGH + // if this becomes a problem it may be improved to non-endless cycle + while( READ(TACH_1) == 0 ) ; + sei(); + tachMeasure = _micros() - tachMeasure; + if( tach1max < tachMeasure ) + tach1max = tachMeasure; + //printf_P(PSTR("TACH_1: %d: capacitor check time=%lu us\n"), (int)tach1cntr, tachMeasure); + } + //printf_P(PSTR("TACH_1: max=%lu us\n"), tach1max); + SERIAL_PROTOCOLPGM("RAMBo FAN "); + if( tach1max > 500 ){ + // bad RAMBo + SERIAL_PROTOCOLLNPGM("BAD"); + } else { + SERIAL_PROTOCOLLNPGM("OK"); + } + // cleanup after the test function + SET_INPUT(TACH_1); + WRITE(TACH_1, HIGH); +#endif +} + +#ifdef BACKLASH_X +extern uint8_t st_backlash_x; +#endif //BACKLASH_X +#ifdef BACKLASH_Y +extern uint8_t st_backlash_y; +#endif //BACKLASH_Y + +//! \ingroup marlin_main + +//! @brief Parse and process commands +//! +//! look here for descriptions of G-codes: http://linuxcnc.org/handbook/gcode/g-code.html +//! http://objects.reprap.org/wiki/Mendel_User_Manual:_RepRapGCodes +//! +//! +//! Implemented Codes +//! ------------------- +//! +//! * _This list is not updated. Current documentation is maintained inside the process_cmd function._ +//! +//!@n PRUSA CODES +//!@n P F - Returns FW versions +//!@n P R - Returns revision of printer +//! +//!@n G0 -> G1 +//!@n G1 - Coordinated Movement X Y Z E +//!@n G2 - CW ARC +//!@n G3 - CCW ARC +//!@n G4 - Dwell S or P +//!@n G10 - retract filament according to settings of M207 +//!@n G11 - retract recover filament according to settings of M208 +//!@n G28 - Home all Axis +//!@n G29 - Detailed Z-Probe, probes the bed at 3 or more points. Will fail if you haven't homed yet. +//!@n G30 - Single Z Probe, probes bed at current XY location. +//!@n G31 - Dock sled (Z_PROBE_SLED only) +//!@n G32 - Undock sled (Z_PROBE_SLED only) +//!@n G80 - Automatic mesh bed leveling +//!@n G81 - Print bed profile +//!@n G90 - Use Absolute Coordinates +//!@n G91 - Use Relative Coordinates +//!@n G92 - Set current position to coordinates given +//! +//!@n M Codes +//!@n M0 - Unconditional stop - Wait for user to press a button on the LCD +//!@n M1 - Same as M0 +//!@n M17 - Enable/Power all stepper motors +//!@n M18 - Disable all stepper motors; same as M84 +//!@n M20 - List SD card +//!@n M21 - Init SD card +//!@n M22 - Release SD card +//!@n M23 - Select SD file (M23 filename.g) +//!@n M24 - Start/resume SD print +//!@n M25 - Pause SD print +//!@n M26 - Set SD position in bytes (M26 S12345) +//!@n M27 - Report SD print status +//!@n M28 - Start SD write (M28 filename.g) +//!@n M29 - Stop SD write +//!@n M30 - Delete file from SD (M30 filename.g) +//!@n M31 - Output time since last M109 or SD card start to serial +//!@n M32 - Select file and start SD print (Can be used _while_ printing from SD card files): +//! syntax "M32 /path/filename#", or "M32 S !filename#" +//! Call gcode file : "M32 P !filename#" and return to caller file after finishing (similar to #include). +//! The '#' is necessary when calling from within sd files, as it stops buffer prereading +//!@n M42 - Change pin status via gcode Use M42 Px Sy to set pin x to value y, when omitting Px the onboard led will be used. +//!@n M73 - Show percent done and print time remaining +//!@n M80 - Turn on Power Supply +//!@n M81 - Turn off Power Supply +//!@n M82 - Set E codes absolute (default) +//!@n M83 - Set E codes relative while in Absolute Coordinates (G90) mode +//!@n M84 - Disable steppers until next move, +//! or use S to specify an inactivity timeout, after which the steppers will be disabled. S0 to disable the timeout. +//!@n M85 - Set inactivity shutdown timer with parameter S. To disable set zero (default) +//!@n M86 - Set safety timer expiration time with parameter S; M86 S0 will disable safety timer +//!@n M92 - Set axis_steps_per_unit - same syntax as G92 +//!@n M104 - Set extruder target temp +//!@n M105 - Read current temp +//!@n M106 - Fan on +//!@n M107 - Fan off +//!@n M109 - Sxxx Wait for extruder current temp to reach target temp. Waits only when heating +//! Rxxx Wait for extruder current temp to reach target temp. Waits when heating and cooling +//! IF AUTOTEMP is enabled, S B F. Exit autotemp by any M109 without F +//!@n M112 - Emergency stop +//!@n M113 - Get or set the timeout interval for Host Keepalive "busy" messages +//!@n M114 - Output current position to serial port +//!@n M115 - Capabilities string +//!@n M117 - display message +//!@n M119 - Output Endstop status to serial port +//!@n M126 - Solenoid Air Valve Open (BariCUDA support by jmil) +//!@n M127 - Solenoid Air Valve Closed (BariCUDA vent to atmospheric pressure by jmil) +//!@n M128 - EtoP Open (BariCUDA EtoP = electricity to air pressure transducer by jmil) +//!@n M129 - EtoP Closed (BariCUDA EtoP = electricity to air pressure transducer by jmil) +//!@n M140 - Set bed target temp +//!@n M150 - Set BlinkM Color Output R: Red<0-255> U(!): Green<0-255> B: Blue<0-255> over i2c, G for green does not work. +//!@n M190 - Sxxx Wait for bed current temp to reach target temp. Waits only when heating +//! Rxxx Wait for bed current temp to reach target temp. Waits when heating and cooling +//!@n M200 D- set filament diameter and set E axis units to cubic millimeters (use S0 to set back to millimeters). +//!@n M201 - Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000) +//!@n M202 - Set max acceleration in units/s^2 for travel moves (M202 X1000 Y1000) Unused in Marlin!! +//!@n M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec +//!@n M204 - Set default acceleration: S normal moves T filament only moves (M204 S3000 T7000) in mm/sec^2 also sets minimum segment time in ms (B20000) to prevent buffer under-runs and M20 minimum feedrate +//!@n M205 - advanced settings: minimum travel speed S=while printing T=travel only, B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk, E=maximum E jerk +//!@n M206 - set additional homing offset +//!@n M207 - set retract length S[positive mm] F[feedrate mm/min] Z[additional zlift/hop], stays in mm regardless of M200 setting +//!@n M208 - set recover=unretract length S[positive mm surplus to the M207 S*] F[feedrate mm/sec] +//!@n M209 - S<1=true/0=false> enable automatic retract detect if the slicer did not support G10/11: every normal extrude-only move will be classified as retract depending on the direction. +//!@n M218 - set hotend offset (in mm): T X Y +//!@n M220 S- set speed factor override percentage +//!@n M221 S- set extrude factor override percentage +//!@n M226 P S- Wait until the specified pin reaches the state required +//!@n M240 - Trigger a camera to take a photograph +//!@n M250 - Set LCD contrast C (value 0..63) +//!@n M280 - set servo position absolute. P: servo index, S: angle or microseconds +//!@n M300 - Play beep sound S P +//!@n M301 - Set PID parameters P I and D +//!@n M302 - Allow cold extrudes, or set the minimum extrude S. +//!@n M303 - PID relay autotune S sets the target temperature. (default target temperature = 150C) +//!@n M304 - Set bed PID parameters P I and D +//!@n M400 - Finish all moves +//!@n M401 - Lower z-probe if present +//!@n M402 - Raise z-probe if present +//!@n M404 - N Enter the nominal filament width (3mm, 1.75mm ) or will display nominal filament width without parameters +//!@n M405 - Turn on Filament Sensor extrusion control. Optional D to set delay in centimeters between sensor and extruder +//!@n M406 - Turn off Filament Sensor extrusion control +//!@n M407 - Displays measured filament diameter +//!@n M500 - stores parameters in EEPROM +//!@n M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily). +//!@n M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to. +//!@n M503 - print the current settings (from memory not from EEPROM) +//!@n M509 - force language selection on next restart +//!@n M540 - Use S[0|1] to enable or disable the stop SD card print on endstop hit (requires ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) +//!@n M600 - Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract] L[later retract distance for removal] +//!@n M605 - Set dual x-carriage movement mode: S [ X R ] +//!@n M860 - Wait for PINDA thermistor to reach target temperature. +//!@n M861 - Set / Read PINDA temperature compensation offsets +//!@n M900 - Set LIN_ADVANCE options, if enabled. See Configuration_adv.h for details. +//!@n M907 - Set digital trimpot motor current using axis codes. +//!@n M908 - Control digital trimpot directly. +//!@n M350 - Set microstepping mode. +//!@n M351 - Toggle MS1 MS2 pins directly. +//! +//!@n M928 - Start SD logging (M928 filename.g) - ended by M29 +//!@n M999 - Restart after being stopped by error +//!

+ +/** @defgroup marlin_main Marlin main */ + +/** \ingroup GCodes */ + +//! _This is a list of currently implemented G Codes in Prusa firmware (dynamically generated from doxygen)_ + + +void process_commands() +{ +#ifdef FANCHECK + if(fan_check_error){ + if(fan_check_error == EFCE_DETECTED){ + fan_check_error = EFCE_REPORTED; + // SERIAL_PROTOCOLLNRPGM(MSG_OCTOPRINT_PAUSED); + lcd_pause_print(); + } // otherwise it has already been reported, so just ignore further processing + return; //ignore usb stream. It is reenabled by selecting resume from the lcd. + } +#endif + + if (!buflen) return; //empty command + #ifdef FILAMENT_RUNOUT_SUPPORT + SET_INPUT(FR_SENS); + #endif + +#ifdef CMDBUFFER_DEBUG + SERIAL_ECHOPGM("Processing a GCODE command: "); + SERIAL_ECHO(cmdbuffer+bufindr+CMDHDRSIZE); + SERIAL_ECHOLNPGM(""); + SERIAL_ECHOPGM("In cmdqueue: "); + SERIAL_ECHO(buflen); + SERIAL_ECHOLNPGM(""); +#endif /* CMDBUFFER_DEBUG */ + + unsigned long codenum; //throw away variable + char *starpos = NULL; +#ifdef ENABLE_AUTO_BED_LEVELING + float x_tmp, y_tmp, z_tmp, real_z; +#endif + + // PRUSA GCODES + KEEPALIVE_STATE(IN_HANDLER); + +#ifdef SNMM + float tmp_motor[3] = DEFAULT_PWM_MOTOR_CURRENT; + float tmp_motor_loud[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD; + int8_t SilentMode; +#endif + + if (code_seen("M117")) { //moved to highest priority place to be able to to print strings which includes "G", "PRUSA" and "^" + starpos = (strchr(strchr_pointer + 5, '*')); + if (starpos != NULL) + *(starpos) = '\0'; + lcd_setstatus(strchr_pointer + 5); + } + +#ifdef TMC2130 + else if (strncmp_P(CMDBUFFER_CURRENT_STRING, PSTR("CRASH_"), 6) == 0) + { + + //! ### CRASH_DETECTED - TMC2130 + // --------------------------------- + if(code_seen("CRASH_DETECTED")) + { + uint8_t mask = 0; + if (code_seen('X')) mask |= X_AXIS_MASK; + if (code_seen('Y')) mask |= Y_AXIS_MASK; + crashdet_detected(mask); + } + + //! ### CRASH_RECOVER - TMC2130 + // ---------------------------------- + else if(code_seen("CRASH_RECOVER")) + crashdet_recover(); + + //! ### CRASH_CANCEL - TMC2130 + // ---------------------------------- + else if(code_seen("CRASH_CANCEL")) + crashdet_cancel(); + } + else if (strncmp_P(CMDBUFFER_CURRENT_STRING, PSTR("TMC_"), 4) == 0) + { + + //! ### TMC_SET_WAVE_ + // -------------------- + if (strncmp_P(CMDBUFFER_CURRENT_STRING + 4, PSTR("SET_WAVE_"), 9) == 0) + { + uint8_t axis = *(CMDBUFFER_CURRENT_STRING + 13); + axis = (axis == 'E')?3:(axis - 'X'); + if (axis < 4) + { + uint8_t fac = (uint8_t)strtol(CMDBUFFER_CURRENT_STRING + 14, NULL, 10); + tmc2130_set_wave(axis, 247, fac); + } + } + + //! ### TMC_SET_STEP_ + // ------------------ + else if (strncmp_P(CMDBUFFER_CURRENT_STRING + 4, PSTR("SET_STEP_"), 9) == 0) + { + uint8_t axis = *(CMDBUFFER_CURRENT_STRING + 13); + axis = (axis == 'E')?3:(axis - 'X'); + if (axis < 4) + { + uint8_t step = (uint8_t)strtol(CMDBUFFER_CURRENT_STRING + 14, NULL, 10); + uint16_t res = tmc2130_get_res(axis); + tmc2130_goto_step(axis, step & (4*res - 1), 2, 1000, res); + } + } + + //! ### TMC_SET_CHOP_ + // ------------------- + else if (strncmp_P(CMDBUFFER_CURRENT_STRING + 4, PSTR("SET_CHOP_"), 9) == 0) + { + uint8_t axis = *(CMDBUFFER_CURRENT_STRING + 13); + axis = (axis == 'E')?3:(axis - 'X'); + if (axis < 4) + { + uint8_t chop0 = tmc2130_chopper_config[axis].toff; + uint8_t chop1 = tmc2130_chopper_config[axis].hstr; + uint8_t chop2 = tmc2130_chopper_config[axis].hend; + uint8_t chop3 = tmc2130_chopper_config[axis].tbl; + char* str_end = 0; + if (CMDBUFFER_CURRENT_STRING[14]) + { + chop0 = (uint8_t)strtol(CMDBUFFER_CURRENT_STRING + 14, &str_end, 10) & 15; + if (str_end && *str_end) + { + chop1 = (uint8_t)strtol(str_end, &str_end, 10) & 7; + if (str_end && *str_end) + { + chop2 = (uint8_t)strtol(str_end, &str_end, 10) & 15; + if (str_end && *str_end) + chop3 = (uint8_t)strtol(str_end, &str_end, 10) & 3; + } + } + } + tmc2130_chopper_config[axis].toff = chop0; + tmc2130_chopper_config[axis].hstr = chop1 & 7; + tmc2130_chopper_config[axis].hend = chop2 & 15; + tmc2130_chopper_config[axis].tbl = chop3 & 3; + tmc2130_setup_chopper(axis, tmc2130_mres[axis], tmc2130_current_h[axis], tmc2130_current_r[axis]); + //printf_P(_N("TMC_SET_CHOP_%c %hhd %hhd %hhd %hhd\n"), "xyze"[axis], chop0, chop1, chop2, chop3); + } + } + } +#ifdef BACKLASH_X + else if (strncmp_P(CMDBUFFER_CURRENT_STRING, PSTR("BACKLASH_X"), 10) == 0) + { + uint8_t bl = (uint8_t)strtol(CMDBUFFER_CURRENT_STRING + 10, NULL, 10); + st_backlash_x = bl; + printf_P(_N("st_backlash_x = %hhd\n"), st_backlash_x); + } +#endif //BACKLASH_X +#ifdef BACKLASH_Y + else if (strncmp_P(CMDBUFFER_CURRENT_STRING, PSTR("BACKLASH_Y"), 10) == 0) + { + uint8_t bl = (uint8_t)strtol(CMDBUFFER_CURRENT_STRING + 10, NULL, 10); + st_backlash_y = bl; + printf_P(_N("st_backlash_y = %hhd\n"), st_backlash_y); + } +#endif //BACKLASH_Y +#endif //TMC2130 + else if(code_seen("PRUSA")){ + /*! + * + ### PRUSA - Internal command set + + Set of internal PRUSA commands + + PRUSA [ Ping | PRN | FAN | fn | thx | uvlo | fsensor_recover | MMURES | RESET | fv | M28 | SN | Fir | Rev | Lang | Lz | Beat | FR ] + + - `Ping` + - `PRN` - Prints revision of the printer + - `FAN` - Prints fan details + - `fn` - Prints farm no. + - `thx` + - `uvlo` + - `fsensor_recover` - Filament sensor recover - restore print and continue + - `MMURES` - Reset MMU + - `RESET` - (Careful!) + - `fv` - ? + - `M28` + - `SN` + - `Fir` - Prints firmware version + - `Rev`- Prints filament size, elelectronics, nozzle type + - `Lang` - Reset the language + - `Lz` + - `Beat` - Kick farm link timer + - `FR` - Full factory reset + - `nozzle set ` - set nozzle diameter (farm mode only), e.g. `PRUSA nozzle set 0.4` + - `nozzle D` - check the nozzle diameter (farm mode only), works like M862.1 P, e.g. `PRUSA nozzle D0.4` + - `nozzle` - prints nozzle diameter (farm mode only), works like M862.1 P, e.g. `PRUSA nozzle` + * + */ + + + if (code_seen("Ping")) { // PRUSA Ping + if (farm_mode) { + PingTime = _millis(); + //MYSERIAL.print(farm_no); MYSERIAL.println(": OK"); + } + } + else if (code_seen("PRN")) { // PRUSA PRN + printf_P(_N("%d"), status_number); + + } else if( code_seen("FANPINTST") ){ + gcode_PRUSA_BadRAMBoFanTest(); + }else if (code_seen("FAN")) { //! PRUSA FAN + printf_P(_N("E0:%d RPM\nPRN0:%d RPM\n"), 60*fan_speed[0], 60*fan_speed[1]); + }else if (code_seen("fn")) { // PRUSA fn + if (farm_mode) { + printf_P(_N("%d"), farm_no); + } + else { + puts_P(_N("Not in farm mode.")); + } + + } + else if (code_seen("thx")) // PRUSA thx + { + no_response = false; + } + else if (code_seen("uvlo")) // PRUSA uvlo + { + eeprom_update_byte((uint8_t*)EEPROM_UVLO,0); + enquecommand_P(PSTR("M24")); + } +#ifdef FILAMENT_SENSOR + else if (code_seen("fsensor_recover")) // PRUSA fsensor_recover + { + fsensor_restore_print_and_continue(); + } +#endif //FILAMENT_SENSOR + else if (code_seen("MMURES")) // PRUSA MMURES + { + mmu_reset(); + } + else if (code_seen("RESET")) { // PRUSA RESET + // careful! + if (farm_mode) { +#if (defined(WATCHDOG) && (MOTHERBOARD == BOARD_EINSY_1_0a)) + boot_app_magic = BOOT_APP_MAGIC; + boot_app_flags = BOOT_APP_FLG_RUN; + wdt_enable(WDTO_15MS); + cli(); + while(1); +#else //WATCHDOG + asm volatile("jmp 0x3E000"); +#endif //WATCHDOG + } + else { + MYSERIAL.println("Not in farm mode."); + } + }else if (code_seen("fv")) { // PRUSA fv + // get file version + #ifdef SDSUPPORT + card.openFile(strchr_pointer + 3,true); + while (true) { + uint16_t readByte = card.get(); + MYSERIAL.write(readByte); + if (readByte=='\n') { + break; + } + } + card.closefile(); + + #endif // SDSUPPORT + + } else if (code_seen("M28")) { // PRUSA M28 + trace(); + prusa_sd_card_upload = true; + card.openFile(strchr_pointer+4,false); + + } else if (code_seen("SN")) { // PRUSA SN + gcode_PRUSA_SN(); + + } else if(code_seen("Fir")){ // PRUSA Fir + + SERIAL_PROTOCOLLN(FW_VERSION_FULL); + + } else if(code_seen("Rev")){ // PRUSA Rev + + SERIAL_PROTOCOLLN(FILAMENT_SIZE "-" ELECTRONICS "-" NOZZLE_TYPE ); + + } else if(code_seen("Lang")) { // PRUSA Lang + lang_reset(); + + } else if(code_seen("Lz")) { // PRUSA Lz + eeprom_update_word(reinterpret_cast(&(EEPROM_Sheets_base->s[(eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)))].z_offset)),0); + + } else if(code_seen("Beat")) { // PRUSA Beat + // Kick farm link timer + kicktime = _millis(); + + } else if(code_seen("FR")) { // PRUSA FR + // Factory full reset + factory_reset(0); + +//-// +/* + } else if(code_seen("rrr")) { +MYSERIAL.println("=== checking ==="); +MYSERIAL.println(eeprom_read_byte((uint8_t*)EEPROM_CHECK_MODE),DEC); +MYSERIAL.println(eeprom_read_byte((uint8_t*)EEPROM_NOZZLE_DIAMETER),DEC); +MYSERIAL.println(eeprom_read_word((uint16_t*)EEPROM_NOZZLE_DIAMETER_uM),DEC); +MYSERIAL.println(farm_mode,DEC); +MYSERIAL.println(eCheckMode,DEC); + } else if(code_seen("www")) { +MYSERIAL.println("=== @ FF ==="); +eeprom_update_byte((uint8_t*)EEPROM_CHECK_MODE,0xFF); +eeprom_update_byte((uint8_t*)EEPROM_NOZZLE_DIAMETER,0xFF); +eeprom_update_word((uint16_t*)EEPROM_NOZZLE_DIAMETER_uM,0xFFFF); +*/ + } else if (code_seen("nozzle")) { // PRUSA nozzle + uint16_t nDiameter; + if(code_seen('D')) + { + nDiameter=(uint16_t)(code_value()*1000.0+0.5); // [,um] + nozzle_diameter_check(nDiameter); + } + else if(code_seen("set") && farm_mode) + { + strchr_pointer++; // skip 1st char (~ 's') + strchr_pointer++; // skip 2nd char (~ 'e') + nDiameter=(uint16_t)(code_value()*1000.0+0.5); // [,um] + eeprom_update_byte((uint8_t*)EEPROM_NOZZLE_DIAMETER,(uint8_t)ClNozzleDiameter::_Diameter_Undef); // for correct synchronization after farm-mode exiting + eeprom_update_word((uint16_t*)EEPROM_NOZZLE_DIAMETER_uM,nDiameter); + } + else SERIAL_PROTOCOLLN((float)eeprom_read_word((uint16_t*)EEPROM_NOZZLE_DIAMETER_uM)/1000.0); + +//-// !!! SupportMenu +/* +// musi byt PRED "PRUSA model" + } else if (code_seen("smodel")) { //! PRUSA smodel + size_t nOffset; +// ! -> "l" + strchr_pointer+=5*sizeof(*strchr_pointer); // skip 1st - 5th char (~ 'smode') + nOffset=strspn(strchr_pointer+1," \t\n\r\v\f"); + if(*(strchr_pointer+1+nOffset)) + printer_smodel_check(strchr_pointer); + else SERIAL_PROTOCOLLN(PRINTER_NAME); + } else if (code_seen("model")) { //! PRUSA model + uint16_t nPrinterModel; + strchr_pointer+=4*sizeof(*strchr_pointer); // skip 1st - 4th char (~ 'mode') + nPrinterModel=(uint16_t)code_value_long(); + if(nPrinterModel!=0) + printer_model_check(nPrinterModel); + else SERIAL_PROTOCOLLN(PRINTER_TYPE); + } else if (code_seen("version")) { //! PRUSA version + strchr_pointer+=7*sizeof(*strchr_pointer); // skip 1st - 7th char (~ 'version') + while(*strchr_pointer==' ') // skip leading spaces + strchr_pointer++; + if(*strchr_pointer!=0) + fw_version_check(strchr_pointer); + else SERIAL_PROTOCOLLN(FW_VERSION); + } else if (code_seen("gcode")) { //! PRUSA gcode + uint16_t nGcodeLevel; + strchr_pointer+=4*sizeof(*strchr_pointer); // skip 1st - 4th char (~ 'gcod') + nGcodeLevel=(uint16_t)code_value_long(); + if(nGcodeLevel!=0) + gcode_level_check(nGcodeLevel); + else SERIAL_PROTOCOLLN(GCODE_LEVEL); +*/ + } + //else if (code_seen('Cal')) { + // lcd_calibration(); + // } + + } + // This prevents reading files with "^" in their names. + // Since it is unclear, if there is some usage of this construct, + // it will be deprecated in 3.9 alpha a possibly completely removed in the future: + // else if (code_seen('^')) { + // // nothing, this is a version line + // } + else if(code_seen('G')) + { + gcode_in_progress = (int)code_value(); +// printf_P(_N("BEGIN G-CODE=%u\n"), gcode_in_progress); + switch (gcode_in_progress) + { + + //! ### G0, G1 - Coordinated movement X Y Z E + // -------------------------------------- + case 0: // G0 -> G1 + case 1: // G1 + if(Stopped == false) { + + #ifdef FILAMENT_RUNOUT_SUPPORT + + if(READ(FR_SENS)){ + + int feedmultiplyBckp=feedmultiply; + float target[4]; + float lastpos[4]; + target[X_AXIS]=current_position[X_AXIS]; + target[Y_AXIS]=current_position[Y_AXIS]; + target[Z_AXIS]=current_position[Z_AXIS]; + target[E_AXIS]=current_position[E_AXIS]; + lastpos[X_AXIS]=current_position[X_AXIS]; + lastpos[Y_AXIS]=current_position[Y_AXIS]; + lastpos[Z_AXIS]=current_position[Z_AXIS]; + lastpos[E_AXIS]=current_position[E_AXIS]; + //retract by E + + target[E_AXIS]+= FILAMENTCHANGE_FIRSTRETRACT ; + + plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 400, active_extruder); + + + target[Z_AXIS]+= FILAMENTCHANGE_ZADD ; + + plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 300, active_extruder); + + target[X_AXIS]= FILAMENTCHANGE_XPOS ; + + target[Y_AXIS]= FILAMENTCHANGE_YPOS ; + + + plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 70, active_extruder); + + target[E_AXIS]+= FILAMENTCHANGE_FINALRETRACT ; + + + plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 20, active_extruder); + + //finish moves + st_synchronize(); + //disable extruder steppers so filament can be removed + disable_e0(); + disable_e1(); + disable_e2(); + _delay(100); + + //LCD_ALERTMESSAGEPGM(_T(MSG_FILAMENTCHANGE)); + uint8_t cnt=0; + int counterBeep = 0; + lcd_wait_interact(); + while(!lcd_clicked()){ + cnt++; + manage_heater(); + manage_inactivity(true); + //lcd_update(0); + if(cnt==0) + { + #if BEEPER > 0 + + if (counterBeep== 500){ + counterBeep = 0; + + } + + + SET_OUTPUT(BEEPER); + if (counterBeep== 0){ +if(eSoundMode!=e_SOUND_MODE_SILENT) + WRITE(BEEPER,HIGH); + } + + if (counterBeep== 20){ + WRITE(BEEPER,LOW); + } + + + + + counterBeep++; + #else + #endif + } + } + + WRITE(BEEPER,LOW); + + target[E_AXIS]+= FILAMENTCHANGE_FIRSTFEED ; + plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 20, active_extruder); + + + target[E_AXIS]+= FILAMENTCHANGE_FINALFEED ; + plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 2, active_extruder); + + + + + + lcd_change_fil_state = 0; + lcd_loading_filament(); + while ((lcd_change_fil_state == 0)||(lcd_change_fil_state != 1)){ + + lcd_change_fil_state = 0; + lcd_alright(); + switch(lcd_change_fil_state){ + + case 2: + target[E_AXIS]+= FILAMENTCHANGE_FIRSTFEED ; + plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 20, active_extruder); + + + target[E_AXIS]+= FILAMENTCHANGE_FINALFEED ; + plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 2, active_extruder); + + + lcd_loading_filament(); + break; + case 3: + target[E_AXIS]+= FILAMENTCHANGE_FINALFEED ; + plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 2, active_extruder); + lcd_loading_color(); + break; + + default: + lcd_change_success(); + break; + } + + } + + + + target[E_AXIS]+= 5; + plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 2, active_extruder); + + target[E_AXIS]+= FILAMENTCHANGE_FIRSTRETRACT; + plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 400, active_extruder); + + + //current_position[E_AXIS]=target[E_AXIS]; //the long retract of L is compensated by manual filament feeding + //plan_set_e_position(current_position[E_AXIS]); + plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 70, active_extruder); //should do nothing + plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], target[Z_AXIS], target[E_AXIS], 70, active_extruder); //move xy back + plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], target[E_AXIS], 200, active_extruder); //move z back + + + target[E_AXIS]= target[E_AXIS] - FILAMENTCHANGE_FIRSTRETRACT; + + + + plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], target[E_AXIS], 5, active_extruder); //final untretract + + + plan_set_e_position(lastpos[E_AXIS]); + + feedmultiply=feedmultiplyBckp; + + + + char cmd[9]; + + sprintf_P(cmd, PSTR("M220 S%i"), feedmultiplyBckp); + enquecommand(cmd); + + } + + + + #endif + + + get_coordinates(); // For X Y Z E F + if (total_filament_used > ((current_position[E_AXIS] - destination[E_AXIS]) * 100)) { //protection against total_filament_used overflow + total_filament_used = total_filament_used + ((destination[E_AXIS] - current_position[E_AXIS]) * 100); + } + #ifdef FWRETRACT + if(cs.autoretract_enabled) + if( !(code_seen('X') || code_seen('Y') || code_seen('Z')) && code_seen('E')) { + float echange=destination[E_AXIS]-current_position[E_AXIS]; + + if((echange<-MIN_RETRACT && !retracted[active_extruder]) || (echange>MIN_RETRACT && retracted[active_extruder])) { //move appears to be an attempt to retract or recover + current_position[E_AXIS] = destination[E_AXIS]; //hide the slicer-generated retract/recover from calculations + plan_set_e_position(current_position[E_AXIS]); //AND from the planner + retract(!retracted[active_extruder]); + return; + } + + + } + #endif //FWRETRACT + prepare_move(); + //ClearToSend(); + } + break; + + //! ### G2 - CW ARC + // ------------------------------ + case 2: + if(Stopped == false) { + get_arc_coordinates(); + prepare_arc_move(true); + } + break; + + + //! ### G3 - CCW ARC + // ------------------------------- + case 3: + if(Stopped == false) { + get_arc_coordinates(); + prepare_arc_move(false); + } + break; + + + //! ### G4 - Dwell + // ------------------------------- + case 4: + codenum = 0; + if(code_seen('P')) codenum = code_value(); // milliseconds to wait + if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait + if(codenum != 0) LCD_MESSAGERPGM(_n("Sleep..."));////MSG_DWELL + st_synchronize(); + codenum += _millis(); // keep track of when we started waiting + previous_millis_cmd = _millis(); + while(_millis() < codenum) { + manage_heater(); + manage_inactivity(); + lcd_update(0); + } + break; + #ifdef FWRETRACT + + + //! ### G10 Retract + // ------------------------------ + case 10: + #if EXTRUDERS > 1 + retracted_swap[active_extruder]=(code_seen('S') && code_value_long() == 1); // checks for swap retract argument + retract(true,retracted_swap[active_extruder]); + #else + retract(true); + #endif + break; + + + //! ### G11 - Retract recover + // ----------------------------- + case 11: + #if EXTRUDERS > 1 + retract(false,retracted_swap[active_extruder]); + #else + retract(false); + #endif + break; + #endif //FWRETRACT + + + //! ### G28 - Home all Axis one at a time + // -------------------------------------------- + case 28: + { + long home_x_value = 0; + long home_y_value = 0; + long home_z_value = 0; + // Which axes should be homed? + bool home_x = code_seen(axis_codes[X_AXIS]); + home_x_value = code_value_long(); + bool home_y = code_seen(axis_codes[Y_AXIS]); + home_y_value = code_value_long(); + bool home_z = code_seen(axis_codes[Z_AXIS]); + home_z_value = code_value_long(); + bool without_mbl = code_seen('W'); + // calibrate? +#ifdef TMC2130 + bool calib = code_seen('C'); + gcode_G28(home_x, home_x_value, home_y, home_y_value, home_z, home_z_value, calib, without_mbl); +#else + gcode_G28(home_x, home_x_value, home_y, home_y_value, home_z, home_z_value, without_mbl); +#endif //TMC2130 + if ((home_x || home_y || without_mbl || home_z) == false) { + // Push the commands to the front of the message queue in the reverse order! + // There shall be always enough space reserved for these commands. + goto case_G80; + } + break; + } + +#ifdef ENABLE_AUTO_BED_LEVELING + + + //! ### G29 - Detailed Z-Probe + // -------------------------------- + case 29: + { + #if Z_MIN_PIN == -1 + #error "You must have a Z_MIN endstop in order to enable Auto Bed Leveling feature! Z_MIN_PIN must point to a valid hardware pin." + #endif + + // Prevent user from running a G29 without first homing in X and Y + if (! (axis_known_position[X_AXIS] && axis_known_position[Y_AXIS]) ) + { + LCD_MESSAGERPGM(MSG_POSITION_UNKNOWN); + SERIAL_ECHO_START; + SERIAL_ECHOLNRPGM(MSG_POSITION_UNKNOWN); + break; // abort G29, since we don't know where we are + } + + st_synchronize(); + // make sure the bed_level_rotation_matrix is identity or the planner will get it incorectly + //vector_3 corrected_position = plan_get_position_mm(); + //corrected_position.debug("position before G29"); + plan_bed_level_matrix.set_to_identity(); + vector_3 uncorrected_position = plan_get_position(); + //uncorrected_position.debug("position durring G29"); + current_position[X_AXIS] = uncorrected_position.x; + current_position[Y_AXIS] = uncorrected_position.y; + current_position[Z_AXIS] = uncorrected_position.z; + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + int l_feedmultiply = setup_for_endstop_move(); + + feedrate = homing_feedrate[Z_AXIS]; +#ifdef AUTO_BED_LEVELING_GRID + // probe at the points of a lattice grid + + int xGridSpacing = (RIGHT_PROBE_BED_POSITION - LEFT_PROBE_BED_POSITION) / (AUTO_BED_LEVELING_GRID_POINTS-1); + int yGridSpacing = (BACK_PROBE_BED_POSITION - FRONT_PROBE_BED_POSITION) / (AUTO_BED_LEVELING_GRID_POINTS-1); + + + // solve the plane equation ax + by + d = z + // A is the matrix with rows [x y 1] for all the probed points + // B is the vector of the Z positions + // the normal vector to the plane is formed by the coefficients of the plane equation in the standard form, which is Vx*x+Vy*y+Vz*z+d = 0 + // so Vx = -a Vy = -b Vz = 1 (we want the vector facing towards positive Z + + // "A" matrix of the linear system of equations + double eqnAMatrix[AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS*3]; + // "B" vector of Z points + double eqnBVector[AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS]; + + + int probePointCounter = 0; + bool zig = true; + + for (int yProbe=FRONT_PROBE_BED_POSITION; yProbe <= BACK_PROBE_BED_POSITION; yProbe += yGridSpacing) + { + int xProbe, xInc; + if (zig) + { + xProbe = LEFT_PROBE_BED_POSITION; + //xEnd = RIGHT_PROBE_BED_POSITION; + xInc = xGridSpacing; + zig = false; + } else // zag + { + xProbe = RIGHT_PROBE_BED_POSITION; + //xEnd = LEFT_PROBE_BED_POSITION; + xInc = -xGridSpacing; + zig = true; + } + + for (int xCount=0; xCount < AUTO_BED_LEVELING_GRID_POINTS; xCount++) + { + float z_before; + if (probePointCounter == 0) + { + // raise before probing + z_before = Z_RAISE_BEFORE_PROBING; + } else + { + // raise extruder + z_before = current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS; + } + + float measured_z = probe_pt(xProbe, yProbe, z_before); + + eqnBVector[probePointCounter] = measured_z; + + eqnAMatrix[probePointCounter + 0*AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS] = xProbe; + eqnAMatrix[probePointCounter + 1*AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS] = yProbe; + eqnAMatrix[probePointCounter + 2*AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS] = 1; + probePointCounter++; + xProbe += xInc; + } + } + clean_up_after_endstop_move(l_feedmultiply); + + // solve lsq problem + double *plane_equation_coefficients = qr_solve(AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS, 3, eqnAMatrix, eqnBVector); + + SERIAL_PROTOCOLPGM("Eqn coefficients: a: "); + SERIAL_PROTOCOL(plane_equation_coefficients[0]); + SERIAL_PROTOCOLPGM(" b: "); + SERIAL_PROTOCOL(plane_equation_coefficients[1]); + SERIAL_PROTOCOLPGM(" d: "); + SERIAL_PROTOCOLLN(plane_equation_coefficients[2]); + + + set_bed_level_equation_lsq(plane_equation_coefficients); + + free(plane_equation_coefficients); + +#else // AUTO_BED_LEVELING_GRID not defined + + // Probe at 3 arbitrary points + // probe 1 + float z_at_pt_1 = probe_pt(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, Z_RAISE_BEFORE_PROBING); + + // probe 2 + float z_at_pt_2 = probe_pt(ABL_PROBE_PT_2_X, ABL_PROBE_PT_2_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS); + + // probe 3 + float z_at_pt_3 = probe_pt(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS); + + clean_up_after_endstop_move(l_feedmultiply); + + set_bed_level_equation_3pts(z_at_pt_1, z_at_pt_2, z_at_pt_3); + + +#endif // AUTO_BED_LEVELING_GRID + st_synchronize(); + + // The following code correct the Z height difference from z-probe position and hotend tip position. + // The Z height on homing is measured by Z-Probe, but the probe is quite far from the hotend. + // When the bed is uneven, this height must be corrected. + real_z = float(st_get_position(Z_AXIS))/cs.axis_steps_per_unit[Z_AXIS]; //get the real Z (since the auto bed leveling is already correcting the plane) + x_tmp = current_position[X_AXIS] + X_PROBE_OFFSET_FROM_EXTRUDER; + y_tmp = current_position[Y_AXIS] + Y_PROBE_OFFSET_FROM_EXTRUDER; + z_tmp = current_position[Z_AXIS]; + + apply_rotation_xyz(plan_bed_level_matrix, x_tmp, y_tmp, z_tmp); //Apply the correction sending the probe offset + current_position[Z_AXIS] = z_tmp - real_z + current_position[Z_AXIS]; //The difference is added to current position and sent to planner. + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + } + break; +#ifndef Z_PROBE_SLED + + //! ### G30 - Single Z Probe + // ------------------------------------ + case 30: + { + st_synchronize(); + // TODO: make sure the bed_level_rotation_matrix is identity or the planner will get set incorectly + int l_feedmultiply = setup_for_endstop_move(); + + feedrate = homing_feedrate[Z_AXIS]; + + run_z_probe(); + SERIAL_PROTOCOLPGM(_T(MSG_BED)); + SERIAL_PROTOCOLPGM(" X: "); + SERIAL_PROTOCOL(current_position[X_AXIS]); + SERIAL_PROTOCOLPGM(" Y: "); + SERIAL_PROTOCOL(current_position[Y_AXIS]); + SERIAL_PROTOCOLPGM(" Z: "); + SERIAL_PROTOCOL(current_position[Z_AXIS]); + SERIAL_PROTOCOLPGM("\n"); + + clean_up_after_endstop_move(l_feedmultiply); + } + break; +#else + + //! ### G31 - Dock the sled + // --------------------------- + case 31: + dock_sled(true); + break; + + + //! ### G32 - Undock the sled + // ---------------------------- + case 32: + dock_sled(false); + break; +#endif // Z_PROBE_SLED +#endif // ENABLE_AUTO_BED_LEVELING + +#ifdef MESH_BED_LEVELING + + //! ### G30 - Single Z Probe + // ---------------------------- + case 30: + { + st_synchronize(); + // TODO: make sure the bed_level_rotation_matrix is identity or the planner will get set incorectly + int l_feedmultiply = setup_for_endstop_move(); + + feedrate = homing_feedrate[Z_AXIS]; + + find_bed_induction_sensor_point_z(-10.f, 3); + + printf_P(_N("%S X: %.5f Y: %.5f Z: %.5f\n"), _T(MSG_BED), _x, _y, _z); + + clean_up_after_endstop_move(l_feedmultiply); + } + break; + + //! ### G75 - Print temperature interpolation + // --------------------------------------------- + case 75: + { + for (int i = 40; i <= 110; i++) + printf_P(_N("%d %.2f"), i, temp_comp_interpolation(i)); + } + break; + + //! ### G76 - PINDA probe temperature calibration + // ------------------------------------------------ + case 76: + { +#ifdef PINDA_THERMISTOR + if (true) + { + + if (calibration_status() >= CALIBRATION_STATUS_XYZ_CALIBRATION) { + //we need to know accurate position of first calibration point + //if xyz calibration was not performed yet, interrupt temperature calibration and inform user that xyz cal. is needed + lcd_show_fullscreen_message_and_wait_P(_i("Please run XYZ calibration first.")); + break; + } + + if (!(axis_known_position[X_AXIS] && axis_known_position[Y_AXIS] && axis_known_position[Z_AXIS])) + { + // We don't know where we are! HOME! + // Push the commands to the front of the message queue in the reverse order! + // There shall be always enough space reserved for these commands. + repeatcommand_front(); // repeat G76 with all its parameters + enquecommand_front_P((PSTR("G28 W0"))); + break; + } + lcd_show_fullscreen_message_and_wait_P(_i("Stable ambient temperature 21-26C is needed a rigid stand is required."));////MSG_TEMP_CAL_WARNING c=20 r=4 + bool result = lcd_show_fullscreen_message_yes_no_and_wait_P(_T(MSG_STEEL_SHEET_CHECK), false, false); + + if (result) + { + current_position[Z_AXIS] = MESH_HOME_Z_SEARCH; + plan_buffer_line_curposXYZE(3000 / 60, active_extruder); + current_position[Z_AXIS] = 50; + current_position[Y_AXIS] = 180; + plan_buffer_line_curposXYZE(3000 / 60, active_extruder); + st_synchronize(); + lcd_show_fullscreen_message_and_wait_P(_T(MSG_REMOVE_STEEL_SHEET)); + current_position[Y_AXIS] = pgm_read_float(bed_ref_points_4 + 1); + current_position[X_AXIS] = pgm_read_float(bed_ref_points_4); + plan_buffer_line_curposXYZE(3000 / 60, active_extruder); + st_synchronize(); + gcode_G28(false, false, true); + + } + if ((current_temperature_pinda > 35) && (farm_mode == false)) { + //waiting for PIDNA probe to cool down in case that we are not in farm mode + current_position[Z_AXIS] = 100; + plan_buffer_line_curposXYZE(3000 / 60, active_extruder); + if (lcd_wait_for_pinda(35) == false) { //waiting for PINDA probe to cool, if this takes more then time expected, temp. cal. fails + lcd_temp_cal_show_result(false); + break; + } + } + lcd_update_enable(true); + KEEPALIVE_STATE(NOT_BUSY); //no need to print busy messages as we print current temperatures periodicaly + SERIAL_ECHOLNPGM("PINDA probe calibration start"); + + float zero_z; + int z_shift = 0; //unit: steps + float start_temp = 5 * (int)(current_temperature_pinda / 5); + if (start_temp < 35) start_temp = 35; + if (start_temp < current_temperature_pinda) start_temp += 5; + printf_P(_N("start temperature: %.1f\n"), start_temp); + +// setTargetHotend(200, 0); + setTargetBed(70 + (start_temp - 30)); + + custom_message_type = CustomMsg::TempCal; + custom_message_state = 1; + lcd_setstatuspgm(_T(MSG_TEMP_CALIBRATION)); + current_position[Z_AXIS] = MESH_HOME_Z_SEARCH; + plan_buffer_line_curposXYZE(3000 / 60, active_extruder); + current_position[X_AXIS] = PINDA_PREHEAT_X; + current_position[Y_AXIS] = PINDA_PREHEAT_Y; + plan_buffer_line_curposXYZE(3000 / 60, active_extruder); + current_position[Z_AXIS] = PINDA_PREHEAT_Z; + plan_buffer_line_curposXYZE(3000 / 60, active_extruder); + st_synchronize(); + + while (current_temperature_pinda < start_temp) + { + delay_keep_alive(1000); + serialecho_temperatures(); + } + + eeprom_update_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, 0); //invalidate temp. calibration in case that in will be aborted during the calibration process + + current_position[Z_AXIS] = MESH_HOME_Z_SEARCH; + plan_buffer_line_curposXYZE(3000 / 60, active_extruder); + current_position[X_AXIS] = pgm_read_float(bed_ref_points_4); + current_position[Y_AXIS] = pgm_read_float(bed_ref_points_4 + 1); + plan_buffer_line_curposXYZE(3000 / 60, active_extruder); + st_synchronize(); + + bool find_z_result = find_bed_induction_sensor_point_z(-1.f); + if (find_z_result == false) { + lcd_temp_cal_show_result(find_z_result); + break; + } + zero_z = current_position[Z_AXIS]; + + printf_P(_N("\nZERO: %.3f\n"), current_position[Z_AXIS]); + + int i = -1; for (; i < 5; i++) + { + float temp = (40 + i * 5); + printf_P(_N("\nStep: %d/6 (skipped)\nPINDA temperature: %d Z shift (mm):0\n"), i + 2, (40 + i*5)); + if (i >= 0) EEPROM_save_B(EEPROM_PROBE_TEMP_SHIFT + i * 2, &z_shift); + if (start_temp <= temp) break; + } + + for (i++; i < 5; i++) + { + float temp = (40 + i * 5); + printf_P(_N("\nStep: %d/6\n"), i + 2); + custom_message_state = i + 2; + setTargetBed(50 + 10 * (temp - 30) / 5); +// setTargetHotend(255, 0); + current_position[Z_AXIS] = MESH_HOME_Z_SEARCH; + plan_buffer_line_curposXYZE(3000 / 60, active_extruder); + current_position[X_AXIS] = PINDA_PREHEAT_X; + current_position[Y_AXIS] = PINDA_PREHEAT_Y; + plan_buffer_line_curposXYZE(3000 / 60, active_extruder); + current_position[Z_AXIS] = PINDA_PREHEAT_Z; + plan_buffer_line_curposXYZE(3000 / 60, active_extruder); + st_synchronize(); + while (current_temperature_pinda < temp) + { + delay_keep_alive(1000); + serialecho_temperatures(); + } + current_position[Z_AXIS] = MESH_HOME_Z_SEARCH; + plan_buffer_line_curposXYZE(3000 / 60, active_extruder); + current_position[X_AXIS] = pgm_read_float(bed_ref_points_4); + current_position[Y_AXIS] = pgm_read_float(bed_ref_points_4 + 1); + plan_buffer_line_curposXYZE(3000 / 60, active_extruder); + st_synchronize(); + find_z_result = find_bed_induction_sensor_point_z(-1.f); + if (find_z_result == false) { + lcd_temp_cal_show_result(find_z_result); + break; + } + z_shift = (int)((current_position[Z_AXIS] - zero_z)*cs.axis_steps_per_unit[Z_AXIS]); + + printf_P(_N("\nPINDA temperature: %.1f Z shift (mm): %.3f"), current_temperature_pinda, current_position[Z_AXIS] - zero_z); + + EEPROM_save_B(EEPROM_PROBE_TEMP_SHIFT + i * 2, &z_shift); + + } + lcd_temp_cal_show_result(true); + + break; + } +#endif //PINDA_THERMISTOR + + setTargetBed(PINDA_MIN_T); + float zero_z; + int z_shift = 0; //unit: steps + int t_c; // temperature + + if (!(axis_known_position[X_AXIS] && axis_known_position[Y_AXIS] && axis_known_position[Z_AXIS])) { + // We don't know where we are! HOME! + // Push the commands to the front of the message queue in the reverse order! + // There shall be always enough space reserved for these commands. + repeatcommand_front(); // repeat G76 with all its parameters + enquecommand_front_P((PSTR("G28 W0"))); + break; + } + puts_P(_N("PINDA probe calibration start")); + custom_message_type = CustomMsg::TempCal; + custom_message_state = 1; + lcd_setstatuspgm(_T(MSG_TEMP_CALIBRATION)); + current_position[X_AXIS] = PINDA_PREHEAT_X; + current_position[Y_AXIS] = PINDA_PREHEAT_Y; + current_position[Z_AXIS] = PINDA_PREHEAT_Z; + plan_buffer_line_curposXYZE(3000 / 60, active_extruder); + st_synchronize(); + + while (abs(degBed() - PINDA_MIN_T) > 1) { + delay_keep_alive(1000); + serialecho_temperatures(); + } + + //enquecommand_P(PSTR("M190 S50")); + for (int i = 0; i < PINDA_HEAT_T; i++) { + delay_keep_alive(1000); + serialecho_temperatures(); + } + eeprom_update_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, 0); //invalidate temp. calibration in case that in will be aborted during the calibration process + + current_position[Z_AXIS] = 5; + plan_buffer_line_curposXYZE(3000 / 60, active_extruder); + + current_position[X_AXIS] = BED_X0; + current_position[Y_AXIS] = BED_Y0; + plan_buffer_line_curposXYZE(3000 / 60, active_extruder); + st_synchronize(); + + find_bed_induction_sensor_point_z(-1.f); + zero_z = current_position[Z_AXIS]; + + printf_P(_N("\nZERO: %.3f\n"), current_position[Z_AXIS]); + + for (int i = 0; i<5; i++) { + printf_P(_N("\nStep: %d/6\n"), i + 2); + custom_message_state = i + 2; + t_c = 60 + i * 10; + + setTargetBed(t_c); + current_position[X_AXIS] = PINDA_PREHEAT_X; + current_position[Y_AXIS] = PINDA_PREHEAT_Y; + current_position[Z_AXIS] = PINDA_PREHEAT_Z; + plan_buffer_line_curposXYZE(3000 / 60, active_extruder); + st_synchronize(); + while (degBed() < t_c) { + delay_keep_alive(1000); + serialecho_temperatures(); + } + for (int i = 0; i < PINDA_HEAT_T; i++) { + delay_keep_alive(1000); + serialecho_temperatures(); + } + current_position[Z_AXIS] = 5; + plan_buffer_line_curposXYZE(3000 / 60, active_extruder); + current_position[X_AXIS] = BED_X0; + current_position[Y_AXIS] = BED_Y0; + plan_buffer_line_curposXYZE(3000 / 60, active_extruder); + st_synchronize(); + find_bed_induction_sensor_point_z(-1.f); + z_shift = (int)((current_position[Z_AXIS] - zero_z)*cs.axis_steps_per_unit[Z_AXIS]); + + printf_P(_N("\nTemperature: %d Z shift (mm): %.3f\n"), t_c, current_position[Z_AXIS] - zero_z); + + EEPROM_save_B(EEPROM_PROBE_TEMP_SHIFT + i*2, &z_shift); + + + } + custom_message_type = CustomMsg::Status; + + eeprom_update_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, 1); + puts_P(_N("Temperature calibration done.")); + disable_x(); + disable_y(); + disable_z(); + disable_e0(); + disable_e1(); + disable_e2(); + setTargetBed(0); //set bed target temperature back to 0 + lcd_show_fullscreen_message_and_wait_P(_T(MSG_TEMP_CALIBRATION_DONE)); + temp_cal_active = true; + eeprom_update_byte((unsigned char *)EEPROM_TEMP_CAL_ACTIVE, 1); + lcd_update_enable(true); + lcd_update(2); + + + + } + break; + + + //! ### G80 - Mesh-based Z probe + // ----------------------------------- + + /* + * Probes a grid and produces a mesh to compensate for variable bed height + * The S0 report the points as below + * +----> X-axis + * | + * | + * v Y-axis + */ + + case 80: + +#ifdef MK1BP + break; +#endif //MK1BP + case_G80: + { + mesh_bed_leveling_flag = true; +#ifndef PINDA_THERMISTOR + static bool run = false; // thermistor-less PINDA temperature compensation is running +#endif // ndef PINDA_THERMISTOR + +#ifdef SUPPORT_VERBOSITY + int8_t verbosity_level = 0; + if (code_seen('V')) { + // Just 'V' without a number counts as V1. + char c = strchr_pointer[1]; + verbosity_level = (c == ' ' || c == '\t' || c == 0) ? 1 : code_value_short(); + } +#endif //SUPPORT_VERBOSITY + // Firstly check if we know where we are + if (!(axis_known_position[X_AXIS] && axis_known_position[Y_AXIS] && axis_known_position[Z_AXIS])) { + // We don't know where we are! HOME! + // Push the commands to the front of the message queue in the reverse order! + // There shall be always enough space reserved for these commands. + if (lcd_commands_type != LcdCommands::StopPrint) { + repeatcommand_front(); // repeat G80 with all its parameters + enquecommand_front_P((PSTR("G28 W0"))); + } + else { + mesh_bed_leveling_flag = false; + } + break; + } + + uint8_t nMeasPoints = MESH_MEAS_NUM_X_POINTS; + if (code_seen('N')) { + nMeasPoints = code_value_uint8(); + if (nMeasPoints != 7) { + nMeasPoints = 3; + } + } + else { + nMeasPoints = eeprom_read_byte((uint8_t*)EEPROM_MBL_POINTS_NR); + } + + uint8_t nProbeRetry = 3; + if (code_seen('R')) { + nProbeRetry = code_value_uint8(); + if (nProbeRetry > 10) { + nProbeRetry = 10; + } + } + else { + nProbeRetry = eeprom_read_byte((uint8_t*)EEPROM_MBL_PROBE_NR); + } + bool magnet_elimination = (eeprom_read_byte((uint8_t*)EEPROM_MBL_MAGNET_ELIMINATION) > 0); + +#ifndef PINDA_THERMISTOR + if (run == false && temp_cal_active == true && calibration_status_pinda() == true && target_temperature_bed >= 50) + { + if (lcd_commands_type != LcdCommands::StopPrint) { + temp_compensation_start(); + run = true; + repeatcommand_front(); // repeat G80 with all its parameters + enquecommand_front_P((PSTR("G28 W0"))); + } + else { + mesh_bed_leveling_flag = false; + } + break; + } + run = false; +#endif //PINDA_THERMISTOR + if (lcd_commands_type == LcdCommands::StopPrint) { + mesh_bed_leveling_flag = false; + break; + } + // Save custom message state, set a new custom message state to display: Calibrating point 9. + CustomMsg custom_message_type_old = custom_message_type; + unsigned int custom_message_state_old = custom_message_state; + custom_message_type = CustomMsg::MeshBedLeveling; + custom_message_state = (nMeasPoints * nMeasPoints) + 10; + lcd_update(1); + + mbl.reset(); //reset mesh bed leveling + + // Reset baby stepping to zero, if the babystepping has already been loaded before. The babystepsTodo value will be + // consumed during the first movements following this statement. + babystep_undo(); + + // Cycle through all points and probe them + // First move up. During this first movement, the babystepping will be reverted. + current_position[Z_AXIS] = MESH_HOME_Z_SEARCH; + plan_buffer_line_curposXYZE(homing_feedrate[Z_AXIS] / 60, active_extruder); + // The move to the first calibration point. + current_position[X_AXIS] = BED_X0; + current_position[Y_AXIS] = BED_Y0; + + #ifdef SUPPORT_VERBOSITY + if (verbosity_level >= 1) + { + bool clamped = world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]); + clamped ? SERIAL_PROTOCOLPGM("First calibration point clamped.\n") : SERIAL_PROTOCOLPGM("No clamping for first calibration point.\n"); + } + #else //SUPPORT_VERBOSITY + world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]); + #endif //SUPPORT_VERBOSITY + + plan_buffer_line_curposXYZE(homing_feedrate[X_AXIS] / 30, active_extruder); + // Wait until the move is finished. + st_synchronize(); + + uint8_t mesh_point = 0; //index number of calibration point + + int XY_AXIS_FEEDRATE = homing_feedrate[X_AXIS] / 20; + int Z_LIFT_FEEDRATE = homing_feedrate[Z_AXIS] / 40; + bool has_z = is_bed_z_jitter_data_valid(); //checks if we have data from Z calibration (offsets of the Z heiths of the 8 calibration points from the first point) + #ifdef SUPPORT_VERBOSITY + if (verbosity_level >= 1) { + has_z ? SERIAL_PROTOCOLPGM("Z jitter data from Z cal. valid.\n") : SERIAL_PROTOCOLPGM("Z jitter data from Z cal. not valid.\n"); + } + #endif // SUPPORT_VERBOSITY + int l_feedmultiply = setup_for_endstop_move(false); //save feedrate and feedmultiply, sets feedmultiply to 100 + const char *kill_message = NULL; + while (mesh_point != nMeasPoints * nMeasPoints) { + // Get coords of a measuring point. + uint8_t ix = mesh_point % nMeasPoints; // from 0 to MESH_NUM_X_POINTS - 1 + uint8_t iy = mesh_point / nMeasPoints; + /*if (!mbl_point_measurement_valid(ix, iy, nMeasPoints, true)) { + printf_P(PSTR("Skipping point [%d;%d] \n"), ix, iy); + custom_message_state--; + mesh_point++; + continue; //skip + }*/ + if (iy & 1) ix = (nMeasPoints - 1) - ix; // Zig zag + if (nMeasPoints == 7) //if we have 7x7 mesh, compare with Z-calibration for points which are in 3x3 mesh + { + has_z = ((ix % 3 == 0) && (iy % 3 == 0)) && is_bed_z_jitter_data_valid(); + } + float z0 = 0.f; + if (has_z && (mesh_point > 0)) { + uint16_t z_offset_u = 0; + if (nMeasPoints == 7) { + z_offset_u = eeprom_read_word((uint16_t*)(EEPROM_BED_CALIBRATION_Z_JITTER + 2 * ((ix/3) + iy - 1))); + } + else { + z_offset_u = eeprom_read_word((uint16_t*)(EEPROM_BED_CALIBRATION_Z_JITTER + 2 * (ix + iy * 3 - 1))); + } + z0 = mbl.z_values[0][0] + *reinterpret_cast(&z_offset_u) * 0.01; + #ifdef SUPPORT_VERBOSITY + if (verbosity_level >= 1) { + printf_P(PSTR("Bed leveling, point: %d, calibration Z stored in eeprom: %d, calibration z: %f \n"), mesh_point, z_offset_u, z0); + } + #endif // SUPPORT_VERBOSITY + } + + // Move Z up to MESH_HOME_Z_SEARCH. + if((ix == 0) && (iy == 0)) current_position[Z_AXIS] = MESH_HOME_Z_SEARCH; + else current_position[Z_AXIS] += 2.f / nMeasPoints; //use relative movement from Z coordinate where PINDa triggered on previous point. This makes calibration faster. + float init_z_bckp = current_position[Z_AXIS]; + plan_buffer_line_curposXYZE(Z_LIFT_FEEDRATE, active_extruder); + st_synchronize(); + + // Move to XY position of the sensor point. + current_position[X_AXIS] = BED_X(ix, nMeasPoints); + current_position[Y_AXIS] = BED_Y(iy, nMeasPoints); + + //printf_P(PSTR("[%f;%f]\n"), current_position[X_AXIS], current_position[Y_AXIS]); + + + #ifdef SUPPORT_VERBOSITY + if (verbosity_level >= 1) { + clamped = world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]); + SERIAL_PROTOCOL(mesh_point); + clamped ? SERIAL_PROTOCOLPGM(": xy clamped.\n") : SERIAL_PROTOCOLPGM(": no xy clamping\n"); + } + #else //SUPPORT_VERBOSITY + world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]); + #endif // SUPPORT_VERBOSITY + + //printf_P(PSTR("after clamping: [%f;%f]\n"), current_position[X_AXIS], current_position[Y_AXIS]); + plan_buffer_line_curposXYZE(XY_AXIS_FEEDRATE, active_extruder); + st_synchronize(); + + // Go down until endstop is hit + const float Z_CALIBRATION_THRESHOLD = 1.f; + if (!find_bed_induction_sensor_point_z((has_z && mesh_point > 0) ? z0 - Z_CALIBRATION_THRESHOLD : -10.f, nProbeRetry)) { //if we have data from z calibration max allowed difference is 1mm for each point, if we dont have data max difference is 10mm from initial point + printf_P(_T(MSG_BED_LEVELING_FAILED_POINT_LOW)); + break; + } + if (init_z_bckp - current_position[Z_AXIS] < 0.1f) { //broken cable or initial Z coordinate too low. Go to MESH_HOME_Z_SEARCH and repeat last step (z-probe) again to distinguish between these two cases. + //printf_P(PSTR("Another attempt! Current Z position: %f\n"), current_position[Z_AXIS]); + current_position[Z_AXIS] = MESH_HOME_Z_SEARCH; + plan_buffer_line_curposXYZE(Z_LIFT_FEEDRATE, active_extruder); + st_synchronize(); + + if (!find_bed_induction_sensor_point_z((has_z && mesh_point > 0) ? z0 - Z_CALIBRATION_THRESHOLD : -10.f, nProbeRetry)) { //if we have data from z calibration max allowed difference is 1mm for each point, if we dont have data max difference is 10mm from initial point + printf_P(_T(MSG_BED_LEVELING_FAILED_POINT_LOW)); + break; + } + if (MESH_HOME_Z_SEARCH - current_position[Z_AXIS] < 0.1f) { + printf_P(PSTR("Bed leveling failed. Sensor disconnected or cable broken.\n")); + break; + } + } + if (has_z && fabs(z0 - current_position[Z_AXIS]) > Z_CALIBRATION_THRESHOLD) { //if we have data from z calibration, max. allowed difference is 1mm for each point + printf_P(PSTR("Bed leveling failed. Sensor triggered too high.\n")); + break; + } + #ifdef SUPPORT_VERBOSITY + if (verbosity_level >= 10) { + SERIAL_ECHOPGM("X: "); + MYSERIAL.print(current_position[X_AXIS], 5); + SERIAL_ECHOLNPGM(""); + SERIAL_ECHOPGM("Y: "); + MYSERIAL.print(current_position[Y_AXIS], 5); + SERIAL_PROTOCOLPGM("\n"); + } + #endif // SUPPORT_VERBOSITY + float offset_z = 0; + +#ifdef PINDA_THERMISTOR + offset_z = temp_compensation_pinda_thermistor_offset(current_temperature_pinda); +#endif //PINDA_THERMISTOR +// #ifdef SUPPORT_VERBOSITY +/* if (verbosity_level >= 1) + { + SERIAL_ECHOPGM("mesh bed leveling: "); + MYSERIAL.print(current_position[Z_AXIS], 5); + SERIAL_ECHOPGM(" offset: "); + MYSERIAL.print(offset_z, 5); + SERIAL_ECHOLNPGM(""); + }*/ +// #endif // SUPPORT_VERBOSITY + mbl.set_z(ix, iy, current_position[Z_AXIS] - offset_z); //store measured z values z_values[iy][ix] = z - offset_z; + + custom_message_state--; + mesh_point++; + lcd_update(1); + } + current_position[Z_AXIS] = MESH_HOME_Z_SEARCH; + #ifdef SUPPORT_VERBOSITY + if (verbosity_level >= 20) { + SERIAL_ECHOLNPGM("Mesh bed leveling while loop finished."); + SERIAL_ECHOLNPGM("MESH_HOME_Z_SEARCH: "); + MYSERIAL.print(current_position[Z_AXIS], 5); + } + #endif // SUPPORT_VERBOSITY + plan_buffer_line_curposXYZE(Z_LIFT_FEEDRATE, active_extruder); + st_synchronize(); + if (mesh_point != nMeasPoints * nMeasPoints) { + Sound_MakeSound(e_SOUND_TYPE_StandardAlert); + bool bState; + do { // repeat until Z-leveling o.k. + lcd_display_message_fullscreen_P(_i("Some problem encountered, Z-leveling enforced ...")); +#ifdef TMC2130 + lcd_wait_for_click_delay(MSG_BED_LEVELING_FAILED_TIMEOUT); + calibrate_z_auto(); // Z-leveling (X-assembly stay up!!!) +#else // TMC2130 + lcd_wait_for_click_delay(0); // ~ no timeout + lcd_calibrate_z_end_stop_manual(true); // Z-leveling (X-assembly stay up!!!) +#endif // TMC2130 + // ~ Z-homing (can not be used "G28", because X & Y-homing would have been done before (Z-homing)) + bState=enable_z_endstop(false); + current_position[Z_AXIS] -= 1; + plan_buffer_line_curposXYZE(homing_feedrate[Z_AXIS] / 40, active_extruder); + st_synchronize(); + enable_z_endstop(true); +#ifdef TMC2130 + tmc2130_home_enter(Z_AXIS_MASK); +#endif // TMC2130 + current_position[Z_AXIS] = MESH_HOME_Z_SEARCH; + plan_buffer_line_curposXYZE(homing_feedrate[Z_AXIS] / 40, active_extruder); + st_synchronize(); +#ifdef TMC2130 + tmc2130_home_exit(); +#endif // TMC2130 + enable_z_endstop(bState); + } while (st_get_position_mm(Z_AXIS) > MESH_HOME_Z_SEARCH); // i.e. Z-leveling not o.k. +// plan_set_z_position(MESH_HOME_Z_SEARCH); // is not necessary ('do-while' loop always ends at the expected Z-position) + custom_message_type=CustomMsg::Status; // display / status-line recovery + lcd_update_enable(true); // display / status-line recovery + gcode_G28(true, true, true); // X & Y & Z-homing (must be after individual Z-homing (problem with spool-holder)!) + repeatcommand_front(); // re-run (i.e. of "G80") + break; + } + clean_up_after_endstop_move(l_feedmultiply); +// SERIAL_ECHOLNPGM("clean up finished "); + +#ifndef PINDA_THERMISTOR + if(temp_cal_active == true && calibration_status_pinda() == true) temp_compensation_apply(); //apply PINDA temperature compensation +#endif + babystep_apply(); // Apply Z height correction aka baby stepping before mesh bed leveing gets activated. +// SERIAL_ECHOLNPGM("babystep applied"); + bool eeprom_bed_correction_valid = eeprom_read_byte((unsigned char*)EEPROM_BED_CORRECTION_VALID) == 1; + #ifdef SUPPORT_VERBOSITY + if (verbosity_level >= 1) { + eeprom_bed_correction_valid ? SERIAL_PROTOCOLPGM("Bed correction data valid\n") : SERIAL_PROTOCOLPGM("Bed correction data not valid\n"); + } + #endif // SUPPORT_VERBOSITY + + for (uint8_t i = 0; i < 4; ++i) { + unsigned char codes[4] = { 'L', 'R', 'F', 'B' }; + long correction = 0; + if (code_seen(codes[i])) + correction = code_value_long(); + else if (eeprom_bed_correction_valid) { + unsigned char *addr = (i < 2) ? + ((i == 0) ? (unsigned char*)EEPROM_BED_CORRECTION_LEFT : (unsigned char*)EEPROM_BED_CORRECTION_RIGHT) : + ((i == 2) ? (unsigned char*)EEPROM_BED_CORRECTION_FRONT : (unsigned char*)EEPROM_BED_CORRECTION_REAR); + correction = eeprom_read_int8(addr); + } + if (correction == 0) + continue; + + if (labs(correction) > BED_ADJUSTMENT_UM_MAX) { + SERIAL_ERROR_START; + SERIAL_ECHOPGM("Excessive bed leveling correction: "); + SERIAL_ECHO(correction); + SERIAL_ECHOLNPGM(" microns"); + } + else { + float offset = float(correction) * 0.001f; + switch (i) { + case 0: + for (uint8_t row = 0; row < nMeasPoints; ++row) { + for (uint8_t col = 0; col < nMeasPoints - 1; ++col) { + mbl.z_values[row][col] += offset * (nMeasPoints - 1 - col) / (nMeasPoints - 1); + } + } + break; + case 1: + for (uint8_t row = 0; row < nMeasPoints; ++row) { + for (uint8_t col = 1; col < nMeasPoints; ++col) { + mbl.z_values[row][col] += offset * col / (nMeasPoints - 1); + } + } + break; + case 2: + for (uint8_t col = 0; col < nMeasPoints; ++col) { + for (uint8_t row = 0; row < nMeasPoints; ++row) { + mbl.z_values[row][col] += offset * (nMeasPoints - 1 - row) / (nMeasPoints - 1); + } + } + break; + case 3: + for (uint8_t col = 0; col < nMeasPoints; ++col) { + for (uint8_t row = 1; row < nMeasPoints; ++row) { + mbl.z_values[row][col] += offset * row / (nMeasPoints - 1); + } + } + break; + } + } + } +// SERIAL_ECHOLNPGM("Bed leveling correction finished"); + if (nMeasPoints == 3) { + mbl.upsample_3x3(); //interpolation from 3x3 to 7x7 points using largrangian polynomials while using the same array z_values[iy][ix] for storing (just coppying measured data to new destination and interpolating between them) + } +/* + SERIAL_PROTOCOLPGM("Num X,Y: "); + SERIAL_PROTOCOL(MESH_NUM_X_POINTS); + SERIAL_PROTOCOLPGM(","); + SERIAL_PROTOCOL(MESH_NUM_Y_POINTS); + SERIAL_PROTOCOLPGM("\nZ search height: "); + SERIAL_PROTOCOL(MESH_HOME_Z_SEARCH); + SERIAL_PROTOCOLLNPGM("\nMeasured points:"); + for (int y = MESH_NUM_Y_POINTS-1; y >= 0; y--) { + for (int x = 0; x < MESH_NUM_X_POINTS; x++) { + SERIAL_PROTOCOLPGM(" "); + SERIAL_PROTOCOL_F(mbl.z_values[y][x], 5); + } + SERIAL_PROTOCOLPGM("\n"); + } +*/ + if (nMeasPoints == 7 && magnet_elimination) { + mbl_interpolation(nMeasPoints); + } +/* + SERIAL_PROTOCOLPGM("Num X,Y: "); + SERIAL_PROTOCOL(MESH_NUM_X_POINTS); + SERIAL_PROTOCOLPGM(","); + SERIAL_PROTOCOL(MESH_NUM_Y_POINTS); + SERIAL_PROTOCOLPGM("\nZ search height: "); + SERIAL_PROTOCOL(MESH_HOME_Z_SEARCH); + SERIAL_PROTOCOLLNPGM("\nMeasured points:"); + for (int y = MESH_NUM_Y_POINTS-1; y >= 0; y--) { + for (int x = 0; x < MESH_NUM_X_POINTS; x++) { + SERIAL_PROTOCOLPGM(" "); + SERIAL_PROTOCOL_F(mbl.z_values[y][x], 5); + } + SERIAL_PROTOCOLPGM("\n"); + } +*/ +// SERIAL_ECHOLNPGM("Upsample finished"); + mbl.active = 1; //activate mesh bed leveling +// SERIAL_ECHOLNPGM("Mesh bed leveling activated"); + go_home_with_z_lift(); +// SERIAL_ECHOLNPGM("Go home finished"); + //unretract (after PINDA preheat retraction) + if (degHotend(active_extruder) > EXTRUDE_MINTEMP && temp_cal_active == true && calibration_status_pinda() == true && target_temperature_bed >= 50) { + current_position[E_AXIS] += default_retraction; + plan_buffer_line_curposXYZE(400, active_extruder); + } + KEEPALIVE_STATE(NOT_BUSY); + // Restore custom message state + lcd_setstatuspgm(_T(WELCOME_MSG)); + custom_message_type = custom_message_type_old; + custom_message_state = custom_message_state_old; + mesh_bed_leveling_flag = false; + mesh_bed_run_from_menu = false; + lcd_update(2); + + } + break; + + //! ### G81 - Mesh bed leveling status + // ----------------------------------------- + + /* + * Prints mesh bed leveling status and bed profile if activated + */ + case 81: + if (mbl.active) { + SERIAL_PROTOCOLPGM("Num X,Y: "); + SERIAL_PROTOCOL(MESH_NUM_X_POINTS); + SERIAL_PROTOCOLPGM(","); + SERIAL_PROTOCOL(MESH_NUM_Y_POINTS); + SERIAL_PROTOCOLPGM("\nZ search height: "); + SERIAL_PROTOCOL(MESH_HOME_Z_SEARCH); + SERIAL_PROTOCOLLNPGM("\nMeasured points:"); + for (int y = MESH_NUM_Y_POINTS-1; y >= 0; y--) { + for (int x = 0; x < MESH_NUM_X_POINTS; x++) { + SERIAL_PROTOCOLPGM(" "); + SERIAL_PROTOCOL_F(mbl.z_values[y][x], 5); + } + SERIAL_PROTOCOLPGM("\n"); + } + } + else + SERIAL_PROTOCOLLNPGM("Mesh bed leveling not active."); + break; + +#if 0 + /* + * G82: Single Z probe at current location + * + * WARNING! USE WITH CAUTION! If you'll try to probe where is no leveling pad, nasty things can happen! + * + */ + case 82: + SERIAL_PROTOCOLLNPGM("Finding bed "); + int l_feedmultiply = setup_for_endstop_move(); + find_bed_induction_sensor_point_z(); + clean_up_after_endstop_move(l_feedmultiply); + SERIAL_PROTOCOLPGM("Bed found at: "); + SERIAL_PROTOCOL_F(current_position[Z_AXIS], 5); + SERIAL_PROTOCOLPGM("\n"); + break; + + /* + * G83: Prusa3D specific: Babystep in Z and store to EEPROM + */ + case 83: + { + int babystepz = code_seen('S') ? code_value() : 0; + int BabyPosition = code_seen('P') ? code_value() : 0; + + if (babystepz != 0) { + //FIXME Vojtech: What shall be the index of the axis Z: 3 or 4? + // Is the axis indexed starting with zero or one? + if (BabyPosition > 4) { + SERIAL_PROTOCOLLNPGM("Index out of bounds"); + }else{ + // Save it to the eeprom + babystepLoadZ = babystepz; + EEPROM_save_B(EEPROM_BABYSTEP_Z0+(BabyPosition*2),&babystepLoadZ); + // adjust the Z + babystepsTodoZadd(babystepLoadZ); + } + + } + + } + break; + /* + * G84: Prusa3D specific: UNDO Babystep Z (move Z axis back) + */ + case 84: + babystepsTodoZsubtract(babystepLoadZ); + // babystepLoadZ = 0; + break; + + /* + * G85: Prusa3D specific: Pick best babystep + */ + case 85: + lcd_pick_babystep(); + break; +#endif + + /** + * ### G86 - Disable babystep correction after home + * + * This G-code will be performed at the start of a calibration script. + * (Prusa3D specific) + */ + case 86: + calibration_status_store(CALIBRATION_STATUS_LIVE_ADJUST); + break; + + + /** + * ### G87 - Enable babystep correction after home + * + * + * This G-code will be performed at the end of a calibration script. + * (Prusa3D specific) + */ + case 87: + calibration_status_store(CALIBRATION_STATUS_CALIBRATED); + break; + + + /** + * ### G88 - Reserved + * + * Currently has no effect. + */ + + // Prusa3D specific: Don't know what it is for, it is in V2Calibration.gcode + + case 88: + break; + + +#endif // ENABLE_MESH_BED_LEVELING + + //! ### G90 - Switch off relative mode + // ------------------------------- + case 90: + relative_mode = false; + break; + + //! ### G91 - Switch on relative mode + // ------------------------------- + case 91: + relative_mode = true; + break; + + //! ### G92 - Set position + // ----------------------------- + case 92: + if(!code_seen(axis_codes[E_AXIS])) + st_synchronize(); + for(int8_t i=0; i < NUM_AXIS; i++) { + if(code_seen(axis_codes[i])) { + if(i == E_AXIS) { + current_position[i] = code_value(); + plan_set_e_position(current_position[E_AXIS]); + } + else { + current_position[i] = code_value()+cs.add_homing[i]; + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + } + } + } + break; + + + //! ### G98 - Activate farm mode + // ----------------------------------- + case 98: + farm_mode = 1; + PingTime = _millis(); + eeprom_update_byte((unsigned char *)EEPROM_FARM_MODE, farm_mode); + EEPROM_save_B(EEPROM_FARM_NUMBER, &farm_no); + SilentModeMenu = SILENT_MODE_OFF; + eeprom_update_byte((unsigned char *)EEPROM_SILENT, SilentModeMenu); + fCheckModeInit(); // alternatively invoke printer reset + break; + + //! ### G99 - Deactivate farm mode + // ------------------------------------- + case 99: + farm_mode = 0; + lcd_printer_connected(); + eeprom_update_byte((unsigned char *)EEPROM_FARM_MODE, farm_mode); + lcd_update(2); + fCheckModeInit(); // alternatively invoke printer reset + break; + default: + printf_P(PSTR("Unknown G code: %s \n"), cmdbuffer + bufindr + CMDHDRSIZE); + } +// printf_P(_N("END G-CODE=%u\n"), gcode_in_progress); + gcode_in_progress = 0; + } // end if(code_seen('G')) + + + //! --------------------------------------------------------------------------------- + + else if(code_seen('M')) + { + + int index; + for (index = 1; *(strchr_pointer + index) == ' ' || *(strchr_pointer + index) == '\t'; index++); + + /*for (++strchr_pointer; *strchr_pointer == ' ' || *strchr_pointer == '\t'; ++strchr_pointer);*/ + if (*(strchr_pointer+index) < '0' || *(strchr_pointer+index) > '9') { + printf_P(PSTR("Invalid M code: %s \n"), cmdbuffer + bufindr + CMDHDRSIZE); + + } else + { + mcode_in_progress = (int)code_value(); +// printf_P(_N("BEGIN M-CODE=%u\n"), mcode_in_progress); + + switch(mcode_in_progress) + { + + //! ### M0, M1 - Stop the printer + // --------------------------------------------------------------- + case 0: // M0 - Unconditional stop - Wait for user button press on LCD + case 1: // M1 - Conditional stop - Wait for user button press on LCD + { + char *src = strchr_pointer + 2; + + codenum = 0; + + bool hasP = false, hasS = false; + if (code_seen('P')) { + codenum = code_value(); // milliseconds to wait + hasP = codenum > 0; + } + if (code_seen('S')) { + codenum = code_value() * 1000; // seconds to wait + hasS = codenum > 0; + } + starpos = strchr(src, '*'); + if (starpos != NULL) *(starpos) = '\0'; + while (*src == ' ') ++src; + if (!hasP && !hasS && *src != '\0') { + lcd_setstatus(src); + } else { + LCD_MESSAGERPGM(_i("Wait for user..."));////MSG_USERWAIT + } + + lcd_ignore_click(); //call lcd_ignore_click aslo for else ??? + st_synchronize(); + previous_millis_cmd = _millis(); + if (codenum > 0){ + codenum += _millis(); // keep track of when we started waiting + KEEPALIVE_STATE(PAUSED_FOR_USER); + while(_millis() < codenum && !lcd_clicked()){ + manage_heater(); + manage_inactivity(true); + lcd_update(0); + } + KEEPALIVE_STATE(IN_HANDLER); + lcd_ignore_click(false); + }else{ + marlin_wait_for_click(); + } + if (IS_SD_PRINTING) + LCD_MESSAGERPGM(_T(MSG_RESUMING_PRINT)); + else + LCD_MESSAGERPGM(_T(WELCOME_MSG)); + } + break; + + //! ### M17 - Enable axes + // --------------------------------- + case 17: + LCD_MESSAGERPGM(_i("No move."));////MSG_NO_MOVE + enable_x(); + enable_y(); + enable_z(); + enable_e0(); + enable_e1(); + enable_e2(); + break; + +#ifdef SDSUPPORT + + //! ### M20 - SD Card file list + // ----------------------------------- + case 20: + SERIAL_PROTOCOLLNRPGM(_N("Begin file list"));////MSG_BEGIN_FILE_LIST + card.ls(); + SERIAL_PROTOCOLLNRPGM(_N("End file list"));////MSG_END_FILE_LIST + break; + + //! ### M21 - Init SD card + // ------------------------------------ + case 21: + card.initsd(); + break; + + //! ### M22 - Release SD card + // ----------------------------------- + case 22: + card.release(); + break; + + //! ### M23 - Select file + // ----------------------------------- + case 23: + starpos = (strchr(strchr_pointer + 4,'*')); + if(starpos!=NULL) + *(starpos)='\0'; + card.openFile(strchr_pointer + 4,true); + break; + + //! ### M24 - Start SD print + // ---------------------------------- + case 24: + if (!card.paused) + failstats_reset_print(); + card.startFileprint(); + starttime=_millis(); + break; + + //! ### M25 - Pause SD print + // ---------------------------------- + case 25: + card.pauseSDPrint(); + break; + + //! ### M26 S\ - Set SD index + //! Set position in SD card file to index in bytes. + //! This command is expected to be called after M23 and before M24. + //! Otherwise effect of this command is undefined. + // ---------------------------------- + case 26: + if(card.cardOK && code_seen('S')) { + long index = code_value_long(); + card.setIndex(index); + // We don't disable interrupt during update of sdpos_atomic + // as we expect, that SD card print is not active in this moment + sdpos_atomic = index; + } + break; + + //! ### M27 - Get SD status + // ---------------------------------- + case 27: + card.getStatus(); + break; + + //! ### M28 - Start SD write + // --------------------------------- + case 28: + starpos = (strchr(strchr_pointer + 4,'*')); + if(starpos != NULL){ + char* npos = strchr(CMDBUFFER_CURRENT_STRING, 'N'); + strchr_pointer = strchr(npos,' ') + 1; + *(starpos) = '\0'; + } + card.openFile(strchr_pointer+4,false); + break; + + //! ### M29 - Stop SD write + // ------------------------------------- + //! Currently has no effect. + case 29: + //processed in write to file routine above + //card,saving = false; + break; + + //! ### M30 - Delete file + // ---------------------------------- + case 30: + if (card.cardOK){ + card.closefile(); + starpos = (strchr(strchr_pointer + 4,'*')); + if(starpos != NULL){ + char* npos = strchr(CMDBUFFER_CURRENT_STRING, 'N'); + strchr_pointer = strchr(npos,' ') + 1; + *(starpos) = '\0'; + } + card.removeFile(strchr_pointer + 4); + } + break; + + //! ### M32 - Select file and start SD print + // ------------------------------------ + case 32: + { + if(card.sdprinting) { + st_synchronize(); + + } + starpos = (strchr(strchr_pointer + 4,'*')); + + char* namestartpos = (strchr(strchr_pointer + 4,'!')); //find ! to indicate filename string start. + if(namestartpos==NULL) + { + namestartpos=strchr_pointer + 4; //default name position, 4 letters after the M + } + else + namestartpos++; //to skip the '!' + + if(starpos!=NULL) + *(starpos)='\0'; + + bool call_procedure=(code_seen('P')); + + if(strchr_pointer>namestartpos) + call_procedure=false; //false alert, 'P' found within filename + + if( card.cardOK ) + { + card.openFile(namestartpos,true,!call_procedure); + if(code_seen('S')) + if(strchr_pointer= 0 && pin_status <= 255) + pin_number = code_value(); + for(int8_t i = 0; i < (int8_t)(sizeof(sensitive_pins)/sizeof(int)); i++) + { + if (sensitive_pins[i] == pin_number) + { + pin_number = -1; + break; + } + } + #if defined(FAN_PIN) && FAN_PIN > -1 + if (pin_number == FAN_PIN) + fanSpeed = pin_status; + #endif + if (pin_number > -1) + { + pinMode(pin_number, OUTPUT); + digitalWrite(pin_number, pin_status); + analogWrite(pin_number, pin_status); + } + } + break; + + + //! ### M44 - Reset the bed skew and offset calibration (Prusa specific) + // -------------------------------------------------------------------- + case 44: // M44: Prusa3D: Reset the bed skew and offset calibration. + + // Reset the baby step value and the baby step applied flag. + calibration_status_store(CALIBRATION_STATUS_ASSEMBLED); + eeprom_update_word(reinterpret_cast(&(EEPROM_Sheets_base->s[(eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)))].z_offset)),0); + + // Reset the skew and offset in both RAM and EEPROM. + reset_bed_offset_and_skew(); + // Reset world2machine_rotation_and_skew and world2machine_shift, therefore + // the planner will not perform any adjustments in the XY plane. + // Wait for the motors to stop and update the current position with the absolute values. + world2machine_revert_to_uncorrected(); + break; + + //! ### M45 - Bed skew and offset with manual Z up (Prusa specific) + // ------------------------------------------------------ + case 45: // M45: Prusa3D: bed skew and offset with manual Z up + { + int8_t verbosity_level = 0; + bool only_Z = code_seen('Z'); + #ifdef SUPPORT_VERBOSITY + if (code_seen('V')) + { + // Just 'V' without a number counts as V1. + char c = strchr_pointer[1]; + verbosity_level = (c == ' ' || c == '\t' || c == 0) ? 1 : code_value_short(); + } + #endif //SUPPORT_VERBOSITY + gcode_M45(only_Z, verbosity_level); + } + break; + + /* + case 46: + { + // M46: Prusa3D: Show the assigned IP address. + uint8_t ip[4]; + bool hasIP = card.ToshibaFlashAir_GetIP(ip); + if (hasIP) { + SERIAL_ECHOPGM("Toshiba FlashAir current IP: "); + SERIAL_ECHO(int(ip[0])); + SERIAL_ECHOPGM("."); + SERIAL_ECHO(int(ip[1])); + SERIAL_ECHOPGM("."); + SERIAL_ECHO(int(ip[2])); + SERIAL_ECHOPGM("."); + SERIAL_ECHO(int(ip[3])); + SERIAL_ECHOLNPGM(""); + } else { + SERIAL_ECHOLNPGM("Toshiba FlashAir GetIP failed"); + } + break; + } + */ + + //! ### M47 - Show end stops dialog on the display (Prusa specific) + // ---------------------------------------------------- + case 47: + + KEEPALIVE_STATE(PAUSED_FOR_USER); + lcd_diag_show_end_stops(); + KEEPALIVE_STATE(IN_HANDLER); + break; + +#if 0 + case 48: // M48: scan the bed induction sensor points, print the sensor trigger coordinates to the serial line for visualization on the PC. + { + // Disable the default update procedure of the display. We will do a modal dialog. + lcd_update_enable(false); + // Let the planner use the uncorrected coordinates. + mbl.reset(); + // Reset world2machine_rotation_and_skew and world2machine_shift, therefore + // the planner will not perform any adjustments in the XY plane. + // Wait for the motors to stop and update the current position with the absolute values. + world2machine_revert_to_uncorrected(); + // Move the print head close to the bed. + current_position[Z_AXIS] = MESH_HOME_Z_SEARCH; + plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS],current_position[Z_AXIS] , current_position[E_AXIS], homing_feedrate[Z_AXIS]/40, active_extruder); + st_synchronize(); + // Home in the XY plane. + set_destination_to_current(); + int l_feedmultiply = setup_for_endstop_move(); + home_xy(); + int8_t verbosity_level = 0; + if (code_seen('V')) { + // Just 'V' without a number counts as V1. + char c = strchr_pointer[1]; + verbosity_level = (c == ' ' || c == '\t' || c == 0) ? 1 : code_value_short(); + } + bool success = scan_bed_induction_points(verbosity_level); + clean_up_after_endstop_move(l_feedmultiply); + // Print head up. + current_position[Z_AXIS] = MESH_HOME_Z_SEARCH; + plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS],current_position[Z_AXIS] , current_position[E_AXIS], homing_feedrate[Z_AXIS]/40, active_extruder); + st_synchronize(); + lcd_update_enable(true); + break; + } +#endif + + +#ifdef ENABLE_AUTO_BED_LEVELING +#ifdef Z_PROBE_REPEATABILITY_TEST + + //! ### M48 - Z-Probe repeatability measurement function. + // ------------------------------------------------------ + //! + //! _Usage:_ + //! + //! M48 + //! + //! This function assumes the bed has been homed. Specifically, that a G28 command + //! as been issued prior to invoking the M48 Z-Probe repeatability measurement function. + //! Any information generated by a prior G29 Bed leveling command will be lost and need to be + //! regenerated. + //! + //! The number of samples will default to 10 if not specified. You can use upper or lower case + //! letters for any of the options EXCEPT n. n must be in lower case because Marlin uses a capital + //! N for its communication protocol and will get horribly confused if you send it a capital N. + //! + case 48: // M48 Z-Probe repeatability + { + #if Z_MIN_PIN == -1 + #error "You must have a Z_MIN endstop in order to enable calculation of Z-Probe repeatability." + #endif + + double sum=0.0; + double mean=0.0; + double sigma=0.0; + double sample_set[50]; + int verbose_level=1, n=0, j, n_samples = 10, n_legs=0; + double X_current, Y_current, Z_current; + double X_probe_location, Y_probe_location, Z_start_location, ext_position; + + if (code_seen('V') || code_seen('v')) { + verbose_level = code_value(); + if (verbose_level<0 || verbose_level>4 ) { + SERIAL_PROTOCOLPGM("?Verbose Level not plausable.\n"); + goto Sigma_Exit; + } + } + + if (verbose_level > 0) { + SERIAL_PROTOCOLPGM("M48 Z-Probe Repeatability test. Version 2.00\n"); + SERIAL_PROTOCOLPGM("Full support at: http://3dprintboard.com/forum.php\n"); + } + + if (code_seen('n')) { + n_samples = code_value(); + if (n_samples<4 || n_samples>50 ) { + SERIAL_PROTOCOLPGM("?Specified sample size not plausable.\n"); + goto Sigma_Exit; + } + } + + X_current = X_probe_location = st_get_position_mm(X_AXIS); + Y_current = Y_probe_location = st_get_position_mm(Y_AXIS); + Z_current = st_get_position_mm(Z_AXIS); + Z_start_location = st_get_position_mm(Z_AXIS) + Z_RAISE_BEFORE_PROBING; + ext_position = st_get_position_mm(E_AXIS); + + if (code_seen('X') || code_seen('x') ) { + X_probe_location = code_value() - X_PROBE_OFFSET_FROM_EXTRUDER; + if (X_probe_locationX_MAX_POS ) { + SERIAL_PROTOCOLPGM("?Specified X position out of range.\n"); + goto Sigma_Exit; + } + } + + if (code_seen('Y') || code_seen('y') ) { + Y_probe_location = code_value() - Y_PROBE_OFFSET_FROM_EXTRUDER; + if (Y_probe_locationY_MAX_POS ) { + SERIAL_PROTOCOLPGM("?Specified Y position out of range.\n"); + goto Sigma_Exit; + } + } + + if (code_seen('L') || code_seen('l') ) { + n_legs = code_value(); + if ( n_legs==1 ) + n_legs = 2; + if ( n_legs<0 || n_legs>15 ) { + SERIAL_PROTOCOLPGM("?Specified number of legs in movement not plausable.\n"); + goto Sigma_Exit; + } + } + +// +// Do all the preliminary setup work. First raise the probe. +// + + st_synchronize(); + plan_bed_level_matrix.set_to_identity(); + plan_buffer_line( X_current, Y_current, Z_start_location, + ext_position, + homing_feedrate[Z_AXIS]/60, + active_extruder); + st_synchronize(); + +// +// Now get everything to the specified probe point So we can safely do a probe to +// get us close to the bed. If the Z-Axis is far from the bed, we don't want to +// use that as a starting point for each probe. +// + if (verbose_level > 2) + SERIAL_PROTOCOL("Positioning probe for the test.\n"); + + plan_buffer_line( X_probe_location, Y_probe_location, Z_start_location, + ext_position, + homing_feedrate[X_AXIS]/60, + active_extruder); + st_synchronize(); + + current_position[X_AXIS] = X_current = st_get_position_mm(X_AXIS); + current_position[Y_AXIS] = Y_current = st_get_position_mm(Y_AXIS); + current_position[Z_AXIS] = Z_current = st_get_position_mm(Z_AXIS); + current_position[E_AXIS] = ext_position = st_get_position_mm(E_AXIS); + +// +// OK, do the inital probe to get us close to the bed. +// Then retrace the right amount and use that in subsequent probes +// + + int l_feedmultiply = setup_for_endstop_move(); + run_z_probe(); + + current_position[Z_AXIS] = Z_current = st_get_position_mm(Z_AXIS); + Z_start_location = st_get_position_mm(Z_AXIS) + Z_RAISE_BEFORE_PROBING; + + plan_buffer_line( X_probe_location, Y_probe_location, Z_start_location, + ext_position, + homing_feedrate[X_AXIS]/60, + active_extruder); + st_synchronize(); + current_position[Z_AXIS] = Z_current = st_get_position_mm(Z_AXIS); + + for( n=0; nX_MAX_POS) + X_current = X_MAX_POS; + + if ( Y_currentY_MAX_POS) + Y_current = Y_MAX_POS; + + if (verbose_level>3 ) { + SERIAL_ECHOPAIR("x: ", X_current); + SERIAL_ECHOPAIR("y: ", Y_current); + SERIAL_PROTOCOLLNPGM(""); + } + + do_blocking_move_to( X_current, Y_current, Z_current ); + } + do_blocking_move_to( X_probe_location, Y_probe_location, Z_start_location); // Go back to the probe location + } + + int l_feedmultiply = setup_for_endstop_move(); + run_z_probe(); + + sample_set[n] = current_position[Z_AXIS]; + +// +// Get the current mean for the data points we have so far +// + sum=0.0; + for( j=0; j<=n; j++) { + sum = sum + sample_set[j]; + } + mean = sum / (double (n+1)); +// +// Now, use that mean to calculate the standard deviation for the +// data points we have so far +// + + sum=0.0; + for( j=0; j<=n; j++) { + sum = sum + (sample_set[j]-mean) * (sample_set[j]-mean); + } + sigma = sqrt( sum / (double (n+1)) ); + + if (verbose_level > 1) { + SERIAL_PROTOCOL(n+1); + SERIAL_PROTOCOL(" of "); + SERIAL_PROTOCOL(n_samples); + SERIAL_PROTOCOLPGM(" z: "); + SERIAL_PROTOCOL_F(current_position[Z_AXIS], 6); + } + + if (verbose_level > 2) { + SERIAL_PROTOCOL(" mean: "); + SERIAL_PROTOCOL_F(mean,6); + + SERIAL_PROTOCOL(" sigma: "); + SERIAL_PROTOCOL_F(sigma,6); + } + + if (verbose_level > 0) + SERIAL_PROTOCOLPGM("\n"); + + plan_buffer_line( X_probe_location, Y_probe_location, Z_start_location, + current_position[E_AXIS], homing_feedrate[Z_AXIS]/60, active_extruder); + st_synchronize(); + + } + + _delay(1000); + + clean_up_after_endstop_move(l_feedmultiply); + +// enable_endstops(true); + + if (verbose_level > 0) { + SERIAL_PROTOCOLPGM("Mean: "); + SERIAL_PROTOCOL_F(mean, 6); + SERIAL_PROTOCOLPGM("\n"); + } + +SERIAL_PROTOCOLPGM("Standard Deviation: "); +SERIAL_PROTOCOL_F(sigma, 6); +SERIAL_PROTOCOLPGM("\n\n"); + +Sigma_Exit: + break; + } +#endif // Z_PROBE_REPEATABILITY_TEST +#endif // ENABLE_AUTO_BED_LEVELING + + //! ### M73 - Set/get print progress + // ------------------------------------- + //! _Usage:_ + //! + //! M73 P R Q S + //! + case 73: //M73 show percent done and time remaining + if(code_seen('P')) print_percent_done_normal = code_value(); + if(code_seen('R')) print_time_remaining_normal = code_value(); + if(code_seen('Q')) print_percent_done_silent = code_value(); + if(code_seen('S')) print_time_remaining_silent = code_value(); + + { + const char* _msg_mode_done_remain = _N("%S MODE: Percent done: %d; print time remaining in mins: %d\n"); + printf_P(_msg_mode_done_remain, _N("NORMAL"), int(print_percent_done_normal), print_time_remaining_normal); + printf_P(_msg_mode_done_remain, _N("SILENT"), int(print_percent_done_silent), print_time_remaining_silent); + } + break; + + //! ### M104 - Set hotend temperature + // ----------------------------------------- + case 104: // M104 + { + uint8_t extruder; + if(setTargetedHotend(104,extruder)){ + break; + } + if (code_seen('S')) + { + setTargetHotendSafe(code_value(), extruder); + } + break; + } + + //! ### M112 - Emergency stop + // ----------------------------------------- + case 112: + kill(_n(""), 3); + break; + + //! ### M140 - Set bed temperature + // ----------------------------------------- + case 140: + if (code_seen('S')) setTargetBed(code_value()); + break; + + //! ### M105 - Report temperatures + // ----------------------------------------- + case 105: + { + uint8_t extruder; + if(setTargetedHotend(105, extruder)){ + break; + } + #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1 + SERIAL_PROTOCOLPGM("ok T:"); + SERIAL_PROTOCOL_F(degHotend(extruder),1); + SERIAL_PROTOCOLPGM(" /"); + SERIAL_PROTOCOL_F(degTargetHotend(extruder),1); + #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1 + SERIAL_PROTOCOLPGM(" B:"); + SERIAL_PROTOCOL_F(degBed(),1); + SERIAL_PROTOCOLPGM(" /"); + SERIAL_PROTOCOL_F(degTargetBed(),1); + #endif //TEMP_BED_PIN + for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) { + SERIAL_PROTOCOLPGM(" T"); + SERIAL_PROTOCOL(cur_extruder); + SERIAL_PROTOCOLPGM(":"); + SERIAL_PROTOCOL_F(degHotend(cur_extruder),1); + SERIAL_PROTOCOLPGM(" /"); + SERIAL_PROTOCOL_F(degTargetHotend(cur_extruder),1); + } + #else + SERIAL_ERROR_START; + SERIAL_ERRORLNRPGM(_i("No thermistors - no temperature"));////MSG_ERR_NO_THERMISTORS + #endif + + SERIAL_PROTOCOLPGM(" @:"); + #ifdef EXTRUDER_WATTS + SERIAL_PROTOCOL((EXTRUDER_WATTS * getHeaterPower(tmp_extruder))/127); + SERIAL_PROTOCOLPGM("W"); + #else + SERIAL_PROTOCOL(getHeaterPower(extruder)); + #endif + + SERIAL_PROTOCOLPGM(" B@:"); + #ifdef BED_WATTS + SERIAL_PROTOCOL((BED_WATTS * getHeaterPower(-1))/127); + SERIAL_PROTOCOLPGM("W"); + #else + SERIAL_PROTOCOL(getHeaterPower(-1)); + #endif + +#ifdef PINDA_THERMISTOR + SERIAL_PROTOCOLPGM(" P:"); + SERIAL_PROTOCOL_F(current_temperature_pinda,1); +#endif //PINDA_THERMISTOR + +#ifdef AMBIENT_THERMISTOR + SERIAL_PROTOCOLPGM(" A:"); + SERIAL_PROTOCOL_F(current_temperature_ambient,1); +#endif //AMBIENT_THERMISTOR + + + #ifdef SHOW_TEMP_ADC_VALUES + {float raw = 0.0; + + #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1 + SERIAL_PROTOCOLPGM(" ADC B:"); + SERIAL_PROTOCOL_F(degBed(),1); + SERIAL_PROTOCOLPGM("C->"); + raw = rawBedTemp(); + SERIAL_PROTOCOL_F(raw/OVERSAMPLENR,5); + SERIAL_PROTOCOLPGM(" Rb->"); + SERIAL_PROTOCOL_F(100 * (1 + (PtA * (raw/OVERSAMPLENR)) + (PtB * sq((raw/OVERSAMPLENR)))), 5); + SERIAL_PROTOCOLPGM(" Rxb->"); + SERIAL_PROTOCOL_F(raw, 5); + #endif + for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) { + SERIAL_PROTOCOLPGM(" T"); + SERIAL_PROTOCOL(cur_extruder); + SERIAL_PROTOCOLPGM(":"); + SERIAL_PROTOCOL_F(degHotend(cur_extruder),1); + SERIAL_PROTOCOLPGM("C->"); + raw = rawHotendTemp(cur_extruder); + SERIAL_PROTOCOL_F(raw/OVERSAMPLENR,5); + SERIAL_PROTOCOLPGM(" Rt"); + SERIAL_PROTOCOL(cur_extruder); + SERIAL_PROTOCOLPGM("->"); + SERIAL_PROTOCOL_F(100 * (1 + (PtA * (raw/OVERSAMPLENR)) + (PtB * sq((raw/OVERSAMPLENR)))), 5); + SERIAL_PROTOCOLPGM(" Rx"); + SERIAL_PROTOCOL(cur_extruder); + SERIAL_PROTOCOLPGM("->"); + SERIAL_PROTOCOL_F(raw, 5); + }} + #endif + SERIAL_PROTOCOLLN(""); + KEEPALIVE_STATE(NOT_BUSY); + return; + break; + } + + //! ### M109 - Wait for extruder temperature + //! Parameters (not mandatory): + //! * S \ set extruder temperature + //! * R \ set extruder temperature + //! + //! Parameters S and R are treated identically. + //! Command always waits for both cool down and heat up. + //! If no parameters are supplied waits for previously + //! set extruder temperature. + // ------------------------------------------------- + case 109: + { + uint8_t extruder; + if(setTargetedHotend(109, extruder)){ + break; + } + LCD_MESSAGERPGM(_T(MSG_HEATING)); + heating_status = 1; + if (farm_mode) { prusa_statistics(1); }; + +#ifdef AUTOTEMP + autotemp_enabled=false; + #endif + if (code_seen('S')) { + setTargetHotendSafe(code_value(), extruder); + } else if (code_seen('R')) { + setTargetHotendSafe(code_value(), extruder); + } + #ifdef AUTOTEMP + if (code_seen('S')) autotemp_min=code_value(); + if (code_seen('B')) autotemp_max=code_value(); + if (code_seen('F')) + { + autotemp_factor=code_value(); + autotemp_enabled=true; + } + #endif + + codenum = _millis(); + + /* See if we are heating up or cooling down */ + target_direction = isHeatingHotend(extruder); // true if heating, false if cooling + + KEEPALIVE_STATE(NOT_BUSY); + + cancel_heatup = false; + + wait_for_heater(codenum, extruder); //loops until target temperature is reached + + LCD_MESSAGERPGM(_T(MSG_HEATING_COMPLETE)); + KEEPALIVE_STATE(IN_HANDLER); + heating_status = 2; + if (farm_mode) { prusa_statistics(2); }; + + //starttime=_millis(); + previous_millis_cmd = _millis(); + } + break; + + //! ### M190 - Wait for bed temperature + //! Parameters (not mandatory): + //! * S \ set extruder temperature and wait for heating + //! * R \ set extruder temperature and wait for heating or cooling + //! + //! If no parameter is supplied, waits for heating or cooling to previously set temperature. + case 190: + #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1 + { + bool CooldownNoWait = false; + LCD_MESSAGERPGM(_T(MSG_BED_HEATING)); + heating_status = 3; + if (farm_mode) { prusa_statistics(1); }; + if (code_seen('S')) + { + setTargetBed(code_value()); + CooldownNoWait = true; + } + else if (code_seen('R')) + { + setTargetBed(code_value()); + } + codenum = _millis(); + + cancel_heatup = false; + target_direction = isHeatingBed(); // true if heating, false if cooling + + KEEPALIVE_STATE(NOT_BUSY); + while ( (target_direction)&&(!cancel_heatup) ? (isHeatingBed()) : (isCoolingBed()&&(CooldownNoWait==false)) ) + { + if(( _millis() - codenum) > 1000 ) //Print Temp Reading every 1 second while heating up. + { + if (!farm_mode) { + float tt = degHotend(active_extruder); + SERIAL_PROTOCOLPGM("T:"); + SERIAL_PROTOCOL(tt); + SERIAL_PROTOCOLPGM(" E:"); + SERIAL_PROTOCOL((int)active_extruder); + SERIAL_PROTOCOLPGM(" B:"); + SERIAL_PROTOCOL_F(degBed(), 1); + SERIAL_PROTOCOLLN(""); + } + codenum = _millis(); + + } + manage_heater(); + manage_inactivity(); + lcd_update(0); + } + LCD_MESSAGERPGM(_T(MSG_BED_DONE)); + KEEPALIVE_STATE(IN_HANDLER); + heating_status = 4; + + previous_millis_cmd = _millis(); + } + #endif + break; + + #if defined(FAN_PIN) && FAN_PIN > -1 + + //! ### M106 - Set fan speed + // ------------------------------------------- + case 106: // M106 Sxxx Fan On S 0 .. 255 + if (code_seen('S')){ + fanSpeed=constrain(code_value(),0,255); + } + else { + fanSpeed=255; + } + break; + + //! ### M107 - Fan off + // ------------------------------- + case 107: + fanSpeed = 0; + break; + #endif //FAN_PIN + + #if defined(PS_ON_PIN) && PS_ON_PIN > -1 + + //! ### M80 - Turn on the Power Supply + // ------------------------------- + case 80: + SET_OUTPUT(PS_ON_PIN); //GND + WRITE(PS_ON_PIN, PS_ON_AWAKE); + + // If you have a switch on suicide pin, this is useful + // if you want to start another print with suicide feature after + // a print without suicide... + #if defined SUICIDE_PIN && SUICIDE_PIN > -1 + SET_OUTPUT(SUICIDE_PIN); + WRITE(SUICIDE_PIN, HIGH); + #endif + + powersupply = true; + LCD_MESSAGERPGM(_T(WELCOME_MSG)); + lcd_update(0); + break; + #endif + + //! ### M81 - Turn off Power Supply + // -------------------------------------- + case 81: + disable_heater(); + st_synchronize(); + disable_e0(); + disable_e1(); + disable_e2(); + finishAndDisableSteppers(); + fanSpeed = 0; + _delay(1000); // Wait a little before to switch off + #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1 + st_synchronize(); + suicide(); + #elif defined(PS_ON_PIN) && PS_ON_PIN > -1 + SET_OUTPUT(PS_ON_PIN); + WRITE(PS_ON_PIN, PS_ON_ASLEEP); + #endif + powersupply = false; + LCD_MESSAGERPGM(CAT4(CUSTOM_MENDEL_NAME,PSTR(" "),MSG_OFF,PSTR("."))); + lcd_update(0); + break; + + //! ### M82 - Set E axis to absolute mode + // --------------------------------------- + case 82: + axis_relative_modes[3] = false; + break; + + //! ### M83 - Set E axis to relative mode + // --------------------------------------- + case 83: + axis_relative_modes[3] = true; + break; + + //! ### M84, M18 - Disable steppers + //--------------------------------------- + //! This command can be used to set the stepper inactivity timeout (`S`) or to disable steppers (`X`,`Y`,`Z`,`E`) + //! + //! M84 [E] [S] [X] [Y] [Z] + //! + case 18: //compatibility + case 84: // M84 + if(code_seen('S')){ + stepper_inactive_time = code_value() * 1000; + } + else + { + bool all_axis = !((code_seen(axis_codes[X_AXIS])) || (code_seen(axis_codes[Y_AXIS])) || (code_seen(axis_codes[Z_AXIS]))|| (code_seen(axis_codes[E_AXIS]))); + if(all_axis) + { + st_synchronize(); + disable_e0(); + disable_e1(); + disable_e2(); + finishAndDisableSteppers(); + } + else + { + st_synchronize(); + if (code_seen('X')) disable_x(); + if (code_seen('Y')) disable_y(); + if (code_seen('Z')) disable_z(); +#if ((E0_ENABLE_PIN != X_ENABLE_PIN) && (E1_ENABLE_PIN != Y_ENABLE_PIN)) // Only enable on boards that have seperate ENABLE_PINS + if (code_seen('E')) { + disable_e0(); + disable_e1(); + disable_e2(); + } + #endif + } + } + //in the end of print set estimated time to end of print and extruders used during print to default values for next print + print_time_remaining_init(); + snmm_filaments_used = 0; + break; + + //! ### M85 - Set max inactive time + // --------------------------------------- + case 85: // M85 + if(code_seen('S')) { + max_inactive_time = code_value() * 1000; + } + break; +#ifdef SAFETYTIMER + + //! ### M86 - Set safety timer expiration time + //! + //! _Usage:_ + //! M86 S + //! + //! Sets the safety timer expiration time in seconds. M86 S0 will disable safety timer. + //! When safety timer expires, heatbed and nozzle target temperatures are set to zero. + case 86: + if (code_seen('S')) { + safetytimer_inactive_time = code_value() * 1000; + safetyTimer.start(); + } + break; +#endif + + //! ### M92 Set Axis steps-per-unit + // --------------------------------------- + //! Same syntax as G92 + case 92: + for(int8_t i=0; i < NUM_AXIS; i++) + { + if(code_seen(axis_codes[i])) + { + if(i == 3) { // E + float value = code_value(); + if(value < 20.0) { + float factor = cs.axis_steps_per_unit[i] / value; // increase e constants if M92 E14 is given for netfab. + cs.max_jerk[E_AXIS] *= factor; + max_feedrate[i] *= factor; + axis_steps_per_sqr_second[i] *= factor; + } + cs.axis_steps_per_unit[i] = value; + } + else { + cs.axis_steps_per_unit[i] = code_value(); + } + } + } + break; + + //! ### M110 - Set Line number + // --------------------------------------- + case 110: + if (code_seen('N')) + gcode_LastN = code_value_long(); + break; + + //! ### M113 - Get or set host keep-alive interval + // ------------------------------------------ + case 113: + if (code_seen('S')) { + host_keepalive_interval = (uint8_t)code_value_short(); +// NOMORE(host_keepalive_interval, 60); + } + else { + SERIAL_ECHO_START; + SERIAL_ECHOPAIR("M113 S", (unsigned long)host_keepalive_interval); + SERIAL_PROTOCOLLN(""); + } + break; + + //! ### M115 - Firmware info + // -------------------------------------- + //! Print the firmware info and capabilities + //! + //! M115 [V] [U] + //! + //! Without any arguments, prints Prusa firmware version number, machine type, extruder count and UUID. + //! `M115 U` Checks the firmware version provided. If the firmware version provided by the U code is higher than the currently running firmware, + //! pause the print for 30s and ask the user to upgrade the firmware. + case 115: // M115 + if (code_seen('V')) { + // Report the Prusa version number. + SERIAL_PROTOCOLLNRPGM(FW_VERSION_STR_P()); + } else if (code_seen('U')) { + // Check the firmware version provided. If the firmware version provided by the U code is higher than the currently running firmware, + // pause the print for 30s and ask the user to upgrade the firmware. + show_upgrade_dialog_if_version_newer(++ strchr_pointer); + } else { + SERIAL_ECHOPGM("FIRMWARE_NAME:Prusa-Firmware "); + SERIAL_ECHORPGM(FW_VERSION_STR_P()); + SERIAL_ECHOPGM(" based on Marlin FIRMWARE_URL:https://github.com/prusa3d/Prusa-Firmware PROTOCOL_VERSION:"); + SERIAL_ECHOPGM(PROTOCOL_VERSION); + SERIAL_ECHOPGM(" MACHINE_TYPE:"); + SERIAL_ECHOPGM(CUSTOM_MENDEL_NAME); + SERIAL_ECHOPGM(" EXTRUDER_COUNT:"); + SERIAL_ECHOPGM(STRINGIFY(EXTRUDERS)); + SERIAL_ECHOPGM(" UUID:"); + SERIAL_ECHOLNPGM(MACHINE_UUID); + } + break; + + //! ### M114 - Get current position + // ------------------------------------- + case 114: + gcode_M114(); + break; + + + + //! ### M117 - Set LCD Message + // -------------------------------------- + + /* + M117 moved up to get the high priority + + case 117: // M117 display message + starpos = (strchr(strchr_pointer + 5,'*')); + if(starpos!=NULL) + *(starpos)='\0'; + lcd_setstatus(strchr_pointer + 5); + break;*/ + + //! ### M120 - Disable endstops + // ---------------------------------------- + case 120: + enable_endstops(false) ; + break; + + //! ### M121 - Enable endstops + // ---------------------------------------- + case 121: + enable_endstops(true) ; + break; + + //! ### M119 - Get endstop states + // ---------------------------------------- + case 119: + SERIAL_PROTOCOLRPGM(_N("Reporting endstop status"));////MSG_M119_REPORT + SERIAL_PROTOCOLLN(""); + #if defined(X_MIN_PIN) && X_MIN_PIN > -1 + SERIAL_PROTOCOLRPGM(_n("x_min: "));////MSG_X_MIN + if(READ(X_MIN_PIN)^X_MIN_ENDSTOP_INVERTING){ + SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_HIT); + }else{ + SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_OPEN); + } + SERIAL_PROTOCOLLN(""); + #endif + #if defined(X_MAX_PIN) && X_MAX_PIN > -1 + SERIAL_PROTOCOLRPGM(_n("x_max: "));////MSG_X_MAX + if(READ(X_MAX_PIN)^X_MAX_ENDSTOP_INVERTING){ + SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_HIT); + }else{ + SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_OPEN); + } + SERIAL_PROTOCOLLN(""); + #endif + #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1 + SERIAL_PROTOCOLRPGM(_n("y_min: "));////MSG_Y_MIN + if(READ(Y_MIN_PIN)^Y_MIN_ENDSTOP_INVERTING){ + SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_HIT); + }else{ + SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_OPEN); + } + SERIAL_PROTOCOLLN(""); + #endif + #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1 + SERIAL_PROTOCOLRPGM(_n("y_max: "));////MSG_Y_MAX + if(READ(Y_MAX_PIN)^Y_MAX_ENDSTOP_INVERTING){ + SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_HIT); + }else{ + SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_OPEN); + } + SERIAL_PROTOCOLLN(""); + #endif + #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1 + SERIAL_PROTOCOLRPGM(MSG_Z_MIN); + if(READ(Z_MIN_PIN)^Z_MIN_ENDSTOP_INVERTING){ + SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_HIT); + }else{ + SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_OPEN); + } + SERIAL_PROTOCOLLN(""); + #endif + #if defined(Z_MAX_PIN) && Z_MAX_PIN > -1 + SERIAL_PROTOCOLRPGM(MSG_Z_MAX); + if(READ(Z_MAX_PIN)^Z_MAX_ENDSTOP_INVERTING){ + SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_HIT); + }else{ + SERIAL_PROTOCOLRPGM(MSG_ENDSTOP_OPEN); + } + SERIAL_PROTOCOLLN(""); + #endif + break; + //TODO: update for all axis, use for loop + + #ifdef BLINKM + + //! ### M150 - Set RGB(W) Color + // ------------------------------------------- + case 150: + { + byte red; + byte grn; + byte blu; + + if(code_seen('R')) red = code_value(); + if(code_seen('U')) grn = code_value(); + if(code_seen('B')) blu = code_value(); + + SendColors(red,grn,blu); + } + break; + #endif //BLINKM + + //! ### M200 - Set filament diameter + // ---------------------------------------- + case 200: // M200 D set filament diameter and set E axis units to cubic millimeters (use S0 to set back to millimeters). + { + + uint8_t extruder = active_extruder; + if(code_seen('T')) { + extruder = code_value(); + if(extruder >= EXTRUDERS) { + SERIAL_ECHO_START; + SERIAL_ECHO(_n("M200 Invalid extruder "));////MSG_M200_INVALID_EXTRUDER + break; + } + } + if(code_seen('D')) { + float diameter = (float)code_value(); + if (diameter == 0.0) { + // setting any extruder filament size disables volumetric on the assumption that + // slicers either generate in extruder values as cubic mm or as as filament feeds + // for all extruders + cs.volumetric_enabled = false; + } else { + cs.filament_size[extruder] = (float)code_value(); + // make sure all extruders have some sane value for the filament size + cs.filament_size[0] = (cs.filament_size[0] == 0.0 ? DEFAULT_NOMINAL_FILAMENT_DIA : cs.filament_size[0]); + #if EXTRUDERS > 1 + cs.filament_size[1] = (cs.filament_size[1] == 0.0 ? DEFAULT_NOMINAL_FILAMENT_DIA : cs.filament_size[1]); + #if EXTRUDERS > 2 + cs.filament_size[2] = (cs.filament_size[2] == 0.0 ? DEFAULT_NOMINAL_FILAMENT_DIA : cs.filament_size[2]); + #endif + #endif + cs.volumetric_enabled = true; + } + } else { + //reserved for setting filament diameter via UFID or filament measuring device + break; + } + calculate_extruder_multipliers(); + } + break; + + //! ### M201 - Set Print Max Acceleration + // ------------------------------------------- + case 201: + for (int8_t i = 0; i < NUM_AXIS; i++) + { + if (code_seen(axis_codes[i])) + { + unsigned long val = code_value(); +#ifdef TMC2130 + unsigned long val_silent = val; + if ((i == X_AXIS) || (i == Y_AXIS)) + { + if (val > NORMAL_MAX_ACCEL_XY) + val = NORMAL_MAX_ACCEL_XY; + if (val_silent > SILENT_MAX_ACCEL_XY) + val_silent = SILENT_MAX_ACCEL_XY; + } + cs.max_acceleration_units_per_sq_second_normal[i] = val; + cs.max_acceleration_units_per_sq_second_silent[i] = val_silent; +#else //TMC2130 + max_acceleration_units_per_sq_second[i] = val; +#endif //TMC2130 + } + } + // steps per sq second need to be updated to agree with the units per sq second (as they are what is used in the planner) + reset_acceleration_rates(); + break; + #if 0 // Not used for Sprinter/grbl gen6 + case 202: // M202 + for(int8_t i=0; i < NUM_AXIS; i++) { + if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * cs.axis_steps_per_unit[i]; + } + break; + #endif + + //! ### M203 - Set Max Feedrate + // --------------------------------------- + case 203: // M203 max feedrate mm/sec + for (int8_t i = 0; i < NUM_AXIS; i++) + { + if (code_seen(axis_codes[i])) + { + float val = code_value(); +#ifdef TMC2130 + float val_silent = val; + if ((i == X_AXIS) || (i == Y_AXIS)) + { + if (val > NORMAL_MAX_FEEDRATE_XY) + val = NORMAL_MAX_FEEDRATE_XY; + if (val_silent > SILENT_MAX_FEEDRATE_XY) + val_silent = SILENT_MAX_FEEDRATE_XY; + } + cs.max_feedrate_normal[i] = val; + cs.max_feedrate_silent[i] = val_silent; +#else //TMC2130 + max_feedrate[i] = val; +#endif //TMC2130 + } + } + break; + + //! ### M204 - Acceleration settings + // ------------------------------------------ + //! Supporting old format: + //! + //! M204 S[normal moves] T[filmanent only moves] + //! + //! and new format: + //! + //! M204 P[printing moves] R[filmanent only moves] T[travel moves] (as of now T is ignored) + case 204: + { + if(code_seen('S')) { + // Legacy acceleration format. This format is used by the legacy Marlin, MK2 or MK3 firmware, + // and it is also generated by Slic3r to control acceleration per extrusion type + // (there is a separate acceleration settings in Slicer for perimeter, first layer etc). + cs.acceleration = code_value(); + // Interpret the T value as retract acceleration in the old Marlin format. + if(code_seen('T')) + cs.retract_acceleration = code_value(); + } else { + // New acceleration format, compatible with the upstream Marlin. + if(code_seen('P')) + cs.acceleration = code_value(); + if(code_seen('R')) + cs.retract_acceleration = code_value(); + if(code_seen('T')) { + // Interpret the T value as the travel acceleration in the new Marlin format. + //FIXME Prusa3D firmware currently does not support travel acceleration value independent from the extruding acceleration value. + // travel_acceleration = code_value(); + } + } + } + break; + + //! ### M205 - Set advanced settings + // --------------------------------------------- + //! Set some advanced settings related to movement. + //! + //! M205 [S] [T] [B] [X] [Y] [Z] [E] + /*! + - `S` - Minimum feedrate for print moves (unit/s) + - `T` - Minimum feedrate for travel moves (units/s) + - `B` - Minimum segment time (us) + - `X` - Maximum X jerk (units/s), similarly for other axes + */ + case 205: + { + if(code_seen('S')) cs.minimumfeedrate = code_value(); + if(code_seen('T')) cs.mintravelfeedrate = code_value(); + if(code_seen('B')) cs.minsegmenttime = code_value() ; + if(code_seen('X')) cs.max_jerk[X_AXIS] = cs.max_jerk[Y_AXIS] = code_value(); + if(code_seen('Y')) cs.max_jerk[Y_AXIS] = code_value(); + if(code_seen('Z')) cs.max_jerk[Z_AXIS] = code_value(); + if(code_seen('E')) cs.max_jerk[E_AXIS] = code_value(); + if (cs.max_jerk[X_AXIS] > DEFAULT_XJERK) cs.max_jerk[X_AXIS] = DEFAULT_XJERK; + if (cs.max_jerk[Y_AXIS] > DEFAULT_YJERK) cs.max_jerk[Y_AXIS] = DEFAULT_YJERK; + } + break; + + //! ### M206 - Set additional homing offsets + // ---------------------------------------------- + case 206: + for(int8_t i=0; i < 3; i++) + { + if(code_seen(axis_codes[i])) cs.add_homing[i] = code_value(); + } + break; + #ifdef FWRETRACT + + //! ### M207 - Set firmware retraction + // -------------------------------------------------- + case 207: //M207 - set retract length S[positive mm] F[feedrate mm/min] Z[additional zlift/hop] + { + if(code_seen('S')) + { + cs.retract_length = code_value() ; + } + if(code_seen('F')) + { + cs.retract_feedrate = code_value()/60 ; + } + if(code_seen('Z')) + { + cs.retract_zlift = code_value() ; + } + }break; + + //! ### M208 - Set retract recover length + // -------------------------------------------- + case 208: // M208 - set retract recover length S[positive mm surplus to the M207 S*] F[feedrate mm/min] + { + if(code_seen('S')) + { + cs.retract_recover_length = code_value() ; + } + if(code_seen('F')) + { + cs.retract_recover_feedrate = code_value()/60 ; + } + }break; + + //! ### M209 - Enable/disable automatict retract + // --------------------------------------------- + case 209: // M209 - S<1=true/0=false> enable automatic retract detect if the slicer did not support G10/11: every normal extrude-only move will be classified as retract depending on the direction. + { + if(code_seen('S')) + { + int t= code_value() ; + switch(t) + { + case 0: + { + cs.autoretract_enabled=false; + retracted[0]=false; + #if EXTRUDERS > 1 + retracted[1]=false; + #endif + #if EXTRUDERS > 2 + retracted[2]=false; + #endif + }break; + case 1: + { + cs.autoretract_enabled=true; + retracted[0]=false; + #if EXTRUDERS > 1 + retracted[1]=false; + #endif + #if EXTRUDERS > 2 + retracted[2]=false; + #endif + }break; + default: + SERIAL_ECHO_START; + SERIAL_ECHORPGM(MSG_UNKNOWN_COMMAND); + SERIAL_ECHO(CMDBUFFER_CURRENT_STRING); + SERIAL_ECHOLNPGM("\"(1)"); + } + } + + }break; + #endif // FWRETRACT + #if EXTRUDERS > 1 + + // ### M218 - Set hotend offset + // ---------------------------------------- + case 218: // M218 - set hotend offset (in mm), T X Y + { + uint8_t extruder; + if(setTargetedHotend(218, extruder)){ + break; + } + if(code_seen('X')) + { + extruder_offset[X_AXIS][extruder] = code_value(); + } + if(code_seen('Y')) + { + extruder_offset[Y_AXIS][extruder] = code_value(); + } + SERIAL_ECHO_START; + SERIAL_ECHORPGM(MSG_HOTEND_OFFSET); + for(extruder = 0; extruder < EXTRUDERS; extruder++) + { + SERIAL_ECHO(" "); + SERIAL_ECHO(extruder_offset[X_AXIS][extruder]); + SERIAL_ECHO(","); + SERIAL_ECHO(extruder_offset[Y_AXIS][extruder]); + } + SERIAL_ECHOLN(""); + }break; + #endif + + //! ### M220 Set feedrate percentage + // ----------------------------------------------- + case 220: // M220 S- set speed factor override percentage + { + if (code_seen('B')) //backup current speed factor + { + saved_feedmultiply_mm = feedmultiply; + } + if(code_seen('S')) + { + feedmultiply = code_value() ; + } + if (code_seen('R')) { //restore previous feedmultiply + feedmultiply = saved_feedmultiply_mm; + } + } + break; + + //! ### M221 - Set extrude factor override percentage + // ---------------------------------------------------- + case 221: // M221 S- set extrude factor override percentage + { + if(code_seen('S')) + { + int tmp_code = code_value(); + if (code_seen('T')) + { + uint8_t extruder; + if(setTargetedHotend(221, extruder)){ + break; + } + extruder_multiply[extruder] = tmp_code; + } + else + { + extrudemultiply = tmp_code ; + } + } + calculate_extruder_multipliers(); + } + break; + + //! ### M226 - Wait for Pin state + // ------------------------------------------ + case 226: // M226 P S- Wait until the specified pin reaches the state required + { + if(code_seen('P')){ + int pin_number = code_value(); // pin number + int pin_state = -1; // required pin state - default is inverted + + if(code_seen('S')) pin_state = code_value(); // required pin state + + if(pin_state >= -1 && pin_state <= 1){ + + for(int8_t i = 0; i < (int8_t)(sizeof(sensitive_pins)/sizeof(int)); i++) + { + if (sensitive_pins[i] == pin_number) + { + pin_number = -1; + break; + } + } + + if (pin_number > -1) + { + int target = LOW; + + st_synchronize(); + + pinMode(pin_number, INPUT); + + switch(pin_state){ + case 1: + target = HIGH; + break; + + case 0: + target = LOW; + break; + + case -1: + target = !digitalRead(pin_number); + break; + } + + while(digitalRead(pin_number) != target){ + manage_heater(); + manage_inactivity(); + lcd_update(0); + } + } + } + } + } + break; + + #if NUM_SERVOS > 0 + + //! ### M280 - Set/Get servo position + // -------------------------------------------- + case 280: // M280 - set servo position absolute. P: servo index, S: angle or microseconds + { + int servo_index = -1; + int servo_position = 0; + if (code_seen('P')) + servo_index = code_value(); + if (code_seen('S')) { + servo_position = code_value(); + if ((servo_index >= 0) && (servo_index < NUM_SERVOS)) { +#if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0) + servos[servo_index].attach(0); +#endif + servos[servo_index].write(servo_position); +#if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0) + _delay(PROBE_SERVO_DEACTIVATION_DELAY); + servos[servo_index].detach(); +#endif + } + else { + SERIAL_ECHO_START; + SERIAL_ECHO("Servo "); + SERIAL_ECHO(servo_index); + SERIAL_ECHOLN(" out of range"); + } + } + else if (servo_index >= 0) { + SERIAL_PROTOCOL(MSG_OK); + SERIAL_PROTOCOL(" Servo "); + SERIAL_PROTOCOL(servo_index); + SERIAL_PROTOCOL(": "); + SERIAL_PROTOCOL(servos[servo_index].read()); + SERIAL_PROTOCOLLN(""); + } + } + break; + #endif // NUM_SERVOS > 0 + + #if (LARGE_FLASH == true && ( BEEPER > 0 || defined(ULTRALCD) || defined(LCD_USE_I2C_BUZZER))) + + //! ### M300 - Play tone + // ----------------------- + case 300: // M300 + { + int beepS = code_seen('S') ? code_value() : 110; + int beepP = code_seen('P') ? code_value() : 1000; + if (beepS > 0) + { + #if BEEPER > 0 + Sound_MakeCustom(beepP,beepS,false); + #endif + } + else + { + _delay(beepP); + } + } + break; + #endif // M300 + + #ifdef PIDTEMP + + //! ### M301 - Set hotend PID + // --------------------------------------- + case 301: + { + if(code_seen('P')) cs.Kp = code_value(); + if(code_seen('I')) cs.Ki = scalePID_i(code_value()); + if(code_seen('D')) cs.Kd = scalePID_d(code_value()); + + #ifdef PID_ADD_EXTRUSION_RATE + if(code_seen('C')) Kc = code_value(); + #endif + + updatePID(); + SERIAL_PROTOCOLRPGM(MSG_OK); + SERIAL_PROTOCOL(" p:"); + SERIAL_PROTOCOL(cs.Kp); + SERIAL_PROTOCOL(" i:"); + SERIAL_PROTOCOL(unscalePID_i(cs.Ki)); + SERIAL_PROTOCOL(" d:"); + SERIAL_PROTOCOL(unscalePID_d(cs.Kd)); + #ifdef PID_ADD_EXTRUSION_RATE + SERIAL_PROTOCOL(" c:"); + //Kc does not have scaling applied above, or in resetting defaults + SERIAL_PROTOCOL(Kc); + #endif + SERIAL_PROTOCOLLN(""); + } + break; + #endif //PIDTEMP + #ifdef PIDTEMPBED + + //! ### M304 - Set bed PID + // -------------------------------------- + case 304: + { + if(code_seen('P')) cs.bedKp = code_value(); + if(code_seen('I')) cs.bedKi = scalePID_i(code_value()); + if(code_seen('D')) cs.bedKd = scalePID_d(code_value()); + + updatePID(); + SERIAL_PROTOCOLRPGM(MSG_OK); + SERIAL_PROTOCOL(" p:"); + SERIAL_PROTOCOL(cs.bedKp); + SERIAL_PROTOCOL(" i:"); + SERIAL_PROTOCOL(unscalePID_i(cs.bedKi)); + SERIAL_PROTOCOL(" d:"); + SERIAL_PROTOCOL(unscalePID_d(cs.bedKd)); + SERIAL_PROTOCOLLN(""); + } + break; + #endif //PIDTEMP + + //! ### M240 - Trigger camera + // -------------------------------------------- + case 240: // M240 Triggers a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/ + { + #ifdef CHDK + + SET_OUTPUT(CHDK); + WRITE(CHDK, HIGH); + chdkHigh = _millis(); + chdkActive = true; + + #else + + #if defined(PHOTOGRAPH_PIN) && PHOTOGRAPH_PIN > -1 + const uint8_t NUM_PULSES=16; + const float PULSE_LENGTH=0.01524; + for(int i=0; i < NUM_PULSES; i++) { + WRITE(PHOTOGRAPH_PIN, HIGH); + _delay_ms(PULSE_LENGTH); + WRITE(PHOTOGRAPH_PIN, LOW); + _delay_ms(PULSE_LENGTH); + } + _delay(7.33); + for(int i=0; i < NUM_PULSES; i++) { + WRITE(PHOTOGRAPH_PIN, HIGH); + _delay_ms(PULSE_LENGTH); + WRITE(PHOTOGRAPH_PIN, LOW); + _delay_ms(PULSE_LENGTH); + } + #endif + #endif //chdk end if + } + break; + #ifdef PREVENT_DANGEROUS_EXTRUDE + + //! ### M302 - Allow cold extrude, or set minimum extrude temperature + // ------------------------------------------------------------------- + case 302: + { + float temp = .0; + if (code_seen('S')) temp=code_value(); + set_extrude_min_temp(temp); + } + break; + #endif + + //! ### M303 - PID autotune + // ------------------------------------- + case 303: + { + float temp = 150.0; + int e=0; + int c=5; + if (code_seen('E')) e=code_value(); + if (e<0) + temp=70; + if (code_seen('S')) temp=code_value(); + if (code_seen('C')) c=code_value(); + PID_autotune(temp, e, c); + } + break; + + //! ### M400 - Wait for all moves to finish + // ----------------------------------------- + case 400: + { + st_synchronize(); + } + break; + + //! ### M403 - Set filament type (material) for particular extruder and notify the MMU + // ---------------------------------------------- + case 403: + { + // currently three different materials are needed (default, flex and PVA) + // add storing this information for different load/unload profiles etc. in the future + // firmware does not wait for "ok" from mmu + if (mmu_enabled) + { + uint8_t extruder = 255; + uint8_t filament = FILAMENT_UNDEFINED; + if(code_seen('E')) extruder = code_value(); + if(code_seen('F')) filament = code_value(); + mmu_set_filament_type(extruder, filament); + } + } + break; + + //! ### M500 - Store settings in EEPROM + // ----------------------------------------- + case 500: + { + Config_StoreSettings(); + } + break; + + //! ### M501 - Read settings from EEPROM + // ---------------------------------------- + case 501: + { + Config_RetrieveSettings(); + } + break; + + //! ### M502 - Revert all settings to factory default + // ------------------------------------------------- + case 502: + { + Config_ResetDefault(); + } + break; + + //! ### M503 - Repport all settings currently in memory + // ------------------------------------------------- + case 503: + { + Config_PrintSettings(); + } + break; + + //! ### M509 - Force language selection + // ------------------------------------------------ + case 509: + { + lang_reset(); + SERIAL_ECHO_START; + SERIAL_PROTOCOLPGM(("LANG SEL FORCED")); + } + break; + #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED + + //! ### M540 - Abort print on endstop hit (enable/disable) + // ----------------------------------------------------- + case 540: + { + if(code_seen('S')) abort_on_endstop_hit = code_value() > 0; + } + break; + #endif + + #ifdef CUSTOM_M_CODE_SET_Z_PROBE_OFFSET + case CUSTOM_M_CODE_SET_Z_PROBE_OFFSET: + { + float value; + if (code_seen('Z')) + { + value = code_value(); + if ((Z_PROBE_OFFSET_RANGE_MIN <= value) && (value <= Z_PROBE_OFFSET_RANGE_MAX)) + { + cs.zprobe_zoffset = -value; // compare w/ line 278 of ConfigurationStore.cpp + SERIAL_ECHO_START; + SERIAL_ECHOLNRPGM(CAT4(MSG_ZPROBE_ZOFFSET, " ", MSG_OK,PSTR(""))); + SERIAL_PROTOCOLLN(""); + } + else + { + SERIAL_ECHO_START; + SERIAL_ECHORPGM(MSG_ZPROBE_ZOFFSET); + SERIAL_ECHORPGM(MSG_Z_MIN); + SERIAL_ECHO(Z_PROBE_OFFSET_RANGE_MIN); + SERIAL_ECHORPGM(MSG_Z_MAX); + SERIAL_ECHO(Z_PROBE_OFFSET_RANGE_MAX); + SERIAL_PROTOCOLLN(""); + } + } + else + { + SERIAL_ECHO_START; + SERIAL_ECHOLNRPGM(CAT2(MSG_ZPROBE_ZOFFSET, PSTR(" : "))); + SERIAL_ECHO(-cs.zprobe_zoffset); + SERIAL_PROTOCOLLN(""); + } + break; + } + #endif // CUSTOM_M_CODE_SET_Z_PROBE_OFFSET + + #ifdef FILAMENTCHANGEENABLE + + //! ### M600 - Initiate Filament change procedure + // -------------------------------------- + case 600: //Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract] L[later retract distance for removal] + { + st_synchronize(); + + float x_position = current_position[X_AXIS]; + float y_position = current_position[Y_AXIS]; + float z_shift = 0; // is it necessary to be a float? + float e_shift_init = 0; + float e_shift_late = 0; + bool automatic = false; + + //Retract extruder + if(code_seen('E')) + { + e_shift_init = code_value(); + } + else + { + #ifdef FILAMENTCHANGE_FIRSTRETRACT + e_shift_init = FILAMENTCHANGE_FIRSTRETRACT ; + #endif + } + + //currently don't work as we are using the same unload sequence as in M702, needs re-work + if (code_seen('L')) + { + e_shift_late = code_value(); + } + else + { + #ifdef FILAMENTCHANGE_FINALRETRACT + e_shift_late = FILAMENTCHANGE_FINALRETRACT; + #endif + } + + //Lift Z + if(code_seen('Z')) + { + z_shift = code_value(); + } + else + { + z_shift = gcode_M600_filament_change_z_shift(); + } + //Move XY to side + if(code_seen('X')) + { + x_position = code_value(); + } + else + { + #ifdef FILAMENTCHANGE_XPOS + x_position = FILAMENTCHANGE_XPOS; + #endif + } + if(code_seen('Y')) + { + y_position = code_value(); + } + else + { + #ifdef FILAMENTCHANGE_YPOS + y_position = FILAMENTCHANGE_YPOS ; + #endif + } + + if (mmu_enabled && code_seen("AUTO")) + automatic = true; + + gcode_M600(automatic, x_position, y_position, z_shift, e_shift_init, e_shift_late); + + } + break; + #endif //FILAMENTCHANGEENABLE + + //! ### M601 - Pause print + // ------------------------------- + case 601: + { + cmdqueue_pop_front(); //trick because we want skip this command (M601) after restore + lcd_pause_print(); + } + break; + + //! ### M602 - Resume print + // ------------------------------- + case 602: { + lcd_resume_print(); + } + break; + + //! ### M603 - Stop print + // ------------------------------- + case 603: { + lcd_print_stop(); + } + +#ifdef PINDA_THERMISTOR + //! ### M860 - Wait for extruder temperature (PINDA) + // -------------------------------------------------------------- + /*! + Wait for PINDA thermistor to reach target temperature + + M860 [S] + + */ + case 860: + { + int set_target_pinda = 0; + + if (code_seen('S')) { + set_target_pinda = code_value(); + } + else { + break; + } + + LCD_MESSAGERPGM(_T(MSG_PLEASE_WAIT)); + + SERIAL_PROTOCOLPGM("Wait for PINDA target temperature:"); + SERIAL_PROTOCOL(set_target_pinda); + SERIAL_PROTOCOLLN(""); + + codenum = _millis(); + cancel_heatup = false; + + bool is_pinda_cooling = false; + if ((degTargetBed() == 0) && (degTargetHotend(0) == 0)) { + is_pinda_cooling = true; + } + + while ( ((!is_pinda_cooling) && (!cancel_heatup) && (current_temperature_pinda < set_target_pinda)) || (is_pinda_cooling && (current_temperature_pinda > set_target_pinda)) ) { + if ((_millis() - codenum) > 1000) //Print Temp Reading every 1 second while waiting. + { + SERIAL_PROTOCOLPGM("P:"); + SERIAL_PROTOCOL_F(current_temperature_pinda, 1); + SERIAL_PROTOCOLPGM("/"); + SERIAL_PROTOCOL(set_target_pinda); + SERIAL_PROTOCOLLN(""); + codenum = _millis(); + } + manage_heater(); + manage_inactivity(); + lcd_update(0); + } + LCD_MESSAGERPGM(MSG_OK); + + break; + } + + //! ### M861 - Set/Get PINDA temperature compensation offsets + // ----------------------------------------------------------- + /*! + + M861 [ ? | ! | Z | S [I] ] + + - `?` - Print current EEPROM offset values + - `!` - Set factory default values + - `Z` - Set all values to 0 (effectively disabling PINDA temperature compensation) + - `S` `I` - Set compensation ustep value S for compensation table index I + */ + case 861: + if (code_seen('?')) { // ? - Print out current EEPROM offset values + uint8_t cal_status = calibration_status_pinda(); + int16_t usteps = 0; + cal_status ? SERIAL_PROTOCOLLN("PINDA cal status: 1") : SERIAL_PROTOCOLLN("PINDA cal status: 0"); + SERIAL_PROTOCOLLN("index, temp, ustep, um"); + for (uint8_t i = 0; i < 6; i++) + { + if(i>0) EEPROM_read_B(EEPROM_PROBE_TEMP_SHIFT + (i-1) * 2, &usteps); + float mm = ((float)usteps) / cs.axis_steps_per_unit[Z_AXIS]; + i == 0 ? SERIAL_PROTOCOLPGM("n/a") : SERIAL_PROTOCOL(i - 1); + SERIAL_PROTOCOLPGM(", "); + SERIAL_PROTOCOL(35 + (i * 5)); + SERIAL_PROTOCOLPGM(", "); + SERIAL_PROTOCOL(usteps); + SERIAL_PROTOCOLPGM(", "); + SERIAL_PROTOCOL(mm * 1000); + SERIAL_PROTOCOLLN(""); + } + } + else if (code_seen('!')) { // ! - Set factory default values + eeprom_write_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, 1); + int16_t z_shift = 8; //40C - 20um - 8usteps + EEPROM_save_B(EEPROM_PROBE_TEMP_SHIFT, &z_shift); + z_shift = 24; //45C - 60um - 24usteps + EEPROM_save_B(EEPROM_PROBE_TEMP_SHIFT + 2, &z_shift); + z_shift = 48; //50C - 120um - 48usteps + EEPROM_save_B(EEPROM_PROBE_TEMP_SHIFT + 4, &z_shift); + z_shift = 80; //55C - 200um - 80usteps + EEPROM_save_B(EEPROM_PROBE_TEMP_SHIFT + 6, &z_shift); + z_shift = 120; //60C - 300um - 120usteps + EEPROM_save_B(EEPROM_PROBE_TEMP_SHIFT + 8, &z_shift); + SERIAL_PROTOCOLLN("factory restored"); + } + else if (code_seen('Z')) { // Z - Set all values to 0 (effectively disabling PINDA temperature compensation) + eeprom_write_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, 1); + int16_t z_shift = 0; + for (uint8_t i = 0; i < 5; i++) EEPROM_save_B(EEPROM_PROBE_TEMP_SHIFT + i * 2, &z_shift); + SERIAL_PROTOCOLLN("zerorized"); + } + else if (code_seen('S')) { // Sxxx Iyyy - Set compensation ustep value S for compensation table index I + int16_t usteps = code_value(); + if (code_seen('I')) { + uint8_t index = code_value(); + if (index < 5) { + EEPROM_save_B(EEPROM_PROBE_TEMP_SHIFT + index * 2, &usteps); + SERIAL_PROTOCOLLN("OK"); + SERIAL_PROTOCOLLN("index, temp, ustep, um"); + for (uint8_t i = 0; i < 6; i++) + { + usteps = 0; + if (i>0) EEPROM_read_B(EEPROM_PROBE_TEMP_SHIFT + (i - 1) * 2, &usteps); + float mm = ((float)usteps) / cs.axis_steps_per_unit[Z_AXIS]; + i == 0 ? SERIAL_PROTOCOLPGM("n/a") : SERIAL_PROTOCOL(i - 1); + SERIAL_PROTOCOLPGM(", "); + SERIAL_PROTOCOL(35 + (i * 5)); + SERIAL_PROTOCOLPGM(", "); + SERIAL_PROTOCOL(usteps); + SERIAL_PROTOCOLPGM(", "); + SERIAL_PROTOCOL(mm * 1000); + SERIAL_PROTOCOLLN(""); + } + } + } + } + else { + SERIAL_PROTOCOLPGM("no valid command"); + } + break; + +#endif //PINDA_THERMISTOR + + //! ### M862 - Print checking + // ---------------------------------------------- + /*! + Checks the parameters of the printer and gcode and performs compatibility check + - M862.1 { P | Q } + - M862.2 { P | Q } + - M862.3 { P"" | Q } + - M862.4 { P | Q } + - M862.5 { P | Q } + + When run with P<> argument, the check is performed against the input value. + When run with Q argument, the current value is shown. + + M862.3 accepts text identifiers of printer types too. + The syntax of M862.3 is (note the quotes around the type): + + M862.3 P "MK3S" + + Accepted printer type identifiers and their numeric counterparts: + - MK1 (100) + - MK2 (200) + - MK2MM (201) + - MK2S (202) + - MK2SMM (203) + - MK2.5 (250) + - MK2.5MMU2 (20250) + - MK2.5S (252) + - MK2.5SMMU2S (20252) + - MK3 (300) + - MK3MMU2 (20300) + - MK3S (302) + - MK3SMMU2S (20302) + */ + case 862: // M862: print checking + float nDummy; + uint8_t nCommand; + nCommand=(uint8_t)(modff(code_value_float(),&nDummy)*10.0+0.5); + switch((ClPrintChecking)nCommand) + { + case ClPrintChecking::_Nozzle: // ~ .1 + uint16_t nDiameter; + if(code_seen('P')) + { + nDiameter=(uint16_t)(code_value()*1000.0+0.5); // [,um] + nozzle_diameter_check(nDiameter); + } +/* + else if(code_seen('S')&&farm_mode) + { + nDiameter=(uint16_t)(code_value()*1000.0+0.5); // [,um] + eeprom_update_byte((uint8_t*)EEPROM_NOZZLE_DIAMETER,(uint8_t)ClNozzleDiameter::_Diameter_Undef); // for correct synchronization after farm-mode exiting + eeprom_update_word((uint16_t*)EEPROM_NOZZLE_DIAMETER_uM,nDiameter); + } +*/ + else if(code_seen('Q')) + SERIAL_PROTOCOLLN((float)eeprom_read_word((uint16_t*)EEPROM_NOZZLE_DIAMETER_uM)/1000.0); + break; + case ClPrintChecking::_Model: // ~ .2 + if(code_seen('P')) + { + uint16_t nPrinterModel; + nPrinterModel=(uint16_t)code_value_long(); + printer_model_check(nPrinterModel); + } + else if(code_seen('Q')) + SERIAL_PROTOCOLLN(nPrinterType); + break; + case ClPrintChecking::_Smodel: // ~ .3 + if(code_seen('P')) + printer_smodel_check(strchr_pointer); + else if(code_seen('Q')) + SERIAL_PROTOCOLLNRPGM(sPrinterName); + break; + case ClPrintChecking::_Version: // ~ .4 + if(code_seen('P')) + fw_version_check(++strchr_pointer); + else if(code_seen('Q')) + SERIAL_PROTOCOLLN(FW_VERSION); + break; + case ClPrintChecking::_Gcode: // ~ .5 + if(code_seen('P')) + { + uint16_t nGcodeLevel; + nGcodeLevel=(uint16_t)code_value_long(); + gcode_level_check(nGcodeLevel); + } + else if(code_seen('Q')) + SERIAL_PROTOCOLLN(GCODE_LEVEL); + break; + } + break; + +#ifdef LIN_ADVANCE + //! ### M900 - Set Linear advance options + // ---------------------------------------------- + case 900: + gcode_M900(); + break; +#endif + + //! ### M907 - Set digital trimpot motor current in mA using axis codes + // --------------------------------------------------------------- + case 907: + { +#ifdef TMC2130 + //! See tmc2130_cur2val() for translation to 0 .. 63 range + for (int i = 0; i < NUM_AXIS; i++) + if(code_seen(axis_codes[i])) + { + long cur_mA = code_value_long(); + uint8_t val = tmc2130_cur2val(cur_mA); + tmc2130_set_current_h(i, val); + tmc2130_set_current_r(i, val); + //if (i == E_AXIS) printf_P(PSTR("E-axis current=%ldmA\n"), cur_mA); + } + +#else //TMC2130 + #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1 + for(int i=0;i -1 + uint8_t channel,current; + if(code_seen('P')) channel=code_value(); + if(code_seen('S')) current=code_value(); + digitalPotWrite(channel, current); + #endif + } + break; + +#ifdef TMC2130_SERVICE_CODES_M910_M918 + + //! ### M910 - TMC2130 init + // ----------------------------------------------- + case 910: + { + tmc2130_init(); + } + break; + + //! ### M911 - Set TMC2130 holding currents + // ------------------------------------------------- + case 911: + { + if (code_seen('X')) tmc2130_set_current_h(0, code_value()); + if (code_seen('Y')) tmc2130_set_current_h(1, code_value()); + if (code_seen('Z')) tmc2130_set_current_h(2, code_value()); + if (code_seen('E')) tmc2130_set_current_h(3, code_value()); + } + break; + + //! ### M912 - Set TMC2130 running currents + // ----------------------------------------------- + case 912: + { + if (code_seen('X')) tmc2130_set_current_r(0, code_value()); + if (code_seen('Y')) tmc2130_set_current_r(1, code_value()); + if (code_seen('Z')) tmc2130_set_current_r(2, code_value()); + if (code_seen('E')) tmc2130_set_current_r(3, code_value()); + } + break; + + //! ### M913 - Print TMC2130 currents + // ----------------------------- + case 913: + { + tmc2130_print_currents(); + } + break; + + //! ### M914 - Set TMC2130 normal mode + // ------------------------------ + case 914: + { + tmc2130_mode = TMC2130_MODE_NORMAL; + update_mode_profile(); + tmc2130_init(); + } + break; + + //! ### M95 - Set TMC2130 silent mode + // ------------------------------ + case 915: + { + tmc2130_mode = TMC2130_MODE_SILENT; + update_mode_profile(); + tmc2130_init(); + } + break; + + //! ### M916 - Set TMC2130 Stallguard sensitivity threshold + // ------------------------------------------------------- + case 916: + { + if (code_seen('X')) tmc2130_sg_thr[X_AXIS] = code_value(); + if (code_seen('Y')) tmc2130_sg_thr[Y_AXIS] = code_value(); + if (code_seen('Z')) tmc2130_sg_thr[Z_AXIS] = code_value(); + if (code_seen('E')) tmc2130_sg_thr[E_AXIS] = code_value(); + for (uint8_t a = X_AXIS; a <= E_AXIS; a++) + printf_P(_N("tmc2130_sg_thr[%c]=%d\n"), "XYZE"[a], tmc2130_sg_thr[a]); + } + break; + + //! ### M917 - Set TMC2130 PWM amplitude offset (pwm_ampl) + // -------------------------------------------------------------- + case 917: + { + if (code_seen('X')) tmc2130_set_pwm_ampl(0, code_value()); + if (code_seen('Y')) tmc2130_set_pwm_ampl(1, code_value()); + if (code_seen('Z')) tmc2130_set_pwm_ampl(2, code_value()); + if (code_seen('E')) tmc2130_set_pwm_ampl(3, code_value()); + } + break; + + //! ### M918 - Set TMC2130 PWM amplitude gradient (pwm_grad) + // ------------------------------------------------------------- + case 918: + { + if (code_seen('X')) tmc2130_set_pwm_grad(0, code_value()); + if (code_seen('Y')) tmc2130_set_pwm_grad(1, code_value()); + if (code_seen('Z')) tmc2130_set_pwm_grad(2, code_value()); + if (code_seen('E')) tmc2130_set_pwm_grad(3, code_value()); + } + break; + +#endif //TMC2130_SERVICE_CODES_M910_M918 + + //! ### M350 - Set microstepping mode + // --------------------------------------------------- + //! Warning: Steps per unit remains unchanged. S code sets stepping mode for all drivers. + case 350: + { + #ifdef TMC2130 + if(code_seen('E')) + { + uint16_t res_new = code_value(); + if ((res_new == 8) || (res_new == 16) || (res_new == 32) || (res_new == 64) || (res_new == 128)) + { + st_synchronize(); + uint8_t axis = E_AXIS; + uint16_t res = tmc2130_get_res(axis); + tmc2130_set_res(axis, res_new); + cs.axis_ustep_resolution[axis] = res_new; + if (res_new > res) + { + uint16_t fac = (res_new / res); + cs.axis_steps_per_unit[axis] *= fac; + position[E_AXIS] *= fac; + } + else + { + uint16_t fac = (res / res_new); + cs.axis_steps_per_unit[axis] /= fac; + position[E_AXIS] /= fac; + } + } + } + #else //TMC2130 + #if defined(X_MS1_PIN) && X_MS1_PIN > -1 + if(code_seen('S')) for(int i=0;i<=4;i++) microstep_mode(i,code_value()); + for(int i=0;i] [E<0|1>] S<1|2> [X<0|1>] [Y<0|1>] [Z<0|1>] + case 351: + { + #if defined(X_MS1_PIN) && X_MS1_PIN > -1 + if(code_seen('S')) switch((int)code_value()) + { + case 1: + for(int i=0;i - select extruder in case of multi extruder printer + //! select filament in case of MMU_V2 + //! if extruder is "?", open menu to let the user select extruder/filament + //! + //! For MMU_V2: + //! @n T Gcode to extrude at least 38.10 mm at feedrate 19.02 mm/s must follow immediately to load to extruder wheels. + //! @n T? Gcode to extrude shouldn't have to follow, load to extruder wheels is done automatically + //! @n Tx Same as T?, except nozzle doesn't have to be preheated. Tc must be placed after extruder nozzle is preheated to finish filament load. + //! @n Tc Load to nozzle after filament was prepared by Tc and extruder nozzle is already heated. + else if(code_seen('T')) + { + int index; + bool load_to_nozzle = false; + for (index = 1; *(strchr_pointer + index) == ' ' || *(strchr_pointer + index) == '\t'; index++); + + *(strchr_pointer + index) = tolower(*(strchr_pointer + index)); + + if ((*(strchr_pointer + index) < '0' || *(strchr_pointer + index) > '4') && *(strchr_pointer + index) != '?' && *(strchr_pointer + index) != 'x' && *(strchr_pointer + index) != 'c') { + SERIAL_ECHOLNPGM("Invalid T code."); + } + else if (*(strchr_pointer + index) == 'x'){ //load to bondtech gears; if mmu is not present do nothing + if (mmu_enabled) + { + tmp_extruder = choose_menu_P(_T(MSG_CHOOSE_FILAMENT), _T(MSG_FILAMENT)); + if ((tmp_extruder == mmu_extruder) && mmu_fil_loaded) //dont execute the same T-code twice in a row + { + printf_P(PSTR("Duplicate T-code ignored.\n")); + } + else + { + st_synchronize(); + mmu_command(MmuCmd::T0 + tmp_extruder); + manage_response(true, true, MMU_TCODE_MOVE); + } + } + } + else if (*(strchr_pointer + index) == 'c') { //load to from bondtech gears to nozzle (nozzle should be preheated) + if (mmu_enabled) + { + st_synchronize(); + mmu_continue_loading(is_usb_printing || (lcd_commands_type == LcdCommands::Layer1Cal)); + mmu_extruder = tmp_extruder; //filament change is finished + mmu_load_to_nozzle(); + } + } + else { + if (*(strchr_pointer + index) == '?') + { + if(mmu_enabled) + { + tmp_extruder = choose_menu_P(_T(MSG_CHOOSE_FILAMENT), _T(MSG_FILAMENT)); + load_to_nozzle = true; + } else + { + tmp_extruder = choose_menu_P(_T(MSG_CHOOSE_EXTRUDER), _T(MSG_EXTRUDER)); + } + } + else { + tmp_extruder = code_value(); + if (mmu_enabled && lcd_autoDepleteEnabled()) + { + tmp_extruder = ad_getAlternative(tmp_extruder); + } + } + st_synchronize(); + snmm_filaments_used |= (1 << tmp_extruder); //for stop print + + if (mmu_enabled) + { + if ((tmp_extruder == mmu_extruder) && mmu_fil_loaded) //dont execute the same T-code twice in a row + { + printf_P(PSTR("Duplicate T-code ignored.\n")); + } + else + { +#if defined(MMU_HAS_CUTTER) && defined(MMU_ALWAYS_CUT) + if (EEPROM_MMU_CUTTER_ENABLED_always == eeprom_read_byte((uint8_t*)EEPROM_MMU_CUTTER_ENABLED)) + { + mmu_command(MmuCmd::K0 + tmp_extruder); + manage_response(true, true, MMU_UNLOAD_MOVE); + } +#endif //defined(MMU_HAS_CUTTER) && defined(MMU_ALWAYS_CUT) + mmu_command(MmuCmd::T0 + tmp_extruder); + manage_response(true, true, MMU_TCODE_MOVE); + mmu_continue_loading(is_usb_printing || (lcd_commands_type == LcdCommands::Layer1Cal)); + + mmu_extruder = tmp_extruder; //filament change is finished + + if (load_to_nozzle)// for single material usage with mmu + { + mmu_load_to_nozzle(); + } + } + } + else + { +#ifdef SNMM + +#ifdef LIN_ADVANCE + if (mmu_extruder != tmp_extruder) + clear_current_adv_vars(); //Check if the selected extruder is not the active one and reset LIN_ADVANCE variables if so. +#endif + + mmu_extruder = tmp_extruder; + + + _delay(100); + + disable_e0(); + disable_e1(); + disable_e2(); + + pinMode(E_MUX0_PIN, OUTPUT); + pinMode(E_MUX1_PIN, OUTPUT); + + _delay(100); + SERIAL_ECHO_START; + SERIAL_ECHO("T:"); + SERIAL_ECHOLN((int)tmp_extruder); + switch (tmp_extruder) { + case 1: + WRITE(E_MUX0_PIN, HIGH); + WRITE(E_MUX1_PIN, LOW); + + break; + case 2: + WRITE(E_MUX0_PIN, LOW); + WRITE(E_MUX1_PIN, HIGH); + + break; + case 3: + WRITE(E_MUX0_PIN, HIGH); + WRITE(E_MUX1_PIN, HIGH); + + break; + default: + WRITE(E_MUX0_PIN, LOW); + WRITE(E_MUX1_PIN, LOW); + + break; + } + _delay(100); + +#else //SNMM + if (tmp_extruder >= EXTRUDERS) { + SERIAL_ECHO_START; + SERIAL_ECHOPGM("T"); + SERIAL_PROTOCOLLN((int)tmp_extruder); + SERIAL_ECHOLNRPGM(_n("Invalid extruder"));////MSG_INVALID_EXTRUDER + } + else { +#if EXTRUDERS > 1 + boolean make_move = false; +#endif + if (code_seen('F')) { +#if EXTRUDERS > 1 + make_move = true; +#endif + next_feedrate = code_value(); + if (next_feedrate > 0.0) { + feedrate = next_feedrate; + } + } +#if EXTRUDERS > 1 + if (tmp_extruder != active_extruder) { + // Save current position to return to after applying extruder offset + memcpy(destination, current_position, sizeof(destination)); + // Offset extruder (only by XY) + int i; + for (i = 0; i < 2; i++) { + current_position[i] = current_position[i] - + extruder_offset[i][active_extruder] + + extruder_offset[i][tmp_extruder]; + } + // Set the new active extruder and position + active_extruder = tmp_extruder; + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + // Move to the old position if 'F' was in the parameters + if (make_move && Stopped == false) { + prepare_move(); + } + } +#endif + SERIAL_ECHO_START; + SERIAL_ECHORPGM(_n("Active Extruder: "));////MSG_ACTIVE_EXTRUDER + SERIAL_PROTOCOLLN((int)active_extruder); + } + +#endif //SNMM + } + } + } // end if(code_seen('T')) (end of T codes) + + //! ---------------------------------------------------------------------------------------------- + + else if (code_seen('D')) // D codes (debug) + { + switch((int)code_value()) + { + + //! ### D-1 - Endless loop + // ------------------- + case -1: + dcode__1(); break; +#ifdef DEBUG_DCODES + + //! ### D0 - Reset + // -------------- + case 0: + dcode_0(); break; + + //! ### D1 - Clear EEPROM + // ------------------ + case 1: + dcode_1(); break; + + //! ### D2 - Read/Write RAM + // -------------------- + case 2: + dcode_2(); break; +#endif //DEBUG_DCODES +#ifdef DEBUG_DCODE3 + + //! ### D3 - Read/Write EEPROM + // ----------------------- + case 3: + dcode_3(); break; +#endif //DEBUG_DCODE3 +#ifdef DEBUG_DCODES + + //! ### D4 - Read/Write PIN + // --------------------- + case 4: + dcode_4(); break; +#endif //DEBUG_DCODES +#ifdef DEBUG_DCODE5 + + //! ### D5 - Read/Write FLASH + // ------------------------ + case 5: + dcode_5(); break; + break; +#endif //DEBUG_DCODE5 +#ifdef DEBUG_DCODES + + //! ### D6 - Read/Write external FLASH + // --------------------------------------- + case 6: + dcode_6(); break; + + //! ### D7 - Read/Write Bootloader + // ------------------------------- + case 7: + dcode_7(); break; + + //! ### D8 - Read/Write PINDA + // --------------------------- + case 8: + dcode_8(); break; + + // ### D9 - Read/Write ADC + // ------------------------ + case 9: + dcode_9(); break; + + //! ### D10 - XYZ calibration = OK + // ------------------------------ + case 10: + dcode_10(); break; +#endif //DEBUG_DCODES +#ifdef HEATBED_ANALYSIS + + //! ### D80 - Bed check + // --------------------- + /*! + - `E` - dimension x + - `F` - dimention y + - `G` - points_x + - `H` - points_y + - `I` - offset_x + - `J` - offset_y + */ + case 80: + { + float dimension_x = 40; + float dimension_y = 40; + int points_x = 40; + int points_y = 40; + float offset_x = 74; + float offset_y = 33; + + if (code_seen('E')) dimension_x = code_value(); + if (code_seen('F')) dimension_y = code_value(); + if (code_seen('G')) {points_x = code_value(); } + if (code_seen('H')) {points_y = code_value(); } + if (code_seen('I')) {offset_x = code_value(); } + if (code_seen('J')) {offset_y = code_value(); } + printf_P(PSTR("DIM X: %f\n"), dimension_x); + printf_P(PSTR("DIM Y: %f\n"), dimension_y); + printf_P(PSTR("POINTS X: %d\n"), points_x); + printf_P(PSTR("POINTS Y: %d\n"), points_y); + printf_P(PSTR("OFFSET X: %f\n"), offset_x); + printf_P(PSTR("OFFSET Y: %f\n"), offset_y); + bed_check(dimension_x,dimension_y,points_x,points_y,offset_x,offset_y); + }break; + + //! ### D81 - Bed analysis + // ----------------------------- + /*! + - `E` - dimension x + - `F` - dimention y + - `G` - points_x + - `H` - points_y + - `I` - offset_x + - `J` - offset_y + */ + case 81: + { + float dimension_x = 40; + float dimension_y = 40; + int points_x = 40; + int points_y = 40; + float offset_x = 74; + float offset_y = 33; + + if (code_seen('E')) dimension_x = code_value(); + if (code_seen('F')) dimension_y = code_value(); + if (code_seen("G")) { strchr_pointer+=1; points_x = code_value(); } + if (code_seen("H")) { strchr_pointer+=1; points_y = code_value(); } + if (code_seen("I")) { strchr_pointer+=1; offset_x = code_value(); } + if (code_seen("J")) { strchr_pointer+=1; offset_y = code_value(); } + + bed_analysis(dimension_x,dimension_y,points_x,points_y,offset_x,offset_y); + + } break; + +#endif //HEATBED_ANALYSIS +#ifdef DEBUG_DCODES + + //! ### D106 print measured fan speed for different pwm values + // -------------------------------------------------------------- + case 106: + { + for (int i = 255; i > 0; i = i - 5) { + fanSpeed = i; + //delay_keep_alive(2000); + for (int j = 0; j < 100; j++) { + delay_keep_alive(100); + + } + printf_P(_N("%d: %d\n"), i, fan_speed[1]); + } + }break; + +#ifdef TMC2130 + //! ### D2130 - TMC2130 Trinamic stepper controller + // --------------------------- + + + /*! + + + D2130[subcommand][value] + + - : + - '0' current off + - '1' current on + - '+' single step + - * value sereval steps + - '-' dtto oposite direction + - '?' read register + - * "mres" + - * "step" + - * "mscnt" + - * "mscuract" + - * "wave" + - '!' set register + - * "mres" + - * "step" + - * "wave" + - '@' home calibrate axis + + Example: + + D2130E?wave ... print extruder microstep linearity compensation curve + + D2130E!wave0 ... disable extruder linearity compensation curve, (sine curve is used) + + D2130E!wave220 ... (sin(x))^1.1 extruder microstep compensation curve used + */ + + + case 2130: + dcode_2130(); break; +#endif //TMC2130 + +#if (defined (FILAMENT_SENSOR) && defined(PAT9125)) + + //! ### D9125 - FILAMENT_SENSOR + // --------------------------------- + case 9125: + dcode_9125(); break; +#endif //FILAMENT_SENSOR + +#endif //DEBUG_DCODES + } + } + + else + { + SERIAL_ECHO_START; + SERIAL_ECHORPGM(MSG_UNKNOWN_COMMAND); + SERIAL_ECHO(CMDBUFFER_CURRENT_STRING); + SERIAL_ECHOLNPGM("\"(2)"); + } + KEEPALIVE_STATE(NOT_BUSY); + ClearToSend(); +} + + + + /** @defgroup GCodes G-Code List + */ + +// --------------------------------------------------- + +void FlushSerialRequestResend() +{ + //char cmdbuffer[bufindr][100]="Resend:"; + MYSERIAL.flush(); + printf_P(_N("%S: %ld\n%S\n"), _n("Resend"), gcode_LastN + 1, MSG_OK); +} + +// Confirm the execution of a command, if sent from a serial line. +// Execution of a command from a SD card will not be confirmed. +void ClearToSend() +{ + previous_millis_cmd = _millis(); + if ((CMDBUFFER_CURRENT_TYPE == CMDBUFFER_CURRENT_TYPE_USB) || (CMDBUFFER_CURRENT_TYPE == CMDBUFFER_CURRENT_TYPE_USB_WITH_LINENR)) + SERIAL_PROTOCOLLNRPGM(MSG_OK); +} + +#if MOTHERBOARD == BOARD_RAMBO_MINI_1_0 || MOTHERBOARD == BOARD_RAMBO_MINI_1_3 +void update_currents() { + float current_high[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD; + float current_low[3] = DEFAULT_PWM_MOTOR_CURRENT; + float tmp_motor[3]; + + //SERIAL_ECHOLNPGM("Currents updated: "); + + if (destination[Z_AXIS] < Z_SILENT) { + //SERIAL_ECHOLNPGM("LOW"); + for (uint8_t i = 0; i < 3; i++) { + st_current_set(i, current_low[i]); + /*MYSERIAL.print(int(i)); + SERIAL_ECHOPGM(": "); + MYSERIAL.println(current_low[i]);*/ + } + } + else if (destination[Z_AXIS] > Z_HIGH_POWER) { + //SERIAL_ECHOLNPGM("HIGH"); + for (uint8_t i = 0; i < 3; i++) { + st_current_set(i, current_high[i]); + /*MYSERIAL.print(int(i)); + SERIAL_ECHOPGM(": "); + MYSERIAL.println(current_high[i]);*/ + } + } + else { + for (uint8_t i = 0; i < 3; i++) { + float q = current_low[i] - Z_SILENT*((current_high[i] - current_low[i]) / (Z_HIGH_POWER - Z_SILENT)); + tmp_motor[i] = ((current_high[i] - current_low[i]) / (Z_HIGH_POWER - Z_SILENT))*destination[Z_AXIS] + q; + st_current_set(i, tmp_motor[i]); + /*MYSERIAL.print(int(i)); + SERIAL_ECHOPGM(": "); + MYSERIAL.println(tmp_motor[i]);*/ + } + } +} +#endif //MOTHERBOARD == BOARD_RAMBO_MINI_1_0 || MOTHERBOARD == BOARD_RAMBO_MINI_1_3 + +void get_coordinates() +{ + bool seen[4]={false,false,false,false}; + for(int8_t i=0; i < NUM_AXIS; i++) { + if(code_seen(axis_codes[i])) + { + bool relative = axis_relative_modes[i] || relative_mode; + destination[i] = (float)code_value(); + if (i == E_AXIS) { + float emult = extruder_multiplier[active_extruder]; + if (emult != 1.) { + if (! relative) { + destination[i] -= current_position[i]; + relative = true; + } + destination[i] *= emult; + } + } + if (relative) + destination[i] += current_position[i]; + seen[i]=true; +#if MOTHERBOARD == BOARD_RAMBO_MINI_1_0 || MOTHERBOARD == BOARD_RAMBO_MINI_1_3 + if (i == Z_AXIS && SilentModeMenu == SILENT_MODE_AUTO) update_currents(); +#endif //MOTHERBOARD == BOARD_RAMBO_MINI_1_0 || MOTHERBOARD == BOARD_RAMBO_MINI_1_3 + } + else destination[i] = current_position[i]; //Are these else lines really needed? + } + if(code_seen('F')) { + next_feedrate = code_value(); +#ifdef MAX_SILENT_FEEDRATE + if (tmc2130_mode == TMC2130_MODE_SILENT) + if (next_feedrate > MAX_SILENT_FEEDRATE) next_feedrate = MAX_SILENT_FEEDRATE; +#endif //MAX_SILENT_FEEDRATE + if(next_feedrate > 0.0) feedrate = next_feedrate; + if (!seen[0] && !seen[1] && !seen[2] && seen[3]) + { +// float e_max_speed = +// printf_P(PSTR("E MOVE speed %7.3f\n"), feedrate / 60) + } + } +} + +void get_arc_coordinates() +{ +#ifdef SF_ARC_FIX + bool relative_mode_backup = relative_mode; + relative_mode = true; +#endif + get_coordinates(); +#ifdef SF_ARC_FIX + relative_mode=relative_mode_backup; +#endif + + if(code_seen('I')) { + offset[0] = code_value(); + } + else { + offset[0] = 0.0; + } + if(code_seen('J')) { + offset[1] = code_value(); + } + else { + offset[1] = 0.0; + } +} + +void clamp_to_software_endstops(float target[3]) +{ +#ifdef DEBUG_DISABLE_SWLIMITS + return; +#endif //DEBUG_DISABLE_SWLIMITS + world2machine_clamp(target[0], target[1]); + + // Clamp the Z coordinate. + if (min_software_endstops) { + float negative_z_offset = 0; + #ifdef ENABLE_AUTO_BED_LEVELING + if (Z_PROBE_OFFSET_FROM_EXTRUDER < 0) negative_z_offset = negative_z_offset + Z_PROBE_OFFSET_FROM_EXTRUDER; + if (cs.add_homing[Z_AXIS] < 0) negative_z_offset = negative_z_offset + cs.add_homing[Z_AXIS]; + #endif + if (target[Z_AXIS] < min_pos[Z_AXIS]+negative_z_offset) target[Z_AXIS] = min_pos[Z_AXIS]+negative_z_offset; + } + if (max_software_endstops) { + if (target[Z_AXIS] > max_pos[Z_AXIS]) target[Z_AXIS] = max_pos[Z_AXIS]; + } +} + +#ifdef MESH_BED_LEVELING + void mesh_plan_buffer_line(const float &x, const float &y, const float &z, const float &e, const float &feed_rate, const uint8_t extruder) { + float dx = x - current_position[X_AXIS]; + float dy = y - current_position[Y_AXIS]; + float dz = z - current_position[Z_AXIS]; + int n_segments = 0; + + if (mbl.active) { + float len = abs(dx) + abs(dy); + if (len > 0) + // Split to 3cm segments or shorter. + n_segments = int(ceil(len / 30.f)); + } + + if (n_segments > 1) { + float de = e - current_position[E_AXIS]; + for (int i = 1; i < n_segments; ++ i) { + float t = float(i) / float(n_segments); + if (saved_printing || (mbl.active == false)) return; + plan_buffer_line( + current_position[X_AXIS] + t * dx, + current_position[Y_AXIS] + t * dy, + current_position[Z_AXIS] + t * dz, + current_position[E_AXIS] + t * de, + feed_rate, extruder); + } + } + // The rest of the path. + plan_buffer_line(x, y, z, e, feed_rate, extruder); + current_position[X_AXIS] = x; + current_position[Y_AXIS] = y; + current_position[Z_AXIS] = z; + current_position[E_AXIS] = e; + } +#endif // MESH_BED_LEVELING + +void prepare_move() +{ + clamp_to_software_endstops(destination); + previous_millis_cmd = _millis(); + + // Do not use feedmultiply for E or Z only moves + if( (current_position[X_AXIS] == destination [X_AXIS]) && (current_position[Y_AXIS] == destination [Y_AXIS])) { + plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); + } + else { +#ifdef MESH_BED_LEVELING + mesh_plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply*(1./(60.f*100.f)), active_extruder); +#else + plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply*(1./(60.f*100.f)), active_extruder); +#endif + } + + for(int8_t i=0; i < NUM_AXIS; i++) { + current_position[i] = destination[i]; + } +} + +void prepare_arc_move(char isclockwise) { + float r = hypot(offset[X_AXIS], offset[Y_AXIS]); // Compute arc radius for mc_arc + + // Trace the arc + mc_arc(current_position, destination, offset, X_AXIS, Y_AXIS, Z_AXIS, feedrate*feedmultiply/60/100.0, r, isclockwise, active_extruder); + + // As far as the parser is concerned, the position is now == target. In reality the + // motion control system might still be processing the action and the real tool position + // in any intermediate location. + for(int8_t i=0; i < NUM_AXIS; i++) { + current_position[i] = destination[i]; + } + previous_millis_cmd = _millis(); +} + +#if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1 + +#if defined(FAN_PIN) + #if CONTROLLERFAN_PIN == FAN_PIN + #error "You cannot set CONTROLLERFAN_PIN equal to FAN_PIN" + #endif +#endif + +unsigned long lastMotor = 0; //Save the time for when a motor was turned on last +unsigned long lastMotorCheck = 0; + +void controllerFan() +{ + if ((_millis() - lastMotorCheck) >= 2500) //Not a time critical function, so we only check every 2500ms + { + lastMotorCheck = _millis(); + + if(!READ(X_ENABLE_PIN) || !READ(Y_ENABLE_PIN) || !READ(Z_ENABLE_PIN) || (soft_pwm_bed > 0) + #if EXTRUDERS > 2 + || !READ(E2_ENABLE_PIN) + #endif + #if EXTRUDER > 1 + #if defined(X2_ENABLE_PIN) && X2_ENABLE_PIN > -1 + || !READ(X2_ENABLE_PIN) + #endif + || !READ(E1_ENABLE_PIN) + #endif + || !READ(E0_ENABLE_PIN)) //If any of the drivers are enabled... + { + lastMotor = _millis(); //... set time to NOW so the fan will turn on + } + + if ((_millis() - lastMotor) >= (CONTROLLERFAN_SECS*1000UL) || lastMotor == 0) //If the last time any driver was enabled, is longer since than CONTROLLERSEC... + { + digitalWrite(CONTROLLERFAN_PIN, 0); + analogWrite(CONTROLLERFAN_PIN, 0); + } + else + { + // allows digital or PWM fan output to be used (see M42 handling) + digitalWrite(CONTROLLERFAN_PIN, CONTROLLERFAN_SPEED); + analogWrite(CONTROLLERFAN_PIN, CONTROLLERFAN_SPEED); + } + } +} +#endif + +#ifdef TEMP_STAT_LEDS +static bool blue_led = false; +static bool red_led = false; +static uint32_t stat_update = 0; + +void handle_status_leds(void) { + float max_temp = 0.0; + if(_millis() > stat_update) { + stat_update += 500; // Update every 0.5s + for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) { + max_temp = max(max_temp, degHotend(cur_extruder)); + max_temp = max(max_temp, degTargetHotend(cur_extruder)); + } + #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1 + max_temp = max(max_temp, degTargetBed()); + max_temp = max(max_temp, degBed()); + #endif + if((max_temp > 55.0) && (red_led == false)) { + digitalWrite(STAT_LED_RED, 1); + digitalWrite(STAT_LED_BLUE, 0); + red_led = true; + blue_led = false; + } + if((max_temp < 54.0) && (blue_led == false)) { + digitalWrite(STAT_LED_RED, 0); + digitalWrite(STAT_LED_BLUE, 1); + red_led = false; + blue_led = true; + } + } +} +#endif + +#ifdef SAFETYTIMER +/** + * @brief Turn off heating after safetytimer_inactive_time milliseconds of inactivity + * + * Full screen blocking notification message is shown after heater turning off. + * Paused print is not considered inactivity, as nozzle is cooled anyway and bed cooling would + * damage print. + * + * If safetytimer_inactive_time is zero, feature is disabled (heating is never turned off because of inactivity) + */ +static void handleSafetyTimer() +{ +#if (EXTRUDERS > 1) +#error Implemented only for one extruder. +#endif //(EXTRUDERS > 1) + if ((PRINTER_ACTIVE) || (!degTargetBed() && !degTargetHotend(0)) || (!safetytimer_inactive_time)) + { + safetyTimer.stop(); + } + else if ((degTargetBed() || degTargetHotend(0)) && (!safetyTimer.running())) + { + safetyTimer.start(); + } + else if (safetyTimer.expired(farm_mode?FARM_DEFAULT_SAFETYTIMER_TIME_ms:safetytimer_inactive_time)) + { + setTargetBed(0); + setAllTargetHotends(0); + lcd_show_fullscreen_message_and_wait_P(_i("Heating disabled by safety timer."));////MSG_BED_HEATING_SAFETY_DISABLED + } +} +#endif //SAFETYTIMER + +void manage_inactivity(bool ignore_stepper_queue/*=false*/) //default argument set in Marlin.h +{ +bool bInhibitFlag; +#ifdef FILAMENT_SENSOR + if (mmu_enabled == false) + { +//-// if (mcode_in_progress != 600) //M600 not in progress +#ifdef PAT9125 + bInhibitFlag=(menu_menu==lcd_menu_extruder_info); // Support::ExtruderInfo menu active +#endif // PAT9125 +#ifdef IR_SENSOR + bInhibitFlag=(menu_menu==lcd_menu_show_sensors_state); // Support::SensorInfo menu active +#endif // IR_SENSOR + if ((mcode_in_progress != 600) && (eFilamentAction != FilamentAction::AutoLoad) && (!bInhibitFlag)) //M600 not in progress, preHeat @ autoLoad menu not active, Support::ExtruderInfo/SensorInfo menu not active + { + if (!moves_planned() && !IS_SD_PRINTING && !is_usb_printing && (lcd_commands_type != LcdCommands::Layer1Cal) && ! eeprom_read_byte((uint8_t*)EEPROM_WIZARD_ACTIVE)) + { + if (fsensor_check_autoload()) + { +#ifdef PAT9125 + fsensor_autoload_check_stop(); +#endif //PAT9125 +//-// if (degHotend0() > EXTRUDE_MINTEMP) +if(0) + { + Sound_MakeCustom(50,1000,false); + loading_flag = true; + enquecommand_front_P((PSTR("M701"))); + } + else + { +/* + lcd_update_enable(false); + show_preheat_nozzle_warning(); + lcd_update_enable(true); +*/ + eFilamentAction=FilamentAction::AutoLoad; + bFilamentFirstRun=false; + if(target_temperature[0]>=EXTRUDE_MINTEMP) + { + bFilamentPreheatState=true; +// mFilamentItem(target_temperature[0],target_temperature_bed); + menu_submenu(mFilamentItemForce); + } + else + { + menu_submenu(lcd_generic_preheat_menu); + lcd_timeoutToStatus.start(); + } + } + } + } + else + { +#ifdef PAT9125 + fsensor_autoload_check_stop(); +#endif //PAT9125 + fsensor_update(); + } + } + } +#endif //FILAMENT_SENSOR + +#ifdef SAFETYTIMER + handleSafetyTimer(); +#endif //SAFETYTIMER + +#if defined(KILL_PIN) && KILL_PIN > -1 + static int killCount = 0; // make the inactivity button a bit less responsive + const int KILL_DELAY = 10000; +#endif + + if(buflen < (BUFSIZE-1)){ + get_command(); + } + + if( (_millis() - previous_millis_cmd) > max_inactive_time ) + if(max_inactive_time) + kill(_n(""), 4); + if(stepper_inactive_time) { + if( (_millis() - previous_millis_cmd) > stepper_inactive_time ) + { + if(blocks_queued() == false && ignore_stepper_queue == false) { + disable_x(); + disable_y(); + disable_z(); + disable_e0(); + disable_e1(); + disable_e2(); + } + } + } + + #ifdef CHDK //Check if pin should be set to LOW after M240 set it to HIGH + if (chdkActive && (_millis() - chdkHigh > CHDK_DELAY)) + { + chdkActive = false; + WRITE(CHDK, LOW); + } + #endif + + #if defined(KILL_PIN) && KILL_PIN > -1 + + // Check if the kill button was pressed and wait just in case it was an accidental + // key kill key press + // ------------------------------------------------------------------------------- + if( 0 == READ(KILL_PIN) ) + { + killCount++; + } + else if (killCount > 0) + { + killCount--; + } + // Exceeded threshold and we can confirm that it was not accidental + // KILL the machine + // ---------------------------------------------------------------- + if ( killCount >= KILL_DELAY) + { + kill("", 5); + } + #endif + + #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1 + controllerFan(); //Check if fan should be turned on to cool stepper drivers down + #endif + #ifdef EXTRUDER_RUNOUT_PREVENT + if( (_millis() - previous_millis_cmd) > EXTRUDER_RUNOUT_SECONDS*1000 ) + if(degHotend(active_extruder)>EXTRUDER_RUNOUT_MINTEMP) + { + bool oldstatus=READ(E0_ENABLE_PIN); + enable_e0(); + float oldepos=current_position[E_AXIS]; + float oldedes=destination[E_AXIS]; + plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], + destination[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/cs.axis_steps_per_unit[E_AXIS], + EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/cs.axis_steps_per_unit[E_AXIS], active_extruder); + current_position[E_AXIS]=oldepos; + destination[E_AXIS]=oldedes; + plan_set_e_position(oldepos); + previous_millis_cmd=_millis(); + st_synchronize(); + WRITE(E0_ENABLE_PIN,oldstatus); + } + #endif + #ifdef TEMP_STAT_LEDS + handle_status_leds(); + #endif + check_axes_activity(); + mmu_loop(); +} + +void kill(const char *full_screen_message, unsigned char id) +{ + printf_P(_N("KILL: %d\n"), id); + //return; + cli(); // Stop interrupts + disable_heater(); + + disable_x(); +// SERIAL_ECHOLNPGM("kill - disable Y"); + disable_y(); + disable_z(); + disable_e0(); + disable_e1(); + disable_e2(); + +#if defined(PS_ON_PIN) && PS_ON_PIN > -1 + pinMode(PS_ON_PIN,INPUT); +#endif + SERIAL_ERROR_START; + SERIAL_ERRORLNRPGM(_n("Printer halted. kill() called!"));////MSG_ERR_KILLED + if (full_screen_message != NULL) { + SERIAL_ERRORLNRPGM(full_screen_message); + lcd_display_message_fullscreen_P(full_screen_message); + } else { + LCD_ALERTMESSAGERPGM(_n("KILLED. "));////MSG_KILLED + } + + // FMC small patch to update the LCD before ending + sei(); // enable interrupts + for ( int i=5; i--; lcd_update(0)) + { + _delay(200); + } + cli(); // disable interrupts + suicide(); + while(1) + { +#ifdef WATCHDOG + wdt_reset(); +#endif //WATCHDOG + /* Intentionally left empty */ + + } // Wait for reset +} + +void Stop() +{ + disable_heater(); + if(Stopped == false) { + Stopped = true; + lcd_print_stop(); + Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart + SERIAL_ERROR_START; + SERIAL_ERRORLNRPGM(MSG_ERR_STOPPED); + LCD_MESSAGERPGM(_T(MSG_STOPPED)); + } +} + +bool IsStopped() { return Stopped; }; + +#ifdef FAST_PWM_FAN +void setPwmFrequency(uint8_t pin, int val) +{ + val &= 0x07; + switch(digitalPinToTimer(pin)) + { + + #if defined(TCCR0A) + case TIMER0A: + case TIMER0B: +// TCCR0B &= ~(_BV(CS00) | _BV(CS01) | _BV(CS02)); +// TCCR0B |= val; + break; + #endif + + #if defined(TCCR1A) + case TIMER1A: + case TIMER1B: +// TCCR1B &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12)); +// TCCR1B |= val; + break; + #endif + + #if defined(TCCR2) + case TIMER2: + case TIMER2: + TCCR2 &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12)); + TCCR2 |= val; + break; + #endif + + #if defined(TCCR2A) + case TIMER2A: + case TIMER2B: + TCCR2B &= ~(_BV(CS20) | _BV(CS21) | _BV(CS22)); + TCCR2B |= val; + break; + #endif + + #if defined(TCCR3A) + case TIMER3A: + case TIMER3B: + case TIMER3C: + TCCR3B &= ~(_BV(CS30) | _BV(CS31) | _BV(CS32)); + TCCR3B |= val; + break; + #endif + + #if defined(TCCR4A) + case TIMER4A: + case TIMER4B: + case TIMER4C: + TCCR4B &= ~(_BV(CS40) | _BV(CS41) | _BV(CS42)); + TCCR4B |= val; + break; + #endif + + #if defined(TCCR5A) + case TIMER5A: + case TIMER5B: + case TIMER5C: + TCCR5B &= ~(_BV(CS50) | _BV(CS51) | _BV(CS52)); + TCCR5B |= val; + break; + #endif + + } +} +#endif //FAST_PWM_FAN + +//! @brief Get and validate extruder number +//! +//! If it is not specified, active_extruder is returned in parameter extruder. +//! @param [in] code M code number +//! @param [out] extruder +//! @return error +//! @retval true Invalid extruder specified in T code +//! @retval false Valid extruder specified in T code, or not specifiead + +bool setTargetedHotend(int code, uint8_t &extruder) +{ + extruder = active_extruder; + if(code_seen('T')) { + extruder = code_value(); + if(extruder >= EXTRUDERS) { + SERIAL_ECHO_START; + switch(code){ + case 104: + SERIAL_ECHORPGM(_n("M104 Invalid extruder "));////MSG_M104_INVALID_EXTRUDER + break; + case 105: + SERIAL_ECHO(_n("M105 Invalid extruder "));////MSG_M105_INVALID_EXTRUDER + break; + case 109: + SERIAL_ECHO(_n("M109 Invalid extruder "));////MSG_M109_INVALID_EXTRUDER + break; + case 218: + SERIAL_ECHO(_n("M218 Invalid extruder "));////MSG_M218_INVALID_EXTRUDER + break; + case 221: + SERIAL_ECHO(_n("M221 Invalid extruder "));////MSG_M221_INVALID_EXTRUDER + break; + } + SERIAL_PROTOCOLLN((int)extruder); + return true; + } + } + return false; +} + +void save_statistics(unsigned long _total_filament_used, unsigned long _total_print_time) //_total_filament_used unit: mm/100; print time in s +{ + if (eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME) == 255 && eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME + 1) == 255 && eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME + 2) == 255 && eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME + 3) == 255) + { + eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, 0); + eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, 0); + } + + unsigned long _previous_filament = eeprom_read_dword((uint32_t *)EEPROM_FILAMENTUSED); //_previous_filament unit: cm + unsigned long _previous_time = eeprom_read_dword((uint32_t *)EEPROM_TOTALTIME); //_previous_time unit: min + + eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, _previous_time + (_total_print_time/60)); //EEPROM_TOTALTIME unit: min + eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, _previous_filament + (_total_filament_used / 1000)); + + total_filament_used = 0; + +} + +float calculate_extruder_multiplier(float diameter) { + float out = 1.f; + if (cs.volumetric_enabled && diameter > 0.f) { + float area = M_PI * diameter * diameter * 0.25; + out = 1.f / area; + } + if (extrudemultiply != 100) + out *= float(extrudemultiply) * 0.01f; + return out; +} + +void calculate_extruder_multipliers() { + extruder_multiplier[0] = calculate_extruder_multiplier(cs.filament_size[0]); +#if EXTRUDERS > 1 + extruder_multiplier[1] = calculate_extruder_multiplier(cs.filament_size[1]); +#if EXTRUDERS > 2 + extruder_multiplier[2] = calculate_extruder_multiplier(cs.filament_size[2]); +#endif +#endif +} + +void delay_keep_alive(unsigned int ms) +{ + for (;;) { + manage_heater(); + // Manage inactivity, but don't disable steppers on timeout. + manage_inactivity(true); + lcd_update(0); + if (ms == 0) + break; + else if (ms >= 50) { + _delay(50); + ms -= 50; + } else { + _delay(ms); + ms = 0; + } + } +} + +static void wait_for_heater(long codenum, uint8_t extruder) { + +#ifdef TEMP_RESIDENCY_TIME + long residencyStart; + residencyStart = -1; + /* continue to loop until we have reached the target temp + _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */ + while ((!cancel_heatup) && ((residencyStart == -1) || + (residencyStart >= 0 && (((unsigned int)(_millis() - residencyStart)) < (TEMP_RESIDENCY_TIME * 1000UL))))) { +#else + while (target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder) && (CooldownNoWait == false))) { +#endif //TEMP_RESIDENCY_TIME + if ((_millis() - codenum) > 1000UL) + { //Print Temp Reading and remaining time every 1 second while heating up/cooling down + if (!farm_mode) { + SERIAL_PROTOCOLPGM("T:"); + SERIAL_PROTOCOL_F(degHotend(extruder), 1); + SERIAL_PROTOCOLPGM(" E:"); + SERIAL_PROTOCOL((int)extruder); + +#ifdef TEMP_RESIDENCY_TIME + SERIAL_PROTOCOLPGM(" W:"); + if (residencyStart > -1) + { + codenum = ((TEMP_RESIDENCY_TIME * 1000UL) - (_millis() - residencyStart)) / 1000UL; + SERIAL_PROTOCOLLN(codenum); + } + else + { + SERIAL_PROTOCOLLN("?"); + } + } +#else + SERIAL_PROTOCOLLN(""); +#endif + codenum = _millis(); + } + manage_heater(); + manage_inactivity(true); //do not disable steppers + lcd_update(0); +#ifdef TEMP_RESIDENCY_TIME + /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time + or when current temp falls outside the hysteresis after target temp was reached */ + if ((residencyStart == -1 && target_direction && (degHotend(extruder) >= (degTargetHotend(extruder) - TEMP_WINDOW))) || + (residencyStart == -1 && !target_direction && (degHotend(extruder) <= (degTargetHotend(extruder) + TEMP_WINDOW))) || + (residencyStart > -1 && labs(degHotend(extruder) - degTargetHotend(extruder)) > TEMP_HYSTERESIS)) + { + residencyStart = _millis(); + } +#endif //TEMP_RESIDENCY_TIME + } +} + +void check_babystep() +{ + int babystep_z = eeprom_read_word(reinterpret_cast(&(EEPROM_Sheets_base-> + s[(eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)))].z_offset))); + + if ((babystep_z < Z_BABYSTEP_MIN) || (babystep_z > Z_BABYSTEP_MAX)) { + babystep_z = 0; //if babystep value is out of min max range, set it to 0 + SERIAL_ECHOLNPGM("Z live adjust out of range. Setting to 0"); + eeprom_write_word(reinterpret_cast(&(EEPROM_Sheets_base-> + s[(eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)))].z_offset)), + babystep_z); + lcd_show_fullscreen_message_and_wait_P(PSTR("Z live adjust out of range. Setting to 0. Click to continue.")); + lcd_update_enable(true); + } +} +#ifdef HEATBED_ANALYSIS +void d_setup() +{ + pinMode(D_DATACLOCK, INPUT_PULLUP); + pinMode(D_DATA, INPUT_PULLUP); + pinMode(D_REQUIRE, OUTPUT); + digitalWrite(D_REQUIRE, HIGH); +} + + +float d_ReadData() +{ + int digit[13]; + String mergeOutput; + float output; + + digitalWrite(D_REQUIRE, HIGH); + for (int i = 0; i<13; i++) + { + for (int j = 0; j < 4; j++) + { + while (digitalRead(D_DATACLOCK) == LOW) {} + while (digitalRead(D_DATACLOCK) == HIGH) {} + bitWrite(digit[i], j, digitalRead(D_DATA)); + } + } + + digitalWrite(D_REQUIRE, LOW); + mergeOutput = ""; + output = 0; + for (int r = 5; r <= 10; r++) //Merge digits + { + mergeOutput += digit[r]; + } + output = mergeOutput.toFloat(); + + if (digit[4] == 8) //Handle sign + { + output *= -1; + } + + for (int i = digit[11]; i > 0; i--) //Handle floating point + { + output /= 10; + } + + return output; + +} + +void bed_check(float x_dimension, float y_dimension, int x_points_num, int y_points_num, float shift_x, float shift_y) { + int t1 = 0; + int t_delay = 0; + int digit[13]; + int m; + char str[3]; + //String mergeOutput; + char mergeOutput[15]; + float output; + + int mesh_point = 0; //index number of calibration point + float bed_zero_ref_x = (-22.f + X_PROBE_OFFSET_FROM_EXTRUDER); //shift between zero point on bed and target and between probe and nozzle + float bed_zero_ref_y = (-0.6f + Y_PROBE_OFFSET_FROM_EXTRUDER); + + float mesh_home_z_search = 4; + float measure_z_height = 0.2f; + float row[x_points_num]; + int ix = 0; + int iy = 0; + + const char* filename_wldsd = "mesh.txt"; + char data_wldsd[x_points_num * 7 + 1]; //6 chars(" -A.BCD")for each measurement + null + char numb_wldsd[8]; // (" -A.BCD" + null) +#ifdef MICROMETER_LOGGING + d_setup(); +#endif //MICROMETER_LOGGING + + int XY_AXIS_FEEDRATE = homing_feedrate[X_AXIS] / 20; + int Z_LIFT_FEEDRATE = homing_feedrate[Z_AXIS] / 40; + + unsigned int custom_message_type_old = custom_message_type; + unsigned int custom_message_state_old = custom_message_state; + custom_message_type = CustomMsg::MeshBedLeveling; + custom_message_state = (x_points_num * y_points_num) + 10; + lcd_update(1); + + //mbl.reset(); + babystep_undo(); + + card.openFile(filename_wldsd, false); + + /*destination[Z_AXIS] = mesh_home_z_search; + //plan_buffer_line_curposXYZE(Z_LIFT_FEEDRATE, active_extruder); + + plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], Z_LIFT_FEEDRATE, active_extruder); + for(int8_t i=0; i < NUM_AXIS; i++) { + current_position[i] = destination[i]; + } + st_synchronize(); + */ + destination[Z_AXIS] = measure_z_height; + plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], Z_LIFT_FEEDRATE, active_extruder); + for(int8_t i=0; i < NUM_AXIS; i++) { + current_position[i] = destination[i]; + } + st_synchronize(); + /*int l_feedmultiply = */setup_for_endstop_move(false); + + SERIAL_PROTOCOLPGM("Num X,Y: "); + SERIAL_PROTOCOL(x_points_num); + SERIAL_PROTOCOLPGM(","); + SERIAL_PROTOCOL(y_points_num); + SERIAL_PROTOCOLPGM("\nZ search height: "); + SERIAL_PROTOCOL(mesh_home_z_search); + SERIAL_PROTOCOLPGM("\nDimension X,Y: "); + SERIAL_PROTOCOL(x_dimension); + SERIAL_PROTOCOLPGM(","); + SERIAL_PROTOCOL(y_dimension); + SERIAL_PROTOCOLLNPGM("\nMeasured points:"); + + while (mesh_point != x_points_num * y_points_num) { + ix = mesh_point % x_points_num; // from 0 to MESH_NUM_X_POINTS - 1 + iy = mesh_point / x_points_num; + if (iy & 1) ix = (x_points_num - 1) - ix; // Zig zag + float z0 = 0.f; + /*destination[Z_AXIS] = mesh_home_z_search; + //plan_buffer_line_curposXYZE(Z_LIFT_FEEDRATE, active_extruder); + + plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], Z_LIFT_FEEDRATE, active_extruder); + for(int8_t i=0; i < NUM_AXIS; i++) { + current_position[i] = destination[i]; + } + st_synchronize();*/ + + + //current_position[X_AXIS] = 13.f + ix * (x_dimension / (x_points_num - 1)) - bed_zero_ref_x + shift_x; + //current_position[Y_AXIS] = 6.4f + iy * (y_dimension / (y_points_num - 1)) - bed_zero_ref_y + shift_y; + + destination[X_AXIS] = ix * (x_dimension / (x_points_num - 1)) + shift_x; + destination[Y_AXIS] = iy * (y_dimension / (y_points_num - 1)) + shift_y; + + mesh_plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], XY_AXIS_FEEDRATE/6, active_extruder); + for(int8_t i=0; i < NUM_AXIS; i++) { + current_position[i] = destination[i]; + } + st_synchronize(); + + // printf_P(PSTR("X = %f; Y= %f \n"), current_position[X_AXIS], current_position[Y_AXIS]); + + delay_keep_alive(1000); +#ifdef MICROMETER_LOGGING + + //memset(numb_wldsd, 0, sizeof(numb_wldsd)); + //dtostrf(d_ReadData(), 8, 5, numb_wldsd); + //strcat(data_wldsd, numb_wldsd); + + + + //MYSERIAL.println(data_wldsd); + //delay(1000); + //delay(3000); + //t1 = millis(); + + //while (digitalRead(D_DATACLOCK) == LOW) {} + //while (digitalRead(D_DATACLOCK) == HIGH) {} + memset(digit, 0, sizeof(digit)); + //cli(); + digitalWrite(D_REQUIRE, LOW); + + for (int i = 0; i<13; i++) + { + //t1 = millis(); + for (int j = 0; j < 4; j++) + { + while (digitalRead(D_DATACLOCK) == LOW) {} + while (digitalRead(D_DATACLOCK) == HIGH) {} + //printf_P(PSTR("Done %d\n"), j); + bitWrite(digit[i], j, digitalRead(D_DATA)); + } + //t_delay = (millis() - t1); + //SERIAL_PROTOCOLPGM(" "); + //SERIAL_PROTOCOL_F(t_delay, 5); + //SERIAL_PROTOCOLPGM(" "); + + } + //sei(); + digitalWrite(D_REQUIRE, HIGH); + mergeOutput[0] = '\0'; + output = 0; + for (int r = 5; r <= 10; r++) //Merge digits + { + sprintf(str, "%d", digit[r]); + strcat(mergeOutput, str); + } + + output = atof(mergeOutput); + + if (digit[4] == 8) //Handle sign + { + output *= -1; + } + + for (int i = digit[11]; i > 0; i--) //Handle floating point + { + output *= 0.1; + } + + + //output = d_ReadData(); + + //row[ix] = current_position[Z_AXIS]; + + + + //row[ix] = d_ReadData(); + + row[ix] = output; + + if (iy % 2 == 1 ? ix == 0 : ix == x_points_num - 1) { + memset(data_wldsd, 0, sizeof(data_wldsd)); + for (int i = 0; i < x_points_num; i++) { + SERIAL_PROTOCOLPGM(" "); + SERIAL_PROTOCOL_F(row[i], 5); + memset(numb_wldsd, 0, sizeof(numb_wldsd)); + dtostrf(row[i], 7, 3, numb_wldsd); + strcat(data_wldsd, numb_wldsd); + } + card.write_command(data_wldsd); + SERIAL_PROTOCOLPGM("\n"); + + } + + custom_message_state--; + mesh_point++; + lcd_update(1); + + } + #endif //MICROMETER_LOGGING + card.closefile(); + //clean_up_after_endstop_move(l_feedmultiply); + +} + +void bed_analysis(float x_dimension, float y_dimension, int x_points_num, int y_points_num, float shift_x, float shift_y) { + int t1 = 0; + int t_delay = 0; + int digit[13]; + int m; + char str[3]; + //String mergeOutput; + char mergeOutput[15]; + float output; + + int mesh_point = 0; //index number of calibration point + float bed_zero_ref_x = (-22.f + X_PROBE_OFFSET_FROM_EXTRUDER); //shift between zero point on bed and target and between probe and nozzle + float bed_zero_ref_y = (-0.6f + Y_PROBE_OFFSET_FROM_EXTRUDER); + + float mesh_home_z_search = 4; + float row[x_points_num]; + int ix = 0; + int iy = 0; + + const char* filename_wldsd = "wldsd.txt"; + char data_wldsd[70]; + char numb_wldsd[10]; + + d_setup(); + + if (!(axis_known_position[X_AXIS] && axis_known_position[Y_AXIS] && axis_known_position[Z_AXIS])) { + // We don't know where we are! HOME! + // Push the commands to the front of the message queue in the reverse order! + // There shall be always enough space reserved for these commands. + repeatcommand_front(); // repeat G80 with all its parameters + + enquecommand_front_P((PSTR("G28 W0"))); + enquecommand_front_P((PSTR("G1 Z5"))); + return; + } + unsigned int custom_message_type_old = custom_message_type; + unsigned int custom_message_state_old = custom_message_state; + custom_message_type = CustomMsg::MeshBedLeveling; + custom_message_state = (x_points_num * y_points_num) + 10; + lcd_update(1); + + mbl.reset(); + babystep_undo(); + + card.openFile(filename_wldsd, false); + + current_position[Z_AXIS] = mesh_home_z_search; + plan_buffer_line_curposXYZE(homing_feedrate[Z_AXIS] / 60, active_extruder); + + int XY_AXIS_FEEDRATE = homing_feedrate[X_AXIS] / 20; + int Z_LIFT_FEEDRATE = homing_feedrate[Z_AXIS] / 40; + + int l_feedmultiply = setup_for_endstop_move(false); + + SERIAL_PROTOCOLPGM("Num X,Y: "); + SERIAL_PROTOCOL(x_points_num); + SERIAL_PROTOCOLPGM(","); + SERIAL_PROTOCOL(y_points_num); + SERIAL_PROTOCOLPGM("\nZ search height: "); + SERIAL_PROTOCOL(mesh_home_z_search); + SERIAL_PROTOCOLPGM("\nDimension X,Y: "); + SERIAL_PROTOCOL(x_dimension); + SERIAL_PROTOCOLPGM(","); + SERIAL_PROTOCOL(y_dimension); + SERIAL_PROTOCOLLNPGM("\nMeasured points:"); + + while (mesh_point != x_points_num * y_points_num) { + ix = mesh_point % x_points_num; // from 0 to MESH_NUM_X_POINTS - 1 + iy = mesh_point / x_points_num; + if (iy & 1) ix = (x_points_num - 1) - ix; // Zig zag + float z0 = 0.f; + current_position[Z_AXIS] = mesh_home_z_search; + plan_buffer_line_curposXYZE(Z_LIFT_FEEDRATE, active_extruder); + st_synchronize(); + + + current_position[X_AXIS] = 13.f + ix * (x_dimension / (x_points_num - 1)) - bed_zero_ref_x + shift_x; + current_position[Y_AXIS] = 6.4f + iy * (y_dimension / (y_points_num - 1)) - bed_zero_ref_y + shift_y; + + plan_buffer_line_curposXYZE(XY_AXIS_FEEDRATE, active_extruder); + st_synchronize(); + + if (!find_bed_induction_sensor_point_z(-10.f)) { //if we have data from z calibration max allowed difference is 1mm for each point, if we dont have data max difference is 10mm from initial point + break; + card.closefile(); + } + + + //memset(numb_wldsd, 0, sizeof(numb_wldsd)); + //dtostrf(d_ReadData(), 8, 5, numb_wldsd); + //strcat(data_wldsd, numb_wldsd); + + + + //MYSERIAL.println(data_wldsd); + //_delay(1000); + //_delay(3000); + //t1 = _millis(); + + //while (digitalRead(D_DATACLOCK) == LOW) {} + //while (digitalRead(D_DATACLOCK) == HIGH) {} + memset(digit, 0, sizeof(digit)); + //cli(); + digitalWrite(D_REQUIRE, LOW); + + for (int i = 0; i<13; i++) + { + //t1 = _millis(); + for (int j = 0; j < 4; j++) + { + while (digitalRead(D_DATACLOCK) == LOW) {} + while (digitalRead(D_DATACLOCK) == HIGH) {} + bitWrite(digit[i], j, digitalRead(D_DATA)); + } + //t_delay = (_millis() - t1); + //SERIAL_PROTOCOLPGM(" "); + //SERIAL_PROTOCOL_F(t_delay, 5); + //SERIAL_PROTOCOLPGM(" "); + } + //sei(); + digitalWrite(D_REQUIRE, HIGH); + mergeOutput[0] = '\0'; + output = 0; + for (int r = 5; r <= 10; r++) //Merge digits + { + sprintf(str, "%d", digit[r]); + strcat(mergeOutput, str); + } + + output = atof(mergeOutput); + + if (digit[4] == 8) //Handle sign + { + output *= -1; + } + + for (int i = digit[11]; i > 0; i--) //Handle floating point + { + output *= 0.1; + } + + + //output = d_ReadData(); + + //row[ix] = current_position[Z_AXIS]; + + memset(data_wldsd, 0, sizeof(data_wldsd)); + + for (int i = 0; i <3; i++) { + memset(numb_wldsd, 0, sizeof(numb_wldsd)); + dtostrf(current_position[i], 8, 5, numb_wldsd); + strcat(data_wldsd, numb_wldsd); + strcat(data_wldsd, ";"); + + } + memset(numb_wldsd, 0, sizeof(numb_wldsd)); + dtostrf(output, 8, 5, numb_wldsd); + strcat(data_wldsd, numb_wldsd); + //strcat(data_wldsd, ";"); + card.write_command(data_wldsd); + + + //row[ix] = d_ReadData(); + + row[ix] = output; // current_position[Z_AXIS]; + + if (iy % 2 == 1 ? ix == 0 : ix == x_points_num - 1) { + for (int i = 0; i < x_points_num; i++) { + SERIAL_PROTOCOLPGM(" "); + SERIAL_PROTOCOL_F(row[i], 5); + + + } + SERIAL_PROTOCOLPGM("\n"); + } + custom_message_state--; + mesh_point++; + lcd_update(1); + + } + card.closefile(); + clean_up_after_endstop_move(l_feedmultiply); +} +#endif //HEATBED_ANALYSIS + +#ifndef PINDA_THERMISTOR +static void temp_compensation_start() { + + custom_message_type = CustomMsg::TempCompPreheat; + custom_message_state = PINDA_HEAT_T + 1; + lcd_update(2); + if (degHotend(active_extruder) > EXTRUDE_MINTEMP) { + current_position[E_AXIS] -= default_retraction; + } + plan_buffer_line_curposXYZE(400, active_extruder); + + current_position[X_AXIS] = PINDA_PREHEAT_X; + current_position[Y_AXIS] = PINDA_PREHEAT_Y; + current_position[Z_AXIS] = PINDA_PREHEAT_Z; + plan_buffer_line_curposXYZE(3000 / 60, active_extruder); + st_synchronize(); + while (fabs(degBed() - target_temperature_bed) > 1) delay_keep_alive(1000); + + for (int i = 0; i < PINDA_HEAT_T; i++) { + delay_keep_alive(1000); + custom_message_state = PINDA_HEAT_T - i; + if (custom_message_state == 99 || custom_message_state == 9) lcd_update(2); //force whole display redraw if number of digits changed + else lcd_update(1); + } + custom_message_type = CustomMsg::Status; + custom_message_state = 0; +} + +static void temp_compensation_apply() { + int i_add; + int z_shift = 0; + float z_shift_mm; + + if (calibration_status() == CALIBRATION_STATUS_CALIBRATED) { + if (target_temperature_bed % 10 == 0 && target_temperature_bed >= 60 && target_temperature_bed <= 100) { + i_add = (target_temperature_bed - 60) / 10; + EEPROM_read_B(EEPROM_PROBE_TEMP_SHIFT + i_add * 2, &z_shift); + z_shift_mm = z_shift / cs.axis_steps_per_unit[Z_AXIS]; + }else { + //interpolation + z_shift_mm = temp_comp_interpolation(target_temperature_bed) / cs.axis_steps_per_unit[Z_AXIS]; + } + printf_P(_N("\nZ shift applied:%.3f\n"), z_shift_mm); + plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS] - z_shift_mm, current_position[E_AXIS], homing_feedrate[Z_AXIS] / 40, active_extruder); + st_synchronize(); + plan_set_z_position(current_position[Z_AXIS]); + } + else { + //we have no temp compensation data + } +} +#endif //ndef PINDA_THERMISTOR + +float temp_comp_interpolation(float inp_temperature) { + + //cubic spline interpolation + + int n, i, j; + float h[10], a, b, c, d, sum, s[10] = { 0 }, x[10], F[10], f[10], m[10][10] = { 0 }, temp; + int shift[10]; + int temp_C[10]; + + n = 6; //number of measured points + + shift[0] = 0; + for (i = 0; i < n; i++) { + if (i>0) EEPROM_read_B(EEPROM_PROBE_TEMP_SHIFT + (i-1) * 2, &shift[i]); //read shift in steps from EEPROM + temp_C[i] = 50 + i * 10; //temperature in C +#ifdef PINDA_THERMISTOR + temp_C[i] = 35 + i * 5; //temperature in C +#else + temp_C[i] = 50 + i * 10; //temperature in C +#endif + x[i] = (float)temp_C[i]; + f[i] = (float)shift[i]; + } + if (inp_temperature < x[0]) return 0; + + + for (i = n - 1; i>0; i--) { + F[i] = (f[i] - f[i - 1]) / (x[i] - x[i - 1]); + h[i - 1] = x[i] - x[i - 1]; + } + //*********** formation of h, s , f matrix ************** + for (i = 1; i0; i--) { + sum = 0; + for (j = i; j <= n - 2; j++) + sum += m[i][j] * s[j]; + s[i] = (m[i][n - 1] - sum) / m[i][i]; + } + + for (i = 0; i x[i + 1])) { + a = (s[i + 1] - s[i]) / (6 * h[i]); + b = s[i] / 2; + c = (f[i + 1] - f[i]) / h[i] - (2 * h[i] * s[i] + s[i + 1] * h[i]) / 6; + d = f[i]; + sum = a*pow((inp_temperature - x[i]), 3) + b*pow((inp_temperature - x[i]), 2) + c*(inp_temperature - x[i]) + d; + } + + return sum; + +} + +#ifdef PINDA_THERMISTOR +float temp_compensation_pinda_thermistor_offset(float temperature_pinda) +{ + if (!temp_cal_active) return 0; + if (!calibration_status_pinda()) return 0; + return temp_comp_interpolation(temperature_pinda) / cs.axis_steps_per_unit[Z_AXIS]; +} +#endif //PINDA_THERMISTOR + +void long_pause() //long pause print +{ + st_synchronize(); + + start_pause_print = _millis(); + + //retract + current_position[E_AXIS] -= default_retraction; + plan_buffer_line_curposXYZE(400, active_extruder); + + //lift z + current_position[Z_AXIS] += Z_PAUSE_LIFT; + if (current_position[Z_AXIS] > Z_MAX_POS) current_position[Z_AXIS] = Z_MAX_POS; + plan_buffer_line_curposXYZE(15, active_extruder); + + //Move XY to side + current_position[X_AXIS] = X_PAUSE_POS; + current_position[Y_AXIS] = Y_PAUSE_POS; + plan_buffer_line_curposXYZE(50, active_extruder); + + // Turn off the print fan + fanSpeed = 0; + + st_synchronize(); +} + +void serialecho_temperatures() { + float tt = degHotend(active_extruder); + SERIAL_PROTOCOLPGM("T:"); + SERIAL_PROTOCOL(tt); + SERIAL_PROTOCOLPGM(" E:"); + SERIAL_PROTOCOL((int)active_extruder); + SERIAL_PROTOCOLPGM(" B:"); + SERIAL_PROTOCOL_F(degBed(), 1); + SERIAL_PROTOCOLLN(""); +} + +#ifdef UVLO_SUPPORT + +void uvlo_() +{ + unsigned long time_start = _millis(); + bool sd_print = card.sdprinting; + // Conserve power as soon as possible. + disable_x(); + disable_y(); + +#ifdef TMC2130 + tmc2130_set_current_h(Z_AXIS, 20); + tmc2130_set_current_r(Z_AXIS, 20); + tmc2130_set_current_h(E_AXIS, 20); + tmc2130_set_current_r(E_AXIS, 20); +#endif //TMC2130 + + + // Indicate that the interrupt has been triggered. + // SERIAL_ECHOLNPGM("UVLO"); + + // Read out the current Z motor microstep counter. This will be later used + // for reaching the zero full step before powering off. + uint16_t z_microsteps = 0; +#ifdef TMC2130 + z_microsteps = tmc2130_rd_MSCNT(Z_TMC2130_CS); +#endif //TMC2130 + + // Calculate the file position, from which to resume this print. + long sd_position = sdpos_atomic; //atomic sd position of last command added in queue + { + uint16_t sdlen_planner = planner_calc_sd_length(); //length of sd commands in planner + sd_position -= sdlen_planner; + uint16_t sdlen_cmdqueue = cmdqueue_calc_sd_length(); //length of sd commands in cmdqueue + sd_position -= sdlen_cmdqueue; + if (sd_position < 0) sd_position = 0; + } + + // Backup the feedrate in mm/min. + int feedrate_bckp = blocks_queued() ? (block_buffer[block_buffer_tail].nominal_speed * 60.f) : feedrate; + + // After this call, the planner queue is emptied and the current_position is set to a current logical coordinate. + // The logical coordinate will likely differ from the machine coordinate if the skew calibration and mesh bed leveling + // are in action. + planner_abort_hard(); + + // Store the current extruder position. + eeprom_update_float((float*)(EEPROM_UVLO_CURRENT_POSITION_E), st_get_position_mm(E_AXIS)); + eeprom_update_byte((uint8_t*)EEPROM_UVLO_E_ABS, axis_relative_modes[3]?0:1); + // Clean the input command queue. + cmdqueue_reset(); + card.sdprinting = false; +// card.closefile(); + // Enable stepper driver interrupt to move Z axis. + // This should be fine as the planner and command queues are empty and the SD card printing is disabled. + //FIXME one may want to disable serial lines at this point of time to avoid interfering with the command queue, + // though it should not happen that the command queue is touched as the plan_buffer_line always succeed without blocking. + sei(); + plan_buffer_line( + current_position[X_AXIS], + current_position[Y_AXIS], + current_position[Z_AXIS], + current_position[E_AXIS] - default_retraction, + 95, active_extruder); + + st_synchronize(); + disable_e0(); + + plan_buffer_line( + current_position[X_AXIS], + current_position[Y_AXIS], + current_position[Z_AXIS] + UVLO_Z_AXIS_SHIFT + float((1024 - z_microsteps + 7) >> 4) / cs.axis_steps_per_unit[Z_AXIS], + current_position[E_AXIS] - default_retraction, + 40, active_extruder); + st_synchronize(); + disable_e0(); + + plan_buffer_line( + current_position[X_AXIS], + current_position[Y_AXIS], + current_position[Z_AXIS] + UVLO_Z_AXIS_SHIFT + float((1024 - z_microsteps + 7) >> 4) / cs.axis_steps_per_unit[Z_AXIS], + current_position[E_AXIS] - default_retraction, + 40, active_extruder); + st_synchronize(); + + disable_e0(); + // Move Z up to the next 0th full step. + // Write the file position. + eeprom_update_dword((uint32_t*)(EEPROM_FILE_POSITION), sd_position); + // Store the mesh bed leveling offsets. This is 2*7*7=98 bytes, which takes 98*3.4us=333us in worst case. + for (int8_t mesh_point = 0; mesh_point < MESH_NUM_X_POINTS * MESH_NUM_Y_POINTS; ++ mesh_point) { + uint8_t ix = mesh_point % MESH_NUM_X_POINTS; // from 0 to MESH_NUM_X_POINTS - 1 + uint8_t iy = mesh_point / MESH_NUM_X_POINTS; + // Scale the z value to 1u resolution. + int16_t v = mbl.active ? int16_t(floor(mbl.z_values[iy][ix] * 1000.f + 0.5f)) : 0; + eeprom_update_word((uint16_t*)(EEPROM_UVLO_MESH_BED_LEVELING_FULL +2*mesh_point), *reinterpret_cast(&v)); + } + // Read out the current Z motor microstep counter. This will be later used + // for reaching the zero full step before powering off. + eeprom_update_word((uint16_t*)(EEPROM_UVLO_Z_MICROSTEPS), z_microsteps); + // Store the current position. + + eeprom_update_float((float*)(EEPROM_UVLO_CURRENT_POSITION + 0), current_position[X_AXIS]); + eeprom_update_float((float*)(EEPROM_UVLO_CURRENT_POSITION + 4), current_position[Y_AXIS]); + eeprom_update_float((float*)EEPROM_UVLO_CURRENT_POSITION_Z , current_position[Z_AXIS]); + // Store the current feed rate, temperatures, fan speed and extruder multipliers (flow rates) + EEPROM_save_B(EEPROM_UVLO_FEEDRATE, &feedrate_bckp); + eeprom_update_byte((uint8_t*)EEPROM_UVLO_TARGET_HOTEND, target_temperature[active_extruder]); + eeprom_update_byte((uint8_t*)EEPROM_UVLO_TARGET_BED, target_temperature_bed); + eeprom_update_byte((uint8_t*)EEPROM_UVLO_FAN_SPEED, fanSpeed); + eeprom_update_float((float*)(EEPROM_EXTRUDER_MULTIPLIER_0), extruder_multiplier[0]); +#if EXTRUDERS > 1 + eeprom_update_float((float*)(EEPROM_EXTRUDER_MULTIPLIER_1), extruder_multiplier[1]); +#if EXTRUDERS > 2 + eeprom_update_float((float*)(EEPROM_EXTRUDER_MULTIPLIER_2), extruder_multiplier[2]); +#endif +#endif + eeprom_update_word((uint16_t*)(EEPROM_EXTRUDEMULTIPLY), (uint16_t)extrudemultiply); + + // Finaly store the "power outage" flag. + if(sd_print) eeprom_update_byte((uint8_t*)EEPROM_UVLO, 1); + + st_synchronize(); + printf_P(_N("stps%d\n"), tmc2130_rd_MSCNT(Z_AXIS)); + + // Increment power failure counter + eeprom_update_byte((uint8_t*)EEPROM_POWER_COUNT, eeprom_read_byte((uint8_t*)EEPROM_POWER_COUNT) + 1); + eeprom_update_word((uint16_t*)EEPROM_POWER_COUNT_TOT, eeprom_read_word((uint16_t*)EEPROM_POWER_COUNT_TOT) + 1); + printf_P(_N("UVLO - end %d\n"), _millis() - time_start); + +#if 0 + // Move the print head to the side of the print until all the power stored in the power supply capacitors is depleted. + current_position[X_AXIS] = (current_position[X_AXIS] < 0.5f * (X_MIN_POS + X_MAX_POS)) ? X_MIN_POS : X_MAX_POS; + plan_buffer_line_curposXYZE(500, active_extruder); + st_synchronize(); +#endif +wdt_enable(WDTO_500MS); +WRITE(BEEPER,HIGH); +while(1) + ; +} + + +void uvlo_tiny() +{ +uint16_t z_microsteps=0; + +// Conserve power as soon as possible. +disable_x(); +disable_y(); +disable_e0(); + +#ifdef TMC2130 +tmc2130_set_current_h(Z_AXIS, 20); +tmc2130_set_current_r(Z_AXIS, 20); +#endif //TMC2130 + +// Read out the current Z motor microstep counter +#ifdef TMC2130 +z_microsteps=tmc2130_rd_MSCNT(Z_TMC2130_CS); +#endif //TMC2130 +planner_abort_hard(); + +//save current position only in case, where the printer is moving on Z axis, which is only when EEPROM_UVLO is 1 +//EEPROM_UVLO is 1 after normal uvlo or after recover_print(), when the extruder is moving on Z axis after rehome +if(eeprom_read_byte((uint8_t*)EEPROM_UVLO)!=2){ + eeprom_update_float((float*)(EEPROM_UVLO_TINY_CURRENT_POSITION_Z), current_position[Z_AXIS]); + eeprom_update_word((uint16_t*)(EEPROM_UVLO_TINY_Z_MICROSTEPS),z_microsteps); +} + +//after multiple power panics current Z axis is unknow +//in this case we set EEPROM_UVLO_TINY_CURRENT_POSITION_Z to last know position which is EEPROM_UVLO_CURRENT_POSITION_Z +if(eeprom_read_float((float*)EEPROM_UVLO_TINY_CURRENT_POSITION_Z) < 0.001f){ + eeprom_update_float((float*)(EEPROM_UVLO_TINY_CURRENT_POSITION_Z), eeprom_read_float((float*)EEPROM_UVLO_CURRENT_POSITION_Z)); + eeprom_update_word((uint16_t*)(EEPROM_UVLO_TINY_Z_MICROSTEPS), eeprom_read_word((uint16_t*)EEPROM_UVLO_Z_MICROSTEPS)); +} + +// Finaly store the "power outage" flag. +eeprom_update_byte((uint8_t*)EEPROM_UVLO,2); + +// Increment power failure counter +eeprom_update_byte((uint8_t*)EEPROM_POWER_COUNT, eeprom_read_byte((uint8_t*)EEPROM_POWER_COUNT) + 1); +eeprom_update_word((uint16_t*)EEPROM_POWER_COUNT_TOT, eeprom_read_word((uint16_t*)EEPROM_POWER_COUNT_TOT) + 1); +wdt_enable(WDTO_500MS); +WRITE(BEEPER,HIGH); +while(1) + ; +} +#endif //UVLO_SUPPORT + +#if (defined(FANCHECK) && defined(TACH_1) && (TACH_1 >-1)) + +void setup_fan_interrupt() { +//INT7 + DDRE &= ~(1 << 7); //input pin + PORTE &= ~(1 << 7); //no internal pull-up + + //start with sensing rising edge + EICRB &= ~(1 << 6); + EICRB |= (1 << 7); + + //enable INT7 interrupt + EIMSK |= (1 << 7); +} + +// The fan interrupt is triggered at maximum 325Hz (may be a bit more due to component tollerances), +// and it takes 4.24 us to process (the interrupt invocation overhead not taken into account). +ISR(INT7_vect) { + //measuring speed now works for fanSpeed > 18 (approximately), which is sufficient because MIN_PRINT_FAN_SPEED is higher +#ifdef FAN_SOFT_PWM + if (!fan_measuring || (fanSpeedSoftPwm < MIN_PRINT_FAN_SPEED)) return; +#else //FAN_SOFT_PWM + if (fanSpeed < MIN_PRINT_FAN_SPEED) return; +#endif //FAN_SOFT_PWM + + if ((1 << 6) & EICRB) { //interrupt was triggered by rising edge + t_fan_rising_edge = millis_nc(); + } + else { //interrupt was triggered by falling edge + if ((millis_nc() - t_fan_rising_edge) >= FAN_PULSE_WIDTH_LIMIT) {//this pulse was from sensor and not from pwm + fan_edge_counter[1] += 2; //we are currently counting all edges so lets count two edges for one pulse + } + } + EICRB ^= (1 << 6); //change edge +} + +#endif + +#ifdef UVLO_SUPPORT +void setup_uvlo_interrupt() { + DDRE &= ~(1 << 4); //input pin + PORTE &= ~(1 << 4); //no internal pull-up + + //sensing falling edge + EICRB |= (1 << 0); + EICRB &= ~(1 << 1); + + //enable INT4 interrupt + EIMSK |= (1 << 4); +} + +ISR(INT4_vect) { + EIMSK &= ~(1 << 4); //disable INT4 interrupt to make sure that this code will be executed just once + SERIAL_ECHOLNPGM("INT4"); + //fire normal uvlo only in case where EEPROM_UVLO is 0 or if IS_SD_PRINTING is 1. + if(PRINTER_ACTIVE && (!(eeprom_read_byte((uint8_t*)EEPROM_UVLO)))) uvlo_(); + if(eeprom_read_byte((uint8_t*)EEPROM_UVLO)) uvlo_tiny(); +} + +void recover_print(uint8_t automatic) { + char cmd[30]; + lcd_update_enable(true); + lcd_update(2); + lcd_setstatuspgm(_i("Recovering print "));////MSG_RECOVERING_PRINT c=20 r=1 + + bool bTiny=(eeprom_read_byte((uint8_t*)EEPROM_UVLO)==2); + recover_machine_state_after_power_panic(bTiny); //recover position, temperatures and extrude_multipliers + // Lift the print head, so one may remove the excess priming material. + if(!bTiny&&(current_position[Z_AXIS]<25)) + enquecommand_P(PSTR("G1 Z25 F800")); + + // Home X and Y axes. Homing just X and Y shall not touch the babystep and the world2machine transformation status. + enquecommand_P(PSTR("G28 X Y")); + // Set the target bed and nozzle temperatures and wait. + sprintf_P(cmd, PSTR("M109 S%d"), target_temperature[active_extruder]); + enquecommand(cmd); + sprintf_P(cmd, PSTR("M190 S%d"), target_temperature_bed); + enquecommand(cmd); + enquecommand_P(PSTR("M83")); //E axis relative mode + //enquecommand_P(PSTR("G1 E5 F120")); //Extrude some filament to stabilize pessure + // If not automatically recoreverd (long power loss), extrude extra filament to stabilize + if(automatic == 0){ + enquecommand_P(PSTR("G1 E5 F120")); //Extrude some filament to stabilize pessure + } + enquecommand_P(PSTR("G1 E" STRINGIFY(-default_retraction)" F480")); + + printf_P(_N("After waiting for temp:\nCurrent pos X_AXIS:%.3f\nCurrent pos Y_AXIS:%.3f\n"), current_position[X_AXIS], current_position[Y_AXIS]); + + // Restart the print. + restore_print_from_eeprom(); + printf_P(_N("Current pos Z_AXIS:%.3f\nCurrent pos E_AXIS:%.3f\n"), current_position[Z_AXIS], current_position[E_AXIS]); +} + +void recover_machine_state_after_power_panic(bool bTiny) +{ + char cmd[30]; + // 1) Recover the logical cordinates at the time of the power panic. + // The logical XY coordinates are needed to recover the machine Z coordinate corrected by the mesh bed leveling. + current_position[X_AXIS] = eeprom_read_float((float*)(EEPROM_UVLO_CURRENT_POSITION + 0)); + current_position[Y_AXIS] = eeprom_read_float((float*)(EEPROM_UVLO_CURRENT_POSITION + 4)); + + // 2) Restore the mesh bed leveling offsets. This is 2*7*7=98 bytes, which takes 98*3.4us=333us in worst case. + mbl.active = false; + for (int8_t mesh_point = 0; mesh_point < MESH_NUM_X_POINTS * MESH_NUM_Y_POINTS; ++ mesh_point) { + uint8_t ix = mesh_point % MESH_NUM_X_POINTS; // from 0 to MESH_NUM_X_POINTS - 1 + uint8_t iy = mesh_point / MESH_NUM_X_POINTS; + // Scale the z value to 10u resolution. + int16_t v; + eeprom_read_block(&v, (void*)(EEPROM_UVLO_MESH_BED_LEVELING_FULL+2*mesh_point), 2); + if (v != 0) + mbl.active = true; + mbl.z_values[iy][ix] = float(v) * 0.001f; + } + + // Recover the logical coordinate of the Z axis at the time of the power panic. + // The current position after power panic is moved to the next closest 0th full step. + if(bTiny){ + current_position[Z_AXIS] = eeprom_read_float((float*)(EEPROM_UVLO_TINY_CURRENT_POSITION_Z)) + + float((1024 - eeprom_read_word((uint16_t*)(EEPROM_UVLO_TINY_Z_MICROSTEPS)) + + 7) >> 4) / cs.axis_steps_per_unit[Z_AXIS]; + + //after multiple power panics the print is slightly in the air so get it little bit down. + //Not exactly sure why is this happening, but it has something to do with bed leveling and world2machine coordinates + current_position[Z_AXIS] -= 0.4*mbl.get_z(current_position[X_AXIS], current_position[Y_AXIS]); + } + else{ + current_position[Z_AXIS] = eeprom_read_float((float*)(EEPROM_UVLO_CURRENT_POSITION_Z)) + + UVLO_Z_AXIS_SHIFT + float((1024 - eeprom_read_word((uint16_t*)(EEPROM_UVLO_Z_MICROSTEPS)) + + 7) >> 4) / cs.axis_steps_per_unit[Z_AXIS]; + } + if (eeprom_read_byte((uint8_t*)EEPROM_UVLO_E_ABS)) { + current_position[E_AXIS] = eeprom_read_float((float*)(EEPROM_UVLO_CURRENT_POSITION_E)); + sprintf_P(cmd, PSTR("G92 E")); + dtostrf(current_position[E_AXIS], 6, 3, cmd + strlen(cmd)); + enquecommand(cmd); + } + + memcpy(destination, current_position, sizeof(destination)); + + SERIAL_ECHOPGM("recover_machine_state_after_power_panic, initial "); + print_world_coordinates(); + + // 3) Initialize the logical to physical coordinate system transformation. + world2machine_initialize(); +// SERIAL_ECHOPGM("recover_machine_state_after_power_panic, initial "); +// print_mesh_bed_leveling_table(); + + // 4) Load the baby stepping value, which is expected to be active at the time of power panic. + // The baby stepping value is used to reset the physical Z axis when rehoming the Z axis. + babystep_load(); + + // 5) Set the physical positions from the logical positions using the world2machine transformation and the active bed leveling. + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + + // 6) Power up the motors, mark their positions as known. + //FIXME Verfiy, whether the X and Y axes should be powered up here, as they will later be re-homed anyway. + axis_known_position[X_AXIS] = true; enable_x(); + axis_known_position[Y_AXIS] = true; enable_y(); + axis_known_position[Z_AXIS] = true; enable_z(); + + SERIAL_ECHOPGM("recover_machine_state_after_power_panic, initial "); + print_physical_coordinates(); + + // 7) Recover the target temperatures. + target_temperature[active_extruder] = eeprom_read_byte((uint8_t*)EEPROM_UVLO_TARGET_HOTEND); + target_temperature_bed = eeprom_read_byte((uint8_t*)EEPROM_UVLO_TARGET_BED); + + // 8) Recover extruder multipilers + extruder_multiplier[0] = eeprom_read_float((float*)(EEPROM_EXTRUDER_MULTIPLIER_0)); +#if EXTRUDERS > 1 + extruder_multiplier[1] = eeprom_read_float((float*)(EEPROM_EXTRUDER_MULTIPLIER_1)); +#if EXTRUDERS > 2 + extruder_multiplier[2] = eeprom_read_float((float*)(EEPROM_EXTRUDER_MULTIPLIER_2)); +#endif +#endif + extrudemultiply = (int)eeprom_read_word((uint16_t*)(EEPROM_EXTRUDEMULTIPLY)); +} + +void restore_print_from_eeprom() { + int feedrate_rec; + uint8_t fan_speed_rec; + char cmd[30]; + char filename[13]; + uint8_t depth = 0; + char dir_name[9]; + + fan_speed_rec = eeprom_read_byte((uint8_t*)EEPROM_UVLO_FAN_SPEED); + EEPROM_read_B(EEPROM_UVLO_FEEDRATE, &feedrate_rec); + SERIAL_ECHOPGM("Feedrate:"); + MYSERIAL.println(feedrate_rec); + + depth = eeprom_read_byte((uint8_t*)EEPROM_DIR_DEPTH); + + MYSERIAL.println(int(depth)); + for (int i = 0; i < depth; i++) { + for (int j = 0; j < 8; j++) { + dir_name[j] = eeprom_read_byte((uint8_t*)EEPROM_DIRS + j + 8 * i); + } + dir_name[8] = '\0'; + MYSERIAL.println(dir_name); + strcpy(dir_names[i], dir_name); + card.chdir(dir_name); + } + + for (int i = 0; i < 8; i++) { + filename[i] = eeprom_read_byte((uint8_t*)EEPROM_FILENAME + i); + } + filename[8] = '\0'; + + MYSERIAL.print(filename); + strcat_P(filename, PSTR(".gco")); + sprintf_P(cmd, PSTR("M23 %s"), filename); + enquecommand(cmd); + uint32_t position = eeprom_read_dword((uint32_t*)(EEPROM_FILE_POSITION)); + SERIAL_ECHOPGM("Position read from eeprom:"); + MYSERIAL.println(position); + // E axis relative mode. + enquecommand_P(PSTR("M83")); + // Move to the XY print position in logical coordinates, where the print has been killed. + strcpy_P(cmd, PSTR("G1 X")); strcat(cmd, ftostr32(eeprom_read_float((float*)(EEPROM_UVLO_CURRENT_POSITION + 0)))); + strcat_P(cmd, PSTR(" Y")); strcat(cmd, ftostr32(eeprom_read_float((float*)(EEPROM_UVLO_CURRENT_POSITION + 4)))); + strcat_P(cmd, PSTR(" F2000")); + enquecommand(cmd); + //moving on Z axis ahead, set EEPROM_UVLO to 1, so normal uvlo can fire + eeprom_update_byte((uint8_t*)EEPROM_UVLO,1); + // Move the Z axis down to the print, in logical coordinates. + strcpy_P(cmd, PSTR("G1 Z")); strcat(cmd, ftostr32(eeprom_read_float((float*)(EEPROM_UVLO_CURRENT_POSITION_Z)))); + enquecommand(cmd); + // Unretract. + enquecommand_P(PSTR("G1 E" STRINGIFY(2*default_retraction)" F480")); + // Set the feedrate saved at the power panic. + sprintf_P(cmd, PSTR("G1 F%d"), feedrate_rec); + enquecommand(cmd); + if (eeprom_read_byte((uint8_t*)EEPROM_UVLO_E_ABS)) + { + enquecommand_P(PSTR("M82")); //E axis abslute mode + } + // Set the fan speed saved at the power panic. + strcpy_P(cmd, PSTR("M106 S")); + strcat(cmd, itostr3(int(fan_speed_rec))); + enquecommand(cmd); + + // Set a position in the file. + sprintf_P(cmd, PSTR("M26 S%lu"), position); + enquecommand(cmd); + enquecommand_P(PSTR("G4 S0")); + enquecommand_P(PSTR("PRUSA uvlo")); +} +#endif //UVLO_SUPPORT + + +//! @brief Immediately stop print moves +//! +//! Immediately stop print moves, save current extruder temperature and position to RAM. +//! If printing from sd card, position in file is saved. +//! If printing from USB, line number is saved. +//! +//! @param z_move +//! @param e_move +void stop_and_save_print_to_ram(float z_move, float e_move) +{ + if (saved_printing) return; +#if 0 + unsigned char nplanner_blocks; +#endif + unsigned char nlines; + uint16_t sdlen_planner; + uint16_t sdlen_cmdqueue; + + + cli(); + if (card.sdprinting) { +#if 0 + nplanner_blocks = number_of_blocks(); +#endif + saved_sdpos = sdpos_atomic; //atomic sd position of last command added in queue + sdlen_planner = planner_calc_sd_length(); //length of sd commands in planner + saved_sdpos -= sdlen_planner; + sdlen_cmdqueue = cmdqueue_calc_sd_length(); //length of sd commands in cmdqueue + saved_sdpos -= sdlen_cmdqueue; + saved_printing_type = PRINTING_TYPE_SD; + + } + else if (is_usb_printing) { //reuse saved_sdpos for storing line number + saved_sdpos = gcode_LastN; //start with line number of command added recently to cmd queue + //reuse planner_calc_sd_length function for getting number of lines of commands in planner: + nlines = planner_calc_sd_length(); //number of lines of commands in planner + saved_sdpos -= nlines; + saved_sdpos -= buflen; //number of blocks in cmd buffer + saved_printing_type = PRINTING_TYPE_USB; + } + else { + saved_printing_type = PRINTING_TYPE_NONE; + //not sd printing nor usb printing + } + +#if 0 + SERIAL_ECHOPGM("SDPOS_ATOMIC="); MYSERIAL.println(sdpos_atomic, DEC); + SERIAL_ECHOPGM("SDPOS="); MYSERIAL.println(card.get_sdpos(), DEC); + SERIAL_ECHOPGM("SDLEN_PLAN="); MYSERIAL.println(sdlen_planner, DEC); + SERIAL_ECHOPGM("SDLEN_CMDQ="); MYSERIAL.println(sdlen_cmdqueue, DEC); + SERIAL_ECHOPGM("PLANNERBLOCKS="); MYSERIAL.println(int(nplanner_blocks), DEC); + SERIAL_ECHOPGM("SDSAVED="); MYSERIAL.println(saved_sdpos, DEC); + //SERIAL_ECHOPGM("SDFILELEN="); MYSERIAL.println(card.fileSize(), DEC); + + + { + card.setIndex(saved_sdpos); + SERIAL_ECHOLNPGM("Content of planner buffer: "); + for (unsigned int idx = 0; idx < sdlen_planner; ++ idx) + MYSERIAL.print(char(card.get())); + SERIAL_ECHOLNPGM("Content of command buffer: "); + for (unsigned int idx = 0; idx < sdlen_cmdqueue; ++ idx) + MYSERIAL.print(char(card.get())); + SERIAL_ECHOLNPGM("End of command buffer"); + } + { + // Print the content of the planner buffer, line by line: + card.setIndex(saved_sdpos); + int8_t iline = 0; + for (unsigned char idx = block_buffer_tail; idx != block_buffer_head; idx = (idx + 1) & (BLOCK_BUFFER_SIZE - 1), ++ iline) { + SERIAL_ECHOPGM("Planner line (from file): "); + MYSERIAL.print(int(iline), DEC); + SERIAL_ECHOPGM(", length: "); + MYSERIAL.print(block_buffer[idx].sdlen, DEC); + SERIAL_ECHOPGM(", steps: ("); + MYSERIAL.print(block_buffer[idx].steps_x, DEC); + SERIAL_ECHOPGM(","); + MYSERIAL.print(block_buffer[idx].steps_y, DEC); + SERIAL_ECHOPGM(","); + MYSERIAL.print(block_buffer[idx].steps_z, DEC); + SERIAL_ECHOPGM(","); + MYSERIAL.print(block_buffer[idx].steps_e, DEC); + SERIAL_ECHOPGM("), events: "); + MYSERIAL.println(block_buffer[idx].step_event_count, DEC); + for (int len = block_buffer[idx].sdlen; len > 0; -- len) + MYSERIAL.print(char(card.get())); + } + } + { + // Print the content of the command buffer, line by line: + int8_t iline = 0; + union { + struct { + char lo; + char hi; + } lohi; + uint16_t value; + } sdlen_single; + int _bufindr = bufindr; + for (int _buflen = buflen; _buflen > 0; ++ iline) { + if (cmdbuffer[_bufindr] == CMDBUFFER_CURRENT_TYPE_SDCARD) { + sdlen_single.lohi.lo = cmdbuffer[_bufindr + 1]; + sdlen_single.lohi.hi = cmdbuffer[_bufindr + 2]; + } + SERIAL_ECHOPGM("Buffer line (from buffer): "); + MYSERIAL.print(int(iline), DEC); + SERIAL_ECHOPGM(", type: "); + MYSERIAL.print(int(cmdbuffer[_bufindr]), DEC); + SERIAL_ECHOPGM(", len: "); + MYSERIAL.println(sdlen_single.value, DEC); + // Print the content of the buffer line. + MYSERIAL.println(cmdbuffer + _bufindr + CMDHDRSIZE); + + SERIAL_ECHOPGM("Buffer line (from file): "); + MYSERIAL.println(int(iline), DEC); + for (; sdlen_single.value > 0; -- sdlen_single.value) + MYSERIAL.print(char(card.get())); + + if (-- _buflen == 0) + break; + // First skip the current command ID and iterate up to the end of the string. + for (_bufindr += CMDHDRSIZE; cmdbuffer[_bufindr] != 0; ++ _bufindr) ; + // Second, skip the end of string null character and iterate until a nonzero command ID is found. + for (++ _bufindr; _bufindr < sizeof(cmdbuffer) && cmdbuffer[_bufindr] == 0; ++ _bufindr) ; + // If the end of the buffer was empty, + if (_bufindr == sizeof(cmdbuffer)) { + // skip to the start and find the nonzero command. + for (_bufindr = 0; cmdbuffer[_bufindr] == 0; ++ _bufindr) ; + } + } + } +#endif + +#if 0 + saved_feedrate2 = feedrate; //save feedrate +#else + // Try to deduce the feedrate from the first block of the planner. + // Speed is in mm/min. + saved_feedrate2 = blocks_queued() ? (block_buffer[block_buffer_tail].nominal_speed * 60.f) : feedrate; +#endif + + planner_abort_hard(); //abort printing + memcpy(saved_pos, current_position, sizeof(saved_pos)); + saved_active_extruder = active_extruder; //save active_extruder + saved_extruder_temperature = degTargetHotend(active_extruder); + + saved_extruder_under_pressure = extruder_under_pressure; //extruder under pressure flag - currently unused + saved_extruder_relative_mode = axis_relative_modes[E_AXIS]; + saved_fanSpeed = fanSpeed; + cmdqueue_reset(); //empty cmdqueue + card.sdprinting = false; +// card.closefile(); + saved_printing = true; + // We may have missed a stepper timer interrupt. Be safe than sorry, reset the stepper timer before re-enabling interrupts. + st_reset_timer(); + sei(); + if ((z_move != 0) || (e_move != 0)) { // extruder or z move +#if 1 + // Rather than calling plan_buffer_line directly, push the move into the command queue, + char buf[48]; + + // First unretract (relative extrusion) + if(!saved_extruder_relative_mode){ + enquecommand(PSTR("M83"), true); + } + //retract 45mm/s + // A single sprintf may not be faster, but is definitely 20B shorter + // than a sequence of commands building the string piece by piece + // A snprintf would have been a safer call, but since it is not used + // in the whole program, its implementation would bring more bytes to the total size + // The behavior of dtostrf 8,3 should be roughly the same as %-0.3 + sprintf_P(buf, PSTR("G1 E%-0.3f F2700"), e_move); + enquecommand(buf, false); + + // Then lift Z axis + sprintf_P(buf, PSTR("G1 Z%-0.3f F%-0.3f"), saved_pos[Z_AXIS] + z_move, homing_feedrate[Z_AXIS]); + // At this point the command queue is empty. + enquecommand(buf, false); + // If this call is invoked from the main Arduino loop() function, let the caller know that the command + // in the command queue is not the original command, but a new one, so it should not be removed from the queue. + repeatcommand_front(); +#else + plan_buffer_line(saved_pos[X_AXIS], saved_pos[Y_AXIS], saved_pos[Z_AXIS] + z_move, saved_pos[E_AXIS] + e_move, homing_feedrate[Z_AXIS], active_extruder); + st_synchronize(); //wait moving + memcpy(current_position, saved_pos, sizeof(saved_pos)); + memcpy(destination, current_position, sizeof(destination)); +#endif + } +} + +//! @brief Restore print from ram +//! +//! Restore print saved by stop_and_save_print_to_ram(). Is blocking, restores +//! print fan speed, waits for extruder temperature restore, then restores +//! position and continues print moves. +//! +//! Internally lcd_update() is called by wait_for_heater(). +//! +//! @param e_move +void restore_print_from_ram_and_continue(float e_move) +{ + if (!saved_printing) return; + +#ifdef FANCHECK + // Do not allow resume printing if fans are still not ok + if ((fan_check_error != EFCE_OK) && (fan_check_error != EFCE_FIXED)) return; + if (fan_check_error == EFCE_FIXED) fan_check_error = EFCE_OK; //reenable serial stream processing if printing from usb +#endif + +// for (int axis = X_AXIS; axis <= E_AXIS; axis++) +// current_position[axis] = st_get_position_mm(axis); + active_extruder = saved_active_extruder; //restore active_extruder + fanSpeed = saved_fanSpeed; + if (degTargetHotend(saved_active_extruder) != saved_extruder_temperature) + { + setTargetHotendSafe(saved_extruder_temperature, saved_active_extruder); + heating_status = 1; + wait_for_heater(_millis(), saved_active_extruder); + heating_status = 2; + } + feedrate = saved_feedrate2; //restore feedrate + axis_relative_modes[E_AXIS] = saved_extruder_relative_mode; + float e = saved_pos[E_AXIS] - e_move; + plan_set_e_position(e); + + #ifdef FANCHECK + fans_check_enabled = false; + #endif + + //first move print head in XY to the saved position: + plan_buffer_line(saved_pos[X_AXIS], saved_pos[Y_AXIS], current_position[Z_AXIS], saved_pos[E_AXIS] - e_move, homing_feedrate[Z_AXIS]/13, active_extruder); + st_synchronize(); + //then move Z + plan_buffer_line(saved_pos[X_AXIS], saved_pos[Y_AXIS], saved_pos[Z_AXIS], saved_pos[E_AXIS] - e_move, homing_feedrate[Z_AXIS]/13, active_extruder); + st_synchronize(); + //and finaly unretract (35mm/s) + plan_buffer_line(saved_pos[X_AXIS], saved_pos[Y_AXIS], saved_pos[Z_AXIS], saved_pos[E_AXIS], 35, active_extruder); + st_synchronize(); + + #ifdef FANCHECK + fans_check_enabled = true; + #endif + + memcpy(current_position, saved_pos, sizeof(saved_pos)); + memcpy(destination, current_position, sizeof(destination)); + if (saved_printing_type == PRINTING_TYPE_SD) { //was sd printing + card.setIndex(saved_sdpos); + sdpos_atomic = saved_sdpos; + card.sdprinting = true; + } + else if (saved_printing_type == PRINTING_TYPE_USB) { //was usb printing + gcode_LastN = saved_sdpos; //saved_sdpos was reused for storing line number when usb printing + serial_count = 0; + FlushSerialRequestResend(); + } + else { + //not sd printing nor usb printing + } + SERIAL_PROTOCOLLNRPGM(MSG_OK); //dummy response because of octoprint is waiting for this + lcd_setstatuspgm(_T(WELCOME_MSG)); + saved_printing_type = PRINTING_TYPE_NONE; + saved_printing = false; +} + +void print_world_coordinates() +{ + printf_P(_N("world coordinates: (%.3f, %.3f, %.3f)\n"), current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS]); +} + +void print_physical_coordinates() +{ + printf_P(_N("physical coordinates: (%.3f, %.3f, %.3f)\n"), st_get_position_mm(X_AXIS), st_get_position_mm(Y_AXIS), st_get_position_mm(Z_AXIS)); +} + +void print_mesh_bed_leveling_table() +{ + SERIAL_ECHOPGM("mesh bed leveling: "); + for (int8_t y = 0; y < MESH_NUM_Y_POINTS; ++ y) + for (int8_t x = 0; x < MESH_NUM_Y_POINTS; ++ x) { + MYSERIAL.print(mbl.z_values[y][x], 3); + SERIAL_ECHOPGM(" "); + } + SERIAL_ECHOLNPGM(""); +} + +uint16_t print_time_remaining() { + uint16_t print_t = PRINT_TIME_REMAINING_INIT; +#ifdef TMC2130 + if (SilentModeMenu == SILENT_MODE_OFF) print_t = print_time_remaining_normal; + else print_t = print_time_remaining_silent; +#else + print_t = print_time_remaining_normal; +#endif //TMC2130 + if ((print_t != PRINT_TIME_REMAINING_INIT) && (feedmultiply != 0)) print_t = 100ul * print_t / feedmultiply; + return print_t; +} + +uint8_t calc_percent_done() +{ + //in case that we have information from M73 gcode return percentage counted by slicer, else return percentage counted as byte_printed/filesize + uint8_t percent_done = 0; +#ifdef TMC2130 + if (SilentModeMenu == SILENT_MODE_OFF && print_percent_done_normal <= 100) { + percent_done = print_percent_done_normal; + } + else if (print_percent_done_silent <= 100) { + percent_done = print_percent_done_silent; + } +#else + if (print_percent_done_normal <= 100) { + percent_done = print_percent_done_normal; + } +#endif //TMC2130 + else { + percent_done = card.percentDone(); + } + return percent_done; +} + +static void print_time_remaining_init() +{ + print_time_remaining_normal = PRINT_TIME_REMAINING_INIT; + print_time_remaining_silent = PRINT_TIME_REMAINING_INIT; + print_percent_done_normal = PRINT_PERCENT_DONE_INIT; + print_percent_done_silent = PRINT_PERCENT_DONE_INIT; +} + +void load_filament_final_feed() +{ + current_position[E_AXIS]+= FILAMENTCHANGE_FINALFEED; + plan_buffer_line_curposXYZE(FILAMENTCHANGE_EFEED_FINAL, active_extruder); +} + +//! @brief Wait for user to check the state +//! @par nozzle_temp nozzle temperature to load filament +void M600_check_state(float nozzle_temp) +{ + lcd_change_fil_state = 0; + while (lcd_change_fil_state != 1) + { + lcd_change_fil_state = 0; + KEEPALIVE_STATE(PAUSED_FOR_USER); + lcd_alright(); + KEEPALIVE_STATE(IN_HANDLER); + switch(lcd_change_fil_state) + { + // Filament failed to load so load it again + case 2: + if (mmu_enabled) + mmu_M600_load_filament(false, nozzle_temp); //nonautomatic load; change to "wrong filament loaded" option? + else + M600_load_filament_movements(); + break; + + // Filament loaded properly but color is not clear + case 3: + st_synchronize(); + load_filament_final_feed(); + lcd_loading_color(); + st_synchronize(); + break; + + // Everything good + default: + lcd_change_success(); + break; + } + } +} + +//! @brief Wait for user action +//! +//! Beep, manage nozzle heater and wait for user to start unload filament +//! If times out, active extruder temperature is set to 0. +//! +//! @param HotendTempBckp Temperature to be restored for active extruder, after user resolves MMU problem. +void M600_wait_for_user(float HotendTempBckp) { + + KEEPALIVE_STATE(PAUSED_FOR_USER); + + int counterBeep = 0; + unsigned long waiting_start_time = _millis(); + uint8_t wait_for_user_state = 0; + lcd_display_message_fullscreen_P(_T(MSG_PRESS_TO_UNLOAD)); + bool bFirst=true; + + while (!(wait_for_user_state == 0 && lcd_clicked())){ + manage_heater(); + manage_inactivity(true); + + #if BEEPER > 0 + if (counterBeep == 500) { + counterBeep = 0; + } + SET_OUTPUT(BEEPER); + if (counterBeep == 0) { + if((eSoundMode==e_SOUND_MODE_BLIND)|| (eSoundMode==e_SOUND_MODE_LOUD)||((eSoundMode==e_SOUND_MODE_ONCE)&&bFirst)) + { + bFirst=false; + WRITE(BEEPER, HIGH); + } + } + if (counterBeep == 20) { + WRITE(BEEPER, LOW); + } + + counterBeep++; + #endif //BEEPER > 0 + + switch (wait_for_user_state) { + case 0: //nozzle is hot, waiting for user to press the knob to unload filament + delay_keep_alive(4); + + if (_millis() > waiting_start_time + (unsigned long)M600_TIMEOUT * 1000) { + lcd_display_message_fullscreen_P(_i("Press knob to preheat nozzle and continue."));////MSG_PRESS_TO_PREHEAT c=20 r=4 + wait_for_user_state = 1; + setAllTargetHotends(0); + st_synchronize(); + disable_e0(); + disable_e1(); + disable_e2(); + } + break; + case 1: //nozzle target temperature is set to zero, waiting for user to start nozzle preheat + delay_keep_alive(4); + + if (lcd_clicked()) { + setTargetHotend(HotendTempBckp, active_extruder); + lcd_wait_for_heater(); + + wait_for_user_state = 2; + } + break; + case 2: //waiting for nozzle to reach target temperature + + if (abs(degTargetHotend(active_extruder) - degHotend(active_extruder)) < 1) { + lcd_display_message_fullscreen_P(_T(MSG_PRESS_TO_UNLOAD)); + waiting_start_time = _millis(); + wait_for_user_state = 0; + } + else { + counterBeep = 20; //beeper will be inactive during waiting for nozzle preheat + lcd_set_cursor(1, 4); + lcd_print(ftostr3(degHotend(active_extruder))); + } + break; + + } + + } + WRITE(BEEPER, LOW); +} + +void M600_load_filament_movements() +{ +#ifdef SNMM + display_loading(); + do + { + current_position[E_AXIS] += 0.002; + plan_buffer_line_curposXYZE(500, active_extruder); + delay_keep_alive(2); + } + while (!lcd_clicked()); + st_synchronize(); + current_position[E_AXIS] += bowden_length[mmu_extruder]; + plan_buffer_line_curposXYZE(3000, active_extruder); + current_position[E_AXIS] += FIL_LOAD_LENGTH - 60; + plan_buffer_line_curposXYZE(1400, active_extruder); + current_position[E_AXIS] += 40; + plan_buffer_line_curposXYZE(400, active_extruder); + current_position[E_AXIS] += 10; + plan_buffer_line_curposXYZE(50, active_extruder); +#else + current_position[E_AXIS]+= FILAMENTCHANGE_FIRSTFEED ; + plan_buffer_line_curposXYZE(FILAMENTCHANGE_EFEED_FIRST, active_extruder); +#endif + load_filament_final_feed(); + lcd_loading_filament(); + st_synchronize(); +} + +void M600_load_filament() { + //load filament for single material and SNMM + lcd_wait_interact(); + + //load_filament_time = _millis(); + KEEPALIVE_STATE(PAUSED_FOR_USER); + +#ifdef PAT9125 + fsensor_autoload_check_start(); +#endif //PAT9125 + while(!lcd_clicked()) + { + manage_heater(); + manage_inactivity(true); +#ifdef FILAMENT_SENSOR + if (fsensor_check_autoload()) + { + Sound_MakeCustom(50,1000,false); + break; + } +#endif //FILAMENT_SENSOR + } +#ifdef PAT9125 + fsensor_autoload_check_stop(); +#endif //PAT9125 + KEEPALIVE_STATE(IN_HANDLER); + +#ifdef FSENSOR_QUALITY + fsensor_oq_meassure_start(70); +#endif //FSENSOR_QUALITY + + M600_load_filament_movements(); + + Sound_MakeCustom(50,1000,false); + +#ifdef FSENSOR_QUALITY + fsensor_oq_meassure_stop(); + + if (!fsensor_oq_result()) + { + bool disable = lcd_show_fullscreen_message_yes_no_and_wait_P(_i("Fil. sensor response is poor, disable it?"), false, true); + lcd_update_enable(true); + lcd_update(2); + if (disable) + fsensor_disable(); + } +#endif //FSENSOR_QUALITY + lcd_update_enable(false); +} + + +//! @brief Wait for click +//! +//! Set +void marlin_wait_for_click() +{ + int8_t busy_state_backup = busy_state; + KEEPALIVE_STATE(PAUSED_FOR_USER); + lcd_consume_click(); + while(!lcd_clicked()) + { + manage_heater(); + manage_inactivity(true); + lcd_update(0); + } + KEEPALIVE_STATE(busy_state_backup); +} + +#define FIL_LOAD_LENGTH 60 + +#ifdef PSU_Delta +bool bEnableForce_z; + +void init_force_z() +{ +WRITE(Z_ENABLE_PIN,Z_ENABLE_ON); +bEnableForce_z=true; // "true"-value enforce "disable_force_z()" executing +disable_force_z(); +} + +void check_force_z() +{ +if(!(bEnableForce_z||eeprom_read_byte((uint8_t*)EEPROM_SILENT))) + init_force_z(); // causes enforced switching into disable-state +} + +void disable_force_z() +{ + uint16_t z_microsteps=0; + + if(!bEnableForce_z) return; // motor already disabled (may be ;-p ) + + bEnableForce_z=false; + + // switching to silent mode +#ifdef TMC2130 + tmc2130_mode=TMC2130_MODE_SILENT; + update_mode_profile(); + tmc2130_init(true); +#endif // TMC2130 + + axis_known_position[Z_AXIS]=false; +} + + +void enable_force_z() +{ +if(bEnableForce_z) + return; // motor already enabled (may be ;-p ) +bEnableForce_z=true; + +// mode recovering +#ifdef TMC2130 +tmc2130_mode=eeprom_read_byte((uint8_t*)EEPROM_SILENT)?TMC2130_MODE_SILENT:TMC2130_MODE_NORMAL; +update_mode_profile(); +tmc2130_init(true); +#endif // TMC2130 + +WRITE(Z_ENABLE_PIN,Z_ENABLE_ON); // slightly redundant ;-p +} +#endif // PSU_Delta diff --git a/Firmware/Sd2Card.cpp b/Firmware/Sd2Card.cpp new file mode 100644 index 0000000..c00c7ef --- /dev/null +++ b/Firmware/Sd2Card.cpp @@ -0,0 +1,813 @@ +/* Arduino Sd2Card Library + * Copyright (C) 2009 by William Greiman + * + * This file is part of the Arduino Sd2Card Library + * + * This Library 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 Library 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 the Arduino Sd2Card Library. If not, see + * . + */ +#include "Marlin.h" + +#ifdef SDSUPPORT +#include "Sd2Card.h" +//------------------------------------------------------------------------------ +#ifndef SOFTWARE_SPI +// functions for hardware SPI +//------------------------------------------------------------------------------ +// make sure SPCR rate is in expected bits +#if (SPR0 != 0 || SPR1 != 1) +#error unexpected SPCR bits +#endif +/** + * Initialize hardware SPI + * Set SCK rate to F_CPU/pow(2, 1 + spiRate) for spiRate [0,6] + */ +static void spiInit(uint8_t spiRate) { + // See avr processor documentation + SPCR = (1 << SPE) | (1 << MSTR) | (spiRate >> 1); + SPSR = spiRate & 1 || spiRate == 6 ? 0 : 1 << SPI2X; +} +//------------------------------------------------------------------------------ +/** SPI receive a byte */ +static uint8_t spiRec() { + SPDR = 0XFF; + while (!(SPSR & (1 << SPIF))) { /* Intentionally left empty */ } + return SPDR; +} +//------------------------------------------------------------------------------ +/** SPI read data - only one call so force inline */ +static inline __attribute__((always_inline)) +void spiRead(uint8_t* buf, uint16_t nbyte) { + if (nbyte-- == 0) return; + SPDR = 0XFF; + for (uint16_t i = 0; i < nbyte; i++) { + while (!(SPSR & (1 << SPIF))) { /* Intentionally left empty */ } + buf[i] = SPDR; + SPDR = 0XFF; + } + while (!(SPSR & (1 << SPIF))) { /* Intentionally left empty */ } + buf[nbyte] = SPDR; +} +//------------------------------------------------------------------------------ +/** SPI send a byte */ +static void spiSend(uint8_t b) { + SPDR = b; + while (!(SPSR & (1 << SPIF))) { /* Intentionally left empty */ } +} +//------------------------------------------------------------------------------ +/** SPI send block - only one call so force inline */ +static inline __attribute__((always_inline)) + void spiSendBlock(uint8_t token, const uint8_t* buf) { + SPDR = token; + for (uint16_t i = 0; i < 512; i += 2) { + while (!(SPSR & (1 << SPIF))) { /* Intentionally left empty */ } + SPDR = buf[i]; + while (!(SPSR & (1 << SPIF))) { /* Intentionally left empty */ } + SPDR = buf[i + 1]; + } + while (!(SPSR & (1 << SPIF))) { /* Intentionally left empty */ } +} +//------------------------------------------------------------------------------ +#else // SOFTWARE_SPI +//------------------------------------------------------------------------------ +/** nop to tune soft SPI timing */ +#define nop asm volatile ("nop\n\t") +//------------------------------------------------------------------------------ +/** Soft SPI receive byte */ +static uint8_t spiRec() { + uint8_t data = 0; + // no interrupts during byte receive - about 8 us + cli(); + // output pin high - like sending 0XFF + fastDigitalWrite(SPI_MOSI_PIN, HIGH); + + for (uint8_t i = 0; i < 8; i++) { + fastDigitalWrite(SPI_SCK_PIN, HIGH); + + // adjust so SCK is nice + nop; + nop; + + data <<= 1; + + if (fastDigitalRead(SPI_MISO_PIN)) data |= 1; + + fastDigitalWrite(SPI_SCK_PIN, LOW); + } + // enable interrupts + sei(); + return data; +} +//------------------------------------------------------------------------------ +/** Soft SPI read data */ +static void spiRead(uint8_t* buf, uint16_t nbyte) { + for (uint16_t i = 0; i < nbyte; i++) { + buf[i] = spiRec(); + } +} +//------------------------------------------------------------------------------ +/** Soft SPI send byte */ +static void spiSend(uint8_t data) { + // no interrupts during byte send - about 8 us + cli(); + for (uint8_t i = 0; i < 8; i++) { + fastDigitalWrite(SPI_SCK_PIN, LOW); + + fastDigitalWrite(SPI_MOSI_PIN, data & 0X80); + + data <<= 1; + + fastDigitalWrite(SPI_SCK_PIN, HIGH); + } + // hold SCK high for a few ns + nop; + nop; + nop; + nop; + + fastDigitalWrite(SPI_SCK_PIN, LOW); + // enable interrupts + sei(); +} +//------------------------------------------------------------------------------ +/** Soft SPI send block */ + void spiSendBlock(uint8_t token, const uint8_t* buf) { + spiSend(token); + for (uint16_t i = 0; i < 512; i++) { + spiSend(buf[i]); + } +} +#endif // SOFTWARE_SPI +//------------------------------------------------------------------------------ +// send command and return error code. Return zero for OK +uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg) { + // select card + chipSelectLow(); + + // wait up to 300 ms if busy + waitNotBusy(300); + + // send command + spiSend(cmd | 0x40); + + // send argument + for (int8_t s = 24; s >= 0; s -= 8) spiSend(arg >> s); + + // send CRC + uint8_t crc = 0XFF; + if (cmd == CMD0) crc = 0X95; // correct crc for CMD0 with arg 0 + if (cmd == CMD8) crc = 0X87; // correct crc for CMD8 with arg 0X1AA + spiSend(crc); + + // skip stuff byte for stop read + if (cmd == CMD12) spiRec(); + + // wait for response + for (uint8_t i = 0; ((status_ = spiRec()) & 0X80) && i != 0XFF; i++) { /* Intentionally left empty */ } + return status_; +} +//------------------------------------------------------------------------------ +/** + * Determine the size of an SD flash memory card. + * + * \return The number of 512 byte data blocks in the card + * or zero if an error occurs. + */ +uint32_t Sd2Card::cardSize() { + csd_t csd; + if (!readCSD(&csd)) return 0; + if (csd.v1.csd_ver == 0) { + uint8_t read_bl_len = csd.v1.read_bl_len; + uint16_t c_size = (csd.v1.c_size_high << 10) + | (csd.v1.c_size_mid << 2) | csd.v1.c_size_low; + uint8_t c_size_mult = (csd.v1.c_size_mult_high << 1) + | csd.v1.c_size_mult_low; + return (uint32_t)(c_size + 1) << (c_size_mult + read_bl_len - 7); + } else if (csd.v2.csd_ver == 1) { + uint32_t c_size = ((uint32_t)csd.v2.c_size_high << 16) + | (csd.v2.c_size_mid << 8) | csd.v2.c_size_low; + return (c_size + 1) << 10; + } else { + error(SD_CARD_ERROR_BAD_CSD); + return 0; + } +} +//------------------------------------------------------------------------------ +void Sd2Card::chipSelectHigh() { + digitalWrite(chipSelectPin_, HIGH); +} +//------------------------------------------------------------------------------ +void Sd2Card::chipSelectLow() { +#ifndef SOFTWARE_SPI + spiInit(spiRate_); +#endif // SOFTWARE_SPI + digitalWrite(chipSelectPin_, LOW); +} +//------------------------------------------------------------------------------ +/** Erase a range of blocks. + * + * \param[in] firstBlock The address of the first block in the range. + * \param[in] lastBlock The address of the last block in the range. + * + * \note This function requests the SD card to do a flash erase for a + * range of blocks. The data on the card after an erase operation is + * either 0 or 1, depends on the card vendor. The card must support + * single block erase. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool Sd2Card::erase(uint32_t firstBlock, uint32_t lastBlock) { + csd_t csd; + if (!readCSD(&csd)) goto fail; + // check for single block erase + if (!csd.v1.erase_blk_en) { + // erase size mask + uint8_t m = (csd.v1.sector_size_high << 1) | csd.v1.sector_size_low; + if ((firstBlock & m) != 0 || ((lastBlock + 1) & m) != 0) { + // error card can't erase specified area + error(SD_CARD_ERROR_ERASE_SINGLE_BLOCK); + goto fail; + } + } + if (type_ != SD_CARD_TYPE_SDHC) { + firstBlock <<= 9; + lastBlock <<= 9; + } + if (cardCommand(CMD32, firstBlock) + || cardCommand(CMD33, lastBlock) + || cardCommand(CMD38, 0)) { + error(SD_CARD_ERROR_ERASE); + goto fail; + } + if (!waitNotBusy(SD_ERASE_TIMEOUT)) { + error(SD_CARD_ERROR_ERASE_TIMEOUT); + goto fail; + } + chipSelectHigh(); + return true; + + fail: + chipSelectHigh(); + return false; +} +//------------------------------------------------------------------------------ +/** Determine if card supports single block erase. + * + * \return The value one, true, is returned if single block erase is supported. + * The value zero, false, is returned if single block erase is not supported. + */ +bool Sd2Card::eraseSingleBlockEnable() { + csd_t csd; + return readCSD(&csd) ? csd.v1.erase_blk_en : false; +} +//------------------------------------------------------------------------------ +/** + * Initialize an SD flash memory card. + * + * \param[in] sckRateID SPI clock rate selector. See setSckRate(). + * \param[in] chipSelectPin SD chip select pin number. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. The reason for failure + * can be determined by calling errorCode() and errorData(). + */ +bool Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) { + errorCode_ = type_ = 0; + chipSelectPin_ = chipSelectPin; + // 16-bit init start time allows over a minute + uint16_t t0 = (uint16_t)_millis(); + uint32_t arg; + + // set pin modes + pinMode(chipSelectPin_, OUTPUT); + chipSelectHigh(); + pinMode(SPI_MISO_PIN, INPUT); + pinMode(SPI_MOSI_PIN, OUTPUT); + pinMode(SPI_SCK_PIN, OUTPUT); + +#ifndef SOFTWARE_SPI + // SS must be in output mode even it is not chip select + pinMode(SS_PIN, OUTPUT); + // set SS high - may be chip select for another SPI device +#if SET_SPI_SS_HIGH + digitalWrite(SS_PIN, HIGH); +#endif // SET_SPI_SS_HIGH + // set SCK rate for initialization commands + spiRate_ = SPI_SD_INIT_RATE; + spiInit(spiRate_); +#endif // SOFTWARE_SPI + + // must supply min of 74 clock cycles with CS high. + for (uint8_t i = 0; i < 10; i++) spiSend(0XFF); + + // command to go idle in SPI mode + while ((status_ = cardCommand(CMD0, 0)) != R1_IDLE_STATE) { + if (((uint16_t)_millis() - t0) > SD_INIT_TIMEOUT) { + error(SD_CARD_ERROR_CMD0); + goto fail; + } + } + // check SD version + if ((cardCommand(CMD8, 0x1AA) & R1_ILLEGAL_COMMAND)) { + type(SD_CARD_TYPE_SD1); + } else { + // only need last byte of r7 response + for (uint8_t i = 0; i < 4; i++) status_ = spiRec(); + if (status_ != 0XAA) { + error(SD_CARD_ERROR_CMD8); + goto fail; + } + type(SD_CARD_TYPE_SD2); + } + // initialize card and send host supports SDHC if SD2 + arg = type() == SD_CARD_TYPE_SD2 ? 0X40000000 : 0; + + while ((status_ = cardAcmd(ACMD41, arg)) != R1_READY_STATE) { + // check for timeout + if (((uint16_t)_millis() - t0) > SD_INIT_TIMEOUT) { + error(SD_CARD_ERROR_ACMD41); + goto fail; + } + } + // if SD2 read OCR register to check for SDHC card + if (type() == SD_CARD_TYPE_SD2) { + if (cardCommand(CMD58, 0)) { + error(SD_CARD_ERROR_CMD58); + goto fail; + } + if ((spiRec() & 0XC0) == 0XC0) type(SD_CARD_TYPE_SDHC); + // discard rest of ocr - contains allowed voltage range + for (uint8_t i = 0; i < 3; i++) spiRec(); + } + chipSelectHigh(); + +#ifndef SOFTWARE_SPI + return setSckRate(sckRateID); +#else // SOFTWARE_SPI + return true; +#endif // SOFTWARE_SPI + + fail: + chipSelectHigh(); + return false; +} +//------------------------------------------------------------------------------ +/** + * Read a 512 byte block from an SD card. + * + * \param[in] blockNumber Logical block to be read. + * \param[out] dst Pointer to the location that will receive the data. + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool Sd2Card::readBlock(uint32_t blockNumber, uint8_t* dst) { +#ifdef SD_CHECK_AND_RETRY + uint8_t retryCnt = 3; + // use address if not SDHC card + if (type()!= SD_CARD_TYPE_SDHC) blockNumber <<= 9; + retry2: + retryCnt --; + if (cardCommand(CMD17, blockNumber)) { + error(SD_CARD_ERROR_CMD17); + if (retryCnt > 0) goto retry; + goto fail; + } + if (!readData(dst, 512)) + { + if (retryCnt > 0) goto retry; + goto fail; + } + return true; + retry: + chipSelectHigh(); + cardCommand(CMD12, 0);//Try sending a stop command, but ignore the result. + errorCode_ = 0; + goto retry2; +#else + // use address if not SDHC card + if (type()!= SD_CARD_TYPE_SDHC) blockNumber <<= 9; + if (cardCommand(CMD17, blockNumber)) { + error(SD_CARD_ERROR_CMD17); + goto fail; + } + return readData(dst, 512); +#endif + + fail: + chipSelectHigh(); + return false; +} +//------------------------------------------------------------------------------ +/** Read one data block in a multiple block read sequence + * + * \param[in] dst Pointer to the location for the data to be read. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool Sd2Card::readData(uint8_t *dst) { + chipSelectLow(); + return readData(dst, 512); +} + +#ifdef SD_CHECK_AND_RETRY +static const uint16_t crctab[] PROGMEM = { + 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, + 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, + 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6, + 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE, + 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485, + 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D, + 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4, + 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC, + 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823, + 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B, + 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12, + 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A, + 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41, + 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49, + 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70, + 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78, + 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F, + 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067, + 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E, + 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256, + 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D, + 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, + 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C, + 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634, + 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB, + 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3, + 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A, + 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92, + 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9, + 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1, + 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8, + 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0 +}; +static uint16_t CRC_CCITT(const uint8_t* data, size_t n) { + uint16_t crc = 0; + for (size_t i = 0; i < n; i++) { + crc = pgm_read_word(&crctab[(crc >> 8 ^ data[i]) & 0XFF]) ^ (crc << 8); + } + return crc; +} +#endif + +//------------------------------------------------------------------------------ +bool Sd2Card::readData(uint8_t* dst, uint16_t count) { + // wait for start block token + uint16_t t0 = _millis(); + while ((status_ = spiRec()) == 0XFF) { + if (((uint16_t)_millis() - t0) > SD_READ_TIMEOUT) { + error(SD_CARD_ERROR_READ_TIMEOUT); + goto fail; + } + } + if (status_ != DATA_START_BLOCK) { + error(SD_CARD_ERROR_READ); + goto fail; + } + // transfer data + spiRead(dst, count); + +#ifdef SD_CHECK_AND_RETRY + { + uint16_t calcCrc = CRC_CCITT(dst, count); + uint16_t recvCrc = spiRec() << 8; + recvCrc |= spiRec(); + if (calcCrc != recvCrc) + { + error(SD_CARD_ERROR_CRC); + goto fail; + } + } +#else + // discard CRC + spiRec(); + spiRec(); +#endif + chipSelectHigh(); + // Toshiba FlashAir Patch. Purge pending status byte. + if (flash_air_compatible_) + spiSend(0XFF); + return true; + + fail: + chipSelectHigh(); + // Toshiba FlashAir Patch. Purge pending status byte. + if (flash_air_compatible_) + spiSend(0XFF); + return false; +} +//------------------------------------------------------------------------------ +/** read CID or CSR register */ +bool Sd2Card::readRegister(uint8_t cmd, void* buf) { + uint8_t* dst = reinterpret_cast(buf); + if (cardCommand(cmd, 0)) { + error(SD_CARD_ERROR_READ_REG); + goto fail; + } + return readData(dst, 16); + + fail: + chipSelectHigh(); + return false; +} +//------------------------------------------------------------------------------ +/** Start a read multiple blocks sequence. + * + * \param[in] blockNumber Address of first block in sequence. + * + * \note This function is used with readData() and readStop() for optimized + * multiple block reads. SPI chipSelect must be low for the entire sequence. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool Sd2Card::readStart(uint32_t blockNumber) { + if (type()!= SD_CARD_TYPE_SDHC) blockNumber <<= 9; + if (cardCommand(CMD18, blockNumber)) { + error(SD_CARD_ERROR_CMD18); + goto fail; + } + chipSelectHigh(); + return true; + + fail: + chipSelectHigh(); + return false; +} +//------------------------------------------------------------------------------ +/** End a read multiple blocks sequence. + * +* \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool Sd2Card::readStop() { + chipSelectLow(); + if (cardCommand(CMD12, 0)) { + error(SD_CARD_ERROR_CMD12); + goto fail; + } + chipSelectHigh(); + return true; + + fail: + chipSelectHigh(); + return false; +} +//------------------------------------------------------------------------------ +/** + * Set the SPI clock rate. + * + * \param[in] sckRateID A value in the range [0, 6]. + * + * The SPI clock will be set to F_CPU/pow(2, 1 + sckRateID). The maximum + * SPI rate is F_CPU/2 for \a sckRateID = 0 and the minimum rate is F_CPU/128 + * for \a scsRateID = 6. + * + * \return The value one, true, is returned for success and the value zero, + * false, is returned for an invalid value of \a sckRateID. + */ +bool Sd2Card::setSckRate(uint8_t sckRateID) { + if (sckRateID > 6) { + error(SD_CARD_ERROR_SCK_RATE); + return false; + } + spiRate_ = sckRateID; + return true; +} +//------------------------------------------------------------------------------ +// wait for card to go not busy +bool Sd2Card::waitNotBusy(uint16_t timeoutMillis) { + uint16_t t0 = _millis(); + while (spiRec() != 0XFF) { + if (((uint16_t)_millis() - t0) >= timeoutMillis) goto fail; + } + return true; + + fail: + return false; +} +//------------------------------------------------------------------------------ +/** + * Writes a 512 byte block to an SD card. + * + * \param[in] blockNumber Logical block to be written. + * \param[in] src Pointer to the location of the data to be written. + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) { + // use address if not SDHC card + if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9; + if (cardCommand(CMD24, blockNumber)) { + error(SD_CARD_ERROR_CMD24); + goto fail; + } + if (!writeData(DATA_START_BLOCK, src)) goto fail; + + // wait for flash programming to complete + if (!waitNotBusy(SD_WRITE_TIMEOUT)) { + error(SD_CARD_ERROR_WRITE_TIMEOUT); + goto fail; + } + // response is r2 so get and check two bytes for nonzero + if (cardCommand(CMD13, 0) || spiRec()) { + error(SD_CARD_ERROR_WRITE_PROGRAMMING); + goto fail; + } + chipSelectHigh(); + return true; + + fail: + chipSelectHigh(); + return false; +} +//------------------------------------------------------------------------------ +/** Write one data block in a multiple block write sequence + * \param[in] src Pointer to the location of the data to be written. + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool Sd2Card::writeData(const uint8_t* src) { + chipSelectLow(); + // wait for previous write to finish + if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail; + if (!writeData(WRITE_MULTIPLE_TOKEN, src)) goto fail; + chipSelectHigh(); + return true; + + fail: + error(SD_CARD_ERROR_WRITE_MULTIPLE); + chipSelectHigh(); + return false; +} +//------------------------------------------------------------------------------ +// send one block of data for write block or write multiple blocks +bool Sd2Card::writeData(uint8_t token, const uint8_t* src) { + spiSendBlock(token, src); + + spiSend(0xff); // dummy crc + spiSend(0xff); // dummy crc + + status_ = spiRec(); + if ((status_ & DATA_RES_MASK) != DATA_RES_ACCEPTED) { + error(SD_CARD_ERROR_WRITE); + goto fail; + } + return true; + + fail: + chipSelectHigh(); + return false; +} +//------------------------------------------------------------------------------ +/** Start a write multiple blocks sequence. + * + * \param[in] blockNumber Address of first block in sequence. + * \param[in] eraseCount The number of blocks to be pre-erased. + * + * \note This function is used with writeData() and writeStop() + * for optimized multiple block writes. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool Sd2Card::writeStart(uint32_t blockNumber, uint32_t eraseCount) { + // send pre-erase count + if (cardAcmd(ACMD23, eraseCount)) { + error(SD_CARD_ERROR_ACMD23); + goto fail; + } + // use address if not SDHC card + if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9; + if (cardCommand(CMD25, blockNumber)) { + error(SD_CARD_ERROR_CMD25); + goto fail; + } + chipSelectHigh(); + return true; + + fail: + chipSelectHigh(); + return false; +} +//------------------------------------------------------------------------------ +/** End a write multiple blocks sequence. + * +* \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool Sd2Card::writeStop() { + chipSelectLow(); + if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail; + spiSend(STOP_TRAN_TOKEN); + if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail; + chipSelectHigh(); + return true; + + fail: + error(SD_CARD_ERROR_STOP_TRAN); + chipSelectHigh(); + return false; +} + +//------------------------------------------------------------------------------ +/** Wait for start block token */ +//FIXME Vojtech: Copied from a current version of Sd2Card Arduino code. +// We shall likely upgrade the rest of the Sd2Card. +uint8_t Sd2Card::waitStartBlock(void) { + uint16_t t0 = _millis(); + while ((status_ = spiRec()) == 0XFF) { + if (((uint16_t)_millis() - t0) > SD_READ_TIMEOUT) { + error(SD_CARD_ERROR_READ_TIMEOUT); + goto fail; + } + } + if (status_ != DATA_START_BLOCK) { + error(SD_CARD_ERROR_READ); + goto fail; + } + return true; + + fail: + chipSelectHigh(); + return false; +} + +// Toshiba FlashAir support, copied from +// https://flashair-developers.com/en/documents/tutorials/arduino/ + +//------------------------------------------------------------------------------ +/** Perform Extention Read. */ +uint8_t Sd2Card::readExt(uint32_t arg, uint8_t* dst, uint16_t count) { + uint16_t i; + + // send command and argument. + if (cardCommand(CMD48, arg)) { + error(SD_CARD_ERROR_CMD48); + goto fail; + } + + // wait for start block token. + if (!waitStartBlock()) { + goto fail; + } + + // receive data + for (i = 0; i < count; ++i) { + dst[i] = spiRec(); + } + + // skip dummy bytes and 16-bit crc. + for (; i < 514; ++i) { + spiRec(); + } + + chipSelectHigh(); + spiSend(0xFF); // dummy clock to force FlashAir finish the command. + return true; + + fail: + chipSelectHigh(); + return false; +} + +//------------------------------------------------------------------------------ +/** + * Read an extension register space. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +uint8_t Sd2Card::readExtMemory(uint8_t mio, uint8_t func, + uint32_t addr, uint16_t count, uint8_t* dst) { + uint32_t offset = addr & 0x1FF; + if (offset + count > 512) count = 512 - offset; + + if (count == 0) return true; + + uint32_t arg = + (((uint32_t)mio & 0x1) << 31) | + (mio ? (((uint32_t)func & 0x7) << 28) : (((uint32_t)func & 0xF) << 27)) | + ((addr & 0x1FFFF) << 9) | + ((count - 1) & 0x1FF); + + return readExt(arg, dst, count); +} + +#endif diff --git a/Firmware/Sd2Card.h b/Firmware/Sd2Card.h new file mode 100644 index 0000000..537d249 --- /dev/null +++ b/Firmware/Sd2Card.h @@ -0,0 +1,262 @@ +/* Arduino Sd2Card Library + * Copyright (C) 2009 by William Greiman + * + * This file is part of the Arduino Sd2Card Library + * + * This Library 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 Library 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 the Arduino Sd2Card Library. If not, see + * . + */ + +#include "Marlin.h" +#ifdef SDSUPPORT + +#ifndef Sd2Card_h +#define Sd2Card_h +/** + * \file + * \brief Sd2Card class for V2 SD/SDHC cards + */ +#include "SdFatConfig.h" +#include "Sd2PinMap.h" +#include "SdInfo.h" +//------------------------------------------------------------------------------ +// SPI speed is F_CPU/2^(1 + index), 0 <= index <= 6 +/** Set SCK to max rate of F_CPU/2. See Sd2Card::setSckRate(). */ +uint8_t const SPI_FULL_SPEED = 0; +/** Set SCK rate to F_CPU/4. See Sd2Card::setSckRate(). */ +uint8_t const SPI_HALF_SPEED = 1; +/** Set SCK rate to F_CPU/8. See Sd2Card::setSckRate(). */ +uint8_t const SPI_QUARTER_SPEED = 2; +/** Set SCK rate to F_CPU/16. See Sd2Card::setSckRate(). */ +uint8_t const SPI_EIGHTH_SPEED = 3; +/** Set SCK rate to F_CPU/32. See Sd2Card::setSckRate(). */ +uint8_t const SPI_SIXTEENTH_SPEED = 4; +//------------------------------------------------------------------------------ +/** init timeout ms */ +uint16_t const SD_INIT_TIMEOUT = 2000; +/** erase timeout ms */ +uint16_t const SD_ERASE_TIMEOUT = 10000; +/** read timeout ms */ +uint16_t const SD_READ_TIMEOUT = 300; +/** write time out ms */ +uint16_t const SD_WRITE_TIMEOUT = 600; +//------------------------------------------------------------------------------ +// SD card errors +/** timeout error for command CMD0 (initialize card in SPI mode) */ +uint8_t const SD_CARD_ERROR_CMD0 = 0X1; +/** CMD8 was not accepted - not a valid SD card*/ +uint8_t const SD_CARD_ERROR_CMD8 = 0X2; +/** card returned an error response for CMD12 (write stop) */ +uint8_t const SD_CARD_ERROR_CMD12 = 0X3; +/** card returned an error response for CMD17 (read block) */ +uint8_t const SD_CARD_ERROR_CMD17 = 0X4; +/** card returned an error response for CMD18 (read multiple block) */ +uint8_t const SD_CARD_ERROR_CMD18 = 0X5; +/** card returned an error response for CMD24 (write block) */ +uint8_t const SD_CARD_ERROR_CMD24 = 0X6; +/** WRITE_MULTIPLE_BLOCKS command failed */ +uint8_t const SD_CARD_ERROR_CMD25 = 0X7; +/** card returned an error response for CMD58 (read OCR) */ +uint8_t const SD_CARD_ERROR_CMD58 = 0X8; +/** SET_WR_BLK_ERASE_COUNT failed */ +uint8_t const SD_CARD_ERROR_ACMD23 = 0X9; +/** ACMD41 initialization process timeout */ +uint8_t const SD_CARD_ERROR_ACMD41 = 0XA; +/** card returned a bad CSR version field */ +uint8_t const SD_CARD_ERROR_BAD_CSD = 0XB; +/** erase block group command failed */ +uint8_t const SD_CARD_ERROR_ERASE = 0XC; +/** card not capable of single block erase */ +uint8_t const SD_CARD_ERROR_ERASE_SINGLE_BLOCK = 0XD; +/** Erase sequence timed out */ +uint8_t const SD_CARD_ERROR_ERASE_TIMEOUT = 0XE; +/** card returned an error token instead of read data */ +uint8_t const SD_CARD_ERROR_READ = 0XF; +/** read CID or CSD failed */ +uint8_t const SD_CARD_ERROR_READ_REG = 0X10; +/** timeout while waiting for start of read data */ +uint8_t const SD_CARD_ERROR_READ_TIMEOUT = 0X11; +/** card did not accept STOP_TRAN_TOKEN */ +uint8_t const SD_CARD_ERROR_STOP_TRAN = 0X12; +/** card returned an error token as a response to a write operation */ +uint8_t const SD_CARD_ERROR_WRITE = 0X13; +/** attempt to write protected block zero */ +uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0X14; // REMOVE - not used +/** card did not go ready for a multiple block write */ +uint8_t const SD_CARD_ERROR_WRITE_MULTIPLE = 0X15; +/** card returned an error to a CMD13 status check after a write */ +uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING = 0X16; +/** timeout occurred during write programming */ +uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT = 0X17; +/** incorrect rate selected */ +uint8_t const SD_CARD_ERROR_SCK_RATE = 0X18; +/** init() not called */ +uint8_t const SD_CARD_ERROR_INIT_NOT_CALLED = 0X19; +/** crc check error */ +uint8_t const SD_CARD_ERROR_CRC = 0X20; + +/** Toshiba FlashAir: iSDIO */ +uint8_t const SD_CARD_ERROR_CMD48 = 0x80; +/** Toshiba FlashAir: iSDIO */ +uint8_t const SD_CARD_ERROR_CMD49 = 0x81; + +//------------------------------------------------------------------------------ +// card types +/** Standard capacity V1 SD card */ +uint8_t const SD_CARD_TYPE_SD1 = 1; +/** Standard capacity V2 SD card */ +uint8_t const SD_CARD_TYPE_SD2 = 2; +/** High Capacity SD card */ +uint8_t const SD_CARD_TYPE_SDHC = 3; +/** + * define SOFTWARE_SPI to use bit-bang SPI + */ +//------------------------------------------------------------------------------ +#if MEGA_SOFT_SPI && (defined(__AVR_ATmega1280__)||defined(__AVR_ATmega2560__)) +#define SOFTWARE_SPI +#elif USE_SOFTWARE_SPI +#define SOFTWARE_SPI +#endif // MEGA_SOFT_SPI +//------------------------------------------------------------------------------ +// SPI pin definitions - do not edit here - change in SdFatConfig.h +// +#ifndef SOFTWARE_SPI +// hardware pin defs +/** The default chip select pin for the SD card is SS. */ +uint8_t const SD_CHIP_SELECT_PIN = SS_PIN; +// The following three pins must not be redefined for hardware SPI. +/** SPI Master Out Slave In pin */ +uint8_t const SPI_MOSI_PIN = MOSI_PIN; +/** SPI Master In Slave Out pin */ +uint8_t const SPI_MISO_PIN = MISO_PIN; +/** SPI Clock pin */ +uint8_t const SPI_SCK_PIN = SCK_PIN; + +#else // SOFTWARE_SPI + +/** SPI chip select pin */ +uint8_t const SD_CHIP_SELECT_PIN = SOFT_SPI_CS_PIN; +/** SPI Master Out Slave In pin */ +uint8_t const SPI_MOSI_PIN = SOFT_SPI_MOSI_PIN; +/** SPI Master In Slave Out pin */ +uint8_t const SPI_MISO_PIN = SOFT_SPI_MISO_PIN; +/** SPI Clock pin */ +uint8_t const SPI_SCK_PIN = SOFT_SPI_SCK_PIN; +#endif // SOFTWARE_SPI +//------------------------------------------------------------------------------ +/** + * \class Sd2Card + * \brief Raw access to SD and SDHC flash memory cards. + */ +class Sd2Card { + public: + /** Construct an instance of Sd2Card. */ + Sd2Card() : errorCode_(SD_CARD_ERROR_INIT_NOT_CALLED), type_(0), flash_air_compatible_(false) {} + uint32_t cardSize(); + bool erase(uint32_t firstBlock, uint32_t lastBlock); + bool eraseSingleBlockEnable(); + /** + * Set SD error code. + * \param[in] code value for error code. + */ + void error(uint8_t code) {errorCode_ = code;} + /** + * \return error code for last error. See Sd2Card.h for a list of error codes. + */ + int errorCode() const {return errorCode_;} + /** \return error data for last error. */ + int errorData() const {return status_;} + /** + * Initialize an SD flash memory card with default clock rate and chip + * select pin. See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin). + * + * \return true for success or false for failure. + */ + bool init(uint8_t sckRateID = SPI_FULL_SPEED, + uint8_t chipSelectPin = SD_CHIP_SELECT_PIN); + bool readBlock(uint32_t block, uint8_t* dst); + /** + * Read a card's CID register. The CID contains card identification + * information such as Manufacturer ID, Product name, Product serial + * number and Manufacturing date. + * + * \param[out] cid pointer to area for returned data. + * + * \return true for success or false for failure. + */ + bool readCID(cid_t* cid) { + return readRegister(CMD10, cid); + } + /** + * Read a card's CSD register. The CSD contains Card-Specific Data that + * provides information regarding access to the card's contents. + * + * \param[out] csd pointer to area for returned data. + * + * \return true for success or false for failure. + */ + bool readCSD(csd_t* csd) { + return readRegister(CMD9, csd); + } + bool readData(uint8_t *dst); + bool readStart(uint32_t blockNumber); + bool readStop(); + bool setSckRate(uint8_t sckRateID); + /** Return the card type: SD V1, SD V2 or SDHC + * \return 0 - SD V1, 1 - SD V2, or 3 - SDHC. + */ + int type() const {return type_;} + bool writeBlock(uint32_t blockNumber, const uint8_t* src); + bool writeData(const uint8_t* src); + bool writeStart(uint32_t blockNumber, uint32_t eraseCount); + bool writeStop(); + + // Toshiba FlashAir support + uint8_t readExtMemory(uint8_t mio, uint8_t func, uint32_t addr, uint16_t count, uint8_t* dst); + + void setFlashAirCompatible(bool flashAirCompatible) { flash_air_compatible_ = flashAirCompatible; } + bool getFlashAirCompatible() const { return flash_air_compatible_; } + + private: + //---------------------------------------------------------------------------- + uint8_t chipSelectPin_; + uint8_t errorCode_; + uint8_t spiRate_; + uint8_t status_; + uint8_t type_; + bool flash_air_compatible_; + // private functions + uint8_t cardAcmd(uint8_t cmd, uint32_t arg) { + cardCommand(CMD55, 0); + return cardCommand(cmd, arg); + } + uint8_t cardCommand(uint8_t cmd, uint32_t arg); + + bool readData(uint8_t* dst, uint16_t count); + bool readRegister(uint8_t cmd, void* buf); + void chipSelectHigh(); + void chipSelectLow(); + void type(uint8_t value) {type_ = value;} + bool waitNotBusy(uint16_t timeoutMillis); + bool writeData(uint8_t token, const uint8_t* src); + + + // Toshiba FlashAir support + uint8_t waitStartBlock(void); + uint8_t readExt(uint32_t arg, uint8_t* dst, uint16_t count); +}; +#endif // Sd2Card_h + + +#endif \ No newline at end of file diff --git a/Firmware/Sd2PinMap.h b/Firmware/Sd2PinMap.h new file mode 100644 index 0000000..8a60873 --- /dev/null +++ b/Firmware/Sd2PinMap.h @@ -0,0 +1,368 @@ +/* Arduino SdFat Library + * Copyright (C) 2010 by William Greiman + * + * This file is part of the Arduino SdFat Library + * + * This Library 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 Library 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 the Arduino SdFat Library. If not, see + * . + */ +// Warning this file was generated by a program. +#include "Marlin.h" +#ifdef SDSUPPORT + +#ifndef Sd2PinMap_h +#define Sd2PinMap_h +#include +//------------------------------------------------------------------------------ +/** struct for mapping digital pins */ +struct pin_map_t { + volatile uint8_t* ddr; + volatile uint8_t* pin; + volatile uint8_t* port; + uint8_t bit; +}; +//------------------------------------------------------------------------------ +#if defined(__AVR_ATmega1280__)\ +|| defined(__AVR_ATmega2560__) +// Mega + +// Two Wire (aka I2C) ports +uint8_t const SDA_PIN = 20; // D1 +uint8_t const SCL_PIN = 21; // D0 + +#undef MOSI_PIN +#undef MISO_PIN +// SPI port +uint8_t const SS_PIN = 53; // B0 +uint8_t const MOSI_PIN = 51; // B2 +uint8_t const MISO_PIN = 50; // B3 +uint8_t const SCK_PIN = 52; // B1 + +static const pin_map_t digitalPinMap[] = { + {&DDRE, &PINE, &PORTE, 0}, // E0 0 + {&DDRE, &PINE, &PORTE, 1}, // E1 1 + {&DDRE, &PINE, &PORTE, 4}, // E4 2 + {&DDRE, &PINE, &PORTE, 5}, // E5 3 + {&DDRG, &PING, &PORTG, 5}, // G5 4 + {&DDRE, &PINE, &PORTE, 3}, // E3 5 + {&DDRH, &PINH, &PORTH, 3}, // H3 6 + {&DDRH, &PINH, &PORTH, 4}, // H4 7 + {&DDRH, &PINH, &PORTH, 5}, // H5 8 + {&DDRH, &PINH, &PORTH, 6}, // H6 9 + {&DDRB, &PINB, &PORTB, 4}, // B4 10 + {&DDRB, &PINB, &PORTB, 5}, // B5 11 + {&DDRB, &PINB, &PORTB, 6}, // B6 12 + {&DDRB, &PINB, &PORTB, 7}, // B7 13 + {&DDRJ, &PINJ, &PORTJ, 1}, // J1 14 + {&DDRJ, &PINJ, &PORTJ, 0}, // J0 15 + {&DDRH, &PINH, &PORTH, 1}, // H1 16 + {&DDRH, &PINH, &PORTH, 0}, // H0 17 + {&DDRD, &PIND, &PORTD, 3}, // D3 18 + {&DDRD, &PIND, &PORTD, 2}, // D2 19 + {&DDRD, &PIND, &PORTD, 1}, // D1 20 + {&DDRD, &PIND, &PORTD, 0}, // D0 21 + {&DDRA, &PINA, &PORTA, 0}, // A0 22 + {&DDRA, &PINA, &PORTA, 1}, // A1 23 + {&DDRA, &PINA, &PORTA, 2}, // A2 24 + {&DDRA, &PINA, &PORTA, 3}, // A3 25 + {&DDRA, &PINA, &PORTA, 4}, // A4 26 + {&DDRA, &PINA, &PORTA, 5}, // A5 27 + {&DDRA, &PINA, &PORTA, 6}, // A6 28 + {&DDRA, &PINA, &PORTA, 7}, // A7 29 + {&DDRC, &PINC, &PORTC, 7}, // C7 30 + {&DDRC, &PINC, &PORTC, 6}, // C6 31 + {&DDRC, &PINC, &PORTC, 5}, // C5 32 + {&DDRC, &PINC, &PORTC, 4}, // C4 33 + {&DDRC, &PINC, &PORTC, 3}, // C3 34 + {&DDRC, &PINC, &PORTC, 2}, // C2 35 + {&DDRC, &PINC, &PORTC, 1}, // C1 36 + {&DDRC, &PINC, &PORTC, 0}, // C0 37 + {&DDRD, &PIND, &PORTD, 7}, // D7 38 + {&DDRG, &PING, &PORTG, 2}, // G2 39 + {&DDRG, &PING, &PORTG, 1}, // G1 40 + {&DDRG, &PING, &PORTG, 0}, // G0 41 + {&DDRL, &PINL, &PORTL, 7}, // L7 42 + {&DDRL, &PINL, &PORTL, 6}, // L6 43 + {&DDRL, &PINL, &PORTL, 5}, // L5 44 + {&DDRL, &PINL, &PORTL, 4}, // L4 45 + {&DDRL, &PINL, &PORTL, 3}, // L3 46 + {&DDRL, &PINL, &PORTL, 2}, // L2 47 + {&DDRL, &PINL, &PORTL, 1}, // L1 48 + {&DDRL, &PINL, &PORTL, 0}, // L0 49 + {&DDRB, &PINB, &PORTB, 3}, // B3 50 + {&DDRB, &PINB, &PORTB, 2}, // B2 51 + {&DDRB, &PINB, &PORTB, 1}, // B1 52 + {&DDRB, &PINB, &PORTB, 0}, // B0 53 + {&DDRF, &PINF, &PORTF, 0}, // F0 54 + {&DDRF, &PINF, &PORTF, 1}, // F1 55 + {&DDRF, &PINF, &PORTF, 2}, // F2 56 + {&DDRF, &PINF, &PORTF, 3}, // F3 57 + {&DDRF, &PINF, &PORTF, 4}, // F4 58 + {&DDRF, &PINF, &PORTF, 5}, // F5 59 + {&DDRF, &PINF, &PORTF, 6}, // F6 60 + {&DDRF, &PINF, &PORTF, 7}, // F7 61 + {&DDRK, &PINK, &PORTK, 0}, // K0 62 + {&DDRK, &PINK, &PORTK, 1}, // K1 63 + {&DDRK, &PINK, &PORTK, 2}, // K2 64 + {&DDRK, &PINK, &PORTK, 3}, // K3 65 + {&DDRK, &PINK, &PORTK, 4}, // K4 66 + {&DDRK, &PINK, &PORTK, 5}, // K5 67 + {&DDRK, &PINK, &PORTK, 6}, // K6 68 + {&DDRK, &PINK, &PORTK, 7} // K7 69 +}; +//------------------------------------------------------------------------------ +#elif defined(__AVR_ATmega644P__)\ +|| defined(__AVR_ATmega644__)\ +|| defined(__AVR_ATmega1284P__) +// Sanguino + +// Two Wire (aka I2C) ports +uint8_t const SDA_PIN = 17; // C1 +uint8_t const SCL_PIN = 18; // C2 + +// SPI port +uint8_t const SS_PIN = 4; // B4 +uint8_t const MOSI_PIN = 5; // B5 +uint8_t const MISO_PIN = 6; // B6 +uint8_t const SCK_PIN = 7; // B7 + +static const pin_map_t digitalPinMap[] = { + {&DDRB, &PINB, &PORTB, 0}, // B0 0 + {&DDRB, &PINB, &PORTB, 1}, // B1 1 + {&DDRB, &PINB, &PORTB, 2}, // B2 2 + {&DDRB, &PINB, &PORTB, 3}, // B3 3 + {&DDRB, &PINB, &PORTB, 4}, // B4 4 + {&DDRB, &PINB, &PORTB, 5}, // B5 5 + {&DDRB, &PINB, &PORTB, 6}, // B6 6 + {&DDRB, &PINB, &PORTB, 7}, // B7 7 + {&DDRD, &PIND, &PORTD, 0}, // D0 8 + {&DDRD, &PIND, &PORTD, 1}, // D1 9 + {&DDRD, &PIND, &PORTD, 2}, // D2 10 + {&DDRD, &PIND, &PORTD, 3}, // D3 11 + {&DDRD, &PIND, &PORTD, 4}, // D4 12 + {&DDRD, &PIND, &PORTD, 5}, // D5 13 + {&DDRD, &PIND, &PORTD, 6}, // D6 14 + {&DDRD, &PIND, &PORTD, 7}, // D7 15 + {&DDRC, &PINC, &PORTC, 0}, // C0 16 + {&DDRC, &PINC, &PORTC, 1}, // C1 17 + {&DDRC, &PINC, &PORTC, 2}, // C2 18 + {&DDRC, &PINC, &PORTC, 3}, // C3 19 + {&DDRC, &PINC, &PORTC, 4}, // C4 20 + {&DDRC, &PINC, &PORTC, 5}, // C5 21 + {&DDRC, &PINC, &PORTC, 6}, // C6 22 + {&DDRC, &PINC, &PORTC, 7}, // C7 23 + {&DDRA, &PINA, &PORTA, 7}, // A7 24 + {&DDRA, &PINA, &PORTA, 6}, // A6 25 + {&DDRA, &PINA, &PORTA, 5}, // A5 26 + {&DDRA, &PINA, &PORTA, 4}, // A4 27 + {&DDRA, &PINA, &PORTA, 3}, // A3 28 + {&DDRA, &PINA, &PORTA, 2}, // A2 29 + {&DDRA, &PINA, &PORTA, 1}, // A1 30 + {&DDRA, &PINA, &PORTA, 0} // A0 31 +}; +//------------------------------------------------------------------------------ +#elif defined(__AVR_ATmega32U4__) +// Teensy 2.0 + +// Two Wire (aka I2C) ports +uint8_t const SDA_PIN = 6; // D1 +uint8_t const SCL_PIN = 5; // D0 + +// SPI port +uint8_t const SS_PIN = 0; // B0 +uint8_t const MOSI_PIN = 2; // B2 +uint8_t const MISO_PIN = 3; // B3 +uint8_t const SCK_PIN = 1; // B1 + +static const pin_map_t digitalPinMap[] = { + {&DDRB, &PINB, &PORTB, 0}, // B0 0 + {&DDRB, &PINB, &PORTB, 1}, // B1 1 + {&DDRB, &PINB, &PORTB, 2}, // B2 2 + {&DDRB, &PINB, &PORTB, 3}, // B3 3 + {&DDRB, &PINB, &PORTB, 7}, // B7 4 + {&DDRD, &PIND, &PORTD, 0}, // D0 5 + {&DDRD, &PIND, &PORTD, 1}, // D1 6 + {&DDRD, &PIND, &PORTD, 2}, // D2 7 + {&DDRD, &PIND, &PORTD, 3}, // D3 8 + {&DDRC, &PINC, &PORTC, 6}, // C6 9 + {&DDRC, &PINC, &PORTC, 7}, // C7 10 + {&DDRD, &PIND, &PORTD, 6}, // D6 11 + {&DDRD, &PIND, &PORTD, 7}, // D7 12 + {&DDRB, &PINB, &PORTB, 4}, // B4 13 + {&DDRB, &PINB, &PORTB, 5}, // B5 14 + {&DDRB, &PINB, &PORTB, 6}, // B6 15 + {&DDRF, &PINF, &PORTF, 7}, // F7 16 + {&DDRF, &PINF, &PORTF, 6}, // F6 17 + {&DDRF, &PINF, &PORTF, 5}, // F5 18 + {&DDRF, &PINF, &PORTF, 4}, // F4 19 + {&DDRF, &PINF, &PORTF, 1}, // F1 20 + {&DDRF, &PINF, &PORTF, 0}, // F0 21 + {&DDRD, &PIND, &PORTD, 4}, // D4 22 + {&DDRD, &PIND, &PORTD, 5}, // D5 23 + {&DDRE, &PINE, &PORTE, 6} // E6 24 +}; +//------------------------------------------------------------------------------ +#elif defined(__AVR_AT90USB646__)\ +|| defined(__AVR_AT90USB1286__) +// Teensy++ 1.0 & 2.0 + +// Two Wire (aka I2C) ports +uint8_t const SDA_PIN = 1; // D1 +uint8_t const SCL_PIN = 0; // D0 + +// SPI port +uint8_t const SS_PIN = 20; // B0 +uint8_t const MOSI_PIN = 22; // B2 +uint8_t const MISO_PIN = 23; // B3 +uint8_t const SCK_PIN = 21; // B1 + +static const pin_map_t digitalPinMap[] = { + {&DDRD, &PIND, &PORTD, 0}, // D0 0 + {&DDRD, &PIND, &PORTD, 1}, // D1 1 + {&DDRD, &PIND, &PORTD, 2}, // D2 2 + {&DDRD, &PIND, &PORTD, 3}, // D3 3 + {&DDRD, &PIND, &PORTD, 4}, // D4 4 + {&DDRD, &PIND, &PORTD, 5}, // D5 5 + {&DDRD, &PIND, &PORTD, 6}, // D6 6 + {&DDRD, &PIND, &PORTD, 7}, // D7 7 + {&DDRE, &PINE, &PORTE, 0}, // E0 8 + {&DDRE, &PINE, &PORTE, 1}, // E1 9 + {&DDRC, &PINC, &PORTC, 0}, // C0 10 + {&DDRC, &PINC, &PORTC, 1}, // C1 11 + {&DDRC, &PINC, &PORTC, 2}, // C2 12 + {&DDRC, &PINC, &PORTC, 3}, // C3 13 + {&DDRC, &PINC, &PORTC, 4}, // C4 14 + {&DDRC, &PINC, &PORTC, 5}, // C5 15 + {&DDRC, &PINC, &PORTC, 6}, // C6 16 + {&DDRC, &PINC, &PORTC, 7}, // C7 17 + {&DDRE, &PINE, &PORTE, 6}, // E6 18 + {&DDRE, &PINE, &PORTE, 7}, // E7 19 + {&DDRB, &PINB, &PORTB, 0}, // B0 20 + {&DDRB, &PINB, &PORTB, 1}, // B1 21 + {&DDRB, &PINB, &PORTB, 2}, // B2 22 + {&DDRB, &PINB, &PORTB, 3}, // B3 23 + {&DDRB, &PINB, &PORTB, 4}, // B4 24 + {&DDRB, &PINB, &PORTB, 5}, // B5 25 + {&DDRB, &PINB, &PORTB, 6}, // B6 26 + {&DDRB, &PINB, &PORTB, 7}, // B7 27 + {&DDRA, &PINA, &PORTA, 0}, // A0 28 + {&DDRA, &PINA, &PORTA, 1}, // A1 29 + {&DDRA, &PINA, &PORTA, 2}, // A2 30 + {&DDRA, &PINA, &PORTA, 3}, // A3 31 + {&DDRA, &PINA, &PORTA, 4}, // A4 32 + {&DDRA, &PINA, &PORTA, 5}, // A5 33 + {&DDRA, &PINA, &PORTA, 6}, // A6 34 + {&DDRA, &PINA, &PORTA, 7}, // A7 35 + {&DDRE, &PINE, &PORTE, 4}, // E4 36 + {&DDRE, &PINE, &PORTE, 5}, // E5 37 + {&DDRF, &PINF, &PORTF, 0}, // F0 38 + {&DDRF, &PINF, &PORTF, 1}, // F1 39 + {&DDRF, &PINF, &PORTF, 2}, // F2 40 + {&DDRF, &PINF, &PORTF, 3}, // F3 41 + {&DDRF, &PINF, &PORTF, 4}, // F4 42 + {&DDRF, &PINF, &PORTF, 5}, // F5 43 + {&DDRF, &PINF, &PORTF, 6}, // F6 44 + {&DDRF, &PINF, &PORTF, 7} // F7 45 +}; +//------------------------------------------------------------------------------ +#elif defined(__AVR_ATmega168__)\ +||defined(__AVR_ATmega168P__)\ +||defined(__AVR_ATmega328P__) +// 168 and 328 Arduinos + +// Two Wire (aka I2C) ports +uint8_t const SDA_PIN = 18; // C4 +uint8_t const SCL_PIN = 19; // C5 + +// SPI port +uint8_t const SS_PIN = 10; // B2 +uint8_t const MOSI_PIN = 11; // B3 +uint8_t const MISO_PIN = 12; // B4 +uint8_t const SCK_PIN = 13; // B5 + +static const pin_map_t digitalPinMap[] = { + {&DDRD, &PIND, &PORTD, 0}, // D0 0 + {&DDRD, &PIND, &PORTD, 1}, // D1 1 + {&DDRD, &PIND, &PORTD, 2}, // D2 2 + {&DDRD, &PIND, &PORTD, 3}, // D3 3 + {&DDRD, &PIND, &PORTD, 4}, // D4 4 + {&DDRD, &PIND, &PORTD, 5}, // D5 5 + {&DDRD, &PIND, &PORTD, 6}, // D6 6 + {&DDRD, &PIND, &PORTD, 7}, // D7 7 + {&DDRB, &PINB, &PORTB, 0}, // B0 8 + {&DDRB, &PINB, &PORTB, 1}, // B1 9 + {&DDRB, &PINB, &PORTB, 2}, // B2 10 + {&DDRB, &PINB, &PORTB, 3}, // B3 11 + {&DDRB, &PINB, &PORTB, 4}, // B4 12 + {&DDRB, &PINB, &PORTB, 5}, // B5 13 + {&DDRC, &PINC, &PORTC, 0}, // C0 14 + {&DDRC, &PINC, &PORTC, 1}, // C1 15 + {&DDRC, &PINC, &PORTC, 2}, // C2 16 + {&DDRC, &PINC, &PORTC, 3}, // C3 17 + {&DDRC, &PINC, &PORTC, 4}, // C4 18 + {&DDRC, &PINC, &PORTC, 5} // C5 19 +}; +#else // defined(__AVR_ATmega1280__) +#error unknown chip +#endif // defined(__AVR_ATmega1280__) +//------------------------------------------------------------------------------ +static const uint8_t digitalPinCount = sizeof(digitalPinMap)/sizeof(pin_map_t); + +uint8_t badPinNumber(void) + __attribute__((error("Pin number is too large or not a constant"))); + +static inline __attribute__((always_inline)) + bool getPinMode(uint8_t pin) { + if (__builtin_constant_p(pin) && pin < digitalPinCount) { + return (*digitalPinMap[pin].ddr >> digitalPinMap[pin].bit) & 1; + } else { + return badPinNumber(); + } +} +static inline __attribute__((always_inline)) + void setPinMode(uint8_t pin, uint8_t mode) { + if (__builtin_constant_p(pin) && pin < digitalPinCount) { + if (mode) { + *digitalPinMap[pin].ddr |= 1 << digitalPinMap[pin].bit; + } else { + *digitalPinMap[pin].ddr &= ~(1 << digitalPinMap[pin].bit); + } + } else { + badPinNumber(); + } +} +static inline __attribute__((always_inline)) + bool fastDigitalRead(uint8_t pin) { + if (__builtin_constant_p(pin) && pin < digitalPinCount) { + return (*digitalPinMap[pin].pin >> digitalPinMap[pin].bit) & 1; + } else { + return badPinNumber(); + } +} +static inline __attribute__((always_inline)) + void fastDigitalWrite(uint8_t pin, uint8_t value) { + if (__builtin_constant_p(pin) && pin < digitalPinCount) { + if (value) { + *digitalPinMap[pin].port |= 1 << digitalPinMap[pin].bit; + } else { + *digitalPinMap[pin].port &= ~(1 << digitalPinMap[pin].bit); + } + } else { + badPinNumber(); + } +} +#endif // Sd2PinMap_h + + +#endif \ No newline at end of file diff --git a/Firmware/SdBaseFile.cpp b/Firmware/SdBaseFile.cpp new file mode 100644 index 0000000..b9e881e --- /dev/null +++ b/Firmware/SdBaseFile.cpp @@ -0,0 +1,1828 @@ +/* Arduino SdFat Library + * Copyright (C) 2009 by William Greiman + * + * This file is part of the Arduino SdFat Library + * + * This Library 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 Library 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 the Arduino SdFat Library. If not, see + * . + */ + +#include "Marlin.h" +#ifdef SDSUPPORT + +#include "SdBaseFile.h" +//------------------------------------------------------------------------------ +// pointer to cwd directory +SdBaseFile* SdBaseFile::cwd_ = 0; +// callback function for date/time +void (*SdBaseFile::dateTime_)(uint16_t* date, uint16_t* time) = 0; +//------------------------------------------------------------------------------ +// add a cluster to a file +bool SdBaseFile::addCluster() { + if (!vol_->allocContiguous(1, &curCluster_)) goto fail; + + // if first cluster of file link to directory entry + if (firstCluster_ == 0) { + firstCluster_ = curCluster_; + flags_ |= F_FILE_DIR_DIRTY; + } + return true; + + fail: + return false; +} +//------------------------------------------------------------------------------ +// Add a cluster to a directory file and zero the cluster. +// return with first block of cluster in the cache +bool SdBaseFile::addDirCluster() { + uint32_t block; + // max folder size + if (fileSize_/sizeof(dir_t) >= 0XFFFF) goto fail; + + if (!addCluster()) goto fail; + if (!vol_->cacheFlush()) goto fail; + + block = vol_->clusterStartBlock(curCluster_); + + // set cache to first block of cluster + vol_->cacheSetBlockNumber(block, true); + + // zero first block of cluster + memset(vol_->cacheBuffer_.data, 0, 512); + + // zero rest of cluster + for (uint8_t i = 1; i < vol_->blocksPerCluster_; i++) { + if (!vol_->writeBlock(block + i, vol_->cacheBuffer_.data)) goto fail; + } + // Increase directory file size by cluster size + fileSize_ += 512UL << vol_->clusterSizeShift_; + return true; + + fail: + return false; +} +//------------------------------------------------------------------------------ +// cache a file's directory entry +// return pointer to cached entry or null for failure +dir_t* SdBaseFile::cacheDirEntry(uint8_t action) { + if (!vol_->cacheRawBlock(dirBlock_, action)) goto fail; + return vol_->cache()->dir + dirIndex_; + + fail: + return 0; +} +//------------------------------------------------------------------------------ +/** Close a file and force cached data and directory information + * to be written to the storage device. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + * Reasons for failure include no file is open or an I/O error. + */ +bool SdBaseFile::close() { + bool rtn = sync(); + type_ = FAT_FILE_TYPE_CLOSED; + return rtn; +} +//------------------------------------------------------------------------------ +/** Check for contiguous file and return its raw block range. + * + * \param[out] bgnBlock the first block address for the file. + * \param[out] endBlock the last block address for the file. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + * Reasons for failure include file is not contiguous, file has zero length + * or an I/O error occurred. + */ +bool SdBaseFile::contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock) { + // error if no blocks + if (firstCluster_ == 0) goto fail; + + for (uint32_t c = firstCluster_; ; c++) { + uint32_t next; + if (!vol_->fatGet(c, &next)) goto fail; + + // check for contiguous + if (next != (c + 1)) { + // error if not end of chain + if (!vol_->isEOC(next)) goto fail; + *bgnBlock = vol_->clusterStartBlock(firstCluster_); + *endBlock = vol_->clusterStartBlock(c) + + vol_->blocksPerCluster_ - 1; + return true; + } + } + + fail: + return false; +} +//------------------------------------------------------------------------------ +/** Create and open a new contiguous file of a specified size. + * + * \note This function only supports short DOS 8.3 names. + * See open() for more information. + * + * \param[in] dirFile The directory where the file will be created. + * \param[in] path A path with a valid DOS 8.3 file name. + * \param[in] size The desired file size. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + * Reasons for failure include \a path contains + * an invalid DOS 8.3 file name, the FAT volume has not been initialized, + * a file is already open, the file already exists, the root + * directory is full or an I/O error. + * + */ +bool SdBaseFile::createContiguous(SdBaseFile* dirFile, + const char* path, uint32_t size) { + uint32_t count; + // don't allow zero length file + if (size == 0) goto fail; + if (!open(dirFile, path, O_CREAT | O_EXCL | O_RDWR)) goto fail; + + // calculate number of clusters needed + count = ((size - 1) >> (vol_->clusterSizeShift_ + 9)) + 1; + + // allocate clusters + if (!vol_->allocContiguous(count, &firstCluster_)) { + remove(); + goto fail; + } + fileSize_ = size; + + // insure sync() will update dir entry + flags_ |= F_FILE_DIR_DIRTY; + + return sync(); + + fail: + return false; +} +//------------------------------------------------------------------------------ +/** Return a file's directory entry. + * + * \param[out] dir Location for return of the file's directory entry. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool SdBaseFile::dirEntry(dir_t* dir) { + dir_t* p; + // make sure fields on SD are correct + if (!sync()) goto fail; + + // read entry + p = cacheDirEntry(SdVolume::CACHE_FOR_READ); + if (!p) goto fail; + + // copy to caller's struct + memcpy(dir, p, sizeof(dir_t)); + return true; + + fail: + return false; +} +//------------------------------------------------------------------------------ +/** Format the name field of \a dir into the 13 byte array + * \a name in standard 8.3 short name format. + * + * \param[in] dir The directory structure containing the name. + * \param[out] name A 13 byte char array for the formatted name. + */ +void SdBaseFile::dirName(const dir_t& dir, char* name) { + uint8_t j = 0; + for (uint8_t i = 0; i < 11; i++) { + if (dir.name[i] == ' ')continue; + if (i == 8) name[j++] = '.'; + name[j++] = dir.name[i]; + } + name[j] = 0; +} +//------------------------------------------------------------------------------ +/** Test for the existence of a file in a directory + * + * \param[in] name Name of the file to be tested for. + * + * The calling instance must be an open directory file. + * + * dirFile.exists("TOFIND.TXT") searches for "TOFIND.TXT" in the directory + * dirFile. + * + * \return true if the file exists else false. + */ +bool SdBaseFile::exists(const char* name) { + SdBaseFile file; + return file.open(this, name, O_READ); +} +//------------------------------------------------------------------------------ +/** + * Get a string from a file. + * + * fgets() reads bytes from a file into the array pointed to by \a str, until + * \a num - 1 bytes are read, or a delimiter is read and transferred to \a str, + * or end-of-file is encountered. The string is then terminated + * with a null byte. + * + * fgets() deletes CR, '\\r', from the string. This insures only a '\\n' + * terminates the string for Windows text files which use CRLF for newline. + * + * \param[out] str Pointer to the array where the string is stored. + * \param[in] num Maximum number of characters to be read + * (including the final null byte). Usually the length + * of the array \a str is used. + * \param[in] delim Optional set of delimiters. The default is "\n". + * + * \return For success fgets() returns the length of the string in \a str. + * If no data is read, fgets() returns zero for EOF or -1 if an error occurred. + **/ +int16_t SdBaseFile::fgets(char* str, int16_t num, char* delim) { + char ch; + int16_t n = 0; + int16_t r = -1; + while ((n + 1) < num && (r = read(&ch, 1)) == 1) { + // delete CR + if (ch == '\r') continue; + str[n++] = ch; + if (!delim) { + if (ch == '\n') break; + } else { + if (strchr(delim, ch)) break; + } + } + if (r < 0) { + // read error + return -1; + } + str[n] = '\0'; + return n; +} +//------------------------------------------------------------------------------ +/** Get a file's name + * + * \param[out] name An array of 13 characters for the file's name. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool SdBaseFile::getFilename(char* name) { + if (!isOpen()) return false; + + if (isRoot()) { + name[0] = '/'; + name[1] = '\0'; + return true; + } + // cache entry + dir_t* p = cacheDirEntry(SdVolume::CACHE_FOR_READ); + if (!p) return false; + + // format name + dirName(*p, name); + return true; +} +//------------------------------------------------------------------------------ +void SdBaseFile::getpos(filepos_t* pos) { + pos->position = curPosition_; + pos->cluster = curCluster_; +} + +//------------------------------------------------------------------------------ +/** List directory contents. + * + * \param[in] pr Print stream for list. + * + * \param[in] flags The inclusive OR of + * + * LS_DATE - %Print file modification date + * + * LS_SIZE - %Print file size. + * + * LS_R - Recursive list of subdirectories. + * + * \param[in] indent Amount of space before file name. Used for recursive + * list to indicate subdirectory level. + */ +void SdBaseFile::ls(uint8_t flags, uint8_t indent) { + rewind(); + int8_t status; + while ((status = lsPrintNext( flags, indent))) { + if (status > 1 && (flags & LS_R)) { + uint16_t index = curPosition()/32 - 1; + SdBaseFile s; + if (s.open(this, index, O_READ)) s.ls( flags, indent + 2); + seekSet(32 * (index + 1)); + } + } +} +//------------------------------------------------------------------------------ +// saves 32 bytes on stack for ls recursion +// return 0 - EOF, 1 - normal file, or 2 - directory +int8_t SdBaseFile::lsPrintNext( uint8_t flags, uint8_t indent) { + dir_t dir; + uint8_t w = 0; + + while (1) { + if (read(&dir, sizeof(dir)) != sizeof(dir)) return 0; + if (dir.name[0] == DIR_NAME_FREE) return 0; + + // skip deleted entry and entries for . and .. + if (dir.name[0] != DIR_NAME_DELETED && dir.name[0] != '.' + && DIR_IS_FILE_OR_SUBDIR(&dir)) break; + } + // indent for dir level + for (uint8_t i = 0; i < indent; i++) MYSERIAL.write(' '); + + // print name + for (uint8_t i = 0; i < 11; i++) { + if (dir.name[i] == ' ')continue; + if (i == 8) { + MYSERIAL.write('.'); + w++; + } + MYSERIAL.write(dir.name[i]); + w++; + } + if (DIR_IS_SUBDIR(&dir)) { + MYSERIAL.write('/'); + w++; + } + if (flags & (LS_DATE | LS_SIZE)) { + while (w++ < 14) MYSERIAL.write(' '); + } + // print modify date/time if requested + if (flags & LS_DATE) { + MYSERIAL.write(' '); + printFatDate( dir.lastWriteDate); + MYSERIAL.write(' '); + printFatTime( dir.lastWriteTime); + } + // print size if requested + if (!DIR_IS_SUBDIR(&dir) && (flags & LS_SIZE)) { + MYSERIAL.write(' '); + MYSERIAL.print(dir.fileSize); + } + MYSERIAL.println(); + return DIR_IS_FILE(&dir) ? 1 : 2; +} +//------------------------------------------------------------------------------ +// format directory name field from a 8.3 name string +bool SdBaseFile::make83Name(const char* str, uint8_t* name, const char** ptr) { + uint8_t c; + uint8_t n = 7; // max index for part before dot + uint8_t i = 0; + // blank fill name and extension + while (i < 11) name[i++] = ' '; + i = 0; + while (*str != '\0' && *str != '/') { + c = *str++; + if (c == '.') { + if (n == 10) goto fail; // only one dot allowed + n = 10; // max index for full 8.3 name + i = 8; // place for extension + } else { + // illegal FAT characters + //PGM_P p = PSTR("|<>^+=?/[];,*\"\\"); + // 2019-08-27 really? + // Microsoft defines, that only a subset of these characters is not allowed. + PGM_P p = PSTR("|<>?/*\"\\"); + uint8_t b; + while ((b = pgm_read_byte(p++))) if (b == c) goto fail; + // check size and only allow ASCII printable characters + if (i > n || c < 0X21 || c > 0X7E)goto fail; + // only upper case allowed in 8.3 names - convert lower to upper + name[i++] = (c < 'a' || c > 'z') ? (c) : (c + ('A' - 'a')); + } + } + *ptr = str; + // must have a file name, extension is optional + return name[0] != ' '; + + fail: + return false; +} +//------------------------------------------------------------------------------ +/** Make a new directory. + * + * \param[in] parent An open SdFat instance for the directory that will contain + * the new directory. + * + * \param[in] path A path with a valid 8.3 DOS name for the new directory. + * + * \param[in] pFlag Create missing parent directories if true. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + * Reasons for failure include this file is already open, \a parent is not a + * directory, \a path is invalid or already exists in \a parent. + */ +bool SdBaseFile::mkdir(SdBaseFile* parent, const char* path, bool pFlag) { + uint8_t dname[11]; + SdBaseFile dir1, dir2; + SdBaseFile* sub = &dir1; + SdBaseFile* start = parent; + + if (!parent || isOpen()) goto fail; + + if (*path == '/') { + while (*path == '/') path++; + if (!parent->isRoot()) { + if (!dir2.openRoot(parent->vol_)) goto fail; + parent = &dir2; + } + } + while (1) { + if (!make83Name(path, dname, &path)) goto fail; + while (*path == '/') path++; + if (!*path) break; + if (!sub->open(parent, dname, O_READ)) { + if (!pFlag || !sub->mkdir(parent, dname)) { + goto fail; + } + } + if (parent != start) parent->close(); + parent = sub; + sub = parent != &dir1 ? &dir1 : &dir2; + } + return mkdir(parent, dname); + + fail: + return false; +} +//------------------------------------------------------------------------------ +bool SdBaseFile::mkdir(SdBaseFile* parent, const uint8_t dname[11]) { + uint32_t block; + dir_t d; + dir_t* p; + + if (!parent->isDir()) goto fail; + + // create a normal file + if (!open(parent, dname, O_CREAT | O_EXCL | O_RDWR)) goto fail; + + // convert file to directory + flags_ = O_READ; + type_ = FAT_FILE_TYPE_SUBDIR; + + // allocate and zero first cluster + if (!addDirCluster())goto fail; + + // force entry to SD + if (!sync()) goto fail; + + // cache entry - should already be in cache due to sync() call + p = cacheDirEntry(SdVolume::CACHE_FOR_WRITE); + if (!p) goto fail; + + // change directory entry attribute + p->attributes = DIR_ATT_DIRECTORY; + + // make entry for '.' + memcpy(&d, p, sizeof(d)); + d.name[0] = '.'; + for (uint8_t i = 1; i < 11; i++) d.name[i] = ' '; + + // cache block for '.' and '..' + block = vol_->clusterStartBlock(firstCluster_); + if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_WRITE)) goto fail; + + // copy '.' to block + memcpy(&vol_->cache()->dir[0], &d, sizeof(d)); + + // make entry for '..' + d.name[1] = '.'; + if (parent->isRoot()) { + d.firstClusterLow = 0; + d.firstClusterHigh = 0; + } else { + d.firstClusterLow = parent->firstCluster_ & 0XFFFF; + d.firstClusterHigh = parent->firstCluster_ >> 16; + } + // copy '..' to block + memcpy(&vol_->cache()->dir[1], &d, sizeof(d)); + + // write first block + return vol_->cacheFlush(); + + fail: + return false; +} +//------------------------------------------------------------------------------ + /** Open a file in the current working directory. + * + * \param[in] path A path with a valid 8.3 DOS name for a file to be opened. + * + * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive + * OR of open flags. see SdBaseFile::open(SdBaseFile*, const char*, uint8_t). + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ + bool SdBaseFile::open(const char* path, uint8_t oflag) { + return open(cwd_, path, oflag); + } +//------------------------------------------------------------------------------ +/** Open a file or directory by name. + * + * \param[in] dirFile An open SdFat instance for the directory containing the + * file to be opened. + * + * \param[in] path A path with a valid 8.3 DOS name for a file to be opened. + * + * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive + * OR of flags from the following list + * + * O_READ - Open for reading. + * + * O_RDONLY - Same as O_READ. + * + * O_WRITE - Open for writing. + * + * O_WRONLY - Same as O_WRITE. + * + * O_RDWR - Open for reading and writing. + * + * O_APPEND - If set, the file offset shall be set to the end of the + * file prior to each write. + * + * O_AT_END - Set the initial position at the end of the file. + * + * O_CREAT - If the file exists, this flag has no effect except as noted + * under O_EXCL below. Otherwise, the file shall be created + * + * O_EXCL - If O_CREAT and O_EXCL are set, open() shall fail if the file exists. + * + * O_SYNC - Call sync() after each write. This flag should not be used with + * write(uint8_t), write_P(PGM_P), writeln_P(PGM_P), or the Arduino Print class. + * These functions do character at a time writes so sync() will be called + * after each byte. + * + * O_TRUNC - If the file exists and is a regular file, and the file is + * successfully opened and is not read only, its length shall be truncated to 0. + * + * WARNING: A given file must not be opened by more than one SdBaseFile object + * of file corruption may occur. + * + * \note Directory files must be opened read only. Write and truncation is + * not allowed for directory files. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + * Reasons for failure include this file is already open, \a dirFile is not + * a directory, \a path is invalid, the file does not exist + * or can't be opened in the access mode specified by oflag. + */ +bool SdBaseFile::open(SdBaseFile* dirFile, const char* path, uint8_t oflag) { + uint8_t dname[11]; + SdBaseFile dir1, dir2; + SdBaseFile *parent = dirFile; + SdBaseFile *sub = &dir1; + + if (!dirFile) goto fail; + + // error if already open + if (isOpen()) goto fail; + + if (*path == '/') { + while (*path == '/') path++; + if (!dirFile->isRoot()) { + if (!dir2.openRoot(dirFile->vol_)) goto fail; + parent = &dir2; + } + } + while (1) { + if (!make83Name(path, dname, &path)) goto fail; + while (*path == '/') path++; + if (!*path) break; + if (!sub->open(parent, dname, O_READ)) goto fail; + if (parent != dirFile) parent->close(); + parent = sub; + sub = parent != &dir1 ? &dir1 : &dir2; + } + return open(parent, dname, oflag); + + fail: + return false; +} +//------------------------------------------------------------------------------ +// open with filename in dname +bool SdBaseFile::open(SdBaseFile* dirFile, + const uint8_t dname[11], uint8_t oflag) { + bool emptyFound = false; + bool fileFound = false; + uint8_t index; + dir_t* p; + + vol_ = dirFile->vol_; + + dirFile->rewind(); + // search for file + + while (dirFile->curPosition_ < dirFile->fileSize_) { + index = 0XF & (dirFile->curPosition_ >> 5); + p = dirFile->readDirCache(); + if (!p) goto fail; + + if (p->name[0] == DIR_NAME_FREE || p->name[0] == DIR_NAME_DELETED) { + // remember first empty slot + if (!emptyFound) { + dirBlock_ = dirFile->vol_->cacheBlockNumber(); + dirIndex_ = index; + emptyFound = true; + } + // done if no entries follow + if (p->name[0] == DIR_NAME_FREE) break; + } else if (!memcmp(dname, p->name, 11)) { + fileFound = true; + break; + } + } + if (fileFound) { + // don't open existing file if O_EXCL + if (oflag & O_EXCL) goto fail; + } else { + // don't create unless O_CREAT and O_WRITE + if (!(oflag & O_CREAT) || !(oflag & O_WRITE)) goto fail; + if (emptyFound) { + index = dirIndex_; + p = cacheDirEntry(SdVolume::CACHE_FOR_WRITE); + if (!p) goto fail; + } else { + if (dirFile->type_ == FAT_FILE_TYPE_ROOT_FIXED) goto fail; + + // add and zero cluster for dirFile - first cluster is in cache for write + if (!dirFile->addDirCluster()) goto fail; + + // use first entry in cluster + p = dirFile->vol_->cache()->dir; + index = 0; + } + // initialize as empty file + memset(p, 0, sizeof(dir_t)); + memcpy(p->name, dname, 11); + + // set timestamps + if (dateTime_) { + // call user date/time function + dateTime_(&p->creationDate, &p->creationTime); + } else { + // use default date/time + p->creationDate = FAT_DEFAULT_DATE; + p->creationTime = FAT_DEFAULT_TIME; + } + p->lastAccessDate = p->creationDate; + p->lastWriteDate = p->creationDate; + p->lastWriteTime = p->creationTime; + + // write entry to SD + if (!dirFile->vol_->cacheFlush()) goto fail; + } + // open entry in cache + return openCachedEntry(index, oflag); + + fail: + return false; +} +//------------------------------------------------------------------------------ +/** Open a file by index. + * + * \param[in] dirFile An open SdFat instance for the directory. + * + * \param[in] index The \a index of the directory entry for the file to be + * opened. The value for \a index is (directory file position)/32. + * + * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive + * OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC. + * + * See open() by path for definition of flags. + * \return true for success or false for failure. + */ +bool SdBaseFile::open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag) { + dir_t* p; + + vol_ = dirFile->vol_; + + // error if already open + if (isOpen() || !dirFile) goto fail; + + // don't open existing file if O_EXCL - user call error + if (oflag & O_EXCL) goto fail; + + // seek to location of entry + if (!dirFile->seekSet(32 * index)) goto fail; + + // read entry into cache + p = dirFile->readDirCache(); + if (!p) goto fail; + + // error if empty slot or '.' or '..' + if (p->name[0] == DIR_NAME_FREE || + p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') { + goto fail; + } + // open cached entry + return openCachedEntry(index & 0XF, oflag); + + fail: + return false; +} +//------------------------------------------------------------------------------ +// open a cached directory entry. Assumes vol_ is initialized +bool SdBaseFile::openCachedEntry(uint8_t dirIndex, uint8_t oflag) { + // location of entry in cache + dir_t* p = &vol_->cache()->dir[dirIndex]; + + // write or truncate is an error for a directory or read-only file + if (p->attributes & (DIR_ATT_READ_ONLY | DIR_ATT_DIRECTORY)) { + if (oflag & (O_WRITE | O_TRUNC)) goto fail; + } + // remember location of directory entry on SD + dirBlock_ = vol_->cacheBlockNumber(); + dirIndex_ = dirIndex; + + // copy first cluster number for directory fields + firstCluster_ = (uint32_t)p->firstClusterHigh << 16; + firstCluster_ |= p->firstClusterLow; + + // make sure it is a normal file or subdirectory + if (DIR_IS_FILE(p)) { + fileSize_ = p->fileSize; + type_ = FAT_FILE_TYPE_NORMAL; + } else if (DIR_IS_SUBDIR(p)) { + if (!vol_->chainSize(firstCluster_, &fileSize_)) goto fail; + type_ = FAT_FILE_TYPE_SUBDIR; + } else { + goto fail; + } + // save open flags for read/write + flags_ = oflag & F_OFLAG; + + // set to start of file + curCluster_ = 0; + curPosition_ = 0; + if ((oflag & O_TRUNC) && !truncate(0)) return false; + return oflag & O_AT_END ? seekEnd(0) : true; + + fail: + type_ = FAT_FILE_TYPE_CLOSED; + return false; +} +//------------------------------------------------------------------------------ +/** Open the next file or subdirectory in a directory. + * + * \param[in] dirFile An open SdFat instance for the directory containing the + * file to be opened. + * + * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive + * OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC. + * + * See open() by path for definition of flags. + * \return true for success or false for failure. + */ +bool SdBaseFile::openNext(SdBaseFile* dirFile, uint8_t oflag) { + dir_t* p; + uint8_t index; + + if (!dirFile) goto fail; + + // error if already open + if (isOpen()) goto fail; + + vol_ = dirFile->vol_; + + while (1) { + index = 0XF & (dirFile->curPosition_ >> 5); + + // read entry into cache + p = dirFile->readDirCache(); + if (!p) goto fail; + + // done if last entry + if (p->name[0] == DIR_NAME_FREE) goto fail; + + // skip empty slot or '.' or '..' + if (p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') { + continue; + } + // must be file or dir + if (DIR_IS_FILE_OR_SUBDIR(p)) { + return openCachedEntry(index, oflag); + } + } + + fail: + return false; +} +//------------------------------------------------------------------------------ +/** Open a directory's parent directory. + * + * \param[in] dir Parent of this directory will be opened. Must not be root. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool SdBaseFile::openParent(SdBaseFile* dir) { + dir_t entry; + dir_t* p; + SdBaseFile file; + uint32_t c; + uint32_t cluster; + uint32_t lbn; + // error if already open or dir is root or dir is not a directory + if (isOpen() || !dir || dir->isRoot() || !dir->isDir()) goto fail; + vol_ = dir->vol_; + // position to '..' + if (!dir->seekSet(32)) goto fail; + // read '..' entry + if (dir->read(&entry, sizeof(entry)) != 32) goto fail; + // verify it is '..' + if (entry.name[0] != '.' || entry.name[1] != '.') goto fail; + // start cluster for '..' + cluster = entry.firstClusterLow; + cluster |= (uint32_t)entry.firstClusterHigh << 16; + if (cluster == 0) return openRoot(vol_); + // start block for '..' + lbn = vol_->clusterStartBlock(cluster); + // first block of parent dir + if (!vol_->cacheRawBlock(lbn, SdVolume::CACHE_FOR_READ)) { + goto fail; + } + p = &vol_->cacheBuffer_.dir[1]; + // verify name for '../..' + if (p->name[0] != '.' || p->name[1] != '.') goto fail; + // '..' is pointer to first cluster of parent. open '../..' to find parent + if (p->firstClusterHigh == 0 && p->firstClusterLow == 0) { + if (!file.openRoot(dir->volume())) goto fail; + } else { + if (!file.openCachedEntry(1, O_READ)) goto fail; + } + // search for parent in '../..' + do { + if (file.readDir(&entry, NULL) != 32) goto fail; + c = entry.firstClusterLow; + c |= (uint32_t)entry.firstClusterHigh << 16; + } while (c != cluster); + // open parent + return open(&file, file.curPosition()/32 - 1, O_READ); + + fail: + return false; +} +//------------------------------------------------------------------------------ +/** Open a volume's root directory. + * + * \param[in] vol The FAT volume containing the root directory to be opened. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + * Reasons for failure include the file is already open, the FAT volume has + * not been initialized or it a FAT12 volume. + */ +bool SdBaseFile::openRoot(SdVolume* vol) { + // error if file is already open + if (isOpen()) goto fail; + + if (vol->fatType() == 16 || (FAT12_SUPPORT && vol->fatType() == 12)) { + type_ = FAT_FILE_TYPE_ROOT_FIXED; + firstCluster_ = 0; + fileSize_ = 32 * vol->rootDirEntryCount(); + } else if (vol->fatType() == 32) { + type_ = FAT_FILE_TYPE_ROOT32; + firstCluster_ = vol->rootDirStart(); + if (!vol->chainSize(firstCluster_, &fileSize_)) goto fail; + } else { + // volume is not initialized, invalid, or FAT12 without support + return false; + } + vol_ = vol; + // read only + flags_ = O_READ; + + // set to start of file + curCluster_ = 0; + curPosition_ = 0; + + // root has no directory entry + dirBlock_ = 0; + dirIndex_ = 0; + return true; + + fail: + return false; +} +//------------------------------------------------------------------------------ +/** Return the next available byte without consuming it. + * + * \return The byte if no error and not at eof else -1; + */ +int SdBaseFile::peek() { + filepos_t pos; + getpos(&pos); + int c = read(); + if (c >= 0) setpos(&pos); + return c; +} + +//------------------------------------------------------------------------------ +/** %Print the name field of a directory entry in 8.3 format. + * \param[in] pr Print stream for output. + * \param[in] dir The directory structure containing the name. + * \param[in] width Blank fill name if length is less than \a width. + * \param[in] printSlash Print '/' after directory names if true. + */ +void SdBaseFile::printDirName(const dir_t& dir, + uint8_t width, bool printSlash) { + uint8_t w = 0; + for (uint8_t i = 0; i < 11; i++) { + if (dir.name[i] == ' ')continue; + if (i == 8) { + MYSERIAL.write('.'); + w++; + } + MYSERIAL.write(dir.name[i]); + w++; + } + if (DIR_IS_SUBDIR(&dir) && printSlash) { + MYSERIAL.write('/'); + w++; + } + while (w < width) { + MYSERIAL.write(' '); + w++; + } +} +//------------------------------------------------------------------------------ +// print uint8_t with width 2 +static void print2u( uint8_t v) { + if (v < 10) MYSERIAL.write('0'); + MYSERIAL.print(v, DEC); +} +//------------------------------------------------------------------------------ +/** %Print a directory date field to Serial. + * + * Format is yyyy-mm-dd. + * + * \param[in] fatDate The date field from a directory entry. + */ + +//------------------------------------------------------------------------------ +/** %Print a directory date field. + * + * Format is yyyy-mm-dd. + * + * \param[in] pr Print stream for output. + * \param[in] fatDate The date field from a directory entry. + */ +void SdBaseFile::printFatDate(uint16_t fatDate) { + MYSERIAL.print(FAT_YEAR(fatDate)); + MYSERIAL.write('-'); + print2u( FAT_MONTH(fatDate)); + MYSERIAL.write('-'); + print2u( FAT_DAY(fatDate)); +} + +//------------------------------------------------------------------------------ +/** %Print a directory time field. + * + * Format is hh:mm:ss. + * + * \param[in] pr Print stream for output. + * \param[in] fatTime The time field from a directory entry. + */ +void SdBaseFile::printFatTime( uint16_t fatTime) { + print2u( FAT_HOUR(fatTime)); + MYSERIAL.write(':'); + print2u( FAT_MINUTE(fatTime)); + MYSERIAL.write(':'); + print2u( FAT_SECOND(fatTime)); +} +//------------------------------------------------------------------------------ +/** Print a file's name to Serial + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool SdBaseFile::printName() { + char name[13]; + if (!getFilename(name)) return false; + MYSERIAL.print(name); + return true; +} +//------------------------------------------------------------------------------ +/** Read the next byte from a file. + * + * \return For success read returns the next byte in the file as an int. + * If an error occurs or end of file is reached -1 is returned. + */ +int16_t SdBaseFile::read() { + uint8_t b; + return read(&b, 1) == 1 ? b : -1; +} +//------------------------------------------------------------------------------ +/** Read data from a file starting at the current position. + * + * \param[out] buf Pointer to the location that will receive the data. + * + * \param[in] nbyte Maximum number of bytes to read. + * + * \return For success read() returns the number of bytes read. + * A value less than \a nbyte, including zero, will be returned + * if end of file is reached. + * If an error occurs, read() returns -1. Possible errors include + * read() called before a file has been opened, corrupt file system + * or an I/O error occurred. + */ +int16_t SdBaseFile::read(void* buf, uint16_t nbyte) { + uint8_t* dst = reinterpret_cast(buf); + uint16_t offset; + uint16_t toRead; + uint32_t block; // raw device block number + + // error if not open or write only + if (!isOpen() || !(flags_ & O_READ)) goto fail; + + // max bytes left in file + if (nbyte >= (fileSize_ - curPosition_)) { + nbyte = fileSize_ - curPosition_; + } + // amount left to read + toRead = nbyte; + while (toRead > 0) { + offset = curPosition_ & 0X1FF; // offset in block + if (type_ == FAT_FILE_TYPE_ROOT_FIXED) { + block = vol_->rootDirStart() + (curPosition_ >> 9); + } else { + uint8_t blockOfCluster = vol_->blockOfCluster(curPosition_); + if (offset == 0 && blockOfCluster == 0) { + // start of new cluster + if (curPosition_ == 0) { + // use first cluster in file + curCluster_ = firstCluster_; + } else { + // get next cluster from FAT + if (!vol_->fatGet(curCluster_, &curCluster_)) goto fail; + } + } + block = vol_->clusterStartBlock(curCluster_) + blockOfCluster; + } + uint16_t n = toRead; + + // amount to be read from current block + if (n > (512 - offset)) n = 512 - offset; + + // no buffering needed if n == 512 + if (n == 512 && block != vol_->cacheBlockNumber()) { + if (!vol_->readBlock(block, dst)) goto fail; + } else { + // read block to cache and copy data to caller + if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_READ)) goto fail; + uint8_t* src = vol_->cache()->data + offset; + memcpy(dst, src, n); + } + dst += n; + curPosition_ += n; + toRead -= n; + } + return nbyte; + + fail: + return -1; +} +//------------------------------------------------------------------------------ +/** Read the next directory entry from a directory file. + * + * \param[out] dir The dir_t struct that will receive the data. + * + * \return For success readDir() returns the number of bytes read. + * A value of zero will be returned if end of file is reached. + * If an error occurs, readDir() returns -1. Possible errors include + * readDir() called before a directory has been opened, this is not + * a directory file or an I/O error occurred. + */ +int8_t SdBaseFile::readDir(dir_t* dir, char* longFilename) { + int16_t n; + // if not a directory file or miss-positioned return an error + if (!isDir() || (0X1F & curPosition_)) return -1; + + //If we have a longFilename buffer, mark it as invalid. If we find a long filename it will be filled automaticly. + if (longFilename != NULL) + { + longFilename[0] = '\0'; + } + + while (1) { + n = read(dir, sizeof(dir_t)); + if (n != sizeof(dir_t)) return n == 0 ? 0 : -1; + // last entry if DIR_NAME_FREE + if (dir->name[0] == DIR_NAME_FREE) return 0; + // skip empty entries and entry for . and .. + if (dir->name[0] == DIR_NAME_DELETED || dir->name[0] == '.') continue; + //Fill the long filename if we have a long filename entry, + // long filename entries are stored before the actual filename. + if (DIR_IS_LONG_NAME(dir) && longFilename != NULL) + { + vfat_t *VFAT = (vfat_t*)dir; + //Sanity check the VFAT entry. The first cluster is always set to zero. And th esequence number should be higher then 0 + if (VFAT->firstClusterLow == 0 && (VFAT->sequenceNumber & 0x1F) > 0 && (VFAT->sequenceNumber & 0x1F) <= MAX_VFAT_ENTRIES) + { + //TODO: Store the filename checksum to verify if a none-long filename aware system modified the file table. + n = ((VFAT->sequenceNumber & 0x1F) - 1) * 13; + longFilename[n+0] = VFAT->name1[0]; + longFilename[n+1] = VFAT->name1[1]; + longFilename[n+2] = VFAT->name1[2]; + longFilename[n+3] = VFAT->name1[3]; + longFilename[n+4] = VFAT->name1[4]; + longFilename[n+5] = VFAT->name2[0]; + longFilename[n+6] = VFAT->name2[1]; + longFilename[n+7] = VFAT->name2[2]; + longFilename[n+8] = VFAT->name2[3]; + longFilename[n+9] = VFAT->name2[4]; + longFilename[n+10] = VFAT->name2[5]; + longFilename[n+11] = VFAT->name3[0]; + longFilename[n+12] = VFAT->name3[1]; + //If this VFAT entry is the last one, add a NUL terminator at the end of the string + if (VFAT->sequenceNumber & 0x40) + longFilename[n+13] = '\0'; + } + } + // return if normal file or subdirectory + if (DIR_IS_FILE_OR_SUBDIR(dir)) return n; + } +} +//------------------------------------------------------------------------------ +// Read next directory entry into the cache +// Assumes file is correctly positioned +dir_t* SdBaseFile::readDirCache() { + uint8_t i; + // error if not directory + if (!isDir()) goto fail; + + // index of entry in cache + i = (curPosition_ >> 5) & 0XF; + + // use read to locate and cache block + if (read() < 0) goto fail; + + // advance to next entry + curPosition_ += 31; + + // return pointer to entry + return vol_->cache()->dir + i; + + fail: + return 0; +} +//------------------------------------------------------------------------------ +/** Remove a file. + * + * The directory entry and all data for the file are deleted. + * + * \note This function should not be used to delete the 8.3 version of a + * file that has a long name. For example if a file has the long name + * "New Text Document.txt" you should not delete the 8.3 name "NEWTEX~1.TXT". + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + * Reasons for failure include the file read-only, is a directory, + * or an I/O error occurred. + */ +bool SdBaseFile::remove() { + dir_t* d; + // free any clusters - will fail if read-only or directory + if (!truncate(0)) goto fail; + + // cache directory entry + d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE); + if (!d) goto fail; + + // mark entry deleted + d->name[0] = DIR_NAME_DELETED; + + // set this file closed + type_ = FAT_FILE_TYPE_CLOSED; + + // write entry to SD + return vol_->cacheFlush(); + return true; + + fail: + return false; +} +//------------------------------------------------------------------------------ +/** Remove a file. + * + * The directory entry and all data for the file are deleted. + * + * \param[in] dirFile The directory that contains the file. + * \param[in] path Path for the file to be removed. + * + * \note This function should not be used to delete the 8.3 version of a + * file that has a long name. For example if a file has the long name + * "New Text Document.txt" you should not delete the 8.3 name "NEWTEX~1.TXT". + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + * Reasons for failure include the file is a directory, is read only, + * \a dirFile is not a directory, \a path is not found + * or an I/O error occurred. + */ +bool SdBaseFile::remove(SdBaseFile* dirFile, const char* path) { + SdBaseFile file; + if (!file.open(dirFile, path, O_WRITE)) goto fail; + return file.remove(); + + fail: + // can't set iostate - static function + return false; +} +//------------------------------------------------------------------------------ +/** Rename a file or subdirectory. + * + * \param[in] dirFile Directory for the new path. + * \param[in] newPath New path name for the file/directory. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + * Reasons for failure include \a dirFile is not open or is not a directory + * file, newPath is invalid or already exists, or an I/O error occurs. + */ +bool SdBaseFile::rename(SdBaseFile* dirFile, const char* newPath) { + dir_t entry; + uint32_t dirCluster = 0; + SdBaseFile file; + dir_t* d; + + // must be an open file or subdirectory + if (!(isFile() || isSubDir())) goto fail; + + // can't move file + if (vol_ != dirFile->vol_) goto fail; + + // sync() and cache directory entry + sync(); + d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE); + if (!d) goto fail; + + // save directory entry + memcpy(&entry, d, sizeof(entry)); + + // mark entry deleted + d->name[0] = DIR_NAME_DELETED; + + // make directory entry for new path + if (isFile()) { + if (!file.open(dirFile, newPath, O_CREAT | O_EXCL | O_WRITE)) { + goto restore; + } + } else { + // don't create missing path prefix components + if (!file.mkdir(dirFile, newPath, false)) { + goto restore; + } + // save cluster containing new dot dot + dirCluster = file.firstCluster_; + } + // change to new directory entry + dirBlock_ = file.dirBlock_; + dirIndex_ = file.dirIndex_; + + // mark closed to avoid possible destructor close call + file.type_ = FAT_FILE_TYPE_CLOSED; + + // cache new directory entry + d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE); + if (!d) goto fail; + + // copy all but name field to new directory entry + memcpy(&d->attributes, &entry.attributes, sizeof(entry) - sizeof(d->name)); + + // update dot dot if directory + if (dirCluster) { + // get new dot dot + uint32_t block = vol_->clusterStartBlock(dirCluster); + if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_READ)) goto fail; + memcpy(&entry, &vol_->cache()->dir[1], sizeof(entry)); + + // free unused cluster + if (!vol_->freeChain(dirCluster)) goto fail; + + // store new dot dot + block = vol_->clusterStartBlock(firstCluster_); + if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_WRITE)) goto fail; + memcpy(&vol_->cache()->dir[1], &entry, sizeof(entry)); + } + return vol_->cacheFlush(); + + restore: + d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE); + if (!d) goto fail; + // restore entry + d->name[0] = entry.name[0]; + vol_->cacheFlush(); + + fail: + return false; +} +//------------------------------------------------------------------------------ +/** Remove a directory file. + * + * The directory file will be removed only if it is empty and is not the + * root directory. rmdir() follows DOS and Windows and ignores the + * read-only attribute for the directory. + * + * \note This function should not be used to delete the 8.3 version of a + * directory that has a long name. For example if a directory has the + * long name "New folder" you should not delete the 8.3 name "NEWFOL~1". + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + * Reasons for failure include the file is not a directory, is the root + * directory, is not empty, or an I/O error occurred. + */ +bool SdBaseFile::rmdir() { + // must be open subdirectory + if (!isSubDir()) goto fail; + + rewind(); + + // make sure directory is empty + while (curPosition_ < fileSize_) { + dir_t* p = readDirCache(); + if (!p) goto fail; + // done if past last used entry + if (p->name[0] == DIR_NAME_FREE) break; + // skip empty slot, '.' or '..' + if (p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') continue; + // error not empty + if (DIR_IS_FILE_OR_SUBDIR(p)) goto fail; + } + // convert empty directory to normal file for remove + type_ = FAT_FILE_TYPE_NORMAL; + flags_ |= O_WRITE; + return remove(); + + fail: + return false; +} +//------------------------------------------------------------------------------ +/** Recursively delete a directory and all contained files. + * + * This is like the Unix/Linux 'rm -rf *' if called with the root directory + * hence the name. + * + * Warning - This will remove all contents of the directory including + * subdirectories. The directory will then be removed if it is not root. + * The read-only attribute for files will be ignored. + * + * \note This function should not be used to delete the 8.3 version of + * a directory that has a long name. See remove() and rmdir(). + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool SdBaseFile::rmRfStar() { + uint16_t index; + SdBaseFile f; + rewind(); + while (curPosition_ < fileSize_) { + // remember position + index = curPosition_/32; + + dir_t* p = readDirCache(); + if (!p) goto fail; + + // done if past last entry + if (p->name[0] == DIR_NAME_FREE) break; + + // skip empty slot or '.' or '..' + if (p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') continue; + + // skip if part of long file name or volume label in root + if (!DIR_IS_FILE_OR_SUBDIR(p)) continue; + + if (!f.open(this, index, O_READ)) goto fail; + if (f.isSubDir()) { + // recursively delete + if (!f.rmRfStar()) goto fail; + } else { + // ignore read-only + f.flags_ |= O_WRITE; + if (!f.remove()) goto fail; + } + // position to next entry if required + if (curPosition_ != (32*(index + 1))) { + if (!seekSet(32*(index + 1))) goto fail; + } + } + // don't try to delete root + if (!isRoot()) { + if (!rmdir()) goto fail; + } + return true; + + fail: + return false; +} +//------------------------------------------------------------------------------ +/** Create a file object and open it in the current working directory. + * + * \param[in] path A path with a valid 8.3 DOS name for a file to be opened. + * + * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive + * OR of open flags. see SdBaseFile::open(SdBaseFile*, const char*, uint8_t). + */ +SdBaseFile::SdBaseFile(const char* path, uint8_t oflag) { + type_ = FAT_FILE_TYPE_CLOSED; + writeError = false; + open(path, oflag); +} +//------------------------------------------------------------------------------ +/** Sets a file's position. + * + * \param[in] pos The new position in bytes from the beginning of the file. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool SdBaseFile::seekSet(uint32_t pos) { + uint32_t nCur; + uint32_t nNew; + // error if file not open or seek past end of file + if (!isOpen() || pos > fileSize_) goto fail; + + if (type_ == FAT_FILE_TYPE_ROOT_FIXED) { + curPosition_ = pos; + goto done; + } + if (pos == 0) { + // set position to start of file + curCluster_ = 0; + curPosition_ = 0; + goto done; + } + // calculate cluster index for cur and new position + nCur = (curPosition_ - 1) >> (vol_->clusterSizeShift_ + 9); + nNew = (pos - 1) >> (vol_->clusterSizeShift_ + 9); + + if (nNew < nCur || curPosition_ == 0) { + // must follow chain from first cluster + curCluster_ = firstCluster_; + } else { + // advance from curPosition + nNew -= nCur; + } + while (nNew--) { + if (!vol_->fatGet(curCluster_, &curCluster_)) goto fail; + } + curPosition_ = pos; + + done: + return true; + + fail: + return false; +} +//------------------------------------------------------------------------------ +void SdBaseFile::setpos(filepos_t* pos) { + curPosition_ = pos->position; + curCluster_ = pos->cluster; +} +//------------------------------------------------------------------------------ +/** The sync() call causes all modified data and directory fields + * to be written to the storage device. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + * Reasons for failure include a call to sync() before a file has been + * opened or an I/O error. + */ +bool SdBaseFile::sync() { + // only allow open files and directories + if (!isOpen()) goto fail; + + if (flags_ & F_FILE_DIR_DIRTY) { + dir_t* d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE); + // check for deleted by another open file object + if (!d || d->name[0] == DIR_NAME_DELETED) goto fail; + + // do not set filesize for dir files + if (!isDir()) d->fileSize = fileSize_; + + // update first cluster fields + d->firstClusterLow = firstCluster_ & 0XFFFF; + d->firstClusterHigh = firstCluster_ >> 16; + + // set modify time if user supplied a callback date/time function + if (dateTime_) { + dateTime_(&d->lastWriteDate, &d->lastWriteTime); + d->lastAccessDate = d->lastWriteDate; + } + // clear directory dirty + flags_ &= ~F_FILE_DIR_DIRTY; + } + return vol_->cacheFlush(); + + fail: + writeError = true; + return false; +} +//------------------------------------------------------------------------------ +/** Copy a file's timestamps + * + * \param[in] file File to copy timestamps from. + * + * \note + * Modify and access timestamps may be overwritten if a date time callback + * function has been set by dateTimeCallback(). + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool SdBaseFile::timestamp(SdBaseFile* file) { + dir_t* d; + dir_t dir; + + // get timestamps + if (!file->dirEntry(&dir)) goto fail; + + // update directory fields + if (!sync()) goto fail; + + d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE); + if (!d) goto fail; + + // copy timestamps + d->lastAccessDate = dir.lastAccessDate; + d->creationDate = dir.creationDate; + d->creationTime = dir.creationTime; + d->creationTimeTenths = dir.creationTimeTenths; + d->lastWriteDate = dir.lastWriteDate; + d->lastWriteTime = dir.lastWriteTime; + + // write back entry + return vol_->cacheFlush(); + + fail: + return false; +} +//------------------------------------------------------------------------------ +/** Set a file's timestamps in its directory entry. + * + * \param[in] flags Values for \a flags are constructed by a bitwise-inclusive + * OR of flags from the following list + * + * T_ACCESS - Set the file's last access date. + * + * T_CREATE - Set the file's creation date and time. + * + * T_WRITE - Set the file's last write/modification date and time. + * + * \param[in] year Valid range 1980 - 2107 inclusive. + * + * \param[in] month Valid range 1 - 12 inclusive. + * + * \param[in] day Valid range 1 - 31 inclusive. + * + * \param[in] hour Valid range 0 - 23 inclusive. + * + * \param[in] minute Valid range 0 - 59 inclusive. + * + * \param[in] second Valid range 0 - 59 inclusive + * + * \note It is possible to set an invalid date since there is no check for + * the number of days in a month. + * + * \note + * Modify and access timestamps may be overwritten if a date time callback + * function has been set by dateTimeCallback(). + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + */ +bool SdBaseFile::timestamp(uint8_t flags, uint16_t year, uint8_t month, + uint8_t day, uint8_t hour, uint8_t minute, uint8_t second) { + uint16_t dirDate; + uint16_t dirTime; + dir_t* d; + + if (!isOpen() + || year < 1980 + || year > 2107 + || month < 1 + || month > 12 + || day < 1 + || day > 31 + || hour > 23 + || minute > 59 + || second > 59) { + goto fail; + } + // update directory entry + if (!sync()) goto fail; + + d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE); + if (!d) goto fail; + + dirDate = FAT_DATE(year, month, day); + dirTime = FAT_TIME(hour, minute, second); + if (flags & T_ACCESS) { + d->lastAccessDate = dirDate; + } + if (flags & T_CREATE) { + d->creationDate = dirDate; + d->creationTime = dirTime; + // seems to be units of 1/100 second not 1/10 as Microsoft states + d->creationTimeTenths = second & 1 ? 100 : 0; + } + if (flags & T_WRITE) { + d->lastWriteDate = dirDate; + d->lastWriteTime = dirTime; + } + return vol_->cacheFlush(); + + fail: + return false; +} +//------------------------------------------------------------------------------ +/** Truncate a file to a specified length. The current file position + * will be maintained if it is less than or equal to \a length otherwise + * it will be set to end of file. + * + * \param[in] length The desired length for the file. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + * Reasons for failure include file is read only, file is a directory, + * \a length is greater than the current file size or an I/O error occurs. + */ +bool SdBaseFile::truncate(uint32_t length) { + uint32_t newPos; + // error if not a normal file or read-only + if (!isFile() || !(flags_ & O_WRITE)) goto fail; + + // error if length is greater than current size + if (length > fileSize_) goto fail; + + // fileSize and length are zero - nothing to do + if (fileSize_ == 0) return true; + + // remember position for seek after truncation + newPos = curPosition_ > length ? length : curPosition_; + + // position to last cluster in truncated file + if (!seekSet(length)) goto fail; + + if (length == 0) { + // free all clusters + if (!vol_->freeChain(firstCluster_)) goto fail; + firstCluster_ = 0; + } else { + uint32_t toFree; + if (!vol_->fatGet(curCluster_, &toFree)) goto fail; + + if (!vol_->isEOC(toFree)) { + // free extra clusters + if (!vol_->freeChain(toFree)) goto fail; + + // current cluster is end of chain + if (!vol_->fatPutEOC(curCluster_)) goto fail; + } + } + fileSize_ = length; + + // need to update directory entry + flags_ |= F_FILE_DIR_DIRTY; + + if (!sync()) goto fail; + + // set file to correct position + return seekSet(newPos); + + fail: + return false; +} +//------------------------------------------------------------------------------ +/** Write data to an open file. + * + * \note Data is moved to the cache but may not be written to the + * storage device until sync() is called. + * + * \param[in] buf Pointer to the location of the data to be written. + * + * \param[in] nbyte Number of bytes to write. + * + * \return For success write() returns the number of bytes written, always + * \a nbyte. If an error occurs, write() returns -1. Possible errors + * include write() is called before a file has been opened, write is called + * for a read-only file, device is full, a corrupt file system or an I/O error. + * + */ +int16_t SdBaseFile::write(const void* buf, uint16_t nbyte) { + // convert void* to uint8_t* - must be before goto statements + const uint8_t* src = reinterpret_cast(buf); + + // number of bytes left to write - must be before goto statements + uint16_t nToWrite = nbyte; + + // error if not a normal file or is read-only + if (!isFile() || !(flags_ & O_WRITE)) goto fail; + + // seek to end of file if append flag + if ((flags_ & O_APPEND) && curPosition_ != fileSize_) { + if (!seekEnd()) goto fail; + } + + while (nToWrite > 0) { + uint8_t blockOfCluster = vol_->blockOfCluster(curPosition_); + uint16_t blockOffset = curPosition_ & 0X1FF; + if (blockOfCluster == 0 && blockOffset == 0) { + // start of new cluster + if (curCluster_ == 0) { + if (firstCluster_ == 0) { + // allocate first cluster of file + if (!addCluster()) goto fail; + } else { + curCluster_ = firstCluster_; + } + } else { + uint32_t next; + if (!vol_->fatGet(curCluster_, &next)) goto fail; + if (vol_->isEOC(next)) { + // add cluster if at end of chain + if (!addCluster()) goto fail; + } else { + curCluster_ = next; + } + } + } + // max space in block + uint16_t n = 512 - blockOffset; + + // lesser of space and amount to write + if (n > nToWrite) n = nToWrite; + + // block for data write + uint32_t block = vol_->clusterStartBlock(curCluster_) + blockOfCluster; + if (n == 512) { + // full block - don't need to use cache + if (vol_->cacheBlockNumber() == block) { + // invalidate cache if block is in cache + vol_->cacheSetBlockNumber(0XFFFFFFFF, false); + } + if (!vol_->writeBlock(block, src)) goto fail; + } else { + if (blockOffset == 0 && curPosition_ >= fileSize_) { + // start of new block don't need to read into cache + if (!vol_->cacheFlush()) goto fail; + // set cache dirty and SD address of block + vol_->cacheSetBlockNumber(block, true); + } else { + // rewrite part of block + if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_WRITE)) goto fail; + } + uint8_t* dst = vol_->cache()->data + blockOffset; + memcpy(dst, src, n); + } + curPosition_ += n; + src += n; + nToWrite -= n; + } + if (curPosition_ > fileSize_) { + // update fileSize and insure sync will update dir entry + fileSize_ = curPosition_; + flags_ |= F_FILE_DIR_DIRTY; + } else if (dateTime_ && nbyte) { + // insure sync will update modified date and time + flags_ |= F_FILE_DIR_DIRTY; + } + + if (flags_ & O_SYNC) { + if (!sync()) goto fail; + } + return nbyte; + + fail: + // return for write error + writeError = true; + return -1; +} +//------------------------------------------------------------------------------ +// suppress cpplint warnings with NOLINT comment +#if ALLOW_DEPRECATED_FUNCTIONS && !defined(DOXYGEN) +void (*SdBaseFile::oldDateTime_)(uint16_t& date, uint16_t& time) = 0; // NOLINT +#endif // ALLOW_DEPRECATED_FUNCTIONS + + +#endif diff --git a/Firmware/SdBaseFile.h b/Firmware/SdBaseFile.h new file mode 100644 index 0000000..923a391 --- /dev/null +++ b/Firmware/SdBaseFile.h @@ -0,0 +1,483 @@ +/* Arduino SdFat Library + * Copyright (C) 2009 by William Greiman + * + * This file is part of the Arduino SdFat Library + * + * This Library 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 Library 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 the Arduino SdFat Library. If not, see + * . + */ +#include "Marlin.h" +#ifdef SDSUPPORT + +#ifndef SdBaseFile_h +#define SdBaseFile_h +/** + * \file + * \brief SdBaseFile class + */ +#include "Marlin.h" +#include "SdFatConfig.h" +#include "SdVolume.h" +//------------------------------------------------------------------------------ +/** + * \struct filepos_t + * \brief internal type for istream + * do not use in user apps + */ +struct filepos_t { + /** stream position */ + uint32_t position; + /** cluster for position */ + uint32_t cluster; + filepos_t() : position(0), cluster(0) {} +}; + +// use the gnu style oflag in open() +/** open() oflag for reading */ +uint8_t const O_READ = 0X01; +/** open() oflag - same as O_IN */ +uint8_t const O_RDONLY = O_READ; +/** open() oflag for write */ +uint8_t const O_WRITE = 0X02; +/** open() oflag - same as O_WRITE */ +uint8_t const O_WRONLY = O_WRITE; +/** open() oflag for reading and writing */ +uint8_t const O_RDWR = (O_READ | O_WRITE); +/** open() oflag mask for access modes */ +uint8_t const O_ACCMODE = (O_READ | O_WRITE); +/** The file offset shall be set to the end of the file prior to each write. */ +uint8_t const O_APPEND = 0X04; +/** synchronous writes - call sync() after each write */ +uint8_t const O_SYNC = 0X08; +/** truncate the file to zero length */ +uint8_t const O_TRUNC = 0X10; +/** set the initial position at the end of the file */ +uint8_t const O_AT_END = 0X20; +/** create the file if nonexistent */ +uint8_t const O_CREAT = 0X40; +/** If O_CREAT and O_EXCL are set, open() shall fail if the file exists */ +uint8_t const O_EXCL = 0X80; + +// SdBaseFile class static and const definitions +// flags for ls() +/** ls() flag to print modify date */ +uint8_t const LS_DATE = 1; +/** ls() flag to print file size */ +uint8_t const LS_SIZE = 2; +/** ls() flag for recursive list of subdirectories */ +uint8_t const LS_R = 4; + + +// flags for timestamp +/** set the file's last access date */ +uint8_t const T_ACCESS = 1; +/** set the file's creation date and time */ +uint8_t const T_CREATE = 2; +/** Set the file's write date and time */ +uint8_t const T_WRITE = 4; +// values for type_ +/** This file has not been opened. */ +uint8_t const FAT_FILE_TYPE_CLOSED = 0; +/** A normal file */ +uint8_t const FAT_FILE_TYPE_NORMAL = 1; +/** A FAT12 or FAT16 root directory */ +uint8_t const FAT_FILE_TYPE_ROOT_FIXED = 2; +/** A FAT32 root directory */ +uint8_t const FAT_FILE_TYPE_ROOT32 = 3; +/** A subdirectory file*/ +uint8_t const FAT_FILE_TYPE_SUBDIR = 4; +/** Test value for directory type */ +uint8_t const FAT_FILE_TYPE_MIN_DIR = FAT_FILE_TYPE_ROOT_FIXED; + +/** date field for FAT directory entry + * \param[in] year [1980,2107] + * \param[in] month [1,12] + * \param[in] day [1,31] + * + * \return Packed date for dir_t entry. + */ +static inline uint16_t FAT_DATE(uint16_t year, uint8_t month, uint8_t day) { + return (year - 1980) << 9 | month << 5 | day; +} +/** year part of FAT directory date field + * \param[in] fatDate Date in packed dir format. + * + * \return Extracted year [1980,2107] + */ +static inline uint16_t FAT_YEAR(uint16_t fatDate) { + return 1980 + (fatDate >> 9); +} +/** month part of FAT directory date field + * \param[in] fatDate Date in packed dir format. + * + * \return Extracted month [1,12] + */ +static inline uint8_t FAT_MONTH(uint16_t fatDate) { + return (fatDate >> 5) & 0XF; +} +/** day part of FAT directory date field + * \param[in] fatDate Date in packed dir format. + * + * \return Extracted day [1,31] + */ +static inline uint8_t FAT_DAY(uint16_t fatDate) { + return fatDate & 0X1F; +} +/** time field for FAT directory entry + * \param[in] hour [0,23] + * \param[in] minute [0,59] + * \param[in] second [0,59] + * + * \return Packed time for dir_t entry. + */ +static inline uint16_t FAT_TIME(uint8_t hour, uint8_t minute, uint8_t second) { + return hour << 11 | minute << 5 | second >> 1; +} +/** hour part of FAT directory time field + * \param[in] fatTime Time in packed dir format. + * + * \return Extracted hour [0,23] + */ +static inline uint8_t FAT_HOUR(uint16_t fatTime) { + return fatTime >> 11; +} +/** minute part of FAT directory time field + * \param[in] fatTime Time in packed dir format. + * + * \return Extracted minute [0,59] + */ +static inline uint8_t FAT_MINUTE(uint16_t fatTime) { + return(fatTime >> 5) & 0X3F; +} +/** second part of FAT directory time field + * Note second/2 is stored in packed time. + * + * \param[in] fatTime Time in packed dir format. + * + * \return Extracted second [0,58] + */ +static inline uint8_t FAT_SECOND(uint16_t fatTime) { + return 2*(fatTime & 0X1F); +} +/** Default date for file timestamps is 1 Jan 2000 */ +uint16_t const FAT_DEFAULT_DATE = ((2000 - 1980) << 9) | (1 << 5) | 1; +/** Default time for file timestamp is 1 am */ +uint16_t const FAT_DEFAULT_TIME = (1 << 11); +//------------------------------------------------------------------------------ +/** + * \class SdBaseFile + * \brief Base class for SdFile with Print and C++ streams. + */ +class SdBaseFile { + public: + /** Create an instance. */ + SdBaseFile() : writeError(false), type_(FAT_FILE_TYPE_CLOSED) {} + SdBaseFile(const char* path, uint8_t oflag); + ~SdBaseFile() {if(isOpen()) close();} + /** + * writeError is set to true if an error occurs during a write(). + * Set writeError to false before calling print() and/or write() and check + * for true after calls to print() and/or write(). + */ + bool writeError; + //---------------------------------------------------------------------------- + // helpers for stream classes + /** get position for streams + * \param[out] pos struct to receive position + */ + void getpos(filepos_t* pos); + /** set position for streams + * \param[out] pos struct with value for new position + */ + void setpos(filepos_t* pos); + //---------------------------------------------------------------------------- + bool close(); + bool contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock); + bool createContiguous(SdBaseFile* dirFile, + const char* path, uint32_t size); + /** \return The current cluster number for a file or directory. */ + uint32_t curCluster() const {return curCluster_;} + /** \return The current position for a file or directory. */ + uint32_t curPosition() const {return curPosition_;} + /** \return Current working directory */ + static SdBaseFile* cwd() {return cwd_;} + /** Set the date/time callback function + * + * \param[in] dateTime The user's call back function. The callback + * function is of the form: + * + * \code + * void dateTime(uint16_t* date, uint16_t* time) { + * uint16_t year; + * uint8_t month, day, hour, minute, second; + * + * // User gets date and time from GPS or real-time clock here + * + * // return date using FAT_DATE macro to format fields + * *date = FAT_DATE(year, month, day); + * + * // return time using FAT_TIME macro to format fields + * *time = FAT_TIME(hour, minute, second); + * } + * \endcode + * + * Sets the function that is called when a file is created or when + * a file's directory entry is modified by sync(). All timestamps, + * access, creation, and modify, are set when a file is created. + * sync() maintains the last access date and last modify date/time. + * + * See the timestamp() function. + */ + static void dateTimeCallback( + void (*dateTime)(uint16_t* date, uint16_t* time)) { + dateTime_ = dateTime; + } + /** Cancel the date/time callback function. */ + static void dateTimeCallbackCancel() {dateTime_ = 0;} + bool dirEntry(dir_t* dir); + static void dirName(const dir_t& dir, char* name); + bool exists(const char* name); + int16_t fgets(char* str, int16_t num, char* delim = 0); + /** \return The total number of bytes in a file or directory. */ + uint32_t fileSize() const {return fileSize_;} + /** \return The first cluster number for a file or directory. */ + uint32_t firstCluster() const {return firstCluster_;} + bool getFilename(char* name); + /** \return True if this is a directory else false. */ + bool isDir() const {return type_ >= FAT_FILE_TYPE_MIN_DIR;} + /** \return True if this is a normal file else false. */ + bool isFile() const {return type_ == FAT_FILE_TYPE_NORMAL;} + /** \return True if this is an open file/directory else false. */ + bool isOpen() const {return type_ != FAT_FILE_TYPE_CLOSED;} + /** \return True if this is a subdirectory else false. */ + bool isSubDir() const {return type_ == FAT_FILE_TYPE_SUBDIR;} + /** \return True if this is the root directory. */ + bool isRoot() const { + return type_ == FAT_FILE_TYPE_ROOT_FIXED || type_ == FAT_FILE_TYPE_ROOT32; + } + void ls( uint8_t flags = 0, uint8_t indent = 0); + bool mkdir(SdBaseFile* dir, const char* path, bool pFlag = true); + // alias for backward compactability + bool makeDir(SdBaseFile* dir, const char* path) { + return mkdir(dir, path, false); + } + bool open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag); + bool open(SdBaseFile* dirFile, const char* path, uint8_t oflag); + bool open(const char* path, uint8_t oflag = O_READ); + bool openNext(SdBaseFile* dirFile, uint8_t oflag); + bool openRoot(SdVolume* vol); + int peek(); + static void printFatDate(uint16_t fatDate); + static void printFatTime( uint16_t fatTime); + bool printName(); + int16_t read(); + int16_t read(void* buf, uint16_t nbyte); + int8_t readDir(dir_t* dir, char* longFilename); + static bool remove(SdBaseFile* dirFile, const char* path); + bool remove(); + /** Set the file's current position to zero. */ + void rewind() {seekSet(0);} + bool rename(SdBaseFile* dirFile, const char* newPath); + bool rmdir(); + // for backward compatibility + bool rmDir() {return rmdir();} + bool rmRfStar(); + /** Set the files position to current position + \a pos. See seekSet(). + * \param[in] offset The new position in bytes from the current position. + * \return true for success or false for failure. + */ + bool seekCur(int32_t offset) { + return seekSet(curPosition_ + offset); + } + /** Set the files position to end-of-file + \a offset. See seekSet(). + * \param[in] offset The new position in bytes from end-of-file. + * \return true for success or false for failure. + */ + bool seekEnd(int32_t offset = 0) {return seekSet(fileSize_ + offset);} + bool seekSet(uint32_t pos); + bool sync(); + bool timestamp(SdBaseFile* file); + bool timestamp(uint8_t flag, uint16_t year, uint8_t month, uint8_t day, + uint8_t hour, uint8_t minute, uint8_t second); + /** Type of file. You should use isFile() or isDir() instead of type() + * if possible. + * + * \return The file or directory type. + */ + uint8_t type() const {return type_;} + bool truncate(uint32_t size); + /** \return SdVolume that contains this file. */ + SdVolume* volume() const {return vol_;} + int16_t write(const void* buf, uint16_t nbyte); +//------------------------------------------------------------------------------ + private: + // allow SdFat to set cwd_ + friend class SdFat; + // global pointer to cwd dir + static SdBaseFile* cwd_; + // data time callback function + static void (*dateTime_)(uint16_t* date, uint16_t* time); + // bits defined in flags_ + // should be 0X0F + static uint8_t const F_OFLAG = (O_ACCMODE | O_APPEND | O_SYNC); + // sync of directory entry required + static uint8_t const F_FILE_DIR_DIRTY = 0X80; + + // private data + uint8_t flags_; // See above for definition of flags_ bits + uint8_t fstate_; // error and eof indicator + uint8_t type_; // type of file see above for values + uint32_t curCluster_; // cluster for current file position + uint32_t curPosition_; // current file position in bytes from beginning + uint32_t dirBlock_; // block for this files directory entry + uint8_t dirIndex_; // index of directory entry in dirBlock + uint32_t fileSize_; // file size in bytes + uint32_t firstCluster_; // first cluster of file + SdVolume* vol_; // volume where file is located + + /** experimental don't use */ + bool openParent(SdBaseFile* dir); + // private functions + bool addCluster(); + bool addDirCluster(); + dir_t* cacheDirEntry(uint8_t action); + int8_t lsPrintNext( uint8_t flags, uint8_t indent); + static bool make83Name(const char* str, uint8_t* name, const char** ptr); + bool mkdir(SdBaseFile* parent, const uint8_t dname[11]); + bool open(SdBaseFile* dirFile, const uint8_t dname[11], uint8_t oflag); + bool openCachedEntry(uint8_t cacheIndex, uint8_t oflags); + dir_t* readDirCache(); +//------------------------------------------------------------------------------ +// to be deleted + static void printDirName( const dir_t& dir, + uint8_t width, bool printSlash); +//------------------------------------------------------------------------------ +// Deprecated functions - suppress cpplint warnings with NOLINT comment +#if ALLOW_DEPRECATED_FUNCTIONS && !defined(DOXYGEN) + public: + /** \deprecated Use: + * bool contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock); + * \param[out] bgnBlock the first block address for the file. + * \param[out] endBlock the last block address for the file. + * \return true for success or false for failure. + */ + bool contiguousRange(uint32_t& bgnBlock, uint32_t& endBlock) { // NOLINT + return contiguousRange(&bgnBlock, &endBlock); + } + /** \deprecated Use: + * bool createContiguous(SdBaseFile* dirFile, + * const char* path, uint32_t size) + * \param[in] dirFile The directory where the file will be created. + * \param[in] path A path with a valid DOS 8.3 file name. + * \param[in] size The desired file size. + * \return true for success or false for failure. + */ + bool createContiguous(SdBaseFile& dirFile, // NOLINT + const char* path, uint32_t size) { + return createContiguous(&dirFile, path, size); + } + /** \deprecated Use: + * static void dateTimeCallback( + * void (*dateTime)(uint16_t* date, uint16_t* time)); + * \param[in] dateTime The user's call back function. + */ + static void dateTimeCallback( + void (*dateTime)(uint16_t& date, uint16_t& time)) { // NOLINT + oldDateTime_ = dateTime; + dateTime_ = dateTime ? oldToNew : 0; + } + /** \deprecated Use: bool dirEntry(dir_t* dir); + * \param[out] dir Location for return of the file's directory entry. + * \return true for success or false for failure. + */ + bool dirEntry(dir_t& dir) {return dirEntry(&dir);} // NOLINT + /** \deprecated Use: + * bool mkdir(SdBaseFile* dir, const char* path); + * \param[in] dir An open SdFat instance for the directory that will contain + * the new directory. + * \param[in] path A path with a valid 8.3 DOS name for the new directory. + * \return true for success or false for failure. + */ + bool mkdir(SdBaseFile& dir, const char* path) { // NOLINT + return mkdir(&dir, path); + } + /** \deprecated Use: + * bool open(SdBaseFile* dirFile, const char* path, uint8_t oflag); + * \param[in] dirFile An open SdFat instance for the directory containing the + * file to be opened. + * \param[in] path A path with a valid 8.3 DOS name for the file. + * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive + * OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC. + * \return true for success or false for failure. + */ + bool open(SdBaseFile& dirFile, // NOLINT + const char* path, uint8_t oflag) { + return open(&dirFile, path, oflag); + } + /** \deprecated Do not use in new apps + * \param[in] dirFile An open SdFat instance for the directory containing the + * file to be opened. + * \param[in] path A path with a valid 8.3 DOS name for a file to be opened. + * \return true for success or false for failure. + */ + bool open(SdBaseFile& dirFile, const char* path) { // NOLINT + return open(dirFile, path, O_RDWR); + } + /** \deprecated Use: + * bool open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag); + * \param[in] dirFile An open SdFat instance for the directory. + * \param[in] index The \a index of the directory entry for the file to be + * opened. The value for \a index is (directory file position)/32. + * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive + * OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC. + * \return true for success or false for failure. + */ + bool open(SdBaseFile& dirFile, uint16_t index, uint8_t oflag) { // NOLINT + return open(&dirFile, index, oflag); + } + /** \deprecated Use: bool openRoot(SdVolume* vol); + * \param[in] vol The FAT volume containing the root directory to be opened. + * \return true for success or false for failure. + */ + bool openRoot(SdVolume& vol) {return openRoot(&vol);} // NOLINT + /** \deprecated Use: int8_t readDir(dir_t* dir); + * \param[out] dir The dir_t struct that will receive the data. + * \return bytes read for success zero for eof or -1 for failure. + */ + int8_t readDir(dir_t& dir, char* longFilename) {return readDir(&dir, longFilename);} // NOLINT + /** \deprecated Use: + * static uint8_t remove(SdBaseFile* dirFile, const char* path); + * \param[in] dirFile The directory that contains the file. + * \param[in] path The name of the file to be removed. + * \return true for success or false for failure. + */ + static bool remove(SdBaseFile& dirFile, const char* path) { // NOLINT + return remove(&dirFile, path); + } +//------------------------------------------------------------------------------ +// rest are private + private: + static void (*oldDateTime_)(uint16_t& date, uint16_t& time); // NOLINT + static void oldToNew(uint16_t* date, uint16_t* time) { + uint16_t d; + uint16_t t; + oldDateTime_(d, t); + *date = d; + *time = t; + } +#endif // ALLOW_DEPRECATED_FUNCTIONS +}; + +#endif // SdBaseFile_h +#endif diff --git a/Firmware/SdFatConfig.h b/Firmware/SdFatConfig.h new file mode 100644 index 0000000..7501434 --- /dev/null +++ b/Firmware/SdFatConfig.h @@ -0,0 +1,123 @@ +/* Arduino SdFat Library + * Copyright (C) 2009 by William Greiman + * + * This file is part of the Arduino SdFat Library + * + * This Library 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 Library 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 the Arduino SdFat Library. If not, see + * . + */ +/** + * \file + * \brief configuration definitions + */ +#include "Marlin.h" +#ifdef SDSUPPORT + +#ifndef SdFatConfig_h +#define SdFatConfig_h +#include +//------------------------------------------------------------------------------ +/** + * To use multiple SD cards set USE_MULTIPLE_CARDS nonzero. + * + * Using multiple cards costs 400 - 500 bytes of flash. + * + * Each card requires about 550 bytes of SRAM so use of a Mega is recommended. + */ +#define USE_MULTIPLE_CARDS 0 +//------------------------------------------------------------------------------ +/** + * Call flush for endl if ENDL_CALLS_FLUSH is nonzero + * + * The standard for iostreams is to call flush. This is very costly for + * SdFat. Each call to flush causes 2048 bytes of I/O to the SD. + * + * SdFat has a single 512 byte buffer for SD I/O so it must write the current + * data block to the SD, read the directory block from the SD, update the + * directory entry, write the directory block to the SD and read the data + * block back into the buffer. + * + * The SD flash memory controller is not designed for this many rewrites + * so performance may be reduced by more than a factor of 100. + * + * If ENDL_CALLS_FLUSH is zero, you must call flush and/or close to force + * all data to be written to the SD. + */ +#define ENDL_CALLS_FLUSH 0 +//------------------------------------------------------------------------------ +/** + * Allow use of deprecated functions if ALLOW_DEPRECATED_FUNCTIONS is nonzero + */ +#define ALLOW_DEPRECATED_FUNCTIONS 1 +//------------------------------------------------------------------------------ +/** + * Allow FAT12 volumes if FAT12_SUPPORT is nonzero. + * FAT12 has not been well tested. + */ +#define FAT12_SUPPORT 0 +//------------------------------------------------------------------------------ +/** + * SPI init rate for SD initialization commands. Must be 5 (F_CPU/64) + * or 6 (F_CPU/128). + */ +#define SPI_SD_INIT_RATE 5 +//------------------------------------------------------------------------------ +/** + * Set the SS pin high for hardware SPI. If SS is chip select for another SPI + * device this will disable that device during the SD init phase. + */ +#define SET_SPI_SS_HIGH 1 +//------------------------------------------------------------------------------ +/** + * Define MEGA_SOFT_SPI nonzero to use software SPI on Mega Arduinos. + * Pins used are SS 10, MOSI 11, MISO 12, and SCK 13. + * + * MEGA_SOFT_SPI allows an unmodified Adafruit GPS Shield to be used + * on Mega Arduinos. Software SPI works well with GPS Shield V1.1 + * but many SD cards will fail with GPS Shield V1.0. + */ +#define MEGA_SOFT_SPI 0 +//------------------------------------------------------------------------------ +/** + * Set USE_SOFTWARE_SPI nonzero to always use software SPI. + */ +#define USE_SOFTWARE_SPI 0 +// define software SPI pins so Mega can use unmodified 168/328 shields +/** Software SPI chip select pin for the SD */ +uint8_t const SOFT_SPI_CS_PIN = 10; +/** Software SPI Master Out Slave In pin */ +uint8_t const SOFT_SPI_MOSI_PIN = 11; +/** Software SPI Master In Slave Out pin */ +uint8_t const SOFT_SPI_MISO_PIN = 12; +/** Software SPI Clock pin */ +uint8_t const SOFT_SPI_SCK_PIN = 13; +//------------------------------------------------------------------------------ +/** + * The __cxa_pure_virtual function is an error handler that is invoked when + * a pure virtual function is called. + */ +#define USE_CXA_PURE_VIRTUAL 1 +/** + * Defines for long (vfat) filenames + */ +/** Number of VFAT entries used. Every entry has 13 UTF-16 characters */ +#define MAX_VFAT_ENTRIES (4) +/** Number of UTF-16 characters per entry */ +#define FILENAME_LENGTH 13 +/** Total size of the buffer used to store the long filenames */ +#define LONG_FILENAME_LENGTH (13*MAX_VFAT_ENTRIES+1) +#endif // SdFatConfig_h + + +#endif diff --git a/Firmware/SdFatStructs.h b/Firmware/SdFatStructs.h new file mode 100644 index 0000000..3867216 --- /dev/null +++ b/Firmware/SdFatStructs.h @@ -0,0 +1,646 @@ +/* Arduino SdFat Library + * Copyright (C) 2009 by William Greiman + * + * This file is part of the Arduino SdFat Library + * + * This Library 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 Library 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 the Arduino SdFat Library. If not, see + * . + */ +#include "Marlin.h" +#ifdef SDSUPPORT + +#ifndef SdFatStructs_h +#define SdFatStructs_h + +#define PACKED __attribute__((__packed__)) +/** + * \file + * \brief FAT file structures + */ +/* + * mostly from Microsoft document fatgen103.doc + * http://www.microsoft.com/whdc/system/platform/firmware/fatgen.mspx + */ +//------------------------------------------------------------------------------ +/** Value for byte 510 of boot block or MBR */ +uint8_t const BOOTSIG0 = 0X55; +/** Value for byte 511 of boot block or MBR */ +uint8_t const BOOTSIG1 = 0XAA; +/** Value for bootSignature field int FAT/FAT32 boot sector */ +uint8_t const EXTENDED_BOOT_SIG = 0X29; +//------------------------------------------------------------------------------ +/** + * \struct partitionTable + * \brief MBR partition table entry + * + * A partition table entry for a MBR formatted storage device. + * The MBR partition table has four entries. + */ +struct partitionTable { + /** + * Boot Indicator . Indicates whether the volume is the active + * partition. Legal values include: 0X00. Do not use for booting. + * 0X80 Active partition. + */ + uint8_t boot; + /** + * Head part of Cylinder-head-sector address of the first block in + * the partition. Legal values are 0-255. Only used in old PC BIOS. + */ + uint8_t beginHead; + /** + * Sector part of Cylinder-head-sector address of the first block in + * the partition. Legal values are 1-63. Only used in old PC BIOS. + */ + unsigned beginSector : 6; + /** High bits cylinder for first block in partition. */ + unsigned beginCylinderHigh : 2; + /** + * Combine beginCylinderLow with beginCylinderHigh. Legal values + * are 0-1023. Only used in old PC BIOS. + */ + uint8_t beginCylinderLow; + /** + * Partition type. See defines that begin with PART_TYPE_ for + * some Microsoft partition types. + */ + uint8_t type; + /** + * head part of cylinder-head-sector address of the last sector in the + * partition. Legal values are 0-255. Only used in old PC BIOS. + */ + uint8_t endHead; + /** + * Sector part of cylinder-head-sector address of the last sector in + * the partition. Legal values are 1-63. Only used in old PC BIOS. + */ + unsigned endSector : 6; + /** High bits of end cylinder */ + unsigned endCylinderHigh : 2; + /** + * Combine endCylinderLow with endCylinderHigh. Legal values + * are 0-1023. Only used in old PC BIOS. + */ + uint8_t endCylinderLow; + /** Logical block address of the first block in the partition. */ + uint32_t firstSector; + /** Length of the partition, in blocks. */ + uint32_t totalSectors; +} PACKED; +/** Type name for partitionTable */ +typedef struct partitionTable part_t; +//------------------------------------------------------------------------------ +/** + * \struct masterBootRecord + * + * \brief Master Boot Record + * + * The first block of a storage device that is formatted with a MBR. + */ +struct masterBootRecord { + /** Code Area for master boot program. */ + uint8_t codeArea[440]; + /** Optional Windows NT disk signature. May contain boot code. */ + uint32_t diskSignature; + /** Usually zero but may be more boot code. */ + uint16_t usuallyZero; + /** Partition tables. */ + part_t part[4]; + /** First MBR signature byte. Must be 0X55 */ + uint8_t mbrSig0; + /** Second MBR signature byte. Must be 0XAA */ + uint8_t mbrSig1; +} PACKED; +/** Type name for masterBootRecord */ +typedef struct masterBootRecord mbr_t; +//------------------------------------------------------------------------------ +/** + * \struct fat_boot + * + * \brief Boot sector for a FAT12/FAT16 volume. + * + */ +struct fat_boot { + /** + * The first three bytes of the boot sector must be valid, + * executable x 86-based CPU instructions. This includes a + * jump instruction that skips the next nonexecutable bytes. + */ + uint8_t jump[3]; + /** + * This is typically a string of characters that identifies + * the operating system that formatted the volume. + */ + char oemId[8]; + /** + * The size of a hardware sector. Valid decimal values for this + * field are 512, 1024, 2048, and 4096. For most disks used in + * the United States, the value of this field is 512. + */ + uint16_t bytesPerSector; + /** + * Number of sectors per allocation unit. This value must be a + * power of 2 that is greater than 0. The legal values are + * 1, 2, 4, 8, 16, 32, 64, and 128. 128 should be avoided. + */ + uint8_t sectorsPerCluster; + /** + * The number of sectors preceding the start of the first FAT, + * including the boot sector. The value of this field is always 1. + */ + uint16_t reservedSectorCount; + /** + * The number of copies of the FAT on the volume. + * The value of this field is always 2. + */ + uint8_t fatCount; + /** + * For FAT12 and FAT16 volumes, this field contains the count of + * 32-byte directory entries in the root directory. For FAT32 volumes, + * this field must be set to 0. For FAT12 and FAT16 volumes, this + * value should always specify a count that when multiplied by 32 + * results in a multiple of bytesPerSector. FAT16 volumes should + * use the value 512. + */ + uint16_t rootDirEntryCount; + /** + * This field is the old 16-bit total count of sectors on the volume. + * This count includes the count of all sectors in all four regions + * of the volume. This field can be 0; if it is 0, then totalSectors32 + * must be nonzero. For FAT32 volumes, this field must be 0. For + * FAT12 and FAT16 volumes, this field contains the sector count, and + * totalSectors32 is 0 if the total sector count fits + * (is less than 0x10000). + */ + uint16_t totalSectors16; + /** + * This dates back to the old MS-DOS 1.x media determination and is + * no longer usually used for anything. 0xF8 is the standard value + * for fixed (nonremovable) media. For removable media, 0xF0 is + * frequently used. Legal values are 0xF0 or 0xF8-0xFF. + */ + uint8_t mediaType; + /** + * Count of sectors occupied by one FAT on FAT12/FAT16 volumes. + * On FAT32 volumes this field must be 0, and sectorsPerFat32 + * contains the FAT size count. + */ + uint16_t sectorsPerFat16; + /** Sectors per track for interrupt 0x13. Not used otherwise. */ + uint16_t sectorsPerTrack; + /** Number of heads for interrupt 0x13. Not used otherwise. */ + uint16_t headCount; + /** + * Count of hidden sectors preceding the partition that contains this + * FAT volume. This field is generally only relevant for media + * visible on interrupt 0x13. + */ + uint32_t hidddenSectors; + /** + * This field is the new 32-bit total count of sectors on the volume. + * This count includes the count of all sectors in all four regions + * of the volume. This field can be 0; if it is 0, then + * totalSectors16 must be nonzero. + */ + uint32_t totalSectors32; + /** + * Related to the BIOS physical drive number. Floppy drives are + * identified as 0x00 and physical hard disks are identified as + * 0x80, regardless of the number of physical disk drives. + * Typically, this value is set prior to issuing an INT 13h BIOS + * call to specify the device to access. The value is only + * relevant if the device is a boot device. + */ + uint8_t driveNumber; + /** used by Windows NT - should be zero for FAT */ + uint8_t reserved1; + /** 0X29 if next three fields are valid */ + uint8_t bootSignature; + /** + * A random serial number created when formatting a disk, + * which helps to distinguish between disks. + * Usually generated by combining date and time. + */ + uint32_t volumeSerialNumber; + /** + * A field once used to store the volume label. The volume label + * is now stored as a special file in the root directory. + */ + char volumeLabel[11]; + /** + * A field with a value of either FAT, FAT12 or FAT16, + * depending on the disk format. + */ + char fileSystemType[8]; + /** X86 boot code */ + uint8_t bootCode[448]; + /** must be 0X55 */ + uint8_t bootSectorSig0; + /** must be 0XAA */ + uint8_t bootSectorSig1; +} PACKED; +/** Type name for FAT Boot Sector */ +typedef struct fat_boot fat_boot_t; +//------------------------------------------------------------------------------ +/** + * \struct fat32_boot + * + * \brief Boot sector for a FAT32 volume. + * + */ +struct fat32_boot { + /** + * The first three bytes of the boot sector must be valid, + * executable x 86-based CPU instructions. This includes a + * jump instruction that skips the next nonexecutable bytes. + */ + uint8_t jump[3]; + /** + * This is typically a string of characters that identifies + * the operating system that formatted the volume. + */ + char oemId[8]; + /** + * The size of a hardware sector. Valid decimal values for this + * field are 512, 1024, 2048, and 4096. For most disks used in + * the United States, the value of this field is 512. + */ + uint16_t bytesPerSector; + /** + * Number of sectors per allocation unit. This value must be a + * power of 2 that is greater than 0. The legal values are + * 1, 2, 4, 8, 16, 32, 64, and 128. 128 should be avoided. + */ + uint8_t sectorsPerCluster; + /** + * The number of sectors preceding the start of the first FAT, + * including the boot sector. Must not be zero + */ + uint16_t reservedSectorCount; + /** + * The number of copies of the FAT on the volume. + * The value of this field is always 2. + */ + uint8_t fatCount; + /** + * FAT12/FAT16 only. For FAT32 volumes, this field must be set to 0. + */ + uint16_t rootDirEntryCount; + /** + * For FAT32 volumes, this field must be 0. + */ + uint16_t totalSectors16; + /** + * This dates back to the old MS-DOS 1.x media determination and is + * no longer usually used for anything. 0xF8 is the standard value + * for fixed (nonremovable) media. For removable media, 0xF0 is + * frequently used. Legal values are 0xF0 or 0xF8-0xFF. + */ + uint8_t mediaType; + /** + * On FAT32 volumes this field must be 0, and sectorsPerFat32 + * contains the FAT size count. + */ + uint16_t sectorsPerFat16; + /** Sectors per track for interrupt 0x13. Not used otherwise. */ + uint16_t sectorsPerTrack; + /** Number of heads for interrupt 0x13. Not used otherwise. */ + uint16_t headCount; + /** + * Count of hidden sectors preceding the partition that contains this + * FAT volume. This field is generally only relevant for media + * visible on interrupt 0x13. + */ + uint32_t hidddenSectors; + /** + * Contains the total number of sectors in the FAT32 volume. + */ + uint32_t totalSectors32; + /** + * Count of sectors occupied by one FAT on FAT32 volumes. + */ + uint32_t sectorsPerFat32; + /** + * This field is only defined for FAT32 media and does not exist on + * FAT12 and FAT16 media. + * Bits 0-3 -- Zero-based number of active FAT. + * Only valid if mirroring is disabled. + * Bits 4-6 -- Reserved. + * Bit 7 -- 0 means the FAT is mirrored at runtime into all FATs. + * -- 1 means only one FAT is active; it is the one referenced + * in bits 0-3. + * Bits 8-15 -- Reserved. + */ + uint16_t fat32Flags; + /** + * FAT32 version. High byte is major revision number. + * Low byte is minor revision number. Only 0.0 define. + */ + uint16_t fat32Version; + /** + * Cluster number of the first cluster of the root directory for FAT32. + * This usually 2 but not required to be 2. + */ + uint32_t fat32RootCluster; + /** + * Sector number of FSINFO structure in the reserved area of the + * FAT32 volume. Usually 1. + */ + uint16_t fat32FSInfo; + /** + * If nonzero, indicates the sector number in the reserved area + * of the volume of a copy of the boot record. Usually 6. + * No value other than 6 is recommended. + */ + uint16_t fat32BackBootBlock; + /** + * Reserved for future expansion. Code that formats FAT32 volumes + * should always set all of the bytes of this field to 0. + */ + uint8_t fat32Reserved[12]; + /** + * Related to the BIOS physical drive number. Floppy drives are + * identified as 0x00 and physical hard disks are identified as + * 0x80, regardless of the number of physical disk drives. + * Typically, this value is set prior to issuing an INT 13h BIOS + * call to specify the device to access. The value is only + * relevant if the device is a boot device. + */ + uint8_t driveNumber; + /** used by Windows NT - should be zero for FAT */ + uint8_t reserved1; + /** 0X29 if next three fields are valid */ + uint8_t bootSignature; + /** + * A random serial number created when formatting a disk, + * which helps to distinguish between disks. + * Usually generated by combining date and time. + */ + uint32_t volumeSerialNumber; + /** + * A field once used to store the volume label. The volume label + * is now stored as a special file in the root directory. + */ + char volumeLabel[11]; + /** + * A text field with a value of FAT32. + */ + char fileSystemType[8]; + /** X86 boot code */ + uint8_t bootCode[420]; + /** must be 0X55 */ + uint8_t bootSectorSig0; + /** must be 0XAA */ + uint8_t bootSectorSig1; +} PACKED; +/** Type name for FAT32 Boot Sector */ +typedef struct fat32_boot fat32_boot_t; +//------------------------------------------------------------------------------ +/** Lead signature for a FSINFO sector */ +uint32_t const FSINFO_LEAD_SIG = 0x41615252; +/** Struct signature for a FSINFO sector */ +uint32_t const FSINFO_STRUCT_SIG = 0x61417272; +/** + * \struct fat32_fsinfo + * + * \brief FSINFO sector for a FAT32 volume. + * + */ +struct fat32_fsinfo { + /** must be 0X52, 0X52, 0X61, 0X41 */ + uint32_t leadSignature; + /** must be zero */ + uint8_t reserved1[480]; + /** must be 0X72, 0X72, 0X41, 0X61 */ + uint32_t structSignature; + /** + * Contains the last known free cluster count on the volume. + * If the value is 0xFFFFFFFF, then the free count is unknown + * and must be computed. Any other value can be used, but is + * not necessarily correct. It should be range checked at least + * to make sure it is <= volume cluster count. + */ + uint32_t freeCount; + /** + * This is a hint for the FAT driver. It indicates the cluster + * number at which the driver should start looking for free clusters. + * If the value is 0xFFFFFFFF, then there is no hint and the driver + * should start looking at cluster 2. + */ + uint32_t nextFree; + /** must be zero */ + uint8_t reserved2[12]; + /** must be 0X00, 0X00, 0X55, 0XAA */ + uint8_t tailSignature[4]; +} PACKED; +/** Type name for FAT32 FSINFO Sector */ +typedef struct fat32_fsinfo fat32_fsinfo_t; +//------------------------------------------------------------------------------ +// End Of Chain values for FAT entries +/** FAT12 end of chain value used by Microsoft. */ +uint16_t const FAT12EOC = 0XFFF; +/** Minimum value for FAT12 EOC. Use to test for EOC. */ +uint16_t const FAT12EOC_MIN = 0XFF8; +/** FAT16 end of chain value used by Microsoft. */ +uint16_t const FAT16EOC = 0XFFFF; +/** Minimum value for FAT16 EOC. Use to test for EOC. */ +uint16_t const FAT16EOC_MIN = 0XFFF8; +/** FAT32 end of chain value used by Microsoft. */ +uint32_t const FAT32EOC = 0X0FFFFFFF; +/** Minimum value for FAT32 EOC. Use to test for EOC. */ +uint32_t const FAT32EOC_MIN = 0X0FFFFFF8; +/** Mask a for FAT32 entry. Entries are 28 bits. */ +uint32_t const FAT32MASK = 0X0FFFFFFF; +//------------------------------------------------------------------------------ +/** + * \struct directoryEntry + * \brief FAT short directory entry + * + * Short means short 8.3 name, not the entry size. + * + * Date Format. A FAT directory entry date stamp is a 16-bit field that is + * basically a date relative to the MS-DOS epoch of 01/01/1980. Here is the + * format (bit 0 is the LSB of the 16-bit word, bit 15 is the MSB of the + * 16-bit word): + * + * Bits 9-15: Count of years from 1980, valid value range 0-127 + * inclusive (1980-2107). + * + * Bits 5-8: Month of year, 1 = January, valid value range 1-12 inclusive. + * + * Bits 0-4: Day of month, valid value range 1-31 inclusive. + * + * Time Format. A FAT directory entry time stamp is a 16-bit field that has + * a granularity of 2 seconds. Here is the format (bit 0 is the LSB of the + * 16-bit word, bit 15 is the MSB of the 16-bit word). + * + * Bits 11-15: Hours, valid value range 0-23 inclusive. + * + * Bits 5-10: Minutes, valid value range 0-59 inclusive. + * + * Bits 0-4: 2-second count, valid value range 0-29 inclusive (0 - 58 seconds). + * + * The valid time range is from Midnight 00:00:00 to 23:59:58. + */ +struct directoryEntry { + /** Short 8.3 name. + * + * The first eight bytes contain the file name with blank fill. + * The last three bytes contain the file extension with blank fill. + */ + uint8_t name[11]; + /** Entry attributes. + * + * The upper two bits of the attribute byte are reserved and should + * always be set to 0 when a file is created and never modified or + * looked at after that. See defines that begin with DIR_ATT_. + */ + uint8_t attributes; + /** + * Reserved for use by Windows NT. Set value to 0 when a file is + * created and never modify or look at it after that. + */ + uint8_t reservedNT; + /** + * The granularity of the seconds part of creationTime is 2 seconds + * so this field is a count of tenths of a second and its valid + * value range is 0-199 inclusive. (WHG note - seems to be hundredths) + */ + uint8_t creationTimeTenths; + /** Time file was created. */ + uint16_t creationTime; + /** Date file was created. */ + uint16_t creationDate; + /** + * Last access date. Note that there is no last access time, only + * a date. This is the date of last read or write. In the case of + * a write, this should be set to the same date as lastWriteDate. + */ + uint16_t lastAccessDate; + /** + * High word of this entry's first cluster number (always 0 for a + * FAT12 or FAT16 volume). + */ + uint16_t firstClusterHigh; + /** Time of last write. File creation is considered a write. */ + uint16_t lastWriteTime; + /** Date of last write. File creation is considered a write. */ + uint16_t lastWriteDate; + /** Low word of this entry's first cluster number. */ + uint16_t firstClusterLow; + /** 32-bit unsigned holding this file's size in bytes. */ + uint32_t fileSize; +} PACKED; +/** + * \struct directoryVFATEntry + * \brief VFAT long filename directory entry + * + * directoryVFATEntries are found in the same list as normal directoryEntry. + * But have the attribute field set to DIR_ATT_LONG_NAME. + * + * Long filenames are saved in multiple directoryVFATEntries. + * Each entry containing 13 UTF-16 characters. + */ +struct directoryVFATEntry { + /** + * Sequence number. Consists of 2 parts: + * bit 6: indicates first long filename block for the next file + * bit 0-4: the position of this long filename block (first block is 1) + */ + uint8_t sequenceNumber; + /** First set of UTF-16 characters */ + uint16_t name1[5];//UTF-16 + /** attributes (at the same location as in directoryEntry), always 0x0F */ + uint8_t attributes; + /** Reserved for use by Windows NT. Always 0. */ + uint8_t reservedNT; + /** Checksum of the short 8.3 filename, can be used to checked if the file system as modified by a not-long-filename aware implementation. */ + uint8_t checksum; + /** Second set of UTF-16 characters */ + uint16_t name2[6];//UTF-16 + /** firstClusterLow is always zero for longFilenames */ + uint16_t firstClusterLow; + /** Third set of UTF-16 characters */ + uint16_t name3[2];//UTF-16 +} PACKED; +//------------------------------------------------------------------------------ +// Definitions for directory entries +// +/** Type name for directoryEntry */ +typedef struct directoryEntry dir_t; +/** Type name for directoryVFATEntry */ +typedef struct directoryVFATEntry vfat_t; +/** escape for name[0] = 0XE5 */ +uint8_t const DIR_NAME_0XE5 = 0X05; +/** name[0] value for entry that is free after being "deleted" */ +uint8_t const DIR_NAME_DELETED = 0XE5; +/** name[0] value for entry that is free and no allocated entries follow */ +uint8_t const DIR_NAME_FREE = 0X00; +/** file is read-only */ +uint8_t const DIR_ATT_READ_ONLY = 0X01; +/** File should hidden in directory listings */ +uint8_t const DIR_ATT_HIDDEN = 0X02; +/** Entry is for a system file */ +uint8_t const DIR_ATT_SYSTEM = 0X04; +/** Directory entry contains the volume label */ +uint8_t const DIR_ATT_VOLUME_ID = 0X08; +/** Entry is for a directory */ +uint8_t const DIR_ATT_DIRECTORY = 0X10; +/** Old DOS archive bit for backup support */ +uint8_t const DIR_ATT_ARCHIVE = 0X20; +/** Test value for long name entry. Test is + (d->attributes & DIR_ATT_LONG_NAME_MASK) == DIR_ATT_LONG_NAME. */ +uint8_t const DIR_ATT_LONG_NAME = 0X0F; +/** Test mask for long name entry */ +uint8_t const DIR_ATT_LONG_NAME_MASK = 0X3F; +/** defined attribute bits */ +uint8_t const DIR_ATT_DEFINED_BITS = 0X3F; +/** Directory entry is part of a long name + * \param[in] dir Pointer to a directory entry. + * + * \return true if the entry is for part of a long name else false. + */ +static inline uint8_t DIR_IS_LONG_NAME(const dir_t* dir) { + return (dir->attributes & DIR_ATT_LONG_NAME_MASK) == DIR_ATT_LONG_NAME; +} +/** Mask for file/subdirectory tests */ +uint8_t const DIR_ATT_FILE_TYPE_MASK = (DIR_ATT_VOLUME_ID | DIR_ATT_DIRECTORY); +/** Directory entry is for a file + * \param[in] dir Pointer to a directory entry. + * + * \return true if the entry is for a normal file else false. + */ +static inline uint8_t DIR_IS_FILE(const dir_t* dir) { + return (dir->attributes & DIR_ATT_FILE_TYPE_MASK) == 0; +} +/** Directory entry is for a subdirectory + * \param[in] dir Pointer to a directory entry. + * + * \return true if the entry is for a subdirectory else false. + */ +static inline uint8_t DIR_IS_SUBDIR(const dir_t* dir) { + return (dir->attributes & DIR_ATT_FILE_TYPE_MASK) == DIR_ATT_DIRECTORY; +} +/** Directory entry is for a file or subdirectory + * \param[in] dir Pointer to a directory entry. + * + * \return true if the entry is for a normal file or subdirectory else false. + */ +static inline uint8_t DIR_IS_FILE_OR_SUBDIR(const dir_t* dir) { + return (dir->attributes & DIR_ATT_VOLUME_ID) == 0; +} +#endif // SdFatStructs_h + + +#endif diff --git a/Firmware/SdFatUtil.cpp b/Firmware/SdFatUtil.cpp new file mode 100644 index 0000000..51da4ee --- /dev/null +++ b/Firmware/SdFatUtil.cpp @@ -0,0 +1,104 @@ +/* Arduino SdFat Library + * Copyright (C) 2008 by William Greiman + * + * This file is part of the Arduino SdFat Library + * + * This Library 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 Library 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 the Arduino SdFat Library. If not, see + * . + */ +#include "Marlin.h" + +#ifdef SDSUPPORT +#include "SdFatUtil.h" + +//------------------------------------------------------------------------------ +/** Amount of free RAM + * \return The number of free bytes. + */ +#ifdef __arm__ +extern "C" char* sbrk(int incr); +int SdFatUtil::FreeRam() { + char top; + return &top - reinterpret_cast(sbrk(0)); +} +#else // __arm__ +extern char *__brkval; +extern char __bss_end; +/** Amount of free RAM + * \return The number of free bytes. + */ +int SdFatUtil::FreeRam() { + char top; + return __brkval ? &top - __brkval : &top - &__bss_end; +} +#endif // __arm + +void SdFatUtil::set_stack_guard() +{ + uint32_t *stack_guard; + + stack_guard = (uint32_t*)&__bss_end; + *stack_guard = STACK_GUARD_TEST_VALUE; +} + +bool SdFatUtil::test_stack_integrity() +{ + uint32_t* stack_guard = (uint32_t*)&__bss_end; + return (*stack_guard == STACK_GUARD_TEST_VALUE); +} + +uint32_t SdFatUtil::get_stack_guard_test_value() +{ + uint32_t* stack_guard; + uint32_t output; + stack_guard = (uint32_t*)&__bss_end; + output = *stack_guard; + return(output); +} +//------------------------------------------------------------------------------ +/** %Print a string in flash memory. + * + * \param[in] pr Print object for output. + * \param[in] str Pointer to string stored in flash memory. + */ +void SdFatUtil::print_P( PGM_P str) { + for (uint8_t c; (c = pgm_read_byte(str)); str++) MYSERIAL.write(c); +} +//------------------------------------------------------------------------------ +/** %Print a string in flash memory followed by a CR/LF. + * + * \param[in] pr Print object for output. + * \param[in] str Pointer to string stored in flash memory. + */ +void SdFatUtil::println_P( PGM_P str) { + print_P( str); + MYSERIAL.println(); +} +//------------------------------------------------------------------------------ +/** %Print a string in flash memory to Serial. + * + * \param[in] str Pointer to string stored in flash memory. + */ +void SdFatUtil::SerialPrint_P(PGM_P str) { + print_P(str); +} +//------------------------------------------------------------------------------ +/** %Print a string in flash memory to Serial followed by a CR/LF. + * + * \param[in] str Pointer to string stored in flash memory. + */ +void SdFatUtil::SerialPrintln_P(PGM_P str) { + println_P( str); +} +#endif diff --git a/Firmware/SdFatUtil.h b/Firmware/SdFatUtil.h new file mode 100644 index 0000000..c42b74b --- /dev/null +++ b/Firmware/SdFatUtil.h @@ -0,0 +1,51 @@ +/* Arduino SdFat Library + * Copyright (C) 2008 by William Greiman + * + * This file is part of the Arduino SdFat Library + * + * This Library 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 Library 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 the Arduino SdFat Library. If not, see + * . + */ +#include "Marlin.h" +#ifdef SDSUPPORT + +#ifndef SdFatUtil_h +#define SdFatUtil_h +/** + * \file + * \brief Useful utility functions. + */ +#include "Marlin.h" +#include "MarlinSerial.h" +/** Store and print a string in flash memory.*/ +#define PgmPrint(x) SerialPrint_P(PSTR(x)) +/** Store and print a string in flash memory followed by a CR/LF.*/ +#define PgmPrintln(x) SerialPrintln_P(PSTR(x)) + +namespace SdFatUtil { + int FreeRam(); + void print_P( PGM_P str); + void println_P( PGM_P str); + void SerialPrint_P(PGM_P str); + void SerialPrintln_P(PGM_P str); + void set_stack_guard(); + bool test_stack_integrity(); + uint32_t get_stack_guard_test_value(); +} + +using namespace SdFatUtil; // NOLINT +#endif // #define SdFatUtil_h + + +#endif \ No newline at end of file diff --git a/Firmware/SdFile.cpp b/Firmware/SdFile.cpp new file mode 100644 index 0000000..2fb4d59 --- /dev/null +++ b/Firmware/SdFile.cpp @@ -0,0 +1,95 @@ +/* Arduino SdFat Library + * Copyright (C) 2009 by William Greiman + * + * This file is part of the Arduino SdFat Library + * + * This Library 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 Library 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 the Arduino SdFat Library. If not, see + * . + */ +#include "Marlin.h" + +#ifdef SDSUPPORT +#include "SdFile.h" +/** Create a file object and open it in the current working directory. + * + * \param[in] path A path with a valid 8.3 DOS name for a file to be opened. + * + * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive + * OR of open flags. see SdBaseFile::open(SdBaseFile*, const char*, uint8_t). + */ +SdFile::SdFile(const char* path, uint8_t oflag) : SdBaseFile(path, oflag) { +} +//------------------------------------------------------------------------------ +/** Write data to an open file. + * + * \note Data is moved to the cache but may not be written to the + * storage device until sync() is called. + * + * \param[in] buf Pointer to the location of the data to be written. + * + * \param[in] nbyte Number of bytes to write. + * + * \return For success write() returns the number of bytes written, always + * \a nbyte. If an error occurs, write() returns -1. Possible errors + * include write() is called before a file has been opened, write is called + * for a read-only file, device is full, a corrupt file system or an I/O error. + * + */ +int16_t SdFile::write(const void* buf, uint16_t nbyte) { + return SdBaseFile::write(buf, nbyte); +} +//------------------------------------------------------------------------------ +/** Write a byte to a file. Required by the Arduino Print class. + * \param[in] b the byte to be written. + * Use writeError to check for errors. + */ +#if ARDUINO >= 100 +size_t SdFile::write(uint8_t b) +{ + return SdBaseFile::write(&b, 1); +} +#else +void SdFile::write(uint8_t b) +{ + SdBaseFile::write(&b, 1); +} +#endif +//------------------------------------------------------------------------------ +/** Write a string to a file. Used by the Arduino Print class. + * \param[in] str Pointer to the string. + * Use writeError to check for errors. + */ +void SdFile::write(const char* str) { + SdBaseFile::write(str, strlen(str)); +} +//------------------------------------------------------------------------------ +/** Write a PROGMEM string to a file. + * \param[in] str Pointer to the PROGMEM string. + * Use writeError to check for errors. + */ +void SdFile::write_P(PGM_P str) { + for (uint8_t c; (c = pgm_read_byte(str)); str++) write(c); +} +//------------------------------------------------------------------------------ +/** Write a PROGMEM string followed by CR/LF to a file. + * \param[in] str Pointer to the PROGMEM string. + * Use writeError to check for errors. + */ +void SdFile::writeln_P(PGM_P str) { + write_P(str); + write_P(PSTR("\r\n")); +} + + +#endif diff --git a/Firmware/SdFile.h b/Firmware/SdFile.h new file mode 100644 index 0000000..60e2f5d --- /dev/null +++ b/Firmware/SdFile.h @@ -0,0 +1,54 @@ +/* Arduino SdFat Library + * Copyright (C) 2009 by William Greiman + * + * This file is part of the Arduino SdFat Library + * + * This Library 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 Library 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 the Arduino SdFat Library. If not, see + * . + */ +/** + * \file + * \brief SdFile class + */ +#include "Marlin.h" + +#ifdef SDSUPPORT +#include "SdBaseFile.h" +//#include +#ifndef SdFile_h +#define SdFile_h +//------------------------------------------------------------------------------ +/** + * \class SdFile + * \brief SdBaseFile with Print. + */ +class SdFile : public SdBaseFile/*, public Print*/ { + public: + SdFile() {} + SdFile(const char* name, uint8_t oflag); + #if ARDUINO >= 100 + size_t write(uint8_t b); + #else + void write(uint8_t b); + #endif + + int16_t write(const void* buf, uint16_t nbyte); + void write(const char* str); + void write_P(PGM_P str); + void writeln_P(PGM_P str); +}; +#endif // SdFile_h + + +#endif \ No newline at end of file diff --git a/Firmware/SdInfo.h b/Firmware/SdInfo.h new file mode 100644 index 0000000..93f0943 --- /dev/null +++ b/Firmware/SdInfo.h @@ -0,0 +1,286 @@ +/* Arduino Sd2Card Library + * Copyright (C) 2009 by William Greiman + * + * This file is part of the Arduino Sd2Card Library + * + * This Library 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 Library 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 the Arduino Sd2Card Library. If not, see + * . + */ +#include "Marlin.h" +#ifdef SDSUPPORT + +#ifndef SdInfo_h +#define SdInfo_h +#include +// Based on the document: +// +// SD Specifications +// Part 1 +// Physical Layer +// Simplified Specification +// Version 3.01 +// May 18, 2010 +// +// http://www.sdcard.org/developers/tech/sdcard/pls/simplified_specs +//------------------------------------------------------------------------------ +// SD card commands +/** GO_IDLE_STATE - init card in spi mode if CS low */ +uint8_t const CMD0 = 0X00; +/** SEND_IF_COND - verify SD Memory Card interface operating condition.*/ +uint8_t const CMD8 = 0X08; +/** SEND_CSD - read the Card Specific Data (CSD register) */ +uint8_t const CMD9 = 0X09; +/** SEND_CID - read the card identification information (CID register) */ +uint8_t const CMD10 = 0X0A; +/** STOP_TRANSMISSION - end multiple block read sequence */ +uint8_t const CMD12 = 0X0C; +/** SEND_STATUS - read the card status register */ +uint8_t const CMD13 = 0X0D; +/** READ_SINGLE_BLOCK - read a single data block from the card */ +uint8_t const CMD17 = 0X11; +/** READ_MULTIPLE_BLOCK - read a multiple data blocks from the card */ +uint8_t const CMD18 = 0X12; +/** WRITE_BLOCK - write a single data block to the card */ +uint8_t const CMD24 = 0X18; +/** WRITE_MULTIPLE_BLOCK - write blocks of data until a STOP_TRANSMISSION */ +uint8_t const CMD25 = 0X19; +/** ERASE_WR_BLK_START - sets the address of the first block to be erased */ +uint8_t const CMD32 = 0X20; +/** ERASE_WR_BLK_END - sets the address of the last block of the continuous + range to be erased*/ +uint8_t const CMD33 = 0X21; +/** ERASE - erase all previously selected blocks */ +uint8_t const CMD38 = 0X26; + +/** Toshiba FlashAir: iSDIO */ +uint8_t const CMD48 = 0x30; +/** Toshiba FlashAir: iSDIO */ +uint8_t const CMD49 = 0x31; + +/** APP_CMD - escape for application specific command */ +uint8_t const CMD55 = 0X37; +/** READ_OCR - read the OCR register of a card */ +uint8_t const CMD58 = 0X3A; +/** SET_WR_BLK_ERASE_COUNT - Set the number of write blocks to be + pre-erased before writing */ +uint8_t const ACMD23 = 0X17; +/** SD_SEND_OP_COMD - Sends host capacity support information and + activates the card's initialization process */ +uint8_t const ACMD41 = 0X29; +//------------------------------------------------------------------------------ +/** status for card in the ready state */ +uint8_t const R1_READY_STATE = 0X00; +/** status for card in the idle state */ +uint8_t const R1_IDLE_STATE = 0X01; +/** status bit for illegal command */ +uint8_t const R1_ILLEGAL_COMMAND = 0X04; +/** start data token for read or write single block*/ +uint8_t const DATA_START_BLOCK = 0XFE; +/** stop token for write multiple blocks*/ +uint8_t const STOP_TRAN_TOKEN = 0XFD; +/** start data token for write multiple blocks*/ +uint8_t const WRITE_MULTIPLE_TOKEN = 0XFC; +/** mask for data response tokens after a write block operation */ +uint8_t const DATA_RES_MASK = 0X1F; +/** write data accepted token */ +uint8_t const DATA_RES_ACCEPTED = 0X05; +//------------------------------------------------------------------------------ +/** Card IDentification (CID) register */ +typedef struct CID { + // byte 0 + /** Manufacturer ID */ + unsigned char mid; + // byte 1-2 + /** OEM/Application ID */ + char oid[2]; + // byte 3-7 + /** Product name */ + char pnm[5]; + // byte 8 + /** Product revision least significant digit */ + unsigned char prv_m : 4; + /** Product revision most significant digit */ + unsigned char prv_n : 4; + // byte 9-12 + /** Product serial number */ + uint32_t psn; + // byte 13 + /** Manufacturing date year low digit */ + unsigned char mdt_year_high : 4; + /** not used */ + unsigned char reserved : 4; + // byte 14 + /** Manufacturing date month */ + unsigned char mdt_month : 4; + /** Manufacturing date year low digit */ + unsigned char mdt_year_low :4; + // byte 15 + /** not used always 1 */ + unsigned char always1 : 1; + /** CRC7 checksum */ + unsigned char crc : 7; +}cid_t; +//------------------------------------------------------------------------------ +/** CSD for version 1.00 cards */ +typedef struct CSDV1 { + // byte 0 + unsigned char reserved1 : 6; + unsigned char csd_ver : 2; + // byte 1 + unsigned char taac; + // byte 2 + unsigned char nsac; + // byte 3 + unsigned char tran_speed; + // byte 4 + unsigned char ccc_high; + // byte 5 + unsigned char read_bl_len : 4; + unsigned char ccc_low : 4; + // byte 6 + unsigned char c_size_high : 2; + unsigned char reserved2 : 2; + unsigned char dsr_imp : 1; + unsigned char read_blk_misalign :1; + unsigned char write_blk_misalign : 1; + unsigned char read_bl_partial : 1; + // byte 7 + unsigned char c_size_mid; + // byte 8 + unsigned char vdd_r_curr_max : 3; + unsigned char vdd_r_curr_min : 3; + unsigned char c_size_low :2; + // byte 9 + unsigned char c_size_mult_high : 2; + unsigned char vdd_w_cur_max : 3; + unsigned char vdd_w_curr_min : 3; + // byte 10 + unsigned char sector_size_high : 6; + unsigned char erase_blk_en : 1; + unsigned char c_size_mult_low : 1; + // byte 11 + unsigned char wp_grp_size : 7; + unsigned char sector_size_low : 1; + // byte 12 + unsigned char write_bl_len_high : 2; + unsigned char r2w_factor : 3; + unsigned char reserved3 : 2; + unsigned char wp_grp_enable : 1; + // byte 13 + unsigned char reserved4 : 5; + unsigned char write_partial : 1; + unsigned char write_bl_len_low : 2; + // byte 14 + unsigned char reserved5: 2; + unsigned char file_format : 2; + unsigned char tmp_write_protect : 1; + unsigned char perm_write_protect : 1; + unsigned char copy : 1; + /** Indicates the file format on the card */ + unsigned char file_format_grp : 1; + // byte 15 + unsigned char always1 : 1; + unsigned char crc : 7; +}csd1_t; +//------------------------------------------------------------------------------ +/** CSD for version 2.00 cards */ +typedef struct CSDV2 { + // byte 0 + unsigned char reserved1 : 6; + unsigned char csd_ver : 2; + // byte 1 + /** fixed to 0X0E */ + unsigned char taac; + // byte 2 + /** fixed to 0 */ + unsigned char nsac; + // byte 3 + unsigned char tran_speed; + // byte 4 + unsigned char ccc_high; + // byte 5 + /** This field is fixed to 9h, which indicates READ_BL_LEN=512 Byte */ + unsigned char read_bl_len : 4; + unsigned char ccc_low : 4; + // byte 6 + /** not used */ + unsigned char reserved2 : 4; + unsigned char dsr_imp : 1; + /** fixed to 0 */ + unsigned char read_blk_misalign :1; + /** fixed to 0 */ + unsigned char write_blk_misalign : 1; + /** fixed to 0 - no partial read */ + unsigned char read_bl_partial : 1; + // byte 7 + /** not used */ + unsigned char reserved3 : 2; + /** high part of card size */ + unsigned char c_size_high : 6; + // byte 8 + /** middle part of card size */ + unsigned char c_size_mid; + // byte 9 + /** low part of card size */ + unsigned char c_size_low; + // byte 10 + /** sector size is fixed at 64 KB */ + unsigned char sector_size_high : 6; + /** fixed to 1 - erase single is supported */ + unsigned char erase_blk_en : 1; + /** not used */ + unsigned char reserved4 : 1; + // byte 11 + unsigned char wp_grp_size : 7; + /** sector size is fixed at 64 KB */ + unsigned char sector_size_low : 1; + // byte 12 + /** write_bl_len fixed for 512 byte blocks */ + unsigned char write_bl_len_high : 2; + /** fixed value of 2 */ + unsigned char r2w_factor : 3; + /** not used */ + unsigned char reserved5 : 2; + /** fixed value of 0 - no write protect groups */ + unsigned char wp_grp_enable : 1; + // byte 13 + unsigned char reserved6 : 5; + /** always zero - no partial block read*/ + unsigned char write_partial : 1; + /** write_bl_len fixed for 512 byte blocks */ + unsigned char write_bl_len_low : 2; + // byte 14 + unsigned char reserved7: 2; + /** Do not use always 0 */ + unsigned char file_format : 2; + unsigned char tmp_write_protect : 1; + unsigned char perm_write_protect : 1; + unsigned char copy : 1; + /** Do not use always 0 */ + unsigned char file_format_grp : 1; + // byte 15 + /** not used always 1 */ + unsigned char always1 : 1; + /** checksum */ + unsigned char crc : 7; +}csd2_t; +//------------------------------------------------------------------------------ +/** union of old and new style CSD register */ +union csd_t { + csd1_t v1; + csd2_t v2; +}; +#endif // SdInfo_h + +#endif \ No newline at end of file diff --git a/Firmware/SdVolume.cpp b/Firmware/SdVolume.cpp new file mode 100644 index 0000000..9d6abf3 --- /dev/null +++ b/Firmware/SdVolume.cpp @@ -0,0 +1,405 @@ +/* Arduino SdFat Library + * Copyright (C) 2009 by William Greiman + * + * This file is part of the Arduino SdFat Library + * + * This Library 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 Library 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 the Arduino SdFat Library. If not, see + * . + */ +#include "Marlin.h" +#ifdef SDSUPPORT + +#include "SdVolume.h" +//------------------------------------------------------------------------------ +#if !USE_MULTIPLE_CARDS +// raw block cache +uint32_t SdVolume::cacheBlockNumber_; // current block number +cache_t SdVolume::cacheBuffer_; // 512 byte cache for Sd2Card +Sd2Card* SdVolume::sdCard_; // pointer to SD card object +bool SdVolume::cacheDirty_; // cacheFlush() will write block if true +uint32_t SdVolume::cacheMirrorBlock_; // mirror block for second FAT +#endif // USE_MULTIPLE_CARDS +//------------------------------------------------------------------------------ +// find a contiguous group of clusters +bool SdVolume::allocContiguous(uint32_t count, uint32_t* curCluster) { + // start of group + uint32_t bgnCluster; + // end of group + uint32_t endCluster; + // last cluster of FAT + uint32_t fatEnd = clusterCount_ + 1; + + // flag to save place to start next search + bool setStart; + + // set search start cluster + if (*curCluster) { + // try to make file contiguous + bgnCluster = *curCluster + 1; + + // don't save new start location + setStart = false; + } else { + // start at likely place for free cluster + bgnCluster = allocSearchStart_; + + // save next search start if one cluster + setStart = count == 1; + } + // end of group + endCluster = bgnCluster; + + // search the FAT for free clusters + for (uint32_t n = 0;; n++, endCluster++) { + // can't find space checked all clusters + if (n >= clusterCount_) goto fail; + + // past end - start from beginning of FAT + if (endCluster > fatEnd) { + bgnCluster = endCluster = 2; + } + uint32_t f; + if (!fatGet(endCluster, &f)) goto fail; + + if (f != 0) { + // cluster in use try next cluster as bgnCluster + bgnCluster = endCluster + 1; + } else if ((endCluster - bgnCluster + 1) == count) { + // done - found space + break; + } + } + // mark end of chain + if (!fatPutEOC(endCluster)) goto fail; + + // link clusters + while (endCluster > bgnCluster) { + if (!fatPut(endCluster - 1, endCluster)) goto fail; + endCluster--; + } + if (*curCluster != 0) { + // connect chains + if (!fatPut(*curCluster, bgnCluster)) goto fail; + } + // return first cluster number to caller + *curCluster = bgnCluster; + + // remember possible next free cluster + if (setStart) allocSearchStart_ = bgnCluster + 1; + + return true; + + fail: + return false; +} +//------------------------------------------------------------------------------ +bool SdVolume::cacheFlush() { + if (cacheDirty_) { + if (!sdCard_->writeBlock(cacheBlockNumber_, cacheBuffer_.data)) { + goto fail; + } + // mirror FAT tables + if (cacheMirrorBlock_) { + if (!sdCard_->writeBlock(cacheMirrorBlock_, cacheBuffer_.data)) { + goto fail; + } + cacheMirrorBlock_ = 0; + } + cacheDirty_ = 0; + } + return true; + + fail: + return false; +} +//------------------------------------------------------------------------------ +bool SdVolume::cacheRawBlock(uint32_t blockNumber, bool dirty) { + if (cacheBlockNumber_ != blockNumber) { + if (!cacheFlush()) goto fail; + if (!sdCard_->readBlock(blockNumber, cacheBuffer_.data)) goto fail; + cacheBlockNumber_ = blockNumber; + } + if (dirty) cacheDirty_ = true; + return true; + + fail: + return false; +} +//------------------------------------------------------------------------------ +// return the size in bytes of a cluster chain +bool SdVolume::chainSize(uint32_t cluster, uint32_t* size) { + uint32_t s = 0; + do { + if (!fatGet(cluster, &cluster)) goto fail; + s += 512UL << clusterSizeShift_; + } while (!isEOC(cluster)); + *size = s; + return true; + + fail: + return false; +} +//------------------------------------------------------------------------------ +// Fetch a FAT entry +bool SdVolume::fatGet(uint32_t cluster, uint32_t* value) { + uint32_t lba; + if (cluster > (clusterCount_ + 1)) goto fail; + if (FAT12_SUPPORT && fatType_ == 12) { + uint16_t index = cluster; + index += index >> 1; + lba = fatStartBlock_ + (index >> 9); + if (!cacheRawBlock(lba, CACHE_FOR_READ)) goto fail; + index &= 0X1FF; + uint16_t tmp = cacheBuffer_.data[index]; + index++; + if (index == 512) { + if (!cacheRawBlock(lba + 1, CACHE_FOR_READ)) goto fail; + index = 0; + } + tmp |= cacheBuffer_.data[index] << 8; + *value = cluster & 1 ? tmp >> 4 : tmp & 0XFFF; + return true; + } + if (fatType_ == 16) { + lba = fatStartBlock_ + (cluster >> 8); + } else if (fatType_ == 32) { + lba = fatStartBlock_ + (cluster >> 7); + } else { + goto fail; + } + if (lba != cacheBlockNumber_) { + if (!cacheRawBlock(lba, CACHE_FOR_READ)) goto fail; + } + if (fatType_ == 16) { + *value = cacheBuffer_.fat16[cluster & 0XFF]; + } else { + *value = cacheBuffer_.fat32[cluster & 0X7F] & FAT32MASK; + } + return true; + + fail: + return false; +} +//------------------------------------------------------------------------------ +// Store a FAT entry +bool SdVolume::fatPut(uint32_t cluster, uint32_t value) { + uint32_t lba; + // error if reserved cluster + if (cluster < 2) goto fail; + + // error if not in FAT + if (cluster > (clusterCount_ + 1)) goto fail; + + if (FAT12_SUPPORT && fatType_ == 12) { + uint16_t index = cluster; + index += index >> 1; + lba = fatStartBlock_ + (index >> 9); + if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) goto fail; + // mirror second FAT + if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_; + index &= 0X1FF; + uint8_t tmp = value; + if (cluster & 1) { + tmp = (cacheBuffer_.data[index] & 0XF) | tmp << 4; + } + cacheBuffer_.data[index] = tmp; + index++; + if (index == 512) { + lba++; + index = 0; + if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) goto fail; + // mirror second FAT + if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_; + } + tmp = value >> 4; + if (!(cluster & 1)) { + tmp = ((cacheBuffer_.data[index] & 0XF0)) | tmp >> 4; + } + cacheBuffer_.data[index] = tmp; + return true; + } + if (fatType_ == 16) { + lba = fatStartBlock_ + (cluster >> 8); + } else if (fatType_ == 32) { + lba = fatStartBlock_ + (cluster >> 7); + } else { + goto fail; + } + if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) goto fail; + // store entry + if (fatType_ == 16) { + cacheBuffer_.fat16[cluster & 0XFF] = value; + } else { + cacheBuffer_.fat32[cluster & 0X7F] = value; + } + // mirror second FAT + if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_; + return true; + + fail: + return false; +} +//------------------------------------------------------------------------------ +// free a cluster chain +bool SdVolume::freeChain(uint32_t cluster) { + uint32_t next; + + // clear free cluster location + allocSearchStart_ = 2; + + do { + if (!fatGet(cluster, &next)) goto fail; + + // free cluster + if (!fatPut(cluster, 0)) goto fail; + + cluster = next; + } while (!isEOC(cluster)); + + return true; + + fail: + return false; +} +//------------------------------------------------------------------------------ +/** Volume free space in clusters. + * + * \return Count of free clusters for success or -1 if an error occurs. + */ +int32_t SdVolume::freeClusterCount() { + uint32_t free = 0; + uint16_t n; + uint32_t todo = clusterCount_ + 2; + + if (fatType_ == 16) { + n = 256; + } else if (fatType_ == 32) { + n = 128; + } else { + // put FAT12 here + return -1; + } + + for (uint32_t lba = fatStartBlock_; todo; todo -= n, lba++) { + if (!cacheRawBlock(lba, CACHE_FOR_READ)) return -1; + if (todo < n) n = todo; + if (fatType_ == 16) { + for (uint16_t i = 0; i < n; i++) { + if (cacheBuffer_.fat16[i] == 0) free++; + } + } else { + for (uint16_t i = 0; i < n; i++) { + if (cacheBuffer_.fat32[i] == 0) free++; + } + } + } + return free; +} +//------------------------------------------------------------------------------ +/** Initialize a FAT volume. + * + * \param[in] dev The SD card where the volume is located. + * + * \param[in] part The partition to be used. Legal values for \a part are + * 1-4 to use the corresponding partition on a device formatted with + * a MBR, Master Boot Record, or zero if the device is formatted as + * a super floppy with the FAT boot sector in block zero. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. Reasons for + * failure include not finding a valid partition, not finding a valid + * FAT file system in the specified partition or an I/O error. + */ +bool SdVolume::init(Sd2Card* dev, uint8_t part) { + uint32_t totalBlocks; + uint32_t volumeStartBlock = 0; + fat32_boot_t* fbs; + + sdCard_ = dev; + fatType_ = 0; + allocSearchStart_ = 2; + cacheDirty_ = 0; // cacheFlush() will write block if true + cacheMirrorBlock_ = 0; + cacheBlockNumber_ = 0XFFFFFFFF; + + // if part == 0 assume super floppy with FAT boot sector in block zero + // if part > 0 assume mbr volume with partition table + if (part) { + if (part > 4)goto fail; + if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) goto fail; + part_t* p = &cacheBuffer_.mbr.part[part-1]; + if ((p->boot & 0X7F) !=0 || + p->totalSectors < 100 || + p->firstSector == 0) { + // not a valid partition + goto fail; + } + volumeStartBlock = p->firstSector; + } + if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) goto fail; + fbs = &cacheBuffer_.fbs32; + if (fbs->bytesPerSector != 512 || + fbs->fatCount == 0 || + fbs->reservedSectorCount == 0 || + fbs->sectorsPerCluster == 0) { + // not valid FAT volume + goto fail; + } + fatCount_ = fbs->fatCount; + blocksPerCluster_ = fbs->sectorsPerCluster; + // determine shift that is same as multiply by blocksPerCluster_ + clusterSizeShift_ = 0; + while (blocksPerCluster_ != (1 << clusterSizeShift_)) { + // error if not power of 2 + if (clusterSizeShift_++ > 7) goto fail; + } + blocksPerFat_ = fbs->sectorsPerFat16 ? + fbs->sectorsPerFat16 : fbs->sectorsPerFat32; + + fatStartBlock_ = volumeStartBlock + fbs->reservedSectorCount; + + // count for FAT16 zero for FAT32 + rootDirEntryCount_ = fbs->rootDirEntryCount; + + // directory start for FAT16 dataStart for FAT32 + rootDirStart_ = fatStartBlock_ + fbs->fatCount * blocksPerFat_; + + // data start for FAT16 and FAT32 + dataStartBlock_ = rootDirStart_ + ((32 * fbs->rootDirEntryCount + 511)/512); + + // total blocks for FAT16 or FAT32 + totalBlocks = fbs->totalSectors16 ? + fbs->totalSectors16 : fbs->totalSectors32; + // total data blocks + clusterCount_ = totalBlocks - (dataStartBlock_ - volumeStartBlock); + + // divide by cluster size to get cluster count + clusterCount_ >>= clusterSizeShift_; + + // FAT type is determined by cluster count + if (clusterCount_ < 4085) { + fatType_ = 12; + if (!FAT12_SUPPORT) goto fail; + } else if (clusterCount_ < 65525) { + fatType_ = 16; + } else { + rootDirStart_ = fbs->fat32RootCluster; + fatType_ = 32; + } + return true; + + fail: + return false; +} +#endif \ No newline at end of file diff --git a/Firmware/SdVolume.h b/Firmware/SdVolume.h new file mode 100644 index 0000000..2ff2b6e --- /dev/null +++ b/Firmware/SdVolume.h @@ -0,0 +1,214 @@ +/* Arduino SdFat Library + * Copyright (C) 2009 by William Greiman + * + * This file is part of the Arduino SdFat Library + * + * This Library 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 Library 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 the Arduino SdFat Library. If not, see + * . + */ +#include "Marlin.h" +#ifdef SDSUPPORT +#ifndef SdVolume_h +#define SdVolume_h +/** + * \file + * \brief SdVolume class + */ +#include "SdFatConfig.h" +#include "Sd2Card.h" +#include "SdFatStructs.h" + +//============================================================================== +// SdVolume class +/** + * \brief Cache for an SD data block + */ +union cache_t { + /** Used to access cached file data blocks. */ + uint8_t data[512]; + /** Used to access cached FAT16 entries. */ + uint16_t fat16[256]; + /** Used to access cached FAT32 entries. */ + uint32_t fat32[128]; + /** Used to access cached directory entries. */ + dir_t dir[16]; + /** Used to access a cached Master Boot Record. */ + mbr_t mbr; + /** Used to access to a cached FAT boot sector. */ + fat_boot_t fbs; + /** Used to access to a cached FAT32 boot sector. */ + fat32_boot_t fbs32; + /** Used to access to a cached FAT32 FSINFO sector. */ + fat32_fsinfo_t fsinfo; +}; +//------------------------------------------------------------------------------ +/** + * \class SdVolume + * \brief Access FAT16 and FAT32 volumes on SD and SDHC cards. + */ +class SdVolume { + public: + /** Create an instance of SdVolume */ + SdVolume() : fatType_(0) {} + /** Clear the cache and returns a pointer to the cache. Used by the WaveRP + * recorder to do raw write to the SD card. Not for normal apps. + * \return A pointer to the cache buffer or zero if an error occurs. + */ + cache_t* cacheClear() { + if (!cacheFlush()) return 0; + cacheBlockNumber_ = 0XFFFFFFFF; + return &cacheBuffer_; + } + /** Initialize a FAT volume. Try partition one first then try super + * floppy format. + * + * \param[in] dev The Sd2Card where the volume is located. + * + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. Reasons for + * failure include not finding a valid partition, not finding a valid + * FAT file system or an I/O error. + */ + bool init(Sd2Card* dev) { return init(dev, 1) ? true : init(dev, 0);} + bool init(Sd2Card* dev, uint8_t part); + + // inline functions that return volume info + /** \return The volume's cluster size in blocks. */ + uint8_t blocksPerCluster() const {return blocksPerCluster_;} + /** \return The number of blocks in one FAT. */ + uint32_t blocksPerFat() const {return blocksPerFat_;} + /** \return The total number of clusters in the volume. */ + uint32_t clusterCount() const {return clusterCount_;} + /** \return The shift count required to multiply by blocksPerCluster. */ + uint8_t clusterSizeShift() const {return clusterSizeShift_;} + /** \return The logical block number for the start of file data. */ + uint32_t dataStartBlock() const {return dataStartBlock_;} + /** \return The number of FAT structures on the volume. */ + uint8_t fatCount() const {return fatCount_;} + /** \return The logical block number for the start of the first FAT. */ + uint32_t fatStartBlock() const {return fatStartBlock_;} + /** \return The FAT type of the volume. Values are 12, 16 or 32. */ + uint8_t fatType() const {return fatType_;} + int32_t freeClusterCount(); + /** \return The number of entries in the root directory for FAT16 volumes. */ + uint32_t rootDirEntryCount() const {return rootDirEntryCount_;} + /** \return The logical block number for the start of the root directory + on FAT16 volumes or the first cluster number on FAT32 volumes. */ + uint32_t rootDirStart() const {return rootDirStart_;} + /** Sd2Card object for this volume + * \return pointer to Sd2Card object. + */ + Sd2Card* sdCard() {return sdCard_;} + /** Debug access to FAT table + * + * \param[in] n cluster number. + * \param[out] v value of entry + * \return true for success or false for failure + */ + bool dbgFat(uint32_t n, uint32_t* v) {return fatGet(n, v);} +//------------------------------------------------------------------------------ + private: + // Allow SdBaseFile access to SdVolume private data. + friend class SdBaseFile; + + // value for dirty argument in cacheRawBlock to indicate read from cache + static bool const CACHE_FOR_READ = false; + // value for dirty argument in cacheRawBlock to indicate write to cache + static bool const CACHE_FOR_WRITE = true; + +#if USE_MULTIPLE_CARDS + cache_t cacheBuffer_; // 512 byte cache for device blocks + uint32_t cacheBlockNumber_; // Logical number of block in the cache + Sd2Card* sdCard_; // Sd2Card object for cache + bool cacheDirty_; // cacheFlush() will write block if true + uint32_t cacheMirrorBlock_; // block number for mirror FAT +#else // USE_MULTIPLE_CARDS + static cache_t cacheBuffer_; // 512 byte cache for device blocks + static uint32_t cacheBlockNumber_; // Logical number of block in the cache + static Sd2Card* sdCard_; // Sd2Card object for cache + static bool cacheDirty_; // cacheFlush() will write block if true + static uint32_t cacheMirrorBlock_; // block number for mirror FAT +#endif // USE_MULTIPLE_CARDS + uint32_t allocSearchStart_; // start cluster for alloc search + uint8_t blocksPerCluster_; // cluster size in blocks + uint32_t blocksPerFat_; // FAT size in blocks + uint32_t clusterCount_; // clusters in one FAT + uint8_t clusterSizeShift_; // shift to convert cluster count to block count + uint32_t dataStartBlock_; // first data block number + uint8_t fatCount_; // number of FATs on volume + uint32_t fatStartBlock_; // start block for first FAT + uint8_t fatType_; // volume type (12, 16, OR 32) + uint16_t rootDirEntryCount_; // number of entries in FAT16 root dir + uint32_t rootDirStart_; // root start block for FAT16, cluster for FAT32 + //---------------------------------------------------------------------------- + bool allocContiguous(uint32_t count, uint32_t* curCluster); + uint8_t blockOfCluster(uint32_t position) const { + return (position >> 9) & (blocksPerCluster_ - 1);} + uint32_t clusterStartBlock(uint32_t cluster) const { + return dataStartBlock_ + ((cluster - 2) << clusterSizeShift_);} + uint32_t blockNumber(uint32_t cluster, uint32_t position) const { + return clusterStartBlock(cluster) + blockOfCluster(position);} + cache_t *cache() {return &cacheBuffer_;} + uint32_t cacheBlockNumber() {return cacheBlockNumber_;} +#if USE_MULTIPLE_CARDS + bool cacheFlush(); + bool cacheRawBlock(uint32_t blockNumber, bool dirty); +#else // USE_MULTIPLE_CARDS + static bool cacheFlush(); + static bool cacheRawBlock(uint32_t blockNumber, bool dirty); +#endif // USE_MULTIPLE_CARDS + // used by SdBaseFile write to assign cache to SD location + void cacheSetBlockNumber(uint32_t blockNumber, bool dirty) { + cacheDirty_ = dirty; + cacheBlockNumber_ = blockNumber; + } + void cacheSetDirty() {cacheDirty_ |= CACHE_FOR_WRITE;} + bool chainSize(uint32_t beginCluster, uint32_t* size); + bool fatGet(uint32_t cluster, uint32_t* value); + bool fatPut(uint32_t cluster, uint32_t value); + bool fatPutEOC(uint32_t cluster) { + return fatPut(cluster, 0x0FFFFFFF); + } + bool freeChain(uint32_t cluster); + bool isEOC(uint32_t cluster) const { + if (FAT12_SUPPORT && fatType_ == 12) return cluster >= FAT12EOC_MIN; + if (fatType_ == 16) return cluster >= FAT16EOC_MIN; + return cluster >= FAT32EOC_MIN; + } + bool readBlock(uint32_t block, uint8_t* dst) { + return sdCard_->readBlock(block, dst);} + bool writeBlock(uint32_t block, const uint8_t* dst) { + return sdCard_->writeBlock(block, dst); + } +//------------------------------------------------------------------------------ + // Deprecated functions - suppress cpplint warnings with NOLINT comment +#if ALLOW_DEPRECATED_FUNCTIONS && !defined(DOXYGEN) + public: + /** \deprecated Use: bool SdVolume::init(Sd2Card* dev); + * \param[in] dev The SD card where the volume is located. + * \return true for success or false for failure. + */ + bool init(Sd2Card& dev) {return init(&dev);} // NOLINT + /** \deprecated Use: bool SdVolume::init(Sd2Card* dev, uint8_t vol); + * \param[in] dev The SD card where the volume is located. + * \param[in] part The partition to be used. + * \return true for success or false for failure. + */ + bool init(Sd2Card& dev, uint8_t part) { // NOLINT + return init(&dev, part); + } +#endif // ALLOW_DEPRECATED_FUNCTIONS +}; +#endif // SdVolume +#endif \ No newline at end of file diff --git a/Firmware/Servo.cpp b/Firmware/Servo.cpp new file mode 100644 index 0000000..5f8c7ef --- /dev/null +++ b/Firmware/Servo.cpp @@ -0,0 +1,344 @@ +/* + Servo.cpp - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2 + Copyright (c) 2009 Michael Margolis. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* + + A servo is activated by creating an instance of the Servo class passing the desired pin to the attach() method. + The servos are pulsed in the background using the value most recently written using the write() method + + Note that analogWrite of PWM on pins associated with the timer are disabled when the first servo is attached. + Timers are seized as needed in groups of 12 servos - 24 servos use two timers, 48 servos will use four. + + The methods are: + + Servo - Class for manipulating servo motors connected to Arduino pins. + + attach(pin ) - Attaches a servo motor to an i/o pin. + attach(pin, min, max ) - Attaches to a pin setting min and max values in microseconds + default min is 544, max is 2400 + + write() - Sets the servo angle in degrees. (invalid angle that is valid as pulse in microseconds is treated as microseconds) + writeMicroseconds() - Sets the servo pulse width in microseconds + read() - Gets the last written servo pulse width as an angle between 0 and 180. + readMicroseconds() - Gets the last written servo pulse width in microseconds. (was read_us() in first release) + attached() - Returns true if there is a servo attached. + detach() - Stops an attached servos from pulsing its i/o pin. + +*/ +#include "Configuration.h" + +#ifdef NUM_SERVOS +#include +#include + +#include "Servo.h" + +#define usToTicks(_us) (( clockCyclesPerMicrosecond()* _us) / 8) // converts microseconds to tick (assumes prescale of 8) // 12 Aug 2009 +#define ticksToUs(_ticks) (( (unsigned)_ticks * 8)/ clockCyclesPerMicrosecond() ) // converts from ticks back to microseconds + + +#define TRIM_DURATION 2 // compensation ticks to trim adjust for digitalWrite delays // 12 August 2009 + +//#define NBR_TIMERS (MAX_SERVOS / SERVOS_PER_TIMER) + +static servo_t servos[MAX_SERVOS]; // static array of servo structures +static volatile int8_t Channel[_Nbr_16timers ]; // counter for the servo being pulsed for each timer (or -1 if refresh interval) + +uint8_t ServoCount = 0; // the total number of attached servos + + +// convenience macros +#define SERVO_INDEX_TO_TIMER(_servo_nbr) ((timer16_Sequence_t)(_servo_nbr / SERVOS_PER_TIMER)) // returns the timer controlling this servo +#define SERVO_INDEX_TO_CHANNEL(_servo_nbr) (_servo_nbr % SERVOS_PER_TIMER) // returns the index of the servo on this timer +#define SERVO_INDEX(_timer,_channel) ((_timer*SERVOS_PER_TIMER) + _channel) // macro to access servo index by timer and channel +#define SERVO(_timer,_channel) (servos[SERVO_INDEX(_timer,_channel)]) // macro to access servo class by timer and channel + +#define SERVO_MIN() (MIN_PULSE_WIDTH - this->min * 4) // minimum value in uS for this servo +#define SERVO_MAX() (MAX_PULSE_WIDTH - this->max * 4) // maximum value in uS for this servo + +/************ static functions common to all instances ***********************/ + +static inline void handle_interrupts(timer16_Sequence_t timer, volatile uint16_t *TCNTn, volatile uint16_t* OCRnA) +{ + if( Channel[timer] < 0 ) + *TCNTn = 0; // channel set to -1 indicated that refresh interval completed so reset the timer + else{ + if( SERVO_INDEX(timer,Channel[timer]) < ServoCount && SERVO(timer,Channel[timer]).Pin.isActive == true ) + digitalWrite( SERVO(timer,Channel[timer]).Pin.nbr,LOW); // pulse this channel low if activated + } + + Channel[timer]++; // increment to the next channel + if( SERVO_INDEX(timer,Channel[timer]) < ServoCount && Channel[timer] < SERVOS_PER_TIMER) { + *OCRnA = *TCNTn + SERVO(timer,Channel[timer]).ticks; + if(SERVO(timer,Channel[timer]).Pin.isActive == true) // check if activated + digitalWrite( SERVO(timer,Channel[timer]).Pin.nbr,HIGH); // its an active channel so pulse it high + } + else { + // finished all channels so wait for the refresh period to expire before starting over + if( ((unsigned)*TCNTn) + 4 < usToTicks(REFRESH_INTERVAL) ) // allow a few ticks to ensure the next OCR1A not missed + *OCRnA = (unsigned int)usToTicks(REFRESH_INTERVAL); + else + *OCRnA = *TCNTn + 4; // at least REFRESH_INTERVAL has elapsed + Channel[timer] = -1; // this will get incremented at the end of the refresh period to start again at the first channel + } +} + +#ifndef WIRING // Wiring pre-defines signal handlers so don't define any if compiling for the Wiring platform +// Interrupt handlers for Arduino +#if defined(_useTimer1) +SIGNAL (TIMER1_COMPA_vect) +{ + handle_interrupts(_timer1, &TCNT1, &OCR1A); +} +#endif + +#if defined(_useTimer3) +SIGNAL (TIMER3_COMPA_vect) +{ + handle_interrupts(_timer3, &TCNT3, &OCR3A); +} +#endif + +#if defined(_useTimer4) +SIGNAL (TIMER4_COMPA_vect) +{ + handle_interrupts(_timer4, &TCNT4, &OCR4A); +} +#endif + +#if defined(_useTimer5) +SIGNAL (TIMER5_COMPA_vect) +{ + handle_interrupts(_timer5, &TCNT5, &OCR5A); +} +#endif + +#elif defined WIRING +// Interrupt handlers for Wiring +#if defined(_useTimer1) +void Timer1Service() +{ + handle_interrupts(_timer1, &TCNT1, &OCR1A); +} +#endif +#if defined(_useTimer3) +void Timer3Service() +{ + handle_interrupts(_timer3, &TCNT3, &OCR3A); +} +#endif +#endif + + +static void initISR(timer16_Sequence_t timer) +{ +#if defined (_useTimer1) + if(timer == _timer1) { + TCCR1A = 0; // normal counting mode + TCCR1B = _BV(CS11); // set prescaler of 8 + TCNT1 = 0; // clear the timer count +#if defined(__AVR_ATmega8__)|| defined(__AVR_ATmega128__) + TIFR |= _BV(OCF1A); // clear any pending interrupts; + TIMSK |= _BV(OCIE1A) ; // enable the output compare interrupt +#else + // here if not ATmega8 or ATmega128 + TIFR1 |= _BV(OCF1A); // clear any pending interrupts; + TIMSK1 |= _BV(OCIE1A) ; // enable the output compare interrupt +#endif +#if defined(WIRING) + timerAttach(TIMER1OUTCOMPAREA_INT, Timer1Service); +#endif + } +#endif + +#if defined (_useTimer3) + if(timer == _timer3) { + TCCR3A = 0; // normal counting mode + TCCR3B = _BV(CS31); // set prescaler of 8 + TCNT3 = 0; // clear the timer count +#if defined(__AVR_ATmega128__) + TIFR |= _BV(OCF3A); // clear any pending interrupts; + ETIMSK |= _BV(OCIE3A); // enable the output compare interrupt +#else + TIFR3 = _BV(OCF3A); // clear any pending interrupts; + TIMSK3 = _BV(OCIE3A) ; // enable the output compare interrupt +#endif +#if defined(WIRING) + timerAttach(TIMER3OUTCOMPAREA_INT, Timer3Service); // for Wiring platform only +#endif + } +#endif + +#if defined (_useTimer4) + if(timer == _timer4) { + TCCR4A = 0; // normal counting mode + TCCR4B = _BV(CS41); // set prescaler of 8 + TCNT4 = 0; // clear the timer count + TIFR4 = _BV(OCF4A); // clear any pending interrupts; + TIMSK4 = _BV(OCIE4A) ; // enable the output compare interrupt + } +#endif + +#if defined (_useTimer5) + if(timer == _timer5) { + TCCR5A = 0; // normal counting mode + TCCR5B = _BV(CS51); // set prescaler of 8 + TCNT5 = 0; // clear the timer count + TIFR5 = _BV(OCF5A); // clear any pending interrupts; + TIMSK5 = _BV(OCIE5A) ; // enable the output compare interrupt + } +#endif +} + +static void finISR(timer16_Sequence_t timer) +{ + //disable use of the given timer +#if defined WIRING // Wiring + if(timer == _timer1) { + #if defined(__AVR_ATmega1281__)||defined(__AVR_ATmega2561__) + TIMSK1 &= ~_BV(OCIE1A) ; // disable timer 1 output compare interrupt + #else + TIMSK &= ~_BV(OCIE1A) ; // disable timer 1 output compare interrupt + #endif + timerDetach(TIMER1OUTCOMPAREA_INT); + } + else if(timer == _timer3) { + #if defined(__AVR_ATmega1281__)||defined(__AVR_ATmega2561__) + TIMSK3 &= ~_BV(OCIE3A); // disable the timer3 output compare A interrupt + #else + ETIMSK &= ~_BV(OCIE3A); // disable the timer3 output compare A interrupt + #endif + timerDetach(TIMER3OUTCOMPAREA_INT); + } +#else + //For arduino - in future: call here to a currently undefined function to reset the timer +#endif +} + +static boolean isTimerActive(timer16_Sequence_t timer) +{ + // returns true if any servo is active on this timer + for(uint8_t channel=0; channel < SERVOS_PER_TIMER; channel++) { + if(SERVO(timer,channel).Pin.isActive == true) + return true; + } + return false; +} + + +/****************** end of static functions ******************************/ + +Servo::Servo() +{ + if( ServoCount < MAX_SERVOS) { + this->servoIndex = ServoCount++; // assign a servo index to this instance + servos[this->servoIndex].ticks = usToTicks(DEFAULT_PULSE_WIDTH); // store default values - 12 Aug 2009 + } + else + this->servoIndex = INVALID_SERVO ; // too many servos +} + +uint8_t Servo::attach(int pin) +{ + return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH); +} + +uint8_t Servo::attach(int pin, int min, int max) +{ + if(this->servoIndex < MAX_SERVOS ) { +#if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0) + if (pin > 0) this->pin = pin; else pin = this->pin; +#endif + pinMode( pin, OUTPUT) ; // set servo pin to output + servos[this->servoIndex].Pin.nbr = pin; + // todo min/max check: abs(min - MIN_PULSE_WIDTH) /4 < 128 + this->min = (MIN_PULSE_WIDTH - min)/4; //resolution of min/max is 4 uS + this->max = (MAX_PULSE_WIDTH - max)/4; + // initialize the timer if it has not already been initialized + timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex); + if(isTimerActive(timer) == false) + initISR(timer); + servos[this->servoIndex].Pin.isActive = true; // this must be set after the check for isTimerActive + } + return this->servoIndex ; +} + +void Servo::detach() +{ + servos[this->servoIndex].Pin.isActive = false; + timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex); + if(isTimerActive(timer) == false) { + finISR(timer); + } +} + +void Servo::write(int value) +{ + if(value < MIN_PULSE_WIDTH) + { // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds) + if(value < 0) value = 0; + if(value > 180) value = 180; + value = map(value, 0, 180, SERVO_MIN(), SERVO_MAX()); + } + this->writeMicroseconds(value); +} + +void Servo::writeMicroseconds(int value) +{ + // calculate and store the values for the given channel + byte channel = this->servoIndex; + if( (channel < MAX_SERVOS) ) // ensure channel is valid + { + if( value < SERVO_MIN() ) // ensure pulse width is valid + value = SERVO_MIN(); + else if( value > SERVO_MAX() ) + value = SERVO_MAX(); + + value = value - TRIM_DURATION; + value = usToTicks(value); // convert to ticks after compensating for interrupt overhead - 12 Aug 2009 + + uint8_t oldSREG = SREG; + cli(); + servos[channel].ticks = value; + SREG = oldSREG; + } +} + +int Servo::read() // return the value as degrees +{ + return map( this->readMicroseconds()+1, SERVO_MIN(), SERVO_MAX(), 0, 180); +} + +int Servo::readMicroseconds() +{ + unsigned int pulsewidth; + if( this->servoIndex != INVALID_SERVO ) + pulsewidth = ticksToUs(servos[this->servoIndex].ticks) + TRIM_DURATION ; // 12 aug 2009 + else + pulsewidth = 0; + + return pulsewidth; +} + +bool Servo::attached() +{ + return servos[this->servoIndex].Pin.isActive ; +} + +#endif diff --git a/Firmware/Servo.h b/Firmware/Servo.h new file mode 100644 index 0000000..204497a --- /dev/null +++ b/Firmware/Servo.h @@ -0,0 +1,135 @@ +/* + Servo.h - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2 + Copyright (c) 2009 Michael Margolis. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +/* + + A servo is activated by creating an instance of the Servo class passing the desired pin to the attach() method. + The servos are pulsed in the background using the value most recently written using the write() method + + Note that analogWrite of PWM on pins associated with the timer are disabled when the first servo is attached. + Timers are seized as needed in groups of 12 servos - 24 servos use two timers, 48 servos will use four. + The sequence used to seize timers is defined in timers.h + + The methods are: + + Servo - Class for manipulating servo motors connected to Arduino pins. + + attach(pin ) - Attaches a servo motor to an i/o pin. + attach(pin, min, max ) - Attaches to a pin setting min and max values in microseconds + default min is 544, max is 2400 + + write() - Sets the servo angle in degrees. (invalid angle that is valid as pulse in microseconds is treated as microseconds) + writeMicroseconds() - Sets the servo pulse width in microseconds + read() - Gets the last written servo pulse width as an angle between 0 and 180. + readMicroseconds() - Gets the last written servo pulse width in microseconds. (was read_us() in first release) + attached() - Returns true if there is a servo attached. + detach() - Stops an attached servos from pulsing its i/o pin. + */ + +#ifndef Servo_h +#define Servo_h + +#include + +/* + * Defines for 16 bit timers used with Servo library + * + * If _useTimerX is defined then TimerX is a 16 bit timer on the current board + * timer16_Sequence_t enumerates the sequence that the timers should be allocated + * _Nbr_16timers indicates how many 16 bit timers are available. + * + */ + +// Say which 16 bit timers can be used and in what order +#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) +#define _useTimer5 +//#define _useTimer1 +#define _useTimer3 +#define _useTimer4 +//typedef enum { _timer5, _timer1, _timer3, _timer4, _Nbr_16timers } timer16_Sequence_t ; +typedef enum { _timer5, _timer3, _timer4, _Nbr_16timers } timer16_Sequence_t ; + +#elif defined(__AVR_ATmega32U4__) +//#define _useTimer1 +#define _useTimer3 +//typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t ; +typedef enum { _timer3, _Nbr_16timers } timer16_Sequence_t ; + +#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) +#define _useTimer3 +//#define _useTimer1 +//typedef enum { _timer3, _timer1, _Nbr_16timers } timer16_Sequence_t ; +typedef enum { _timer3, _Nbr_16timers } timer16_Sequence_t ; + +#elif defined(__AVR_ATmega128__) ||defined(__AVR_ATmega1281__) || defined(__AVR_ATmega1284P__) ||defined(__AVR_ATmega2561__) +#define _useTimer3 +//#define _useTimer1 +//typedef enum { _timer3, _timer1, _Nbr_16timers } timer16_Sequence_t ; +typedef enum { _timer3, _Nbr_16timers } timer16_Sequence_t ; + +#else // everything else +//#define _useTimer1 +//typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t ; +typedef enum { _Nbr_16timers } timer16_Sequence_t ; +#endif + +#define Servo_VERSION 2 // software version of this library + +#define MIN_PULSE_WIDTH 544 // the shortest pulse sent to a servo +#define MAX_PULSE_WIDTH 2400 // the longest pulse sent to a servo +#define DEFAULT_PULSE_WIDTH 1500 // default pulse width when servo is attached +#define REFRESH_INTERVAL 20000 // minimum time to refresh servos in microseconds + +#define SERVOS_PER_TIMER 12 // the maximum number of servos controlled by one timer +#define MAX_SERVOS (_Nbr_16timers * SERVOS_PER_TIMER) + +#define INVALID_SERVO 255 // flag indicating an invalid servo index + +typedef struct { + uint8_t nbr :6 ; // a pin number from 0 to 63 + uint8_t isActive :1 ; // true if this channel is enabled, pin not pulsed if false +} ServoPin_t ; + +typedef struct { + ServoPin_t Pin; + unsigned int ticks; +} servo_t; + +class Servo +{ +public: + Servo(); + uint8_t attach(int pin); // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure + uint8_t attach(int pin, int min, int max); // as above but also sets min and max values for writes. + void detach(); + void write(int value); // if value is < 200 it is treated as an angle, otherwise as pulse width in microseconds + void writeMicroseconds(int value); // Write pulse width in microseconds + int read(); // returns current pulse width as an angle between 0 and 180 degrees + int readMicroseconds(); // returns current pulse width in microseconds for this servo (was read_us() in first release) + bool attached(); // return true if this servo is attached, otherwise false +#if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0) + int pin; // store the hardware pin of the servo +#endif +private: + uint8_t servoIndex; // index into the channel data for this servo + int8_t min; // minimum is this value times 4 added to MIN_PULSE_WIDTH + int8_t max; // maximum is this value times 4 added to MAX_PULSE_WIDTH +}; + +#endif diff --git a/Firmware/Timer.cpp b/Firmware/Timer.cpp new file mode 100644 index 0000000..a94a858 --- /dev/null +++ b/Firmware/Timer.cpp @@ -0,0 +1,68 @@ +/** + * @file + * @author Marek Bel + */ + +#include "Timer.h" +#include "system_timer.h" + +/** + * @brief construct Timer + * + * It is guaranteed, that construction is equivalent with zeroing all members. + * This property can be exploited in menu_data. + */ +template +Timer::Timer() : m_isRunning(false), m_started() +{ +} + +/** + * @brief Start timer + */ +template +void Timer::start() +{ + m_started = _millis(); + m_isRunning = true; +} + +/** + * @brief Timer has expired + * + * Timer is considered expired after msPeriod has passed from time the timer was started. + * Timer is stopped after expiration. + * This function must be called at least each (T maximum value - msPeriod) milliseconds to be sure to + * catch first expiration. + * This function is expected to handle wrap around of time register well. + * + * @param msPeriod Time interval in milliseconds. Do not omit "ul" when using constant literal with LongTimer. + * @retval true Timer has expired + * @retval false Timer not expired yet, or is not running, or time window in which is timer considered expired passed. + */ +template +bool Timer::expired(T msPeriod) +{ + if (!m_isRunning) return false; + bool expired = false; + const T now = _millis(); + if (m_started <= m_started + msPeriod) + { + if ((now >= m_started + msPeriod) || (now < m_started)) + { + expired = true; + } + } + else + { + if ((now >= m_started + msPeriod) && (now < m_started)) + { + expired = true; + } + } + if (expired) m_isRunning = false; + return expired; +} + +template class Timer; +template class Timer; diff --git a/Firmware/Timer.h b/Firmware/Timer.h new file mode 100644 index 0000000..e2e84ef --- /dev/null +++ b/Firmware/Timer.h @@ -0,0 +1,53 @@ +/** + * @file + * @author Marek Bel + */ + +#ifndef TIMER_H +#define TIMER_H + +/** + * @brief simple timer + * + * Simple and memory saving implementation. Should handle timer register wrap around well. + * Resolution is one millisecond. To save memory, doesn't store timer period. + * If you wish timer which is storing period, derive from this. + */ +template +class Timer +{ +public: + Timer(); + void start(); + void stop(){m_isRunning = false;} + bool running(){return m_isRunning;} + bool expired(T msPeriod); +protected: + T started(){return m_started;} +private: + bool m_isRunning; + T m_started; +}; + +/** + * @brief Timer unsigned long specialization + * + * Maximum period is at least 49 days. + */ +#if __cplusplus>=201103L +using LongTimer = Timer; +#else +typedef Timer LongTimer; +#endif +/** + * @brief Timer unsigned short specialization + * + * Maximum period is at least 65 seconds. + */ +#if __cplusplus>=201103L +using ShortTimer = Timer; +#else +typedef Timer ShortTimer; +#endif + +#endif /* TIMER_H */ diff --git a/Firmware/TimerRemaining.h b/Firmware/TimerRemaining.h new file mode 100644 index 0000000..1a9138c --- /dev/null +++ b/Firmware/TimerRemaining.h @@ -0,0 +1,56 @@ +/** + * @file + * @author Marek Bel + */ + +#ifndef TIMERREMAINING_H +#define TIMERREMAINING_H + +#include "Timer.h" +#include "Arduino.h" +#include "system_timer.h" +#include + +class TimerRemaining : public LongTimer +{ +public: + TimerRemaining() : m_period(){} + void start() = delete; + bool expired(unsigned long msPeriod) = delete; + /** + * @brief Start timer + * @param msPeriod Time to expire in milliseconds + */ + void start(unsigned long msPeriod) + { + m_period = msPeriod; + LongTimer::start(); + } + /** + * @brief Time remaining to expiration + * + * @param msPeriod timer period in milliseconds + * @return time remaining to expiration in milliseconds + * @retval 0 Timer has expired, or was not even started. + */ + unsigned long remaining() + { + if (!running()) return 0; + if (expired()) return 0; + const unsigned long now = _millis(); + return (started() + m_period - now); + } + /** + * @brief Timer has expired. + * @retval true Timer has expired. + * @retval false Timer has not expired. + */ + bool expired() + { + return LongTimer::expired(m_period); + } +private: + unsigned long m_period; //!< Timer period in milliseconds. +}; + +#endif // ifndef TIMERREMAINING_H diff --git a/Firmware/adc.c b/Firmware/adc.c new file mode 100644 index 0000000..26728ce --- /dev/null +++ b/Firmware/adc.c @@ -0,0 +1,94 @@ +//adc.c + +#include "adc.h" +#include +#include +#include + +uint8_t adc_state; +uint8_t adc_count; +uint16_t adc_values[ADC_CHAN_CNT]; +uint16_t adc_sim_mask; + + +#ifdef ADC_CALLBACK + extern void ADC_CALLBACK(void); +#endif //ADC_CALLBACK + + +void adc_init(void) +{ + printf_P(PSTR("adc_init\n")); + adc_sim_mask = 0x00; + ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); + ADMUX |= (1 << REFS0); + ADCSRA |= (1 << ADEN); +// ADCSRA |= (1 << ADIF) | (1 << ADSC); + DIDR0 = (ADC_CHAN_MSK & 0xff); + DIDR2 = (ADC_CHAN_MSK >> 8); + adc_reset(); +// adc_sim_mask = 0b0101; +// adc_sim_mask = 0b100101; +// adc_values[0] = 1023 * 16; +// adc_values[2] = 1023 * 16; +// adc_values[5] = 1002 * 16; +} + +void adc_reset(void) +{ + adc_state = 0; + adc_count = 0; + uint8_t i; for (i = 0; i < ADC_CHAN_CNT; i++) + if ((adc_sim_mask & (1 << i)) == 0) + adc_values[i] = 0; +} + +void adc_setmux(uint8_t ch) +{ + ch &= 0x0f; + if (ch & 0x08) ADCSRB |= (1 << MUX5); + else ADCSRB &= ~(1 << MUX5); + ADMUX = (ADMUX & ~(0x07)) | (ch & 0x07); +} + +uint8_t adc_chan(uint8_t index) +{ + uint8_t chan = 0; + uint16_t mask = 1; + while (mask) + { + if ((mask & ADC_CHAN_MSK) && (index-- == 0)) break; + mask <<= 1; + chan++; + } + return chan; +} + +void adc_cycle(void) +{ + if (adc_state & 0x80) + { + uint8_t index = adc_state & 0x0f; + if ((adc_sim_mask & (1 << index)) == 0) + adc_values[index] += ADC; + if (++index >= ADC_CHAN_CNT) + { + index = 0; + adc_count++; + if (adc_count >= ADC_OVRSAMPL) + { +#ifdef ADC_CALLBACK + ADC_CALLBACK(); +#endif //ADC_CALLBACK + adc_reset(); + } + } + adc_setmux(adc_chan(index)); + adc_state = index; + } + else + { + ADCSRA |= (1 << ADSC); //start conversion + adc_state |= 0x80; + } +} diff --git a/Firmware/adc.h b/Firmware/adc.h new file mode 100644 index 0000000..9ff137d --- /dev/null +++ b/Firmware/adc.h @@ -0,0 +1,45 @@ +//adc.h +#ifndef _ADC_H +#define _ADC_H + +#include +#include "config.h" + + +#if defined(__cplusplus) +extern "C" { +#endif //defined(__cplusplus) + +/* +http://resnet.uoregon.edu/~gurney_j/jmpc/bitwise.html +*/ +#define BITCOUNT(x) (((BX_(x)+(BX_(x)>>4)) & 0x0F0F0F0F) % 255) +#define BX_(x) ((x) - (((x)>>1)&0x77777777) - (((x)>>2)&0x33333333) - (((x)>>3)&0x11111111)) + +#define ADC_PIN_IDX(pin) BITCOUNT(ADC_CHAN_MSK & ((1 << (pin)) - 1)) + +#if BITCOUNT(ADC_CHAN_MSK) != ADC_CHAN_CNT +# error "ADC_CHAN_MSK oes not match ADC_CHAN_CNT" +#endif + +extern uint8_t adc_state; +extern uint8_t adc_count; +extern uint16_t adc_values[ADC_CHAN_CNT]; +extern uint16_t adc_sim_mask; + + +extern void adc_init(void); + +extern void adc_reset(void); + +extern void adc_setmux(uint8_t ch); + +extern uint8_t adc_chan(uint8_t index); + +extern void adc_cycle(void); + + +#if defined(__cplusplus) +} +#endif //defined(__cplusplus) +#endif //_ADC_H diff --git a/Firmware/boards.h b/Firmware/boards.h new file mode 100644 index 0000000..3e02653 --- /dev/null +++ b/Firmware/boards.h @@ -0,0 +1,14 @@ +#ifndef BOARDS_H +#define BOARDS_H + +#define BOARD_UNKNOWN -1 + +#define BOARD_RAMBO_MINI_1_0 200 // Rambo-mini 1.0 - 200 (orig 102) +#define BOARD_RAMBO_MINI_1_3 203 // Rambo-mini 1.3 - 203 (orig 302) + +#define BOARD_EINSY_1_0a 310 // EINSy 1.0a - 310 (new) + +#define MB(board) (MOTHERBOARD==BOARD_##board) +#define IS_RAMPS (MB(RAMPS_OLD) || MB(RAMPS_13_EFB) || MB(RAMPS_13_EEB) || MB(RAMPS_13_EFF) || MB(RAMPS_13_EEF)) + +#endif //__BOARDS_H diff --git a/Firmware/bootapp.c b/Firmware/bootapp.c new file mode 100644 index 0000000..4fd67db --- /dev/null +++ b/Firmware/bootapp.c @@ -0,0 +1,55 @@ +//bootapp.c +#include "bootapp.h" +#include +#include +#include + + +#include +extern FILE _uartout; +#define uartout (&_uartout) + +void bootapp_print_vars(void) +{ + fprintf_P(uartout, PSTR("boot_src_addr =0x%08lx\n"), boot_src_addr); + fprintf_P(uartout, PSTR("boot_dst_addr =0x%08lx\n"), boot_dst_addr); + fprintf_P(uartout, PSTR("boot_copy_size =0x%04x\n"), boot_copy_size); + fprintf_P(uartout, PSTR("boot_reserved =0x%02x\n"), boot_reserved); + fprintf_P(uartout, PSTR("boot_app_flags =0x%02x\n"), boot_app_flags); + fprintf_P(uartout, PSTR("boot_app_magic =0x%08lx\n"), boot_app_magic); +} + + +void bootapp_ram2flash(uint16_t rptr, uint16_t fptr, uint16_t size) +{ + cli(); + boot_app_magic = BOOT_APP_MAGIC; + boot_app_flags |= BOOT_APP_FLG_COPY; + boot_app_flags |= BOOT_APP_FLG_ERASE; +/* uint16_t ui; for (ui = 0; ui < size; ui++) + { + uint8_t uc = ram_array[ui+rptr]; + if (pgm_read_byte(ui+fptr) & uc != uc) + { + boot_app_flags |= BOOT_APP_FLG_ERASE; + break; + } + }*/ + boot_copy_size = (uint16_t)size; + boot_src_addr = (uint32_t)rptr; + boot_dst_addr = (uint32_t)fptr; + bootapp_print_vars(); + wdt_enable(WDTO_15MS); + while(1); +} + +void bootapp_reboot_user0(uint8_t reserved) +{ + cli(); + boot_app_magic = BOOT_APP_MAGIC; + boot_app_flags = BOOT_APP_FLG_USER0; + boot_reserved = reserved; + bootapp_print_vars(); + wdt_enable(WDTO_15MS); + while(1); +} diff --git a/Firmware/bootapp.h b/Firmware/bootapp.h new file mode 100644 index 0000000..3fb0306 --- /dev/null +++ b/Firmware/bootapp.h @@ -0,0 +1,41 @@ +//language.h +#ifndef BOOTAPP_H +#define BOOTAPP_H + +#include "config.h" +#include + + +#define RAMSIZE 0x2000 +#define ram_array ((uint8_t*)(0)) +#define boot_src_addr (*((uint32_t*)(RAMSIZE - 16))) +#define boot_dst_addr (*((uint32_t*)(RAMSIZE - 12))) +#define boot_copy_size (*((uint16_t*)(RAMSIZE - 8))) +#define boot_reserved (*((uint8_t*)(RAMSIZE - 6))) +#define boot_app_flags (*((uint8_t*)(RAMSIZE - 5))) +#define boot_app_magic (*((uint32_t*)(RAMSIZE - 4))) +#define BOOT_APP_FLG_ERASE 0x01 +#define BOOT_APP_FLG_COPY 0x02 +#define BOOT_APP_FLG_FLASH 0x04 +#define BOOT_APP_FLG_RUN 0x08 + +#define BOOT_APP_FLG_USER0 0x80 + +#define BOOT_APP_MAGIC 0x55aa55aa + + +#if defined(__cplusplus) +extern "C" { +#endif //defined(__cplusplus) + +extern void bootapp_print_vars(void); + +extern void bootapp_ram2flash(uint16_t rptr, uint16_t fptr, uint16_t size); + +extern void bootapp_reboot_user0(uint8_t reserved); + +#if defined(__cplusplus) +} +#endif //defined(__cplusplus) + +#endif //BOOTAPP_H diff --git a/Firmware/cardreader.cpp b/Firmware/cardreader.cpp new file mode 100644 index 0000000..ab4d377 --- /dev/null +++ b/Firmware/cardreader.cpp @@ -0,0 +1,1023 @@ +#include "Marlin.h" +#include "cardreader.h" +#include "ultralcd.h" +#include "stepper.h" +#include "temperature.h" +#include "language.h" + +#ifdef SDSUPPORT + +#define LONGEST_FILENAME (longFilename[0] ? longFilename : filename) + +CardReader::CardReader() +{ + + #ifdef SDCARD_SORT_ALPHA + sort_count = 0; + #if SDSORT_GCODE + sort_alpha = true; + sort_folders = FOLDER_SORTING; + //sort_reverse = false; + #endif + #endif + + filesize = 0; + sdpos = 0; + sdprinting = false; + cardOK = false; + paused = false; + saving = false; + logging = false; + autostart_atmillis=0; + workDirDepth = 0; + file_subcall_ctr=0; + memset(workDirParents, 0, sizeof(workDirParents)); + + autostart_stilltocheck=true; //the SD start is delayed, because otherwise the serial cannot answer fast enough to make contact with the host software. + lastnr=0; + //power to SD reader + #if SDPOWER > -1 + SET_OUTPUT(SDPOWER); + WRITE(SDPOWER,HIGH); + #endif //SDPOWER + + autostart_atmillis=_millis()+5000; +} + +char *createFilename(char *buffer,const dir_t &p) //buffer>12characters +{ + char *pos=buffer; + for (uint8_t i = 0; i < 11; i++) + { + if (p.name[i] == ' ')continue; + if (i == 8) + { + *pos++='.'; + } + *pos++=p.name[i]; + } + *pos++=0; + return buffer; +} + +/** ++* Dive into a folder and recurse depth-first to perform a pre-set operation lsAction: ++* LS_Count - Add +1 to nrFiles for every file within the parent ++* LS_GetFilename - Get the filename of the file indexed by nrFiles ++* LS_SerialPrint - Print the full path and size of each file to serial output ++*/ + +void CardReader::lsDive(const char *prepend, SdFile parent, const char * const match/*=NULL*/) { + dir_t p; + uint8_t cnt = 0; + // Read the next entry from a directory + while (parent.readDir(p, longFilename) > 0) { + // If the entry is a directory and the action is LS_SerialPrint + if (DIR_IS_SUBDIR(&p) && lsAction != LS_Count && lsAction != LS_GetFilename) { + // Get the short name for the item, which we know is a folder + char lfilename[FILENAME_LENGTH]; + createFilename(lfilename, p); + // Allocate enough stack space for the full path to a folder, trailing slash, and nul + bool prepend_is_empty = (prepend[0] == '\0'); + int len = (prepend_is_empty ? 1 : strlen(prepend)) + strlen(lfilename) + 1 + 1; + char path[len]; + // Append the FOLDERNAME12/ to the passed string. + // It contains the full path to the "parent" argument. + // We now have the full path to the item in this folder. + strcpy(path, prepend_is_empty ? "/" : prepend); // root slash if prepend is empty + strcat(path, lfilename); // FILENAME_LENGTH-1 characters maximum + strcat(path, "/"); // 1 character + // Serial.print(path); + // Get a new directory object using the full path + // and dive recursively into it. + SdFile dir; + if (!dir.open(parent, lfilename, O_READ)) { + if (lsAction == LS_SerialPrint) { + //SERIAL_ECHO_START(); + //SERIAL_ECHOPGM(_i("Cannot open subdir"));////MSG_SD_CANT_OPEN_SUBDIR + //SERIAL_ECHOLN(lfilename); + } + } + lsDive(path, dir); + // close() is done automatically by destructor of SdFile + } + else { + uint8_t pn0 = p.name[0]; + if (pn0 == DIR_NAME_FREE) break; + if (pn0 == DIR_NAME_DELETED || pn0 == '.') continue; + if (longFilename[0] == '.') continue; + if (!DIR_IS_FILE_OR_SUBDIR(&p) || (p.attributes & DIR_ATT_HIDDEN)) continue; + filenameIsDir = DIR_IS_SUBDIR(&p); + if (!filenameIsDir && (p.name[8] != 'G' || p.name[9] == '~')) continue; + switch (lsAction) { + case LS_Count: + nrFiles++; + break; + + case LS_SerialPrint: + createFilename(filename, p); + SERIAL_PROTOCOL(prepend); + SERIAL_PROTOCOL(filename); + MYSERIAL.write(' '); + SERIAL_PROTOCOLLN(p.fileSize); + break; + + case LS_GetFilename: + //SERIAL_ECHOPGM("File: "); + createFilename(filename, p); + cluster = parent.curCluster(); + position = parent.curPosition(); + /*MYSERIAL.println(filename); + SERIAL_ECHOPGM("Write date: "); + writeDate = p.lastWriteDate; + MYSERIAL.println(writeDate); + writeTime = p.lastWriteTime; + SERIAL_ECHOPGM("Creation date: "); + MYSERIAL.println(p.creationDate); + SERIAL_ECHOPGM("Access date: "); + MYSERIAL.println(p.lastAccessDate); + SERIAL_ECHOLNPGM("");*/ + creationDate = p.creationDate; + creationTime = p.creationTime; + //writeDate = p.lastAccessDate; + if (match != NULL) { + if (strcasecmp(match, filename) == 0) return; + } + else if (cnt == nrFiles) return; + cnt++; + break; + } + } + } // while readDir +} + +void CardReader::ls() +{ + lsAction=LS_SerialPrint; + //if(lsAction==LS_Count) + //nrFiles=0; + + root.rewind(); + lsDive("",root); +} + + +void CardReader::initsd() +{ + cardOK = false; + if(root.isOpen()) + root.close(); +#ifdef SDSLOW + if (!card.init(SPI_HALF_SPEED,SDSS) + #if defined(LCD_SDSS) && (LCD_SDSS != SDSS) + && !card.init(SPI_HALF_SPEED,LCD_SDSS) + #endif + ) +#else + if (!card.init(SPI_FULL_SPEED,SDSS) + #if defined(LCD_SDSS) && (LCD_SDSS != SDSS) + && !card.init(SPI_FULL_SPEED,LCD_SDSS) + #endif + ) +#endif + { + //if (!card.init(SPI_HALF_SPEED,SDSS)) + SERIAL_ECHO_START; + SERIAL_ECHOLNRPGM(_n("SD init fail"));////MSG_SD_INIT_FAIL + } + else if (!volume.init(&card)) + { + SERIAL_ERROR_START; + SERIAL_ERRORLNRPGM(_n("volume.init failed"));////MSG_SD_VOL_INIT_FAIL + } + else if (!root.openRoot(&volume)) + { + SERIAL_ERROR_START; + SERIAL_ERRORLNRPGM(_n("openRoot failed"));////MSG_SD_OPENROOT_FAIL + } + else + { + cardOK = true; + SERIAL_ECHO_START; + SERIAL_ECHOLNRPGM(_n("SD card ok"));////MSG_SD_CARD_OK + } + workDir=root; + curDir=&root; + workDirDepth = 0; + + #ifdef SDCARD_SORT_ALPHA + presort(); + #endif + + /* + if(!workDir.openRoot(&volume)) + { + SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL); + } + */ + +} + +void CardReader::setroot() +{ + /*if(!workDir.openRoot(&volume)) + { + SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL); + }*/ + workDir=root; + + curDir=&workDir; + #ifdef SDCARD_SORT_ALPHA + presort(); + #endif +} +void CardReader::release() +{ + sdprinting = false; + cardOK = false; +} + +void CardReader::startFileprint() +{ + if(cardOK) + { + sdprinting = true; + paused = false; + Stopped = false; + #ifdef SDCARD_SORT_ALPHA + //flush_presort(); + #endif + } +} + +void CardReader::pauseSDPrint() +{ + if(sdprinting) + { + sdprinting = false; + paused = true; + } +} + + +void CardReader::openLogFile(const char* name) +{ + logging = true; + openFile(name, false); +} + +void CardReader::getDirName(char* name, uint8_t level) +{ + workDirParents[level].getFilename(name); +} + +uint16_t CardReader::getWorkDirDepth() { + return workDirDepth; +} + +void CardReader::getAbsFilename(char *t) +{ + uint8_t cnt=0; + *t='/';t++;cnt++; + for(uint8_t i=0;i dirname_start) + { + const size_t maxLen = 12; + char subdirname[maxLen+1]; + subdirname[maxLen] = 0; + const size_t len = ((static_cast(dirname_end-dirname_start))>maxLen) ? maxLen : (dirname_end-dirname_start); + strncpy(subdirname, dirname_start, len); + SERIAL_ECHOLN(subdirname); + if (!dir.open(curDir, subdirname, O_READ)) + { + SERIAL_PROTOCOLRPGM(MSG_SD_OPEN_FILE_FAIL); + SERIAL_PROTOCOL(subdirname); + SERIAL_PROTOCOLLNPGM("."); + return; + } + else + { + //SERIAL_ECHOLN("dive ok"); + } + + curDir = &dir; + dirname_start = dirname_end + 1; + } + else // the reminder after all /fsa/fdsa/ is the filename + { + fileName = dirname_start; + //SERIAL_ECHOLN("remaider"); + //SERIAL_ECHOLN(fname); + break; + } + + } + } + else //relative path + { + curDir = &workDir; + } +} + +void CardReader::openFile(const char* name,bool read, bool replace_current/*=true*/) +{ + if(!cardOK) + return; + if(file.isOpen()) //replacing current file by new file, or subfile call + { + if(!replace_current) + { + if((int)file_subcall_ctr>(int)SD_PROCEDURE_DEPTH-1) + { + SERIAL_ERROR_START; + SERIAL_ERRORPGM("trying to call sub-gcode files with too many levels. MAX level is:"); + SERIAL_ERRORLN(SD_PROCEDURE_DEPTH); + kill("", 1); + return; + } + + SERIAL_ECHO_START; + SERIAL_ECHOPGM("SUBROUTINE CALL target:\""); + SERIAL_ECHO(name); + SERIAL_ECHOPGM("\" parent:\""); + + //store current filename and position + getAbsFilename(filenames[file_subcall_ctr]); + + SERIAL_ECHO(filenames[file_subcall_ctr]); + SERIAL_ECHOPGM("\" pos"); + SERIAL_ECHOLN(sdpos); + filespos[file_subcall_ctr]=sdpos; + file_subcall_ctr++; + } + else + { + SERIAL_ECHO_START; + SERIAL_ECHOPGM("Now doing file: "); + SERIAL_ECHOLN(name); + } + file.close(); + } + else //opening fresh file + { + file_subcall_ctr=0; //resetting procedure depth in case user cancels print while in procedure + SERIAL_ECHO_START; + SERIAL_ECHOPGM("Now fresh file: "); + SERIAL_ECHOLN(name); + } + sdprinting = false; + paused = false; + + + SdFile myDir; + const char *fname=name; + diveSubfolder(fname,myDir); + + if(read) + { + if (file.open(curDir, fname, O_READ)) + { + filesize = file.fileSize(); + SERIAL_PROTOCOLRPGM(_N("File opened: "));////MSG_SD_FILE_OPENED + SERIAL_PROTOCOL(fname); + SERIAL_PROTOCOLRPGM(_n(" Size: "));////MSG_SD_SIZE + SERIAL_PROTOCOLLN(filesize); + sdpos = 0; + + SERIAL_PROTOCOLLNRPGM(_N("File selected"));////MSG_SD_FILE_SELECTED + getfilename(0, fname); + lcd_setstatus(longFilename[0] ? longFilename : fname); + lcd_setstatus("SD-PRINTING "); + } + else + { + SERIAL_PROTOCOLRPGM(MSG_SD_OPEN_FILE_FAIL); + SERIAL_PROTOCOL(fname); + SERIAL_PROTOCOLLNPGM("."); + } + } + else + { //write + if (!file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) + { + SERIAL_PROTOCOLRPGM(MSG_SD_OPEN_FILE_FAIL); + SERIAL_PROTOCOL(fname); + SERIAL_PROTOCOLLNPGM("."); + } + else + { + saving = true; + SERIAL_PROTOCOLRPGM(_N("Writing to file: "));////MSG_SD_WRITE_TO_FILE + SERIAL_PROTOCOLLN(name); + lcd_setstatus(fname); + } + } + +} + +void CardReader::removeFile(const char* name) +{ + if(!cardOK) return; + file.close(); + sdprinting = false; + + SdFile myDir; + const char *fname=name; + diveSubfolder(fname,myDir); + + if (file.remove(curDir, fname)) + { + SERIAL_PROTOCOLPGM("File deleted:"); + SERIAL_PROTOCOLLN(fname); + sdpos = 0; + #ifdef SDCARD_SORT_ALPHA + presort(); + #endif + } + else + { + SERIAL_PROTOCOLPGM("Deletion failed, File: "); + SERIAL_PROTOCOL(fname); + SERIAL_PROTOCOLLNPGM("."); + } + +} + +uint32_t CardReader::getFileSize() +{ + return filesize; +} + +void CardReader::getStatus() +{ + if(sdprinting){ + SERIAL_PROTOCOL(longFilename); + SERIAL_PROTOCOLPGM("\n"); + SERIAL_PROTOCOLRPGM(_N("SD printing byte "));////MSG_SD_PRINTING_BYTE + SERIAL_PROTOCOL(sdpos); + SERIAL_PROTOCOLPGM("/"); + SERIAL_PROTOCOLLN(filesize); + uint16_t time = _millis()/60000 - starttime/60000; + SERIAL_PROTOCOL(itostr2(time/60)); + SERIAL_PROTOCOL(':'); + SERIAL_PROTOCOL(itostr2(time%60)); + SERIAL_PROTOCOLPGM("\n"); + } + else if (paused) { + SERIAL_PROTOCOLLNPGM("SD print paused"); + } + else if (saved_printing) { + SERIAL_PROTOCOLLNPGM("Print saved"); + } + else { + SERIAL_PROTOCOLLNPGM("Not SD printing"); + } +} +void CardReader::write_command(char *buf) +{ + char* begin = buf; + char* npos = 0; + char* end = buf + strlen(buf) - 1; + + file.writeError = false; + if((npos = strchr(buf, 'N')) != NULL) + { + begin = strchr(npos, ' ') + 1; + end = strchr(npos, '*') - 1; + } + end[1] = '\r'; + end[2] = '\n'; + end[3] = '\0'; + file.write(begin); + if (file.writeError) + { + SERIAL_ERROR_START; + SERIAL_ERRORLNRPGM(MSG_SD_ERR_WRITE_TO_FILE); + } +} + +#define CHUNK_SIZE 64 + +void CardReader::write_command_no_newline(char *buf) +{ + file.write(buf, CHUNK_SIZE); + if (file.writeError) + { + SERIAL_ERROR_START; + SERIAL_ERRORLNRPGM(MSG_SD_ERR_WRITE_TO_FILE); + MYSERIAL.println("An error while writing to the SD Card."); + } +} + + +void CardReader::checkautostart(bool force) +{ + if(!force) + { + if(!autostart_stilltocheck) + return; + if(autostart_atmillis<_millis()) + return; + } + autostart_stilltocheck=false; + if(!cardOK) + { + initsd(); + if(!cardOK) //fail + return; + } + + char autoname[30]; + sprintf_P(autoname, PSTR("auto%i.g"), lastnr); + for(int8_t i=0;i<(int8_t)strlen(autoname);i++) + autoname[i]=tolower(autoname[i]); + dir_t p; + + root.rewind(); + + bool found=false; + while (root.readDir(p, NULL) > 0) + { + for(int8_t i=0;i<(int8_t)strlen((char*)p.name);i++) + p.name[i]=tolower(p.name[i]); + //Serial.print((char*)p.name); + //Serial.print(" "); + //Serial.println(autoname); + if(p.name[9]!='~') //skip safety copies + if(strncmp((char*)p.name,autoname,5)==0) + { + char cmd[30]; + // M23: Select SD file + sprintf_P(cmd, PSTR("M23 %s"), autoname); + enquecommand(cmd); + // M24: Start/resume SD print + enquecommand_P(PSTR("M24")); + found=true; + } + } + if(!found) + lastnr=-1; + else + lastnr++; +} + +void CardReader::closefile(bool store_location) +{ + file.sync(); + file.close(); + saving = false; + logging = false; + + if(store_location) + { + //future: store printer state, filename and position for continuing a stopped print + // so one can unplug the printer and continue printing the next day. + + } + + +} + +void CardReader::getfilename(uint16_t nr, const char * const match/*=NULL*/) +{ + curDir=&workDir; + lsAction=LS_GetFilename; + nrFiles=nr; + curDir->rewind(); + lsDive("",*curDir,match); + +} + +void CardReader::getfilename_simple(uint32_t position, const char * const match/*=NULL*/) +{ + curDir = &workDir; + lsAction = LS_GetFilename; + nrFiles = 0; + curDir->seekSet(position); + lsDive("", *curDir, match); +} + +uint16_t CardReader::getnrfilenames() +{ + curDir=&workDir; + lsAction=LS_Count; + nrFiles=0; + curDir->rewind(); + lsDive("",*curDir); + //SERIAL_ECHOLN(nrFiles); + return nrFiles; +} + +void CardReader::chdir(const char * relpath) +{ + SdFile newfile; + SdFile *parent=&root; + + if(workDir.isOpen()) + parent=&workDir; + + if(!newfile.open(*parent,relpath, O_READ)) + { + SERIAL_ECHO_START; + SERIAL_ECHORPGM(_n("Cannot enter subdir: "));////MSG_SD_CANT_ENTER_SUBDIR + SERIAL_ECHOLN(relpath); + } + else + { + if (workDirDepth < MAX_DIR_DEPTH) { + for (int d = ++workDirDepth; d--;) + workDirParents[d+1] = workDirParents[d]; + workDirParents[0]=*parent; + } + workDir=newfile; + #ifdef SDCARD_SORT_ALPHA + presort(); + #endif + } +} + +void CardReader::updir() +{ + if(workDirDepth > 0) + { + --workDirDepth; + workDir = workDirParents[0]; + for (unsigned int d = 0; d < workDirDepth; d++) + { + workDirParents[d] = workDirParents[d+1]; + } + #ifdef SDCARD_SORT_ALPHA + presort(); + #endif + } +} + +#ifdef SDCARD_SORT_ALPHA + +/** +* Get the name of a file in the current directory by sort-index +*/ +void CardReader::getfilename_sorted(const uint16_t nr) { + getfilename( + #if SDSORT_GCODE + sort_alpha && + #endif + (nr < sort_count) ? sort_order[nr] : nr + ); +} + +/** +* Read all the files and produce a sort key +* +* We can do this in 3 ways... +* - Minimal RAM: Read two filenames at a time sorting along... +* - Some RAM: Buffer the directory just for this sort +* - Most RAM: Buffer the directory and return filenames from RAM +*/ +void CardReader::presort() { + if (farm_mode || IS_SD_INSERTED == false) return; //sorting is not used in farm mode + uint8_t sdSort = eeprom_read_byte((uint8_t*)EEPROM_SD_SORT); + + if (sdSort == SD_SORT_NONE) return; //sd sort is turned off + + #if SDSORT_GCODE + if (!sort_alpha) return; + #endif + KEEPALIVE_STATE(IN_HANDLER); + + // Throw away old sort index + flush_presort(); + + // If there are files, sort up to the limit + uint16_t fileCnt = getnrfilenames(); + if (fileCnt > 0) { + + // Never sort more than the max allowed + // If you use folders to organize, 20 may be enough + if (fileCnt > SDSORT_LIMIT) { + lcd_show_fullscreen_message_and_wait_P(_i("Some files will not be sorted. Max. No. of files in 1 folder for sorting is 100."));////MSG_FILE_CNT c=20 r=4 + fileCnt = SDSORT_LIMIT; + } + lcd_clear(); + #if !SDSORT_USES_RAM + lcd_set_progress(); + #endif + lcd_puts_at_P(0, 1, _i("Sorting files"));////MSG_SORTING c=20 r=1 + + // Sort order is always needed. May be static or dynamic. + #if SDSORT_DYNAMIC_RAM + sort_order = new uint8_t[fileCnt]; + #endif + + // Use RAM to store the entire directory during pre-sort. + // SDSORT_LIMIT should be set to prevent over-allocation. + #if SDSORT_USES_RAM + + // If using dynamic ram for names, allocate on the heap. + #if SDSORT_CACHE_NAMES + #if SDSORT_DYNAMIC_RAM + sortshort = new char*[fileCnt]; + sortnames = new char*[fileCnt]; + #endif + #elif SDSORT_USES_STACK + char sortnames[fileCnt][LONG_FILENAME_LENGTH]; + uint16_t creation_time[fileCnt]; + uint16_t creation_date[fileCnt]; + #endif + + // Folder sorting needs 1 bit per entry for flags. + #if HAS_FOLDER_SORTING + #if SDSORT_DYNAMIC_RAM + isDir = new uint8_t[(fileCnt + 7) >> 3]; + #elif SDSORT_USES_STACK + uint8_t isDir[(fileCnt + 7) >> 3]; + #endif + #endif + + #else // !SDSORT_USES_RAM + + uint32_t positions[fileCnt]; + + // By default re-read the names from SD for every compare + // retaining only two filenames at a time. This is very + // slow but is safest and uses minimal RAM. + char name1[LONG_FILENAME_LENGTH + 1]; + uint16_t creation_time_bckp; + uint16_t creation_date_bckp; + + #endif + position = 0; + if (fileCnt > 1) { + // Init sort order. + for (uint16_t i = 0; i < fileCnt; i++) { + if (!IS_SD_INSERTED) return; + manage_heater(); + sort_order[i] = i; + positions[i] = position; + getfilename(i); + // If using RAM then read all filenames now. + #if SDSORT_USES_RAM + getfilename(i); + #if SDSORT_DYNAMIC_RAM + // Use dynamic method to copy long filename + sortnames[i] = strdup(LONGEST_FILENAME); + #if SDSORT_CACHE_NAMES + // When caching also store the short name, since + // we're replacing the getfilename() behavior. + sortshort[i] = strdup(filename); + #endif + #else + // Copy filenames into the static array + strcpy(sortnames[i], LONGEST_FILENAME); + creation_time[i] = creationTime; + creation_date[i] = creationDate; + #if SDSORT_CACHE_NAMES + strcpy(sortshort[i], filename); + #endif + #endif + // char out[30]; + // sprintf_P(out, PSTR("---- %i %s %s"), i, filenameIsDir ? "D" : " ", sortnames[i]); + // SERIAL_ECHOLN(out); + #if HAS_FOLDER_SORTING + const uint16_t bit = i & 0x07, ind = i >> 3; + if (bit == 0) isDir[ind] = 0x00; + if (filenameIsDir) isDir[ind] |= _BV(bit); + #endif + #endif + } + +#ifdef QUICKSORT + quicksort(0, fileCnt - 1); +#else //Qicksort not defined, use Bubble Sort + uint32_t counter = 0; + uint16_t total = 0.5*(fileCnt - 1)*(fileCnt); + + // Compare names from the array or just the two buffered names + #if SDSORT_USES_RAM + #define _SORT_CMP_NODIR() (strcasecmp(sortnames[o1], sortnames[o2]) > 0) + #define _SORT_CMP_TIME_NODIR() (((creation_date[o1] == creation_date[o2]) && (creation_time[o1] < creation_time[o2])) || \ + (creation_date[o1] < creation_date [o2])) + #else + #define _SORT_CMP_NODIR() (strcasecmp(name1, name2) > 0) //true if lowercase(name1) > lowercase(name2) + #define _SORT_CMP_TIME_NODIR() (((creation_date_bckp == creationDate) && (creation_time_bckp > creationTime)) || \ + (creation_date_bckp > creationDate)) + + #endif + + #if HAS_FOLDER_SORTING + #if SDSORT_USES_RAM + // Folder sorting needs an index and bit to test for folder-ness. + const uint8_t ind1 = o1 >> 3, bit1 = o1 & 0x07, + ind2 = o2 >> 3, bit2 = o2 & 0x07; + #define _SORT_CMP_DIR(fs) \ + (((isDir[ind1] & _BV(bit1)) != 0) == ((isDir[ind2] & _BV(bit2)) != 0) \ + ? _SORT_CMP_NODIR() \ + : (isDir[fs > 0 ? ind1 : ind2] & (fs > 0 ? _BV(bit1) : _BV(bit2))) != 0) + #define _SORT_CMP_TIME_DIR(fs) \ + (((isDir[ind1] & _BV(bit1)) != 0) == ((isDir[ind2] & _BV(bit2)) != 0) \ + ? _SORT_CMP_TIME_NODIR() \ + : (isDir[fs > 0 ? ind1 : ind2] & (fs > 0 ? _BV(bit1) : _BV(bit2))) != 0) + #else + #define _SORT_CMP_DIR(fs) ((dir1 == filenameIsDir) ? _SORT_CMP_NODIR() : (fs > 0 ? dir1 : !dir1)) + #define _SORT_CMP_TIME_DIR(fs) ((dir1 == filenameIsDir) ? _SORT_CMP_TIME_NODIR() : (fs < 0 ? dir1 : !dir1)) + #endif + #endif + + for (uint16_t i = fileCnt; --i;) { + if (!IS_SD_INSERTED) return; + bool didSwap = false; + + #if !SDSORT_USES_RAM //show progresss bar only if slow sorting method is used + int8_t percent = (counter * 100) / total;//((counter * 100) / pow((fileCnt-1),2)); + for (int column = 0; column < 20; column++) { + if (column < (percent / 5)) + { + lcd_set_cursor(column, 2); + lcd_print('\x01'); //simple progress bar + } + } + counter++; + #endif + + //MYSERIAL.println(int(i)); + for (uint16_t j = 0; j < i; ++j) { + if (!IS_SD_INSERTED) return; + manage_heater(); + const uint16_t o1 = sort_order[j], o2 = sort_order[j + 1]; + + // The most economical method reads names as-needed + // throughout the loop. Slow if there are many. + #if !SDSORT_USES_RAM + counter++; + getfilename_simple(positions[o1]); + strcpy(name1, LONGEST_FILENAME); // save (or getfilename below will trounce it) + creation_date_bckp = creationDate; + creation_time_bckp = creationTime; + #if HAS_FOLDER_SORTING + bool dir1 = filenameIsDir; + #endif + getfilename_simple(positions[o2]); + char *name2 = LONGEST_FILENAME; // use the string in-place + + #endif // !SDSORT_USES_RAM + + // Sort the current pair according to settings. + if ( + #if HAS_FOLDER_SORTING + (sdSort == SD_SORT_TIME && _SORT_CMP_TIME_DIR(FOLDER_SORTING)) || (sdSort == SD_SORT_ALPHA && _SORT_CMP_DIR(FOLDER_SORTING)) + #else + (sdSort == SD_SORT_TIME && _SORT_CMP_TIME_NODIR()) || (sdSort == SD_SORT_ALPHA && _SORT_CMP_NODIR()) + #endif + ) + { + sort_order[j] = o2; + sort_order[j + 1] = o1; + didSwap = true; + } + } + if (!didSwap) break; + } //end of bubble sort loop +#endif + // Using RAM but not keeping names around + #if (SDSORT_USES_RAM && !SDSORT_CACHE_NAMES) + #if SDSORT_DYNAMIC_RAM + for (uint16_t i = 0; i < fileCnt; ++i) free(sortnames[i]); + #if HAS_FOLDER_SORTING + free(isDir); + #endif + #endif + #endif + } + else { + sort_order[0] = 0; + #if (SDSORT_USES_RAM && SDSORT_CACHE_NAMES) + getfilename(0); + #if SDSORT_DYNAMIC_RAM + sortnames = new char*[1]; + sortnames[0] = strdup(LONGEST_FILENAME); // malloc + sortshort = new char*[1]; + sortshort[0] = strdup(filename); // malloc + isDir = new uint8_t[1]; + #else + strcpy(sortnames[0], LONGEST_FILENAME); + strcpy(sortshort[0], filename); + #endif + isDir[0] = filenameIsDir ? 0x01 : 0x00; + #endif + } + + sort_count = fileCnt; + } +#if !SDSORT_USES_RAM //show progresss bar only if slow sorting method is used + for (int column = 0; column <= 19; column++) + { + lcd_set_cursor(column, 2); + lcd_print('\x01'); //simple progress bar + } + _delay(300); + lcd_set_degree(); + lcd_clear(); +#endif + lcd_update(2); + KEEPALIVE_STATE(NOT_BUSY); + lcd_timeoutToStatus.start(); +} + +void CardReader::flush_presort() { + if (sort_count > 0) { + #if SDSORT_DYNAMIC_RAM + delete sort_order; + #if SDSORT_CACHE_NAMES + for (uint8_t i = 0; i < sort_count; ++i) { + free(sortshort[i]); // strdup + free(sortnames[i]); // strdup + } + delete sortshort; + delete sortnames; + #endif + #endif + sort_count = 0; + } +} + +#endif // SDCARD_SORT_ALPHA + + + +void CardReader::printingHasFinished() +{ + st_synchronize(); + if(file_subcall_ctr>0) //heading up to a parent file that called current as a procedure. + { + file.close(); + file_subcall_ctr--; + openFile(filenames[file_subcall_ctr],true,true); + setIndex(filespos[file_subcall_ctr]); + startFileprint(); + } + else + { + quickStop(); + file.close(); + sdprinting = false; + if(SD_FINISHED_STEPPERRELEASE) + { + finishAndDisableSteppers(); + //enquecommand_P(PSTR(SD_FINISHED_RELEASECOMMAND)); + } + autotempShutdown(); + #ifdef SDCARD_SORT_ALPHA + //presort(); + #endif + } +} + +bool CardReader::ToshibaFlashAir_GetIP(uint8_t *ip) +{ + memset(ip, 0, 4); + return card.readExtMemory(1, 1, 0x400+0x150, 4, ip); +} + +#endif //SDSUPPORT diff --git a/Firmware/cardreader.h b/Firmware/cardreader.h new file mode 100644 index 0000000..26981ef --- /dev/null +++ b/Firmware/cardreader.h @@ -0,0 +1,184 @@ +#ifndef CARDREADER_H +#define CARDREADER_H + +#ifdef SDSUPPORT + +#define MAX_DIR_DEPTH 10 + +#include "SdFile.h" +enum LsAction {LS_SerialPrint,LS_Count,LS_GetFilename}; +class CardReader +{ +public: + CardReader(); + + void initsd(); + void write_command(char *buf); + void write_command_no_newline(char *buf); + //files auto[0-9].g on the sd card are performed in a row + //this is to delay autostart and hence the initialisaiton of the sd card to some seconds after the normal init, so the device is available quick after a reset + + void checkautostart(bool x); + void openFile(const char* name,bool read,bool replace_current=true); + void openLogFile(const char* name); + void removeFile(const char* name); + void closefile(bool store_location=false); + void release(); + void startFileprint(); + void pauseSDPrint(); + uint32_t getFileSize(); + void getStatus(); + void printingHasFinished(); + + void getfilename(uint16_t nr, const char* const match=NULL); + void getfilename_simple(uint32_t position, const char * const match = NULL); + uint16_t getnrfilenames(); + + void getAbsFilename(char *t); + void getDirName(char* name, uint8_t level); + uint16_t getWorkDirDepth(); + + + void ls(); + void chdir(const char * relpath); + void updir(); + void setroot(); + + #ifdef SDCARD_SORT_ALPHA + void presort(); + #ifdef SDSORT_QUICKSORT + void swap(uint8_t left, uint8_t right); + void quicksort(uint8_t left, uint8_t right); + #endif //SDSORT_QUICKSORT + void getfilename_sorted(const uint16_t nr); + #if SDSORT_GCODE + FORCE_INLINE void setSortOn(bool b) { sort_alpha = b; presort(); } + FORCE_INLINE void setSortFolders(int i) { sort_folders = i; presort(); } + //FORCE_INLINE void setSortReverse(bool b) { sort_reverse = b; } + #endif + #endif + + FORCE_INLINE bool isFileOpen() { return file.isOpen(); } + FORCE_INLINE bool eof() { return sdpos>=filesize ;}; + FORCE_INLINE int16_t get() { sdpos = file.curPosition();return (int16_t)file.read();}; + FORCE_INLINE void setIndex(long index) {sdpos = index;file.seekSet(index);}; + FORCE_INLINE uint8_t percentDone(){if(!isFileOpen()) return 0; if(filesize) return sdpos/((filesize+99)/100); else return 0;}; + FORCE_INLINE char* getWorkDirName(){workDir.getFilename(filename);return filename;}; + FORCE_INLINE uint32_t get_sdpos() { if (!isFileOpen()) return 0; else return(sdpos); }; + + bool ToshibaFlashAir_isEnabled() const { return card.getFlashAirCompatible(); } + void ToshibaFlashAir_enable(bool enable) { card.setFlashAirCompatible(enable); } + bool ToshibaFlashAir_GetIP(uint8_t *ip); + +public: + bool saving; + bool logging; + bool sdprinting ; + bool cardOK ; + bool paused ; + char filename[13]; + uint16_t creationTime, creationDate; + uint32_t cluster, position; + char longFilename[LONG_FILENAME_LENGTH]; + bool filenameIsDir; + int lastnr; //last number of the autostart; +private: + SdFile root,*curDir,workDir,workDirParents[MAX_DIR_DEPTH]; + uint16_t workDirDepth; + + // Sort files and folders alphabetically. +#ifdef SDCARD_SORT_ALPHA + uint16_t sort_count; // Count of sorted items in the current directory + #if SDSORT_GCODE + bool sort_alpha; // Flag to enable / disable the feature + int sort_folders; // Flag to enable / disable folder sorting + //bool sort_reverse; // Flag to enable / disable reverse sorting + #endif + + // By default the sort index is static + #if SDSORT_DYNAMIC_RAM + uint8_t *sort_order; + #else + uint8_t sort_order[SDSORT_LIMIT]; + #endif + // Cache filenames to speed up SD menus. + #if SDSORT_USES_RAM + + // If using dynamic ram for names, allocate on the heap. + #if SDSORT_CACHE_NAMES + #if SDSORT_DYNAMIC_RAM + char **sortshort, **sortnames; + #else + char sortshort[SDSORT_LIMIT][FILENAME_LENGTH]; + char sortnames[SDSORT_LIMIT][FILENAME_LENGTH]; + #endif + #elif !SDSORT_USES_STACK + char sortnames[SDSORT_LIMIT][FILENAME_LENGTH]; + uint16_t creation_time[SDSORT_LIMIT]; + uint16_t creation_date[SDSORT_LIMIT]; + #endif + + // Folder sorting uses an isDir array when caching items. + #if HAS_FOLDER_SORTING + #if SDSORT_DYNAMIC_RAM + uint8_t *isDir; + #elif (SDSORT_CACHE_NAMES) || !(SDSORT_USES_STACK) + uint8_t isDir[(SDSORT_LIMIT + 7) >> 3]; + #endif + #endif + + #endif // SDSORT_USES_RAM + +#endif // SDCARD_SORT_ALPHA + +#ifdef DEBUG_SD_SPEED_TEST +public: +#endif //DEBUG_SD_SPEED_TEST + Sd2Card card; + +private: + SdVolume volume; + SdFile file; + #define SD_PROCEDURE_DEPTH 1 + #define MAXPATHNAMELENGTH (13*MAX_DIR_DEPTH+MAX_DIR_DEPTH+1) + uint8_t file_subcall_ctr; + uint32_t filespos[SD_PROCEDURE_DEPTH]; + char filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH]; + uint32_t filesize; + //int16_t n; + unsigned long autostart_atmillis; + uint32_t sdpos ; + + bool autostart_stilltocheck; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware. + + LsAction lsAction; //stored for recursion. + int16_t nrFiles; //counter for the files in the current directory and recycled as position counter for getting the nrFiles'th name in the directory. + char* diveDirName; + + void diveSubfolder (const char *fileName, SdFile& dir); + void lsDive(const char *prepend, SdFile parent, const char * const match=NULL); +#ifdef SDCARD_SORT_ALPHA + void flush_presort(); +#endif +}; +extern bool Stopped; +extern CardReader card; +#define IS_SD_PRINTING (card.sdprinting) + +#if (SDCARDDETECT > -1) +# ifdef SDCARDDETECTINVERTED +# define IS_SD_INSERTED (READ(SDCARDDETECT)!=0) +# else +# define IS_SD_INSERTED (READ(SDCARDDETECT)==0) +# endif //SDCARDTETECTINVERTED +#else +//If we don't have a card detect line, aways asume the card is inserted +# define IS_SD_INSERTED true +#endif + +#else + +#define IS_SD_PRINTING (false) + +#endif //SDSUPPORT +#endif diff --git a/Firmware/cmdqueue.cpp b/Firmware/cmdqueue.cpp new file mode 100644 index 0000000..74a8b90 --- /dev/null +++ b/Firmware/cmdqueue.cpp @@ -0,0 +1,701 @@ +#include "cmdqueue.h" +#include "cardreader.h" +#include "ultralcd.h" + +extern bool Stopped; + +// Reserve BUFSIZE lines of length MAX_CMD_SIZE plus CMDBUFFER_RESERVE_FRONT. +char cmdbuffer[BUFSIZE * (MAX_CMD_SIZE + 1) + CMDBUFFER_RESERVE_FRONT]; +// Head of the circular buffer, where to read. +size_t bufindr = 0; +// Tail of the buffer, where to write. +static size_t bufindw = 0; +// Number of lines in cmdbuffer. +int buflen = 0; +// Flag for processing the current command inside the main Arduino loop(). +// If a new command was pushed to the front of a command buffer while +// processing another command, this replaces the command on the top. +// Therefore don't remove the command from the queue in the loop() function. +bool cmdbuffer_front_already_processed = false; + +int serial_count = 0; //index of character read from serial line +boolean comment_mode = false; +char *strchr_pointer; // just a pointer to find chars in the command string like X, Y, Z, E, etc + +unsigned long TimeSent = _millis(); +unsigned long TimeNow = _millis(); + +long gcode_N = 0; +long gcode_LastN = 0; +long Stopped_gcode_LastN = 0; + +uint32_t sdpos_atomic = 0; + + +// Pop the currently processed command from the queue. +// It is expected, that there is at least one command in the queue. +bool cmdqueue_pop_front() +{ + if (buflen > 0) { +#ifdef CMDBUFFER_DEBUG + SERIAL_ECHOPGM("Dequeing "); + SERIAL_ECHO(cmdbuffer+bufindr+CMDHDRSIZE); + SERIAL_ECHOLNPGM(""); + SERIAL_ECHOPGM("Old indices: buflen "); + SERIAL_ECHO(buflen); + SERIAL_ECHOPGM(", bufindr "); + SERIAL_ECHO(bufindr); + SERIAL_ECHOPGM(", bufindw "); + SERIAL_ECHO(bufindw); + SERIAL_ECHOPGM(", serial_count "); + SERIAL_ECHO(serial_count); + SERIAL_ECHOPGM(", bufsize "); + SERIAL_ECHO(sizeof(cmdbuffer)); + SERIAL_ECHOLNPGM(""); +#endif /* CMDBUFFER_DEBUG */ + if (-- buflen == 0) { + // Empty buffer. + if (serial_count == 0) + // No serial communication is pending. Reset both pointers to zero. + bufindw = 0; + bufindr = bufindw; + } else { + // There is at least one ready line in the buffer. + // First skip the current command ID and iterate up to the end of the string. + for (bufindr += CMDHDRSIZE; cmdbuffer[bufindr] != 0; ++ bufindr) ; + // Second, skip the end of string null character and iterate until a nonzero command ID is found. + for (++ bufindr; bufindr < sizeof(cmdbuffer) && cmdbuffer[bufindr] == 0; ++ bufindr) ; + // If the end of the buffer was empty, + if (bufindr == sizeof(cmdbuffer)) { + // skip to the start and find the nonzero command. + for (bufindr = 0; cmdbuffer[bufindr] == 0; ++ bufindr) ; + } +#ifdef CMDBUFFER_DEBUG + SERIAL_ECHOPGM("New indices: buflen "); + SERIAL_ECHO(buflen); + SERIAL_ECHOPGM(", bufindr "); + SERIAL_ECHO(bufindr); + SERIAL_ECHOPGM(", bufindw "); + SERIAL_ECHO(bufindw); + SERIAL_ECHOPGM(", serial_count "); + SERIAL_ECHO(serial_count); + SERIAL_ECHOPGM(" new command on the top: "); + SERIAL_ECHO(cmdbuffer+bufindr+CMDHDRSIZE); + SERIAL_ECHOLNPGM(""); +#endif /* CMDBUFFER_DEBUG */ + } + return true; + } + return false; +} + +void cmdqueue_reset() +{ + bufindr = 0; + bufindw = 0; + buflen = 0; + + //commands are removed from command queue after process_command() function is finished + //reseting command queue and enqueing new commands during some (usually long running) command processing would cause that new commands are immediately removed from queue (or damaged) + //this will ensure that all new commands which are enqueued after cmdqueue reset, will be always executed + cmdbuffer_front_already_processed = true; +} + +// How long a string could be pushed to the front of the command queue? +// If yes, adjust bufindr to the new position, where the new command could be enqued. +// len_asked does not contain the zero terminator size. +static bool cmdqueue_could_enqueue_front(size_t len_asked) +{ + // MAX_CMD_SIZE has to accommodate the zero terminator. + if (len_asked >= MAX_CMD_SIZE) + return false; + // Remove the currently processed command from the queue. + if (! cmdbuffer_front_already_processed) { + cmdqueue_pop_front(); + cmdbuffer_front_already_processed = true; + } + if (bufindr == bufindw && buflen > 0) + // Full buffer. + return false; + // Adjust the end of the write buffer based on whether a partial line is in the receive buffer. + int endw = (serial_count > 0) ? (bufindw + MAX_CMD_SIZE + 1) : bufindw; + if (bufindw < bufindr) { + int bufindr_new = bufindr - len_asked - (1 + CMDHDRSIZE); + // Simple case. There is a contiguous space between the write buffer and the read buffer. + if (endw <= bufindr_new) { + bufindr = bufindr_new; + return true; + } + } else { + // Otherwise the free space is split between the start and end. + if (len_asked + (1 + CMDHDRSIZE) <= bufindr) { + // Could fit at the start. + bufindr -= len_asked + (1 + CMDHDRSIZE); + return true; + } + int bufindr_new = sizeof(cmdbuffer) - len_asked - (1 + CMDHDRSIZE); + if (endw <= bufindr_new) { + memset(cmdbuffer, 0, bufindr); + bufindr = bufindr_new; + return true; + } + } + return false; +} + +// Could one enqueue a command of length len_asked into the buffer, +// while leaving CMDBUFFER_RESERVE_FRONT at the start? +// If yes, adjust bufindw to the new position, where the new command could be enqued. +// len_asked does not contain the zero terminator size. +// This function may update bufindw, therefore for the power panic to work, this function must be called +// with the interrupts disabled! +static bool cmdqueue_could_enqueue_back(size_t len_asked, bool atomic_update = false) +{ + // MAX_CMD_SIZE has to accommodate the zero terminator. + if (len_asked >= MAX_CMD_SIZE) + return false; + + if (bufindr == bufindw && buflen > 0) + // Full buffer. + return false; + + if (serial_count > 0) { + // If there is some data stored starting at bufindw, len_asked is certainly smaller than + // the allocated data buffer. Try to reserve a new buffer and to move the already received + // serial data. + // How much memory to reserve for the commands pushed to the front? + // End of the queue, when pushing to the end. + size_t endw = bufindw + len_asked + (1 + CMDHDRSIZE); + if (bufindw < bufindr) + // Simple case. There is a contiguous space between the write buffer and the read buffer. + return endw + CMDBUFFER_RESERVE_FRONT <= bufindr; + // Otherwise the free space is split between the start and end. + if (// Could one fit to the end, including the reserve? + endw + CMDBUFFER_RESERVE_FRONT <= sizeof(cmdbuffer) || + // Could one fit to the end, and the reserve to the start? + (endw <= sizeof(cmdbuffer) && CMDBUFFER_RESERVE_FRONT <= bufindr)) + return true; + // Could one fit both to the start? + if (len_asked + (1 + CMDHDRSIZE) + CMDBUFFER_RESERVE_FRONT <= bufindr) { + // Mark the rest of the buffer as used. + memset(cmdbuffer+bufindw, 0, sizeof(cmdbuffer)-bufindw); + // and point to the start. + // Be careful! The bufindw needs to be changed atomically for the power panic & filament panic to work. + if (atomic_update) + cli(); + bufindw = 0; + if (atomic_update) + sei(); + return true; + } + } else { + // How much memory to reserve for the commands pushed to the front? + // End of the queue, when pushing to the end. + size_t endw = bufindw + len_asked + (1 + CMDHDRSIZE); + if (bufindw < bufindr) + // Simple case. There is a contiguous space between the write buffer and the read buffer. + return endw + CMDBUFFER_RESERVE_FRONT <= bufindr; + // Otherwise the free space is split between the start and end. + if (// Could one fit to the end, including the reserve? + endw + CMDBUFFER_RESERVE_FRONT <= sizeof(cmdbuffer) || + // Could one fit to the end, and the reserve to the start? + (endw <= sizeof(cmdbuffer) && CMDBUFFER_RESERVE_FRONT <= bufindr)) + return true; + // Could one fit both to the start? + if (len_asked + (1 + CMDHDRSIZE) + CMDBUFFER_RESERVE_FRONT <= bufindr) { + // Mark the rest of the buffer as used. + memset(cmdbuffer+bufindw, 0, sizeof(cmdbuffer)-bufindw); + // and point to the start. + // Be careful! The bufindw needs to be changed atomically for the power panic & filament panic to work. + if (atomic_update) + cli(); + bufindw = 0; + if (atomic_update) + sei(); + return true; + } + } + return false; +} + +#ifdef CMDBUFFER_DEBUG +void cmdqueue_dump_to_serial_single_line(int nr, const char *p) +{ + SERIAL_ECHOPGM("Entry nr: "); + SERIAL_ECHO(nr); + SERIAL_ECHOPGM(", type: "); + int type = *p; + SERIAL_ECHO(type); + SERIAL_ECHOPGM(", size: "); + unsigned int size = *(unsigned int*)(p + 1); + SERIAL_ECHO(size); + SERIAL_ECHOPGM(", cmd: "); + SERIAL_ECHO(p + CMDHDRSIZE); + SERIAL_ECHOLNPGM(""); +} + +void cmdqueue_dump_to_serial() +{ + if (buflen == 0) { + SERIAL_ECHOLNPGM("The command buffer is empty."); + } else { + SERIAL_ECHOPGM("Content of the buffer: entries "); + SERIAL_ECHO(buflen); + SERIAL_ECHOPGM(", indr "); + SERIAL_ECHO(bufindr); + SERIAL_ECHOPGM(", indw "); + SERIAL_ECHO(bufindw); + SERIAL_ECHOLNPGM(""); + int nr = 0; + if (bufindr < bufindw) { + for (const char *p = cmdbuffer + bufindr; p < cmdbuffer + bufindw; ++ nr) { + cmdqueue_dump_to_serial_single_line(nr, p); + // Skip the command. + for (p += CMDHDRSIZE; *p != 0; ++ p); + // Skip the gaps. + for (++p; p < cmdbuffer + bufindw && *p == 0; ++ p); + } + } else { + for (const char *p = cmdbuffer + bufindr; p < cmdbuffer + sizeof(cmdbuffer); ++ nr) { + cmdqueue_dump_to_serial_single_line(nr, p); + // Skip the command. + for (p += CMDHDRSIZE; *p != 0; ++ p); + // Skip the gaps. + for (++p; p < cmdbuffer + sizeof(cmdbuffer) && *p == 0; ++ p); + } + for (const char *p = cmdbuffer; p < cmdbuffer + bufindw; ++ nr) { + cmdqueue_dump_to_serial_single_line(nr, p); + // Skip the command. + for (p += CMDHDRSIZE; *p != 0; ++ p); + // Skip the gaps. + for (++p; p < cmdbuffer + bufindw && *p == 0; ++ p); + } + } + SERIAL_ECHOLNPGM("End of the buffer."); + } +} +#endif /* CMDBUFFER_DEBUG */ + +//adds an command to the main command buffer +//thats really done in a non-safe way. +//needs overworking someday +// Currently the maximum length of a command piped through this function is around 20 characters +void enquecommand(const char *cmd, bool from_progmem) +{ + size_t len = from_progmem ? strlen_P(cmd) : strlen(cmd); + // Does cmd fit the queue while leaving sufficient space at the front for the chained commands? + // If it fits, it may move bufindw, so it points to a contiguous buffer, which fits cmd. + if (cmdqueue_could_enqueue_back(len)) { + // This is dangerous if a mixing of serial and this happens + // This may easily be tested: If serial_count > 0, we have a problem. + cmdbuffer[bufindw] = CMDBUFFER_CURRENT_TYPE_UI; + if (from_progmem) + strcpy_P(cmdbuffer + bufindw + CMDHDRSIZE, cmd); + else + strcpy(cmdbuffer + bufindw + CMDHDRSIZE, cmd); + SERIAL_ECHO_START; + SERIAL_ECHORPGM(MSG_Enqueing); + SERIAL_ECHO(cmdbuffer + bufindw + CMDHDRSIZE); + SERIAL_ECHOLNPGM("\""); + bufindw += len + (CMDHDRSIZE + 1); + if (bufindw == sizeof(cmdbuffer)) + bufindw = 0; + ++ buflen; +#ifdef CMDBUFFER_DEBUG + cmdqueue_dump_to_serial(); +#endif /* CMDBUFFER_DEBUG */ + } else { + SERIAL_ERROR_START; + SERIAL_ECHORPGM(MSG_Enqueing); + if (from_progmem) + SERIAL_PROTOCOLRPGM(cmd); + else + SERIAL_ECHO(cmd); + SERIAL_ECHOLNPGM("\" failed: Buffer full!"); +#ifdef CMDBUFFER_DEBUG + cmdqueue_dump_to_serial(); +#endif /* CMDBUFFER_DEBUG */ + } +} + +bool cmd_buffer_empty() +{ + return (buflen == 0); +} + +void enquecommand_front(const char *cmd, bool from_progmem) +{ + size_t len = from_progmem ? strlen_P(cmd) : strlen(cmd); + // Does cmd fit the queue? This call shall move bufindr, so the command may be copied. + if (cmdqueue_could_enqueue_front(len)) { + cmdbuffer[bufindr] = CMDBUFFER_CURRENT_TYPE_UI; + if (from_progmem) + strcpy_P(cmdbuffer + bufindr + CMDHDRSIZE, cmd); + else + strcpy(cmdbuffer + bufindr + CMDHDRSIZE, cmd); + ++ buflen; + SERIAL_ECHO_START; + SERIAL_ECHOPGM("Enqueing to the front: \""); + SERIAL_ECHO(cmdbuffer + bufindr + CMDHDRSIZE); + SERIAL_ECHOLNPGM("\""); +#ifdef CMDBUFFER_DEBUG + cmdqueue_dump_to_serial(); +#endif /* CMDBUFFER_DEBUG */ + } else { + SERIAL_ERROR_START; + SERIAL_ECHOPGM("Enqueing to the front: \""); + if (from_progmem) + SERIAL_PROTOCOLRPGM(cmd); + else + SERIAL_ECHO(cmd); + SERIAL_ECHOLNPGM("\" failed: Buffer full!"); +#ifdef CMDBUFFER_DEBUG + cmdqueue_dump_to_serial(); +#endif /* CMDBUFFER_DEBUG */ + } +} + +// Mark the command at the top of the command queue as new. +// Therefore it will not be removed from the queue. +void repeatcommand_front() +{ + cmdbuffer_front_already_processed = true; +} + +bool is_buffer_empty() +{ + if (buflen == 0) return true; + else return false; +} + +void proc_commands() { + if (buflen) + { + process_commands(); + if (!cmdbuffer_front_already_processed) + cmdqueue_pop_front(); + cmdbuffer_front_already_processed = false; + } +} + +void get_command() +{ + // Test and reserve space for the new command string. + if (! cmdqueue_could_enqueue_back(MAX_CMD_SIZE - 1, true)) + return; + + if (MYSERIAL.available() == RX_BUFFER_SIZE - 1) { //compare number of chars buffered in rx buffer with rx buffer size + MYSERIAL.flush(); + SERIAL_ECHOLNPGM("Full RX Buffer"); //if buffer was full, there is danger that reading of last gcode will not be completed + } + + // start of serial line processing loop + while ((MYSERIAL.available() > 0 && !saved_printing) || (MYSERIAL.available() > 0 && isPrintPaused)) { //is print is saved (crash detection or filament detection), dont process data from serial line + + char serial_char = MYSERIAL.read(); +/* if (selectedSerialPort == 1) + { + selectedSerialPort = 0; + MYSERIAL.write(serial_char); // for debuging serial line 2 in farm_mode + selectedSerialPort = 1; + } */ //RP - removed + TimeSent = _millis(); + TimeNow = _millis(); + + if (serial_char < 0) + // Ignore extended ASCII characters. These characters have no meaning in the G-code apart from the file names + // and Marlin does not support such file names anyway. + // Serial characters with a highest bit set to 1 are generated when the USB cable is unplugged, leading + // to a hang-up of the print process from an SD card. + continue; + if(serial_char == '\n' || + serial_char == '\r' || + serial_count >= (MAX_CMD_SIZE - 1) ) + { + if(!serial_count) { //if empty line + comment_mode = false; //for new command + return; + } + cmdbuffer[bufindw+serial_count+CMDHDRSIZE] = 0; //terminate string + if(!comment_mode){ + + gcode_N = 0; + + // Line numbers must be first in buffer + + if ((strstr(cmdbuffer+bufindw+CMDHDRSIZE, "PRUSA") == NULL) && + (cmdbuffer[bufindw+CMDHDRSIZE] == 'N')) { + + // Line number met. When sending a G-code over a serial line, each line may be stamped with its index, + // and Marlin tests, whether the successive lines are stamped with an increasing line number ID + gcode_N = (strtol(cmdbuffer+bufindw+CMDHDRSIZE+1, NULL, 10)); + if(gcode_N != gcode_LastN+1 && (strstr_P(cmdbuffer+bufindw+CMDHDRSIZE, PSTR("M110")) == NULL) ) { + // M110 - set current line number. + // Line numbers not sent in succession. + SERIAL_ERROR_START; + SERIAL_ERRORRPGM(_n("Line Number is not Last Line Number+1, Last Line: "));////MSG_ERR_LINE_NO + SERIAL_ERRORLN(gcode_LastN); + //Serial.println(gcode_N); + FlushSerialRequestResend(); + serial_count = 0; + return; + } + + if((strchr_pointer = strchr(cmdbuffer+bufindw+CMDHDRSIZE, '*')) != NULL) + { + byte checksum = 0; + char *p = cmdbuffer+bufindw+CMDHDRSIZE; + while (p != strchr_pointer) + checksum = checksum^(*p++); + if (int(strtol(strchr_pointer+1, NULL, 10)) != int(checksum)) { + SERIAL_ERROR_START; + SERIAL_ERRORRPGM(_n("checksum mismatch, Last Line: "));////MSG_ERR_CHECKSUM_MISMATCH + SERIAL_ERRORLN(gcode_LastN); + FlushSerialRequestResend(); + serial_count = 0; + return; + } + // If no errors, remove the checksum and continue parsing. + *strchr_pointer = 0; + } + else + { + SERIAL_ERROR_START; + SERIAL_ERRORRPGM(_n("No Checksum with line number, Last Line: "));////MSG_ERR_NO_CHECKSUM + SERIAL_ERRORLN(gcode_LastN); + FlushSerialRequestResend(); + serial_count = 0; + return; + } + + // Don't parse N again with code_seen('N') + cmdbuffer[bufindw + CMDHDRSIZE] = '$'; + //if no errors, continue parsing + gcode_LastN = gcode_N; + } + // if we don't receive 'N' but still see '*' + if ((cmdbuffer[bufindw + CMDHDRSIZE] != 'N') && (cmdbuffer[bufindw + CMDHDRSIZE] != '$') && (strchr(cmdbuffer+bufindw+CMDHDRSIZE, '*') != NULL)) + { + + SERIAL_ERROR_START; + SERIAL_ERRORRPGM(_n("No Line Number with checksum, Last Line: "));////MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM + SERIAL_ERRORLN(gcode_LastN); + FlushSerialRequestResend(); + serial_count = 0; + return; + } + if ((strchr_pointer = strchr(cmdbuffer+bufindw+CMDHDRSIZE, 'G')) != NULL) { + if (! IS_SD_PRINTING) { + usb_printing_counter = 10; + is_usb_printing = true; + } + if (Stopped == true) { + int gcode = strtol(strchr_pointer+1, NULL, 10); + if (gcode >= 0 && gcode <= 3) { + SERIAL_ERRORLNRPGM(MSG_ERR_STOPPED); + LCD_MESSAGERPGM(_T(MSG_STOPPED)); + } + } + } // end of 'G' command + + //If command was e-stop process now + if(strcmp(cmdbuffer+bufindw+CMDHDRSIZE, "M112") == 0) + kill("", 2); + + // Store the current line into buffer, move to the next line. + // Store type of entry + cmdbuffer[bufindw] = gcode_N ? CMDBUFFER_CURRENT_TYPE_USB_WITH_LINENR : CMDBUFFER_CURRENT_TYPE_USB; +#ifdef CMDBUFFER_DEBUG + SERIAL_ECHO_START; + SERIAL_ECHOPGM("Storing a command line to buffer: "); + SERIAL_ECHO(cmdbuffer+bufindw+CMDHDRSIZE); + SERIAL_ECHOLNPGM(""); +#endif /* CMDBUFFER_DEBUG */ + bufindw += strlen(cmdbuffer+bufindw+CMDHDRSIZE) + (1 + CMDHDRSIZE); + if (bufindw == sizeof(cmdbuffer)) + bufindw = 0; + ++ buflen; +#ifdef CMDBUFFER_DEBUG + SERIAL_ECHOPGM("Number of commands in the buffer: "); + SERIAL_ECHO(buflen); + SERIAL_ECHOLNPGM(""); +#endif /* CMDBUFFER_DEBUG */ + } // end of 'not comment mode' + serial_count = 0; //clear buffer + // Don't call cmdqueue_could_enqueue_back if there are no characters waiting + // in the queue, as this function will reserve the memory. + if (MYSERIAL.available() == 0 || ! cmdqueue_could_enqueue_back(MAX_CMD_SIZE-1, true)) + return; + } // end of "end of line" processing + else { + // Not an "end of line" symbol. Store the new character into a buffer. + if(serial_char == ';') comment_mode = true; + if(!comment_mode) cmdbuffer[bufindw+CMDHDRSIZE+serial_count++] = serial_char; + } + } // end of serial line processing loop + + if(farm_mode){ + TimeNow = _millis(); + if ( ((TimeNow - TimeSent) > 800) && (serial_count > 0) ) { + cmdbuffer[bufindw+serial_count+CMDHDRSIZE] = 0; + + bufindw += strlen(cmdbuffer+bufindw+CMDHDRSIZE) + (1 + CMDHDRSIZE); + if (bufindw == sizeof(cmdbuffer)) + bufindw = 0; + ++ buflen; + + serial_count = 0; + + SERIAL_ECHOPGM("TIMEOUT:"); + //memset(cmdbuffer, 0 , sizeof(cmdbuffer)); + return; + } + } + + #ifdef SDSUPPORT + if(!card.sdprinting || serial_count!=0){ + // If there is a half filled buffer from serial line, wait until return before + // continuing with the serial line. + return; + } + + //'#' stops reading from SD to the buffer prematurely, so procedural macro calls are possible + // if it occurs, stop_buffering is triggered and the buffer is ran dry. + // this character _can_ occur in serial com, due to checksums. however, no checksums are used in SD printing + + static bool stop_buffering=false; + if(buflen==0) stop_buffering=false; + union { + struct { + char lo; + char hi; + } lohi; + uint16_t value; + } sd_count; + sd_count.value = 0; + // Reads whole lines from the SD card. Never leaves a half-filled line in the cmdbuffer. + while( !card.eof() && !stop_buffering) { + int16_t n=card.get(); + char serial_char = (char)n; + if(serial_char == '\n' || + serial_char == '\r' || + ((serial_char == '#' || serial_char == ':') && comment_mode == false) || + serial_count >= (MAX_CMD_SIZE - 1) || n==-1) + { + if(card.eof()){ + SERIAL_PROTOCOLLNRPGM(_n("Done printing file"));////MSG_FILE_PRINTED + stoptime=_millis(); + char time[30]; + unsigned long t=(stoptime-starttime-pause_time)/1000; + pause_time = 0; + int hours, minutes; + minutes=(t/60)%60; + hours=t/60/60; + save_statistics(total_filament_used, t); + sprintf_P(time, PSTR("%i hours %i minutes"),hours, minutes); + SERIAL_ECHO_START; + SERIAL_ECHOLN(time); + lcd_setstatus(time); + card.printingHasFinished(); + card.checkautostart(true); + + if (farm_mode) + { + prusa_statistics(6); + lcd_commands_type = LcdCommands::FarmModeConfirm; + } + + } + if(serial_char=='#') + stop_buffering=true; + + if(!serial_count) + { + // This is either an empty line, or a line with just a comment. + // Continue to the following line, and continue accumulating the number of bytes + // read from the sdcard into sd_count, + // so that the lenght of the already read empty lines and comments will be added + // to the following non-empty line. + comment_mode = false; + continue; //if empty line + } + // The new command buffer could be updated non-atomically, because it is not yet considered + // to be inside the active queue. + sd_count.value = (card.get_sdpos()+1) - sdpos_atomic; + cmdbuffer[bufindw] = CMDBUFFER_CURRENT_TYPE_SDCARD; + cmdbuffer[bufindw+1] = sd_count.lohi.lo; + cmdbuffer[bufindw+2] = sd_count.lohi.hi; + cmdbuffer[bufindw+serial_count+CMDHDRSIZE] = 0; //terminate string + // Calculate the length before disabling the interrupts. + uint8_t len = strlen(cmdbuffer+bufindw+CMDHDRSIZE) + (1 + CMDHDRSIZE); + +// SERIAL_ECHOPGM("SD cmd("); +// MYSERIAL.print(sd_count.value, DEC); +// SERIAL_ECHOPGM(") "); +// SERIAL_ECHOLN(cmdbuffer+bufindw+CMDHDRSIZE); +// SERIAL_ECHOPGM("cmdbuffer:"); +// MYSERIAL.print(cmdbuffer); +// SERIAL_ECHOPGM("buflen:"); +// MYSERIAL.print(buflen+1); + sd_count.value = 0; + + cli(); + // This block locks the interrupts globally for 3.56 us, + // which corresponds to a maximum repeat frequency of 280.70 kHz. + // This blocking is safe in the context of a 10kHz stepper driver interrupt + // or a 115200 Bd serial line receive interrupt, which will not trigger faster than 12kHz. + ++ buflen; + bufindw += len; + sdpos_atomic = card.get_sdpos()+1; + if (bufindw == sizeof(cmdbuffer)) + bufindw = 0; + sei(); + + comment_mode = false; //for new command + serial_count = 0; //clear buffer + // The following line will reserve buffer space if available. + if (! cmdqueue_could_enqueue_back(MAX_CMD_SIZE-1, true)) + return; + } + else + { + if(serial_char == ';') comment_mode = true; + else if(!comment_mode) cmdbuffer[bufindw+CMDHDRSIZE+serial_count++] = serial_char; + } + } + + #endif //SDSUPPORT +} + +uint16_t cmdqueue_calc_sd_length() +{ + if (buflen == 0) + return 0; + union { + struct { + char lo; + char hi; + } lohi; + uint16_t value; + } sdlen_single; + uint16_t sdlen = 0; + for (size_t _buflen = buflen, _bufindr = bufindr;;) { + if (cmdbuffer[_bufindr] == CMDBUFFER_CURRENT_TYPE_SDCARD) { + sdlen_single.lohi.lo = cmdbuffer[_bufindr + 1]; + sdlen_single.lohi.hi = cmdbuffer[_bufindr + 2]; + sdlen += sdlen_single.value; + } + if (-- _buflen == 0) + break; + // First skip the current command ID and iterate up to the end of the string. + for (_bufindr += CMDHDRSIZE; cmdbuffer[_bufindr] != 0; ++ _bufindr) ; + // Second, skip the end of string null character and iterate until a nonzero command ID is found. + for (++ _bufindr; _bufindr < sizeof(cmdbuffer) && cmdbuffer[_bufindr] == 0; ++ _bufindr) ; + // If the end of the buffer was empty, + if (_bufindr == sizeof(cmdbuffer)) { + // skip to the start and find the nonzero command. + for (_bufindr = 0; cmdbuffer[_bufindr] == 0; ++ _bufindr) ; + } + } + return sdlen; +} diff --git a/Firmware/cmdqueue.h b/Firmware/cmdqueue.h new file mode 100644 index 0000000..13185f1 --- /dev/null +++ b/Firmware/cmdqueue.h @@ -0,0 +1,94 @@ +#ifndef CMDQUEUE_H +#define CMDQUEUE_H + +#include "Marlin.h" +#include "language.h" + + +// String circular buffer. Commands may be pushed to the buffer from both sides: +// Chained commands will be pushed to the front, interactive (from LCD menu) +// and printing commands (from serial line or from SD card) are pushed to the tail. +// First character of each entry indicates the type of the entry: +#define CMDBUFFER_CURRENT_TYPE_UNKNOWN 0 +// Command in cmdbuffer was sent over USB. +#define CMDBUFFER_CURRENT_TYPE_USB 1 +// Command in cmdbuffer was read from SDCARD. +#define CMDBUFFER_CURRENT_TYPE_SDCARD 2 +// Command in cmdbuffer was generated by the UI. +#define CMDBUFFER_CURRENT_TYPE_UI 3 +// Command in cmdbuffer was generated by another G-code. +#define CMDBUFFER_CURRENT_TYPE_CHAINED 4 +// Command has been processed and its SD card length has been possibly pushed +// to the planner queue, but not yet removed from the cmdqueue. +// This is a temporary state to reduce stepper interrupt locking time. +#define CMDBUFFER_CURRENT_TYPE_TO_BE_REMOVED 5 +//Command in cmdbuffer was sent over USB and contains line number +#define CMDBUFFER_CURRENT_TYPE_USB_WITH_LINENR 6 + +// How much space to reserve for the chained commands +// of type CMDBUFFER_CURRENT_TYPE_CHAINED, +// which are pushed to the front of the queue? +// Maximum 5 commands of max length 20 + null terminator. +#define CMDBUFFER_RESERVE_FRONT (5*21) + +extern char cmdbuffer[BUFSIZE * (MAX_CMD_SIZE + 1) + CMDBUFFER_RESERVE_FRONT]; +extern size_t bufindr; +extern int buflen; +extern bool cmdbuffer_front_already_processed; + +// Type of a command, which is to be executed right now. +#define CMDBUFFER_CURRENT_TYPE (cmdbuffer[bufindr]) +// String of a command, which is to be executed right now. +#define CMDBUFFER_CURRENT_STRING (cmdbuffer+bufindr+CMDHDRSIZE) + +// Enable debugging of the command buffer. +// Debugging information will be sent to serial line. +//#define CMDBUFFER_DEBUG + +extern uint32_t sdpos_atomic; + +extern int serial_count; +extern boolean comment_mode; +extern char *strchr_pointer; + +extern unsigned long TimeSent; +extern unsigned long TimeNow; + +extern long gcode_N; +extern long gcode_LastN; +extern long Stopped_gcode_LastN; + +extern bool cmdqueue_pop_front(); +extern void cmdqueue_reset(); +#ifdef CMDBUFFER_DEBUG +extern void cmdqueue_dump_to_serial_single_line(int nr, const char *p); +extern void cmdqueue_dump_to_serial(); +#endif /* CMDBUFFER_DEBUG */ +extern bool cmd_buffer_empty(); +extern void enquecommand(const char *cmd, bool from_progmem); +extern void enquecommand_front(const char *cmd, bool from_progmem); +extern void repeatcommand_front(); +extern bool is_buffer_empty(); +extern void get_command(); +extern uint16_t cmdqueue_calc_sd_length(); + +// Return True if a character was found +static inline bool code_seen(char code) { return (strchr_pointer = strchr(CMDBUFFER_CURRENT_STRING, code)) != NULL; } +static inline bool code_seen(const char *code) { return (strchr_pointer = strstr(CMDBUFFER_CURRENT_STRING, code)) != NULL; } +static inline float code_value() { return strtod(strchr_pointer+1, NULL);} +static inline long code_value_long() { return strtol(strchr_pointer+1, NULL, 10); } +static inline int16_t code_value_short() { return int16_t(strtol(strchr_pointer+1, NULL, 10)); }; +static inline uint8_t code_value_uint8() { return uint8_t(strtol(strchr_pointer+1, NULL, 10)); }; + +static inline float code_value_float() +{ + char* e = strchr(strchr_pointer, 'E'); + if (!e) return strtod(strchr_pointer + 1, NULL); + *e = 0; + float ret = strtod(strchr_pointer + 1, NULL); + *e = 'E'; + return ret; +} + + +#endif //CMDQUEUE_H diff --git a/Firmware/config.h b/Firmware/config.h new file mode 100644 index 0000000..0473c3f --- /dev/null +++ b/Firmware/config.h @@ -0,0 +1,55 @@ +#ifndef _CONFIG_H +#define _CONFIG_H + + +//ADC configuration +#define ADC_CHAN_MSK 0b0000001001011111 //used AD channels bit mask (0,1,2,3,4,6,9) +#define ADC_CHAN_CNT 7 //number of used channels) +#define ADC_OVRSAMPL 16 //oversampling multiplier +#define ADC_CALLBACK adc_ready //callback function () + +//SWI2C configuration +#define SWI2C +//#define SWI2C_SDA 20 //SDA on P3 +//#define SWI2C_SCL 21 //SCL on P3 +#define SWI2C_A8 +#define SWI2C_DEL 20 //2us clock delay +#define SWI2C_TMO 2048 //2048 cycles timeout + +//PAT9125 configuration +#define PAT9125_SWI2C +#define PAT9125_I2C_ADDR 0x75 //ID=LO +//#define PAT9125_I2C_ADDR 0x79 //ID=HI +//#define PAT9125_I2C_ADDR 0x73 //ID=NC +#define PAT9125_XRES 0 +#define PAT9125_YRES 240 + +//SM4 configuration +#define SM4_DEFDELAY 500 //default step delay [us] + +//TMC2130 - Trinamic stepper driver +//pinout - hardcoded +//spi: +#define TMC2130_SPI_RATE 0 // fosc/4 = 4MHz +#define TMC2130_SPCR SPI_SPCR(TMC2130_SPI_RATE, 1, 1, 1, 0) +#define TMC2130_SPSR SPI_SPSR(TMC2130_SPI_RATE) + +//W25X20CL configuration +//pinout: +#define W25X20CL_PIN_CS 32 +//spi: +#define W25X20CL_SPI_RATE 0 // fosc/4 = 4MHz +#define W25X20CL_SPCR SPI_SPCR(W25X20CL_SPI_RATE, 1, 1, 1, 0) +#define W25X20CL_SPSR SPI_SPSR(W25X20CL_SPI_RATE) + +#include "boards.h" +#include "Configuration_prusa.h" + +//LANG - Multi-language support +//#define LANG_MODE 0 // primary language only +#define LANG_MODE 1 // sec. language support + +#define LANG_SIZE_RESERVED 0x3000 // reserved space for secondary language (12288 bytes) + + +#endif //_CONFIG_H diff --git a/Firmware/conv2str.cpp b/Firmware/conv2str.cpp new file mode 100644 index 0000000..10aa711 --- /dev/null +++ b/Firmware/conv2str.cpp @@ -0,0 +1,292 @@ +//conv2str.cpp - Float conversion utilities + +#include "conv2str.h" +#include + + +// convert float to string with +123.4 format +char conv[8]; + +char *ftostr3(const float &x) +{ + return itostr3((int)x); +} + +char *itostr2(const uint8_t &x) +{ + //sprintf(conv,"%5.1f",x); + int xx = x; + conv[0] = (xx / 10) % 10 + '0'; + conv[1] = (xx) % 10 + '0'; + conv[2] = 0; + return conv; +} + +// Convert float to string with 123.4 format, dropping sign +char *ftostr31(const float &x) +{ + int xx = x * 10; + conv[0] = (xx >= 0) ? '+' : '-'; + xx = abs(xx); + conv[1] = (xx / 1000) % 10 + '0'; + conv[2] = (xx / 100) % 10 + '0'; + conv[3] = (xx / 10) % 10 + '0'; + conv[4] = '.'; + conv[5] = (xx) % 10 + '0'; + conv[6] = 0; + return conv; +} + +// Convert float to string with 123.4 format +char *ftostr31ns(const float &x) +{ + int xx = x * 10; + //conv[0]=(xx>=0)?'+':'-'; + xx = abs(xx); + conv[0] = (xx / 1000) % 10 + '0'; + conv[1] = (xx / 100) % 10 + '0'; + conv[2] = (xx / 10) % 10 + '0'; + conv[3] = '.'; + conv[4] = (xx) % 10 + '0'; + conv[5] = 0; + return conv; +} + +char *ftostr32(const float &x) +{ + long xx = x * 100; + if (xx >= 0) + conv[0] = (xx / 10000) % 10 + '0'; + else + conv[0] = '-'; + xx = abs(xx); + conv[1] = (xx / 1000) % 10 + '0'; + conv[2] = (xx / 100) % 10 + '0'; + conv[3] = '.'; + conv[4] = (xx / 10) % 10 + '0'; + conv[5] = (xx) % 10 + '0'; + conv[6] = 0; + return conv; +} + +//// Convert float to rj string with 123.45 format +char *ftostr32ns(const float &x) { + long xx = abs(x); + conv[0] = xx >= 10000 ? (xx / 10000) % 10 + '0' : ' '; + conv[1] = xx >= 1000 ? (xx / 1000) % 10 + '0' : ' '; + conv[2] = xx >= 100 ? (xx / 100) % 10 + '0' : '0'; + conv[3] = '.'; + conv[4] = (xx / 10) % 10 + '0'; + conv[5] = xx % 10 + '0'; + return conv; +} + + +// Convert float to string with 1.234 format +char *ftostr43(const float &x, uint8_t offset) +{ + const size_t maxOffset = sizeof(conv)/sizeof(conv[0]) - 6; + if (offset>maxOffset) offset = maxOffset; + long xx = x * 1000; + if (xx >= 0) + conv[offset] = (xx / 1000) % 10 + '0'; + else + conv[offset] = '-'; + xx = abs(xx); + conv[offset + 1] = '.'; + conv[offset + 2] = (xx / 100) % 10 + '0'; + conv[offset + 3] = (xx / 10) % 10 + '0'; + conv[offset + 4] = (xx) % 10 + '0'; + conv[offset + 5] = 0; + return conv; +} + +//Float to string with 1.23 format +char *ftostr12ns(const float &x) +{ + long xx = x * 100; + + xx = abs(xx); + conv[0] = (xx / 100) % 10 + '0'; + conv[1] = '.'; + conv[2] = (xx / 10) % 10 + '0'; + conv[3] = (xx) % 10 + '0'; + conv[4] = 0; + return conv; +} + +//Float to string with 1.234 format +char *ftostr13ns(const float &x) +{ + long xx = x * 1000; + if (xx >= 0) + conv[0] = ' '; + else + conv[0] = '-'; + xx = abs(xx); + conv[1] = (xx / 1000) % 10 + '0'; + conv[2] = '.'; + conv[3] = (xx / 100) % 10 + '0'; + conv[4] = (xx / 10) % 10 + '0'; + conv[5] = (xx) % 10 + '0'; + conv[6] = 0; + return conv; +} + +// convert float to space-padded string with -_23.4_ format +char *ftostr32sp(const float &x) { + long xx = abs(x * 100); + uint8_t dig; + + if (x < 0) { // negative val = -_0 + conv[0] = '-'; + dig = (xx / 1000) % 10; + conv[1] = dig ? '0' + dig : ' '; + } + else { // positive val = __0 + dig = (xx / 10000) % 10; + if (dig) { + conv[0] = '0' + dig; + conv[1] = '0' + (xx / 1000) % 10; + } + else { + conv[0] = ' '; + dig = (xx / 1000) % 10; + conv[1] = dig ? '0' + dig : ' '; + } + } + + conv[2] = '0' + (xx / 100) % 10; // lsd always + + dig = xx % 10; + if (dig) { // 2 decimal places + conv[5] = '0' + dig; + conv[4] = '0' + (xx / 10) % 10; + conv[3] = '.'; + } + else { // 1 or 0 decimal place + dig = (xx / 10) % 10; + if (dig) { + conv[4] = '0' + dig; + conv[3] = '.'; + } + else { + conv[3] = conv[4] = ' '; + } + conv[5] = ' '; + } + conv[6] = '\0'; + return conv; +} + +char *itostr31(const int &xx) +{ + conv[0] = (xx >= 0) ? '+' : '-'; + conv[1] = (xx / 1000) % 10 + '0'; + conv[2] = (xx / 100) % 10 + '0'; + conv[3] = (xx / 10) % 10 + '0'; + conv[4] = '.'; + conv[5] = (xx) % 10 + '0'; + conv[6] = 0; + return conv; +} + +// Convert int to rj string with 123 or -12 format +char *itostr3(const int &x) +{ + int xx = x; + if (xx < 0) { + conv[0] = '-'; + xx = -xx; + } else if (xx >= 100) + conv[0] = (xx / 100) % 10 + '0'; + else + conv[0] = ' '; + if (xx >= 10) + conv[1] = (xx / 10) % 10 + '0'; + else + conv[1] = ' '; + conv[2] = (xx) % 10 + '0'; + conv[3] = 0; + return conv; +} + +// Convert int to lj string with 123 format +char *itostr3left(const int &xx) +{ + if (xx >= 100) + { + conv[0] = (xx / 100) % 10 + '0'; + conv[1] = (xx / 10) % 10 + '0'; + conv[2] = (xx) % 10 + '0'; + conv[3] = 0; + } + else if (xx >= 10) + { + conv[0] = (xx / 10) % 10 + '0'; + conv[1] = (xx) % 10 + '0'; + conv[2] = 0; + } + else + { + conv[0] = (xx) % 10 + '0'; + conv[1] = 0; + } + return conv; +} + +// Convert int to rj string with 1234 format +char *itostr4(const int &xx) { + conv[0] = xx >= 1000 ? (xx / 1000) % 10 + '0' : ' '; + conv[1] = xx >= 100 ? (xx / 100) % 10 + '0' : ' '; + conv[2] = xx >= 10 ? (xx / 10) % 10 + '0' : ' '; + conv[3] = xx % 10 + '0'; + conv[4] = 0; + return conv; +} + +// Convert float to rj string with 12345 format +char *ftostr5(const float &x) { + long xx = abs(x); + conv[0] = xx >= 10000 ? (xx / 10000) % 10 + '0' : ' '; + conv[1] = xx >= 1000 ? (xx / 1000) % 10 + '0' : ' '; + conv[2] = xx >= 100 ? (xx / 100) % 10 + '0' : ' '; + conv[3] = xx >= 10 ? (xx / 10) % 10 + '0' : ' '; + conv[4] = xx % 10 + '0'; + conv[5] = 0; + return conv; +} + +// Convert float to string with +1234.5 format +char *ftostr51(const float &x) +{ + long xx = x * 10; + conv[0] = (xx >= 0) ? '+' : '-'; + xx = abs(xx); + conv[1] = (xx / 10000) % 10 + '0'; + conv[2] = (xx / 1000) % 10 + '0'; + conv[3] = (xx / 100) % 10 + '0'; + conv[4] = (xx / 10) % 10 + '0'; + conv[5] = '.'; + conv[6] = (xx) % 10 + '0'; + conv[7] = 0; + return conv; +} + +// Convert float to string with +123.45 format +char *ftostr52(const float &x) +{ + long xx = x * 100; + conv[0] = (xx >= 0) ? '+' : '-'; + xx = abs(xx); + conv[1] = (xx / 10000) % 10 + '0'; + conv[2] = (xx / 1000) % 10 + '0'; + conv[3] = (xx / 100) % 10 + '0'; + conv[4] = '.'; + conv[5] = (xx / 10) % 10 + '0'; + conv[6] = (xx) % 10 + '0'; + conv[7] = 0; + return conv; +} + + diff --git a/Firmware/conv2str.h b/Firmware/conv2str.h new file mode 100644 index 0000000..c6cf176 --- /dev/null +++ b/Firmware/conv2str.h @@ -0,0 +1,28 @@ +//conv2str.h - Float conversion utilities +#ifndef _CONV2STR_H +#define _CONV2STR_H + +#include + + +char *itostr2(const uint8_t &x); +char *itostr31(const int &xx); +char *itostr3(const int &xx); +char *itostr3left(const int &xx); +char *itostr4(const int &xx); + +char *ftostr3(const float &x); +char *ftostr31ns(const float &x); // float to string without sign character +char *ftostr31(const float &x); +char *ftostr32(const float &x); +char *ftostr32ns(const float &x); +char *ftostr43(const float &x, uint8_t offset = 0); +char *ftostr12ns(const float &x); +char *ftostr13ns(const float &x); +char *ftostr32sp(const float &x); // remove zero-padding from ftostr32 +char *ftostr5(const float &x); +char *ftostr51(const float &x); +char *ftostr52(const float &x); + + +#endif //_CONV2STR_H diff --git a/Firmware/doxyfile b/Firmware/doxyfile new file mode 100644 index 0000000..346ce60 --- /dev/null +++ b/Firmware/doxyfile @@ -0,0 +1,2494 @@ +# Doxyfile 1.8.13 + +# This file describes the settings to be used by the documentation system +# doxygen (www.doxygen.org) for a project. +# +# All text after a double hash (##) is considered a comment and is placed in +# front of the TAG it is preceding. +# +# All text after a single hash (#) is considered a comment and will be ignored. +# The format is: +# TAG = value [value, ...] +# For lists, items can also be appended using: +# TAG += value [value, ...] +# Values that contain spaces should be placed between quotes (\" \"). + +#--------------------------------------------------------------------------- +# Project related configuration options +#--------------------------------------------------------------------------- + +# This tag specifies the encoding used for all characters in the config file +# that follow. The default is UTF-8 which is also the encoding used for all text +# before the first occurrence of this tag. Doxygen uses libiconv (or the iconv +# built into libc) for the transcoding. See http://www.gnu.org/software/libiconv +# for the list of possible encodings. +# The default value is: UTF-8. + +DOXYFILE_ENCODING = UTF-8 + +# The PROJECT_NAME tag is a single word (or a sequence of words surrounded by +# double-quotes, unless you are using Doxywizard) that should identify the +# project for which the documentation is generated. This name is used in the +# title of most generated pages and in a few other places. +# The default value is: My Project. + +PROJECT_NAME = "Prusa3d Marlin fork" + +# The PROJECT_NUMBER tag can be used to enter a project or revision number. This +# could be handy for archiving the generated documentation or if some version +# control system is used. + +PROJECT_NUMBER = + +# Using the PROJECT_BRIEF tag one can provide an optional one line description +# for a project that appears at the top of each page and should give viewer a +# quick idea about the purpose of the project. Keep the description short. + +PROJECT_BRIEF = + +# With the PROJECT_LOGO tag one can specify a logo or an icon that is included +# in the documentation. The maximum height of the logo should not exceed 55 +# pixels and the maximum width should not exceed 200 pixels. Doxygen will copy +# the logo to the output directory. + +PROJECT_LOGO = + +# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) path +# into which the generated documentation will be written. If a relative path is +# entered, it will be relative to the location where doxygen was started. If +# left blank the current directory will be used. + +OUTPUT_DIRECTORY = Doc + +# If the CREATE_SUBDIRS tag is set to YES then doxygen will create 4096 sub- +# directories (in 2 levels) under the output directory of each output format and +# will distribute the generated files over these directories. Enabling this +# option can be useful when feeding doxygen a huge amount of source files, where +# putting all generated files in the same directory would otherwise causes +# performance problems for the file system. +# The default value is: NO. + +CREATE_SUBDIRS = NO + +# If the ALLOW_UNICODE_NAMES tag is set to YES, doxygen will allow non-ASCII +# characters to appear in the names of generated files. If set to NO, non-ASCII +# characters will be escaped, for example _xE3_x81_x84 will be used for Unicode +# U+3044. +# The default value is: NO. + +ALLOW_UNICODE_NAMES = NO + +# The OUTPUT_LANGUAGE tag is used to specify the language in which all +# documentation generated by doxygen is written. Doxygen will use this +# information to generate all constant output in the proper language. +# Possible values are: Afrikaans, Arabic, Armenian, Brazilian, Catalan, Chinese, +# Chinese-Traditional, Croatian, Czech, Danish, Dutch, English (United States), +# Esperanto, Farsi (Persian), Finnish, French, German, Greek, Hungarian, +# Indonesian, Italian, Japanese, Japanese-en (Japanese with English messages), +# Korean, Korean-en (Korean with English messages), Latvian, Lithuanian, +# Macedonian, Norwegian, Persian (Farsi), Polish, Portuguese, Romanian, Russian, +# Serbian, Serbian-Cyrillic, Slovak, Slovene, Spanish, Swedish, Turkish, +# Ukrainian and Vietnamese. +# The default value is: English. + +OUTPUT_LANGUAGE = English + +# If the BRIEF_MEMBER_DESC tag is set to YES, doxygen will include brief member +# descriptions after the members that are listed in the file and class +# documentation (similar to Javadoc). Set to NO to disable this. +# The default value is: YES. + +BRIEF_MEMBER_DESC = YES + +# If the REPEAT_BRIEF tag is set to YES, doxygen will prepend the brief +# description of a member or function before the detailed description +# +# Note: If both HIDE_UNDOC_MEMBERS and BRIEF_MEMBER_DESC are set to NO, the +# brief descriptions will be completely suppressed. +# The default value is: YES. + +REPEAT_BRIEF = NO + +# This tag implements a quasi-intelligent brief description abbreviator that is +# used to form the text in various listings. Each string in this list, if found +# as the leading text of the brief description, will be stripped from the text +# and the result, after processing the whole list, is used as the annotated +# text. Otherwise, the brief description is used as-is. If left blank, the +# following values are used ($name is automatically replaced with the name of +# the entity):The $name class, The $name widget, The $name file, is, provides, +# specifies, contains, represents, a, an and the. + +ABBREVIATE_BRIEF = "The $name class" \ + "The $name widget" \ + "The $name file" \ + is \ + provides \ + specifies \ + contains \ + represents \ + a \ + an \ + the + +# If the ALWAYS_DETAILED_SEC and REPEAT_BRIEF tags are both set to YES then +# doxygen will generate a detailed section even if there is only a brief +# description. +# The default value is: NO. + +ALWAYS_DETAILED_SEC = NO + +# If the INLINE_INHERITED_MEMB tag is set to YES, doxygen will show all +# inherited members of a class in the documentation of that class as if those +# members were ordinary class members. Constructors, destructors and assignment +# operators of the base classes will not be shown. +# The default value is: NO. + +INLINE_INHERITED_MEMB = NO + +# If the FULL_PATH_NAMES tag is set to YES, doxygen will prepend the full path +# before files name in the file list and in the header files. If set to NO the +# shortest path that makes the file name unique will be used +# The default value is: YES. + +FULL_PATH_NAMES = YES + +# The STRIP_FROM_PATH tag can be used to strip a user-defined part of the path. +# Stripping is only done if one of the specified strings matches the left-hand +# part of the path. The tag can be used to show relative paths in the file list. +# If left blank the directory from which doxygen is run is used as the path to +# strip. +# +# Note that you can specify absolute paths here, but also relative paths, which +# will be relative from the directory where doxygen is started. +# This tag requires that the tag FULL_PATH_NAMES is set to YES. + +STRIP_FROM_PATH = + +# The STRIP_FROM_INC_PATH tag can be used to strip a user-defined part of the +# path mentioned in the documentation of a class, which tells the reader which +# header file to include in order to use a class. If left blank only the name of +# the header file containing the class definition is used. Otherwise one should +# specify the list of include paths that are normally passed to the compiler +# using the -I flag. + +STRIP_FROM_INC_PATH = + +# If the SHORT_NAMES tag is set to YES, doxygen will generate much shorter (but +# less readable) file names. This can be useful is your file systems doesn't +# support long names like on DOS, Mac, or CD-ROM. +# The default value is: NO. + +SHORT_NAMES = NO + +# If the JAVADOC_AUTOBRIEF tag is set to YES then doxygen will interpret the +# first line (until the first dot) of a Javadoc-style comment as the brief +# description. If set to NO, the Javadoc-style will behave just like regular Qt- +# style comments (thus requiring an explicit @brief command for a brief +# description.) +# The default value is: NO. + +JAVADOC_AUTOBRIEF = NO + +# If the QT_AUTOBRIEF tag is set to YES then doxygen will interpret the first +# line (until the first dot) of a Qt-style comment as the brief description. If +# set to NO, the Qt-style will behave just like regular Qt-style comments (thus +# requiring an explicit \brief command for a brief description.) +# The default value is: NO. + +QT_AUTOBRIEF = NO + +# The MULTILINE_CPP_IS_BRIEF tag can be set to YES to make doxygen treat a +# multi-line C++ special comment block (i.e. a block of //! or /// comments) as +# a brief description. This used to be the default behavior. The new default is +# to treat a multi-line C++ comment block as a detailed description. Set this +# tag to YES if you prefer the old behavior instead. +# +# Note that setting this tag to YES also means that rational rose comments are +# not recognized any more. +# The default value is: NO. + +MULTILINE_CPP_IS_BRIEF = NO + +# If the INHERIT_DOCS tag is set to YES then an undocumented member inherits the +# documentation from any documented member that it re-implements. +# The default value is: YES. + +INHERIT_DOCS = YES + +# If the SEPARATE_MEMBER_PAGES tag is set to YES then doxygen will produce a new +# page for each member. If set to NO, the documentation of a member will be part +# of the file/class/namespace that contains it. +# The default value is: NO. + +SEPARATE_MEMBER_PAGES = NO + +# The TAB_SIZE tag can be used to set the number of spaces in a tab. Doxygen +# uses this value to replace tabs by spaces in code fragments. +# Minimum value: 1, maximum value: 16, default value: 4. + +TAB_SIZE = 4 + +# This tag can be used to specify a number of aliases that act as commands in +# the documentation. An alias has the form: +# name=value +# For example adding +# "sideeffect=@par Side Effects:\n" +# will allow you to put the command \sideeffect (or @sideeffect) in the +# documentation, which will result in a user-defined paragraph with heading +# "Side Effects:". You can put \n's in the value part of an alias to insert +# newlines. + +ALIASES = + +# This tag can be used to specify a number of word-keyword mappings (TCL only). +# A mapping has the form "name=value". For example adding "class=itcl::class" +# will allow you to use the command class in the itcl::class meaning. + +TCL_SUBST = + +# Set the OPTIMIZE_OUTPUT_FOR_C tag to YES if your project consists of C sources +# only. Doxygen will then generate output that is more tailored for C. For +# instance, some of the names that are used will be different. The list of all +# members will be omitted, etc. +# The default value is: NO. + +OPTIMIZE_OUTPUT_FOR_C = NO + +# Set the OPTIMIZE_OUTPUT_JAVA tag to YES if your project consists of Java or +# Python sources only. Doxygen will then generate output that is more tailored +# for that language. For instance, namespaces will be presented as packages, +# qualified scopes will look different, etc. +# The default value is: NO. + +OPTIMIZE_OUTPUT_JAVA = NO + +# Set the OPTIMIZE_FOR_FORTRAN tag to YES if your project consists of Fortran +# sources. Doxygen will then generate output that is tailored for Fortran. +# The default value is: NO. + +OPTIMIZE_FOR_FORTRAN = NO + +# Set the OPTIMIZE_OUTPUT_VHDL tag to YES if your project consists of VHDL +# sources. Doxygen will then generate output that is tailored for VHDL. +# The default value is: NO. + +OPTIMIZE_OUTPUT_VHDL = NO + +# Doxygen selects the parser to use depending on the extension of the files it +# parses. With this tag you can assign which parser to use for a given +# extension. Doxygen has a built-in mapping, but you can override or extend it +# using this tag. The format is ext=language, where ext is a file extension, and +# language is one of the parsers supported by doxygen: IDL, Java, Javascript, +# C#, C, C++, D, PHP, Objective-C, Python, Fortran (fixed format Fortran: +# FortranFixed, free formatted Fortran: FortranFree, unknown formatted Fortran: +# Fortran. In the later case the parser tries to guess whether the code is fixed +# or free formatted code, this is the default for Fortran type files), VHDL. For +# instance to make doxygen treat .inc files as Fortran files (default is PHP), +# and .f files as C (default is Fortran), use: inc=Fortran f=C. +# +# Note: For files without extension you can use no_extension as a placeholder. +# +# Note that for custom extensions you also need to set FILE_PATTERNS otherwise +# the files are not read by doxygen. + +EXTENSION_MAPPING = + +# If the MARKDOWN_SUPPORT tag is enabled then doxygen pre-processes all comments +# according to the Markdown format, which allows for more readable +# documentation. See http://daringfireball.net/projects/markdown/ for details. +# The output of markdown processing is further processed by doxygen, so you can +# mix doxygen, HTML, and XML commands with Markdown formatting. Disable only in +# case of backward compatibilities issues. +# The default value is: YES. + +MARKDOWN_SUPPORT = YES + +# When the TOC_INCLUDE_HEADINGS tag is set to a non-zero value, all headings up +# to that level are automatically included in the table of contents, even if +# they do not have an id attribute. +# Note: This feature currently applies only to Markdown headings. +# Minimum value: 0, maximum value: 99, default value: 0. +# This tag requires that the tag MARKDOWN_SUPPORT is set to YES. + +TOC_INCLUDE_HEADINGS = 0 + +# When enabled doxygen tries to link words that correspond to documented +# classes, or namespaces to their corresponding documentation. Such a link can +# be prevented in individual cases by putting a % sign in front of the word or +# globally by setting AUTOLINK_SUPPORT to NO. +# The default value is: YES. + +AUTOLINK_SUPPORT = YES + +# If you use STL classes (i.e. std::string, std::vector, etc.) but do not want +# to include (a tag file for) the STL sources as input, then you should set this +# tag to YES in order to let doxygen match functions declarations and +# definitions whose arguments contain STL classes (e.g. func(std::string); +# versus func(std::string) {}). This also make the inheritance and collaboration +# diagrams that involve STL classes more complete and accurate. +# The default value is: NO. + +BUILTIN_STL_SUPPORT = NO + +# If you use Microsoft's C++/CLI language, you should set this option to YES to +# enable parsing support. +# The default value is: NO. + +CPP_CLI_SUPPORT = NO + +# Set the SIP_SUPPORT tag to YES if your project consists of sip (see: +# http://www.riverbankcomputing.co.uk/software/sip/intro) sources only. Doxygen +# will parse them like normal C++ but will assume all classes use public instead +# of private inheritance when no explicit protection keyword is present. +# The default value is: NO. + +SIP_SUPPORT = NO + +# For Microsoft's IDL there are propget and propput attributes to indicate +# getter and setter methods for a property. Setting this option to YES will make +# doxygen to replace the get and set methods by a property in the documentation. +# This will only work if the methods are indeed getting or setting a simple +# type. If this is not the case, or you want to show the methods anyway, you +# should set this option to NO. +# The default value is: YES. + +IDL_PROPERTY_SUPPORT = YES + +# If member grouping is used in the documentation and the DISTRIBUTE_GROUP_DOC +# tag is set to YES then doxygen will reuse the documentation of the first +# member in the group (if any) for the other members of the group. By default +# all members of a group must be documented explicitly. +# The default value is: NO. + +DISTRIBUTE_GROUP_DOC = NO + +# If one adds a struct or class to a group and this option is enabled, then also +# any nested class or struct is added to the same group. By default this option +# is disabled and one has to add nested compounds explicitly via \ingroup. +# The default value is: NO. + +GROUP_NESTED_COMPOUNDS = NO + +# Set the SUBGROUPING tag to YES to allow class member groups of the same type +# (for instance a group of public functions) to be put as a subgroup of that +# type (e.g. under the Public Functions section). Set it to NO to prevent +# subgrouping. Alternatively, this can be done per class using the +# \nosubgrouping command. +# The default value is: YES. + +SUBGROUPING = YES + +# When the INLINE_GROUPED_CLASSES tag is set to YES, classes, structs and unions +# are shown inside the group in which they are included (e.g. using \ingroup) +# instead of on a separate page (for HTML and Man pages) or section (for LaTeX +# and RTF). +# +# Note that this feature does not work in combination with +# SEPARATE_MEMBER_PAGES. +# The default value is: NO. + +INLINE_GROUPED_CLASSES = NO + +# When the INLINE_SIMPLE_STRUCTS tag is set to YES, structs, classes, and unions +# with only public data fields or simple typedef fields will be shown inline in +# the documentation of the scope in which they are defined (i.e. file, +# namespace, or group documentation), provided this scope is documented. If set +# to NO, structs, classes, and unions are shown on a separate page (for HTML and +# Man pages) or section (for LaTeX and RTF). +# The default value is: NO. + +INLINE_SIMPLE_STRUCTS = NO + +# When TYPEDEF_HIDES_STRUCT tag is enabled, a typedef of a struct, union, or +# enum is documented as struct, union, or enum with the name of the typedef. So +# typedef struct TypeS {} TypeT, will appear in the documentation as a struct +# with name TypeT. When disabled the typedef will appear as a member of a file, +# namespace, or class. And the struct will be named TypeS. This can typically be +# useful for C code in case the coding convention dictates that all compound +# types are typedef'ed and only the typedef is referenced, never the tag name. +# The default value is: NO. + +TYPEDEF_HIDES_STRUCT = NO + +# The size of the symbol lookup cache can be set using LOOKUP_CACHE_SIZE. This +# cache is used to resolve symbols given their name and scope. Since this can be +# an expensive process and often the same symbol appears multiple times in the +# code, doxygen keeps a cache of pre-resolved symbols. If the cache is too small +# doxygen will become slower. If the cache is too large, memory is wasted. The +# cache size is given by this formula: 2^(16+LOOKUP_CACHE_SIZE). The valid range +# is 0..9, the default is 0, corresponding to a cache size of 2^16=65536 +# symbols. At the end of a run doxygen will report the cache usage and suggest +# the optimal cache size from a speed point of view. +# Minimum value: 0, maximum value: 9, default value: 0. + +LOOKUP_CACHE_SIZE = 0 + +#--------------------------------------------------------------------------- +# Build related configuration options +#--------------------------------------------------------------------------- + +# If the EXTRACT_ALL tag is set to YES, doxygen will assume all entities in +# documentation are documented, even if no documentation was available. Private +# class members and static file members will be hidden unless the +# EXTRACT_PRIVATE respectively EXTRACT_STATIC tags are set to YES. +# Note: This will also disable the warnings about undocumented members that are +# normally produced when WARNINGS is set to YES. +# The default value is: NO. + +EXTRACT_ALL = NO + +# If the EXTRACT_PRIVATE tag is set to YES, all private members of a class will +# be included in the documentation. +# The default value is: NO. + +EXTRACT_PRIVATE = NO + +# If the EXTRACT_PACKAGE tag is set to YES, all members with package or internal +# scope will be included in the documentation. +# The default value is: NO. + +EXTRACT_PACKAGE = NO + +# If the EXTRACT_STATIC tag is set to YES, all static members of a file will be +# included in the documentation. +# The default value is: NO. + +EXTRACT_STATIC = YES + +# If the EXTRACT_LOCAL_CLASSES tag is set to YES, classes (and structs) defined +# locally in source files will be included in the documentation. If set to NO, +# only classes defined in header files are included. Does not have any effect +# for Java sources. +# The default value is: YES. + +EXTRACT_LOCAL_CLASSES = YES + +# This flag is only useful for Objective-C code. If set to YES, local methods, +# which are defined in the implementation section but not in the interface are +# included in the documentation. If set to NO, only methods in the interface are +# included. +# The default value is: NO. + +EXTRACT_LOCAL_METHODS = NO + +# If this flag is set to YES, the members of anonymous namespaces will be +# extracted and appear in the documentation as a namespace called +# 'anonymous_namespace{file}', where file will be replaced with the base name of +# the file that contains the anonymous namespace. By default anonymous namespace +# are hidden. +# The default value is: NO. + +EXTRACT_ANON_NSPACES = NO + +# If the HIDE_UNDOC_MEMBERS tag is set to YES, doxygen will hide all +# undocumented members inside documented classes or files. If set to NO these +# members will be included in the various overviews, but no documentation +# section is generated. This option has no effect if EXTRACT_ALL is enabled. +# The default value is: NO. + +HIDE_UNDOC_MEMBERS = NO + +# If the HIDE_UNDOC_CLASSES tag is set to YES, doxygen will hide all +# undocumented classes that are normally visible in the class hierarchy. If set +# to NO, these classes will be included in the various overviews. This option +# has no effect if EXTRACT_ALL is enabled. +# The default value is: NO. + +HIDE_UNDOC_CLASSES = NO + +# If the HIDE_FRIEND_COMPOUNDS tag is set to YES, doxygen will hide all friend +# (class|struct|union) declarations. If set to NO, these declarations will be +# included in the documentation. +# The default value is: NO. + +HIDE_FRIEND_COMPOUNDS = NO + +# If the HIDE_IN_BODY_DOCS tag is set to YES, doxygen will hide any +# documentation blocks found inside the body of a function. If set to NO, these +# blocks will be appended to the function's detailed documentation block. +# The default value is: NO. + +HIDE_IN_BODY_DOCS = NO + +# The INTERNAL_DOCS tag determines if documentation that is typed after a +# \internal command is included. If the tag is set to NO then the documentation +# will be excluded. Set it to YES to include the internal documentation. +# The default value is: NO. + +INTERNAL_DOCS = NO + +# If the CASE_SENSE_NAMES tag is set to NO then doxygen will only generate file +# names in lower-case letters. If set to YES, upper-case letters are also +# allowed. This is useful if you have classes or files whose names only differ +# in case and if your file system supports case sensitive file names. Windows +# and Mac users are advised to set this option to NO. +# The default value is: system dependent. + +CASE_SENSE_NAMES = YES + +# If the HIDE_SCOPE_NAMES tag is set to NO then doxygen will show members with +# their full class and namespace scopes in the documentation. If set to YES, the +# scope will be hidden. +# The default value is: NO. + +HIDE_SCOPE_NAMES = NO + +# If the HIDE_COMPOUND_REFERENCE tag is set to NO (default) then doxygen will +# append additional text to a page's title, such as Class Reference. If set to +# YES the compound reference will be hidden. +# The default value is: NO. + +HIDE_COMPOUND_REFERENCE= NO + +# If the SHOW_INCLUDE_FILES tag is set to YES then doxygen will put a list of +# the files that are included by a file in the documentation of that file. +# The default value is: YES. + +SHOW_INCLUDE_FILES = YES + +# If the SHOW_GROUPED_MEMB_INC tag is set to YES then Doxygen will add for each +# grouped member an include statement to the documentation, telling the reader +# which file to include in order to use the member. +# The default value is: NO. + +SHOW_GROUPED_MEMB_INC = NO + +# If the FORCE_LOCAL_INCLUDES tag is set to YES then doxygen will list include +# files with double quotes in the documentation rather than with sharp brackets. +# The default value is: NO. + +FORCE_LOCAL_INCLUDES = NO + +# If the INLINE_INFO tag is set to YES then a tag [inline] is inserted in the +# documentation for inline members. +# The default value is: YES. + +INLINE_INFO = YES + +# If the SORT_MEMBER_DOCS tag is set to YES then doxygen will sort the +# (detailed) documentation of file and class members alphabetically by member +# name. If set to NO, the members will appear in declaration order. +# The default value is: YES. + +SORT_MEMBER_DOCS = YES + +# If the SORT_BRIEF_DOCS tag is set to YES then doxygen will sort the brief +# descriptions of file, namespace and class members alphabetically by member +# name. If set to NO, the members will appear in declaration order. Note that +# this will also influence the order of the classes in the class list. +# The default value is: NO. + +SORT_BRIEF_DOCS = NO + +# If the SORT_MEMBERS_CTORS_1ST tag is set to YES then doxygen will sort the +# (brief and detailed) documentation of class members so that constructors and +# destructors are listed first. If set to NO the constructors will appear in the +# respective orders defined by SORT_BRIEF_DOCS and SORT_MEMBER_DOCS. +# Note: If SORT_BRIEF_DOCS is set to NO this option is ignored for sorting brief +# member documentation. +# Note: If SORT_MEMBER_DOCS is set to NO this option is ignored for sorting +# detailed member documentation. +# The default value is: NO. + +SORT_MEMBERS_CTORS_1ST = NO + +# If the SORT_GROUP_NAMES tag is set to YES then doxygen will sort the hierarchy +# of group names into alphabetical order. If set to NO the group names will +# appear in their defined order. +# The default value is: NO. + +SORT_GROUP_NAMES = NO + +# If the SORT_BY_SCOPE_NAME tag is set to YES, the class list will be sorted by +# fully-qualified names, including namespaces. If set to NO, the class list will +# be sorted only by class name, not including the namespace part. +# Note: This option is not very useful if HIDE_SCOPE_NAMES is set to YES. +# Note: This option applies only to the class list, not to the alphabetical +# list. +# The default value is: NO. + +SORT_BY_SCOPE_NAME = NO + +# If the STRICT_PROTO_MATCHING option is enabled and doxygen fails to do proper +# type resolution of all parameters of a function it will reject a match between +# the prototype and the implementation of a member function even if there is +# only one candidate or it is obvious which candidate to choose by doing a +# simple string match. By disabling STRICT_PROTO_MATCHING doxygen will still +# accept a match between prototype and implementation in such cases. +# The default value is: NO. + +STRICT_PROTO_MATCHING = NO + +# The GENERATE_TODOLIST tag can be used to enable (YES) or disable (NO) the todo +# list. This list is created by putting \todo commands in the documentation. +# The default value is: YES. + +GENERATE_TODOLIST = YES + +# The GENERATE_TESTLIST tag can be used to enable (YES) or disable (NO) the test +# list. This list is created by putting \test commands in the documentation. +# The default value is: YES. + +GENERATE_TESTLIST = YES + +# The GENERATE_BUGLIST tag can be used to enable (YES) or disable (NO) the bug +# list. This list is created by putting \bug commands in the documentation. +# The default value is: YES. + +GENERATE_BUGLIST = YES + +# The GENERATE_DEPRECATEDLIST tag can be used to enable (YES) or disable (NO) +# the deprecated list. This list is created by putting \deprecated commands in +# the documentation. +# The default value is: YES. + +GENERATE_DEPRECATEDLIST= YES + +# The ENABLED_SECTIONS tag can be used to enable conditional documentation +# sections, marked by \if ... \endif and \cond +# ... \endcond blocks. + +ENABLED_SECTIONS = + +# The MAX_INITIALIZER_LINES tag determines the maximum number of lines that the +# initial value of a variable or macro / define can have for it to appear in the +# documentation. If the initializer consists of more lines than specified here +# it will be hidden. Use a value of 0 to hide initializers completely. The +# appearance of the value of individual variables and macros / defines can be +# controlled using \showinitializer or \hideinitializer command in the +# documentation regardless of this setting. +# Minimum value: 0, maximum value: 10000, default value: 30. + +MAX_INITIALIZER_LINES = 30 + +# Set the SHOW_USED_FILES tag to NO to disable the list of files generated at +# the bottom of the documentation of classes and structs. If set to YES, the +# list will mention the files that were used to generate the documentation. +# The default value is: YES. + +SHOW_USED_FILES = YES + +# Set the SHOW_FILES tag to NO to disable the generation of the Files page. This +# will remove the Files entry from the Quick Index and from the Folder Tree View +# (if specified). +# The default value is: YES. + +SHOW_FILES = YES + +# Set the SHOW_NAMESPACES tag to NO to disable the generation of the Namespaces +# page. This will remove the Namespaces entry from the Quick Index and from the +# Folder Tree View (if specified). +# The default value is: YES. + +SHOW_NAMESPACES = YES + +# The FILE_VERSION_FILTER tag can be used to specify a program or script that +# doxygen should invoke to get the current version for each file (typically from +# the version control system). Doxygen will invoke the program by executing (via +# popen()) the command command input-file, where command is the value of the +# FILE_VERSION_FILTER tag, and input-file is the name of an input file provided +# by doxygen. Whatever the program writes to standard output is used as the file +# version. For an example see the documentation. + +FILE_VERSION_FILTER = + +# The LAYOUT_FILE tag can be used to specify a layout file which will be parsed +# by doxygen. The layout file controls the global structure of the generated +# output files in an output format independent way. To create the layout file +# that represents doxygen's defaults, run doxygen with the -l option. You can +# optionally specify a file name after the option, if omitted DoxygenLayout.xml +# will be used as the name of the layout file. +# +# Note that if you run doxygen from a directory containing a file called +# DoxygenLayout.xml, doxygen will parse it automatically even if the LAYOUT_FILE +# tag is left empty. + +LAYOUT_FILE = + +# The CITE_BIB_FILES tag can be used to specify one or more bib files containing +# the reference definitions. This must be a list of .bib files. The .bib +# extension is automatically appended if omitted. This requires the bibtex tool +# to be installed. See also http://en.wikipedia.org/wiki/BibTeX for more info. +# For LaTeX the style of the bibliography can be controlled using +# LATEX_BIB_STYLE. To use this feature you need bibtex and perl available in the +# search path. See also \cite for info how to create references. + +CITE_BIB_FILES = + +#--------------------------------------------------------------------------- +# Configuration options related to warning and progress messages +#--------------------------------------------------------------------------- + +# The QUIET tag can be used to turn on/off the messages that are generated to +# standard output by doxygen. If QUIET is set to YES this implies that the +# messages are off. +# The default value is: NO. + +QUIET = NO + +# The WARNINGS tag can be used to turn on/off the warning messages that are +# generated to standard error (stderr) by doxygen. If WARNINGS is set to YES +# this implies that the warnings are on. +# +# Tip: Turn warnings on while writing the documentation. +# The default value is: YES. + +WARNINGS = YES + +# If the WARN_IF_UNDOCUMENTED tag is set to YES then doxygen will generate +# warnings for undocumented members. If EXTRACT_ALL is set to YES then this flag +# will automatically be disabled. +# The default value is: YES. + +WARN_IF_UNDOCUMENTED = YES + +# If the WARN_IF_DOC_ERROR tag is set to YES, doxygen will generate warnings for +# potential errors in the documentation, such as not documenting some parameters +# in a documented function, or documenting parameters that don't exist or using +# markup commands wrongly. +# The default value is: YES. + +WARN_IF_DOC_ERROR = YES + +# This WARN_NO_PARAMDOC option can be enabled to get warnings for functions that +# are documented, but have no documentation for their parameters or return +# value. If set to NO, doxygen will only warn about wrong or incomplete +# parameter documentation, but not about the absence of documentation. +# The default value is: NO. + +WARN_NO_PARAMDOC = NO + +# If the WARN_AS_ERROR tag is set to YES then doxygen will immediately stop when +# a warning is encountered. +# The default value is: NO. + +WARN_AS_ERROR = NO + +# The WARN_FORMAT tag determines the format of the warning messages that doxygen +# can produce. The string should contain the $file, $line, and $text tags, which +# will be replaced by the file and line number from which the warning originated +# and the warning text. Optionally the format may contain $version, which will +# be replaced by the version of the file (if it could be obtained via +# FILE_VERSION_FILTER) +# The default value is: $file:$line: $text. + +WARN_FORMAT = "$file:$line: $text" + +# The WARN_LOGFILE tag can be used to specify a file to which warning and error +# messages should be written. If left blank the output is written to standard +# error (stderr). + +WARN_LOGFILE = + +#--------------------------------------------------------------------------- +# Configuration options related to the input files +#--------------------------------------------------------------------------- + +# The INPUT tag is used to specify the files and/or directories that contain +# documented source files. You may enter file names like myfile.cpp or +# directories like /usr/src/myproject. Separate the files or directories with +# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING +# Note: If this tag is empty the current directory is searched. + +INPUT = + +# This tag can be used to specify the character encoding of the source files +# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses +# libiconv (or the iconv built into libc) for the transcoding. See the libiconv +# documentation (see: http://www.gnu.org/software/libiconv) for the list of +# possible encodings. +# The default value is: UTF-8. + +INPUT_ENCODING = UTF-8 + +# If the value of the INPUT tag contains directories, you can use the +# FILE_PATTERNS tag to specify one or more wildcard patterns (like *.cpp and +# *.h) to filter out the source-files in the directories. +# +# Note that for custom extensions or not directly supported extensions you also +# need to set EXTENSION_MAPPING for the extension otherwise the files are not +# read by doxygen. +# +# If left blank the following patterns are tested:*.c, *.cc, *.cxx, *.cpp, +# *.c++, *.java, *.ii, *.ixx, *.ipp, *.i++, *.inl, *.idl, *.ddl, *.odl, *.h, +# *.hh, *.hxx, *.hpp, *.h++, *.cs, *.d, *.php, *.php4, *.php5, *.phtml, *.inc, +# *.m, *.markdown, *.md, *.mm, *.dox, *.py, *.pyw, *.f90, *.f95, *.f03, *.f08, +# *.f, *.for, *.tcl, *.vhd, *.vhdl, *.ucf and *.qsf. + +FILE_PATTERNS = *.c \ + *.cc \ + *.cxx \ + *.cpp \ + *.c++ \ + *.java \ + *.ii \ + *.ixx \ + *.ipp \ + *.i++ \ + *.inl \ + *.idl \ + *.ddl \ + *.odl \ + *.h \ + *.hh \ + *.hxx \ + *.hpp \ + *.h++ \ + *.cs \ + *.d \ + *.php \ + *.php4 \ + *.php5 \ + *.phtml \ + *.inc \ + *.m \ + *.markdown \ + *.md \ + *.mm \ + *.dox \ + *.py \ + *.pyw \ + *.f90 \ + *.f95 \ + *.f03 \ + *.f08 \ + *.f \ + *.for \ + *.tcl \ + *.vhd \ + *.vhdl \ + *.ucf \ + *.qsf + +# The RECURSIVE tag can be used to specify whether or not subdirectories should +# be searched for input files as well. +# The default value is: NO. + +RECURSIVE = NO + +# The EXCLUDE tag can be used to specify files and/or directories that should be +# excluded from the INPUT source files. This way you can easily exclude a +# subdirectory from a directory tree whose root is specified with the INPUT tag. +# +# Note that relative paths are relative to the directory from which doxygen is +# run. + +EXCLUDE = + +# The EXCLUDE_SYMLINKS tag can be used to select whether or not files or +# directories that are symbolic links (a Unix file system feature) are excluded +# from the input. +# The default value is: NO. + +EXCLUDE_SYMLINKS = NO + +# If the value of the INPUT tag contains directories, you can use the +# EXCLUDE_PATTERNS tag to specify one or more wildcard patterns to exclude +# certain files from those directories. +# +# Note that the wildcards are matched against the file with absolute path, so to +# exclude all test directories for example use the pattern */test/* + +EXCLUDE_PATTERNS = + +# The EXCLUDE_SYMBOLS tag can be used to specify one or more symbol names +# (namespaces, classes, functions, etc.) that should be excluded from the +# output. The symbol name can be a fully qualified name, a word, or if the +# wildcard * is used, a substring. Examples: ANamespace, AClass, +# AClass::ANamespace, ANamespace::*Test +# +# Note that the wildcards are matched against the file with absolute path, so to +# exclude all test directories use the pattern */test/* + +EXCLUDE_SYMBOLS = + +# The EXAMPLE_PATH tag can be used to specify one or more files or directories +# that contain example code fragments that are included (see the \include +# command). + +EXAMPLE_PATH = + +# If the value of the EXAMPLE_PATH tag contains directories, you can use the +# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp and +# *.h) to filter out the source-files in the directories. If left blank all +# files are included. + +EXAMPLE_PATTERNS = * + +# If the EXAMPLE_RECURSIVE tag is set to YES then subdirectories will be +# searched for input files to be used with the \include or \dontinclude commands +# irrespective of the value of the RECURSIVE tag. +# The default value is: NO. + +EXAMPLE_RECURSIVE = NO + +# The IMAGE_PATH tag can be used to specify one or more files or directories +# that contain images that are to be included in the documentation (see the +# \image command). + +IMAGE_PATH = + +# The INPUT_FILTER tag can be used to specify a program that doxygen should +# invoke to filter for each input file. Doxygen will invoke the filter program +# by executing (via popen()) the command: +# +# +# +# where is the value of the INPUT_FILTER tag, and is the +# name of an input file. Doxygen will then use the output that the filter +# program writes to standard output. If FILTER_PATTERNS is specified, this tag +# will be ignored. +# +# Note that the filter must not add or remove lines; it is applied before the +# code is scanned, but not when the output code is generated. If lines are added +# or removed, the anchors will not be placed correctly. +# +# Note that for custom extensions or not directly supported extensions you also +# need to set EXTENSION_MAPPING for the extension otherwise the files are not +# properly processed by doxygen. + +INPUT_FILTER = + +# The FILTER_PATTERNS tag can be used to specify filters on a per file pattern +# basis. Doxygen will compare the file name with each pattern and apply the +# filter if there is a match. The filters are a list of the form: pattern=filter +# (like *.cpp=my_cpp_filter). See INPUT_FILTER for further information on how +# filters are used. If the FILTER_PATTERNS tag is empty or if none of the +# patterns match the file name, INPUT_FILTER is applied. +# +# Note that for custom extensions or not directly supported extensions you also +# need to set EXTENSION_MAPPING for the extension otherwise the files are not +# properly processed by doxygen. + +FILTER_PATTERNS = + +# If the FILTER_SOURCE_FILES tag is set to YES, the input filter (if set using +# INPUT_FILTER) will also be used to filter the input files that are used for +# producing the source files to browse (i.e. when SOURCE_BROWSER is set to YES). +# The default value is: NO. + +FILTER_SOURCE_FILES = NO + +# The FILTER_SOURCE_PATTERNS tag can be used to specify source filters per file +# pattern. A pattern will override the setting for FILTER_PATTERN (if any) and +# it is also possible to disable source filtering for a specific pattern using +# *.ext= (so without naming a filter). +# This tag requires that the tag FILTER_SOURCE_FILES is set to YES. + +FILTER_SOURCE_PATTERNS = + +# If the USE_MDFILE_AS_MAINPAGE tag refers to the name of a markdown file that +# is part of the input, its contents will be placed on the main page +# (index.html). This can be useful if you have a project on for instance GitHub +# and want to reuse the introduction page also for the doxygen output. + +USE_MDFILE_AS_MAINPAGE = + +#--------------------------------------------------------------------------- +# Configuration options related to source browsing +#--------------------------------------------------------------------------- + +# If the SOURCE_BROWSER tag is set to YES then a list of source files will be +# generated. Documented entities will be cross-referenced with these sources. +# +# Note: To get rid of all source code in the generated output, make sure that +# also VERBATIM_HEADERS is set to NO. +# The default value is: NO. + +SOURCE_BROWSER = NO + +# Setting the INLINE_SOURCES tag to YES will include the body of functions, +# classes and enums directly into the documentation. +# The default value is: NO. + +INLINE_SOURCES = NO + +# Setting the STRIP_CODE_COMMENTS tag to YES will instruct doxygen to hide any +# special comment blocks from generated source code fragments. Normal C, C++ and +# Fortran comments will always remain visible. +# The default value is: YES. + +STRIP_CODE_COMMENTS = YES + +# If the REFERENCED_BY_RELATION tag is set to YES then for each documented +# function all documented functions referencing it will be listed. +# The default value is: NO. + +REFERENCED_BY_RELATION = NO + +# If the REFERENCES_RELATION tag is set to YES then for each documented function +# all documented entities called/used by that function will be listed. +# The default value is: NO. + +REFERENCES_RELATION = NO + +# If the REFERENCES_LINK_SOURCE tag is set to YES and SOURCE_BROWSER tag is set +# to YES then the hyperlinks from functions in REFERENCES_RELATION and +# REFERENCED_BY_RELATION lists will link to the source code. Otherwise they will +# link to the documentation. +# The default value is: YES. + +REFERENCES_LINK_SOURCE = YES + +# If SOURCE_TOOLTIPS is enabled (the default) then hovering a hyperlink in the +# source code will show a tooltip with additional information such as prototype, +# brief description and links to the definition and documentation. Since this +# will make the HTML file larger and loading of large files a bit slower, you +# can opt to disable this feature. +# The default value is: YES. +# This tag requires that the tag SOURCE_BROWSER is set to YES. + +SOURCE_TOOLTIPS = YES + +# If the USE_HTAGS tag is set to YES then the references to source code will +# point to the HTML generated by the htags(1) tool instead of doxygen built-in +# source browser. The htags tool is part of GNU's global source tagging system +# (see http://www.gnu.org/software/global/global.html). You will need version +# 4.8.6 or higher. +# +# To use it do the following: +# - Install the latest version of global +# - Enable SOURCE_BROWSER and USE_HTAGS in the config file +# - Make sure the INPUT points to the root of the source tree +# - Run doxygen as normal +# +# Doxygen will invoke htags (and that will in turn invoke gtags), so these +# tools must be available from the command line (i.e. in the search path). +# +# The result: instead of the source browser generated by doxygen, the links to +# source code will now point to the output of htags. +# The default value is: NO. +# This tag requires that the tag SOURCE_BROWSER is set to YES. + +USE_HTAGS = NO + +# If the VERBATIM_HEADERS tag is set the YES then doxygen will generate a +# verbatim copy of the header file for each class for which an include is +# specified. Set to NO to disable this. +# See also: Section \class. +# The default value is: YES. + +VERBATIM_HEADERS = YES + +# If the CLANG_ASSISTED_PARSING tag is set to YES then doxygen will use the +# clang parser (see: http://clang.llvm.org/) for more accurate parsing at the +# cost of reduced performance. This can be particularly helpful with template +# rich C++ code for which doxygen's built-in parser lacks the necessary type +# information. +# Note: The availability of this option depends on whether or not doxygen was +# generated with the -Duse-libclang=ON option for CMake. +# The default value is: NO. + +CLANG_ASSISTED_PARSING = NO + +# If clang assisted parsing is enabled you can provide the compiler with command +# line options that you would normally use when invoking the compiler. Note that +# the include paths will already be set by doxygen for the files and directories +# specified with INPUT and INCLUDE_PATH. +# This tag requires that the tag CLANG_ASSISTED_PARSING is set to YES. + +CLANG_OPTIONS = + +#--------------------------------------------------------------------------- +# Configuration options related to the alphabetical class index +#--------------------------------------------------------------------------- + +# If the ALPHABETICAL_INDEX tag is set to YES, an alphabetical index of all +# compounds will be generated. Enable this if the project contains a lot of +# classes, structs, unions or interfaces. +# The default value is: YES. + +ALPHABETICAL_INDEX = YES + +# The COLS_IN_ALPHA_INDEX tag can be used to specify the number of columns in +# which the alphabetical index list will be split. +# Minimum value: 1, maximum value: 20, default value: 5. +# This tag requires that the tag ALPHABETICAL_INDEX is set to YES. + +COLS_IN_ALPHA_INDEX = 5 + +# In case all classes in a project start with a common prefix, all classes will +# be put under the same header in the alphabetical index. The IGNORE_PREFIX tag +# can be used to specify a prefix (or a list of prefixes) that should be ignored +# while generating the index headers. +# This tag requires that the tag ALPHABETICAL_INDEX is set to YES. + +IGNORE_PREFIX = + +#--------------------------------------------------------------------------- +# Configuration options related to the HTML output +#--------------------------------------------------------------------------- + +# If the GENERATE_HTML tag is set to YES, doxygen will generate HTML output +# The default value is: YES. + +GENERATE_HTML = YES + +# The HTML_OUTPUT tag is used to specify where the HTML docs will be put. If a +# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of +# it. +# The default directory is: html. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_OUTPUT = html + +# The HTML_FILE_EXTENSION tag can be used to specify the file extension for each +# generated HTML page (for example: .htm, .php, .asp). +# The default value is: .html. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_FILE_EXTENSION = .html + +# The HTML_HEADER tag can be used to specify a user-defined HTML header file for +# each generated HTML page. If the tag is left blank doxygen will generate a +# standard header. +# +# To get valid HTML the header file that includes any scripts and style sheets +# that doxygen needs, which is dependent on the configuration options used (e.g. +# the setting GENERATE_TREEVIEW). It is highly recommended to start with a +# default header using +# doxygen -w html new_header.html new_footer.html new_stylesheet.css +# YourConfigFile +# and then modify the file new_header.html. See also section "Doxygen usage" +# for information on how to generate the default header that doxygen normally +# uses. +# Note: The header is subject to change so you typically have to regenerate the +# default header when upgrading to a newer version of doxygen. For a description +# of the possible markers and block names see the documentation. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_HEADER = + +# The HTML_FOOTER tag can be used to specify a user-defined HTML footer for each +# generated HTML page. If the tag is left blank doxygen will generate a standard +# footer. See HTML_HEADER for more information on how to generate a default +# footer and what special commands can be used inside the footer. See also +# section "Doxygen usage" for information on how to generate the default footer +# that doxygen normally uses. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_FOOTER = + +# The HTML_STYLESHEET tag can be used to specify a user-defined cascading style +# sheet that is used by each HTML page. It can be used to fine-tune the look of +# the HTML output. If left blank doxygen will generate a default style sheet. +# See also section "Doxygen usage" for information on how to generate the style +# sheet that doxygen normally uses. +# Note: It is recommended to use HTML_EXTRA_STYLESHEET instead of this tag, as +# it is more robust and this tag (HTML_STYLESHEET) will in the future become +# obsolete. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_STYLESHEET = + +# The HTML_EXTRA_STYLESHEET tag can be used to specify additional user-defined +# cascading style sheets that are included after the standard style sheets +# created by doxygen. Using this option one can overrule certain style aspects. +# This is preferred over using HTML_STYLESHEET since it does not replace the +# standard style sheet and is therefore more robust against future updates. +# Doxygen will copy the style sheet files to the output directory. +# Note: The order of the extra style sheet files is of importance (e.g. the last +# style sheet in the list overrules the setting of the previous ones in the +# list). For an example see the documentation. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_EXTRA_STYLESHEET = + +# The HTML_EXTRA_FILES tag can be used to specify one or more extra images or +# other source files which should be copied to the HTML output directory. Note +# that these files will be copied to the base HTML output directory. Use the +# $relpath^ marker in the HTML_HEADER and/or HTML_FOOTER files to load these +# files. In the HTML_STYLESHEET file, use the file name only. Also note that the +# files will be copied as-is; there are no commands or markers available. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_EXTRA_FILES = + +# The HTML_COLORSTYLE_HUE tag controls the color of the HTML output. Doxygen +# will adjust the colors in the style sheet and background images according to +# this color. Hue is specified as an angle on a colorwheel, see +# http://en.wikipedia.org/wiki/Hue for more information. For instance the value +# 0 represents red, 60 is yellow, 120 is green, 180 is cyan, 240 is blue, 300 +# purple, and 360 is red again. +# Minimum value: 0, maximum value: 359, default value: 220. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_COLORSTYLE_HUE = 220 + +# The HTML_COLORSTYLE_SAT tag controls the purity (or saturation) of the colors +# in the HTML output. For a value of 0 the output will use grayscales only. A +# value of 255 will produce the most vivid colors. +# Minimum value: 0, maximum value: 255, default value: 100. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_COLORSTYLE_SAT = 100 + +# The HTML_COLORSTYLE_GAMMA tag controls the gamma correction applied to the +# luminance component of the colors in the HTML output. Values below 100 +# gradually make the output lighter, whereas values above 100 make the output +# darker. The value divided by 100 is the actual gamma applied, so 80 represents +# a gamma of 0.8, The value 220 represents a gamma of 2.2, and 100 does not +# change the gamma. +# Minimum value: 40, maximum value: 240, default value: 80. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_COLORSTYLE_GAMMA = 80 + +# If the HTML_TIMESTAMP tag is set to YES then the footer of each generated HTML +# page will contain the date and time when the page was generated. Setting this +# to YES can help to show when doxygen was last run and thus if the +# documentation is up to date. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_TIMESTAMP = NO + +# If the HTML_DYNAMIC_SECTIONS tag is set to YES then the generated HTML +# documentation will contain sections that can be hidden and shown after the +# page has loaded. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_DYNAMIC_SECTIONS = NO + +# With HTML_INDEX_NUM_ENTRIES one can control the preferred number of entries +# shown in the various tree structured indices initially; the user can expand +# and collapse entries dynamically later on. Doxygen will expand the tree to +# such a level that at most the specified number of entries are visible (unless +# a fully collapsed tree already exceeds this amount). So setting the number of +# entries 1 will produce a full collapsed tree by default. 0 is a special value +# representing an infinite number of entries and will result in a full expanded +# tree by default. +# Minimum value: 0, maximum value: 9999, default value: 100. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_INDEX_NUM_ENTRIES = 100 + +# If the GENERATE_DOCSET tag is set to YES, additional index files will be +# generated that can be used as input for Apple's Xcode 3 integrated development +# environment (see: http://developer.apple.com/tools/xcode/), introduced with +# OSX 10.5 (Leopard). To create a documentation set, doxygen will generate a +# Makefile in the HTML output directory. Running make will produce the docset in +# that directory and running make install will install the docset in +# ~/Library/Developer/Shared/Documentation/DocSets so that Xcode will find it at +# startup. See http://developer.apple.com/tools/creatingdocsetswithdoxygen.html +# for more information. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +GENERATE_DOCSET = NO + +# This tag determines the name of the docset feed. A documentation feed provides +# an umbrella under which multiple documentation sets from a single provider +# (such as a company or product suite) can be grouped. +# The default value is: Doxygen generated docs. +# This tag requires that the tag GENERATE_DOCSET is set to YES. + +DOCSET_FEEDNAME = "Doxygen generated docs" + +# This tag specifies a string that should uniquely identify the documentation +# set bundle. This should be a reverse domain-name style string, e.g. +# com.mycompany.MyDocSet. Doxygen will append .docset to the name. +# The default value is: org.doxygen.Project. +# This tag requires that the tag GENERATE_DOCSET is set to YES. + +DOCSET_BUNDLE_ID = org.doxygen.Project + +# The DOCSET_PUBLISHER_ID tag specifies a string that should uniquely identify +# the documentation publisher. This should be a reverse domain-name style +# string, e.g. com.mycompany.MyDocSet.documentation. +# The default value is: org.doxygen.Publisher. +# This tag requires that the tag GENERATE_DOCSET is set to YES. + +DOCSET_PUBLISHER_ID = org.doxygen.Publisher + +# The DOCSET_PUBLISHER_NAME tag identifies the documentation publisher. +# The default value is: Publisher. +# This tag requires that the tag GENERATE_DOCSET is set to YES. + +DOCSET_PUBLISHER_NAME = Publisher + +# If the GENERATE_HTMLHELP tag is set to YES then doxygen generates three +# additional HTML index files: index.hhp, index.hhc, and index.hhk. The +# index.hhp is a project file that can be read by Microsoft's HTML Help Workshop +# (see: http://www.microsoft.com/en-us/download/details.aspx?id=21138) on +# Windows. +# +# The HTML Help Workshop contains a compiler that can convert all HTML output +# generated by doxygen into a single compiled HTML file (.chm). Compiled HTML +# files are now used as the Windows 98 help format, and will replace the old +# Windows help format (.hlp) on all Windows platforms in the future. Compressed +# HTML files also contain an index, a table of contents, and you can search for +# words in the documentation. The HTML workshop also contains a viewer for +# compressed HTML files. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +GENERATE_HTMLHELP = NO + +# The CHM_FILE tag can be used to specify the file name of the resulting .chm +# file. You can add a path in front of the file if the result should not be +# written to the html output directory. +# This tag requires that the tag GENERATE_HTMLHELP is set to YES. + +CHM_FILE = + +# The HHC_LOCATION tag can be used to specify the location (absolute path +# including file name) of the HTML help compiler (hhc.exe). If non-empty, +# doxygen will try to run the HTML help compiler on the generated index.hhp. +# The file has to be specified with full path. +# This tag requires that the tag GENERATE_HTMLHELP is set to YES. + +HHC_LOCATION = + +# The GENERATE_CHI flag controls if a separate .chi index file is generated +# (YES) or that it should be included in the master .chm file (NO). +# The default value is: NO. +# This tag requires that the tag GENERATE_HTMLHELP is set to YES. + +GENERATE_CHI = NO + +# The CHM_INDEX_ENCODING is used to encode HtmlHelp index (hhk), content (hhc) +# and project file content. +# This tag requires that the tag GENERATE_HTMLHELP is set to YES. + +CHM_INDEX_ENCODING = + +# The BINARY_TOC flag controls whether a binary table of contents is generated +# (YES) or a normal table of contents (NO) in the .chm file. Furthermore it +# enables the Previous and Next buttons. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTMLHELP is set to YES. + +BINARY_TOC = NO + +# The TOC_EXPAND flag can be set to YES to add extra items for group members to +# the table of contents of the HTML help documentation and to the tree view. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTMLHELP is set to YES. + +TOC_EXPAND = NO + +# If the GENERATE_QHP tag is set to YES and both QHP_NAMESPACE and +# QHP_VIRTUAL_FOLDER are set, an additional index file will be generated that +# can be used as input for Qt's qhelpgenerator to generate a Qt Compressed Help +# (.qch) of the generated HTML documentation. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +GENERATE_QHP = NO + +# If the QHG_LOCATION tag is specified, the QCH_FILE tag can be used to specify +# the file name of the resulting .qch file. The path specified is relative to +# the HTML output folder. +# This tag requires that the tag GENERATE_QHP is set to YES. + +QCH_FILE = + +# The QHP_NAMESPACE tag specifies the namespace to use when generating Qt Help +# Project output. For more information please see Qt Help Project / Namespace +# (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#namespace). +# The default value is: org.doxygen.Project. +# This tag requires that the tag GENERATE_QHP is set to YES. + +QHP_NAMESPACE = org.doxygen.Project + +# The QHP_VIRTUAL_FOLDER tag specifies the namespace to use when generating Qt +# Help Project output. For more information please see Qt Help Project / Virtual +# Folders (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#virtual- +# folders). +# The default value is: doc. +# This tag requires that the tag GENERATE_QHP is set to YES. + +QHP_VIRTUAL_FOLDER = doc + +# If the QHP_CUST_FILTER_NAME tag is set, it specifies the name of a custom +# filter to add. For more information please see Qt Help Project / Custom +# Filters (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#custom- +# filters). +# This tag requires that the tag GENERATE_QHP is set to YES. + +QHP_CUST_FILTER_NAME = + +# The QHP_CUST_FILTER_ATTRS tag specifies the list of the attributes of the +# custom filter to add. For more information please see Qt Help Project / Custom +# Filters (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#custom- +# filters). +# This tag requires that the tag GENERATE_QHP is set to YES. + +QHP_CUST_FILTER_ATTRS = + +# The QHP_SECT_FILTER_ATTRS tag specifies the list of the attributes this +# project's filter section matches. Qt Help Project / Filter Attributes (see: +# http://qt-project.org/doc/qt-4.8/qthelpproject.html#filter-attributes). +# This tag requires that the tag GENERATE_QHP is set to YES. + +QHP_SECT_FILTER_ATTRS = + +# The QHG_LOCATION tag can be used to specify the location of Qt's +# qhelpgenerator. If non-empty doxygen will try to run qhelpgenerator on the +# generated .qhp file. +# This tag requires that the tag GENERATE_QHP is set to YES. + +QHG_LOCATION = + +# If the GENERATE_ECLIPSEHELP tag is set to YES, additional index files will be +# generated, together with the HTML files, they form an Eclipse help plugin. To +# install this plugin and make it available under the help contents menu in +# Eclipse, the contents of the directory containing the HTML and XML files needs +# to be copied into the plugins directory of eclipse. The name of the directory +# within the plugins directory should be the same as the ECLIPSE_DOC_ID value. +# After copying Eclipse needs to be restarted before the help appears. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +GENERATE_ECLIPSEHELP = NO + +# A unique identifier for the Eclipse help plugin. When installing the plugin +# the directory name containing the HTML and XML files should also have this +# name. Each documentation set should have its own identifier. +# The default value is: org.doxygen.Project. +# This tag requires that the tag GENERATE_ECLIPSEHELP is set to YES. + +ECLIPSE_DOC_ID = org.doxygen.Project + +# If you want full control over the layout of the generated HTML pages it might +# be necessary to disable the index and replace it with your own. The +# DISABLE_INDEX tag can be used to turn on/off the condensed index (tabs) at top +# of each HTML page. A value of NO enables the index and the value YES disables +# it. Since the tabs in the index contain the same information as the navigation +# tree, you can set this option to YES if you also set GENERATE_TREEVIEW to YES. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +DISABLE_INDEX = NO + +# The GENERATE_TREEVIEW tag is used to specify whether a tree-like index +# structure should be generated to display hierarchical information. If the tag +# value is set to YES, a side panel will be generated containing a tree-like +# index structure (just like the one that is generated for HTML Help). For this +# to work a browser that supports JavaScript, DHTML, CSS and frames is required +# (i.e. any modern browser). Windows users are probably better off using the +# HTML help feature. Via custom style sheets (see HTML_EXTRA_STYLESHEET) one can +# further fine-tune the look of the index. As an example, the default style +# sheet generated by doxygen has an example that shows how to put an image at +# the root of the tree instead of the PROJECT_NAME. Since the tree basically has +# the same information as the tab index, you could consider setting +# DISABLE_INDEX to YES when enabling this option. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +GENERATE_TREEVIEW = NO + +# The ENUM_VALUES_PER_LINE tag can be used to set the number of enum values that +# doxygen will group on one line in the generated HTML documentation. +# +# Note that a value of 0 will completely suppress the enum values from appearing +# in the overview section. +# Minimum value: 0, maximum value: 20, default value: 4. +# This tag requires that the tag GENERATE_HTML is set to YES. + +ENUM_VALUES_PER_LINE = 4 + +# If the treeview is enabled (see GENERATE_TREEVIEW) then this tag can be used +# to set the initial width (in pixels) of the frame in which the tree is shown. +# Minimum value: 0, maximum value: 1500, default value: 250. +# This tag requires that the tag GENERATE_HTML is set to YES. + +TREEVIEW_WIDTH = 250 + +# If the EXT_LINKS_IN_WINDOW option is set to YES, doxygen will open links to +# external symbols imported via tag files in a separate window. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +EXT_LINKS_IN_WINDOW = NO + +# Use this tag to change the font size of LaTeX formulas included as images in +# the HTML documentation. When you change the font size after a successful +# doxygen run you need to manually remove any form_*.png images from the HTML +# output directory to force them to be regenerated. +# Minimum value: 8, maximum value: 50, default value: 10. +# This tag requires that the tag GENERATE_HTML is set to YES. + +FORMULA_FONTSIZE = 10 + +# Use the FORMULA_TRANPARENT tag to determine whether or not the images +# generated for formulas are transparent PNGs. Transparent PNGs are not +# supported properly for IE 6.0, but are supported on all modern browsers. +# +# Note that when changing this option you need to delete any form_*.png files in +# the HTML output directory before the changes have effect. +# The default value is: YES. +# This tag requires that the tag GENERATE_HTML is set to YES. + +FORMULA_TRANSPARENT = YES + +# Enable the USE_MATHJAX option to render LaTeX formulas using MathJax (see +# http://www.mathjax.org) which uses client side Javascript for the rendering +# instead of using pre-rendered bitmaps. Use this if you do not have LaTeX +# installed or if you want to formulas look prettier in the HTML output. When +# enabled you may also need to install MathJax separately and configure the path +# to it using the MATHJAX_RELPATH option. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +USE_MATHJAX = NO + +# When MathJax is enabled you can set the default output format to be used for +# the MathJax output. See the MathJax site (see: +# http://docs.mathjax.org/en/latest/output.html) for more details. +# Possible values are: HTML-CSS (which is slower, but has the best +# compatibility), NativeMML (i.e. MathML) and SVG. +# The default value is: HTML-CSS. +# This tag requires that the tag USE_MATHJAX is set to YES. + +MATHJAX_FORMAT = HTML-CSS + +# When MathJax is enabled you need to specify the location relative to the HTML +# output directory using the MATHJAX_RELPATH option. The destination directory +# should contain the MathJax.js script. For instance, if the mathjax directory +# is located at the same level as the HTML output directory, then +# MATHJAX_RELPATH should be ../mathjax. The default value points to the MathJax +# Content Delivery Network so you can quickly see the result without installing +# MathJax. However, it is strongly recommended to install a local copy of +# MathJax from http://www.mathjax.org before deployment. +# The default value is: http://cdn.mathjax.org/mathjax/latest. +# This tag requires that the tag USE_MATHJAX is set to YES. + +MATHJAX_RELPATH = http://cdn.mathjax.org/mathjax/latest + +# The MATHJAX_EXTENSIONS tag can be used to specify one or more MathJax +# extension names that should be enabled during MathJax rendering. For example +# MATHJAX_EXTENSIONS = TeX/AMSmath TeX/AMSsymbols +# This tag requires that the tag USE_MATHJAX is set to YES. + +MATHJAX_EXTENSIONS = + +# The MATHJAX_CODEFILE tag can be used to specify a file with javascript pieces +# of code that will be used on startup of the MathJax code. See the MathJax site +# (see: http://docs.mathjax.org/en/latest/output.html) for more details. For an +# example see the documentation. +# This tag requires that the tag USE_MATHJAX is set to YES. + +MATHJAX_CODEFILE = + +# When the SEARCHENGINE tag is enabled doxygen will generate a search box for +# the HTML output. The underlying search engine uses javascript and DHTML and +# should work on any modern browser. Note that when using HTML help +# (GENERATE_HTMLHELP), Qt help (GENERATE_QHP), or docsets (GENERATE_DOCSET) +# there is already a search function so this one should typically be disabled. +# For large projects the javascript based search engine can be slow, then +# enabling SERVER_BASED_SEARCH may provide a better solution. It is possible to +# search using the keyboard; to jump to the search box use + S +# (what the is depends on the OS and browser, but it is typically +# , /