A feature complete driver for the PCA9952/55 LED driver from NXP.
Dependents: PCA9955_HelloWorld
Diff: PCA9955.cpp
- Revision:
- 12:2b8adb10c605
- Parent:
- 11:dbf20a128eb6
- Child:
- 13:275e5ea3dc5c
--- a/PCA9955.cpp Tue May 06 17:03:59 2014 +0000 +++ b/PCA9955.cpp Mon May 12 14:58:54 2014 +0000 @@ -320,7 +320,7 @@ float PCA9955::groupDuty() { //Return the value as a float - return groupDuty_char() / 255.0; + return groupDuty_u8() / 255.0; } void PCA9955::groupDuty(float duty, int altAddr) @@ -332,16 +332,16 @@ duty = 1.0; //Convert the value to a char and write it - groupDuty_char((char)(duty * 255.0), altAddr); + groupDuty_u8((char)(duty * 255.0), altAddr); } -char PCA9955::groupDuty_char() +char PCA9955::groupDuty_u8() { //Return the 8-bit register value return read(REG_GRPPWM); } -void PCA9955::groupDuty_char(char duty, int altAddr) +void PCA9955::groupDuty_u8(char duty, int altAddr) { //Write the new 8-bit register value write((altAddr == NULL) ? m_ADDR : altAddr, REG_GRPPWM, duty); @@ -350,7 +350,7 @@ float PCA9955::groupBlinkPeriod() { //Read the 8-bit register value - char value = groupBlinkPeriod_char(); + char value = groupBlinkPeriod_u8(); //Return the period in seconds if (value == 0x00) @@ -374,16 +374,16 @@ } //Write the new 8-bit register value - groupBlinkPeriod_char(value, altAddr); + groupBlinkPeriod_u8(value, altAddr); } -char PCA9955::groupBlinkPeriod_char() +char PCA9955::groupBlinkPeriod_u8() { //Return the 8-bit register value return read(REG_GRPFREQ); } -void PCA9955::groupBlinkPeriod_char(char period, int altAddr) +void PCA9955::groupBlinkPeriod_u8(char period, int altAddr) { //Write the new 8-bit register value write((altAddr == NULL) ? m_ADDR : altAddr, REG_GRPFREQ, period); @@ -392,7 +392,7 @@ float PCA9955::outputDuty(Output output) { //Return the value as a float - return outputDuty_char(output) / 255.0; + return outputDuty_u8(output) / 255.0; } void PCA9955::outputDuty(Output output, float duty, int altAddr) @@ -404,16 +404,16 @@ duty = 1.0; //Convert the value to a char and write it - outputDuty_char(output, (char)(duty * 255.0), altAddr); + outputDuty_u8(output, (char)(duty * 255.0), altAddr); } -char PCA9955::outputDuty_char(Output output) +char PCA9955::outputDuty_u8(Output output) { //Return the 8-bit register value return read(REG_PWM0 + (char)output); } -void PCA9955::outputDuty_char(Output output, char duty, int altAddr) +void PCA9955::outputDuty_u8(Output output, char duty, int altAddr) { //Write the new 8-bit register value write((altAddr == NULL) ? m_ADDR : altAddr, REG_PWM0 + (char)output, duty); @@ -422,7 +422,7 @@ float PCA9955::outputCurrent(Output output) { //Return the value as a float - return outputCurrent_char(output) / 255.0; + return outputCurrent_u8(output) / 255.0; } void PCA9955::outputCurrent(Output output, float iref, int altAddr) @@ -434,16 +434,16 @@ iref = 1.0; //Convert the value to a char and write it - outputCurrent_char(output, (char)(iref * 255.0), altAddr); + outputCurrent_u8(output, (char)(iref * 255.0), altAddr); } -char PCA9955::outputCurrent_char(Output output) +char PCA9955::outputCurrent_u8(Output output) { //Return the 8-bit register value return read(REG_IREF0 + (char)output); } -void PCA9955::outputCurrent_char(Output output, char iref, int altAddr) +void PCA9955::outputCurrent_u8(Output output, char iref, int altAddr) { //Write the new 8-bit register value write((altAddr == NULL) ? m_ADDR : altAddr, REG_IREF0 + (char)output, iref); @@ -509,6 +509,22 @@ write((altAddr == NULL) ? m_ADDR : altAddr, REG_ALLCALLADR, addr); } +void PCA9955::getOutputStates(OutputState* states, Output start, Output end) +{ + //Read the range of output states + for (int i = 0; i < end - start + 1; i++) { + states[i] = outputState((Output)(start + i)); + } +} + +void PCA9955::setOutputStates(OutputState* states, Output start, Output end) +{ + //Set the range of output states + for (int i = 0; i < end - start + 1; i++) { + outputState((Output)(start + i), states[i]); + } +} + void PCA9955::allOutputStates(OutputState state, int altAddr) { char buff[5]; @@ -529,26 +545,26 @@ writeMulti((altAddr == NULL) ? m_ADDR : altAddr, buff, 5); } -void PCA9955::getOutputDuties(float* duties) +void PCA9955::getOutputDuties(float* duties, Output start, Output end) { - char buff[16]; + char buff[end - start + 1]; - //Read all of the duty cycles as unsigned chars first - getOutputDuties_char(buff); + //Read the range of the duty cycles as unsigned chars first + getOutputDuties_u8(buff, start, end); //Convert all of the duty cycles to percents - for (int i = 0; i < 16; i++) { + for (int i = 0; i < end - start + 1; i++) { duties[i] = buff[i] / 255.0; } } -void PCA9955::setOutputDuties(float* duties, int altAddr) +void PCA9955::setOutputDuties(float* duties, Output start, Output end, int altAddr) { - char buff[17]; + char buff[end - start + 2]; //Assemble the sending array - buff[0] = REG_PWM0 | REG_AUTO_INC; - for (int i = 1; i < 17; i++) { + buff[0] = (REG_PWM0 + start) | REG_AUTO_INC; + for (int i = 1; i < end - start + 2; i++) { //Range check the value if (duties[i - 1] < 0.0) duties[i - 1] = 0.0; @@ -560,7 +576,7 @@ } //Send the array - writeMulti((altAddr == NULL) ? m_ADDR : altAddr, buff, 17); + writeMulti((altAddr == NULL) ? m_ADDR : altAddr, buff, end - start + 2); } void PCA9955::allOutputDuties(float duty, int altAddr) @@ -572,53 +588,53 @@ duty = 1.0; //Convert the value to a char and write it - allOutputDuties_char((char)(duty * 255.0), altAddr); + allOutputDuties_u8((char)(duty * 255.0), altAddr); } -void PCA9955::getOutputDuties_char(char* duties) +void PCA9955::getOutputDuties_u8(char* duties, Output start, Output end) { //Read all of the duty cycles at once - readMulti(REG_PWM0 | REG_AUTO_INC, duties, 16); + readMulti((REG_PWM0 + start) | REG_AUTO_INC, duties, end - start + 1); } -void PCA9955::setOutputDuties_char(char* duties, int altAddr) +void PCA9955::setOutputDuties_u8(char* duties, Output start, Output end, int altAddr) { - char buff[17]; + char buff[end - start + 2]; //Assemble the sending array - buff[0] = REG_PWM0 | REG_AUTO_INC; - memcpy(buff + 1, duties, 16); + buff[0] = (REG_PWM0 + start) | REG_AUTO_INC; + memcpy(buff + 1, duties, end - start + 1); //Send the array - writeMulti((altAddr == NULL) ? m_ADDR : altAddr, buff, 17); + writeMulti((altAddr == NULL) ? m_ADDR : altAddr, buff, end - start + 2); } -void PCA9955::allOutputDuties_char(char duty, int altAddr) +void PCA9955::allOutputDuties_u8(char duty, int altAddr) { //Write the new 8-bit register value write((altAddr == NULL) ? m_ADDR : altAddr, REG_PWMALL, duty); } -void PCA9955::getOutputCurrents(float* irefs) +void PCA9955::getOutputCurrents(float* irefs, Output start, Output end) { - char buff[16]; + char buff[end - start + 1]; //Read all of the current references as unsigned chars first - getOutputCurrents_char(buff); + getOutputCurrents_u8(buff, start, end); //Convert all of the duty cycles to percents - for (int i = 0; i < 16; i++) { + for (int i = 0; i < end - start + 1; i++) { irefs[i] = buff[i] / 255.0; } } -void PCA9955::setOutputCurrents(float* irefs, int altAddr) +void PCA9955::setOutputCurrents(float* irefs, Output start, Output end, int altAddr) { - char buff[17]; + char buff[end - start + 2]; //Assemble the sending array - buff[0] = REG_IREF0 | REG_AUTO_INC; - for (int i = 1; i < 17; i++) { + buff[0] = (REG_IREF0 + start) | REG_AUTO_INC; + for (int i = 1; i < end - start + 2; i++) { //Range check the value if (irefs[i - 1] < 0.0) irefs[i - 1] = 0.0; @@ -630,7 +646,7 @@ } //Send the array - writeMulti((altAddr == NULL) ? m_ADDR : altAddr, buff, 17); + writeMulti((altAddr == NULL) ? m_ADDR : altAddr, buff, end - start + 2); } void PCA9955::allOutputCurrents(float iref, int altAddr) @@ -642,28 +658,28 @@ iref = 1.0; //Convert the value to a char and write it - allOutputCurrents_char((char)(iref * 255.0), altAddr); + allOutputCurrents_u8((char)(iref * 255.0), altAddr); } -void PCA9955::getOutputCurrents_char(char* irefs) +void PCA9955::getOutputCurrents_u8(char* irefs, Output start, Output end) { //Read all of the current references at once - readMulti(REG_IREF0 | REG_AUTO_INC, irefs, 16); + readMulti((REG_IREF0 + start) | REG_AUTO_INC, irefs, end - start + 1); } -void PCA9955::setOutputCurrents_char(char* irefs, int altAddr) +void PCA9955::setOutputCurrents_u8(char* irefs, Output start, Output end, int altAddr) { - char buff[17]; + char buff[end - start + 2]; //Assemble the sending array - buff[0] = REG_IREF0 | REG_AUTO_INC; - memcpy(buff + 1, irefs, 16); + buff[0] = (REG_IREF0 + start) | REG_AUTO_INC; + memcpy(buff + 1, irefs, end - start + 1); //Send the array - writeMulti((altAddr == NULL) ? m_ADDR : altAddr, buff, 17); + writeMulti((altAddr == NULL) ? m_ADDR : altAddr, buff, end - start + 2); } -void PCA9955::allOutputCurrents_char(char iref, int altAddr) +void PCA9955::allOutputCurrents_u8(char iref, int altAddr) { //Write the new 8-bit register value write((altAddr == NULL) ? m_ADDR : altAddr, REG_IREFALL, iref);