浏览代码

Fix / optimize PCA9533 LED (Mightyboard) (#17381)

grauerfuchs 4 年前
父节点
当前提交
293a0997c9
没有帐户链接到提交者的电子邮件

+ 0
- 1
Marlin/Configuration.h 查看文件

@@ -2164,7 +2164,6 @@
2164 2164
 //#define PCA9632
2165 2165
 
2166 2166
 // Support for PCA9533 PWM LED driver
2167
-// https://github.com/mikeshub/SailfishRGB_LED
2168 2167
 //#define PCA9533
2169 2168
 
2170 2169
 /**

+ 3
- 3
Marlin/src/feature/leds/leds.cpp 查看文件

@@ -39,7 +39,7 @@
39 39
 #endif
40 40
 
41 41
 #if ENABLED(PCA9533)
42
-  #include <SailfishRGB_LED.h>
42
+  #include "pca9533.h"
43 43
 #endif
44 44
 
45 45
 #if ENABLED(LED_COLOR_PRESETS)
@@ -72,7 +72,7 @@ void LEDLights::setup() {
72 72
     neo.init();
73 73
   #endif
74 74
   #if ENABLED(PCA9533)
75
-    RGBinit();
75
+    PCA9533_init();
76 76
   #endif
77 77
   #if ENABLED(LED_USER_PRESET_STARTUP)
78 78
     set_default();
@@ -141,7 +141,7 @@ void LEDLights::set_color(const LEDColor &incol
141 141
   #endif
142 142
 
143 143
   #if ENABLED(PCA9533)
144
-    RGBsetColor(incol.r, incol.g, incol.b, true);
144
+    PCA9533_setColor(incol.r, incol.g, incol.b);
145 145
   #endif
146 146
 
147 147
   #if EITHER(LED_CONTROL_MENU, PRINTER_EVENT_LEDS)

+ 127
- 0
Marlin/src/feature/leds/pca9533.cpp 查看文件

@@ -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

+ 59
- 0
Marlin/src/feature/leds/pca9533.h 查看文件

@@ -0,0 +1,59 @@
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
+#pragma once
23
+
24
+/*
25
+ * Driver for the PCA9533 LED controller found on the MightyBoard
26
+ * used by FlashForge Creator Pro, MakerBot, etc.
27
+ * Written 2020 APR 01 by grauerfuchs
28
+ */
29
+#include <Arduino.h>
30
+
31
+#define ENABLE_I2C_PULLUPS
32
+
33
+// Chip address (for Wire)
34
+#define PCA9533_Addr        0xC4 
35
+
36
+// Control registers
37
+#define PCA9533_REG_READ    0x00
38
+#define PCA9533_REG_PSC0    0x01
39
+#define PCA9533_REG_PWM0    0x02
40
+#define PCA9533_REG_PSC1    0x03
41
+#define PCA9533_REG_PWM1    0x04
42
+#define PCA9533_REG_SEL     0x05
43
+#define PCA9533_REGM_AI     0x10
44
+
45
+// LED selector operation
46
+#define PCA9533_LED_OP_OFF  0B00
47
+#define PCA9533_LED_OP_ON   0B01
48
+#define PCA9533_LED_OP_PWM0 0B10
49
+#define PCA9533_LED_OP_PWM1 0B11
50
+
51
+// Select register bit offsets for LED colors
52
+#define PCA9533_LED_OFS_RED 0
53
+#define PCA9533_LED_OFS_GRN 2
54
+#define PCA9533_LED_OFS_BLU 4
55
+
56
+void PCA9533_init();
57
+void PCA9533_reset();
58
+void PCA9533_setColor(uint8_t red, uint8_t green, uint8_t blue);
59
+void PCA9533_setOff();

+ 1
- 1
buildroot/share/PlatformIO/variants/BIGTREE_SKR_PRO_1v1/variant.h 查看文件

@@ -234,7 +234,7 @@ extern "C" {
234 234
 
235 235
 // On-board LED pin number
236 236
 #define LED_BUILTIN             PA7
237
-//#define LED_GREEN             LED_BUILTIN   should be defined here but omitted to avoid redefinition in SailfishRGB_LED
237
+#define LED_GREEN               LED_BUILTIN
238 238
 
239 239
 // Below SPI and I2C definitions already done in the core
240 240
 // Could be redefined here if differs from the default one

+ 1
- 1
buildroot/share/tests/DUE-tests 查看文件

@@ -16,7 +16,7 @@ opt_set FANMUX0_PIN 53
16 16
 opt_enable S_CURVE_ACCELERATION EEPROM_SETTINGS GCODE_MACROS \
17 17
            PIDTEMPBED FIX_MOUNTED_PROBE Z_SAFE_HOMING CODEPENDENT_XY_HOMING \
18 18
            EEPROM_SETTINGS SDSUPPORT BINARY_FILE_TRANSFER \
19
-           BLINKM PCA9632 RGB_LED RGB_LED_R_PIN RGB_LED_G_PIN RGB_LED_B_PIN LED_CONTROL_MENU \
19
+           BLINKM PCA9533 PCA9632 RGB_LED RGB_LED_R_PIN RGB_LED_G_PIN RGB_LED_B_PIN LED_CONTROL_MENU \
20 20
            NEOPIXEL_LED CASE_LIGHT_ENABLE CASE_LIGHT_USE_NEOPIXEL CASE_LIGHT_MENU \
21 21
            NOZZLE_PARK_FEATURE ADVANCED_PAUSE_FEATURE FILAMENT_RUNOUT_DISTANCE_MM FILAMENT_RUNOUT_SENSOR \
22 22
            AUTO_BED_LEVELING_BILINEAR Z_MIN_PROBE_REPEATABILITY_TEST DEBUG_LEVELING_FEATURE \

+ 1
- 1
buildroot/share/tests/rambo-tests 查看文件

@@ -53,7 +53,7 @@ opt_enable REPRAP_DISCOUNT_SMART_CONTROLLER LCD_PROGRESS_BAR LCD_PROGRESS_BAR_TE
53 53
            FIX_MOUNTED_PROBE Z_SAFE_HOMING CODEPENDENT_XY_HOMING PIDTEMPBED PROBE_TEMP_COMPENSATION \
54 54
            PROBING_HEATERS_OFF PROBING_FANS_OFF PROBING_STEPPERS_OFF WAIT_FOR_BED_HEATER \
55 55
            EEPROM_SETTINGS SDSUPPORT SD_REPRINT_LAST_SELECTED_FILE BINARY_FILE_TRANSFER \
56
-           BLINKM PCA9632 RGB_LED RGB_LED_R_PIN RGB_LED_G_PIN RGB_LED_B_PIN LED_CONTROL_MENU \
56
+           BLINKM PCA9533 PCA9632 RGB_LED RGB_LED_R_PIN RGB_LED_G_PIN RGB_LED_B_PIN LED_CONTROL_MENU \
57 57
            NEOPIXEL_LED CASE_LIGHT_ENABLE CASE_LIGHT_USE_NEOPIXEL CASE_LIGHT_MENU \
58 58
            PID_PARAMS_PER_HOTEND PID_AUTOTUNE_MENU PID_EDIT_MENU LCD_SHOW_E_TOTAL \
59 59
            PRINTCOUNTER SERVICE_NAME_1 SERVICE_INTERVAL_1 LEVEL_BED_CORNERS \

+ 5
- 6
platformio.ini 查看文件

@@ -33,7 +33,6 @@ lib_deps =
33 33
   LiquidTWI2=https://github.com/lincomatic/LiquidTWI2/archive/master.zip
34 34
   Arduino-L6470=https://github.com/ameyer/Arduino-L6470/archive/0.8.0.zip
35 35
   SailfishLCD=https://github.com/mikeshub/SailfishLCD/archive/master.zip
36
-  SailfishRGB_LED=https://github.com/mikeshub/SailfishRGB_LED/archive/master.zip
37 36
   SlowSoftI2CMaster=https://github.com/mikeshub/SlowSoftI2CMaster/archive/master.zip
38 37
 
39 38
 # Globally defined properties
@@ -623,7 +622,7 @@ build_flags       = ${common.build_flags}
623 622
 build_unflags     = -std=gnu++11
624 623
 extra_scripts     = pre:buildroot/share/PlatformIO/scripts/generic_create_variant.py
625 624
   buildroot/share/PlatformIO/scripts/STEVAL__F401XX.py
626
-lib_ignore        = Adafruit NeoPixel, TMCStepper, SailfishLCD, SailfishRGB_LED, SlowSoftI2CMaster, SoftwareSerial
625
+lib_ignore        = Adafruit NeoPixel, TMCStepper, SailfishLCD, SlowSoftI2CMaster, SoftwareSerial
627 626
 src_filter        = ${common.default_src_filter} +<src/HAL/STM32>
628 627
 
629 628
 #
@@ -639,7 +638,7 @@ build_flags       = ${common.build_flags}
639 638
   -IMarlin/src/HAL/STM32
640 639
 build_unflags     = -std=gnu++11
641 640
 extra_scripts     = pre:buildroot/share/PlatformIO/scripts/generic_create_variant.py
642
-lib_ignore        = Adafruit NeoPixel, TMCStepper, SailfishLCD, SailfishRGB_LED, SlowSoftI2CMaster, SoftwareSerial
641
+lib_ignore        = Adafruit NeoPixel, TMCStepper, SailfishLCD, SlowSoftI2CMaster, SoftwareSerial
643 642
 src_filter        = ${common.default_src_filter} +<src/HAL/STM32>
644 643
 
645 644
 
@@ -679,7 +678,7 @@ build_flags       = ${common.build_flags}
679 678
   -IMarlin/src/HAL/STM32
680 679
 build_unflags     = -std=gnu++11
681 680
 extra_scripts     = pre:buildroot/share/PlatformIO/scripts/generic_create_variant.py
682
-lib_ignore        = Adafruit NeoPixel, TMCStepper, SailfishLCD, SailfishRGB_LED, SlowSoftI2CMaster, SoftwareSerial
681
+lib_ignore        = Adafruit NeoPixel, TMCStepper, SailfishLCD, SlowSoftI2CMaster, SoftwareSerial
683 682
 src_filter        = ${common.default_src_filter} +<src/HAL/STM32>
684 683
 
685 684
 #
@@ -742,7 +741,7 @@ build_flags       = ${common.build_flags}
742 741
   -DPIN_SERIAL2_TX=PD_5
743 742
 build_unflags     = -std=gnu++11
744 743
 extra_scripts     = pre:buildroot/share/PlatformIO/scripts/generic_create_variant.py
745
-lib_ignore        = Adafruit NeoPixel, SailfishLCD, SailfishRGB_LED, SlowSoftI2CMaster
744
+lib_ignore        = Adafruit NeoPixel, SailfishLCD, SlowSoftI2CMaster
746 745
 src_filter        = ${common.default_src_filter} +<src/HAL/STM32>
747 746
 
748 747
 #
@@ -780,7 +779,7 @@ lib_deps      = ${common.lib_deps}
780 779
   ESP3DLib=https://github.com/luc-github/ESP3DLib.git
781 780
   arduinoWebSockets=https://github.com/Links2004/arduinoWebSockets.git
782 781
   ESP32SSDP=https://github.com/luc-github/ESP32SSDP.git
783
-lib_ignore    = LiquidCrystal, LiquidTWI2, SailfishLCD, SailfishRGB_LED, ESPAsyncTCP
782
+lib_ignore    = LiquidCrystal, LiquidTWI2, SailfishLCD, ESPAsyncTCP
784 783
 src_filter    = ${common.default_src_filter} +<src/HAL/ESP32>
785 784
 upload_speed  = 115200
786 785
 #upload_port   = marlinesp.local

正在加载...
取消
保存