Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SX1276Lib AdaFruit_RGBLCD MCP23017 mbed
Fork of AdaFruit_RGBLCD by
Diff: Modules/TempModule.cpp
- Revision:
- 20:93c70a1869ee
- Parent:
- 15:d1eaddb363be
- Child:
- 21:c44cfd3259c0
--- a/Modules/TempModule.cpp Thu Aug 14 12:06:47 2014 +0000
+++ b/Modules/TempModule.cpp Sat Aug 30 16:30:27 2014 +0000
@@ -3,49 +3,91 @@
#include "extra_chars.h"
+#define MCP9808_I2CADDR_DEFAULT 0x30
+#define MCP9808_REG_CONFIG 0x01
+
+#define MCP9808_REG_CONFIG_SHUTDOWN 0x0100
+#define MCP9808_REG_CONFIG_CRITLOCKED 0x0080
+#define MCP9808_REG_CONFIG_WINLOCKED 0x0040
+#define MCP9808_REG_CONFIG_INTCLR 0x0020
+#define MCP9808_REG_CONFIG_ALERTSTAT 0x0010
+#define MCP9808_REG_CONFIG_ALERTCTRL 0x0008
+#define MCP9808_REG_CONFIG_ALERTSEL 0x0002
+#define MCP9808_REG_CONFIG_ALERTPOL 0x0002
+#define MCP9808_REG_CONFIG_ALERTMODE 0x0001
+
+#define MCP9808_REG_UPPER_TEMP 0x02
+#define MCP9808_REG_LOWER_TEMP 0x03
+#define MCP9808_REG_CRIT_TEMP 0x04
+#define MCP9808_REG_AMBIENT_TEMP 0x05
+#define MCP9808_REG_MANUF_ID 0x06
+#define MCP9808_REG_DEVICE_ID 0x07
+
TempModule::TempModule(Serial & in_cDisplay)
: Module(in_cDisplay)
- , m_nTemp(28)
+ , m_cI2C(D3, D6) //I2C_SDA, I2C_SCL)
+ , m_nAddress(MCP9808_I2CADDR_DEFAULT)
{
+ uint16_t nManufacturer = read16(MCP9808_REG_MANUF_ID);// != 0x0054) return false;
+ uint16_t nDeviceId = read16(MCP9808_REG_DEVICE_ID);//
+
+ // Check to see if we need to autoscan for our device
+ bool bAutoScan = (0x0054 != nManufacturer || 0x0400 != nDeviceId);
+ if (bAutoScan) m_nAddress = 0;
+
+ while (bAutoScan && m_nAddress < 0x80)
+ {
+ nManufacturer = read16(MCP9808_REG_MANUF_ID);// != 0x0054) return false;
+ nDeviceId = read16(MCP9808_REG_DEVICE_ID);//
+
+ bAutoScan = (0x0054 != nManufacturer || 0x0400 != nDeviceId);
+ m_nAddress++;
+ }
}
TempModule::~TempModule()
{
}
-void TempModule::change
-(
- size_t in_nIndex,
- bool in_bUp
-)
+float TempModule::readTempC()
{
- enum ETemp
- {
- eTens = 0,
- eSingles = 1,
- };
+ uint16_t t = read16(MCP9808_REG_AMBIENT_TEMP);
+
+ float temp = t & 0x0FFF;
+ temp /= 16.0f;
+
+ // check to see if signed bit set
+ if (t & 0x1000) temp -= 0xff;
- switch (in_nIndex)
- {
- case eTens: m_nTemp += (in_bUp ? 1 : -1) * 10; break;
- case eSingles: m_nTemp += (in_bUp ? 1 : -1); break;
- }
-
- if (m_nTemp > 40) m_nTemp = 40;
- if (m_nTemp < -40) m_nTemp = -40;
+ return temp;
+}
+
+void TempModule::write16(uint8_t in_nRegister, uint16_t in_nValue)
+{
+ char aBuffer[3] = { in_nRegister, in_nValue >> 8, in_nValue & 0xFF };
+ m_cI2C.write(MCP9808_I2CADDR_DEFAULT, aBuffer, sizeof(aBuffer));
}
-int TempModule::getCursorOffset(size_t & inout_nIndex)
+uint16_t TempModule::read16(uint8_t in_nRegister)
{
- const int k_aCursor[] = { 6, 7 };
+ m_cI2C.write(m_nAddress, (const char *)&in_nRegister, sizeof(in_nRegister));
- if ((int)inout_nIndex < 0) inout_nIndex = 0;
- if (inout_nIndex >= _countof(k_aCursor)) inout_nIndex = _countof(k_aCursor) - 1;
-
- return k_aCursor[inout_nIndex];
+ char aReadBuffer[2];
+ m_cI2C.read(m_nAddress, aReadBuffer, sizeof(aReadBuffer));
+
+ return (aReadBuffer[0] << 8) | aReadBuffer[1];
}
-void TempModule::show(bool /*in_bRefresh*/)
+void TempModule::show(bool in_bRefresh)
{
- m_cDisplay.printf("Room: %i%cC ",m_nTemp,eDegree);
+ static time_t nTime = time(NULL) + 1;
+ if (in_bRefresh || nTime < time(NULL))
+ {
+ m_cDisplay.printf("Room: %0.1f%cC ",readTempC(),eDegree);
+
+// uint8_t nValue = read16(MCP9808_REG_MANUF_ID);
+// m_cDisplay.printf("%x - %x - %x ", m_nAddress, nValue,read16(MCP9808_REG_DEVICE_ID));
+
+ nTime = time(NULL) + 1;
+ }
}
