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