Penn Electric Racing / Mbed 2 deprecated SystemManagement

Dependencies:   mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP

Fork of SystemManagement by Martin Deng

TemperatureRead/TemperatureRead.cpp

Committer:
pspatel321
Date:
2014-10-25
Revision:
17:c9ce210f6654

File content as of revision 17:c9ce210f6654:

#include "TemperatureRead.h"

TemperatureRead::TemperatureRead(PinName pin, struct TempTable _table) : ana(pin) {
    usingAna = true;
    table = _table;
    temperature = 0;
}
TemperatureRead::TemperatureRead(struct TempTable _table)  {
    usingAna = false;   
    table = _table;
    temperature = 0;
}

float TemperatureRead::convert(float in) {
    if (in < table.input[0]) { temperature = -INFINITY; return temperature; }                    // Out of range of the table
    if (in > table.input[table.numEntries-1]) { temperaure = -INFINITY; return temperature; }    // Out of range of the table
    int lowerIndex = 0;
    int upperIndex = table.numEntries-1;
    for (int i = 0; i < table.numEntries; i++) {                  // Converge on the entries that surround the input
        if (in >= table.input[lowerIndex]) { lowerIndex = i; }
        if (in <= table.input[upperIndex]) { upperIndex = table.numEntries-1 - i; }
    }
    // Interpolate and return
    temperature = table.output[lowerIndex] + (table.output[upperIndex] - table.output[lowerIndex]) * ((in - table.input[lowerIndex]) / (table.input[upperIndex] - table.input[lowerIndex]));
    return temperature;
}
float TemperatureRead::read() {
    if (!usingAna) { temperature = 0; return temperature;
    else return convert(ana.read());
}