A feature complete driver for the PCA9952/55 LED driver from NXP.
Dependents: PCA9955_HelloWorld
PCA9955.cpp@10:c8c96c894ec6, 2014-05-02 (annotated)
- Committer:
- neilt6
- Date:
- Fri May 02 17:29:33 2014 +0000
- Revision:
- 10:c8c96c894ec6
- Parent:
- 9:a9f91f91633b
- Child:
- 11:dbf20a128eb6
Added an I2C frequency argument to the constructor
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 | 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 | 0:7b3cbb5a53b8 | 39 | //The SWRST magic data byte |
neilt6 | 0:7b3cbb5a53b8 | 40 | char data = 0x06; |
neilt6 | 0:7b3cbb5a53b8 | 41 | |
neilt6 | 0:7b3cbb5a53b8 | 42 | //Issue the SWRST call 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 | 9:a9f91f91633b | 323 | return groupDuty_char() / 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 | 9:a9f91f91633b | 335 | groupDuty_char((char)(duty * 255.0), altAddr); |
neilt6 | 0:7b3cbb5a53b8 | 336 | } |
neilt6 | 0:7b3cbb5a53b8 | 337 | |
neilt6 | 1:016f916c5579 | 338 | char PCA9955::groupDuty_char() |
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 | 7:7dd3cc73e873 | 344 | void PCA9955::groupDuty_char(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 | 0:7b3cbb5a53b8 | 353 | char value = groupBlinkPeriod_char(); |
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 | 7:7dd3cc73e873 | 377 | groupBlinkPeriod_char(value, altAddr); |
neilt6 | 0:7b3cbb5a53b8 | 378 | } |
neilt6 | 0:7b3cbb5a53b8 | 379 | |
neilt6 | 1:016f916c5579 | 380 | char PCA9955::groupBlinkPeriod_char() |
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 | 7:7dd3cc73e873 | 386 | void PCA9955::groupBlinkPeriod_char(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 | 9:a9f91f91633b | 395 | return outputDuty_char(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 | 9:a9f91f91633b | 407 | outputDuty_char(output, (char)(duty * 255.0), altAddr); |
neilt6 | 0:7b3cbb5a53b8 | 408 | } |
neilt6 | 0:7b3cbb5a53b8 | 409 | |
neilt6 | 0:7b3cbb5a53b8 | 410 | char PCA9955::outputDuty_char(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 | 7:7dd3cc73e873 | 416 | void PCA9955::outputDuty_char(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 | 9:a9f91f91633b | 425 | return outputCurrent_char(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 | 9:a9f91f91633b | 437 | outputCurrent_char(output, (char)(iref * 255.0), altAddr); |
neilt6 | 0:7b3cbb5a53b8 | 438 | } |
neilt6 | 0:7b3cbb5a53b8 | 439 | |
neilt6 | 0:7b3cbb5a53b8 | 440 | char PCA9955::outputCurrent_char(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 | 7:7dd3cc73e873 | 446 | void PCA9955::outputCurrent_char(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 | 7:7dd3cc73e873 | 512 | void PCA9955::allOutputStates(OutputState state, int altAddr) |
neilt6 | 0:7b3cbb5a53b8 | 513 | { |
neilt6 | 0:7b3cbb5a53b8 | 514 | char buff[5]; |
neilt6 | 0:7b3cbb5a53b8 | 515 | |
neilt6 | 0:7b3cbb5a53b8 | 516 | //Assemble the sending array |
neilt6 | 0:7b3cbb5a53b8 | 517 | buff[0] = REG_LEDOUT0 | REG_AUTO_INC; |
neilt6 | 0:7b3cbb5a53b8 | 518 | if (state == OUTPUT_OFF) { |
neilt6 | 0:7b3cbb5a53b8 | 519 | memset(buff + 1, 0x00, 4); |
neilt6 | 0:7b3cbb5a53b8 | 520 | } else if (state == OUTPUT_ON) { |
neilt6 | 0:7b3cbb5a53b8 | 521 | memset(buff + 1, 0x55, 4); |
neilt6 | 0:7b3cbb5a53b8 | 522 | } else if (state == OUTPUT_PWM) { |
neilt6 | 0:7b3cbb5a53b8 | 523 | memset(buff + 1, 0xAA, 4); |
neilt6 | 0:7b3cbb5a53b8 | 524 | } else { |
neilt6 | 0:7b3cbb5a53b8 | 525 | memset(buff + 1, 0xFF, 4); |
neilt6 | 0:7b3cbb5a53b8 | 526 | } |
neilt6 | 0:7b3cbb5a53b8 | 527 | |
neilt6 | 0:7b3cbb5a53b8 | 528 | //Send the array |
neilt6 | 7:7dd3cc73e873 | 529 | writeMulti((altAddr == NULL) ? m_ADDR : altAddr, buff, 5); |
neilt6 | 0:7b3cbb5a53b8 | 530 | } |
neilt6 | 0:7b3cbb5a53b8 | 531 | |
neilt6 | 1:016f916c5579 | 532 | void PCA9955::getOutputDuties(float* duties) |
neilt6 | 1:016f916c5579 | 533 | { |
neilt6 | 1:016f916c5579 | 534 | char buff[16]; |
neilt6 | 1:016f916c5579 | 535 | |
neilt6 | 1:016f916c5579 | 536 | //Read all of the duty cycles as unsigned chars first |
neilt6 | 1:016f916c5579 | 537 | getOutputDuties_char(buff); |
neilt6 | 1:016f916c5579 | 538 | |
neilt6 | 1:016f916c5579 | 539 | //Convert all of the duty cycles to percents |
neilt6 | 1:016f916c5579 | 540 | for (int i = 0; i < 16; i++) { |
neilt6 | 9:a9f91f91633b | 541 | duties[i] = buff[i] / 255.0; |
neilt6 | 1:016f916c5579 | 542 | } |
neilt6 | 1:016f916c5579 | 543 | } |
neilt6 | 1:016f916c5579 | 544 | |
neilt6 | 8:12a800c51b35 | 545 | void PCA9955::setOutputDuties(float* duties, int altAddr) |
neilt6 | 1:016f916c5579 | 546 | { |
neilt6 | 1:016f916c5579 | 547 | char buff[17]; |
neilt6 | 1:016f916c5579 | 548 | |
neilt6 | 1:016f916c5579 | 549 | //Assemble the sending array |
neilt6 | 1:016f916c5579 | 550 | buff[0] = REG_PWM0 | REG_AUTO_INC; |
neilt6 | 1:016f916c5579 | 551 | for (int i = 1; i < 17; i++) { |
neilt6 | 1:016f916c5579 | 552 | //Range check the value |
neilt6 | 9:a9f91f91633b | 553 | if (duties[i - 1] < 0.0) |
neilt6 | 9:a9f91f91633b | 554 | duties[i - 1] = 0.0; |
neilt6 | 9:a9f91f91633b | 555 | if (duties[i - 1] > 1.0) |
neilt6 | 9:a9f91f91633b | 556 | duties[i - 1] = 1.0; |
neilt6 | 1:016f916c5579 | 557 | |
neilt6 | 1:016f916c5579 | 558 | //Convert the value to a char and write it |
neilt6 | 9:a9f91f91633b | 559 | buff[i] = duties[i - 1] * 255.0; |
neilt6 | 1:016f916c5579 | 560 | } |
neilt6 | 1:016f916c5579 | 561 | |
neilt6 | 1:016f916c5579 | 562 | //Send the array |
neilt6 | 7:7dd3cc73e873 | 563 | writeMulti((altAddr == NULL) ? m_ADDR : altAddr, buff, 17); |
neilt6 | 1:016f916c5579 | 564 | } |
neilt6 | 1:016f916c5579 | 565 | |
neilt6 | 8:12a800c51b35 | 566 | void PCA9955::allOutputDuties(float duty, int altAddr) |
neilt6 | 8:12a800c51b35 | 567 | { |
neilt6 | 8:12a800c51b35 | 568 | //Range check the value |
neilt6 | 9:a9f91f91633b | 569 | if (duty < 0.0) |
neilt6 | 9:a9f91f91633b | 570 | duty = 0.0; |
neilt6 | 9:a9f91f91633b | 571 | if (duty > 1.0) |
neilt6 | 9:a9f91f91633b | 572 | duty = 1.0; |
neilt6 | 8:12a800c51b35 | 573 | |
neilt6 | 8:12a800c51b35 | 574 | //Convert the value to a char and write it |
neilt6 | 9:a9f91f91633b | 575 | allOutputDuties_char((char)(duty * 255.0), altAddr); |
neilt6 | 8:12a800c51b35 | 576 | } |
neilt6 | 8:12a800c51b35 | 577 | |
neilt6 | 1:016f916c5579 | 578 | void PCA9955::getOutputDuties_char(char* duties) |
neilt6 | 1:016f916c5579 | 579 | { |
neilt6 | 1:016f916c5579 | 580 | //Read all of the duty cycles at once |
neilt6 | 1:016f916c5579 | 581 | readMulti(REG_PWM0 | REG_AUTO_INC, duties, 16); |
neilt6 | 1:016f916c5579 | 582 | } |
neilt6 | 1:016f916c5579 | 583 | |
neilt6 | 8:12a800c51b35 | 584 | void PCA9955::setOutputDuties_char(char* duties, int altAddr) |
neilt6 | 1:016f916c5579 | 585 | { |
neilt6 | 1:016f916c5579 | 586 | char buff[17]; |
neilt6 | 1:016f916c5579 | 587 | |
neilt6 | 1:016f916c5579 | 588 | //Assemble the sending array |
neilt6 | 1:016f916c5579 | 589 | buff[0] = REG_PWM0 | REG_AUTO_INC; |
neilt6 | 1:016f916c5579 | 590 | memcpy(buff + 1, duties, 16); |
neilt6 | 1:016f916c5579 | 591 | |
neilt6 | 1:016f916c5579 | 592 | //Send the array |
neilt6 | 7:7dd3cc73e873 | 593 | writeMulti((altAddr == NULL) ? m_ADDR : altAddr, buff, 17); |
neilt6 | 1:016f916c5579 | 594 | } |
neilt6 | 1:016f916c5579 | 595 | |
neilt6 | 8:12a800c51b35 | 596 | void PCA9955::allOutputDuties_char(char duty, int altAddr) |
neilt6 | 8:12a800c51b35 | 597 | { |
neilt6 | 8:12a800c51b35 | 598 | //Write the new 8-bit register value |
neilt6 | 8:12a800c51b35 | 599 | write((altAddr == NULL) ? m_ADDR : altAddr, REG_PWMALL, duty); |
neilt6 | 8:12a800c51b35 | 600 | } |
neilt6 | 8:12a800c51b35 | 601 | |
neilt6 | 1:016f916c5579 | 602 | void PCA9955::getOutputCurrents(float* irefs) |
neilt6 | 1:016f916c5579 | 603 | { |
neilt6 | 1:016f916c5579 | 604 | char buff[16]; |
neilt6 | 1:016f916c5579 | 605 | |
neilt6 | 1:016f916c5579 | 606 | //Read all of the current references as unsigned chars first |
neilt6 | 1:016f916c5579 | 607 | getOutputCurrents_char(buff); |
neilt6 | 1:016f916c5579 | 608 | |
neilt6 | 1:016f916c5579 | 609 | //Convert all of the duty cycles to percents |
neilt6 | 1:016f916c5579 | 610 | for (int i = 0; i < 16; i++) { |
neilt6 | 9:a9f91f91633b | 611 | irefs[i] = buff[i] / 255.0; |
neilt6 | 1:016f916c5579 | 612 | } |
neilt6 | 1:016f916c5579 | 613 | } |
neilt6 | 1:016f916c5579 | 614 | |
neilt6 | 8:12a800c51b35 | 615 | void PCA9955::setOutputCurrents(float* irefs, int altAddr) |
neilt6 | 1:016f916c5579 | 616 | { |
neilt6 | 1:016f916c5579 | 617 | char buff[17]; |
neilt6 | 1:016f916c5579 | 618 | |
neilt6 | 1:016f916c5579 | 619 | //Assemble the sending array |
neilt6 | 1:016f916c5579 | 620 | buff[0] = REG_IREF0 | REG_AUTO_INC; |
neilt6 | 1:016f916c5579 | 621 | for (int i = 1; i < 17; i++) { |
neilt6 | 1:016f916c5579 | 622 | //Range check the value |
neilt6 | 9:a9f91f91633b | 623 | if (irefs[i - 1] < 0.0) |
neilt6 | 9:a9f91f91633b | 624 | irefs[i - 1] = 0.0; |
neilt6 | 9:a9f91f91633b | 625 | if (irefs[i - 1] > 1.0) |
neilt6 | 9:a9f91f91633b | 626 | irefs[i - 1] = 1.0; |
neilt6 | 1:016f916c5579 | 627 | |
neilt6 | 1:016f916c5579 | 628 | //Convert the value to a char and write it |
neilt6 | 9:a9f91f91633b | 629 | buff[i] = irefs[i - 1] * 255.0; |
neilt6 | 1:016f916c5579 | 630 | } |
neilt6 | 1:016f916c5579 | 631 | |
neilt6 | 1:016f916c5579 | 632 | //Send the array |
neilt6 | 7:7dd3cc73e873 | 633 | writeMulti((altAddr == NULL) ? m_ADDR : altAddr, buff, 17); |
neilt6 | 1:016f916c5579 | 634 | } |
neilt6 | 1:016f916c5579 | 635 | |
neilt6 | 8:12a800c51b35 | 636 | void PCA9955::allOutputCurrents(float iref, int altAddr) |
neilt6 | 8:12a800c51b35 | 637 | { |
neilt6 | 8:12a800c51b35 | 638 | //Range check the value |
neilt6 | 9:a9f91f91633b | 639 | if (iref < 0.0) |
neilt6 | 9:a9f91f91633b | 640 | iref = 0.0; |
neilt6 | 9:a9f91f91633b | 641 | if (iref > 1.0) |
neilt6 | 9:a9f91f91633b | 642 | iref = 1.0; |
neilt6 | 8:12a800c51b35 | 643 | |
neilt6 | 8:12a800c51b35 | 644 | //Convert the value to a char and write it |
neilt6 | 9:a9f91f91633b | 645 | allOutputCurrents_char((char)(iref * 255.0), altAddr); |
neilt6 | 8:12a800c51b35 | 646 | } |
neilt6 | 8:12a800c51b35 | 647 | |
neilt6 | 1:016f916c5579 | 648 | void PCA9955::getOutputCurrents_char(char* irefs) |
neilt6 | 1:016f916c5579 | 649 | { |
neilt6 | 1:016f916c5579 | 650 | //Read all of the current references at once |
neilt6 | 1:016f916c5579 | 651 | readMulti(REG_IREF0 | REG_AUTO_INC, irefs, 16); |
neilt6 | 1:016f916c5579 | 652 | } |
neilt6 | 1:016f916c5579 | 653 | |
neilt6 | 8:12a800c51b35 | 654 | void PCA9955::setOutputCurrents_char(char* irefs, int altAddr) |
neilt6 | 1:016f916c5579 | 655 | { |
neilt6 | 1:016f916c5579 | 656 | char buff[17]; |
neilt6 | 1:016f916c5579 | 657 | |
neilt6 | 1:016f916c5579 | 658 | //Assemble the sending array |
neilt6 | 1:016f916c5579 | 659 | buff[0] = REG_IREF0 | REG_AUTO_INC; |
neilt6 | 1:016f916c5579 | 660 | memcpy(buff + 1, irefs, 16); |
neilt6 | 1:016f916c5579 | 661 | |
neilt6 | 1:016f916c5579 | 662 | //Send the array |
neilt6 | 7:7dd3cc73e873 | 663 | writeMulti((altAddr == NULL) ? m_ADDR : altAddr, buff, 17); |
neilt6 | 1:016f916c5579 | 664 | } |
neilt6 | 1:016f916c5579 | 665 | |
neilt6 | 8:12a800c51b35 | 666 | void PCA9955::allOutputCurrents_char(char iref, int altAddr) |
neilt6 | 8:12a800c51b35 | 667 | { |
neilt6 | 8:12a800c51b35 | 668 | //Write the new 8-bit register value |
neilt6 | 8:12a800c51b35 | 669 | write((altAddr == NULL) ? m_ADDR : altAddr, REG_IREFALL, iref); |
neilt6 | 8:12a800c51b35 | 670 | } |
neilt6 | 8:12a800c51b35 | 671 | |
neilt6 | 1:016f916c5579 | 672 | unsigned short PCA9955::faultTest() |
neilt6 | 0:7b3cbb5a53b8 | 673 | { |
neilt6 | 0:7b3cbb5a53b8 | 674 | //Read the current 8-bit register value |
neilt6 | 0:7b3cbb5a53b8 | 675 | char value = read(REG_MODE2); |
neilt6 | 0:7b3cbb5a53b8 | 676 | |
neilt6 | 0:7b3cbb5a53b8 | 677 | //Set the FAULTTEST bit |
neilt6 | 0:7b3cbb5a53b8 | 678 | value |= (1 << 6); |
neilt6 | 0:7b3cbb5a53b8 | 679 | |
neilt6 | 0:7b3cbb5a53b8 | 680 | //Write the value back out |
neilt6 | 7:7dd3cc73e873 | 681 | write(m_ADDR, REG_MODE2, value); |
neilt6 | 0:7b3cbb5a53b8 | 682 | |
neilt6 | 0:7b3cbb5a53b8 | 683 | //Wait for the fault test to complete |
neilt6 | 0:7b3cbb5a53b8 | 684 | while (read(REG_MODE2) & (1 << 6)); |
neilt6 | 0:7b3cbb5a53b8 | 685 | |
neilt6 | 0:7b3cbb5a53b8 | 686 | //Read the lower 8 flags |
neilt6 | 0:7b3cbb5a53b8 | 687 | unsigned short flags = read(REG_EFLAG0); |
neilt6 | 0:7b3cbb5a53b8 | 688 | |
neilt6 | 0:7b3cbb5a53b8 | 689 | //Add the upper 8 flags |
neilt6 | 0:7b3cbb5a53b8 | 690 | flags |= read(REG_EFLAG1) << 8; |
neilt6 | 0:7b3cbb5a53b8 | 691 | |
neilt6 | 0:7b3cbb5a53b8 | 692 | //Return the combined flags |
neilt6 | 0:7b3cbb5a53b8 | 693 | return flags; |
neilt6 | 0:7b3cbb5a53b8 | 694 | } |
neilt6 | 0:7b3cbb5a53b8 | 695 | |
neilt6 | 1:016f916c5579 | 696 | PCA9955& PCA9955::operator=(float value) |
neilt6 | 1:016f916c5579 | 697 | { |
neilt6 | 1:016f916c5579 | 698 | //Set all of the output duties |
neilt6 | 1:016f916c5579 | 699 | allOutputDuties(value); |
neilt6 | 1:016f916c5579 | 700 | return *this; |
neilt6 | 1:016f916c5579 | 701 | } |
neilt6 | 1:016f916c5579 | 702 | |
neilt6 | 0:7b3cbb5a53b8 | 703 | char PCA9955::read(char reg) |
neilt6 | 0:7b3cbb5a53b8 | 704 | { |
neilt6 | 0:7b3cbb5a53b8 | 705 | //Select the register |
neilt6 | 1:016f916c5579 | 706 | m_I2C.write(m_ADDR, ®, 1, true); |
neilt6 | 0:7b3cbb5a53b8 | 707 | |
neilt6 | 0:7b3cbb5a53b8 | 708 | //Read the 8-bit register |
neilt6 | 1:016f916c5579 | 709 | m_I2C.read(m_ADDR, ®, 1); |
neilt6 | 0:7b3cbb5a53b8 | 710 | |
neilt6 | 0:7b3cbb5a53b8 | 711 | //Return the byte |
neilt6 | 0:7b3cbb5a53b8 | 712 | return reg; |
neilt6 | 0:7b3cbb5a53b8 | 713 | } |
neilt6 | 0:7b3cbb5a53b8 | 714 | |
neilt6 | 7:7dd3cc73e873 | 715 | void PCA9955::write(int addr, char reg, char data) |
neilt6 | 0:7b3cbb5a53b8 | 716 | { |
neilt6 | 0:7b3cbb5a53b8 | 717 | //Create a temporary buffer |
neilt6 | 0:7b3cbb5a53b8 | 718 | char buff[2]; |
neilt6 | 0:7b3cbb5a53b8 | 719 | |
neilt6 | 0:7b3cbb5a53b8 | 720 | //Load the register address and 8-bit data |
neilt6 | 0:7b3cbb5a53b8 | 721 | buff[0] = reg; |
neilt6 | 0:7b3cbb5a53b8 | 722 | buff[1] = data; |
neilt6 | 0:7b3cbb5a53b8 | 723 | |
neilt6 | 0:7b3cbb5a53b8 | 724 | //Write the data |
neilt6 | 7:7dd3cc73e873 | 725 | m_I2C.write(addr, buff, 2); |
neilt6 | 1:016f916c5579 | 726 | } |
neilt6 | 1:016f916c5579 | 727 | |
neilt6 | 1:016f916c5579 | 728 | void PCA9955::readMulti(char startReg, char* data, int length) |
neilt6 | 1:016f916c5579 | 729 | { |
neilt6 | 1:016f916c5579 | 730 | //Select the starting register |
neilt6 | 1:016f916c5579 | 731 | m_I2C.write(m_ADDR, &startReg, 1, true); |
neilt6 | 1:016f916c5579 | 732 | |
neilt6 | 1:016f916c5579 | 733 | //Read the specified number of bytes |
neilt6 | 1:016f916c5579 | 734 | m_I2C.read(m_ADDR, data, length); |
neilt6 | 0:7b3cbb5a53b8 | 735 | } |
neilt6 | 0:7b3cbb5a53b8 | 736 | |
neilt6 | 7:7dd3cc73e873 | 737 | void PCA9955::writeMulti(int addr, char* data, int length) |
neilt6 | 0:7b3cbb5a53b8 | 738 | { |
neilt6 | 0:7b3cbb5a53b8 | 739 | //Write the data |
neilt6 | 7:7dd3cc73e873 | 740 | m_I2C.write(addr, data, length); |
neilt6 | 0:7b3cbb5a53b8 | 741 | } |