lel

Dependents:   Project2

Fork of LM75B by Neil Thiessen

Files at this revision

API Documentation at this revision

Comitter:
neilt6
Date:
Wed Aug 14 04:33:25 2013 +0000
Parent:
7:bebde8098fca
Child:
9:74b44a27fa40
Commit message:
Modified library so as to be more compliant with C++ conventions

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	Fri Aug 09 22:21:36 2013 +0000
+++ b/LM75B.cpp	Wed Aug 14 04:33:25 2013 +0000
@@ -17,16 +17,16 @@
 #include "LM75B.h"
 #include "mbed.h"
 
-LM75B::LM75B(PinName sda, PinName scl, Address addr) : _i2c(sda, scl)
+LM75B::LM75B(PinName sda, PinName scl, Address addr) : m_I2C(sda, scl)
 {
     //Set the internal device address
-    _addr = (int)addr;
+    m_Addr = (int)addr;
 }
 
-LM75B::PowerMode LM75B::getPowerMode(void)
+LM75B::PowerMode LM75B::powerMode(void)
 {
     //Read the 8-bit register value
-    char value = _read8(__LM75B_REG_CONF);
+    char value = read8(REG_CONF);
 
     //Return the status of the SHUTDOWN bit
     if (value & (1 << 0))
@@ -35,10 +35,10 @@
         return POWER_NORMAL;
 }
 
-void LM75B::setPowerMode(PowerMode mode)
+void LM75B::powerMode(PowerMode mode)
 {
     //Read the current 8-bit register value
-    char value = _read8(__LM75B_REG_CONF);
+    char value = read8(REG_CONF);
 
     //Set or clear the SHUTDOWN bit
     if (mode == POWER_SHUTDOWN)
@@ -47,13 +47,13 @@
         value &= ~(1 << 0);
 
     //Write the value back out
-    _write8(__LM75B_REG_CONF, value);
+    write8(REG_CONF, value);
 }
 
-LM75B::OSMode LM75B::getOSMode(void)
+LM75B::OSMode LM75B::osMode(void)
 {
     //Read the 8-bit register value
-    char value = _read8(__LM75B_REG_CONF);
+    char value = read8(REG_CONF);
 
     //Return the status of the OS_COMP_INT bit
     if (value & (1 << 1))
@@ -62,10 +62,10 @@
         return OS_COMPARATOR;
 }
 
-void LM75B::setOSMode(OSMode mode)
+void LM75B::osMode(OSMode mode)
 {
     //Read the current 8-bit register value
-    char value = _read8(__LM75B_REG_CONF);
+    char value = read8(REG_CONF);
 
     //Set or clear the OS_COMP_INT bit
     if (mode == OS_INTERRUPT)
@@ -74,13 +74,13 @@
         value &= ~(1 << 1);
 
     //Write the value back out
-    _write8(__LM75B_REG_CONF, value);
+    write8(REG_CONF, value);
 }
 
-LM75B::OSPolarity LM75B::getOSPolarity(void)
+LM75B::OSPolarity LM75B::osPolarity(void)
 {
     //Read the 8-bit register value
-    char value = _read8(__LM75B_REG_CONF);
+    char value = read8(REG_CONF);
 
     //Return the status of the OS_POL bit
     if (value & (1 << 2))
@@ -89,10 +89,10 @@
         return OS_ACTIVE_LOW;
 }
 
-void LM75B::setOSPolarity(OSPolarity polarity)
+void LM75B::osPolarity(OSPolarity polarity)
 {
     //Read the current 8-bit register value
-    char value = _read8(__LM75B_REG_CONF);
+    char value = read8(REG_CONF);
 
     //Set or clear the OS_POL bit
     if (polarity == OS_ACTIVE_HIGH)
@@ -101,13 +101,13 @@
         value &= ~(1 << 2);
 
     //Write the value back out
-    _write8(__LM75B_REG_CONF, value);
+    write8(REG_CONF, value);
 }
 
-LM75B::OSFaultQueue LM75B::getOSFaultQueue(void)
+LM75B::OSFaultQueue LM75B::osFaultQueue(void)
 {
     //Read the 8-bit register value
-    char value = _read8(__LM75B_REG_CONF);
+    char value = read8(REG_CONF);
 
     //Return the status of the OS_F_QUE bits
     if ((value & (1 << 3)) && (value & (1 << 4)))
@@ -120,10 +120,10 @@
         return OS_FAULT_QUEUE_1;
 }
 
