My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /**
  23. * Driver for the Philips PCA9632 LED driver.
  24. * Written by Robert Mendon Feb 2017.
  25. */
  26. #include "../../inc/MarlinConfig.h"
  27. #if ENABLED(PCA9632)
  28. #include "pca9632.h"
  29. #include "leds.h"
  30. #include <Wire.h>
  31. #define PCA9632_MODE1_VALUE 0b00000001 //(ALLCALL)
  32. #define PCA9632_MODE2_VALUE 0b00010101 //(DIMMING, INVERT, CHANGE ON STOP,TOTEM)
  33. #define PCA9632_LEDOUT_VALUE 0b00101010
  34. /* Register addresses */
  35. #define PCA9632_MODE1 0x00
  36. #define PCA9632_MODE2 0x01
  37. #define PCA9632_PWM0 0x02
  38. #define PCA9632_PWM1 0x03
  39. #define PCA9632_PWM2 0x04
  40. #define PCA9632_PWM3 0x05
  41. #define PCA9632_GRPPWM 0x06
  42. #define PCA9632_GRPFREQ 0x07
  43. #define PCA9632_LEDOUT 0x08
  44. #define PCA9632_SUBADR1 0x09
  45. #define PCA9632_SUBADR2 0x0A
  46. #define PCA9632_SUBADR3 0x0B
  47. #define PCA9632_ALLCALLADDR 0x0C
  48. #define PCA9632_NO_AUTOINC 0x00
  49. #define PCA9632_AUTO_ALL 0x80
  50. #define PCA9632_AUTO_IND 0xA0
  51. #define PCA9632_AUTOGLO 0xC0
  52. #define PCA9632_AUTOGI 0xE0
  53. // Red=LED0 Green=LED1 Blue=LED2 White=LED3
  54. #ifndef PCA9632_RED
  55. #define PCA9632_RED 0x00
  56. #endif
  57. #ifndef PCA9632_GRN
  58. #define PCA9632_GRN 0x02
  59. #endif
  60. #ifndef PCA9632_BLU
  61. #define PCA9632_BLU 0x04
  62. #endif
  63. #if HAS_WHITE_LED && !defined(PCA9632_WHT)
  64. #define PCA9632_WHT 0x06
  65. #endif
  66. // If any of the color indexes are greater than 0x04 they can't use auto increment
  67. #if !defined(PCA9632_NO_AUTO_INC) && (PCA9632_RED > 0x04 || PCA9632_GRN > 0x04 || PCA9632_BLU > 0x04 || PCA9632_WHT > 0x04)
  68. #define PCA9632_NO_AUTO_INC
  69. #endif
  70. #define LED_OFF 0x00
  71. #define LED_ON 0x01
  72. #define LED_PWM 0x02
  73. #define PCA9632_ADDRESS 0b01100000
  74. byte PCA_init = 0;
  75. static void PCA9632_WriteRegister(const byte addr, const byte regadd, const byte value) {
  76. Wire.beginTransmission(I2C_ADDRESS(addr));
  77. Wire.write(regadd);
  78. Wire.write(value);
  79. Wire.endTransmission();
  80. }
  81. static void PCA9632_WriteAllRegisters(const byte addr, const byte regadd, const byte vr, const byte vg, const byte vb
  82. OPTARG(PCA9632_RGBW, const byte vw)
  83. ) {
  84. #if DISABLED(PCA9632_NO_AUTO_INC)
  85. uint8_t data[4];
  86. data[0] = PCA9632_AUTO_IND | regadd;
  87. data[1 + (PCA9632_RED >> 1)] = vr;
  88. data[1 + (PCA9632_GRN >> 1)] = vg;
  89. data[1 + (PCA9632_BLU >> 1)] = vb;
  90. Wire.beginTransmission(I2C_ADDRESS(addr));
  91. Wire.write(data, sizeof(data));
  92. Wire.endTransmission();
  93. #else
  94. PCA9632_WriteRegister(addr, regadd + (PCA9632_RED >> 1), vr);
  95. PCA9632_WriteRegister(addr, regadd + (PCA9632_GRN >> 1), vg);
  96. PCA9632_WriteRegister(addr, regadd + (PCA9632_BLU >> 1), vb);
  97. #if ENABLED(PCA9632_RGBW)
  98. PCA9632_WriteRegister(addr, regadd + (PCA9632_WHT >> 1), vw);
  99. #endif
  100. #endif
  101. }
  102. #if 0
  103. static byte PCA9632_ReadRegister(const byte addr, const byte regadd) {
  104. Wire.beginTransmission(I2C_ADDRESS(addr));
  105. Wire.write(regadd);
  106. const byte value = Wire.read();
  107. Wire.endTransmission();
  108. return value;
  109. }
  110. #endif
  111. void PCA9632_set_led_color(const LEDColor &color) {
  112. Wire.begin();
  113. if (!PCA_init) {
  114. PCA_init = 1;
  115. PCA9632_WriteRegister(PCA9632_ADDRESS,PCA9632_MODE1, PCA9632_MODE1_VALUE);
  116. PCA9632_WriteRegister(PCA9632_ADDRESS,PCA9632_MODE2, PCA9632_MODE2_VALUE);
  117. }
  118. const byte LEDOUT = (color.r ? LED_PWM << PCA9632_RED : 0)
  119. | (color.g ? LED_PWM << PCA9632_GRN : 0)
  120. | (color.b ? LED_PWM << PCA9632_BLU : 0)
  121. #if ENABLED(PCA9632_RGBW)
  122. | (color.w ? LED_PWM << PCA9632_WHT : 0)
  123. #endif
  124. ;
  125. PCA9632_WriteAllRegisters(PCA9632_ADDRESS,PCA9632_PWM0, color.r, color.g, color.b
  126. OPTARG(PCA9632_RGBW, color.w)
  127. );
  128. PCA9632_WriteRegister(PCA9632_ADDRESS,PCA9632_LEDOUT, LEDOUT);
  129. }
  130. #if ENABLED(PCA9632_BUZZER)
  131. void PCA9632_buzz(const long, const uint16_t) {
  132. uint8_t data[] = PCA9632_BUZZER_DATA;
  133. Wire.beginTransmission(I2C_ADDRESS(PCA9632_ADDRESS));
  134. Wire.write(data, sizeof(data));
  135. Wire.endTransmission();
  136. }
  137. #endif // PCA9632_BUZZER
  138. #endif // PCA9632