A library for STMicroelectronics STTS751 I2C temperature sensor

Dependents:   STTS751_Demo

Revision:
0:4211e78bfa5d
Child:
1:b0a3645a3dba
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/STTS751.cpp	Sat Jan 18 02:29:05 2014 +0000
@@ -0,0 +1,50 @@
+/* STTS751 (I2C Temperature Sensor) Driver
+ * Copyright 2014, Takuo WATANABE (wtakuo)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "STTS751.h"
+
+STTS751::STTS751(PinName sda, PinName scl, Address addr, Model model): _i2c(sda, scl), _addr(addr | model)
+{
+    init();
+}
+
+STTS751::STTS751(I2C &i2c, Address addr, Model model): _i2c(i2c), _addr(addr | model)
+{
+    init();
+}
+
+void STTS751::init()
+{
+    // TODO: proper initialization of the device
+}
+
+float STTS751::temp()
+{
+    signed char h = read8(REG_TEMPERATURE_H);
+    unsigned char l = read8(REG_TEMPERATURE_L);
+    return ((h << 8) | l) / 256.0;
+}
+
+STTS751::operator float()
+{
+    return temp();
+}
+
+char STTS751::read8(char reg) {
+    _i2c.write(_addr, &reg, 1, true);
+    _i2c.read(_addr, &reg, 1);
+    return reg;
+}