|
@@ -0,0 +1,69 @@
|
|
1
|
+/**
|
|
2
|
+ * Marlin 3D Printer Firmware
|
|
3
|
+ * Copyright (C) 2019 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
|
+#ifdef ARDUINO_ARCH_ESP32
|
|
23
|
+
|
|
24
|
+#include "../../inc/MarlinConfig.h"
|
|
25
|
+
|
|
26
|
+#if HAS_SERVOS
|
|
27
|
+
|
|
28
|
+#include "HAL_Servo_ESP32.h"
|
|
29
|
+
|
|
30
|
+int Servo::channel_next_free = 0;
|
|
31
|
+
|
|
32
|
+Servo::Servo() {
|
|
33
|
+ this->channel = channel_next_free++;
|
|
34
|
+}
|
|
35
|
+
|
|
36
|
+int8_t Servo::attach(const int pin) {
|
|
37
|
+ if (this->channel >= CHANNEL_MAX_NUM) return -1;
|
|
38
|
+ if (pin > 0) this->pin = pin;
|
|
39
|
+
|
|
40
|
+ ledcSetup(this->channel, 50, 16); // channel X, 50 Hz, 16-bit depth
|
|
41
|
+ ledcAttachPin(this->pin, this->channel);
|
|
42
|
+ return true;
|
|
43
|
+}
|
|
44
|
+
|
|
45
|
+void Servo::detach() { ledcDetachPin(this->pin) }
|
|
46
|
+
|
|
47
|
+int Servo::read() { return this->degrees; }
|
|
48
|
+
|
|
49
|
+void Servo::write(int degrees) {
|
|
50
|
+ this->degrees = constrain(degrees, MIN_ANGLE, MAX_ANGLE);
|
|
51
|
+ int us = map(this->degrees, MIN_ANGLE, MAX_ANGLE, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
|
|
52
|
+ int duty = map(us, 0, TAU_USEC, 0, MAX_COMPARE);
|
|
53
|
+ ledcWrite(channel, duty);
|
|
54
|
+}
|
|
55
|
+
|
|
56
|
+void Servo::move(const int value) {
|
|
57
|
+ constexpr uint16_t servo_delay[] = SERVO_DELAY;
|
|
58
|
+ static_assert(COUNT(servo_delay) == NUM_SERVOS, "SERVO_DELAY must be an array NUM_SERVOS long.");
|
|
59
|
+ if (this->attach(0) >= 0) {
|
|
60
|
+ this->write(value);
|
|
61
|
+ safe_delay(servo_delay[this->channel]);
|
|
62
|
+ #if ENABLED(DEACTIVATE_SERVOS_AFTER_MOVE)
|
|
63
|
+ this->detach();
|
|
64
|
+ #endif
|
|
65
|
+ }
|
|
66
|
+}
|
|
67
|
+#endif // HAS_SERVOS
|
|
68
|
+
|
|
69
|
+#endif // ARDUINO_ARCH_ESP32
|