Skip to content

Commit

Permalink
code and examples modification for Common-Anode Led
Browse files Browse the repository at this point in the history
  • Loading branch information
nadavmatalon committed Nov 4, 2016
1 parent 98ee37a commit e8b4af5
Show file tree
Hide file tree
Showing 9 changed files with 316 additions and 41 deletions.
17 changes: 8 additions & 9 deletions PCA9536_RGB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Ver. 1.0.0 - First release (25.10.16)
Ver. 1.1.0 - Interupt-based Blink (31.10.16)
Ver. 1.2.0 - Code odifications re C-CATHODE Leds (4.11.16)
*==============================================================================================================*
LICENSE
Expand Down Expand Up @@ -60,10 +61,9 @@ PCA9536_RGB::~PCA9536_RGB() {}

void PCA9536_RGB::init() {
_firstBlink = 1;
for (byte i=0; i<3; i++) {
setMode(getPin(i), _ledType);
setState(getPin(i), ~_ledType);
}
byte colors[3] = { _red, _green, _blue };
for (byte i=0; i<3; i++) setMode(colors[i], IO_OUTPUT);
turnOff();
}

/*==============================================================================================================*
Expand All @@ -79,23 +79,23 @@ void PCA9536_RGB::turnOn(color_t color) {
*==============================================================================================================*/

void PCA9536_RGB::turnOn() {
for (byte i=0; i<3; i++) setState(getPin(i), _ledType);
for (byte i=0; i<3; i++) turnOn(i);
}

/*==============================================================================================================*
TURN OFF A SINGLE COLOR
*==============================================================================================================*/

void PCA9536_RGB::turnOff(color_t color) { // PARAM: RED / GREEN / BLUE
setState(getPin(color), ~_ledType);
setState(getPin(color), (_ledType ^ 1));
}

/*==============================================================================================================*
TURN OFF ALL THREE COLORS
*==============================================================================================================*/

void PCA9536_RGB::turnOff() {
for (byte i=0; i<3; i++) setState(getPin(i), ~_ledType);
for (byte i=0; i<3; i++) turnOff(i);
}

/*==============================================================================================================*
Expand Down Expand Up @@ -164,7 +164,7 @@ void PCA9536_RGB::blink(color_t color) {
*==============================================================================================================*/

byte PCA9536_RGB::state(color_t color) {
return getState(getPin(color)) ? _ledType : ~_ledType;
return getState(getPin(color)) ? _ledType : (_ledType ^ 1);
}

