Skip to content

Commit

Permalink
Support RGBW on PCA9632 (MarlinFirmware#20455)
Browse files Browse the repository at this point in the history
Co-authored-by: Scott Lahteine <[email protected]>
  • Loading branch information
2 people authored and tharts committed Jan 6, 2021
1 parent af34697 commit f4af573
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Marlin/src/feature/leds/leds.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#endif

// A white component can be passed
#if EITHER(RGBW_LED, NEOPIXEL_LED)
#if ANY(RGBW_LED, NEOPIXEL_LED, PCA9632_RGBW)
#define HAS_WHITE_LED 1
#endif

Expand Down
48 changes: 31 additions & 17 deletions Marlin/src/feature/leds/pca9632.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
#define PCA9632_AUTOGLO 0xC0
#define PCA9632_AUTOGI 0xE0

// Red=LED0 Green=LED1 Blue=LED2
// Red=LED0 Green=LED1 Blue=LED2 White=LED3
#ifndef PCA9632_RED
#define PCA9632_RED 0x00
#endif
Expand All @@ -68,9 +68,12 @@
#ifndef PCA9632_BLU
#define PCA9632_BLU 0x04
#endif
#if HAS_WHITE_LED && !defined(PCA9632_WHT)
#define PCA9632_WHT 0x06
#endif

// If any of the color indexes are greater than 0x04 they can't use auto increment
#if !defined(PCA9632_NO_AUTO_INC) && (PCA9632_RED > 0x04 || PCA9632_GRN > 0x04 || PCA9632_BLU > 0x04)
#if !defined(PCA9632_NO_AUTO_INC) && (PCA9632_RED > 0x04 || PCA9632_GRN > 0x04 || PCA9632_BLU > 0x04 || PCA9632_WHT > 0x04)
#define PCA9632_NO_AUTO_INC
#endif

Expand All @@ -89,25 +92,28 @@ static void PCA9632_WriteRegister(const byte addr, const byte regadd, const byte
Wire.endTransmission();
}

static void PCA9632_WriteAllRegisters(const byte addr, const byte regadd, const byte vr, const byte vg, const byte vb) {
static void PCA9632_WriteAllRegisters(const byte addr, const byte regadd, const byte vr, const byte vg, const byte vb
#if ENABLED(PCA9632_RGBW)
, const byte vw
#endif
) {
#if DISABLED(PCA9632_NO_AUTO_INC)
uint8_t data[4], len = 4;
uint8_t data[4];
data[0] = PCA9632_AUTO_IND | regadd;
data[1 + (PCA9632_RED >> 1)] = vr;
data[1 + (PCA9632_GRN >> 1)] = vg;
data[1 + (PCA9632_BLU >> 1)] = vb;
Wire.beginTransmission(I2C_ADDRESS(addr));
Wire.write(data, sizeof(data));
Wire.endTransmission();
#else
uint8_t data[6], len = 6;
data[0] = regadd + (PCA9632_RED >> 1);
data[1] = vr;
data[2] = regadd + (PCA9632_GRN >> 1);
data[3] = vg;
data[4] = regadd + (PCA9632_BLU >> 1);
data[5] = vb;
PCA9632_WriteRegister(addr, regadd + (PCA9632_RED >> 1), vr);
PCA9632_WriteRegister(addr, regadd + (PCA9632_GRN >> 1), vg);
PCA9632_WriteRegister(addr, regadd + (PCA9632_BLU >> 1), vb);
#if ENABLED(PCA9632_RGBW)
PCA9632_WriteRegister(addr, regadd + (PCA9632_WHT >> 1), vw);
#endif
#endif
Wire.beginTransmission(I2C_ADDRESS(addr));
Wire.write(data, len);
Wire.endTransmission();
}

#if 0
Expand All @@ -130,9 +136,17 @@ void PCA9632_set_led_color(const LEDColor &color) {

const byte LEDOUT = (color.r ? LED_PWM << PCA9632_RED : 0)
| (color.g ? LED_PWM << PCA9632_GRN : 0)
| (color.b ? LED_PWM << PCA9632_BLU : 0);

PCA9632_WriteAllRegisters(PCA9632_ADDRESS,PCA9632_PWM0, color.r, color.g, color.b);
| (color.b ? LED_PWM << PCA9632_BLU : 0)
#if ENABLED(PCA9632_RGBW)
| (color.w ? LED_PWM << PCA9632_WHT : 0)
#endif
;

PCA9632_WriteAllRegisters(PCA9632_ADDRESS,PCA9632_PWM0, color.r, color.g, color.b
#if ENABLED(PCA9632_RGBW)
, color.w
#endif
);
PCA9632_WriteRegister(PCA9632_ADDRESS,PCA9632_LEDOUT, LEDOUT);
}

Expand Down

0 comments on commit f4af573

Please sign in to comment.