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