Justin Howard / Mbed 2 deprecated AdaFruit_RGBLCD

Dependencies:   AdaFruit_RGBLCDShield MCP23017 mbed RTclock

Dependents:   SX1276_GPS

Fork of MCP_test by Wim Huiskamp

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TempModule.cpp Source File

TempModule.cpp

00001 #include "mbed.h"
00002 #include "TempModule.h"
00003 
00004 #include "extra_chars.h"
00005 
00006 #define MCP9808_I2CADDR_DEFAULT        (0x18 << 1)
00007 #define MCP9808_REG_CONFIG             0x01
00008 
00009 #define MCP9808_REG_CONFIG_SHUTDOWN    0x0100
00010 #define MCP9808_REG_CONFIG_CRITLOCKED  0x0080
00011 #define MCP9808_REG_CONFIG_WINLOCKED   0x0040
00012 #define MCP9808_REG_CONFIG_INTCLR      0x0020
00013 #define MCP9808_REG_CONFIG_ALERTSTAT   0x0010
00014 #define MCP9808_REG_CONFIG_ALERTCTRL   0x0008
00015 #define MCP9808_REG_CONFIG_ALERTSEL    0x0002
00016 #define MCP9808_REG_CONFIG_ALERTPOL    0x0002
00017 #define MCP9808_REG_CONFIG_ALERTMODE   0x0001
00018 
00019 #define MCP9808_REG_UPPER_TEMP         0x02
00020 #define MCP9808_REG_LOWER_TEMP         0x03
00021 #define MCP9808_REG_CRIT_TEMP          0x04
00022 #define MCP9808_REG_AMBIENT_TEMP       0x05
00023 #define MCP9808_REG_MANUF_ID           0x06
00024 #define MCP9808_REG_DEVICE_ID          0x07
00025 
00026 TempModule::TempModule(Serial & in_cDisplay,I2C & in_nI2C,uint8_t in_nAddress)
00027     : Module(in_cDisplay)
00028     , m_cI2C(in_nI2C)
00029     , m_nAddress(in_nAddress)
00030     , m_bValid(false)
00031 {
00032     uint16_t nManufacturer = read16(MCP9808_REG_MANUF_ID);// != 0x0054) return false;
00033     uint16_t nDeviceId = read16(MCP9808_REG_DEVICE_ID);//
00034     
00035     // Check to see if we need to autoscan for our device
00036     m_bValid = (0x0054 == nManufacturer || 0x0400 == nDeviceId);
00037 
00038 //    bool bAutoScan = true;
00039 //    m_nAddress = 0;
00040 //    
00041 //    while (bAutoScan && m_nAddress < 0x80)
00042 //    {
00043 //        nManufacturer = read16(MCP9808_REG_MANUF_ID);// != 0x0054) return false;
00044 //        nDeviceId = read16(MCP9808_REG_DEVICE_ID);//
00045 //        
00046 //        bAutoScan = (0x0054 != nManufacturer || 0x0400 != nDeviceId);
00047 //        m_nAddress++;
00048 //    }
00049 }
00050 
00051 TempModule::~TempModule()
00052 {
00053 }
00054 
00055 float TempModule::readTempC()
00056 {
00057     uint16_t t = read16(MCP9808_REG_AMBIENT_TEMP);
00058     
00059     float temp = t & 0x0FFF;
00060     temp /=  16.0f;
00061     
00062     // check to see if signed bit set
00063     if (t & 0x1000) temp -= 0xff;
00064     
00065     return temp;
00066 }
00067 
00068 void TempModule::write16(uint8_t in_nRegister, uint16_t in_nValue)
00069 {
00070     char aBuffer[3] = { in_nRegister, in_nValue >> 8, in_nValue & 0xFF };
00071     m_cI2C.write(MCP9808_I2CADDR_DEFAULT, aBuffer, sizeof(aBuffer));
00072 }
00073 
00074 uint16_t TempModule::read16(uint8_t in_nRegister)
00075 {
00076     m_cI2C.write(m_nAddress, (const char *)&in_nRegister, sizeof(in_nRegister));
00077     
00078     char aReadBuffer[2];
00079     m_cI2C.read(m_nAddress, aReadBuffer, sizeof(aReadBuffer));  
00080     
00081     return (aReadBuffer[0] << 8) | aReadBuffer[1];
00082 }
00083 
00084 void TempModule::show(bool in_bRefresh)
00085 {
00086     static time_t nTime = time(NULL) + 1;
00087     if (in_bRefresh || nTime < time(NULL))
00088     {
00089         m_cDisplay.printf("Room: %0.1f%cC     ",readTempC(),eDegree);
00090         
00091 //        uint8_t nValue = read16(MCP9808_REG_MANUF_ID);        
00092 //        m_cDisplay.printf("%x - %x - %x          ", m_nAddress, nValue,read16(MCP9808_REG_DEVICE_ID));
00093         
00094         nTime = time(NULL) + 1;
00095     }
00096 }