/*==============================================================================================================*
Expand All @@ -186,4 +186,3 @@ byte PCA9536_RGB::getPin(color_t color) {
ISR(TIMER1_OVF_vect) {
_blinkFlag = 1;
}

3 changes: 2 additions & 1 deletion PCA9536_RGB.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
Ver. 1.0.0 - First release (25.10.16)
Ver. 1.1.0 - Interupt-based Blink (31.10.16)
Ver. 1.2.0 - Code odifications re C-CATHODE Leds (4.11.16)
*===============================================================================================================*
INTRODUCTION
*===============================================================================================================*
Expand Down
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Platform Badge](https://img.shields.io/badge/platform-Arduino-orange.svg)](https://www.arduino.cc/)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
[![SemVer](https://img.shields.io/badge/SemVer-1.1.0-brightgreen.svg)](http://semver.org/)
[![SemVer](https://img.shields.io/badge/SemVer-1.2.0-brightgreen.svg)](http://semver.org/)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
[![SemVer](https://img.shields.io/badge/eagle-pcb-yellow.svg)](https://cadsoft.io/)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Expand All @@ -23,10 +23,14 @@ The basis of the current API comes from the [PCA9536 Library](https://github.com
- **PCA9536_RGB.h** - Library Header file.
- **PCA9536_RGB.cpp** - Library Compilation.
- **/examples**
- **/PCA9536_Usage**
- **PCA9536_Usage.ino** - A sketch providing illustration of how to use the basic functions of this library.
- **/PCA9536_Blink**
- **PCA9536_Blink.ino** - A sketch providing illustration of how to use the blink() functionality.
- **/PCA9536_Usage_Anode**
- **PCA9536_Usage_Anode.ino** - A sketch providing illustration of how to use the basic functions of this library with a Common-Anode RGB Led.
- **/PCA9536_Usage_Cathode**
- **PCA9536_Usage_Cathode.ino** - A sketch providing illustration of how to use the basic functions of this library with a Common-Cathode RGB Led.
- **/PCA9536_Blink_Anode**
- **PCA9536_Blink_Anode.ino** - A sketch providing illustration of how to use the blink() functionality with a Common-Cathode RGB Led.
- **/PCA9536_Blink_Cathode**
- **PCA9536_Blink_Cathode.ino** - A sketch providing illustration of how to use the blink() functionality with a Common-Cathode RGB Led.
- **/extras**
- **License.txt** - A cope of the end-user license agreement.
- **/eagle**
Expand Down Expand Up @@ -180,6 +184,7 @@ Please report any issues/bugs/suggestions in the [Issues](https://github.com/nad

__Ver. 1.0.0__ - First release (24.10.16)
__Ver. 1.1.0__ - Interrupt-Based Blink (31.10.16)
__Ver. 1.2.0__ - Code odifications re C-CATHODE Leds (4.11.16)

## LICENSE

Expand Down
109 changes: 109 additions & 0 deletions examples/PCA9536_RGB_Blink_Anode/PCA9536_RGB_Blink_Anode.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
PCA9536_RGB LIBRARY - BLINK COLOR EXAMPLE (COMMON-ANODE)
--------------------------------------------------------
INTRODUCTION
------------
This sketch offers a simple code illustrating the use of the blink() function of the PCA9536_RGB library.
The blinking function uses an ISR (Interrupt Service Routine) based on overflow of Arduino Timer 1 (not to
be confused with the ISR used for monitoring the state of the push-button switch use below)
INPORTANT: The current library depends on the Arduino IDE's native 'Wire' library for I2C communication between the Arduino (Master) and the PCA9536 (Slave).
WIRING DIAGRAM
--------------
PCA9536
-------
IO0 --| • |-- VCC
| |
IO1 --| |-- SDA
| |
IO2 --| |-- SCL
| |
GND --| |-- IO3
-------
This sketch should be used with the following hookup:
PIN 1 (IO0) - Connect to Common-Anode RGB Led BLUE pin via appropriate series resistor
PIN 2 (IO1) - Connect to Common-Anode RGB Led GREEN pin via appropriate series resistor
PIN 3 (IO2) - Connect to Common-Anode RGB Led RED pin via appropriate series resistor
PIN 4 (GND) - Connect to Arduino GND
PIN 5 (IO3) - Leave unconnected for the purpose of this sketch
PIN 6 (SCL) - Connect to Arduino Pin A5 with a 2K2 (400MHz) or 10K (100MHz) pull-up resistor
PIN 7 (SDA) - Connect to Arduino Pin A4 with a 2K2 (400MHz) or 10K (100MHz) pull-up resistor
PIN 8 (VCC) - Connect to Arduino 5V output
DECOUPING - Connect a 0.1uF Ceramic Capacitor between the PCA9536's VCC & GND pins
RGB LED COMMON-ANODE PIN - Connect to Arduino 5V output
RUNNING THE SKETCH
------------------
1. Hook-up the PCA9536 and RGB Led (Common-Anode) as instructed above
2. Upload the sketch to the Arduino
3. Sit back and watch the pretty lights :-)
BUG REPORTS
-----------
Please report any bugs/issues/suggestions in the Github Repository of this library at:
https://github.com/nadavmatalon/PCA9536_RGB/issues
LICENSE
-------
The MIT License (MIT)
Copyright (c) 2016 Nadav Matalon
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without
limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "PCA9536_RGB.h"

