Seeed / Grove_Node

Dependencies:   BLE_API color_pixels mbed-src-nrf51822 nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

unified_driver/analog_thermometer.cpp

Committer:
yihui
Date:
2014-11-06
Revision:
9:84cb66d0375d

File content as of revision 9:84cb66d0375d:


#include "unified_driver.h"
#include "mbed.h"

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

int analog_thermometer_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 analog_thermometer_write(void *obj, void *data)
{
    return 0;
}

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

driver_t analog_thermometer_driver = 
{
    "analog thermometer",
    1,
    analog_thermometer_init,
    analog_thermometer_read,
    analog_thermometer_write,
    analog_thermometer_fini,
};