i2c RGBW led driver push/pull or opendrain output

Fork of PCA9633 by Mederic Melard

Files at this revision

API Documentation at this revision

Comitter:
mederic
Date:
Thu Jun 21 07:28:23 2018 +0000
Parent:
0:39b243509a43
Child:
2:651ace168465
Commit message:
first released

Changed in this revision

PCA9633.cpp Show annotated file Show diff for this revision Revisions of this file
PCA9633.h Show annotated file Show diff for this revision Revisions of this file
--- a/PCA9633.cpp	Wed Jun 20 12:56:31 2018 +0000
+++ b/PCA9633.cpp	Thu Jun 21 07:28:23 2018 +0000
@@ -4,7 +4,7 @@
 //***********************************/************************************
 //                              Constant                                //
 //***********************************/************************************
-#define PCA9633_ADD_SWRST   0x03 // Software Reset Call Address
+#define PCA9633_ADD_SWRST   0x06 // Software Reset Call Address (0x03<<1)
 #define PCA9633_REG_SWRST   0xa5 // Software Reset register
 #define PCA9633_VAL_SWRST   0x5a // Software Reset register
 
@@ -27,56 +27,51 @@
 PCA9633::PCA9633(I2C *i2c, char addr, bool invert, bool openDrain):_i2c(i2c){
     _addr = addr<<1;
     softReset();
-    _buf[0] = PCA9633_REG_MODE1;
-    _buf[1] = 0;
     /*  SLEEP=0 Normal Mode
         SUB1 =0 PCA9633 does not respond to I2C-bus subaddress 1.
         SUB2 =0 PCA9633 does not respond to I2C-bus subaddress 2.
         SUB3 =0 PCA9633 does not respond to I2C-bus subaddress 3.
         ALLCAL =0 PCA9633 does not respond to LED All Call I2C-bus address.*/
-    _i2c->write(_addr,_buf,2);
+    char buf[2] = {PCA9633_REG_MODE1,0};
+    _i2c->write(_addr,buf,2);
     config(invert, openDrain);
 }
 
-
 void PCA9633::config(bool invert, bool openDrain){
-    _buf[0] = PCA9633_REG_MODE2;
-    _buf[1] = (invert<<PCA9633_BIT_INVRT ) | (openDrain<<PCA9633_BIT_OUTDRV);
-    _i2c->write(_addr,_buf,2);
+    char buf[2] = {PCA9633_REG_MODE2,0};
+    buf[1] = (invert<<PCA9633_BIT_INVRT ) | (openDrain<<PCA9633_BIT_OUTDRV);
+    _i2c->write(_addr,buf,2);
 }
 
 void PCA9633::softReset(void){
-    _buf[0] = PCA9633_REG_SWRST;
-    _buf[1] = PCA9633_VAL_SWRST;
-    _i2c->write(PCA9633_ADD_SWRST,_buf,2); 
+    char buf[2] = {PCA9633_REG_SWRST,PCA9633_VAL_SWRST};
+    _i2c->write(0x06,buf,2);
 }
 
 void PCA9633::pwm(char bright, char pos){
-    _buf[0] = PCA9633_REG_PWM0;
-    _buf[1] = bright;_buf[2] = bright;_buf[3] = bright;_buf[4] = bright;
+    char buf[5] = {PCA9633_REG_PWM0,bright,bright,bright,bright};
     char len = 5;  
     if(pos<PCA9633::ALL){
         len=2;
-        _buf[0]+=pos;
+        buf[0]+=pos;
     }
-    _i2c->write(_addr,_buf,len);
+    _i2c->write(_addr,buf,len);
 }
 
