|
@@ -0,0 +1,127 @@
|
|
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 <http://www.gnu.org/licenses/>.
|
|
20
|
+ *
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+/**
|
|
24
|
+ * PCA9533 LED controller driver (MightyBoard, FlashForge Creator Pro, etc.)
|
|
25
|
+ * by @grauerfuchs - 1 Apr 2020
|
|
26
|
+ */
|
|
27
|
+#include "../../inc/MarlinConfig.h"
|
|
28
|
+
|
|
29
|
+#if ENABLED(PCA9533)
|
|
30
|
+
|
|
31
|
+#include "pca9533.h"
|
|
32
|
+#include <Wire.h>
|
|
33
|
+
|
|
34
|
+void PCA9533_init() {
|
|
35
|
+ Wire.begin();
|
|
36
|
+ PCA9533_reset();
|
|
37
|
+}
|
|
38
|
+
|
|
39
|
+static void PCA9533_writeAllRegisters(uint8_t psc0, uint8_t pwm0, uint8_t psc1, uint8_t pwm1, uint8_t ls0){
|
|
40
|
+ uint8_t data[6] = { PCA9533_REG_PSC0 | PCA9533_REGM_AI, psc0, pwm0, psc1, pwm1, ls0 };
|
|
41
|
+ Wire.beginTransmission(PCA9533_Addr >> 1);
|
|
42
|
+ Wire.write(data, 6);
|
|
43
|
+ Wire.endTransmission();
|
|
44
|
+ delayMicroseconds(1);
|
|
45
|
+}
|
|
46
|
+
|
|
47
|
+static void PCA9533_writeRegister(uint8_t reg, uint8_t val){
|
|
48
|
+ uint8_t data[2] = { reg, val };
|
|
49
|
+ Wire.beginTransmission(PCA9533_Addr >> 1);
|
|
50
|
+ Wire.write(data, 2);
|
|
51
|
+ Wire.endTransmission();
|
|
52
|
+ delayMicroseconds(1);
|
|
53
|
+}
|
|
54
|
+
|
|
55
|
+// Reset (clear) all registers
|
|
56
|
+void PCA9533_reset() {
|
|
57
|
+ PCA9533_writeAllRegisters(0, 0, 0, 0, 0);
|
|
58
|
+}
|
|
59
|
+
|
|
60
|
+// Turn all LEDs off
|
|
61
|
+void PCA9533_setOff() {
|
|
62
|
+ PCA9533_writeRegister(PCA9533_REG_SEL, 0);
|
|
63
|
+}
|
|
64
|
+
|
|
65
|
+void PCA9533_setColor(uint8_t red, uint8_t green, uint8_t blue) {
|
|
66
|
+ uint8_t r_pwm0 = 0; // Register data - PWM value
|
|
67
|
+ uint8_t r_pwm1 = 0; // Register data - PWM value
|
|
68
|
+
|
|
69
|
+ uint8_t op_g = 0, op_r = 0, op_b = 0; // Opcodes - Green, Red, Blue
|
|
70
|
+
|
|
71
|
+ // Light theory! GREEN takes priority because
|
|
72
|
+ // it's the most visible to the human eye.
|
|
73
|
+ if (green == 0) op_g = PCA9533_LED_OP_OFF;
|
|
74
|
+ else if (green == 255) op_g = PCA9533_LED_OP_ON;
|
|
75
|
+ else { r_pwm0 = green; op_g = PCA9533_LED_OP_PWM0; }
|
|
76
|
+
|
|
77
|
+ // RED
|
|
78
|
+ if (red == 0) op_r = PCA9533_LED_OP_OFF;
|
|
79
|
+ else if (red == 255) op_r = PCA9533_LED_OP_ON;
|
|
80
|
+ else if (r_pwm0 == 0 || r_pwm0 == red) {
|
|
81
|
+ r_pwm0 = red; op_r = PCA9533_LED_OP_PWM0;
|
|
82
|
+ }
|
|
83
|
+ else {
|
|
84
|
+ r_pwm1 = red; op_r = PCA9533_LED_OP_PWM1;
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ // BLUE
|
|
88
|
+ if (blue == 0) op_b = PCA9533_LED_OP_OFF;
|
|
89
|
+ else if (blue == 255) op_b = PCA9533_LED_OP_ON;
|
|
90
|
+ else if (r_pwm0 == 0 || r_pwm0 == blue) {
|
|
91
|
+ r_pwm0 = blue; op_b = PCA9533_LED_OP_PWM0;
|
|
92
|
+ }
|
|
93
|
+ else if (r_pwm1 == 0 || r_pwm1 == blue) {
|
|
94
|
+ r_pwm1 = blue; op_b = PCA9533_LED_OP_PWM1;
|
|
95
|
+ }
|
|
96
|
+ else {
|
|
97
|
+ /**
|
|
98
|
+ * Conflict. 3 values are requested but only 2 channels exist.
|
|
99
|
+ * G is on channel 0 and R is on channel 1, so work from there.
|
|
100
|
+ * Find the closest match, average the values, then use the free channel.
|
|
101
|
+ */
|
|
102
|
+ uint8_t dgb = ABS(green - blue),
|
|
103
|
+ dgr = ABS(green - red),
|
|
104
|
+ dbr = ABS(blue - red);
|
|
105
|
+ if (dgb < dgr && dgb < dbr) { // Mix with G on channel 0.
|
|
106
|
+ op_b = PCA9533_LED_OP_PWM0;
|
|
107
|
+ r_pwm0 = uint8_t(((uint16_t)green + (uint16_t)blue) / 2);
|
|
108
|
+ }
|
|
109
|
+ else if (dbr <= dgr && dbr <= dgb) { // Mix with R on channel 1.
|
|
110
|
+ op_b = PCA9533_LED_OP_PWM1;
|
|
111
|
+ r_pwm1 = uint8_t(((uint16_t)red + (uint16_t)blue) / 2);
|
|
112
|
+ }
|
|
113
|
+ else { // Mix R+G on 0 and put B on 1.
|
|
114
|
+ op_r = PCA9533_LED_OP_PWM0;
|
|
115
|
+ r_pwm0 = uint8_t(((uint16_t)green + (uint16_t)red) / 2);
|
|
116
|
+ op_b = PCA9533_LED_OP_PWM1;
|
|
117
|
+ r_pwm1 = blue;
|
|
118
|
+ }
|
|
119
|
+ }
|
|
120
|
+
|
|
121
|
+ // Write the changes to the hardware
|
|
122
|
+ PCA9533_writeAllRegisters(0, r_pwm0, 0, r_pwm1,
|
|
123
|
+ (op_g << PCA9533_LED_OFS_GRN) | (op_r << PCA9533_LED_OFS_RED) | (op_b << PCA9533_LED_OFS_BLU)
|
|
124
|
+ );
|
|
125
|
+}
|
|
126
|
+
|
|
127
|
+#endif // PCA9533
|