eka sinambela / Mbed 2 deprecated g3_waterplay

Dependencies:   mbed

Fork of g3_waterplay by Mario Simaremare

TemperatureSensor.cpp

Committer:
mariosimaremare
Date:
2016-06-17
Revision:
5:4cbe44452889
Parent:
3:7c648d1d8802
Child:
7:46e65aeb4df2

File content as of revision 5:4cbe44452889:

/*
* G3: WATERPLAY
*/

#include "TemperatureSensor.h"
#include <algorithm>
#include "mbed.h"

TemperatureSensor::TemperatureSensor(
    Printer &printer,
    PinName pin
):
    _printer(printer),
    _analog_in(pin),
    _reading(0.0),
    _voltage(0.0),
    _temperature(0.0),
    _status(0.0),
    _strStatus("OK")
{
    this->reload();
}

void TemperatureSensor::reload()
{
    double readings[SAMPLING_NUMBER];
    for(int counter = 0; counter < SAMPLING_NUMBER; ++counter){
        readings[counter] = _analog_in.read();
    }
    
    sort(readings, readings + SAMPLING_NUMBER);
    
    this->_reading = readings[SAMPLING_NUMBER / 2];
    this->_voltage = this->_reading * VIN * CONVERTER;
    double RT = (this->_voltage * RESISTANCE) / (VIN - this->_voltage);
    double logRT = log(RT);
    double k0 = K0; 
    double k1 = K1 * logRT;
    double k2 = K2 * pow(logRT, 3.0);
    double kelvin = 1.0 / (k0 + k1 + k2);
    this->_temperature = (kelvin + KELVIN_TO_CELCIUS) + VARIANCE;
    
    this->_status = 0.0;
    this->_strStatus = "OK";
    if(this->_temperature < LOWER_BOUNDARY){
        this->_status = this->_temperature - LOWER_BOUNDARY;
        this->_strStatus = "LW";
    } else if(this->_temperature > UPPER_BOUNDARY){
        this->_status = this->_temperature - UPPER_BOUNDARY;
        this->_strStatus = "HI";
    }
}

double TemperatureSensor::getReading()
{
    return(this->_reading);
}


double TemperatureSensor::getVoltage()
{
    return(this->_voltage);
}

double TemperatureSensor::getTemperature()
{
    return(this->_temperature);
}

double TemperatureSensor::getStatus()
{
    return(this->_status);
}

char* TemperatureSensor::getStrStatus()
{
    return(this->_strStatus);
}