-void LM75B::setOSFaultQueue(OSFaultQueue queue)
+void LM75B::osFaultQueue(OSFaultQueue queue)
 {
     //Read the current 8-bit register value
-    char value = _read8(__LM75B_REG_CONF);
+    char value = read8(REG_CONF);
 
     //Clear the old OS_F_QUE bits
     value &= ~(3 << 3);
@@ -137,40 +137,40 @@
         value |= (3 << 3);
 
     //Write the value back out
-    _write8(__LM75B_REG_CONF, value);
+    write8(REG_CONF, value);
 }
 
-float LM75B::getAlertTemp(void)
+float LM75B::alertTemp(void)
 {
     //Use the 9-bit helper to read the TOS register
-    return _readTempHelper(__LM75B_REG_TOS);
+    return readAlertTempHelper(REG_TOS);
 }
 
-void LM75B::setAlertTemp(float temp)
+void LM75B::alertTemp(float temp)
 {
     //Use the 9-bit helper to write to the TOS register
-    return _writeTempHelper(__LM75B_REG_TOS, temp);
+    return writeAlertTempHelper(REG_TOS, temp);
 }
 
-float LM75B::getAlertHyst(void)
+float LM75B::alertHyst(void)
 {
     //Use the 9-bit helper to read the THYST register
-    return _readTempHelper(__LM75B_REG_THYST);
+    return readAlertTempHelper(REG_THYST);
 }
 
-void LM75B::setAlertHyst(float temp)
+void LM75B::alertHyst(float temp)
 {
     //Use the 9-bit helper to write to the THYST register
-    return _writeTempHelper(__LM75B_REG_THYST, temp);
+    return writeAlertTempHelper(REG_THYST, temp);
 }
 
-float LM75B::getTemp(void)
+float LM75B::temp(void)
 {
     //Signed return value
     short value;
 
     //Read the 11-bit raw temperature value
-    value = _read16(__LM75B_REG_TEMP) >> 5;
+    value = read16(REG_TEMP) >> 5;
 
     //Sign extend negative numbers
     if (value & (1 << 10))
@@ -180,19 +180,19 @@
     return value * 0.125;
 }
 
-char LM75B::_read8(char reg)
+char LM75B::read8(char reg)
 {
     //Select the register
-    _i2c.write(_addr, &reg, 1);
+    m_I2C.write(m_Addr, &reg, 1);
 
     //Read the 8-bit register
-    _i2c.read(_addr, &reg, 1);
+    m_I2C.read(m_Addr, &reg, 1);
 
     //Return the byte
     return reg;
 }
 
-void LM75B::_write8(char reg, char data)
+void LM75B::write8(char reg, char data)
 {
     //Create a temporary buffer
     char buff[2];
@@ -202,25 +202,25 @@
     buff[1] = data;
 
     //Write the data
-    _i2c.write(_addr, buff, 2);
+    m_I2C.write(m_Addr, buff, 2);
 }
 
-unsigned short LM75B::_read16(char reg)
+unsigned short LM75B::read16(char reg)
 {
     //Create a temporary buffer
     char buff[2];
 
     //Select the register
-    _i2c.write(_addr, &reg, 1);
+    m_I2C.write(m_Addr, &reg, 1);
 
     //Read the 16-bit register
-    _i2c.read(_addr, buff, 2);
+    m_I2C.read(m_Addr, buff, 2);
 
     //Return the combined 16-bit value
     return (buff[0] << 8) | buff[1];
 }
 
-void LM75B::_write16(char reg, unsigned short data)
+void LM75B::write16(char reg, unsigned short data)
 {
     //Create a temporary buffer
     char buff[3];
@@ -231,16 +231,16 @@
     buff[2] = data;
 
     //Write the data
-    _i2c.write(_addr, buff, 3);
+    m_I2C.write(m_Addr, buff, 3);
 }
 
-float LM75B::_readTempHelper(char reg)
+float LM75B::readAlertTempHelper(char reg)
 {
     //Signed return value
     short value;
 
     //Read the 9-bit raw temperature value
-    value = _read16(reg) >> 7;
+    value = read16(reg) >> 7;
 
     //Sign extend negative numbers
     if (value & (1 << 8))
@@ -250,7 +250,7 @@
     return value * 0.5;
 }
 
-void LM75B::_writeTempHelper(char reg, float temp)
+void LM75B::writeAlertTempHelper(char reg, float temp)
 {
     //Range limit temp
     if (temp < -55.0)
@@ -263,5 +263,5 @@
     value <<= 7;
 
     //Send the new value
-    _write16(reg, value);
+    write16(reg, value);
 }
--- a/LM75B.h	Fri Aug 09 22:21:36 2013 +0000
+++ b/LM75B.h	Wed Aug 14 04:33:25 2013 +0000
@@ -19,12 +19,6 @@
 
 #include "mbed.h"
 
