A library for STMicroelectronics STTS751 I2C temperature sensor

Dependents:   STTS751_Demo

STTS751.cpp

Committer:
takuo
Date:
2014-01-18
Revision:
1:b0a3645a3dba
Parent:
0:4211e78bfa5d
Child:
2:3116fe4a0079

File content as of revision 1:b0a3645a3dba:

/* A library for STMicroelectronics STTS751 I2C temperature sensor
 * 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;
}