-void PCA9633::dim(char val){
-     _buf[0] = PCA9633_REG_MODE2;
-     _buf[1] = 0;
-     _i2c->write(_addr,_buf,1);          //cmd to read mode2 
-     _i2c->read(_addr, _buf+1,1);      //stock in buf[1]
-     _buf[1] &= ~(1<<PCA9633_BIT_DMBLNK); //set dimming in mode2
-     _i2c->write(_addr,_buf,2);          //write mode2
+void PCA9633::dim(char val){;
+    char buf[2] = {PCA9633_REG_MODE2,0};
+     _i2c->write(_addr,buf,1);          //cmd to read mode2 
+     _i2c->read(_addr, &buf[1],1);      //stock in buf[1]
+     buf[1] &= ~(1<<PCA9633_BIT_DMBLNK); //set dimming in mode2
+     _i2c->write(_addr,buf,2);          //write mode2
      
-     _buf[0] = PCA9633_REG_GRPPWM;
-     _buf[1] = val;
-     _i2c->write(_addr,_buf,2);          //dimming
+     buf[0] = PCA9633_REG_GRPPWM;
+     buf[1] = val;
+     _i2c->write(_addr,buf,2);          //dimming
 }
 
 void PCA9633::blink(char duty, float period){
-    char buf[3] = {PCA9633_REG_MODE2,0};
+    char buf[3] = {PCA9633_REG_MODE2,0,0};
     _i2c->write(_addr,buf,1);          //cmd to read mode2 
     _i2c->read(_addr, &buf[1],1);      //stock in buf[1]
     buf[1] |= (1<<PCA9633_BIT_DMBLNK); //set blink in mode2
@@ -90,18 +85,18 @@
 }
 
 void PCA9633::ledout(char state, char pos){
-    char buf[5] = {PCA9633_REG_LEDOUT,0};
+    char buf[2] = {PCA9633_REG_LEDOUT,0};
     _i2c->write(_addr,buf,1);          //cmd to read ledout 
     _i2c->read(_addr, &buf[1],1);      //stock in buf[1]
      
     if(pos<PCA9633::ALL){
-        buf[1] &= ~(0x03<<(pos*2)); // erase 2 state bit of selected position
-        buf[1] |= (state<<(pos*2)); // set state at position
+        buf[1] &= ~(0x03<<(pos<<1)); // erase 2 state bit of selected position
+        buf[1] |= (state<<(pos<<1)); // set state at position
     }
     else{
         for(int i=0;i<PCA9633::ALL;i++){
-            buf[1] &= ~(0x03<<(i*2)); // erase 2 state bit of selected position
-            buf[1] |= (state<<(i*2)); // set state at position  
+            buf[1] &= ~(0x03<<(i<<1)); // erase 2 state bit of selected position
+            buf[1] |= (state<<(i<<1)); // set state at position  
         }
     }
     _i2c->write(_addr,buf,2);          //write LEDOUT
--- a/PCA9633.h	Wed Jun 20 12:56:31 2018 +0000
+++ b/PCA9633.h	Thu Jun 21 07:28:23 2018 +0000
@@ -7,7 +7,29 @@
  *  This driver ignore !OE pin functionality.
  *  This driver disable i2c LED All Call address.
  *  This driver disable i2c SUB addresing.  
- */  
+ *
+ * Example:
+ * @code
+  * #include "mbed.h"
+ * #include "PCA9633.h"
+ * I2C smbus(PB_7,PB_6);
+ *
+ * PCA9633 leds(&smbus);
+ * 
+ * int main() {
+ *     leds.config(true, true); //The output config was openDrain and inverted
+ *     leds.pwm(32,0);     //LED0 12.5%
+ *     leds.pwm(64,1);     //LED1 25%     
+ *     leds.pwm(128,2);    //LED2 50%    
+ *     leds.pwm(255,3);    //LED3 100%
+ *     leds.ledout(PCA9633::GROUP) //All led was dim/blink and single configurable
+ *     led.blink(64,1.5)           //Leds blink each 1.5s with 25% duty cycle
+ *   
+ *     while(1) {
+ *     }
+ * }
+ * @endcode
+ */ 
 class PCA9633{
     public:
     
@@ -69,7 +91,6 @@
     private:
         I2C *_i2c;
         char _addr;
-        char _buf[5];
 };
 
 #endif