lel

Dependents:   Project2

Fork of LM75B by Neil Thiessen

Files at this revision

API Documentation at this revision

Comitter:
neilt6
Date:
Thu Nov 07 17:42:41 2013 +0000
Parent:
12:fc27dc535ea9
Child:
14:3a44310726fe
Commit message:
Minor improvements

Changed in this revision

LM75B.cpp Show annotated file Show diff for this revision Revisions of this file
LM75B.h Show annotated file Show diff for this revision Revisions of this file
--- a/LM75B.cpp	Mon Sep 16 21:32:18 2013 +0000
+++ b/LM75B.cpp	Thu Nov 07 17:42:41 2013 +0000
@@ -16,16 +16,16 @@
 
 #include "LM75B.h"
 
-LM75B::LM75B(PinName sda, PinName scl, Address addr) : m_I2C(sda, scl)
+LM75B::LM75B(PinName sda, PinName scl, Address addr) : m_I2C(sda, scl), m_ADDR((int)addr)
 {
-    //Set the internal device address
-    m_Addr = (int)addr;
+    //Set the I2C bus frequency to 400kHz
+    m_I2C.frequency(400000);
 }
 
-bool LM75B::open(void)
+bool LM75B::open()
 {
     //Probe for the LM75B using a Zero Length Transfer
-    if (!m_I2C.write(m_Addr, NULL, 0)) {
+    if (!m_I2C.write(m_ADDR, NULL, 0)) {
         //Reset the device to default configuration
         write8(REG_CONF, 0x00);
         write16(REG_THYST, 0x4B00);
@@ -39,7 +39,7 @@
     }
 }
 
-LM75B::PowerMode LM75B::powerMode(void)
+LM75B::PowerMode LM75B::powerMode()
 {
     //Read the 8-bit register value
     char value = read8(REG_CONF);
@@ -66,7 +66,7 @@
     write8(REG_CONF, value);
 }
 
-LM75B::OSMode LM75B::osMode(void)
+LM75B::OSMode LM75B::osMode()
 {
     //Read the 8-bit register value
     char value = read8(REG_CONF);
@@ -93,7 +93,7 @@
     write8(REG_CONF, value);
 }
 
-LM75B::OSPolarity LM75B::osPolarity(void)
+LM75B::OSPolarity LM75B::osPolarity()
 {
     //Read the 8-bit register value
     char value = read8(REG_CONF);
@@ -120,7 +120,7 @@
     write8(REG_CONF, value);
 }
 
-LM75B::OSFaultQueue LM75B::osFaultQueue(void)
+LM75B::OSFaultQueue LM75B::osFaultQueue()
 {
     //Read the 8-bit register value
     char value = read8(REG_CONF);
@@ -156,7 +156,7 @@
     write8(REG_CONF, value);
 }
 
-float LM75B::alertTemp(void)
+float LM75B::alertTemp()
 {
     //Use the 9-bit helper to read the TOS register
     return readAlertTempHelper(REG_TOS);
@@ -168,7 +168,7 @@
     return writeAlertTempHelper(REG_TOS, temp);
 }
 
-float LM75B::alertHyst(void)
+float LM75B::alertHyst()
 {
     //Use the 9-bit helper to read the THYST register
     return readAlertTempHelper(REG_THYST);
@@ -180,7 +180,7 @@
     return writeAlertTempHelper(REG_THYST, temp);
 }
 
-float LM75B::temp(void)
+float LM75B::temp()
 {
     //Signed return value
     short value;
@@ -196,13 +196,19 @@
     return value * 0.125;
 }
 
+LM75B::operator float()
+{
+    //Return the current temperature reading
+    return temp();
+}
+
 char LM75B::read8(char reg)
 {
     //Select the register
-    m_I2C.write(m_Addr, &reg, 1);
+    m_I2C.write(m_ADDR, &reg, 1, true);
 
     //Read the 8-bit register
-    m_I2C.read(m_Addr, &reg, 1);
+    m_I2C.read(m_ADDR, &reg, 1);
 
     //Return the byte
     return reg;
@@ -218,7 +224,7 @@
     buff[1] = data;
 
     //Write the data
-    m_I2C.write(m_Addr, buff, 2);
+    m_I2C.write(m_ADDR, buff, 2);
 }
 
 unsigned short LM75B::read16(char reg)
@@ -227,10 +233,10 @@
     char buff[2];
 
     //Select the register
-    m_I2C.write(m_Addr, &reg, 1);
+    m_I2C.write(m_ADDR, &reg, 1, true);
 
     //Read the 16-bit register
-    m_I2C.read(m_Addr, buff, 2);
+    m_I2C.read(m_ADDR, buff, 2);
 
     //Return the combined 16-bit value
     return (buff[0] << 8) | buff[1];
@@ -247,7 +253,7 @@
     buff[2] = data;
 
     //Write the data
-    m_I2C.write(m_Addr, buff, 3);
+    m_I2C.write(m_ADDR, buff, 3);
 }
 
 float LM75B::readAlertTempHelper(char reg)
--- a/LM75B.h	Mon Sep 16 21:32:18 2013 +0000
+++ b/LM75B.h	Thu Nov 07 17:42:41 2013 +0000
@@ -109,13 +109,13 @@
      *   'true' if the device exists on the bus,
      *   'false' if the device doesn't exist on the bus.
      */
-    bool open(void);
+    bool open();
 
     /** Get the current power mode of the LM75B
      *
      * @returns The current power mode as a PowerMode enum.
      */
-    LM75B::PowerMode powerMode(void);
+    LM75B::PowerMode powerMode();
 
     /** Set the power mode of the LM75B
      *
@@ -127,7 +127,7 @@
      *
      * @returns The current OS pin mode as an OSMode enum.
      */
-    LM75B::OSMode osMode(void);
+    LM75B::OSMode osMode();
 
     /** Set the OS pin mode of the LM75B
      *
@@ -139,7 +139,7 @@
      *
      * @returns The current OS pin polarity as an OSPolarity enum.
      */
-    LM75B::OSPolarity osPolarity(void);
+    LM75B::OSPolarity osPolarity();
 
     /** Set the OS pin polarity of the LM75B
      *
@@ -151,7 +151,7 @@
      *
      * @returns The current OS pin fault queue length as an OSFaultQueue enum.
      */
-    LM75B::OSFaultQueue osFaultQueue(void);
+    LM75B::OSFaultQueue osFaultQueue();
 
     /** Set the OS pin fault queue length of the LM75B
      *
@@ -163,7 +163,7 @@
      *
      * @returns The current alert temperature threshold in °C.
      */
-    float alertTemp(void);
+    float alertTemp();
 
     /** Set the alert temperature threshold of the LM75B
      *
@@ -175,7 +175,7 @@
      *
      * @returns The current alert temperature hysteresis threshold in °C.
      */
-    float alertHyst(void);
+    float alertHyst();
 
     /** Set the alert temperature hysteresis threshold of the LM75B
      *
@@ -187,16 +187,14 @@
      *
      * @returns The current temperature measurement in °C.
      */
-    float temp(void);
+    float temp();
 
 #ifdef MBED_OPERATORS
     /** A shorthand for temp()
      *
      * @returns The current temperature measurement in °C.
      */
-    operator float() {
-        return temp();
-    }
+    operator float();
 #endif
 
 private:
@@ -210,7 +208,7 @@
 
     //Member variables
     I2C m_I2C;
-    int m_Addr;
+    const int m_ADDR;
 
     //Internal functions
     char read8(char reg);