A feature complete driver for the PCA9952/55 LED driver from NXP.
Dependents: PCA9955_HelloWorld
PCA9955.cpp@4:6ca7ab31c5fb, 2013-11-08 (annotated)
- Committer:
- neilt6
- Date:
- Fri Nov 08 16:33:36 2013 +0000
- Revision:
- 4:6ca7ab31c5fb
- Parent:
- 1:016f916c5579
- Child:
- 5:7ad949955db8
Added second constructor to allow for modified addresses
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:016f916c5579 | 19 | PCA9955::PCA9955(PinName sda, PinName scl, Address addr) : m_I2C(sda, scl), m_ADDR((int)addr) |
neilt6 | 0:7b3cbb5a53b8 | 20 | { |
neilt6 | 1:016f916c5579 | 21 | //Set the I2C bus frequency to 400kHz |
neilt6 | 1:016f916c5579 | 22 | m_I2C.frequency(400000); |
neilt6 | 0:7b3cbb5a53b8 | 23 | } |
neilt6 | 0:7b3cbb5a53b8 | 24 | |
neilt6 | 4:6ca7ab31c5fb | 25 | PCA9955::PCA9955(PinName sda, PinName scl, int addr) : m_I2C(sda, scl), m_ADDR(addr) |
neilt6 | 4:6ca7ab31c5fb | 26 | { |
neilt6 | 4:6ca7ab31c5fb | 27 | //Set the I2C bus frequency to 400kHz |
neilt6 | 4:6ca7ab31c5fb | 28 | m_I2C.frequency(400000); |
neilt6 | 4:6ca7ab31c5fb | 29 | } |
neilt6 | 4:6ca7ab31c5fb | 30 | |
neilt6 | 1:016f916c5579 | 31 | bool PCA9955::open() |
neilt6 | 0:7b3cbb5a53b8 | 32 | { |
neilt6 | 0:7b3cbb5a53b8 | 33 | //Probe for the PCA9952/55 using a Zero Length Transfer |
neilt6 | 1:016f916c5579 | 34 | if (!m_I2C.write(m_ADDR, NULL, 0)) { |
neilt6 | 1:016f916c5579 | 35 | //NOTE: We don't issue a SWRST here since it might reset other I2C devices as well |
neilt6 | 1:016f916c5579 | 36 | |
neilt6 | 1:016f916c5579 | 37 | //Read the current 8-bit register value |
neilt6 | 1:016f916c5579 | 38 | char value = read(REG_MODE1); |
neilt6 | 1:016f916c5579 | 39 | |
neilt6 | 1:016f916c5579 | 40 | //Configure Auto-Increment for 0x00 to 0x41 |
neilt6 | 1:016f916c5579 | 41 | value &= ~(1 << 5); //AI0 bit |
neilt6 | 1:016f916c5579 | 42 | value &= ~(1 << 6); //AI1 bit |
neilt6 | 1:016f916c5579 | 43 | |
neilt6 | 1:016f916c5579 | 44 | //Write the value back out |
neilt6 | 1:016f916c5579 | 45 | write(REG_MODE1, value); |
neilt6 | 0:7b3cbb5a53b8 | 46 | |
neilt6 | 0:7b3cbb5a53b8 | 47 | //Return success |
neilt6 | 0:7b3cbb5a53b8 | 48 | return true; |
neilt6 | 0:7b3cbb5a53b8 | 49 | } else { |
neilt6 | 0:7b3cbb5a53b8 | 50 | //Return failure |
neilt6 | 0:7b3cbb5a53b8 | 51 | return false; |
neilt6 | 0:7b3cbb5a53b8 | 52 | } |
neilt6 | 0:7b3cbb5a53b8 | 53 | } |
neilt6 | 0:7b3cbb5a53b8 | 54 | |
neilt6 | 1:016f916c5579 | 55 | void PCA9955::reset() |
neilt6 | 0:7b3cbb5a53b8 | 56 | { |
neilt6 | 0:7b3cbb5a53b8 | 57 | //The SWRST magic data byte |
neilt6 | 0:7b3cbb5a53b8 | 58 | char data = 0x06; |
neilt6 | 0:7b3cbb5a53b8 | 59 | |
neilt6 | 0:7b3cbb5a53b8 | 60 | //Issue the SWRST call to the General Call address |
neilt6 | 0:7b3cbb5a53b8 | 61 | m_I2C.write(0x00, &data, 1); |
neilt6 | 0:7b3cbb5a53b8 | 62 | |
neilt6 | 0:7b3cbb5a53b8 | 63 | //Wait for 10ms to allow the device to reset |
neilt6 | 0:7b3cbb5a53b8 | 64 | wait_ms(10); |
neilt6 | 0:7b3cbb5a53b8 | 65 | } |
neilt6 | 0:7b3cbb5a53b8 | 66 | |
neilt6 | 1:016f916c5579 | 67 | bool PCA9955::allCallEnabled() |
neilt6 | 0:7b3cbb5a53b8 | 68 | { |
neilt6 | 0:7b3cbb5a53b8 | 69 | //Read the 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 70 | char value = read(REG_MODE1); |
neilt6 | 0:7b3cbb5a53b8 | 71 | |
neilt6 | 0:7b3cbb5a53b8 | 72 | //Return the status of the ALLCALL bit |
neilt6 | 0:7b3cbb5a53b8 | 73 | if (value & (1 << 0)) |
neilt6 | 0:7b3cbb5a53b8 | 74 | return true; |
neilt6 | 0:7b3cbb5a53b8 | 75 | else |
neilt6 | 0:7b3cbb5a53b8 | 76 | return false; |
neilt6 | 0:7b3cbb5a53b8 | 77 | } |
neilt6 | 0:7b3cbb5a53b8 | 78 | |
neilt6 | 0:7b3cbb5a53b8 | 79 | void PCA9955::allCallEnabled(bool enabled) |
neilt6 | 0:7b3cbb5a53b8 | 80 | { |
neilt6 | 0:7b3cbb5a53b8 | 81 | //Read the current 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 82 | char value = read(REG_MODE1); |
neilt6 | 0:7b3cbb5a53b8 | 83 | |
neilt6 | 0:7b3cbb5a53b8 | 84 | //Set or clear the ALLCALL bit |
neilt6 | 0:7b3cbb5a53b8 | 85 | if (enabled) |
neilt6 | 0:7b3cbb5a53b8 | 86 | value |= (1 << 0); |
neilt6 | 0:7b3cbb5a53b8 | 87 | else |
neilt6 | 0:7b3cbb5a53b8 | 88 | value &= ~(1 << 0); |
neilt6 | 0:7b3cbb5a53b8 | 89 | |
neilt6 | 0:7b3cbb5a53b8 | 90 | //Write the value back out |
neilt6 | 0:7b3cbb5a53b8 | 91 | write(REG_MODE1, value); |
neilt6 | 0:7b3cbb5a53b8 | 92 | } |
neilt6 | 0:7b3cbb5a53b8 | 93 | |
neilt6 | 1:016f916c5579 | 94 | bool PCA9955::subCall3Enabled() |
neilt6 | 0:7b3cbb5a53b8 | 95 | { |
neilt6 | 0:7b3cbb5a53b8 | 96 | //Read the 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 97 | char value = read(REG_MODE1); |
neilt6 | 0:7b3cbb5a53b8 | 98 | |
neilt6 | 0:7b3cbb5a53b8 | 99 | //Return the status of the SUB3 bit |
neilt6 | 0:7b3cbb5a53b8 | 100 | if (value & (1 << 1)) |
neilt6 | 0:7b3cbb5a53b8 | 101 | return true; |
neilt6 | 0:7b3cbb5a53b8 | 102 | else |
neilt6 | 0:7b3cbb5a53b8 | 103 | return false; |
neilt6 | 0:7b3cbb5a53b8 | 104 | } |
neilt6 | 0:7b3cbb5a53b8 | 105 | |
neilt6 | 0:7b3cbb5a53b8 | 106 | void PCA9955::subCall3Enabled(bool enabled) |
neilt6 | 0:7b3cbb5a53b8 | 107 | { |
neilt6 | 0:7b3cbb5a53b8 | 108 | //Read the current 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 109 | char value = read(REG_MODE1); |
neilt6 | 0:7b3cbb5a53b8 | 110 | |
neilt6 | 0:7b3cbb5a53b8 | 111 | //Set or clear the SUB3 bit |
neilt6 | 0:7b3cbb5a53b8 | 112 | if (enabled) |
neilt6 | 0:7b3cbb5a53b8 | 113 | value |= (1 << 1); |
neilt6 | 0:7b3cbb5a53b8 | 114 | else |
neilt6 | 0:7b3cbb5a53b8 | 115 | value &= ~(1 << 1); |
neilt6 | 0:7b3cbb5a53b8 | 116 | |
neilt6 | 0:7b3cbb5a53b8 | 117 | //Write the value back out |
neilt6 | 0:7b3cbb5a53b8 | 118 | write(REG_MODE1, value); |
neilt6 | 0:7b3cbb5a53b8 | 119 | } |
neilt6 | 0:7b3cbb5a53b8 | 120 | |
neilt6 | 1:016f916c5579 | 121 | bool PCA9955::subCall2Enabled() |
neilt6 | 0:7b3cbb5a53b8 | 122 | { |
neilt6 | 0:7b3cbb5a53b8 | 123 | //Read the 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 124 | char value = read(REG_MODE1); |
neilt6 | 0:7b3cbb5a53b8 | 125 | |
neilt6 | 0:7b3cbb5a53b8 | 126 | //Return the status of the SUB2 bit |
neilt6 | 0:7b3cbb5a53b8 | 127 | if (value & (1 << 2)) |
neilt6 | 0:7b3cbb5a53b8 | 128 | return true; |
neilt6 | 0:7b3cbb5a53b8 | 129 | else |
neilt6 | 0:7b3cbb5a53b8 | 130 | return false; |
neilt6 | 0:7b3cbb5a53b8 | 131 | } |
neilt6 | 0:7b3cbb5a53b8 | 132 | |
neilt6 | 0:7b3cbb5a53b8 | 133 | void PCA9955::subCall2Enabled(bool enabled) |
neilt6 | 0:7b3cbb5a53b8 | 134 | { |
neilt6 | 0:7b3cbb5a53b8 | 135 | //Read the current 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 136 | char value = read(REG_MODE1); |
neilt6 | 0:7b3cbb5a53b8 | 137 | |
neilt6 | 0:7b3cbb5a53b8 | 138 | //Set or clear the SUB2 bit |
neilt6 | 0:7b3cbb5a53b8 | 139 | if (enabled) |
neilt6 | 0:7b3cbb5a53b8 | 140 | value |= (1 << 2); |
neilt6 | 0:7b3cbb5a53b8 | 141 | else |
neilt6 | 0:7b3cbb5a53b8 | 142 | value &= ~(1 << 2); |
neilt6 | 0:7b3cbb5a53b8 | 143 | |
neilt6 | 0:7b3cbb5a53b8 | 144 | //Write the value back out |
neilt6 | 0:7b3cbb5a53b8 | 145 | write(REG_MODE1, value); |
neilt6 | 0:7b3cbb5a53b8 | 146 | } |
neilt6 | 0:7b3cbb5a53b8 | 147 | |
neilt6 | 1:016f916c5579 | 148 | bool PCA9955::subCall1Enabled() |
neilt6 | 0:7b3cbb5a53b8 | 149 | { |
neilt6 | 0:7b3cbb5a53b8 | 150 | //Read the 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 151 | char value = read(REG_MODE1); |
neilt6 | 0:7b3cbb5a53b8 | 152 | |
neilt6 | 0:7b3cbb5a53b8 | 153 | //Return the status of the SUB1 bit |
neilt6 | 0:7b3cbb5a53b8 | 154 | if (value & (1 << 3)) |
neilt6 | 0:7b3cbb5a53b8 | 155 | return true; |
neilt6 | 0:7b3cbb5a53b8 | 156 | else |
neilt6 | 0:7b3cbb5a53b8 | 157 | return false; |
neilt6 | 0:7b3cbb5a53b8 | 158 | } |
neilt6 | 0:7b3cbb5a53b8 | 159 | |
neilt6 | 0:7b3cbb5a53b8 | 160 | void PCA9955::subCall1Enabled(bool enabled) |
neilt6 | 0:7b3cbb5a53b8 | 161 | { |
neilt6 | 0:7b3cbb5a53b8 | 162 | //Read the current 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 163 | char value = read(REG_MODE1); |
neilt6 | 0:7b3cbb5a53b8 | 164 | |
neilt6 | 0:7b3cbb5a53b8 | 165 | //Set or clear the SUB1 bit |
neilt6 | 0:7b3cbb5a53b8 | 166 | if (enabled) |
neilt6 | 0:7b3cbb5a53b8 | 167 | value |= (1 << 3); |
neilt6 | 0:7b3cbb5a53b8 | 168 | else |
neilt6 | 0:7b3cbb5a53b8 | 169 | value &= ~(1 << 3); |
neilt6 | 0:7b3cbb5a53b8 | 170 | |
neilt6 | 0:7b3cbb5a53b8 | 171 | //Write the value back out |
neilt6 | 0:7b3cbb5a53b8 | 172 | write(REG_MODE1, value); |
neilt6 | 0:7b3cbb5a53b8 | 173 | } |
neilt6 | 0:7b3cbb5a53b8 | 174 | |
neilt6 | 1:016f916c5579 | 175 | PCA9955::PowerMode PCA9955::powerMode() |
neilt6 | 0:7b3cbb5a53b8 | 176 | { |
neilt6 | 0:7b3cbb5a53b8 | 177 | //Read the 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 178 | char value = read(REG_MODE1); |
neilt6 | 0:7b3cbb5a53b8 | 179 | |
neilt6 | 0:7b3cbb5a53b8 | 180 | //Return the status of the SLEEP bit |
neilt6 | 0:7b3cbb5a53b8 | 181 | if (value & (1 << 4)) |
neilt6 | 0:7b3cbb5a53b8 | 182 | return POWER_SHUTDOWN; |
neilt6 | 0:7b3cbb5a53b8 | 183 | else |
neilt6 | 0:7b3cbb5a53b8 | 184 | return POWER_NORMAL; |
neilt6 | 0:7b3cbb5a53b8 | 185 | } |
neilt6 | 0:7b3cbb5a53b8 | 186 | |
neilt6 | 0:7b3cbb5a53b8 | 187 | void PCA9955::powerMode(PowerMode mode) |
neilt6 | 0:7b3cbb5a53b8 | 188 | { |
neilt6 | 0:7b3cbb5a53b8 | 189 | //Read the current 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 190 | char value = read(REG_MODE1); |
neilt6 | 0:7b3cbb5a53b8 | 191 | |
neilt6 | 0:7b3cbb5a53b8 | 192 | //Set or clear the SLEEP bit |
neilt6 | 0:7b3cbb5a53b8 | 193 | if (mode == POWER_SHUTDOWN) |
neilt6 | 0:7b3cbb5a53b8 | 194 | value |= (1 << 4); |
neilt6 | 0:7b3cbb5a53b8 | 195 | else |
neilt6 | 0:7b3cbb5a53b8 | 196 | value &= ~(1 << 4); |
neilt6 | 0:7b3cbb5a53b8 | 197 | |
neilt6 | 0:7b3cbb5a53b8 | 198 | //Write the value back out |
neilt6 | 0:7b3cbb5a53b8 | 199 | write(REG_MODE1, value); |
neilt6 | 0:7b3cbb5a53b8 | 200 | } |
neilt6 | 0:7b3cbb5a53b8 | 201 | |
neilt6 | 1:016f916c5579 | 202 | PCA9955::OutputChangeMode PCA9955::outputChangeMode() |
neilt6 | 0:7b3cbb5a53b8 | 203 | { |
neilt6 | 0:7b3cbb5a53b8 | 204 | //Read the 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 205 | char value = read(REG_MODE2); |
neilt6 | 0:7b3cbb5a53b8 | 206 | |
neilt6 | 0:7b3cbb5a53b8 | 207 | //Return the status of the OCH bit |
neilt6 | 0:7b3cbb5a53b8 | 208 | if (value & (1 << 3)) |
neilt6 | 0:7b3cbb5a53b8 | 209 | return OUTPUT_CHANGE_ON_ACK; |
neilt6 | 0:7b3cbb5a53b8 | 210 | else |
neilt6 | 0:7b3cbb5a53b8 | 211 | return OUTPUT_CHANGE_ON_STOP; |
neilt6 | 0:7b3cbb5a53b8 | 212 | } |
neilt6 | 0:7b3cbb5a53b8 | 213 | |
neilt6 | 0:7b3cbb5a53b8 | 214 | void PCA9955::outputChangeMode(OutputChangeMode mode) |
neilt6 | 0:7b3cbb5a53b8 | 215 | { |
neilt6 | 0:7b3cbb5a53b8 | 216 | //Read the current 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 217 | char value = read(REG_MODE2); |
neilt6 | 0:7b3cbb5a53b8 | 218 | |
neilt6 | 0:7b3cbb5a53b8 | 219 | //Set or clear the OCH bit |
neilt6 | 0:7b3cbb5a53b8 | 220 | if (mode == OUTPUT_CHANGE_ON_ACK) |
neilt6 | 0:7b3cbb5a53b8 | 221 | value |= (1 << 3); |
neilt6 | 0:7b3cbb5a53b8 | 222 | else |
neilt6 | 0:7b3cbb5a53b8 | 223 | value &= ~(1 << 3); |
neilt6 | 0:7b3cbb5a53b8 | 224 | |
neilt6 | 0:7b3cbb5a53b8 | 225 | //Write the value back out |
neilt6 | 0:7b3cbb5a53b8 | 226 | write(REG_MODE2, value); |
neilt6 | 0:7b3cbb5a53b8 | 227 | } |
neilt6 | 0:7b3cbb5a53b8 | 228 | |
neilt6 | 1:016f916c5579 | 229 | PCA9955::GroupMode PCA9955::groupMode() |
neilt6 | 0:7b3cbb5a53b8 | 230 | { |
neilt6 | 0:7b3cbb5a53b8 | 231 | //Read the 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 232 | char value = read(REG_MODE2); |
neilt6 | 0:7b3cbb5a53b8 | 233 | |
neilt6 | 0:7b3cbb5a53b8 | 234 | //Return the status of the DMBLNK bit |
neilt6 | 0:7b3cbb5a53b8 | 235 | if (value & (1 << 5)) |
neilt6 | 0:7b3cbb5a53b8 | 236 | return GROUP_BLINKING; |
neilt6 | 0:7b3cbb5a53b8 | 237 | else |
neilt6 | 0:7b3cbb5a53b8 | 238 | return GROUP_DIMMING; |
neilt6 | 0:7b3cbb5a53b8 | 239 | } |
neilt6 | 0:7b3cbb5a53b8 | 240 | |
neilt6 | 0:7b3cbb5a53b8 | 241 | void PCA9955::groupMode(GroupMode mode) |
neilt6 | 0:7b3cbb5a53b8 | 242 | { |
neilt6 | 0:7b3cbb5a53b8 | 243 | //Read the current 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 244 | char value = read(REG_MODE2); |
neilt6 | 0:7b3cbb5a53b8 | 245 | |
neilt6 | 0:7b3cbb5a53b8 | 246 | //Set or clear the DMBLNK bit |
neilt6 | 0:7b3cbb5a53b8 | 247 | if (mode == GROUP_BLINKING) |
neilt6 | 0:7b3cbb5a53b8 | 248 | value |= (1 << 5); |
neilt6 | 0:7b3cbb5a53b8 | 249 | else |
neilt6 | 0:7b3cbb5a53b8 | 250 | value &= ~(1 << 5); |
neilt6 | 0:7b3cbb5a53b8 | 251 | |
neilt6 | 0:7b3cbb5a53b8 | 252 | //Write the value back out |
neilt6 | 0:7b3cbb5a53b8 | 253 | write(REG_MODE2, value); |
neilt6 | 0:7b3cbb5a53b8 | 254 | } |
neilt6 | 0:7b3cbb5a53b8 | 255 | |
neilt6 | 1:016f916c5579 | 256 | bool PCA9955::overTemp() |
neilt6 | 0:7b3cbb5a53b8 | 257 | { |
neilt6 | 0:7b3cbb5a53b8 | 258 | //Read the current 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 259 | char value = read(REG_MODE2); |
neilt6 | 0:7b3cbb5a53b8 | 260 | |
neilt6 | 0:7b3cbb5a53b8 | 261 | //Return the status of the OVERTEMP bit |
neilt6 | 0:7b3cbb5a53b8 | 262 | if (value & (1 << 7)) |
neilt6 | 0:7b3cbb5a53b8 | 263 | return true; |
neilt6 | 0:7b3cbb5a53b8 | 264 | else |
neilt6 | 0:7b3cbb5a53b8 | 265 | return false; |
neilt6 | 0:7b3cbb5a53b8 | 266 | } |
neilt6 | 0:7b3cbb5a53b8 | 267 | |
neilt6 | 0:7b3cbb5a53b8 | 268 | PCA9955::OutputState PCA9955::outputState(Output output) |
neilt6 | 0:7b3cbb5a53b8 | 269 | { |
neilt6 | 0:7b3cbb5a53b8 | 270 | char value; |
neilt6 | 0:7b3cbb5a53b8 | 271 | char reg; |
neilt6 | 0:7b3cbb5a53b8 | 272 | |
neilt6 | 0:7b3cbb5a53b8 | 273 | //Determine which register to read |
neilt6 | 0:7b3cbb5a53b8 | 274 | if (output < 4) { |
neilt6 | 0:7b3cbb5a53b8 | 275 | reg = REG_LEDOUT0; |
neilt6 | 0:7b3cbb5a53b8 | 276 | } else if (output < 8) { |
neilt6 | 0:7b3cbb5a53b8 | 277 | output = (Output)(output - 4); |
neilt6 | 0:7b3cbb5a53b8 | 278 | reg = REG_LEDOUT1; |
neilt6 | 0:7b3cbb5a53b8 | 279 | } else if (output < 12) { |
neilt6 | 0:7b3cbb5a53b8 | 280 | output = (Output)(output - 8); |
neilt6 | 0:7b3cbb5a53b8 | 281 | reg = REG_LEDOUT2; |
neilt6 | 0:7b3cbb5a53b8 | 282 | } else { |
neilt6 | 0:7b3cbb5a53b8 | 283 | output = (Output)(output - 12); |
neilt6 | 0:7b3cbb5a53b8 | 284 | reg = REG_LEDOUT3; |
neilt6 | 0:7b3cbb5a53b8 | 285 | } |
neilt6 | 0:7b3cbb5a53b8 | 286 | |
neilt6 | 0:7b3cbb5a53b8 | 287 | //Read the 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 288 | value = read(reg); |
neilt6 | 0:7b3cbb5a53b8 | 289 | |
neilt6 | 0:7b3cbb5a53b8 | 290 | //Shift and mask the other output states |
neilt6 | 0:7b3cbb5a53b8 | 291 | value = (value >> (output * 2)) & 0x03; |
neilt6 | 0:7b3cbb5a53b8 | 292 | |
neilt6 | 0:7b3cbb5a53b8 | 293 | //Return the selected output's state |
neilt6 | 0:7b3cbb5a53b8 | 294 | if (value == 0) |
neilt6 | 0:7b3cbb5a53b8 | 295 | return OUTPUT_OFF; |
neilt6 | 0:7b3cbb5a53b8 | 296 | else if (value == 1) |
neilt6 | 0:7b3cbb5a53b8 | 297 | return OUTPUT_ON; |
neilt6 | 0:7b3cbb5a53b8 | 298 | else if (value == 2) |
neilt6 | 0:7b3cbb5a53b8 | 299 | return OUTPUT_PWM; |
neilt6 | 0:7b3cbb5a53b8 | 300 | else |
neilt6 | 0:7b3cbb5a53b8 | 301 | return OUTPUT_PWM_GRPPWM; |
neilt6 | 0:7b3cbb5a53b8 | 302 | } |
neilt6 | 0:7b3cbb5a53b8 | 303 | |
neilt6 | 0:7b3cbb5a53b8 | 304 | void PCA9955::outputState(Output output, OutputState state) |
neilt6 | 0:7b3cbb5a53b8 | 305 | { |
neilt6 | 0:7b3cbb5a53b8 | 306 | char value; |
neilt6 | 0:7b3cbb5a53b8 | 307 | char reg; |
neilt6 | 0:7b3cbb5a53b8 | 308 | |
neilt6 | 0:7b3cbb5a53b8 | 309 | //Determine which register to read |
neilt6 | 0:7b3cbb5a53b8 | 310 | if (output < 4) { |
neilt6 | 0:7b3cbb5a53b8 | 311 | reg = REG_LEDOUT0; |
neilt6 | 0:7b3cbb5a53b8 | 312 | } else if (output < 8) { |
neilt6 | 0:7b3cbb5a53b8 | 313 | output = (Output)(output - 4); |
neilt6 | 0:7b3cbb5a53b8 | 314 | reg = REG_LEDOUT1; |
neilt6 | 0:7b3cbb5a53b8 | 315 | } else if (output < 12) { |
neilt6 | 0:7b3cbb5a53b8 | 316 | output = (Output)(output - 8); |
neilt6 | 0:7b3cbb5a53b8 | 317 | reg = REG_LEDOUT2; |
neilt6 | 0:7b3cbb5a53b8 | 318 | } else { |
neilt6 | 0:7b3cbb5a53b8 | 319 | output = (Output)(output - 12); |
neilt6 | 0:7b3cbb5a53b8 | 320 | reg = REG_LEDOUT3; |
neilt6 | 0:7b3cbb5a53b8 | 321 | } |
neilt6 | 0:7b3cbb5a53b8 | 322 | |
neilt6 | 0:7b3cbb5a53b8 | 323 | //Read the current 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 324 | value = read(reg); |
neilt6 | 0:7b3cbb5a53b8 | 325 | |
neilt6 | 0:7b3cbb5a53b8 | 326 | //Mask off the old output state (also turns the output off) |
neilt6 | 0:7b3cbb5a53b8 | 327 | value &= ~(0x03 << (output * 2)); |
neilt6 | 0:7b3cbb5a53b8 | 328 | |
neilt6 | 0:7b3cbb5a53b8 | 329 | //Add the new output state |
neilt6 | 0:7b3cbb5a53b8 | 330 | if (state == OUTPUT_ON) |
neilt6 | 0:7b3cbb5a53b8 | 331 | value |= (1 << (output * 2)); |
neilt6 | 0:7b3cbb5a53b8 | 332 | else if (state == OUTPUT_PWM) |
neilt6 | 0:7b3cbb5a53b8 | 333 | value |= (2 << (output * 2)); |
neilt6 | 0:7b3cbb5a53b8 | 334 | else if (state == OUTPUT_PWM_GRPPWM) |
neilt6 | 0:7b3cbb5a53b8 | 335 | value |= (3 << (output * 2)); |
neilt6 | 0:7b3cbb5a53b8 | 336 | |
neilt6 | 0:7b3cbb5a53b8 | 337 | //Write the value back out |
neilt6 | 0:7b3cbb5a53b8 | 338 | write(reg, value); |
neilt6 | 0:7b3cbb5a53b8 | 339 | } |
neilt6 | 0:7b3cbb5a53b8 | 340 | |
neilt6 | 1:016f916c5579 | 341 | float PCA9955::groupDuty() |
neilt6 | 0:7b3cbb5a53b8 | 342 | { |
neilt6 | 0:7b3cbb5a53b8 | 343 | //Return the value as a float |
neilt6 | 0:7b3cbb5a53b8 | 344 | return groupDuty_char() / 255.0f; |
neilt6 | 0:7b3cbb5a53b8 | 345 | } |
neilt6 | 0:7b3cbb5a53b8 | 346 | |
neilt6 | 0:7b3cbb5a53b8 | 347 | void PCA9955::groupDuty(float duty) |
neilt6 | 0:7b3cbb5a53b8 | 348 | { |
neilt6 | 0:7b3cbb5a53b8 | 349 | //Range check the value |
neilt6 | 0:7b3cbb5a53b8 | 350 | if (duty < 0.0f) |
neilt6 | 0:7b3cbb5a53b8 | 351 | duty = 0.0f; |
neilt6 | 0:7b3cbb5a53b8 | 352 | if (duty > 1.0f) |
neilt6 | 0:7b3cbb5a53b8 | 353 | duty = 1.0f; |
neilt6 | 0:7b3cbb5a53b8 | 354 | |
neilt6 | 0:7b3cbb5a53b8 | 355 | //Convert the value to a char and write it |
neilt6 | 0:7b3cbb5a53b8 | 356 | groupDuty_char((char)(duty * 255.0f)); |
neilt6 | 0:7b3cbb5a53b8 | 357 | } |
neilt6 | 0:7b3cbb5a53b8 | 358 | |
neilt6 | 1:016f916c5579 | 359 | char PCA9955::groupDuty_char() |
neilt6 | 0:7b3cbb5a53b8 | 360 | { |
neilt6 | 0:7b3cbb5a53b8 | 361 | //Return the 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 362 | return read(REG_GRPPWM); |
neilt6 | 0:7b3cbb5a53b8 | 363 | } |
neilt6 | 0:7b3cbb5a53b8 | 364 | |
neilt6 | 0:7b3cbb5a53b8 | 365 | void PCA9955::groupDuty_char(char duty) |
neilt6 | 0:7b3cbb5a53b8 | 366 | { |
neilt6 | 0:7b3cbb5a53b8 | 367 | //Write the new 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 368 | write(REG_GRPPWM, duty); |
neilt6 | 0:7b3cbb5a53b8 | 369 | } |
neilt6 | 0:7b3cbb5a53b8 | 370 | |
neilt6 | 1:016f916c5579 | 371 | float PCA9955::groupBlinkPeriod() |
neilt6 | 0:7b3cbb5a53b8 | 372 | { |
neilt6 | 0:7b3cbb5a53b8 | 373 | //Read the 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 374 | char value = groupBlinkPeriod_char(); |
neilt6 | 0:7b3cbb5a53b8 | 375 | |
neilt6 | 0:7b3cbb5a53b8 | 376 | //Return the period in seconds |
neilt6 | 0:7b3cbb5a53b8 | 377 | if (value == 0x00) |
neilt6 | 0:7b3cbb5a53b8 | 378 | return 0.067f; |
neilt6 | 0:7b3cbb5a53b8 | 379 | else if (value == 0xFF) |
neilt6 | 0:7b3cbb5a53b8 | 380 | return 16.8f; |
neilt6 | 0:7b3cbb5a53b8 | 381 | else |
neilt6 | 0:7b3cbb5a53b8 | 382 | return (value + 1) / 15.26f; |
neilt6 | 0:7b3cbb5a53b8 | 383 | } |
neilt6 | 0:7b3cbb5a53b8 | 384 | |
neilt6 | 0:7b3cbb5a53b8 | 385 | void PCA9955::groupBlinkPeriod(float period) |
neilt6 | 0:7b3cbb5a53b8 | 386 | { |
neilt6 | 0:7b3cbb5a53b8 | 387 | char value = 0; |
neilt6 | 0:7b3cbb5a53b8 | 388 | |
neilt6 | 0:7b3cbb5a53b8 | 389 | //Do a smart conversion |
neilt6 | 0:7b3cbb5a53b8 | 390 | if (period > 0.067f) { |
neilt6 | 0:7b3cbb5a53b8 | 391 | if (period < 16.8f) |
neilt6 | 0:7b3cbb5a53b8 | 392 | value = (char)((period * 15.26f) - 1); |
neilt6 | 0:7b3cbb5a53b8 | 393 | else |
neilt6 | 0:7b3cbb5a53b8 | 394 | value = 0xFF; |
neilt6 | 0:7b3cbb5a53b8 | 395 | } |
neilt6 | 0:7b3cbb5a53b8 | 396 | |
neilt6 | 0:7b3cbb5a53b8 | 397 | //Write the new 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 398 | groupBlinkPeriod_char(value); |
neilt6 | 0:7b3cbb5a53b8 | 399 | } |
neilt6 | 0:7b3cbb5a53b8 | 400 | |
neilt6 | 1:016f916c5579 | 401 | char PCA9955::groupBlinkPeriod_char() |
neilt6 | 0:7b3cbb5a53b8 | 402 | { |
neilt6 | 0:7b3cbb5a53b8 | 403 | //Return the 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 404 | return read(REG_GRPFREQ); |
neilt6 | 0:7b3cbb5a53b8 | 405 | } |
neilt6 | 0:7b3cbb5a53b8 | 406 | |
neilt6 | 0:7b3cbb5a53b8 | 407 | void PCA9955::groupBlinkPeriod_char(char period) |
neilt6 | 0:7b3cbb5a53b8 | 408 | { |
neilt6 | 0:7b3cbb5a53b8 | 409 | //Write the new 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 410 | write(REG_GRPFREQ, period); |
neilt6 | 0:7b3cbb5a53b8 | 411 | } |
neilt6 | 0:7b3cbb5a53b8 | 412 | |
neilt6 | 0:7b3cbb5a53b8 | 413 | float PCA9955::outputDuty(Output output) |
neilt6 | 0:7b3cbb5a53b8 | 414 | { |
neilt6 | 0:7b3cbb5a53b8 | 415 | //Return the value as a float |
neilt6 | 0:7b3cbb5a53b8 | 416 | return outputDuty_char(output) / 255.0f; |
neilt6 | 0:7b3cbb5a53b8 | 417 | } |
neilt6 | 0:7b3cbb5a53b8 | 418 | |
neilt6 | 0:7b3cbb5a53b8 | 419 | void PCA9955::outputDuty(Output output, float duty) |
neilt6 | 0:7b3cbb5a53b8 | 420 | { |
neilt6 | 0:7b3cbb5a53b8 | 421 | //Range check the value |
neilt6 | 0:7b3cbb5a53b8 | 422 | if (duty < 0.0f) |
neilt6 | 0:7b3cbb5a53b8 | 423 | duty = 0.0f; |
neilt6 | 0:7b3cbb5a53b8 | 424 | if (duty > 1.0f) |
neilt6 | 0:7b3cbb5a53b8 | 425 | duty = 1.0f; |
neilt6 | 0:7b3cbb5a53b8 | 426 | |
neilt6 | 0:7b3cbb5a53b8 | 427 | //Convert the value to a char and write it |
neilt6 | 0:7b3cbb5a53b8 | 428 | outputDuty_char(output, (char)(duty * 255.0f)); |
neilt6 | 0:7b3cbb5a53b8 | 429 | } |
neilt6 | 0:7b3cbb5a53b8 | 430 | |
neilt6 | 0:7b3cbb5a53b8 | 431 | char PCA9955::outputDuty_char(Output output) |
neilt6 | 0:7b3cbb5a53b8 | 432 | { |
neilt6 | 0:7b3cbb5a53b8 | 433 | //Return the 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 434 | return read(REG_PWM0 + (char)output); |
neilt6 | 0:7b3cbb5a53b8 | 435 | } |
neilt6 | 0:7b3cbb5a53b8 | 436 | |
neilt6 | 0:7b3cbb5a53b8 | 437 | void PCA9955::outputDuty_char(Output output, char duty) |
neilt6 | 0:7b3cbb5a53b8 | 438 | { |
neilt6 | 0:7b3cbb5a53b8 | 439 | //Write the new 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 440 | write(REG_PWM0 + (char)output, duty); |
neilt6 | 0:7b3cbb5a53b8 | 441 | } |
neilt6 | 0:7b3cbb5a53b8 | 442 | |
neilt6 | 0:7b3cbb5a53b8 | 443 | float PCA9955::outputCurrent(Output output) |
neilt6 | 0:7b3cbb5a53b8 | 444 | { |
neilt6 | 0:7b3cbb5a53b8 | 445 | //Return the value as a float |
neilt6 | 0:7b3cbb5a53b8 | 446 | return outputCurrent_char(output) / 255.0f; |
neilt6 | 0:7b3cbb5a53b8 | 447 | } |
neilt6 | 0:7b3cbb5a53b8 | 448 | |
neilt6 | 0:7b3cbb5a53b8 | 449 | void PCA9955::outputCurrent(Output output, float iref) |
neilt6 | 0:7b3cbb5a53b8 | 450 | { |
neilt6 | 0:7b3cbb5a53b8 | 451 | //Range check the value |
neilt6 | 0:7b3cbb5a53b8 | 452 | if (iref < 0.0f) |
neilt6 | 0:7b3cbb5a53b8 | 453 | iref = 0.0f; |
neilt6 | 0:7b3cbb5a53b8 | 454 | if (iref > 1.0f) |
neilt6 | 0:7b3cbb5a53b8 | 455 | iref = 1.0f; |
neilt6 | 0:7b3cbb5a53b8 | 456 | |
neilt6 | 0:7b3cbb5a53b8 | 457 | //Convert the value to a char and write it |
neilt6 | 0:7b3cbb5a53b8 | 458 | outputCurrent_char(output, (char)(iref * 255.0f)); |
neilt6 | 0:7b3cbb5a53b8 | 459 | } |
neilt6 | 0:7b3cbb5a53b8 | 460 | |
neilt6 | 0:7b3cbb5a53b8 | 461 | char PCA9955::outputCurrent_char(Output output) |
neilt6 | 0:7b3cbb5a53b8 | 462 | { |
neilt6 | 0:7b3cbb5a53b8 | 463 | //Return the 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 464 | return read(REG_IREF0 + (char)output); |
neilt6 | 0:7b3cbb5a53b8 | 465 | } |
neilt6 | 0:7b3cbb5a53b8 | 466 | |
neilt6 | 0:7b3cbb5a53b8 | 467 | void PCA9955::outputCurrent_char(Output output, char iref) |
neilt6 | 0:7b3cbb5a53b8 | 468 | { |
neilt6 | 0:7b3cbb5a53b8 | 469 | //Write the new 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 470 | write(REG_IREF0 + (char)output, iref); |
neilt6 | 0:7b3cbb5a53b8 | 471 | } |
neilt6 | 0:7b3cbb5a53b8 | 472 | |
neilt6 | 1:016f916c5579 | 473 | char PCA9955::outputDelay() |
neilt6 | 0:7b3cbb5a53b8 | 474 | { |
neilt6 | 0:7b3cbb5a53b8 | 475 | //Return the 8-bit register value (minus the top 4 bits) |
neilt6 | 0:7b3cbb5a53b8 | 476 | return read(REG_OFFSET) & 0x0F; |
neilt6 | 0:7b3cbb5a53b8 | 477 | } |
neilt6 | 0:7b3cbb5a53b8 | 478 | |
neilt6 | 0:7b3cbb5a53b8 | 479 | void PCA9955::outputDelay(char clocks) |
neilt6 | 0:7b3cbb5a53b8 | 480 | { |
neilt6 | 0:7b3cbb5a53b8 | 481 | //Write the new 8-bit register value (minus the top 4 bits) |
neilt6 | 0:7b3cbb5a53b8 | 482 | write(REG_OFFSET, clocks & 0x0F); |
neilt6 | 0:7b3cbb5a53b8 | 483 | } |
neilt6 | 0:7b3cbb5a53b8 | 484 | |
neilt6 | 1:016f916c5579 | 485 | char PCA9955::subCall1Addr() |
neilt6 | 0:7b3cbb5a53b8 | 486 | { |
neilt6 | 0:7b3cbb5a53b8 | 487 | //Return the 8-bit address |
neilt6 | 0:7b3cbb5a53b8 | 488 | return read(REG_SUBADR1); |
neilt6 | 0:7b3cbb5a53b8 | 489 | } |
neilt6 | 0:7b3cbb5a53b8 | 490 | |
neilt6 | 0:7b3cbb5a53b8 | 491 | void PCA9955::subCall1Addr(char addr) |
neilt6 | 0:7b3cbb5a53b8 | 492 | { |
neilt6 | 0:7b3cbb5a53b8 | 493 | //Write the new 8-bit address |
neilt6 | 0:7b3cbb5a53b8 | 494 | write(REG_SUBADR1, addr); |
neilt6 | 0:7b3cbb5a53b8 | 495 | } |
neilt6 | 0:7b3cbb5a53b8 | 496 | |
neilt6 | 1:016f916c5579 | 497 | char PCA9955::subCall2Addr() |
neilt6 | 0:7b3cbb5a53b8 | 498 | { |
neilt6 | 0:7b3cbb5a53b8 | 499 | //Return the 8-bit address |
neilt6 | 0:7b3cbb5a53b8 | 500 | return read(REG_SUBADR2); |
neilt6 | 0:7b3cbb5a53b8 | 501 | } |
neilt6 | 0:7b3cbb5a53b8 | 502 | |
neilt6 | 0:7b3cbb5a53b8 | 503 | void PCA9955::subCall2Addr(char addr) |
neilt6 | 0:7b3cbb5a53b8 | 504 | { |
neilt6 | 0:7b3cbb5a53b8 | 505 | //Write the new 8-bit address |
neilt6 | 0:7b3cbb5a53b8 | 506 | write(REG_SUBADR2, addr); |
neilt6 | 0:7b3cbb5a53b8 | 507 | } |
neilt6 | 0:7b3cbb5a53b8 | 508 | |
neilt6 | 1:016f916c5579 | 509 | char PCA9955::subCall3Addr() |
neilt6 | 0:7b3cbb5a53b8 | 510 | { |
neilt6 | 0:7b3cbb5a53b8 | 511 | //Return the 8-bit address |
neilt6 | 0:7b3cbb5a53b8 | 512 | return read(REG_SUBADR3); |
neilt6 | 0:7b3cbb5a53b8 | 513 | } |
neilt6 | 0:7b3cbb5a53b8 | 514 | |
neilt6 | 0:7b3cbb5a53b8 | 515 | void PCA9955::subCall3Addr(char addr) |
neilt6 | 0:7b3cbb5a53b8 | 516 | { |
neilt6 | 0:7b3cbb5a53b8 | 517 | //Write the new 8-bit address |
neilt6 | 0:7b3cbb5a53b8 | 518 | write(REG_SUBADR3, addr); |
neilt6 | 0:7b3cbb5a53b8 | 519 | } |
neilt6 | 0:7b3cbb5a53b8 | 520 | |
neilt6 | 1:016f916c5579 | 521 | char PCA9955::allCallAddr() |
neilt6 | 0:7b3cbb5a53b8 | 522 | { |
neilt6 | 0:7b3cbb5a53b8 | 523 | //Return the 8-bit address |
neilt6 | 0:7b3cbb5a53b8 | 524 | return read(REG_ALLCALLADR); |
neilt6 | 0:7b3cbb5a53b8 | 525 | } |
neilt6 | 0:7b3cbb5a53b8 | 526 | |
neilt6 | 0:7b3cbb5a53b8 | 527 | void PCA9955::allCallAddr(char addr) |
neilt6 | 0:7b3cbb5a53b8 | 528 | { |
neilt6 | 0:7b3cbb5a53b8 | 529 | //Write the new 8-bit address |
neilt6 | 0:7b3cbb5a53b8 | 530 | write(REG_ALLCALLADR, addr); |
neilt6 | 0:7b3cbb5a53b8 | 531 | } |
neilt6 | 0:7b3cbb5a53b8 | 532 | |
neilt6 | 0:7b3cbb5a53b8 | 533 | void PCA9955::allOutputStates(OutputState state) |
neilt6 | 0:7b3cbb5a53b8 | 534 | { |
neilt6 | 0:7b3cbb5a53b8 | 535 | char buff[5]; |
neilt6 | 0:7b3cbb5a53b8 | 536 | |
neilt6 | 0:7b3cbb5a53b8 | 537 | //Assemble the sending array |
neilt6 | 0:7b3cbb5a53b8 | 538 | buff[0] = REG_LEDOUT0 | REG_AUTO_INC; |
neilt6 | 0:7b3cbb5a53b8 | 539 | if (state == OUTPUT_OFF) { |
neilt6 | 0:7b3cbb5a53b8 | 540 | memset(buff + 1, 0x00, 4); |
neilt6 | 0:7b3cbb5a53b8 | 541 | } else if (state == OUTPUT_ON) { |
neilt6 | 0:7b3cbb5a53b8 | 542 | memset(buff + 1, 0x55, 4); |
neilt6 | 0:7b3cbb5a53b8 | 543 | } else if (state == OUTPUT_PWM) { |
neilt6 | 0:7b3cbb5a53b8 | 544 | memset(buff + 1, 0xAA, 4); |
neilt6 | 0:7b3cbb5a53b8 | 545 | } else { |
neilt6 | 0:7b3cbb5a53b8 | 546 | memset(buff + 1, 0xFF, 4); |
neilt6 | 0:7b3cbb5a53b8 | 547 | } |
neilt6 | 0:7b3cbb5a53b8 | 548 | |
neilt6 | 0:7b3cbb5a53b8 | 549 | //Send the array |
neilt6 | 0:7b3cbb5a53b8 | 550 | writeMulti(buff, 5); |
neilt6 | 0:7b3cbb5a53b8 | 551 | } |
neilt6 | 0:7b3cbb5a53b8 | 552 | |
neilt6 | 1:016f916c5579 | 553 | void PCA9955::getOutputDuties(float* duties) |
neilt6 | 1:016f916c5579 | 554 | { |
neilt6 | 1:016f916c5579 | 555 | char buff[16]; |
neilt6 | 1:016f916c5579 | 556 | |
neilt6 | 1:016f916c5579 | 557 | //Read all of the duty cycles as unsigned chars first |
neilt6 | 1:016f916c5579 | 558 | getOutputDuties_char(buff); |
neilt6 | 1:016f916c5579 | 559 | |
neilt6 | 1:016f916c5579 | 560 | //Convert all of the duty cycles to percents |
neilt6 | 1:016f916c5579 | 561 | for (int i = 0; i < 16; i++) { |
neilt6 | 1:016f916c5579 | 562 | duties[i] = buff[i] / 255.0f; |
neilt6 | 1:016f916c5579 | 563 | } |
neilt6 | 1:016f916c5579 | 564 | } |
neilt6 | 1:016f916c5579 | 565 | |
neilt6 | 0:7b3cbb5a53b8 | 566 | void PCA9955::allOutputDuties(float duty) |
neilt6 | 0:7b3cbb5a53b8 | 567 | { |
neilt6 | 0:7b3cbb5a53b8 | 568 | //Range check the value |
neilt6 | 0:7b3cbb5a53b8 | 569 | if (duty < 0.0f) |
neilt6 | 0:7b3cbb5a53b8 | 570 | duty = 0.0f; |
neilt6 | 0:7b3cbb5a53b8 | 571 | if (duty > 1.0f) |
neilt6 | 0:7b3cbb5a53b8 | 572 | duty = 1.0f; |
neilt6 | 0:7b3cbb5a53b8 | 573 | |
neilt6 | 0:7b3cbb5a53b8 | 574 | //Convert the value to a char and write it |
neilt6 | 0:7b3cbb5a53b8 | 575 | allOutputDuties_char((char)(duty * 255.0f)); |
neilt6 | 0:7b3cbb5a53b8 | 576 | } |
neilt6 | 0:7b3cbb5a53b8 | 577 | |
neilt6 | 1:016f916c5579 | 578 | void PCA9955::allOutputDuties(float* duties) |
neilt6 | 1:016f916c5579 | 579 | { |
neilt6 | 1:016f916c5579 | 580 | char buff[17]; |
neilt6 | 1:016f916c5579 | 581 | |
neilt6 | 1:016f916c5579 | 582 | //Assemble the sending array |
neilt6 | 1:016f916c5579 | 583 | buff[0] = REG_PWM0 | REG_AUTO_INC; |
neilt6 | 1:016f916c5579 | 584 | for (int i = 1; i < 17; i++) { |
neilt6 | 1:016f916c5579 | 585 | //Range check the value |
neilt6 | 1:016f916c5579 | 586 | if (duties[i - 1] < 0.0f) |
neilt6 | 1:016f916c5579 | 587 | duties[i - 1] = 0.0f; |
neilt6 | 1:016f916c5579 | 588 | if (duties[i - 1] > 1.0f) |
neilt6 | 1:016f916c5579 | 589 | duties[i - 1] = 1.0f; |
neilt6 | 1:016f916c5579 | 590 | |
neilt6 | 1:016f916c5579 | 591 | //Convert the value to a char and write it |
neilt6 | 1:016f916c5579 | 592 | buff[i] = duties[i - 1] * 255.0f; |
neilt6 | 1:016f916c5579 | 593 | } |
neilt6 | 1:016f916c5579 | 594 | |
neilt6 | 1:016f916c5579 | 595 | //Send the array |
neilt6 | 1:016f916c5579 | 596 | writeMulti(buff, 17); |
neilt6 | 1:016f916c5579 | 597 | } |
neilt6 | 1:016f916c5579 | 598 | |
neilt6 | 1:016f916c5579 | 599 | void PCA9955::getOutputDuties_char(char* duties) |
neilt6 | 1:016f916c5579 | 600 | { |
neilt6 | 1:016f916c5579 | 601 | //Read all of the duty cycles at once |
neilt6 | 1:016f916c5579 | 602 | readMulti(REG_PWM0 | REG_AUTO_INC, duties, 16); |
neilt6 | 1:016f916c5579 | 603 | } |
neilt6 | 1:016f916c5579 | 604 | |
neilt6 | 0:7b3cbb5a53b8 | 605 | void PCA9955::allOutputDuties_char(char duty) |
neilt6 | 0:7b3cbb5a53b8 | 606 | { |
neilt6 | 0:7b3cbb5a53b8 | 607 | //Write the new 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 608 | write(REG_PWMALL, duty); |
neilt6 | 0:7b3cbb5a53b8 | 609 | } |
neilt6 | 0:7b3cbb5a53b8 | 610 | |
neilt6 | 1:016f916c5579 | 611 | void PCA9955::allOutputDuties_char(char* duties) |
neilt6 | 1:016f916c5579 | 612 | { |
neilt6 | 1:016f916c5579 | 613 | char buff[17]; |
neilt6 | 1:016f916c5579 | 614 | |
neilt6 | 1:016f916c5579 | 615 | //Assemble the sending array |
neilt6 | 1:016f916c5579 | 616 | buff[0] = REG_PWM0 | REG_AUTO_INC; |
neilt6 | 1:016f916c5579 | 617 | memcpy(buff + 1, duties, 16); |
neilt6 | 1:016f916c5579 | 618 | |
neilt6 | 1:016f916c5579 | 619 | //Send the array |
neilt6 | 1:016f916c5579 | 620 | writeMulti(buff, 17); |
neilt6 | 1:016f916c5579 | 621 | } |
neilt6 | 1:016f916c5579 | 622 | |
neilt6 | 1:016f916c5579 | 623 | void PCA9955::getOutputCurrents(float* irefs) |
neilt6 | 1:016f916c5579 | 624 | { |
neilt6 | 1:016f916c5579 | 625 | char buff[16]; |
neilt6 | 1:016f916c5579 | 626 | |
neilt6 | 1:016f916c5579 | 627 | //Read all of the current references as unsigned chars first |
neilt6 | 1:016f916c5579 | 628 | getOutputCurrents_char(buff); |
neilt6 | 1:016f916c5579 | 629 | |
neilt6 | 1:016f916c5579 | 630 | //Convert all of the duty cycles to percents |
neilt6 | 1:016f916c5579 | 631 | for (int i = 0; i < 16; i++) { |
neilt6 | 1:016f916c5579 | 632 | irefs[i] = buff[i] / 255.0f; |
neilt6 | 1:016f916c5579 | 633 | } |
neilt6 | 1:016f916c5579 | 634 | } |
neilt6 | 1:016f916c5579 | 635 | |
neilt6 | 0:7b3cbb5a53b8 | 636 | void PCA9955::allOutputCurrents(float iref) |
neilt6 | 0:7b3cbb5a53b8 | 637 | { |
neilt6 | 0:7b3cbb5a53b8 | 638 | //Range check the value |
neilt6 | 0:7b3cbb5a53b8 | 639 | if (iref < 0.0f) |
neilt6 | 0:7b3cbb5a53b8 | 640 | iref = 0.0f; |
neilt6 | 0:7b3cbb5a53b8 | 641 | if (iref > 1.0f) |
neilt6 | 0:7b3cbb5a53b8 | 642 | iref = 1.0f; |
neilt6 | 0:7b3cbb5a53b8 | 643 | |
neilt6 | 0:7b3cbb5a53b8 | 644 | //Convert the value to a char and write it |
neilt6 | 0:7b3cbb5a53b8 | 645 | allOutputCurrents_char((char)(iref * 255.0f)); |
neilt6 | 0:7b3cbb5a53b8 | 646 | } |
neilt6 | 0:7b3cbb5a53b8 | 647 | |
neilt6 | 1:016f916c5579 | 648 | void PCA9955::allOutputCurrents(float* irefs) |
neilt6 | 1:016f916c5579 | 649 | { |
neilt6 | 1:016f916c5579 | 650 | char buff[17]; |
neilt6 | 1:016f916c5579 | 651 | |
neilt6 | 1:016f916c5579 | 652 | //Assemble the sending array |
neilt6 | 1:016f916c5579 | 653 | buff[0] = REG_IREF0 | REG_AUTO_INC; |
neilt6 | 1:016f916c5579 | 654 | for (int i = 1; i < 17; i++) { |
neilt6 | 1:016f916c5579 | 655 | //Range check the value |
neilt6 | 1:016f916c5579 | 656 | if (irefs[i - 1] < 0.0f) |
neilt6 | 1:016f916c5579 | 657 | irefs[i - 1] = 0.0f; |
neilt6 | 1:016f916c5579 | 658 | if (irefs[i - 1] > 1.0f) |
neilt6 | 1:016f916c5579 | 659 | irefs[i - 1] = 1.0f; |
neilt6 | 1:016f916c5579 | 660 | |
neilt6 | 1:016f916c5579 | 661 | //Convert the value to a char and write it |
neilt6 | 1:016f916c5579 | 662 | buff[i] = irefs[i - 1] * 255.0f; |
neilt6 | 1:016f916c5579 | 663 | } |
neilt6 | 1:016f916c5579 | 664 | |
neilt6 | 1:016f916c5579 | 665 | //Send the array |
neilt6 | 1:016f916c5579 | 666 | writeMulti(buff, 17); |
neilt6 | 1:016f916c5579 | 667 | } |
neilt6 | 1:016f916c5579 | 668 | |
neilt6 | 1:016f916c5579 | 669 | void PCA9955::getOutputCurrents_char(char* irefs) |
neilt6 | 1:016f916c5579 | 670 | { |
neilt6 | 1:016f916c5579 | 671 | //Read all of the current references at once |
neilt6 | 1:016f916c5579 | 672 | readMulti(REG_IREF0 | REG_AUTO_INC, irefs, 16); |
neilt6 | 1:016f916c5579 | 673 | } |
neilt6 | 1:016f916c5579 | 674 | |
neilt6 | 0:7b3cbb5a53b8 | 675 | void PCA9955::allOutputCurrents_char(char iref) |
neilt6 | 0:7b3cbb5a53b8 | 676 | { |
neilt6 | 0:7b3cbb5a53b8 | 677 | //Write the new 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 678 | write(REG_IREFALL, iref); |
neilt6 | 0:7b3cbb5a53b8 | 679 | } |
neilt6 | 0:7b3cbb5a53b8 | 680 | |
neilt6 | 1:016f916c5579 | 681 | void PCA9955::allOutputCurrents_char(char* irefs) |
neilt6 | 1:016f916c5579 | 682 | { |
neilt6 | 1:016f916c5579 | 683 | char buff[17]; |
neilt6 | 1:016f916c5579 | 684 | |
neilt6 | 1:016f916c5579 | 685 | //Assemble the sending array |
neilt6 | 1:016f916c5579 | 686 | buff[0] = REG_IREF0 | REG_AUTO_INC; |
neilt6 | 1:016f916c5579 | 687 | memcpy(buff + 1, irefs, 16); |
neilt6 | 1:016f916c5579 | 688 | |
neilt6 | 1:016f916c5579 | 689 | //Send the array |
neilt6 | 1:016f916c5579 | 690 | writeMulti(buff, 17); |
neilt6 | 1:016f916c5579 | 691 | } |
neilt6 | 1:016f916c5579 | 692 | |
neilt6 | 1:016f916c5579 | 693 | unsigned short PCA9955::faultTest() |
neilt6 | 0:7b3cbb5a53b8 | 694 | { |
neilt6 | 0:7b3cbb5a53b8 | 695 | //Read the current 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 696 | char value = read(REG_MODE2); |
neilt6 | 0:7b3cbb5a53b8 | 697 | |
neilt6 | 0:7b3cbb5a53b8 | 698 | //Set the FAULTTEST bit |
neilt6 | 0:7b3cbb5a53b8 | 699 | value |= (1 << 6); |
neilt6 | 0:7b3cbb5a53b8 | 700 | |
neilt6 | 0:7b3cbb5a53b8 | 701 | //Write the value back out |
neilt6 | 0:7b3cbb5a53b8 | 702 | write(REG_MODE2, value); |
neilt6 | 0:7b3cbb5a53b8 | 703 | |
neilt6 | 0:7b3cbb5a53b8 | 704 | //Wait for the fault test to complete |
neilt6 | 0:7b3cbb5a53b8 | 705 | while (read(REG_MODE2) & (1 << 6)); |
neilt6 | 0:7b3cbb5a53b8 | 706 | |
neilt6 | 0:7b3cbb5a53b8 | 707 | //Read the lower 8 flags |
neilt6 | 0:7b3cbb5a53b8 | 708 | unsigned short flags = read(REG_EFLAG0); |
neilt6 | 0:7b3cbb5a53b8 | 709 | |
neilt6 | 0:7b3cbb5a53b8 | 710 | //Add the upper 8 flags |
neilt6 | 0:7b3cbb5a53b8 | 711 | flags |= read(REG_EFLAG1) << 8; |
neilt6 | 0:7b3cbb5a53b8 | 712 | |
neilt6 | 0:7b3cbb5a53b8 | 713 | //Return the combined flags |
neilt6 | 0:7b3cbb5a53b8 | 714 | return flags; |
neilt6 | 0:7b3cbb5a53b8 | 715 | } |
neilt6 | 0:7b3cbb5a53b8 | 716 | |
neilt6 | 1:016f916c5579 | 717 | PCA9955& PCA9955::operator=(float value) |
neilt6 | 1:016f916c5579 | 718 | { |
neilt6 | 1:016f916c5579 | 719 | //Set all of the output duties |
neilt6 | 1:016f916c5579 | 720 | allOutputDuties(value); |
neilt6 | 1:016f916c5579 | 721 | return *this; |
neilt6 | 1:016f916c5579 | 722 | } |
neilt6 | 1:016f916c5579 | 723 | |
neilt6 | 0:7b3cbb5a53b8 | 724 | char PCA9955::read(char reg) |
neilt6 | 0:7b3cbb5a53b8 | 725 | { |
neilt6 | 0:7b3cbb5a53b8 | 726 | //Select the register |
neilt6 | 1:016f916c5579 | 727 | m_I2C.write(m_ADDR, ®, 1, true); |
neilt6 | 0:7b3cbb5a53b8 | 728 | |
neilt6 | 0:7b3cbb5a53b8 | 729 | //Read the 8-bit register |
neilt6 | 1:016f916c5579 | 730 | m_I2C.read(m_ADDR, ®, 1); |
neilt6 | 0:7b3cbb5a53b8 | 731 | |
neilt6 | 0:7b3cbb5a53b8 | 732 | //Return the byte |
neilt6 | 0:7b3cbb5a53b8 | 733 | return reg; |
neilt6 | 0:7b3cbb5a53b8 | 734 | } |
neilt6 | 0:7b3cbb5a53b8 | 735 | |
neilt6 | 0:7b3cbb5a53b8 | 736 | void PCA9955::write(char reg, char data) |
neilt6 | 0:7b3cbb5a53b8 | 737 | { |
neilt6 | 0:7b3cbb5a53b8 | 738 | //Create a temporary buffer |
neilt6 | 0:7b3cbb5a53b8 | 739 | char buff[2]; |
neilt6 | 0:7b3cbb5a53b8 | 740 | |
neilt6 | 0:7b3cbb5a53b8 | 741 | //Load the register address and 8-bit data |
neilt6 | 0:7b3cbb5a53b8 | 742 | buff[0] = reg; |
neilt6 | 0:7b3cbb5a53b8 | 743 | buff[1] = data; |
neilt6 | 0:7b3cbb5a53b8 | 744 | |
neilt6 | 0:7b3cbb5a53b8 | 745 | //Write the data |
neilt6 | 1:016f916c5579 | 746 | m_I2C.write(m_ADDR, buff, 2); |
neilt6 | 1:016f916c5579 | 747 | } |
neilt6 | 1:016f916c5579 | 748 | |
neilt6 | 1:016f916c5579 | 749 | void PCA9955::readMulti(char startReg, char* data, int length) |
neilt6 | 1:016f916c5579 | 750 | { |
neilt6 | 1:016f916c5579 | 751 | //Select the starting register |
neilt6 | 1:016f916c5579 | 752 | m_I2C.write(m_ADDR, &startReg, 1, true); |
neilt6 | 1:016f916c5579 | 753 | |
neilt6 | 1:016f916c5579 | 754 | //Read the specified number of bytes |
neilt6 | 1:016f916c5579 | 755 | m_I2C.read(m_ADDR, data, length); |
neilt6 | 0:7b3cbb5a53b8 | 756 | } |
neilt6 | 0:7b3cbb5a53b8 | 757 | |
neilt6 | 0:7b3cbb5a53b8 | 758 | void PCA9955::writeMulti(char* data, int length) |
neilt6 | 0:7b3cbb5a53b8 | 759 | { |
neilt6 | 0:7b3cbb5a53b8 | 760 | //Write the data |
neilt6 | 1:016f916c5579 | 761 | m_I2C.write(m_ADDR, data, length); |
neilt6 | 0:7b3cbb5a53b8 | 762 | } |