1 |
ron |
1 |
/**
|
|
|
2 |
* Marlin 3D Printer Firmware
|
|
|
3 |
* Copyright (C) 2016 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 |
* Driver for the Philips PCA9632 LED driver.
|
|
|
25 |
* Written by Robert Mendon Feb 2017.
|
|
|
26 |
*/
|
|
|
27 |
|
|
|
28 |
#include "MarlinConfig.h"
|
|
|
29 |
|
|
|
30 |
#if ENABLED(PCA9632)
|
|
|
31 |
|
|
|
32 |
#include "pca9632.h"
|
|
|
33 |
#include "leds.h"
|
|
|
34 |
#include <Wire.h>
|
|
|
35 |
|
|
|
36 |
#define PCA9632_MODE1_VALUE 0b00000001 //(ALLCALL)
|
|
|
37 |
#define PCA9632_MODE2_VALUE 0b00010101 //(DIMMING, INVERT, CHANGE ON STOP,TOTEM)
|
|
|
38 |
#define PCA9632_LEDOUT_VALUE 0b00101010
|
|
|
39 |
|
|
|
40 |
/* Register addresses */
|
|
|
41 |
#define PCA9632_MODE1 0x00
|
|
|
42 |
#define PCA9632_MODE2 0x01
|
|
|
43 |
#define PCA9632_PWM0 0x02
|
|
|
44 |
#define PCA9632_PWM1 0x03
|
|
|
45 |
#define PCA9632_PWM2 0x04
|
|
|
46 |
#define PCA9632_PWM3 0x05
|
|
|
47 |
#define PCA9632_GRPPWM 0x06
|
|
|
48 |
#define PCA9632_GRPFREQ 0x07
|
|
|
49 |
#define PCA9632_LEDOUT 0x08
|
|
|
50 |
#define PCA9632_SUBADR1 0x09
|
|
|
51 |
#define PCA9632_SUBADR2 0x0A
|
|
|
52 |
#define PCA9632_SUBADR3 0x0B
|
|
|
53 |
#define PCA9632_ALLCALLADDR 0x0C
|
|
|
54 |
|
|
|
55 |
#define PCA9632_NO_AUTOINC 0x00
|
|
|
56 |
#define PCA9632_AUTO_ALL 0x80
|
|
|
57 |
#define PCA9632_AUTO_IND 0xA0
|
|
|
58 |
#define PCA9632_AUTOGLO 0xC0
|
|
|
59 |
#define PCA9632_AUTOGI 0xE0
|
|
|
60 |
|
|
|
61 |
// Red LED0
|
|
|
62 |
// Green LED1
|
|
|
63 |
// Blue LED2
|
|
|
64 |
#define PCA9632_RED 0x00
|
|
|
65 |
#define PCA9632_GRN 0x02
|
|
|
66 |
#define PCA9632_BLU 0x04
|
|
|
67 |
|
|
|
68 |
#define LED_OFF 0x00
|
|
|
69 |
#define LED_ON 0x01
|
|
|
70 |
#define LED_PWM 0x02
|
|
|
71 |
|
|
|
72 |
#define PCA9632_ADDRESS 0b01100000
|
|
|
73 |
|
|
|
74 |
byte PCA_init = 0;
|
|
|
75 |
|
|
|
76 |
static void PCA9632_WriteRegister(const byte addr, const byte regadd, const byte value) {
|
|
|
77 |
Wire.beginTransmission(addr);
|
|
|
78 |
Wire.write(regadd);
|
|
|
79 |
Wire.write(value);
|
|
|
80 |
Wire.endTransmission();
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
static void PCA9632_WriteAllRegisters(const byte addr, const byte regadd, const byte value1, const byte value2, const byte value3) {
|
|
|
84 |
Wire.beginTransmission(addr);
|
|
|
85 |
Wire.write(PCA9632_AUTO_IND | regadd);
|
|
|
86 |
Wire.write(value1);
|
|
|
87 |
Wire.write(value2);
|
|
|
88 |
Wire.write(value3);
|
|
|
89 |
Wire.endTransmission();
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
#if 0
|
|
|
93 |
static byte PCA9632_ReadRegister(const byte addr, const byte regadd) {
|
|
|
94 |
Wire.beginTransmission(addr);
|
|
|
95 |
Wire.write(regadd);
|
|
|
96 |
const byte value = Wire.read();
|
|
|
97 |
Wire.endTransmission();
|
|
|
98 |
return value;
|
|
|
99 |
}
|
|
|
100 |
#endif
|
|
|
101 |
|
|
|
102 |
void pca9632_set_led_color(const LEDColor &color) {
|
|
|
103 |
Wire.begin();
|
|
|
104 |
if (!PCA_init) {
|
|
|
105 |
PCA_init = 1;
|
|
|
106 |
PCA9632_WriteRegister(PCA9632_ADDRESS,PCA9632_MODE1, PCA9632_MODE1_VALUE);
|
|
|
107 |
PCA9632_WriteRegister(PCA9632_ADDRESS,PCA9632_MODE2, PCA9632_MODE2_VALUE);
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
const byte LEDOUT = (color.r ? LED_PWM << PCA9632_RED : 0)
|
|
|
111 |
| (color.g ? LED_PWM << PCA9632_GRN : 0)
|
|
|
112 |
| (color.b ? LED_PWM << PCA9632_BLU : 0);
|
|
|
113 |
|
|
|
114 |
PCA9632_WriteAllRegisters(PCA9632_ADDRESS,PCA9632_PWM0, color.r, color.g, color.b);
|
|
|
115 |
PCA9632_WriteRegister(PCA9632_ADDRESS,PCA9632_LEDOUT, LEDOUT);
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
#endif // PCA9632
|