A feature complete driver for the PCA9952/55 LED driver from NXP.

Dependents:   PCA9955_HelloWorld

Committer:
neilt6
Date:
Tue Nov 05 21:16:25 2013 +0000
Revision:
0:7b3cbb5a53b8
Child:
1:016f916c5579
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
neilt6 0:7b3cbb5a53b8 1 /* PCA9955 Driver Library
neilt6 0:7b3cbb5a53b8 2 * Copyright (c) 2013 Neil Thiessen
neilt6 0:7b3cbb5a53b8 3 *
neilt6 0:7b3cbb5a53b8 4 * Licensed under the Apache License, Version 2.0 (the "License");
neilt6 0:7b3cbb5a53b8 5 * you may not use this file except in compliance with the License.
neilt6 0:7b3cbb5a53b8 6 * You may obtain a copy of the License at
neilt6 0:7b3cbb5a53b8 7 *
neilt6 0:7b3cbb5a53b8 8 * http://www.apache.org/licenses/LICENSE-2.0
neilt6 0:7b3cbb5a53b8 9 *
neilt6 0:7b3cbb5a53b8 10 * Unless required by applicable law or agreed to in writing, software
neilt6 0:7b3cbb5a53b8 11 * distributed under the License is distributed on an "AS IS" BASIS,
neilt6 0:7b3cbb5a53b8 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
neilt6 0:7b3cbb5a53b8 13 * See the License for the specific language governing permissions and
neilt6 0:7b3cbb5a53b8 14 * limitations under the License.
neilt6 0:7b3cbb5a53b8 15 */
neilt6 0:7b3cbb5a53b8 16
neilt6 0:7b3cbb5a53b8 17 #include "PCA9955.h"
neilt6 0:7b3cbb5a53b8 18
neilt6 0:7b3cbb5a53b8 19 PCA9955::PCA9955(PinName sda, PinName scl, Address addr) : m_I2C(sda, scl)
neilt6 0:7b3cbb5a53b8 20 {
neilt6 0:7b3cbb5a53b8 21 //Set the internal device address
neilt6 0:7b3cbb5a53b8 22 m_Addr = (int)addr;
neilt6 0:7b3cbb5a53b8 23 }
neilt6 0:7b3cbb5a53b8 24
neilt6 0:7b3cbb5a53b8 25 bool PCA9955::open(void)
neilt6 0:7b3cbb5a53b8 26 {
neilt6 0:7b3cbb5a53b8 27 //Probe for the PCA9952/55 using a Zero Length Transfer
neilt6 0:7b3cbb5a53b8 28 if (!m_I2C.write(m_Addr, NULL, 0)) {
neilt6 0:7b3cbb5a53b8 29 //NOTE: We don't reset here as SWRST may reset other I2C devices as well
neilt6 0:7b3cbb5a53b8 30
neilt6 0:7b3cbb5a53b8 31 //Return success
neilt6 0:7b3cbb5a53b8 32 return true;
neilt6 0:7b3cbb5a53b8 33 } else {
neilt6 0:7b3cbb5a53b8 34 //Return failure
neilt6 0:7b3cbb5a53b8 35 return false;
neilt6 0:7b3cbb5a53b8 36 }
neilt6 0:7b3cbb5a53b8 37 }
neilt6 0:7b3cbb5a53b8 38
neilt6 0:7b3cbb5a53b8 39 void PCA9955::reset(void)
neilt6 0:7b3cbb5a53b8 40 {
neilt6 0:7b3cbb5a53b8 41 //The SWRST magic data byte
neilt6 0:7b3cbb5a53b8 42 char data = 0x06;
neilt6 0:7b3cbb5a53b8 43
neilt6 0:7b3cbb5a53b8 44 //Issue the SWRST call to the General Call address
neilt6 0:7b3cbb5a53b8 45 m_I2C.write(0x00, &data, 1);
neilt6 0:7b3cbb5a53b8 46
neilt6 0:7b3cbb5a53b8 47 //Wait for 10ms to allow the device to reset
neilt6 0:7b3cbb5a53b8 48 wait_ms(10);
neilt6 0:7b3cbb5a53b8 49 }
neilt6 0:7b3cbb5a53b8 50
neilt6 0:7b3cbb5a53b8 51 bool PCA9955::allCallEnabled(void)
neilt6 0:7b3cbb5a53b8 52 {
neilt6 0:7b3cbb5a53b8 53 //Read the 8-bit register value
neilt6 0:7b3cbb5a53b8 54 char value = read(REG_MODE1);
neilt6 0:7b3cbb5a53b8 55
neilt6 0:7b3cbb5a53b8 56 //Return the status of the ALLCALL bit
neilt6 0:7b3cbb5a53b8 57 if (value & (1 << 0))
neilt6 0:7b3cbb5a53b8 58 return true;
neilt6 0:7b3cbb5a53b8 59 else
neilt6 0:7b3cbb5a53b8 60 return false;
neilt6 0:7b3cbb5a53b8 61 }
neilt6 0:7b3cbb5a53b8 62
neilt6 0:7b3cbb5a53b8 63 void PCA9955::allCallEnabled(bool enabled)
neilt6 0:7b3cbb5a53b8 64 {
neilt6 0:7b3cbb5a53b8 65 //Read the current 8-bit register value
neilt6 0:7b3cbb5a53b8 66 char value = read(REG_MODE1);
neilt6 0:7b3cbb5a53b8 67
neilt6 0:7b3cbb5a53b8 68 //Set or clear the ALLCALL bit
neilt6 0:7b3cbb5a53b8 69 if (enabled)
neilt6 0:7b3cbb5a53b8 70 value |= (1 << 0);
neilt6 0:7b3cbb5a53b8 71 else
neilt6 0:7b3cbb5a53b8 72 value &= ~(1 << 0);
neilt6 0:7b3cbb5a53b8 73
neilt6 0:7b3cbb5a53b8 74 //Write the value back out
neilt6 0:7b3cbb5a53b8 75 write(REG_MODE1, value);
neilt6 0:7b3cbb5a53b8 76 }
neilt6 0:7b3cbb5a53b8 77
neilt6 0:7b3cbb5a53b8 78 bool PCA9955::subCall3Enabled(void)
neilt6 0:7b3cbb5a53b8 79 {
neilt6 0:7b3cbb5a53b8 80 //Read the 8-bit register value
neilt6 0:7b3cbb5a53b8 81 char value = read(REG_MODE1);
neilt6 0:7b3cbb5a53b8 82
neilt6 0:7b3cbb5a53b8 83 //Return the status of the SUB3 bit
neilt6 0:7b3cbb5a53b8 84 if (value & (1 << 1))
neilt6 0:7b3cbb5a53b8 85 return true;
neilt6 0:7b3cbb5a53b8 86 else
neilt6 0:7b3cbb5a53b8 87 return false;
neilt6 0:7b3cbb5a53b8 88 }
neilt6 0:7b3cbb5a53b8 89
neilt6 0:7b3cbb5a53b8 90 void PCA9955::subCall3Enabled(bool enabled)
neilt6 0:7b3cbb5a53b8 91 {
neilt6 0:7b3cbb5a53b8 92 //Read the current 8-bit register value
neilt6 0:7b3cbb5a53b8 93 char value = read(REG_MODE1);
neilt6 0:7b3cbb5a53b8 94
neilt6 0:7b3cbb5a53b8 95 //Set or clear the SUB3 bit
neilt6 0:7b3cbb5a53b8 96 if (enabled)
neilt6 0:7b3cbb5a53b8 97 value |= (1 << 1);
neilt6 0:7b3cbb5a53b8 98 else
neilt6 0:7b3cbb5a53b8 99 value &= ~(1 << 1);
neilt6 0:7b3cbb5a53b8 100
neilt6 0:7b3cbb5a53b8 101 //Write the value back out
neilt6 0:7b3cbb5a53b8 102 write(REG_MODE1, value);
neilt6 0:7b3cbb5a53b8 103 }
neilt6 0:7b3cbb5a53b8 104
neilt6 0:7b3cbb5a53b8 105 bool PCA9955::subCall2Enabled(void)
neilt6 0:7b3cbb5a53b8 106 {
neilt6 0:7b3cbb5a53b8 107 //Read the 8-bit register value
neilt6 0:7b3cbb5a53b8 108 char value = read(REG_MODE1);
neilt6 0:7b3cbb5a53b8 109
neilt6 0:7b3cbb5a53b8 110 //Return the status of the SUB2 bit
neilt6 0:7b3cbb5a53b8 111 if (value & (1 << 2))
neilt6 0:7b3cbb5a53b8 112 return true;
neilt6 0:7b3cbb5a53b8 113 else
neilt6 0:7b3cbb5a53b8 114 return false;
neilt6 0:7b3cbb5a53b8 115 }
neilt6 0:7b3cbb5a53b8 116
neilt6 0:7b3cbb5a53b8 117 void PCA9955::subCall2Enabled(bool enabled)
neilt6 0:7b3cbb5a53b8 118 {
neilt6 0:7b3cbb5a53b8 119 //Read the current 8-bit register value
neilt6 0:7b3cbb5a53b8 120 char value = read(REG_MODE1);
neilt6 0:7b3cbb5a53b8 121
neilt6 0:7b3cbb5a53b8 122 //Set or clear the SUB2 bit
neilt6 0:7b3cbb5a53b8 123 if (enabled)
neilt6 0:7b3cbb5a53b8 124 value |= (1 << 2);
neilt6 0:7b3cbb5a53b8 125 else
neilt6 0:7b3cbb5a53b8 126 value &= ~(1 << 2);
neilt6 0:7b3cbb5a53b8 127
neilt6 0:7b3cbb5a53b8 128 //Write the value back out
neilt6 0:7b3cbb5a53b8 129 write(REG_MODE1, value);
neilt6 0:7b3cbb5a53b8 130 }
neilt6 0:7b3cbb5a53b8 131
neilt6 0:7b3cbb5a53b8 132 bool PCA9955::subCall1Enabled(void)
neilt6 0:7b3cbb5a53b8 133 {
neilt6 0:7b3cbb5a53b8 134 //Read the 8-bit register value
neilt6 0:7b3cbb5a53b8 135 char value = read(REG_MODE1);
neilt6 0:7b3cbb5a53b8 136
neilt6 0:7b3cbb5a53b8 137 //Return the status of the SUB1 bit
neilt6 0:7b3cbb5a53b8 138 if (value & (1 << 3))
neilt6 0:7b3cbb5a53b8 139 return true;
neilt6 0:7b3cbb5a53b8 140 else
neilt6 0:7b3cbb5a53b8 141 return false;
neilt6 0:7b3cbb5a53b8 142 }
neilt6 0:7b3cbb5a53b8 143
neilt6 0:7b3cbb5a53b8 144 void PCA9955::subCall1Enabled(bool enabled)
neilt6 0:7b3cbb5a53b8 145 {
neilt6 0:7b3cbb5a53b8 146 //Read the current 8-bit register value
neilt6 0:7b3cbb5a53b8 147 char value = read(REG_MODE1);
neilt6 0:7b3cbb5a53b8 148
neilt6 0:7b3cbb5a53b8 149 //Set or clear the SUB1 bit
neilt6 0:7b3cbb5a53b8 150 if (enabled)
neilt6 0:7b3cbb5a53b8 151 value |= (1 << 3);
neilt6 0:7b3cbb5a53b8 152 else
neilt6 0:7b3cbb5a53b8 153 value &= ~(1 << 3);
neilt6 0:7b3cbb5a53b8 154
neilt6 0:7b3cbb5a53b8 155 //Write the value back out
neilt6 0:7b3cbb5a53b8 156 write(REG_MODE1, value);
neilt6 0:7b3cbb5a53b8 157 }
neilt6 0:7b3cbb5a53b8 158
neilt6 0:7b3cbb5a53b8 159 PCA9955::PowerMode PCA9955::powerMode(void)
neilt6 0:7b3cbb5a53b8 160 {
neilt6 0:7b3cbb5a53b8 161 //Read the 8-bit register value
neilt6 0:7b3cbb5a53b8 162 char value = read(REG_MODE1);
neilt6 0:7b3cbb5a53b8 163
neilt6 0:7b3cbb5a53b8 164 //Return the status of the SLEEP bit
neilt6 0:7b3cbb5a53b8 165 if (value & (1 << 4))
neilt6 0:7b3cbb5a53b8 166 return POWER_SHUTDOWN;
neilt6 0:7b3cbb5a53b8 167 else
neilt6 0:7b3cbb5a53b8 168 return POWER_NORMAL;
neilt6 0:7b3cbb5a53b8 169 }
neilt6 0:7b3cbb5a53b8 170
neilt6 0:7b3cbb5a53b8 171 void PCA9955::powerMode(PowerMode mode)
neilt6 0:7b3cbb5a53b8 172 {
neilt6 0:7b3cbb5a53b8 173 //Read the current 8-bit register value
neilt6 0:7b3cbb5a53b8 174 char value = read(REG_MODE1);
neilt6 0:7b3cbb5a53b8 175
neilt6 0:7b3cbb5a53b8 176 //Set or clear the SLEEP bit
neilt6 0:7b3cbb5a53b8 177 if (mode == POWER_SHUTDOWN)
neilt6 0:7b3cbb5a53b8 178 value |= (1 << 4);
neilt6 0:7b3cbb5a53b8 179 else
neilt6 0:7b3cbb5a53b8 180 value &= ~(1 << 4);
neilt6 0:7b3cbb5a53b8 181
neilt6 0:7b3cbb5a53b8 182 //Write the value back out
neilt6 0:7b3cbb5a53b8 183 write(REG_MODE1, value);
neilt6 0:7b3cbb5a53b8 184 }
neilt6 0:7b3cbb5a53b8 185
neilt6 0:7b3cbb5a53b8 186 PCA9955::OutputChangeMode PCA9955::outputChangeMode(void)
neilt6 0:7b3cbb5a53b8 187 {
neilt6 0:7b3cbb5a53b8 188 //Read the 8-bit register value
neilt6 0:7b3cbb5a53b8 189 char value = read(REG_MODE2);
neilt6 0:7b3cbb5a53b8 190
neilt6 0:7b3cbb5a53b8 191 //Return the status of the OCH bit
neilt6 0:7b3cbb5a53b8 192 if (value & (1 << 3))
neilt6 0:7b3cbb5a53b8 193 return OUTPUT_CHANGE_ON_ACK;
neilt6 0:7b3cbb5a53b8 194 else
neilt6 0:7b3cbb5a53b8 195 return OUTPUT_CHANGE_ON_STOP;
neilt6 0:7b3cbb5a53b8 196 }
neilt6 0:7b3cbb5a53b8 197
neilt6 0:7b3cbb5a53b8 198 void PCA9955::outputChangeMode(OutputChangeMode mode)
neilt6 0:7b3cbb5a53b8 199 {
neilt6 0:7b3cbb5a53b8 200 //Read the current 8-bit register value
neilt6 0:7b3cbb5a53b8 201 char value = read(REG_MODE2);
neilt6 0:7b3cbb5a53b8 202
neilt6 0:7b3cbb5a53b8 203 //Set or clear the OCH bit
neilt6 0:7b3cbb5a53b8 204 if (mode == OUTPUT_CHANGE_ON_ACK)
neilt6 0:7b3cbb5a53b8 205 value |= (1 << 3);
neilt6 0:7b3cbb5a53b8 206 else
neilt6 0:7b3cbb5a53b8 207 value &= ~(1 << 3);
neilt6 0:7b3cbb5a53b8 208
neilt6 0:7b3cbb5a53b8 209 //Write the value back out
neilt6 0:7b3cbb5a53b8 210 write(REG_MODE2, value);
neilt6 0:7b3cbb5a53b8 211 }
neilt6 0:7b3cbb5a53b8 212
neilt6 0:7b3cbb5a53b8 213 PCA9955::GroupMode PCA9955::groupMode(void)
neilt6 0:7b3cbb5a53b8 214 {
neilt6 0:7b3cbb5a53b8 215 //Read the 8-bit register value
neilt6 0:7b3cbb5a53b8 216 char value = read(REG_MODE2);
neilt6 0:7b3cbb5a53b8 217
neilt6 0:7b3cbb5a53b8 218 //Return the status of the DMBLNK bit
neilt6 0:7b3cbb5a53b8 219 if (value & (1 << 5))
neilt6 0:7b3cbb5a53b8 220 return GROUP_BLINKING;
neilt6 0:7b3cbb5a53b8 221 else
neilt6 0:7b3cbb5a53b8 222 return GROUP_DIMMING;
neilt6 0:7b3cbb5a53b8 223 }
neilt6 0:7b3cbb5a53b8 224
neilt6 0:7b3cbb5a53b8 225 void PCA9955::groupMode(GroupMode mode)
neilt6 0:7b3cbb5a53b8 226 {
neilt6 0:7b3cbb5a53b8 227 //Read the current 8-bit register value
neilt6 0:7b3cbb5a53b8 228 char value = read(REG_MODE2);
neilt6 0:7b3cbb5a53b8 229
neilt6 0:7b3cbb5a53b8 230 //Set or clear the DMBLNK bit
neilt6 0:7b3cbb5a53b8 231 if (mode == GROUP_BLINKING)
neilt6 0:7b3cbb5a53b8 232 value |= (1 << 5);
neilt6 0:7b3cbb5a53b8 233 else
neilt6 0:7b3cbb5a53b8 234 value &= ~(1 << 5);
neilt6 0:7b3cbb5a53b8 235
neilt6 0:7b3cbb5a53b8 236 //Write the value back out
neilt6 0:7b3cbb5a53b8 237 write(REG_MODE2, value);
neilt6 0:7b3cbb5a53b8 238 }
neilt6 0:7b3cbb5a53b8 239
neilt6 0:7b3cbb5a53b8 240 bool PCA9955::overTemp(void)
neilt6 0:7b3cbb5a53b8 241 {
neilt6 0:7b3cbb5a53b8 242 //Read the current 8-bit register value
neilt6 0:7b3cbb5a53b8 243 char value = read(REG_MODE2);
neilt6 0:7b3cbb5a53b8 244
neilt6 0:7b3cbb5a53b8 245 //Return the status of the OVERTEMP bit
neilt6 0:7b3cbb5a53b8 246 if (value & (1 << 7))
neilt6 0:7b3cbb5a53b8 247 return true;
neilt6 0:7b3cbb5a53b8 248 else
neilt6 0:7b3cbb5a53b8 249 return false;
neilt6 0:7b3cbb5a53b8 250 }
neilt6 0:7b3cbb5a53b8 251
neilt6 0:7b3cbb5a53b8 252 PCA9955::OutputState PCA9955::outputState(Output output)
neilt6 0:7b3cbb5a53b8 253 {
neilt6 0:7b3cbb5a53b8 254 char value;
neilt6 0:7b3cbb5a53b8 255 char reg;
neilt6 0:7b3cbb5a53b8 256
neilt6 0:7b3cbb5a53b8 257 //Determine which register to read
neilt6 0:7b3cbb5a53b8 258 if (output < 4) {
neilt6 0:7b3cbb5a53b8 259 reg = REG_LEDOUT0;
neilt6 0:7b3cbb5a53b8 260 } else if (output < 8) {
neilt6 0:7b3cbb5a53b8 261 output = (Output)(output - 4);
neilt6 0:7b3cbb5a53b8 262 reg = REG_LEDOUT1;
neilt6 0:7b3cbb5a53b8 263 } else if (output < 12) {
neilt6 0:7b3cbb5a53b8 264 output = (Output)(output - 8);
neilt6 0:7b3cbb5a53b8 265 reg = REG_LEDOUT2;
neilt6 0:7b3cbb5a53b8 266 } else {
neilt6 0:7b3cbb5a53b8 267 output = (Output)(output - 12);
neilt6 0:7b3cbb5a53b8 268 reg = REG_LEDOUT3;
neilt6 0:7b3cbb5a53b8 269 }
neilt6 0:7b3cbb5a53b8 270
neilt6 0:7b3cbb5a53b8 271 //Read the 8-bit register value
neilt6 0:7b3cbb5a53b8 272 value = read(reg);
neilt6 0:7b3cbb5a53b8 273
neilt6 0:7b3cbb5a53b8 274 //Shift and mask the other output states
neilt6 0:7b3cbb5a53b8 275 value = (value >> (output * 2)) & 0x03;
neilt6 0:7b3cbb5a53b8 276
neilt6 0:7b3cbb5a53b8 277 //Return the selected output's state
neilt6 0:7b3cbb5a53b8 278 if (value == 0)
neilt6 0:7b3cbb5a53b8 279 return OUTPUT_OFF;
neilt6 0:7b3cbb5a53b8 280 else if (value == 1)
neilt6 0:7b3cbb5a53b8 281 return OUTPUT_ON;
neilt6 0:7b3cbb5a53b8 282 else if (value == 2)
neilt6 0:7b3cbb5a53b8 283 return OUTPUT_PWM;
neilt6 0:7b3cbb5a53b8 284 else
neilt6 0:7b3cbb5a53b8 285 return OUTPUT_PWM_GRPPWM;
neilt6 0:7b3cbb5a53b8 286 }
neilt6 0:7b3cbb5a53b8 287
neilt6 0:7b3cbb5a53b8 288 void PCA9955::outputState(Output output, OutputState state)
neilt6 0:7b3cbb5a53b8 289 {
neilt6 0:7b3cbb5a53b8 290 char value;
neilt6 0:7b3cbb5a53b8 291 char reg;
neilt6 0:7b3cbb5a53b8 292
neilt6 0:7b3cbb5a53b8 293 //Determine which register to read
neilt6 0:7b3cbb5a53b8 294 if (output < 4) {
neilt6 0:7b3cbb5a53b8 295 reg = REG_LEDOUT0;
neilt6 0:7b3cbb5a53b8 296 } else if (output < 8) {
neilt6 0:7b3cbb5a53b8 297 output = (Output)(output - 4);
neilt6 0:7b3cbb5a53b8 298 reg = REG_LEDOUT1;
neilt6 0:7b3cbb5a53b8 299 } else if (output < 12) {
neilt6 0:7b3cbb5a53b8 300 output = (Output)(output - 8);
neilt6 0:7b3cbb5a53b8 301 reg = REG_LEDOUT2;
neilt6 0:7b3cbb5a53b8 302 } else {
neilt6 0:7b3cbb5a53b8 303 output = (Output)(output - 12);
neilt6 0:7b3cbb5a53b8 304 reg = REG_LEDOUT3;
neilt6 0:7b3cbb5a53b8 305 }
neilt6 0:7b3cbb5a53b8 306
neilt6 0:7b3cbb5a53b8 307 //Read the current 8-bit register value
neilt6 0:7b3cbb5a53b8 308 value = read(reg);
neilt6 0:7b3cbb5a53b8 309
neilt6 0:7b3cbb5a53b8 310 //Mask off the old output state (also turns the output off)
neilt6 0:7b3cbb5a53b8 311 value &= ~(0x03 << (output * 2));
neilt6 0:7b3cbb5a53b8 312
neilt6 0:7b3cbb5a53b8 313 //Add the new output state
neilt6 0:7b3cbb5a53b8 314 if (state == OUTPUT_ON)
neilt6 0:7b3cbb5a53b8 315 value |= (1 << (output * 2));
neilt6 0:7b3cbb5a53b8 316 else if (state == OUTPUT_PWM)
neilt6 0:7b3cbb5a53b8 317 value |= (2 << (output * 2));
neilt6 0:7b3cbb5a53b8 318 else if (state == OUTPUT_PWM_GRPPWM)
neilt6 0:7b3cbb5a53b8 319 value |= (3 << (output * 2));
neilt6 0:7b3cbb5a53b8 320
neilt6 0:7b3cbb5a53b8 321 //Write the value back out
neilt6 0:7b3cbb5a53b8 322 write(reg, value);
neilt6 0:7b3cbb5a53b8 323 }
neilt6 0:7b3cbb5a53b8 324
neilt6 0:7b3cbb5a53b8 325 float PCA9955::groupDuty(void)
neilt6 0:7b3cbb5a53b8 326 {
neilt6 0:7b3cbb5a53b8 327 //Return the value as a float
neilt6 0:7b3cbb5a53b8 328 return groupDuty_char() / 255.0f;
neilt6 0:7b3cbb5a53b8 329 }
neilt6 0:7b3cbb5a53b8 330
neilt6 0:7b3cbb5a53b8 331 void PCA9955::groupDuty(float duty)
neilt6 0:7b3cbb5a53b8 332 {
neilt6 0:7b3cbb5a53b8 333 //Range check the value
neilt6 0:7b3cbb5a53b8 334 if (duty < 0.0f)
neilt6 0:7b3cbb5a53b8 335 duty = 0.0f;
neilt6 0:7b3cbb5a53b8 336 if (duty > 1.0f)
neilt6 0:7b3cbb5a53b8 337 duty = 1.0f;
neilt6 0:7b3cbb5a53b8 338
neilt6 0:7b3cbb5a53b8 339 //Convert the value to a char and write it
neilt6 0:7b3cbb5a53b8 340 groupDuty_char((char)(duty * 255.0f));
neilt6 0:7b3cbb5a53b8 341 }
neilt6 0:7b3cbb5a53b8 342
neilt6 0:7b3cbb5a53b8 343 char PCA9955::groupDuty_char(void)
neilt6 0:7b3cbb5a53b8 344 {
neilt6 0:7b3cbb5a53b8 345 //Return the 8-bit register value
neilt6 0:7b3cbb5a53b8 346 return read(REG_GRPPWM);
neilt6 0:7b3cbb5a53b8 347 }
neilt6 0:7b3cbb5a53b8 348
neilt6 0:7b3cbb5a53b8 349 void PCA9955::groupDuty_char(char duty)
neilt6 0:7b3cbb5a53b8 350 {
neilt6 0:7b3cbb5a53b8 351 //Write the new 8-bit register value
neilt6 0:7b3cbb5a53b8 352 write(REG_GRPPWM, duty);
neilt6 0:7b3cbb5a53b8 353 }
neilt6 0:7b3cbb5a53b8 354
neilt6 0:7b3cbb5a53b8 355 float PCA9955::groupBlinkPeriod(void)
neilt6 0:7b3cbb5a53b8 356 {
neilt6 0:7b3cbb5a53b8 357 //Read the 8-bit register value
neilt6 0:7b3cbb5a53b8 358 char value = groupBlinkPeriod_char();
neilt6 0:7b3cbb5a53b8 359
neilt6 0:7b3cbb5a53b8 360 //Return the period in seconds
neilt6 0:7b3cbb5a53b8 361 if (value == 0x00)
neilt6 0:7b3cbb5a53b8 362 return 0.067f;
neilt6 0:7b3cbb5a53b8 363 else if (value == 0xFF)
neilt6 0:7b3cbb5a53b8 364 return 16.8f;
neilt6 0:7b3cbb5a53b8 365 else
neilt6 0:7b3cbb5a53b8 366 return (value + 1) / 15.26f;
neilt6 0:7b3cbb5a53b8 367 }
neilt6 0:7b3cbb5a53b8 368
neilt6 0:7b3cbb5a53b8 369 void PCA9955::groupBlinkPeriod(float period)
neilt6 0:7b3cbb5a53b8 370 {
neilt6 0:7b3cbb5a53b8 371 char value = 0;
neilt6 0:7b3cbb5a53b8 372
neilt6 0:7b3cbb5a53b8 373 //Do a smart conversion
neilt6 0:7b3cbb5a53b8 374 if (period > 0.067f) {
neilt6 0:7b3cbb5a53b8 375 if (period < 16.8f)
neilt6 0:7b3cbb5a53b8 376 value = (char)((period * 15.26f) - 1);
neilt6 0:7b3cbb5a53b8 377 else
neilt6 0:7b3cbb5a53b8 378 value = 0xFF;
neilt6 0:7b3cbb5a53b8 379 }
neilt6 0:7b3cbb5a53b8 380
neilt6 0:7b3cbb5a53b8 381 //Write the new 8-bit register value
neilt6 0:7b3cbb5a53b8 382 groupBlinkPeriod_char(value);
neilt6 0:7b3cbb5a53b8 383 }
neilt6 0:7b3cbb5a53b8 384
neilt6 0:7b3cbb5a53b8 385 char PCA9955::groupBlinkPeriod_char(void)
neilt6 0:7b3cbb5a53b8 386 {
neilt6 0:7b3cbb5a53b8 387 //Return the 8-bit register value
neilt6 0:7b3cbb5a53b8 388 return read(REG_GRPFREQ);
neilt6 0:7b3cbb5a53b8 389 }
neilt6 0:7b3cbb5a53b8 390
neilt6 0:7b3cbb5a53b8 391 void PCA9955::groupBlinkPeriod_char(char period)
neilt6 0:7b3cbb5a53b8 392 {
neilt6 0:7b3cbb5a53b8 393 //Write the new 8-bit register value
neilt6 0:7b3cbb5a53b8 394 write(REG_GRPFREQ, period);
neilt6 0:7b3cbb5a53b8 395 }
neilt6 0:7b3cbb5a53b8 396
neilt6 0:7b3cbb5a53b8 397 float PCA9955::outputDuty(Output output)
neilt6 0:7b3cbb5a53b8 398 {
neilt6 0:7b3cbb5a53b8 399 //Return the value as a float
neilt6 0:7b3cbb5a53b8 400 return outputDuty_char(output) / 255.0f;
neilt6 0:7b3cbb5a53b8 401 }
neilt6 0:7b3cbb5a53b8 402
neilt6 0:7b3cbb5a53b8 403 void PCA9955::outputDuty(Output output, float duty)
neilt6 0:7b3cbb5a53b8 404 {
neilt6 0:7b3cbb5a53b8 405 //Range check the value
neilt6 0:7b3cbb5a53b8 406 if (duty < 0.0f)
neilt6 0:7b3cbb5a53b8 407 duty = 0.0f;
neilt6 0:7b3cbb5a53b8 408 if (duty > 1.0f)
neilt6 0:7b3cbb5a53b8 409 duty = 1.0f;
neilt6 0:7b3cbb5a53b8 410
neilt6 0:7b3cbb5a53b8 411 //Convert the value to a char and write it
neilt6 0:7b3cbb5a53b8 412 outputDuty_char(output, (char)(duty * 255.0f));
neilt6 0:7b3cbb5a53b8 413 }
neilt6 0:7b3cbb5a53b8 414
neilt6 0:7b3cbb5a53b8 415 char PCA9955::outputDuty_char(Output output)
neilt6 0:7b3cbb5a53b8 416 {
neilt6 0:7b3cbb5a53b8 417 //Return the 8-bit register value
neilt6 0:7b3cbb5a53b8 418 return read(REG_PWM0 + (char)output);
neilt6 0:7b3cbb5a53b8 419 }
neilt6 0:7b3cbb5a53b8 420
neilt6 0:7b3cbb5a53b8 421 void PCA9955::outputDuty_char(Output output, char duty)
neilt6 0:7b3cbb5a53b8 422 {
neilt6 0:7b3cbb5a53b8 423 //Write the new 8-bit register value
neilt6 0:7b3cbb5a53b8 424 write(REG_PWM0 + (char)output, duty);
neilt6 0:7b3cbb5a53b8 425 }
neilt6 0:7b3cbb5a53b8 426
neilt6 0:7b3cbb5a53b8 427 float PCA9955::outputCurrent(Output output)
neilt6 0:7b3cbb5a53b8 428 {
neilt6 0:7b3cbb5a53b8 429 //Return the value as a float
neilt6 0:7b3cbb5a53b8 430 return outputCurrent_char(output) / 255.0f;
neilt6 0:7b3cbb5a53b8 431 }
neilt6 0:7b3cbb5a53b8 432
neilt6 0:7b3cbb5a53b8 433 void PCA9955::outputCurrent(Output output, float iref)
neilt6 0:7b3cbb5a53b8 434 {
neilt6 0:7b3cbb5a53b8 435 //Range check the value
neilt6 0:7b3cbb5a53b8 436 if (iref < 0.0f)
neilt6 0:7b3cbb5a53b8 437 iref = 0.0f;
neilt6 0:7b3cbb5a53b8 438 if (iref > 1.0f)
neilt6 0:7b3cbb5a53b8 439 iref = 1.0f;
neilt6 0:7b3cbb5a53b8 440
neilt6 0:7b3cbb5a53b8 441 //Convert the value to a char and write it
neilt6 0:7b3cbb5a53b8 442 outputCurrent_char(output, (char)(iref * 255.0f));
neilt6 0:7b3cbb5a53b8 443 }
neilt6 0:7b3cbb5a53b8 444
neilt6 0:7b3cbb5a53b8 445 char PCA9955::outputCurrent_char(Output output)
neilt6 0:7b3cbb5a53b8 446 {
neilt6 0:7b3cbb5a53b8 447 //Return the 8-bit register value
neilt6 0:7b3cbb5a53b8 448 return read(REG_IREF0 + (char)output);
neilt6 0:7b3cbb5a53b8 449 }
neilt6 0:7b3cbb5a53b8 450
neilt6 0:7b3cbb5a53b8 451 void PCA9955::outputCurrent_char(Output output, char iref)
neilt6 0:7b3cbb5a53b8 452 {
neilt6 0:7b3cbb5a53b8 453 //Write the new 8-bit register value
neilt6 0:7b3cbb5a53b8 454 write(REG_IREF0 + (char)output, iref);
neilt6 0:7b3cbb5a53b8 455 }
neilt6 0:7b3cbb5a53b8 456
neilt6 0:7b3cbb5a53b8 457 char PCA9955::outputDelay(void)
neilt6 0:7b3cbb5a53b8 458 {
neilt6 0:7b3cbb5a53b8 459 //Return the 8-bit register value (minus the top 4 bits)
neilt6 0:7b3cbb5a53b8 460 return read(REG_OFFSET) & 0x0F;
neilt6 0:7b3cbb5a53b8 461 }
neilt6 0:7b3cbb5a53b8 462
neilt6 0:7b3cbb5a53b8 463 void PCA9955::outputDelay(char clocks)
neilt6 0:7b3cbb5a53b8 464 {
neilt6 0:7b3cbb5a53b8 465 //Write the new 8-bit register value (minus the top 4 bits)
neilt6 0:7b3cbb5a53b8 466 write(REG_OFFSET, clocks & 0x0F);
neilt6 0:7b3cbb5a53b8 467 }
neilt6 0:7b3cbb5a53b8 468
neilt6 0:7b3cbb5a53b8 469 char PCA9955::subCall1Addr(void)
neilt6 0:7b3cbb5a53b8 470 {
neilt6 0:7b3cbb5a53b8 471 //Return the 8-bit address
neilt6 0:7b3cbb5a53b8 472 return read(REG_SUBADR1);
neilt6 0:7b3cbb5a53b8 473 }
neilt6 0:7b3cbb5a53b8 474
neilt6 0:7b3cbb5a53b8 475 void PCA9955::subCall1Addr(char addr)
neilt6 0:7b3cbb5a53b8 476 {
neilt6 0:7b3cbb5a53b8 477 //Write the new 8-bit address
neilt6 0:7b3cbb5a53b8 478 write(REG_SUBADR1, addr);
neilt6 0:7b3cbb5a53b8 479 }
neilt6 0:7b3cbb5a53b8 480
neilt6 0:7b3cbb5a53b8 481 char PCA9955::subCall2Addr(void)
neilt6 0:7b3cbb5a53b8 482 {
neilt6 0:7b3cbb5a53b8 483 //Return the 8-bit address
neilt6 0:7b3cbb5a53b8 484 return read(REG_SUBADR2);
neilt6 0:7b3cbb5a53b8 485 }
neilt6 0:7b3cbb5a53b8 486
neilt6 0:7b3cbb5a53b8 487 void PCA9955::subCall2Addr(char addr)
neilt6 0:7b3cbb5a53b8 488 {
neilt6 0:7b3cbb5a53b8 489 //Write the new 8-bit address
neilt6 0:7b3cbb5a53b8 490 write(REG_SUBADR2, addr);
neilt6 0:7b3cbb5a53b8 491 }
neilt6 0:7b3cbb5a53b8 492
neilt6 0:7b3cbb5a53b8 493 char PCA9955::subCall3Addr(void)
neilt6 0:7b3cbb5a53b8 494 {
neilt6 0:7b3cbb5a53b8 495 //Return the 8-bit address
neilt6 0:7b3cbb5a53b8 496 return read(REG_SUBADR3);
neilt6 0:7b3cbb5a53b8 497 }
neilt6 0:7b3cbb5a53b8 498
neilt6 0:7b3cbb5a53b8 499 void PCA9955::subCall3Addr(char addr)
neilt6 0:7b3cbb5a53b8 500 {
neilt6 0:7b3cbb5a53b8 501 //Write the new 8-bit address
neilt6 0:7b3cbb5a53b8 502 write(REG_SUBADR3, addr);
neilt6 0:7b3cbb5a53b8 503 }
neilt6 0:7b3cbb5a53b8 504
neilt6 0:7b3cbb5a53b8 505 char PCA9955::allCallAddr(void)
neilt6 0:7b3cbb5a53b8 506 {
neilt6 0:7b3cbb5a53b8 507 //Return the 8-bit address
neilt6 0:7b3cbb5a53b8 508 return read(REG_ALLCALLADR);
neilt6 0:7b3cbb5a53b8 509 }
neilt6 0:7b3cbb5a53b8 510
neilt6 0:7b3cbb5a53b8 511 void PCA9955::allCallAddr(char addr)
neilt6 0:7b3cbb5a53b8 512 {
neilt6 0:7b3cbb5a53b8 513 //Write the new 8-bit address
neilt6 0:7b3cbb5a53b8 514 write(REG_ALLCALLADR, addr);
neilt6 0:7b3cbb5a53b8 515 }
neilt6 0:7b3cbb5a53b8 516
neilt6 0:7b3cbb5a53b8 517 void PCA9955::allOutputStates(OutputState state)
neilt6 0:7b3cbb5a53b8 518 {
neilt6 0:7b3cbb5a53b8 519 char buff[5];
neilt6 0:7b3cbb5a53b8 520
neilt6 0:7b3cbb5a53b8 521 //Assemble the sending array
neilt6 0:7b3cbb5a53b8 522 buff[0] = REG_LEDOUT0 | REG_AUTO_INC;
neilt6 0:7b3cbb5a53b8 523 if (state == OUTPUT_OFF) {
neilt6 0:7b3cbb5a53b8 524 memset(buff + 1, 0x00, 4);
neilt6 0:7b3cbb5a53b8 525 } else if (state == OUTPUT_ON) {
neilt6 0:7b3cbb5a53b8 526 memset(buff + 1, 0x55, 4);
neilt6 0:7b3cbb5a53b8 527 } else if (state == OUTPUT_PWM) {
neilt6 0:7b3cbb5a53b8 528 memset(buff + 1, 0xAA, 4);
neilt6 0:7b3cbb5a53b8 529 } else {
neilt6 0:7b3cbb5a53b8 530 memset(buff + 1, 0xFF, 4);
neilt6 0:7b3cbb5a53b8 531 }
neilt6 0:7b3cbb5a53b8 532
neilt6 0:7b3cbb5a53b8 533 //Send the array
neilt6 0:7b3cbb5a53b8 534 writeMulti(buff, 5);
neilt6 0:7b3cbb5a53b8 535 }
neilt6 0:7b3cbb5a53b8 536
neilt6 0:7b3cbb5a53b8 537 void PCA9955::allOutputDuties(float duty)
neilt6 0:7b3cbb5a53b8 538 {
neilt6 0:7b3cbb5a53b8 539 //Range check the value
neilt6 0:7b3cbb5a53b8 540 if (duty < 0.0f)
neilt6 0:7b3cbb5a53b8 541 duty = 0.0f;
neilt6 0:7b3cbb5a53b8 542 if (duty > 1.0f)
neilt6 0:7b3cbb5a53b8 543 duty = 1.0f;
neilt6 0:7b3cbb5a53b8 544
neilt6 0:7b3cbb5a53b8 545 //Convert the value to a char and write it
neilt6 0:7b3cbb5a53b8 546 allOutputDuties_char((char)(duty * 255.0f));
neilt6 0:7b3cbb5a53b8 547 }
neilt6 0:7b3cbb5a53b8 548
neilt6 0:7b3cbb5a53b8 549 void PCA9955::allOutputDuties_char(char duty)
neilt6 0:7b3cbb5a53b8 550 {
neilt6 0:7b3cbb5a53b8 551 //Write the new 8-bit register value
neilt6 0:7b3cbb5a53b8 552 write(REG_PWMALL, duty);
neilt6 0:7b3cbb5a53b8 553 }
neilt6 0:7b3cbb5a53b8 554
neilt6 0:7b3cbb5a53b8 555 void PCA9955::allOutputCurrents(float iref)
neilt6 0:7b3cbb5a53b8 556 {
neilt6 0:7b3cbb5a53b8 557 //Range check the value
neilt6 0:7b3cbb5a53b8 558 if (iref < 0.0f)
neilt6 0:7b3cbb5a53b8 559 iref = 0.0f;
neilt6 0:7b3cbb5a53b8 560 if (iref > 1.0f)
neilt6 0:7b3cbb5a53b8 561 iref = 1.0f;
neilt6 0:7b3cbb5a53b8 562
neilt6 0:7b3cbb5a53b8 563 //Convert the value to a char and write it
neilt6 0:7b3cbb5a53b8 564 allOutputCurrents_char((char)(iref * 255.0f));
neilt6 0:7b3cbb5a53b8 565 }
neilt6 0:7b3cbb5a53b8 566
neilt6 0:7b3cbb5a53b8 567 void PCA9955::allOutputCurrents_char(char iref)
neilt6 0:7b3cbb5a53b8 568 {
neilt6 0:7b3cbb5a53b8 569 //Write the new 8-bit register value
neilt6 0:7b3cbb5a53b8 570 write(REG_IREFALL, iref);
neilt6 0:7b3cbb5a53b8 571 }
neilt6 0:7b3cbb5a53b8 572
neilt6 0:7b3cbb5a53b8 573 unsigned short PCA9955::faultTest(void)
neilt6 0:7b3cbb5a53b8 574 {
neilt6 0:7b3cbb5a53b8 575 //Read the current 8-bit register value
neilt6 0:7b3cbb5a53b8 576 char value = read(REG_MODE2);
neilt6 0:7b3cbb5a53b8 577
neilt6 0:7b3cbb5a53b8 578 //Set the FAULTTEST bit
neilt6 0:7b3cbb5a53b8 579 value |= (1 << 6);
neilt6 0:7b3cbb5a53b8 580
neilt6 0:7b3cbb5a53b8 581 //Write the value back out
neilt6 0:7b3cbb5a53b8 582 write(REG_MODE2, value);
neilt6 0:7b3cbb5a53b8 583
neilt6 0:7b3cbb5a53b8 584 //Wait for the fault test to complete
neilt6 0:7b3cbb5a53b8 585 while (read(REG_MODE2) & (1 << 6));
neilt6 0:7b3cbb5a53b8 586
neilt6 0:7b3cbb5a53b8 587 //Read the lower 8 flags
neilt6 0:7b3cbb5a53b8 588 unsigned short flags = read(REG_EFLAG0);
neilt6 0:7b3cbb5a53b8 589
neilt6 0:7b3cbb5a53b8 590 //Add the upper 8 flags
neilt6 0:7b3cbb5a53b8 591 flags |= read(REG_EFLAG1) << 8;
neilt6 0:7b3cbb5a53b8 592
neilt6 0:7b3cbb5a53b8 593 //Return the combined flags
neilt6 0:7b3cbb5a53b8 594 return flags;
neilt6 0:7b3cbb5a53b8 595 }
neilt6 0:7b3cbb5a53b8 596
neilt6 0:7b3cbb5a53b8 597 char PCA9955::read(char reg)
neilt6 0:7b3cbb5a53b8 598 {
neilt6 0:7b3cbb5a53b8 599 //Select the register
neilt6 0:7b3cbb5a53b8 600 m_I2C.write(m_Addr, &reg, 1);
neilt6 0:7b3cbb5a53b8 601
neilt6 0:7b3cbb5a53b8 602 //Read the 8-bit register
neilt6 0:7b3cbb5a53b8 603 m_I2C.read(m_Addr, &reg, 1);
neilt6 0:7b3cbb5a53b8 604
neilt6 0:7b3cbb5a53b8 605 //Return the byte
neilt6 0:7b3cbb5a53b8 606 return reg;
neilt6 0:7b3cbb5a53b8 607 }
neilt6 0:7b3cbb5a53b8 608
neilt6 0:7b3cbb5a53b8 609 void PCA9955::readMulti(char reg, char* data, int length)
neilt6 0:7b3cbb5a53b8 610 {
neilt6 0:7b3cbb5a53b8 611 //Select the starting register
neilt6 0:7b3cbb5a53b8 612 m_I2C.write(m_Addr, &reg, 1);
neilt6 0:7b3cbb5a53b8 613
neilt6 0:7b3cbb5a53b8 614 //Read the specified number of bytes
neilt6 0:7b3cbb5a53b8 615 m_I2C.read(m_Addr, data, length);
neilt6 0:7b3cbb5a53b8 616 }
neilt6 0:7b3cbb5a53b8 617
neilt6 0:7b3cbb5a53b8 618 void PCA9955::write(char reg, char data)
neilt6 0:7b3cbb5a53b8 619 {
neilt6 0:7b3cbb5a53b8 620 //Create a temporary buffer
neilt6 0:7b3cbb5a53b8 621 char buff[2];
neilt6 0:7b3cbb5a53b8 622
neilt6 0:7b3cbb5a53b8 623 //Load the register address and 8-bit data
neilt6 0:7b3cbb5a53b8 624 buff[0] = reg;
neilt6 0:7b3cbb5a53b8 625 buff[1] = data;
neilt6 0:7b3cbb5a53b8 626
neilt6 0:7b3cbb5a53b8 627 //Write the data
neilt6 0:7b3cbb5a53b8 628 m_I2C.write(m_Addr, buff, 2);
neilt6 0:7b3cbb5a53b8 629 }
neilt6 0:7b3cbb5a53b8 630
neilt6 0:7b3cbb5a53b8 631 void PCA9955::writeMulti(char* data, int length)
neilt6 0:7b3cbb5a53b8 632 {
neilt6 0:7b3cbb5a53b8 633 //Write the data
neilt6 0:7b3cbb5a53b8 634 m_I2C.write(m_Addr, data, length);
neilt6 0:7b3cbb5a53b8 635 }