Penn Electric Racing / Mbed 2 deprecated SystemManagement

Dependencies:   mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP

Fork of SystemManagement by Martin Deng

Revision:
17:c9ce210f6654
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TemperatureRead/TemperatureRead.cpp	Sat Oct 25 03:28:55 2014 +0000
@@ -0,0 +1,30 @@
+#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());
+}
\ No newline at end of file