PCA9536_RGB rgb(IO2, IO1, IO0, C_ANODE); // construct a new PCA9536_RGB instance

const color_t BLINK_COLOR = GREEN; // selected color to be blinked
const unsigned int BLINK_RATE = 600; // blink rate (= color 'ON' period; equal to 'OFF' period) in mS

void setup() {
DDRD &= ~bit(DDD2); // pinMode(digital pin 2, INPUT)
EICRA |= bit(ISC00); // trigger INT0 on any state CHANGE (both button pressed and released)
EIMSK |= bit(INT0); // enable external interrupt INT0
Wire.begin(); // join the I2C Bus
rgb.init(); // initialize the PCA9536_RGB instance with the above defined configuration
rgb.blinkSetup(BLINK_RATE); // set blink rate in mS
}

void loop() {
if (GPIOR0) rgb.blink(BLINK_COLOR); // if button is being pressed, blink selected color
else if (rgb.state(BLINK_COLOR)) rgb.turnOff(BLINK_COLOR); // if button has been released, but selected color remains ON, turn off color
}

ISR(INT0_vect, ISR_NAKED) { // Part I of ISR for INT0 (on digital pin 2)
asm volatile ( // bitWrite(GPIOR0, 0, ~PIND2)
" sbic %[pin], %[bit] \n" // if !bitRead(PIND, 2), skip the next instruction
" cbi %[gpio], 0 \n" // bitClear(GPIO0, 0)
" sbis %[pin], %[bit] \n" // if bitRead(PIND, 2), skip the next instruction
" sbi %[gpio], 0 \n" // bitSet(GPIO0, 0)
" rjmp INT0_vect_part_2 \n" // move on to part 2 of the ISR for INT0
:: [pin] "I" (_SFR_IO_ADDR(PIND)), // selected register to be tied to GPIOR0 (in this case: PIND)
[bit] "I" (2), // specific bit number in above register to be tied to first bit og GPIOR0
[gpio] "I" (_SFR_IO_ADDR(GPIOR0))); // selected GPIOR register (in this case: GPIOR0)
}

ISR(INT0_vect_part_2) {} // Part II of ISR for INT0 (on digital pin 2) - needed for compilation
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
PCA9536_RGB LIBRARY - BLINK COLOR EXAMPLE
-----------------------------------------
PCA9536_RGB LIBRARY - BLINK COLOR EXAMPLE (COMMON-CATHODE)
----------------------------------------------------------
INTRODUCTION
------------
Expand All @@ -25,26 +25,22 @@
GND --| |-- IO3
-------
PIN 1 (IO0) - Connect BLUE pin of a Common-Anode RGB Led via appropriate series resistor
PIN 2 (IO1) - Connect GREEN pin of a Common-Anode RGB Led via appropriate series resistor
PIN 3 (IO2) - Connect RED pin of a Common-Anode RGB Led via appropriate series resistor
This sketch should be used with the following hookup:
PIN 1 (IO0) - Connect to Common-Cathode RGB Led BLUE pin via appropriate series resistor
PIN 2 (IO1) - Connect to Common-Cathode RGB Led GREEN pin via appropriate series resistor
PIN 3 (IO2) - Connect to Common-Cathode RGB Led RED pin via appropriate series resistor
PIN 4 (GND) - Connect to Arduino GND
PIN 5 (IO3) - Leave unconnected for the purpose of this sketch
PIN 6 (SCL) - Connect to Arduino Pin A5 with a 2K2 (400MHz) or 10K (100MHz) pull-up resistor
PIN 7 (SDA) - Connect to Arduino Pin A4 with a 2K2 (400MHz) or 10K (100MHz) pull-up resistor
PIN 8 (VCC) - Connect to Arduino 5V output
DECOUPING - Connect a 0.1uF Ceramic Capacitor between the PCA9536's VCC & GND pins
RGB LED COMMON-ANODE PIN - Connect to Arduino 5V output
RGB LED COMMON-CATHODE PIN - Connect to Arduino GND
PUSH BUTTON SWITCH - connect a push-button switch between Arduino Digital Pin 2 and GND.
IMPORTANT: It is possible to connect any type of RGB Led (Commone-Anode / Common-Cathode)
to any of the PCA9536's I/O pins (IO0, IO1, IO2, IO3), but then it is necessary
to update the constructor's configuration accordingly in the sketch itself, and,
if it's a Common-Cathod type of Led, connect the Common-Cathode pin to Arduino GND
(rather than the Arduino 5V Output).
BUG REPORTS
-----------
Please report any bugs/issues/suggestions in the Github Repository of this library at:
Expand Down Expand Up @@ -74,7 +70,7 @@

