My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

M42.cpp 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../../inc/MarlinConfig.h"
  23. #if ENABLED(DIRECT_PIN_CONTROL)
  24. #include "../gcode.h"
  25. #include "../../MarlinCore.h" // for pin_is_protected
  26. #if HAS_FAN
  27. #include "../../module/temperature.h"
  28. #endif
  29. #ifdef MAPLE_STM32F1
  30. // these are enums on the F1...
  31. #define INPUT_PULLDOWN INPUT_PULLDOWN
  32. #define INPUT_ANALOG INPUT_ANALOG
  33. #define OUTPUT_OPEN_DRAIN OUTPUT_OPEN_DRAIN
  34. #endif
  35. void protected_pin_err() {
  36. SERIAL_ERROR_MSG(STR_ERR_PROTECTED_PIN);
  37. }
  38. /**
  39. * M42: Change pin status via GCode
  40. *
  41. * P<pin> Pin number (LED if omitted)
  42. * For LPC1768 specify pin P1_02 as M42 P102,
  43. * P1_20 as M42 P120, etc.
  44. *
  45. * S<byte> Pin status from 0 - 255
  46. * I Flag to ignore Marlin's pin protection
  47. *
  48. * T<mode> Pin mode: 0=INPUT 1=OUTPUT 2=INPUT_PULLUP 3=INPUT_PULLDOWN
  49. */
  50. void GcodeSuite::M42() {
  51. const int pin_index = PARSED_PIN_INDEX('P', GET_PIN_MAP_INDEX(LED_PIN));
  52. if (pin_index < 0) return;
  53. const pin_t pin = GET_PIN_MAP_PIN(pin_index);
  54. if (!parser.boolval('I') && pin_is_protected(pin)) return protected_pin_err();
  55. bool avoidWrite = false;
  56. if (parser.seenval('T')) {
  57. switch (parser.value_byte()) {
  58. case 0: pinMode(pin, INPUT); avoidWrite = true; break;
  59. case 1: pinMode(pin, OUTPUT); break;
  60. case 2: pinMode(pin, INPUT_PULLUP); avoidWrite = true; break;
  61. #ifdef INPUT_PULLDOWN
  62. case 3: pinMode(pin, INPUT_PULLDOWN); avoidWrite = true; break;
  63. #endif
  64. #ifdef INPUT_ANALOG
  65. case 4: pinMode(pin, INPUT_ANALOG); avoidWrite = true; break;
  66. #endif
  67. #ifdef OUTPUT_OPEN_DRAIN
  68. case 5: pinMode(pin, OUTPUT_OPEN_DRAIN); break;
  69. #endif
  70. default: SERIAL_ECHOLNPGM("Invalid Pin Mode"); return;
  71. }
  72. }
  73. if (!parser.seenval('S')) return;
  74. const byte pin_status = parser.value_byte();
  75. #if HAS_FAN
  76. switch (pin) {
  77. #if HAS_FAN0
  78. case FAN0_PIN: thermalManager.fan_speed[0] = pin_status; return;
  79. #endif
  80. #if HAS_FAN1
  81. case FAN1_PIN: thermalManager.fan_speed[1] = pin_status; return;
  82. #endif
  83. #if HAS_FAN2
  84. case FAN2_PIN: thermalManager.fan_speed[2] = pin_status; return;
  85. #endif
  86. #if HAS_FAN3
  87. case FAN3_PIN: thermalManager.fan_speed[3] = pin_status; return;
  88. #endif
  89. #if HAS_FAN4
  90. case FAN4_PIN: thermalManager.fan_speed[4] = pin_status; return;
  91. #endif
  92. #if HAS_FAN5
  93. case FAN5_PIN: thermalManager.fan_speed[5] = pin_status; return;
  94. #endif
  95. #if HAS_FAN6
  96. case FAN6_PIN: thermalManager.fan_speed[6] = pin_status; return;
  97. #endif
  98. #if HAS_FAN7
  99. case FAN7_PIN: thermalManager.fan_speed[7] = pin_status; return;
  100. #endif
  101. }
  102. #endif
  103. if (avoidWrite) {
  104. SERIAL_ECHOLNPGM("?Cannot write to INPUT");
  105. return;
  106. }
  107. // An OUTPUT_OPEN_DRAIN should not be changed to normal OUTPUT (STM32)
  108. // Use M42 Px M1/5 S0/1 to set the output type and then set value
  109. #ifndef OUTPUT_OPEN_DRAIN
  110. pinMode(pin, OUTPUT);
  111. #endif
  112. extDigitalWrite(pin, pin_status);
  113. #ifdef ARDUINO_ARCH_STM32
  114. // A simple I/O will be set to 0 by hal.set_pwm_duty()
  115. if (pin_status <= 1 && !PWM_PIN(pin)) return;
  116. #endif
  117. hal.set_pwm_duty(pin, pin_status);
  118. }
  119. #endif // DIRECT_PIN_CONTROL