runout.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * runout.h - Runout sensor support
  24. */
  25. #ifndef _RUNOUT_H_
  26. #define _RUNOUT_H_
  27. #include "cardreader.h"
  28. #include "printcounter.h"
  29. #include "stepper.h"
  30. #include "Marlin.h"
  31. #include "MarlinConfig.h"
  32. #define FIL_RUNOUT_THRESHOLD 5
  33. class FilamentRunoutSensor {
  34. public:
  35. FilamentRunoutSensor() {}
  36. static void setup();
  37. FORCE_INLINE static void reset() { runout_count = 0; filament_ran_out = false; }
  38. FORCE_INLINE static void run() {
  39. if ((IS_SD_PRINTING || print_job_timer.isRunning()) && check() && !filament_ran_out) {
  40. filament_ran_out = true;
  41. enqueue_and_echo_commands_P(PSTR(FILAMENT_RUNOUT_SCRIPT));
  42. planner.synchronize();
  43. }
  44. }
  45. private:
  46. static bool filament_ran_out;
  47. static uint8_t runout_count;
  48. FORCE_INLINE static bool check() {
  49. #if NUM_RUNOUT_SENSORS < 2
  50. // A single sensor applying to all extruders
  51. const bool is_out = READ(FIL_RUNOUT_PIN) == FIL_RUNOUT_INVERTING;
  52. #else
  53. // Read the sensor for the active extruder
  54. bool is_out;
  55. switch (active_extruder) {
  56. case 0: is_out = READ(FIL_RUNOUT_PIN) == FIL_RUNOUT_INVERTING; break;
  57. case 1: is_out = READ(FIL_RUNOUT2_PIN) == FIL_RUNOUT_INVERTING; break;
  58. #if NUM_RUNOUT_SENSORS > 2
  59. case 2: is_out = READ(FIL_RUNOUT3_PIN) == FIL_RUNOUT_INVERTING; break;
  60. #if NUM_RUNOUT_SENSORS > 3
  61. case 3: is_out = READ(FIL_RUNOUT4_PIN) == FIL_RUNOUT_INVERTING; break;
  62. #if NUM_RUNOUT_SENSORS > 4
  63. case 4: is_out = READ(FIL_RUNOUT5_PIN) == FIL_RUNOUT_INVERTING; break;
  64. #endif
  65. #endif
  66. #endif
  67. }
  68. #endif
  69. return (is_out ? ++runout_count : (runout_count = 0)) > FIL_RUNOUT_THRESHOLD;
  70. }
  71. };
  72. extern FilamentRunoutSensor runout;
  73. #endif // _RUNOUT_H_