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