Class module for NCP5623B I2C LED driver

Dependents:   mDotEVBM2X MTDOT-EVB-LinkCheck-AL MTDOT-EVBDemo-DRH MTDOT_BOX_EVB_Blinky ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NCP5623B.cpp Source File

NCP5623B.cpp

Go to the documentation of this file.
00001 /**
00002  * @file    NCP5623B.cpp
00003  * @brief   Device driver - NCP5623B Triple LED Driver IC w/RTOS support
00004  * @author  Tim Barr
00005  * @version 1.0
00006  * @see     http://www.onsemi.com/pub/Collateral/NCP5623B-D.PDF
00007  *
00008  * Copyright (c) 2015
00009  *
00010  * Licensed under the Apache License, Version 2.0 (the "License");
00011  * you may not use this file except in compliance with the License.
00012  * You may obtain a copy of the License at
00013  *
00014  *     http://www.apache.org/licenses/LICENSE-2.0
00015  *
00016  * Unless required by applicable law or agreed to in writing, software
00017  * distributed under the License is distributed on an "AS IS" BASIS,
00018  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00019  * See the License for the specific language governing permissions and
00020  * limitations under the License.
00021  */
00022 
00023 #include "NCP5623B.h"
00024 #include "mbed_debug.h"
00025 #include "rtos.h"
00026 
00027 NCP5623B::NCP5623B(I2C &i2c)
00028 {
00029     _i2c = &i2c;
00030 
00031     NCP5623B::init();
00032 
00033     return;
00034 }
00035 
00036 uint8_t NCP5623B::init(void)
00037 {
00038     uint8_t result = 0;
00039 
00040     // Turn off all LEDs and initialize all registers
00041     result |= NCP5623B::writeRegister(NCP5623B::DIMDWNSET, 0x00);
00042     result |= NCP5623B::writeRegister(NCP5623B::DIMTIME, 0x01);
00043     osDelay (1000);
00044     result |= NCP5623B::writeRegister(NCP5623B::LEDCURR, 0x00);
00045     result |= NCP5623B::writeRegister(NCP5623B::PWMLED1, 0x00);
00046     result |= NCP5623B::writeRegister(NCP5623B::PWMLED2, 0x00);
00047     result |= NCP5623B::writeRegister(NCP5623B::PWMLED3, 0x00);
00048 
00049     return result;
00050 }
00051 
00052 /** Shutdown LEDS
00053  *  @return status of command
00054  */
00055 uint8_t NCP5623B::shutdown(void) const
00056 {
00057     uint8_t result = 0;
00058 
00059     result |= NCP5623B::writeRegister(NCP5623B::SHUTDWN, 0x00);
00060     return result;
00061 
00062 }
00063 
00064 /** Set static LED Current
00065  *  data - value of current draw for all LEDs range 0-31
00066  *  @return status of command
00067  */
00068 uint8_t NCP5623B::setLEDCurrent(uint8_t data) const
00069 {
00070     uint8_t result = 0;
00071 
00072     result |= NCP5623B::writeRegister(NCP5623B::LEDCURR, data);
00073     return result;
00074 }
00075 
00076 /** Set PWM mode for specific LED
00077  *  @lednum - selects LED
00078  *  @data - PWM value to set  range 0-31 0-100% Pulse width
00079  *  @return status of command
00080  */
00081 uint8_t NCP5623B::setPWM(LEDNUM lednum, int8_t data ) const
00082 {
00083     uint8_t result = 0;
00084 
00085     switch (lednum) {
00086         case NCP5623B::LED_1:
00087             result |= NCP5623B::writeRegister(NCP5623B::PWMLED1, data);
00088             break;
00089         case NCP5623B::LED_2:
00090             result |= NCP5623B::writeRegister(NCP5623B::PWMLED2, data);
00091             break;
00092         case NCP5623B::LED_3:
00093             result |= NCP5623B::writeRegister(NCP5623B::PWMLED3, data);
00094             break;
00095     }
00096     return result;
00097 }
00098 
00099 /** Set Dimming mode for all LEDs
00100  *  @dimdir - direction of dimming
00101  *  @endstep - ending step of ramp up or ramp down range 0-31
00102  *  @steptime - time per step range 0-31 in 8 msec multiples
00103  *  @return status of command
00104  */
00105 uint8_t NCP5623B::setDimming(DIMDIRECTION dimdir, uint8_t endstep, uint8_t steptime) const
00106 {
00107     uint8_t result = 0;
00108 
00109     if (dimdir == NCP5623B::DIMDWN)
00110         result |= NCP5623B::writeRegister(NCP5623B::DIMDWNSET, endstep);
00111     else
00112         result |= NCP5623B::writeRegister(NCP5623B::DIMUPSET, endstep);
00113 
00114     result |= NCP5623B::writeRegister(NCP5623B::DIMTIME, steptime);
00115 
00116     return result;
00117 }
00118 /** Write to a register (exposed for debugging reasons)
00119  * @param reg - The register to be written
00120  * @param data - The data to be written
00121  */
00122 uint8_t NCP5623B::writeRegister(NCP5623B::REGISTER const reg, uint8_t const data) const
00123 {
00124     char buf[1];
00125     uint8_t result = 0;
00126 
00127     buf[0] = reg | (data & NCP5623B::DATAMASK);
00128 
00129     result |= _i2c->write(_i2c_addr, buf, 1);
00130 
00131     if (result != 0)
00132         debug("NCP5623B::writeRegister error code: %d\r\n", result);
00133 
00134     return result;
00135 }