-//i2c register definitions
-#define __LM75B_REG_TEMP  0x00
-#define __LM75B_REG_CONF  0x01
-#define __LM75B_REG_THYST 0x02
-#define __LM75B_REG_TOS   0x03
-
 /** LM75B class.
  *  Used for controlling an LM75B temperature sensor connected via I2C.
  *
@@ -33,16 +27,15 @@
  * #include "mbed.h"
  * #include "LM75B.h"
  *
- * Serial pc(USBTX, USBRX);
  * LM75B sensor(p28, p27, LM75B::ADDRESS_0);
  *
  * int main() {
  *     while (1) {
  *         //Read the temperature
- *         float temp = sensor.getTemp();
+ *         float temp = sensor.temp();
  *
  *         //Print the temperature
- *         pc.printf("Temp = %.3f\n", temp);
+ *         printf("Temp = %.3f\n", temp);
  *
  *         //Sleep for 0.5 seconds
  *         wait(0.5);
@@ -108,89 +101,100 @@
      *
      * @returns The current power mode as a PowerMode enum.
      */
-    LM75B::PowerMode getPowerMode(void);
+    LM75B::PowerMode powerMode(void);
 
     /** Set the power mode of the LM75B
      *
      * @param mode The new power mode as a PowerMode enum.
      */
-    void setPowerMode(PowerMode mode);
+    void powerMode(PowerMode mode);
 
     /** Get the current OS pin mode of the LM75B
      *
      * @returns The current OS pin mode as an OSMode enum.
      */
-    LM75B::OSMode getOSMode(void);
+    LM75B::OSMode osMode(void);
 
     /** Set the OS pin mode of the LM75B
      *
      * @param mode The new OS pin mode as an OSMode enum.
      */
-    void setOSMode(OSMode mode);
+    void osMode(OSMode mode);
 
     /** Get the current OS pin polarity of the LM75B
      *
      * @returns The current OS pin polarity as an OSPolarity enum.
      */
-    LM75B::OSPolarity getOSPolarity(void);
+    LM75B::OSPolarity osPolarity(void);
 
     /** Set the OS pin polarity of the LM75B
      *
      * @param polarity The new OS pin polarity as an OSPolarity enum.
      */
-    void setOSPolarity(OSPolarity polarity);
+    void osPolarity(OSPolarity polarity);
 
     /** Get the current OS pin fault queue length of the LM75B
      *
      * @returns The current OS pin fault queue length as an OSFaultQueue enum.
      */
-    LM75B::OSFaultQueue getOSFaultQueue(void);
+    LM75B::OSFaultQueue osFaultQueue(void);
 
     /** Set the OS pin fault queue length of the LM75B
      *
      * @param queue The new OS pin fault queue length as an OSFaultQueue enum.
      */
-    void setOSFaultQueue(OSFaultQueue queue);
+    void osFaultQueue(OSFaultQueue queue);
 
     /** Get the current alert temperature threshold of the LM75B
      *
      * @returns The current alert temperature threshold in °C.
      */
-    float getAlertTemp(void);
+    float alertTemp(void);
 
     /** Set the alert temperature threshold of the LM75B
      *
      * @param temp The new alert temperature threshold in °C.
      */
-    void setAlertTemp(float temp);
+    void alertTemp(float temp);
 
     /** Get the current alert temperature hysteresis threshold of the LM75B
      *
      * @returns The current alert temperature hysteresis threshold in °C.
      */
-    float getAlertHyst(void);
+    float alertHyst(void);
 
     /** Set the alert temperature hysteresis threshold of the LM75B
      *
      * @param temp The new alert temperature hysteresis threshold in °C.
      */
-    void setAlertHyst(float temp);
+    void alertHyst(float temp);
 
     /** Get the current temperature measurement of the LM75B
      *
      * @returns The current temperature measurement in °C.
      */
-    float getTemp(void);
+    float temp(void);
 
 private:
-    I2C _i2c;
-    int _addr;
-    char _read8(char reg);
-    void _write8(char reg, char data);
-    unsigned short _read16(char reg);
-    void _write16(char reg, unsigned short data);
-    float _readTempHelper(char reg);
-    void _writeTempHelper(char reg, float temp);
+    //I2C register addresses
+    enum Register {
+        REG_TEMP    = 0x00,
+        REG_CONF    = 0x01,
+        REG_THYST   = 0x02,
+        REG_TOS     = 0x03
+    };
+
+    //Member variables
+    I2C m_I2C;
+    int m_Addr;
+
+    //Internal functions
+    char read8(char reg);
+    void write8(char reg, char data);
+    unsigned short read16(char reg);
+    void write16(char reg, unsigned short data);
+    float readAlertTempHelper(char reg);
+    void writeAlertTempHelper(char reg, float temp);
 };
 
 #endif