wireless sensor / PHtest

Fork of LinearAnalogSensors by Penn Electric

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LinearTemp.cpp Source File

LinearTemp.cpp

00001 /* Copyright (c) <2012> <P. Patel>, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction, 
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or 
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018  
00019 // --------------------- Median Filtered Linear Temperature Sensor Reader ----------------------------
00020  
00021 #include "LinearTemp.h"
00022 #include "mbed.h"
00023 
00024 // Constructor using two calibration points to define linear multiplier and offset
00025 LinearTemp::LinearTemp(PinName pin, float temp1, unsigned short read1, float temp2, unsigned short read2):_pin(pin) {
00026     _temp = 0.0;                                     // Zero the temperature
00027     _multiplier = (temp2 - temp1) / (read2 - read1); // Calculate multiplier as slope
00028     _offset = temp1 - (_multiplier * read1);         // Calculate offset
00029 }
00030 
00031 // Constructor using user defined multiplier and offset
00032 LinearTemp::LinearTemp(PinName pin, float multiplier, float offset):_pin(pin) {
00033     _temp = 0.0;                                     // Zero the temperature
00034     _multiplier = multiplier;                        // Set multiplier
00035     _offset = offset;                                // Set offset
00036 }
00037 
00038 // Populates an array with temperature readings, sorts, and returns median
00039 float LinearTemp::readTemp() {
00040    /*
00041     _tempArr[0] = _pin.read_u16();
00042     for (int i = 1; i < 9; i++) { 
00043         _tempArr[i] = _pin.read_u16();
00044         unsigned short tmp = _tempArr[i];
00045         int j;            
00046         for (j = i - 1; j >= 0; j--) {
00047             if (tmp>=_tempArr[j]) break;
00048             _tempArr[j + 1] = _tempArr[j];
00049         }
00050         _tempArr[j + 1] = tmp;
00051     }
00052     //_temp = (_tempArr[4] * _multiplier) + _offset;
00053     */
00054     _temp = (float)_pin.read_u16();
00055     _temp = _temp*3/(16*4096);
00056     return _temp;
00057 }
00058 // Returns last calculated temperature value
00059 float LinearTemp::getTemp() {
00060     return _temp;
00061 }