pca9685: Initial implementation

This library implements generic support for the pca9685 16 channel 12
bit PWM LED controller.  This controller is used on some Adafruit
motor shields.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Zion Orent <zorent@ics.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Jon Trulson
2015-02-26 18:27:24 -07:00
committed by Mihai Tudor Panu
parent 970d6a083f
commit 2aa24162c3
8 changed files with 791 additions and 0 deletions

View File

@ -98,6 +98,7 @@ add_executable (l298-example l298.cxx)
add_executable (l298-stepper-example l298-stepper.cxx)
add_executable (at42qt1070-example at42qt1070.cxx)
add_executable (grovemd-example grovemd.cxx)
add_executable (pca9685-example pca9685.cxx)
include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@ -177,6 +178,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/ina132)
include_directories (${PROJECT_SOURCE_DIR}/src/l298)
include_directories (${PROJECT_SOURCE_DIR}/src/at42qt1070)
include_directories (${PROJECT_SOURCE_DIR}/src/grovemd)
include_directories (${PROJECT_SOURCE_DIR}/src/pca9685)
target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@ -276,3 +278,4 @@ target_link_libraries (l298-example l298 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (l298-stepper-example l298 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (at42qt1070-example at42qt1070 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (grovemd-example grovemd ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (pca9685-example pca9685 ${CMAKE_THREAD_LIBS_INIT})

78
examples/c++/pca9685.cxx Normal file
View File

@ -0,0 +1,78 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2015 Intel Corporation.
*
* 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 <unistd.h>
#include <signal.h>
#include <iostream>
#include "pca9685.h"
using namespace std;
int main(int argc, char **argv)
{
//! [Interesting]
// Instantiate an PCA9685 on I2C bus 0
upm::PCA9685 *leds = new upm::PCA9685(PCA9685_I2C_BUS,
PCA9685_DEFAULT_I2C_ADDR);
// put device to sleep
leds->setModeSleep(true);
// setup a period of 50Hz
leds->setPrescaleFromHz(50);
// wake device up
leds->setModeSleep(false);
// Setup a 50% duty cycle -- on time at 0, off time at 2048 (4096 / 2)
// Set for all channels
leds->ledOnTime(PCA9685_ALL_LED, 0);
leds->ledOffTime(PCA9685_ALL_LED, 2048);
// but, turn channel 3 full off and channel 4 full on
cout << "Turning channel 3 off, and channel 4 on." << endl;
cout << "All other channels will be PWM'd at a 50% duty cycle." << endl;
leds->ledFullOff(3, true);
leds->ledFullOn(4, true);
// now, just sleep for 5 seconds, reset channels 3 and 4, and exit.
cout << "Sleeping for 5 seconds..." << endl;
sleep(5);
cout << "Exiting..." << endl;
// clear the bits we set earlier
leds->ledFullOff(3, false);
leds->ledFullOn(4, false);
//! [Interesting]
delete leds;
return 0;
}

View File

@ -0,0 +1,85 @@
/*jslint node:true, vars:true, bitwise:true, unparam:true */
/*jshint unused:true */
/*
* Author: Zion Orent <zorent@ics.com>
* Copyright (c) 2015 Intel Corporation.
*
* 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.
*/
function exit()
{
console.log("Exiting");
if (myLEDController_obj)
{
// clear the bits we set earlier
myLEDController_obj.ledFullOff(3, false);
myLEDController_obj.ledFullOn(4, false);
}
myLEDController_obj = null;
if (LEDController_lib)
{
LEDController_lib.cleanUp();
LEDController_lib = null;
}
process.exit(0);
}
// The pca9685 is an led controller.
// It's being used in this case to drive motors.
var LEDController_lib = require('jsupm_pca9685');
var I2CBus = LEDController_lib.PCA9685_I2C_BUS;
var I2CAddr = LEDController_lib.PCA9685_DEFAULT_I2C_ADDR;
// Instantiate an PCA9685 on I2C bus 0
var myLEDController_obj = new LEDController_lib.PCA9685(I2CBus, I2CAddr);
// put device to sleep
myLEDController_obj.setModeSleep(true);
// setup a period of 50Hz
myLEDController_obj.setPrescaleFromHz(50);
// wake device up
myLEDController_obj.setModeSleep(false);
// Setup a 50% duty cycle -- on time at 0, off time at 2048 (4096 / 2)
// Set for all channels
var LEDNum = LEDController_lib.PCA9685_ALL_LED;
myLEDController_obj.ledOnTime(LEDNum, 0);
myLEDController_obj.ledOffTime(LEDNum, 2048);
// but, turn channel 3 full off and channel 4 full on
console.log("Turning channel 3 off, and channel 4 on.");
console.log("All other channels will be PWM'd at a 50% duty cycle.");
myLEDController_obj.ledFullOff(3, true);
myLEDController_obj.ledFullOn(4, true);
// now, just sleep for 5 seconds, reset channels 3 and 4, and exit.
console.log("Sleeping for 5 seconds...");
setTimeout(exit, 5000);
process.on('SIGINT', function()
{
exit();
});