James Sutton / Mbed 2 deprecated IBMIoTClientEthernetExample-MACfix-DebugFix

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample-MACfix by Chris Styles

Committer:
samdanbury
Date:
Wed Aug 20 12:45:14 2014 +0000
Revision:
6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samdanbury 6:37b6d0d56190 1 /* LM75B Driver Library
samdanbury 6:37b6d0d56190 2 * Copyright (c) 2013 Neil Thiessen
samdanbury 6:37b6d0d56190 3 *
samdanbury 6:37b6d0d56190 4 * Licensed under the Apache License, Version 2.0 (the "License");
samdanbury 6:37b6d0d56190 5 * you may not use this file except in compliance with the License.
samdanbury 6:37b6d0d56190 6 * You may obtain a copy of the License at
samdanbury 6:37b6d0d56190 7 *
samdanbury 6:37b6d0d56190 8 * http://www.apache.org/licenses/LICENSE-2.0
samdanbury 6:37b6d0d56190 9 *
samdanbury 6:37b6d0d56190 10 * Unless required by applicable law or agreed to in writing, software
samdanbury 6:37b6d0d56190 11 * distributed under the License is distributed on an "AS IS" BASIS,
samdanbury 6:37b6d0d56190 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
samdanbury 6:37b6d0d56190 13 * See the License for the specific language governing permissions and
samdanbury 6:37b6d0d56190 14 * limitations under the License.
samdanbury 6:37b6d0d56190 15 */
samdanbury 6:37b6d0d56190 16
samdanbury 6:37b6d0d56190 17 #include "LM75B.h"
samdanbury 6:37b6d0d56190 18
samdanbury 6:37b6d0d56190 19 LM75B::LM75B(PinName sda, PinName scl, Address addr, int hz) : m_I2C(sda, scl), m_ADDR((int)addr)
samdanbury 6:37b6d0d56190 20 {
samdanbury 6:37b6d0d56190 21 //Set the I2C bus frequency
samdanbury 6:37b6d0d56190 22 m_I2C.frequency(hz);
samdanbury 6:37b6d0d56190 23 }
samdanbury 6:37b6d0d56190 24
samdanbury 6:37b6d0d56190 25 bool LM75B::open()
samdanbury 6:37b6d0d56190 26 {
samdanbury 6:37b6d0d56190 27 //Probe for the LM75B using a Zero Length Transfer
samdanbury 6:37b6d0d56190 28 if (!m_I2C.write(m_ADDR, NULL, 0)) {
samdanbury 6:37b6d0d56190 29 //Return success
samdanbury 6:37b6d0d56190 30 return true;
samdanbury 6:37b6d0d56190 31 } else {
samdanbury 6:37b6d0d56190 32 //Return failure
samdanbury 6:37b6d0d56190 33 return false;
samdanbury 6:37b6d0d56190 34 }
samdanbury 6:37b6d0d56190 35 }
samdanbury 6:37b6d0d56190 36
samdanbury 6:37b6d0d56190 37 LM75B::PowerMode LM75B::powerMode()
samdanbury 6:37b6d0d56190 38 {
samdanbury 6:37b6d0d56190 39 //Read the 8-bit register value
samdanbury 6:37b6d0d56190 40 char value = read8(REG_CONF);
samdanbury 6:37b6d0d56190 41
samdanbury 6:37b6d0d56190 42 //Return the status of the SHUTDOWN bit
samdanbury 6:37b6d0d56190 43 if (value & (1 << 0))
samdanbury 6:37b6d0d56190 44 return POWER_SHUTDOWN;
samdanbury 6:37b6d0d56190 45 else
samdanbury 6:37b6d0d56190 46 return POWER_NORMAL;
samdanbury 6:37b6d0d56190 47 }
samdanbury 6:37b6d0d56190 48
samdanbury 6:37b6d0d56190 49 void LM75B::powerMode(PowerMode mode)
samdanbury 6:37b6d0d56190 50 {
samdanbury 6:37b6d0d56190 51 //Read the current 8-bit register value
samdanbury 6:37b6d0d56190 52 char value = read8(REG_CONF);
samdanbury 6:37b6d0d56190 53
samdanbury 6:37b6d0d56190 54 //Set or clear the SHUTDOWN bit
samdanbury 6:37b6d0d56190 55 if (mode == POWER_SHUTDOWN)
samdanbury 6:37b6d0d56190 56 value |= (1 << 0);
samdanbury 6:37b6d0d56190 57 else
samdanbury 6:37b6d0d56190 58 value &= ~(1 << 0);
samdanbury 6:37b6d0d56190 59
samdanbury 6:37b6d0d56190 60 //Write the value back out
samdanbury 6:37b6d0d56190 61 write8(REG_CONF, value);
samdanbury 6:37b6d0d56190 62 }
samdanbury 6:37b6d0d56190 63
samdanbury 6:37b6d0d56190 64 LM75B::OSMode LM75B::osMode()
samdanbury 6:37b6d0d56190 65 {
samdanbury 6:37b6d0d56190 66 //Read the 8-bit register value
samdanbury 6:37b6d0d56190 67 char value = read8(REG_CONF);
samdanbury 6:37b6d0d56190 68
samdanbury 6:37b6d0d56190 69 //Return the status of the OS_COMP_INT bit
samdanbury 6:37b6d0d56190 70 if (value & (1 << 1))
samdanbury 6:37b6d0d56190 71 return OS_INTERRUPT;
samdanbury 6:37b6d0d56190 72 else
samdanbury 6:37b6d0d56190 73 return OS_COMPARATOR;
samdanbury 6:37b6d0d56190 74 }
samdanbury 6:37b6d0d56190 75
samdanbury 6:37b6d0d56190 76 void LM75B::osMode(OSMode mode)
samdanbury 6:37b6d0d56190 77 {
samdanbury 6:37b6d0d56190 78 //Read the current 8-bit register value
samdanbury 6:37b6d0d56190 79 char value = read8(REG_CONF);
samdanbury 6:37b6d0d56190 80
samdanbury 6:37b6d0d56190 81 //Set or clear the OS_COMP_INT bit
samdanbury 6:37b6d0d56190 82 if (mode == OS_INTERRUPT)
samdanbury 6:37b6d0d56190 83 value |= (1 << 1);
samdanbury 6:37b6d0d56190 84 else
samdanbury 6:37b6d0d56190 85 value &= ~(1 << 1);
samdanbury 6:37b6d0d56190 86
samdanbury 6:37b6d0d56190 87 //Write the value back out
samdanbury 6:37b6d0d56190 88 write8(REG_CONF, value);
samdanbury 6:37b6d0d56190 89 }
samdanbury 6:37b6d0d56190 90
samdanbury 6:37b6d0d56190 91 LM75B::OSPolarity LM75B::osPolarity()
samdanbury 6:37b6d0d56190 92 {
samdanbury 6:37b6d0d56190 93 //Read the 8-bit register value
samdanbury 6:37b6d0d56190 94 char value = read8(REG_CONF);
samdanbury 6:37b6d0d56190 95
samdanbury 6:37b6d0d56190 96 //Return the status of the OS_POL bit
samdanbury 6:37b6d0d56190 97 if (value & (1 << 2))
samdanbury 6:37b6d0d56190 98 return OS_ACTIVE_HIGH;
samdanbury 6:37b6d0d56190 99 else
samdanbury 6:37b6d0d56190 100 return OS_ACTIVE_LOW;
samdanbury 6:37b6d0d56190 101 }
samdanbury 6:37b6d0d56190 102
samdanbury 6:37b6d0d56190 103 void LM75B::osPolarity(OSPolarity polarity)
samdanbury 6:37b6d0d56190 104 {
samdanbury 6:37b6d0d56190 105 //Read the current 8-bit register value
samdanbury 6:37b6d0d56190 106 char value = read8(REG_CONF);
samdanbury 6:37b6d0d56190 107
samdanbury 6:37b6d0d56190 108 //Set or clear the OS_POL bit
samdanbury 6:37b6d0d56190 109 if (polarity == OS_ACTIVE_HIGH)
samdanbury 6:37b6d0d56190 110 value |= (1 << 2);
samdanbury 6:37b6d0d56190 111 else
samdanbury 6:37b6d0d56190 112 value &= ~(1 << 2);
samdanbury 6:37b6d0d56190 113
samdanbury 6:37b6d0d56190 114 //Write the value back out
samdanbury 6:37b6d0d56190 115 write8(REG_CONF, value);
samdanbury 6:37b6d0d56190 116 }
samdanbury 6:37b6d0d56190 117
samdanbury 6:37b6d0d56190 118 LM75B::OSFaultQueue LM75B::osFaultQueue()
samdanbury 6:37b6d0d56190 119 {
samdanbury 6:37b6d0d56190 120 //Read the 8-bit register value
samdanbury 6:37b6d0d56190 121 char value = read8(REG_CONF);
samdanbury 6:37b6d0d56190 122
samdanbury 6:37b6d0d56190 123 //Return the status of the OS_F_QUE bits
samdanbury 6:37b6d0d56190 124 if ((value & (1 << 3)) && (value & (1 << 4)))
samdanbury 6:37b6d0d56190 125 return OS_FAULT_QUEUE_6;
samdanbury 6:37b6d0d56190 126 else if (!(value & (1 << 3)) && (value & (1 << 4)))
samdanbury 6:37b6d0d56190 127 return OS_FAULT_QUEUE_4;
samdanbury 6:37b6d0d56190 128 else if ((value & (1 << 3)) && !(value & (1 << 4)))
samdanbury 6:37b6d0d56190 129 return OS_FAULT_QUEUE_2;
samdanbury 6:37b6d0d56190 130 else
samdanbury 6:37b6d0d56190 131 return OS_FAULT_QUEUE_1;
samdanbury 6:37b6d0d56190 132 }
samdanbury 6:37b6d0d56190 133
samdanbury 6:37b6d0d56190 134 void LM75B::osFaultQueue(OSFaultQueue queue)
samdanbury 6:37b6d0d56190 135 {
samdanbury 6:37b6d0d56190 136 //Read the current 8-bit register value
samdanbury 6:37b6d0d56190 137 char value = read8(REG_CONF);
samdanbury 6:37b6d0d56190 138
samdanbury 6:37b6d0d56190 139 //Clear the old OS_F_QUE bits
samdanbury 6:37b6d0d56190 140 value &= ~(3 << 3);
samdanbury 6:37b6d0d56190 141
samdanbury 6:37b6d0d56190 142 //Set the new OS_F_QUE bits
samdanbury 6:37b6d0d56190 143 if (queue == OS_FAULT_QUEUE_2)
samdanbury 6:37b6d0d56190 144 value |= (1 << 3);
samdanbury 6:37b6d0d56190 145 else if (queue == OS_FAULT_QUEUE_4)
samdanbury 6:37b6d0d56190 146 value |= (2 << 3);
samdanbury 6:37b6d0d56190 147 else if (queue == OS_FAULT_QUEUE_6)
samdanbury 6:37b6d0d56190 148 value |= (3 << 3);
samdanbury 6:37b6d0d56190 149
samdanbury 6:37b6d0d56190 150 //Write the value back out
samdanbury 6:37b6d0d56190 151 write8(REG_CONF, value);
samdanbury 6:37b6d0d56190 152 }
samdanbury 6:37b6d0d56190 153
samdanbury 6:37b6d0d56190 154 float LM75B::alertTemp()
samdanbury 6:37b6d0d56190 155 {
samdanbury 6:37b6d0d56190 156 //Use the 9-bit helper to read the TOS register
samdanbury 6:37b6d0d56190 157 return readAlertTempHelper(REG_TOS);
samdanbury 6:37b6d0d56190 158 }
samdanbury 6:37b6d0d56190 159
samdanbury 6:37b6d0d56190 160 void LM75B::alertTemp(float temp)
samdanbury 6:37b6d0d56190 161 {
samdanbury 6:37b6d0d56190 162 //Use the 9-bit helper to write to the TOS register
samdanbury 6:37b6d0d56190 163 return writeAlertTempHelper(REG_TOS, temp);
samdanbury 6:37b6d0d56190 164 }
samdanbury 6:37b6d0d56190 165
samdanbury 6:37b6d0d56190 166 float LM75B::alertHyst()
samdanbury 6:37b6d0d56190 167 {
samdanbury 6:37b6d0d56190 168 //Use the 9-bit helper to read the THYST register
samdanbury 6:37b6d0d56190 169 return readAlertTempHelper(REG_THYST);
samdanbury 6:37b6d0d56190 170 }
samdanbury 6:37b6d0d56190 171
samdanbury 6:37b6d0d56190 172 void LM75B::alertHyst(float temp)
samdanbury 6:37b6d0d56190 173 {
samdanbury 6:37b6d0d56190 174 //Use the 9-bit helper to write to the THYST register
samdanbury 6:37b6d0d56190 175 return writeAlertTempHelper(REG_THYST, temp);
samdanbury 6:37b6d0d56190 176 }
samdanbury 6:37b6d0d56190 177
samdanbury 6:37b6d0d56190 178 float LM75B::temp()
samdanbury 6:37b6d0d56190 179 {
samdanbury 6:37b6d0d56190 180 //Signed return value
samdanbury 6:37b6d0d56190 181 short value;
samdanbury 6:37b6d0d56190 182
samdanbury 6:37b6d0d56190 183 //Read the 11-bit raw temperature value
samdanbury 6:37b6d0d56190 184 value = read16(REG_TEMP) >> 5;
samdanbury 6:37b6d0d56190 185
samdanbury 6:37b6d0d56190 186 //Sign extend negative numbers
samdanbury 6:37b6d0d56190 187 if (value & (1 << 10))
samdanbury 6:37b6d0d56190 188 value |= 0xFC00;
samdanbury 6:37b6d0d56190 189
samdanbury 6:37b6d0d56190 190 //Return the temperature in °C
samdanbury 6:37b6d0d56190 191 return value * 0.125;
samdanbury 6:37b6d0d56190 192 }
samdanbury 6:37b6d0d56190 193
samdanbury 6:37b6d0d56190 194 #ifdef MBED_OPERATORS
samdanbury 6:37b6d0d56190 195 LM75B::operator float()
samdanbury 6:37b6d0d56190 196 {
samdanbury 6:37b6d0d56190 197 //Return the current temperature reading
samdanbury 6:37b6d0d56190 198 return temp();
samdanbury 6:37b6d0d56190 199 }
samdanbury 6:37b6d0d56190 200 #endif
samdanbury 6:37b6d0d56190 201
samdanbury 6:37b6d0d56190 202 char LM75B::read8(char reg)
samdanbury 6:37b6d0d56190 203 {
samdanbury 6:37b6d0d56190 204 //Select the register
samdanbury 6:37b6d0d56190 205 m_I2C.write(m_ADDR, &reg, 1, true);
samdanbury 6:37b6d0d56190 206
samdanbury 6:37b6d0d56190 207 //Read the 8-bit register
samdanbury 6:37b6d0d56190 208 m_I2C.read(m_ADDR, &reg, 1);
samdanbury 6:37b6d0d56190 209
samdanbury 6:37b6d0d56190 210 //Return the byte
samdanbury 6:37b6d0d56190 211 return reg;
samdanbury 6:37b6d0d56190 212 }
samdanbury 6:37b6d0d56190 213
samdanbury 6:37b6d0d56190 214 void LM75B::write8(char reg, char data)
samdanbury 6:37b6d0d56190 215 {
samdanbury 6:37b6d0d56190 216 //Create a temporary buffer
samdanbury 6:37b6d0d56190 217 char buff[2];
samdanbury 6:37b6d0d56190 218
samdanbury 6:37b6d0d56190 219 //Load the register address and 8-bit data
samdanbury 6:37b6d0d56190 220 buff[0] = reg;
samdanbury 6:37b6d0d56190 221 buff[1] = data;
samdanbury 6:37b6d0d56190 222
samdanbury 6:37b6d0d56190 223 //Write the data
samdanbury 6:37b6d0d56190 224 m_I2C.write(m_ADDR, buff, 2);
samdanbury 6:37b6d0d56190 225 }
samdanbury 6:37b6d0d56190 226
samdanbury 6:37b6d0d56190 227 unsigned short LM75B::read16(char reg)
samdanbury 6:37b6d0d56190 228 {
samdanbury 6:37b6d0d56190 229 //Create a temporary buffer
samdanbury 6:37b6d0d56190 230 char buff[2];
samdanbury 6:37b6d0d56190 231
samdanbury 6:37b6d0d56190 232 //Select the register
samdanbury 6:37b6d0d56190 233 m_I2C.write(m_ADDR, &reg, 1, true);
samdanbury 6:37b6d0d56190 234
samdanbury 6:37b6d0d56190 235 //Read the 16-bit register
samdanbury 6:37b6d0d56190 236 m_I2C.read(m_ADDR, buff, 2);
samdanbury 6:37b6d0d56190 237
samdanbury 6:37b6d0d56190 238 //Return the combined 16-bit value
samdanbury 6:37b6d0d56190 239 return (buff[0] << 8) | buff[1];
samdanbury 6:37b6d0d56190 240 }
samdanbury 6:37b6d0d56190 241
samdanbury 6:37b6d0d56190 242 void LM75B::write16(char reg, unsigned short data)
samdanbury 6:37b6d0d56190 243 {
samdanbury 6:37b6d0d56190 244 //Create a temporary buffer
samdanbury 6:37b6d0d56190 245 char buff[3];
samdanbury 6:37b6d0d56190 246
samdanbury 6:37b6d0d56190 247 //Load the register address and 16-bit data
samdanbury 6:37b6d0d56190 248 buff[0] = reg;
samdanbury 6:37b6d0d56190 249 buff[1] = data >> 8;
samdanbury 6:37b6d0d56190 250 buff[2] = data;
samdanbury 6:37b6d0d56190 251
samdanbury 6:37b6d0d56190 252 //Write the data
samdanbury 6:37b6d0d56190 253 m_I2C.write(m_ADDR, buff, 3);
samdanbury 6:37b6d0d56190 254 }
samdanbury 6:37b6d0d56190 255
samdanbury 6:37b6d0d56190 256 float LM75B::readAlertTempHelper(char reg)
samdanbury 6:37b6d0d56190 257 {
samdanbury 6:37b6d0d56190 258 //Signed return value
samdanbury 6:37b6d0d56190 259 short value;
samdanbury 6:37b6d0d56190 260
samdanbury 6:37b6d0d56190 261 //Read the 9-bit raw temperature value
samdanbury 6:37b6d0d56190 262 value = read16(reg) >> 7;
samdanbury 6:37b6d0d56190 263
samdanbury 6:37b6d0d56190 264 //Sign extend negative numbers
samdanbury 6:37b6d0d56190 265 if (value & (1 << 8))
samdanbury 6:37b6d0d56190 266 value |= 0xFF00;
samdanbury 6:37b6d0d56190 267
samdanbury 6:37b6d0d56190 268 //Return the temperature in °C
samdanbury 6:37b6d0d56190 269 return value * 0.5;
samdanbury 6:37b6d0d56190 270 }
samdanbury 6:37b6d0d56190 271
samdanbury 6:37b6d0d56190 272 void LM75B::writeAlertTempHelper(char reg, float temp)
samdanbury 6:37b6d0d56190 273 {
samdanbury 6:37b6d0d56190 274 //Range limit temp
samdanbury 6:37b6d0d56190 275 if (temp < -55.0)
samdanbury 6:37b6d0d56190 276 temp = -55.0;
samdanbury 6:37b6d0d56190 277 else if (temp > 125.0)
samdanbury 6:37b6d0d56190 278 temp = 125.0;
samdanbury 6:37b6d0d56190 279
samdanbury 6:37b6d0d56190 280 //Extract and shift the signed integer
samdanbury 6:37b6d0d56190 281 short value = temp * 2;
samdanbury 6:37b6d0d56190 282 value <<= 7;
samdanbury 6:37b6d0d56190 283
samdanbury 6:37b6d0d56190 284 //Send the new value
samdanbury 6:37b6d0d56190 285 write16(reg, value);
samdanbury 6:37b6d0d56190 286 }