The firmware of the Grove Node

Dependencies:   BLE_API color_pixels mbed-src-nrf51822 nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

udriver/temperature_sensor.cpp

Committer:
yihui
Date:
2015-06-04
Revision:
11:c0885b74a63a
Parent:
10:f34ff4e47741

File content as of revision 11:c0885b74a63a:


#include "udriver.h"
#include "mbed.h"

int temperature_sensor_init(void *obj, void *params)
{
    int pin = *(int *)params;
    *(AnalogIn **)obj = new AnalogIn((PinName)pin);
    
    return 0;
}
    

int temperature_sensor_read(void *obj, void *data)
{
    const uint32_t beta = 3975;
    float a;
    float temperature;
    float resistance;
    AnalogIn *probe = *(AnalogIn **)obj;
    
    a = probe->read();
    
    /* Calculate the resistance of the thermistor from analog votage read. */
    resistance = (float) 10000.0 * ((1 / a) - 1);
    
    /* Convert the resistance to temperature using Steinhart's Hart equation */
    temperature = (1/((log(resistance/10000.0)/beta) + (1.0/298.15)))-273.15; 
    
    *(float *)data = temperature;
    
    return 0;
}

int temperature_sensor_write(void *obj, void *data)
{
    return 0;
}

int temperature_sensor_fini(void *obj)
{
    AnalogIn *ptr = *(AnalogIn **)obj;
    delete ptr;
    
    return 0;
}

driver_t temperature_sensor_driver = 
{
    .init  = temperature_sensor_init,
    .read  = temperature_sensor_read,
    .write = temperature_sensor_write,
    .fini  = temperature_sensor_fini,
    
    .d     = 1,
};