#include "PCA9536_RGB.h"

PCA9536_RGB rgb(IO2, IO1, IO0, C_ANODE); // construct a new PCA9536_RGB instance
PCA9536_RGB rgb(IO2, IO1, IO0, C_CATHODE); // construct a new PCA9536_RGB instance

const color_t BLINK_COLOR = GREEN; // selected color to be blinked
const unsigned int BLINK_RATE = 600; // blink rate (= color 'ON' period; equal to 'OFF' period) in mS
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
PCA9536_RGB LIBRARY - USAGE EXAMPLE
-----------------------------------
PCA9536_RGB LIBRARY - USAGE EXAMPLE (COMMON-ANODE)
--------------------------------------------------
INTRODUCTION
------------
Expand All @@ -25,11 +25,11 @@
GND --| |-- IO3
-------
This hookup can be used with the following sketch:
This sketch should be used with the following hookup:
PIN 1 (IO0) - Connect BLUE pin of a Common-Anode RGB Led via appropriate series resistor
PIN 2 (IO1) - Connect GREEN pin of a Common-Anode RGB Led via appropriate series resistor
PIN 3 (IO2) - Connect RED pin of a Common-Anode RGB Led via appropriate series resistor
PIN 1 (IO0) - Connect to Common-Anode RGB Led BLUE pin via appropriate series resistor
PIN 2 (IO1) - Connect to Common-Anode RGB Led GREEN pin via appropriate series resistor
PIN 3 (IO2) - Connect to Common-Anode RGB Led RED pin via appropriate series resistor
PIN 4 (GND) - Connect to Arduino GND
PIN 5 (IO3) - Leave unconnected for the purpose of this sketch
PIN 6 (SCL) - Connect to Arduino Pin A5 with a 2K2 (400MHz) or 10K (100MHz) pull-up resistor
Expand All @@ -39,13 +39,9 @@
RGB LED COMMON-ANODE PIN - Connect to Arduino 5V output
IMPORTANT: It is possible to connect any type of RGB Led (Commone-Anode / Common-Cathode)
to any of the PCA9536's I/O pins (IO0, IO1, IO2, IO3), but then it is necessary
to update the constructor's configuration accordingly in the sketch itself.
RUNNING THE SKETCH
------------------
1. Hook-up the PCA9536 and RGB Led as instructed above
1. Hook-up the PCA9536 and RGB Led (Common-Cathode) as instructed above
2. Upload the sketch to the Arduino
3. (Optional) Open the Serial Port (make sure the baid-rate is to 9600)
4. Sit back and watch the pretty lights :-)
Expand Down Expand Up @@ -88,6 +84,7 @@ void setup() {
Serial.begin(9600);
Wire.begin();
rgb.init();
rgb.turnOff();
longDelay();
printDivider();
Serial.print(F("\nPCA9536 RGB LED LIBRARY USAGE\n"));
Expand Down
Loading

0 comments on commit e8b4af5

Please sign in to comment.