A library for STMicroelectronics STTS751 I2C temperature sensor
STTS751.cpp@0:4211e78bfa5d, 2014-01-18 (annotated)
- Committer:
- takuo
- Date:
- Sat Jan 18 02:29:05 2014 +0000
- Revision:
- 0:4211e78bfa5d
- Child:
- 1:b0a3645a3dba
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
takuo | 0:4211e78bfa5d | 1 | /* STTS751 (I2C Temperature Sensor) Driver |
takuo | 0:4211e78bfa5d | 2 | * Copyright 2014, Takuo WATANABE (wtakuo) |
takuo | 0:4211e78bfa5d | 3 | * |
takuo | 0:4211e78bfa5d | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
takuo | 0:4211e78bfa5d | 5 | * you may not use this file except in compliance with the License. |
takuo | 0:4211e78bfa5d | 6 | * You may obtain a copy of the License at |
takuo | 0:4211e78bfa5d | 7 | * |
takuo | 0:4211e78bfa5d | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
takuo | 0:4211e78bfa5d | 9 | * |
takuo | 0:4211e78bfa5d | 10 | * Unless required by applicable law or agreed to in writing, software |
takuo | 0:4211e78bfa5d | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
takuo | 0:4211e78bfa5d | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
takuo | 0:4211e78bfa5d | 13 | * See the License for the specific language governing permissions and |
takuo | 0:4211e78bfa5d | 14 | * limitations under the License. |
takuo | 0:4211e78bfa5d | 15 | */ |
takuo | 0:4211e78bfa5d | 16 | |
takuo | 0:4211e78bfa5d | 17 | #include "STTS751.h" |
takuo | 0:4211e78bfa5d | 18 | |
takuo | 0:4211e78bfa5d | 19 | STTS751::STTS751(PinName sda, PinName scl, Address addr, Model model): _i2c(sda, scl), _addr(addr | model) |
takuo | 0:4211e78bfa5d | 20 | { |
takuo | 0:4211e78bfa5d | 21 | init(); |
takuo | 0:4211e78bfa5d | 22 | } |
takuo | 0:4211e78bfa5d | 23 | |
takuo | 0:4211e78bfa5d | 24 | STTS751::STTS751(I2C &i2c, Address addr, Model model): _i2c(i2c), _addr(addr | model) |
takuo | 0:4211e78bfa5d | 25 | { |
takuo | 0:4211e78bfa5d | 26 | init(); |
takuo | 0:4211e78bfa5d | 27 | } |
takuo | 0:4211e78bfa5d | 28 | |
takuo | 0:4211e78bfa5d | 29 | void STTS751::init() |
takuo | 0:4211e78bfa5d | 30 | { |
takuo | 0:4211e78bfa5d | 31 | // TODO: proper initialization of the device |
takuo | 0:4211e78bfa5d | 32 | } |
takuo | 0:4211e78bfa5d | 33 | |
takuo | 0:4211e78bfa5d | 34 | float STTS751::temp() |
takuo | 0:4211e78bfa5d | 35 | { |
takuo | 0:4211e78bfa5d | 36 | signed char h = read8(REG_TEMPERATURE_H); |
takuo | 0:4211e78bfa5d | 37 | unsigned char l = read8(REG_TEMPERATURE_L); |
takuo | 0:4211e78bfa5d | 38 | return ((h << 8) | l) / 256.0; |
takuo | 0:4211e78bfa5d | 39 | } |
takuo | 0:4211e78bfa5d | 40 | |
takuo | 0:4211e78bfa5d | 41 | STTS751::operator float() |
takuo | 0:4211e78bfa5d | 42 | { |
takuo | 0:4211e78bfa5d | 43 | return temp(); |
takuo | 0:4211e78bfa5d | 44 | } |
takuo | 0:4211e78bfa5d | 45 | |
takuo | 0:4211e78bfa5d | 46 | char STTS751::read8(char reg) { |
takuo | 0:4211e78bfa5d | 47 | _i2c.write(_addr, ®, 1, true); |
takuo | 0:4211e78bfa5d | 48 | _i2c.read(_addr, ®, 1); |
takuo | 0:4211e78bfa5d | 49 | return reg; |
takuo | 0:4211e78bfa5d | 50 | } |